bump version.
[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 $data->tabhead[] = get_string('takensessions', 'attendance');
127 $data->tabhead[] = get_string('points', 'attendance');
128 $data->tabhead[] = get_string('percentage', 'attendance');
129
130 $i = 0;
131 $data->table = array();
132 foreach ($reportdata->users as $user) {
133 if (isset($formdata->ident['id'])) {
134 $data->table[$i][] = $user->id;
135 }
136 if (isset($formdata->ident['uname'])) {
137 $data->table[$i][] = $user->username;
138 }
139
140 $optionalrow = array('idnumber', 'institution', 'department');
141 foreach ($optionalrow as $opt) {
142 if (isset($formdata->ident[$opt])) {
143 $data->table[$i][] = $user->$opt;
144 }
145 }
146
147 $data->table[$i][] = $user->lastname;
148 $data->table[$i][] = $user->firstname;
149 if (!empty($groupmode)) {
150 $grouptext = '';
151 $groupsraw = groups_get_all_groups($course->id, $user->id, 0, 'g.name');
152 $groups = array();
153 foreach ($groupsraw as $group) {
154 $groups[] = $group->name;;
155 }
156 $data->table[$i][] = implode(', ', $groups);
157 }
158 $cellsgenerator = new user_sessions_cells_text_generator($reportdata, $user);
159 $data->table[$i] = array_merge($data->table[$i], $cellsgenerator->get_cells(isset($formdata->includeremarks)));
160
161 $usersummary = $reportdata->summary->get_taken_sessions_summary_for($user->id);
162 $data->table[$i][] = $usersummary->numtakensessions;
163 $data->table[$i][] = format_float($usersummary->takensessionspoints, 1, true, true) . ' / ' .
164 format_float($usersummary->takensessionsmaxpoints, 1, true, true);
165 $data->table[$i][] = format_float($usersummary->takensessionspercentage * 100);
166
167 $i++;
168 }
169
170 if ($formdata->format === 'text') {
171 exporttocsv($data, $filename);
172 } else {
173 exporttotableed($data, $filename, $formdata->format);
174 }
175 exit;
176 } else {
177 print_error('studentsnotfound', 'attendance', $att->url_manage());
178 }
179 }
180
181 $output = $PAGE->get_renderer('mod_attendance');
182 $tabs = new attendance_tabs($att, attendance_tabs::TAB_EXPORT);
183 echo $output->header();
184 echo $output->heading(get_string('attendanceforthecourse', 'attendance').' :: ' .format_string($course->fullname));
185 echo $output->render($tabs);
186
187 $mform->display();
188
189 echo $OUTPUT->footer();
190
191
192 function exporttotableed($data, $filename, $format) {
193 global $CFG;
194
195 if ($format === 'excel') {
196 require_once("$CFG->libdir/excellib.class.php");
197 $filename .= ".xls";
198 $workbook = new MoodleExcelWorkbook("-");
199 } else {
200 require_once("$CFG->libdir/odslib.class.php");
201 $filename .= ".ods";
202 $workbook = new MoodleODSWorkbook("-");
203 }
204 // Sending HTTP headers.
205 $workbook->send($filename);
206 // Creating the first worksheet.
207 $myxls = $workbook->add_worksheet('Attendances');
208 // Format types.
209 $formatbc = $workbook->add_format();
210 $formatbc->set_bold(1);
211
212 $myxls->write(0, 0, get_string('course'), $formatbc);
213 $myxls->write(0, 1, $data->course);
214 $myxls->write(1, 0, get_string('group'), $formatbc);
215 $myxls->write(1, 1, $data->group);
216
217 $i = 3;
218 $j = 0;
219 foreach ($data->tabhead as $cell) {
220 // Merge cells if the heading would be empty (remarks column).
221 if (empty($cell)) {
222 $myxls->merge_cells($i, $j - 1, $i, $j);
223 } else {
224 $myxls->write($i, $j, $cell, $formatbc);
225 }
226 $j++;
227 }
228 $i++;
229 $j = 0;
230 foreach ($data->table as $row) {
231 foreach ($row as $cell) {
232 $myxls->write($i, $j++, $cell);
233 }
234 $i++;
235 $j = 0;
236 }
237 $workbook->close();
238 }
239
240 function exporttocsv($data, $filename) {
241 $filename .= ".txt";
242
243 header("Content-Type: application/download\n");
244 header("Content-Disposition: attachment; filename=\"$filename\"");
245 header("Expires: 0");
246 header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
247 header("Pragma: public");
248
249 echo get_string('course')."\t".$data->course."\n";
250 echo get_string('group')."\t".$data->group."\n\n";
251
252 echo implode("\t", $data->tabhead)."\n";
253 foreach ($data->table as $row) {
254 echo implode("\t", $row)."\n";
255 }
256 }