Implement data privacy provider.
[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->navbar->add(get_string('export', 'attendance'));
49
50 $formparams = array('course' => $course, 'cm' => $cm, 'modcontext' => $context);
51 $mform = new mod_attendance_export_form($att->url_export(), $formparams);
52
53 if ($formdata = $mform->get_data()) {
54
55 $pageparams = new mod_attendance_page_with_filter_controls();
56 $pageparams->init($cm);
57 $pageparams->page = 0;
58 $pageparams->group = $formdata->group;
59 $pageparams->set_current_sesstype($formdata->group ? $formdata->group : mod_attendance_page_with_filter_controls::SESSTYPE_ALL);
60 if (isset($formdata->includeallsessions)) {
61 if (isset($formdata->includenottaken)) {
62 $pageparams->view = ATT_VIEW_ALL;
63 } else {
64 $pageparams->view = ATT_VIEW_ALLPAST;
65 $pageparams->curdate = time();
66 }
67 $pageparams->init_start_end_date();
68 } else {
69 $pageparams->startdate = $formdata->sessionstartdate;
70 $pageparams->enddate = $formdata->sessionenddate;
71 }
72 if ($formdata->selectedusers) {
73 $pageparams->userids = $formdata->users;
74 }
75 $att->pageparams = $pageparams;
76
77 $reportdata = new attendance_report_data($att);
78 if ($reportdata->users) {
79 $filename = clean_filename($course->shortname.'_Attendances_'.userdate(time(), '%Y%m%d-%H%M'));
80
81 $group = $formdata->group ? $reportdata->groups[$formdata->group] : 0;
82 $data = new stdClass;
83 $data->tabhead = array();
84 $data->course = $att->course->fullname;
85 $data->group = $group ? $group->name : get_string('allparticipants');
86
87 if (isset($formdata->ident['id'])) {
88 $data->tabhead[] = get_string('studentid', 'attendance');
89 }
90 if (isset($formdata->ident['uname'])) {
91 $data->tabhead[] = get_string('username');
92 }
93
94 $optional = array('idnumber', 'institution', 'department');
95 foreach ($optional as $opt) {
96 if (isset($formdata->ident[$opt])) {
97 $data->tabhead[] = get_string($opt);
98 }
99 }
100
101 $data->tabhead[] = get_string('lastname');
102 $data->tabhead[] = get_string('firstname');
103 $groupmode = groups_get_activity_groupmode($cm, $course);
104 if (!empty($groupmode)) {
105 $data->tabhead[] = get_string('groups');
106 }
107
108 if (count($reportdata->sessions) > 0) {
109 foreach ($reportdata->sessions as $sess) {
110 $text = userdate($sess->sessdate, get_string('strftimedmyhm', 'attendance'));
111 $text .= ' ';
112 if (!empty($sess->groupid) && empty($reportdata->groups[$sess->groupid])) {
113 $text .= get_string('deletedgroup', 'attendance');
114 } else {
115 $text .= $sess->groupid ? $reportdata->groups[$sess->groupid]->name : get_string('commonsession', 'attendance');
116 }
117 $data->tabhead[] = $text;
118 if (isset($formdata->includeremarks)) {
119 $data->tabhead[] = ''; // Space for the remarks.
120 }
121 }
122 } else {
123 print_error('sessionsnotfound', 'attendance', $att->url_manage());
124 }
125
126 $setnumber = -1;
127 foreach ($reportdata->statuses as $sts) {
128 if ($sts->setnumber != $setnumber) {
129 $setnumber = $sts->setnumber;
130 }
131
132 $data->tabhead[] = $sts->acronym;
133 }
134
135 $data->tabhead[] = get_string('takensessions', 'attendance');
136 $data->tabhead[] = get_string('points', 'attendance');
137 $data->tabhead[] = get_string('percentage', 'attendance');
138
139 $i = 0;
140 $data->table = array();
141 foreach ($reportdata->users as $user) {
142 if (isset($formdata->ident['id'])) {
143 $data->table[$i][] = $user->id;
144 }
145 if (isset($formdata->ident['uname'])) {
146 $data->table[$i][] = $user->username;
147 }
148
149 $optionalrow = array('idnumber', 'institution', 'department');
150 foreach ($optionalrow as $opt) {
151 if (isset($formdata->ident[$opt])) {
152 $data->table[$i][] = $user->$opt;
153 }
154 }
155
156 $data->table[$i][] = $user->lastname;
157 $data->table[$i][] = $user->firstname;
158 if (!empty($groupmode)) {
159 $grouptext = '';
160 $groupsraw = groups_get_all_groups($course->id, $user->id, 0, 'g.name');
161 $groups = array();
162 foreach ($groupsraw as $group) {
163 $groups[] = $group->name;;
164 }
165 $data->table[$i][] = implode(', ', $groups);
166 }
167 $cellsgenerator = new user_sessions_cells_text_generator($reportdata, $user);
168 $data->table[$i] = array_merge($data->table[$i], $cellsgenerator->get_cells(isset($formdata->includeremarks)));
169
170 $usersummary = $reportdata->summary->get_taken_sessions_summary_for($user->id);
171
172 foreach ($reportdata->statuses as $sts) {
173 if (isset($usersummary->userstakensessionsbyacronym[$sts->setnumber][$sts->acronym])) {
174 $data->table[$i][] = $usersummary->userstakensessionsbyacronym[$sts->setnumber][$sts->acronym];
175 } else {
176 $data->table[$i][] = 0;
177 }
178 }
179
180 $data->table[$i][] = $usersummary->numtakensessions;
181 $data->table[$i][] = format_float($usersummary->takensessionspoints, 1, true, true) . ' / ' .
182 format_float($usersummary->takensessionsmaxpoints, 1, true, true);
183 $data->table[$i][] = format_float($usersummary->takensessionspercentage * 100);
184
185 $i++;
186 }
187
188 if ($formdata->format === 'text') {
189 attendance_exporttocsv($data, $filename);
190 } else {
191 attendance_exporttotableed($data, $filename, $formdata->format);
192 }
193 exit;
194 } else {
195 print_error('studentsnotfound', 'attendance', $att->url_manage());
196 }
197 }
198
199 $output = $PAGE->get_renderer('mod_attendance');
200 $tabs = new attendance_tabs($att, attendance_tabs::TAB_EXPORT);
201 echo $output->header();
202 echo $output->heading(get_string('attendanceforthecourse', 'attendance').' :: ' .format_string($course->fullname));
203 echo $output->render($tabs);
204
205 $mform->display();
206
207 echo $OUTPUT->footer();
208
209
210