Merge pull request #83 from barrysspace/expired_self_enrolments_MDL26
[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 $att = new attendance($att, $cm, $course, $PAGE->context);
40
41 $att->perm->require_export_capability();
42
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'));
49
50 $formparams = array('course' => $course, 'cm' => $cm, 'modcontext' => $PAGE->context);
51 $mform = new mod_attendance_export_form($att->url_export(), $formparams);
52
53 if ($mform->is_submitted()) {
54 $formdata = $mform->get_data();
55
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;
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 $att->pageparams = $pageparams;
74
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'));
78
79 $group = $formdata->group ? $reportdata->groups[$formdata->group] : 0;
80 $data = new stdClass;
81 $data->tabhead = array();
82 $data->course = $att->course->fullname;
83 $data->group = $group ? $group->name : get_string('allparticipants');
84
85 if (isset($formdata->ident['id'])) {
86 $data->tabhead[] = get_string('studentid', 'attendance');
87 }
88 if (isset($formdata->ident['uname'])) {
89 $data->tabhead[] = get_string('username');
90 }
91
92 $optional = array('idnumber', 'institution', 'department');
93 foreach ($optional as $opt) {
94 if (isset($formdata->ident[$opt])) {
95 $data->tabhead[] = get_string($opt);
96 }
97 }
98
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');
104 }
105
106 if (count($reportdata->sessions) > 0) {
107 foreach ($reportdata->sessions as $sess) {
108 $text = userdate($sess->sessdate, get_string('strftimedmyhm', 'attendance'));
109 $text .= ' ';
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);
114 }
115 }
116 } else {
117 print_error('sessionsnotfound', 'attendance', $att->url_manage());
118 }
119 if ($reportdata->gradable) {
120 $data->tabhead[] = get_string('grade');
121 }
122
123 $i = 0;
124 $data->table = array();
125 foreach ($reportdata->users as $user) {
126 if (isset($formdata->ident['id'])) {
127 $data->table[$i][] = $user->id;
128 }
129 if (isset($formdata->ident['uname'])) {
130 $data->table[$i][] = $user->username;
131 }
132
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;
137 }
138 }
139
140 $data->table[$i][] = $user->lastname;
141 $data->table[$i][] = $user->firstname;
142 if (!empty($groupmode)) {
143 $grouptext = '';
144 $groupsraw = groups_get_all_groups($course->id, $user->id, 0, 'g.name');
145 $groups = array();
146 foreach ($groupsraw as $group) {
147 $groups[] = $group->name;;
148 }
149 $data->table[$i][] = implode(', ', $groups);
150 }
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];
155 }
156 $i++;
157 }
158
159 if ($formdata->format === 'text') {
160 exporttocsv($data, $filename);
161 } else {
162 exporttotableed($data, $filename, $formdata->format);
163 }
164 exit;
165 } else {
166 print_error('studentsnotfound', 'attendance', $att->url_manage());
167 }
168 }
169
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);
175
176 $mform->display();
177
178 echo $OUTPUT->footer();
179
180
181 function exporttotableed($data, $filename, $format) {
182 global $CFG;
183
184 if ($format === 'excel') {
185 require_once("$CFG->libdir/excellib.class.php");
186 $filename .= ".xls";
187 $workbook = new MoodleExcelWorkbook("-");
188 } else {
189 require_once("$CFG->libdir/odslib.class.php");
190 $filename .= ".ods";
191 $workbook = new MoodleODSWorkbook("-");
192 }
193 // Sending HTTP headers.
194 $workbook->send($filename);
195 // Creating the first worksheet.
196 $myxls = $workbook->add_worksheet('Attendances');
197 // Format types.
198 $formatbc = $workbook->add_format();
199 $formatbc->set_bold(1);
200
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);
205
206 $i = 3;
207 $j = 0;
208 foreach ($data->tabhead as $cell) {
209 $myxls->write($i, $j++, $cell, $formatbc);
210 }
211 $i++;
212 $j = 0;
213 foreach ($data->table as $row) {
214 foreach ($row as $cell) {
215 $myxls->write($i, $j++, $cell);
216 }
217 $i++;
218 $j = 0;
219 }
220 $workbook->close();
221 }
222
223 function exporttocsv($data, $filename) {
224 $filename .= ".txt";
225
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");
231
232 echo get_string('course')."\t".$data->course."\n";
233 echo get_string('group')."\t".$data->group."\n\n";
234
235 echo implode("\t", $data->tabhead)."\n";
236 foreach ($data->table as $row) {
237 echo implode("\t", $row)."\n";
238 }
239 }