2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Export attendance sessions
20 * @package mod_attendance
21 * @copyright 2011 Artem Andreev <andreev.artem@gmail.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once(dirname(__FILE__
).'/../../config.php');
26 require_once(dirname(__FILE__
).'/locallib.php');
27 require_once(dirname(__FILE__
).'/export_form.php');
28 require_once(dirname(__FILE__
).'/renderables.php');
29 require_once(dirname(__FILE__
).'/renderhelpers.php');
31 $id = required_param('id', PARAM_INT
);
33 $cm = get_coursemodule_from_id('attendance', $id, 0, false
, MUST_EXIST
);
34 $course = $DB->get_record('course', array('id' => $cm->course
), '*', MUST_EXIST
);
35 $att = $DB->get_record('attendance', array('id' => $cm->instance
), '*', MUST_EXIST
);
37 require_login($course, true
, $cm);
39 $att = new attendance($att, $cm, $course, $PAGE->context
);
41 $att->perm
->require_export_capability();
43 $PAGE->set_url($att->url_export());
44 $PAGE->set_title($course->shortname
. ": ".$att->name
);
45 $PAGE->set_heading($course->fullname
);
46 $PAGE->set_cacheable(true
);
47 $PAGE->set_button($OUTPUT->update_module_button($cm->id
, 'attendance'));
48 $PAGE->navbar
->add(get_string('export', 'quiz'));
50 $formparams = array('course' => $course, 'cm' => $cm, 'modcontext' => $PAGE->context
);
51 $mform = new mod_attendance_export_form($att->url_export(), $formparams);
53 if ($mform->is_submitted()) {
54 $formdata = $mform->get_data();
56 $pageparams = new att_page_with_filter_controls();
57 $pageparams->init($cm);
58 $pageparams->page
= 0;
59 $pageparams->group
= $formdata->group
;
60 $pageparams->set_current_sesstype($formdata->group ?
$formdata->group
: att_page_with_filter_controls
::SESSTYPE_ALL
);
61 if (isset($formdata->includeallsessions
)) {
62 if (isset($formdata->includenottaken
)) {
63 $pageparams->view
= ATT_VIEW_ALL
;
65 $pageparams->view
= ATT_VIEW_ALLPAST
;
66 $pageparams->curdate
= time();
68 $pageparams->init_start_end_date();
70 $pageparams->startdate
= $formdata->sessionstartdate
;
71 $pageparams->enddate
= $formdata->sessionenddate
;
73 $att->pageparams
= $pageparams;
75 $reportdata = new attendance_report_data($att);
76 if ($reportdata->users
) {
77 $filename = clean_filename($course->shortname
.'_Attendances_'.userdate(time(), '%Y%m%d-%H%M'));
79 $group = $formdata->group ?
$reportdata->groups
[$formdata->group
] : 0;
81 $data->tabhead
= array();
82 $data->course
= $att->course
->fullname
;
83 $data->group
= $group ?
$group->name
: get_string('allparticipants');
85 if (isset($formdata->ident
['id'])) {
86 $data->tabhead
[] = get_string('studentid', 'attendance');
88 if (isset($formdata->ident
['uname'])) {
89 $data->tabhead
[] = get_string('username');
92 $optional = array('idnumber', 'institution', 'department');
93 foreach ($optional as $opt) {
94 if (isset($formdata->ident
[$opt])) {
95 $data->tabhead
[] = get_string($opt);
99 $data->tabhead
[] = get_string('lastname');
100 $data->tabhead
[] = get_string('firstname');
101 $groupmode = groups_get_activity_groupmode($cm, $course);
102 if (!empty($groupmode)) {
103 $data->tabhead
[] = get_string('groups');
106 if (count($reportdata->sessions
) > 0) {
107 foreach ($reportdata->sessions
as $sess) {
108 $text = userdate($sess->sessdate
, get_string('strftimedmyhm', 'attendance'));
110 $text .= $sess->groupid ?
$reportdata->groups
[$sess->groupid
]->name
: get_string('commonsession', 'attendance');
111 $data->tabhead
[] = $text;
112 if (isset($formdata->includeremarks
)) {
113 $data->tabhead
[] = get_string('remark', 'attendance', $text);
117 print_error('sessionsnotfound', 'attendance', $att->url_manage());
119 if ($reportdata->gradable
) {
120 $data->tabhead
[] = get_string('grade');
124 $data->table
= array();
125 foreach ($reportdata->users
as $user) {
126 if (isset($formdata->ident
['id'])) {
127 $data->table
[$i][] = $user->id
;
129 if (isset($formdata->ident
['uname'])) {
130 $data->table
[$i][] = $user->username
;
133 $optional_row = array('idnumber', 'institution', 'department');
134 foreach ($optional_row as $opt) {
135 if (isset($formdata->ident
[$opt])) {
136 $data->table
[$i][] = $user->$opt;
140 $data->table
[$i][] = $user->lastname
;
141 $data->table
[$i][] = $user->firstname
;
142 if (!empty($groupmode)) {
144 $groupsraw = groups_get_all_groups($course->id
, $user->id
, 0, 'g.name');
146 foreach ($groupsraw as $group) {
147 $groups[] = $group->name
;;
149 $data->table
[$i][] = implode(', ', $groups);
151 $cellsgenerator = new user_sessions_cells_text_generator($reportdata, $user);
152 $data->table
[$i] = array_merge($data->table
[$i], $cellsgenerator->get_cells(isset($formdata->includeremarks
)));
153 if ($reportdata->gradable
) {
154 $data->table
[$i][] = $reportdata->grades
[$user->id
].' / '.$reportdata->maxgrades
[$user->id
];
159 if ($formdata->format
=== 'text') {
160 exporttocsv($data, $filename);
162 exporttotableed($data, $filename, $formdata->format
);
166 print_error('studentsnotfound', 'attendance', $att->url_manage());
170 $output = $PAGE->get_renderer('mod_attendance');
171 $tabs = new attendance_tabs($att, attendance_tabs
::TAB_EXPORT
);
172 echo $output->header();
173 echo $output->heading(get_string('attendanceforthecourse', 'attendance').' :: ' .$course->fullname
);
174 echo $output->render($tabs);
178 echo $OUTPUT->footer();
181 function exporttotableed($data, $filename, $format) {
184 if ($format === 'excel') {
185 require_once("$CFG->libdir/excellib.class.php");
187 $workbook = new MoodleExcelWorkbook("-");
189 require_once("$CFG->libdir/odslib.class.php");
191 $workbook = new MoodleODSWorkbook("-");
193 // Sending HTTP headers.
194 $workbook->send($filename);
195 // Creating the first worksheet.
196 $myxls = $workbook->add_worksheet('Attendances');
198 $formatbc = $workbook->add_format();
199 $formatbc->set_bold(1);
201 $myxls->write(0, 0, get_string('course'), $formatbc);
202 $myxls->write(0, 1, $data->course
);
203 $myxls->write(1, 0, get_string('group'), $formatbc);
204 $myxls->write(1, 1, $data->group
);
208 foreach ($data->tabhead
as $cell) {
209 $myxls->write($i, $j++
, $cell, $formatbc);
213 foreach ($data->table
as $row) {
214 foreach ($row as $cell) {
215 $myxls->write($i, $j++
, $cell);
223 function exporttocsv($data, $filename) {
226 header("Content-Type: application/download\n");
227 header("Content-Disposition: attachment; filename=\"$filename\"");
228 header("Expires: 0");
229 header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
230 header("Pragma: public");
232 echo get_string('course')."\t".$data->course
."\n";
233 echo get_string('group')."\t".$data->group
."\n\n";
235 echo implode("\t", $data->tabhead
)."\n";
236 foreach ($data->table
as $row) {
237 echo implode("\t", $row)."\n";