Fixes #223 allow student attendance to be disabled at site-level
[moodle-mod_attendance.git] / export.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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.
13 //
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/>.
16
17 /**
18 * Export attendance sessions
19 *
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
23 */
24
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');
30
31 $id = required_param('id', PARAM_INT);
32
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);
36
37 require_login($course, true, $cm);
38
39 $context = context_module::instance($cm->id);
40 require_capability('mod/attendance:export', $context);
41
42 $att = new mod_attendance_structure($att, $cm, $course, $context);
43
44 $PAGE->set_url($att->url_export());
45 $PAGE->set_title($course->shortname. ": ".$att->name);
46 $PAGE->set_heading($course->fullname);
47 $PAGE->set_cacheable(true);
48 $PAGE->set_button($OUTPUT->update_module_button($cm->id, 'attendance'));
49 $PAGE->navbar->add(get_string('export', 'attendance'));
50
51 $formparams = array('course' => $course, 'cm' => $cm, 'modcontext' => $context);
52 $mform = new mod_attendance_export_form($att->url_export(), $formparams);
53
54 if ($formdata = $mform->get_data()) {
55
56 $pageparams = new mod_attendance_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 : mod_attendance_page_with_filter_controls::SESSTYPE_ALL);
61 if (isset($formdata->includeallsessions)) {
62 if (isset($formdata->includenottaken)) {
63 $pageparams->view = ATT_VIEW_ALL;
64 } else {
65 $pageparams->view = ATT_VIEW_ALLPAST;
66 $pageparams->curdate = time();
67 }
68 $pageparams->init_start_end_date();
69 } else {
70 $pageparams->startdate = $formdata->sessionstartdate;
71 $pageparams->enddate = $formdata->sessionenddate;
72 }
73 if ($formdata->selectedusers) {
74 $pageparams->userids = $formdata->users;
75 }
76 $att->pageparams = $pageparams;
77
78 $reportdata = new attendance_report_data($att);
79 if ($reportdata->users) {
80 $filename = clean_filename($course->shortname.'_Attendances_'.userdate(time(), '%Y%m%d-%H%M'));
81
82 $group = $formdata->group ? $reportdata->groups[$formdata->group] : 0;
83 $data = new stdClass;
84 $data->tabhead = array();
85 $data->course = $att->course->fullname;
86 $data->group = $group ? $group->name : get_string('allparticipants');
87
88 if (isset($formdata->ident['id'])) {
89 $data->tabhead[] = get_string('studentid', 'attendance');
90 }
91 if (isset($formdata->ident['uname'])) {
92 $data->tabhead[] = get_string('username');
93 }
94
95 $optional = array('idnumber', 'institution', 'department');
96 foreach ($optional as $opt) {
97 if (isset($formdata->ident[$opt])) {
98 $data->tabhead[] = get_string($opt);
99 }
100 }
101
102 $data->tabhead[] = get_string('lastname');
103 $data->tabhead[] = get_string('firstname');
104 $groupmode = groups_get_activity_groupmode($cm, $course);
105 if (!empty($groupmode)) {
106 $data->tabhead[] = get_string('groups');
107 }
108
109 if (count($reportdata->sessions) > 0) {
110 foreach ($reportdata->sessions as $sess) {
111 $text = userdate($sess->sessdate, get_string('strftimedmyhm', 'attendance'));
112 $text .= ' ';
113 if (!empty($sess->groupid) && empty($reportdata->groups[$sess->groupid])) {
114 $text .= get_string('deletedgroup', 'attendance');
115 } else {
116 $text .= $sess->groupid ? $reportdata->groups[$sess->groupid]->name : get_string('commonsession', 'attendance');
117 }
118 $data->tabhead[] = $text;
119 if (isset($formdata->includeremarks)) {
120 $data->tabhead[] = ''; // Space for the remarks.
121 }
122 }
123 } else {
124 print_error('sessionsnotfound', 'attendance', $att->url_manage());
125 }
126 if ($reportdata->gradable) {
127 $data->tabhead[] = get_string('grade');
128 $data->tabhead[] = get_string('percentage', 'attendance');
129 }
130
131 $i = 0;
132 $data->table = array();
133 foreach ($reportdata->users as $user) {
134 if (isset($formdata->ident['id'])) {
135 $data->table[$i][] = $user->id;
136 }
137 if (isset($formdata->ident['uname'])) {
138 $data->table[$i][] = $user->username;
139 }
140
141 $optionalrow = array('idnumber', 'institution', 'department');
142 foreach ($optionalrow as $opt) {
143 if (isset($formdata->ident[$opt])) {
144 $data->table[$i][] = $user->$opt;
145 }
146 }
147
148 $data->table[$i][] = $user->lastname;
149 $data->table[$i][] = $user->firstname;
150 if (!empty($groupmode)) {
151 $grouptext = '';
152 $groupsraw = groups_get_all_groups($course->id, $user->id, 0, 'g.name');
153 $groups = array();
154 foreach ($groupsraw as $group) {
155 $groups[] = $group->name;;
156 }
157 $data->table[$i][] = implode(', ', $groups);
158 }
159 $cellsgenerator = new user_sessions_cells_text_generator($reportdata, $user);
160 $data->table[$i] = array_merge($data->table[$i], $cellsgenerator->get_cells(isset($formdata->includeremarks)));
161 if ($reportdata->gradable) {
162 $data->table[$i][] = format_float($reportdata->grades[$user->id]).' / '.
163 format_float($reportdata->maxgrades[$user->id]);
164 if ($reportdata->maxgrades[$user->id]) {
165 $percent = $reportdata->grades[$user->id] * 100.0 / $reportdata->maxgrades[$user->id];
166 } else {
167 $percent = 0.0;
168 }
169 $data->table[$i][] = $percent;
170 }
171 $i++;
172 }
173
174 if ($formdata->format === 'text') {
175 exporttocsv($data, $filename);
176 } else {
177 exporttotableed($data, $filename, $formdata->format);
178 }
179 exit;
180 } else {
181 print_error('studentsnotfound', 'attendance', $att->url_manage());
182 }
183 }
184
185 $output = $PAGE->get_renderer('mod_attendance');
186 $tabs = new attendance_tabs($att, attendance_tabs::TAB_EXPORT);
187 echo $output->header();
188 echo $output->heading(get_string('attendanceforthecourse', 'attendance').' :: ' .format_string($course->fullname));
189 echo $output->render($tabs);
190
191 $mform->display();
192
193 echo $OUTPUT->footer();
194
195
196 function exporttotableed($data, $filename, $format) {
197 global $CFG;
198
199 if ($format === 'excel') {
200 require_once("$CFG->libdir/excellib.class.php");
201 $filename .= ".xls";
202 $workbook = new MoodleExcelWorkbook("-");
203 } else {
204 require_once("$CFG->libdir/odslib.class.php");
205 $filename .= ".ods";
206 $workbook = new MoodleODSWorkbook("-");
207 }
208 // Sending HTTP headers.
209 $workbook->send($filename);
210 // Creating the first worksheet.
211 $myxls = $workbook->add_worksheet('Attendances');
212 // Format types.
213 $formatbc = $workbook->add_format();
214 $formatbc->set_bold(1);
215
216 $myxls->write(0, 0, get_string('course'), $formatbc);
217 $myxls->write(0, 1, $data->course);
218 $myxls->write(1, 0, get_string('group'), $formatbc);
219 $myxls->write(1, 1, $data->group);
220
221 $i = 3;
222 $j = 0;
223 foreach ($data->tabhead as $cell) {
224 // Merge cells if the heading would be empty (remarks column).
225 if (empty($cell)) {
226 $myxls->merge_cells($i, $j - 1, $i, $j);
227 } else {
228 $myxls->write($i, $j, $cell, $formatbc);
229 }
230 $j++;
231 }
232 $i++;
233 $j = 0;
234 foreach ($data->table as $row) {
235 foreach ($row as $cell) {
236 $myxls->write($i, $j++, $cell);
237 }
238 $i++;
239 $j = 0;
240 }
241 $workbook->close();
242 }
243
244 function exporttocsv($data, $filename) {
245 $filename .= ".txt";
246
247 header("Content-Type: application/download\n");
248 header("Content-Disposition: attachment; filename=\"$filename\"");
249 header("Expires: 0");
250 header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
251 header("Pragma: public");
252
253 echo get_string('course')."\t".$data->course."\n";
254 echo get_string('group')."\t".$data->group."\n\n";
255
256 echo implode("\t", $data->tabhead)."\n";
257 foreach ($data->table as $row) {
258 echo implode("\t", $row)."\n";
259 }
260 }