-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathexport.php
More file actions
421 lines (386 loc) · 14.2 KB
/
Copy pathexport.php
File metadata and controls
421 lines (386 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Run the export
*
* @package gradeexport_checklist
* @copyright 2010 Davo Smith
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// Note - to adjust the user columns included in the report, edit 'columns.php'.
require_once(__DIR__ . '/../../../config.php');
require_once($CFG->dirroot . '/mod/checklist/lib.php'); // For CHECKLIST_* definitions.
require_once($CFG->dirroot . '/grade/export/lib.php');
require_once($CFG->dirroot . '/lib/excellib.class.php');
$courseid = required_param('id', PARAM_INT);
$district = optional_param('choosedistrict', false, PARAM_TEXT);
$checklistid = required_param('choosechecklist', PARAM_INT);
$group = optional_param('group', 0, PARAM_INT);
$exportoptional = optional_param('exportoptional', false, PARAM_BOOL);
$percentcol = optional_param('percentcol', false, PARAM_BOOL);
$percentrow = optional_param('percentrow', false, PARAM_BOOL);
$percentheadings = optional_param('percentheadings', false, PARAM_BOOL);
$course = $DB->get_record('course', ['id' => $courseid], '*', MUST_EXIST);
require_login($course->id);
$context = context_course::instance($course->id);
require_capability('gradeexport/checklist:view', $context);
$viewall = has_capability('gradeexport/checklist:viewall', $context);
$viewdistrict = has_capability('gradeexport/checklist:viewdistrict', $context);
if (!$viewall && (!$viewdistrict || !$district)) {
throw new moodle_exception('nopermission', 'gradeexport_checklist');
}
if (!$viewall) {
$sql = "SELECT ud.data AS district FROM {user_info_data} ud, {user_info_field} uf ";
$sql .= "WHERE ud.fieldid = uf.id AND uf.shortname = 'district' AND ud.userid = ?";
$mydistrict = $DB->get_record_sql($sql, [$USER->id]);
if ($district != $mydistrict->district) {
throw new moodle_exception('wrongdistrict', 'gradeexport_checklist');
}
}
if ($group) {
if ($course->groupmode != VISIBLEGROUPS && !has_capability('moodle/site:accessallgroups', $context)) {
if (!groups_is_member($group)) {
throw new moodle_exception('wronggroup', 'gradeexport_checklist');
}
}
} else {
if ($course->groupmode != VISIBLEGROUPS && !has_capability('moodle/site:accessallgroups', $context)) {
$group = groups_get_all_groups($course->id, $USER->id);
if ($group) {
$group = array_keys($group);
}
}
}
if (!$checklist = $DB->get_record('checklist', ['id' => $checklistid])) {
throw new moodle_exception('checklistnotfound', 'gradeexport_checklist');
}
$studentmarking = ($checklist->teacheredit != CHECKLIST_MARKING_TEACHER);
$teachermarking = ($checklist->teacheredit != CHECKLIST_MARKING_STUDENT);
$strchecklistreport = get_string('checklistreport', 'gradeexport_checklist');
$users = get_users_by_capability(
$context,
'mod/checklist:updateown',
'u.*',
'u.firstname, u.lastname',
'',
'',
$group,
false
);
if ($district && $district !== 'ALL' && $users) {
[$usql, $uparam] = $DB->get_in_or_equal(array_keys($users));
$sql = "
SELECT u.*
FROM ({user} u
JOIN {user_info_data} ud ON u.id = ud.userid)
JOIN {user_info_field} uf ON ud.fieldid = uf.id
WHERE u.id $usql AND uf.shortname = 'district' AND ud.data = ?";
$users = $DB->get_records_sql($sql, array_merge($uparam, [$district]));
}
if (!$users) {
throw new moodle_exception('nousers', 'gradeexport_checklist');
}
require_once($CFG->dirroot . '/grade/export/checklist/columns.php');
if (!$percentcol) {
unset($checklistexportusercolumns['_percent']);
}
// Useful for debugging.
if (defined('BEHAT_SITE_RUNNING')) {
/**
* Class FakeMoodleExcelWorkbook
*/
class FakeMoodleExcelWorkbook {
/**
* FakeMoodleExcelWorkbook constructor.
* @param mixed $ignore
*/
public function __construct($ignore) {
}
/**
* Send the finished spreadsheet
* @param mixed $ignore
*/
public function send($ignore) {
}
/**
* Write a string to the spreadsheet
* @param int $row
* @param int $col
* @param string $data
*/
public function write_string($row, $col, $data) {
echo "($row, $col) = $data<br/>";
}
/**
* Write a number to the spreadsheet
* @param int $row
* @param int $col
* @param string $data
*/
public function write_number($row, $col, $data) {
echo "($row, $col) = $data<br/>";
}
/**
* Add a worksheet to the workbook
* @param mixed $ignore
* @return FakeMoodleExcelWorkbook
*/
public function add_worksheet($ignore) {
return new FakeMoodleExcelWorkbook($ignore);
}
/**
* Close the workbook
*/
public function close() {
}
}
}
/**
* Only write the data if it exists.
* @param object $myxls
* @param int $row
* @param int $col
* @param array $user
* @param array $extra
* @param string $element
*/
function safe_write_string($myxls, $row, $col, $user, $extra, $element) {
if (isset($user[$element])) {
$myxls->write_string($row, $col, $user[$element]);
} else if (isset($extra[$element])) {
$myxls->write_string($row, $col, $extra[$element]->data);
}
}
// Calculate file name.
$downloadfilename = clean_filename("{$course->shortname} {$checklist->name} $strchecklistreport.xls");
// Creating a workbook.
if (defined('BEHAT_SITE_RUNNING')) {
$workbook = new FakeMoodleExcelWorkbook("-");
} else {
$workbook = new MoodleExcelWorkbook("-");
}
// Sending HTTP headers.
$workbook->send($downloadfilename);
// Adding the worksheet.
$wsname = str_replace(['\\', '/', '?', '*', '[', ']', ' ', ':', '\''], '', $checklist->name);
$wsname = substr($wsname, 0, 31);
$myxls = $workbook->add_worksheet($wsname);
// Print names of all the fields.
$col = 0;
$row = 0;
foreach ($checklistexportusercolumns as $field => $headerstr) {
$myxls->write_string($row, $col++, $headerstr);
}
if ($percentheadings) {
if ($exportoptional) {
$itemoptional = ''; // All items.
} else {
$itemoptional = ' AND itemoptional <> ' . CHECKLIST_OPTIONAL_YES . ' '; // All except optional items.
}
} else {
if ($exportoptional) {
$itemoptional = ' AND itemoptional < ' . CHECKLIST_OPTIONAL_HEADING; // All except headings.
} else {
$itemoptional = ' AND itemoptional = ' . CHECKLIST_OPTIONAL_NO; // Only required items.
}
}
$items = $DB->get_records_select(
'checklist_item',
"checklist = ? AND userid = 0 $itemoptional AND hidden = 0",
[$checklist->id],
'position'
);
if ($items) {
$parentitem = 0;
foreach ($items as $item) {
if ($item->itemoptional == CHECKLIST_OPTIONAL_HEADING) {
$parentitem = $item->id;
$items[$item->id]->subitems = [];
} else if ($parentitem && $item->itemoptional == CHECKLIST_OPTIONAL_NO) {
$items[$parentitem]->subitems[] = $item->id;
}
$myxls->write_string($row, $col++, strip_tags($item->displaytext));
}
$countitems = [];
if ($percentrow && !empty($users)) {
[$isql, $iparam] = $DB->get_in_or_equal(array_keys($items));
[$usql, $uparam] = $DB->get_in_or_equal(array_keys($users));
$sql = "SELECT item, COUNT(*) AS countitems
FROM {checklist_check}
WHERE item $isql
AND userid $usql";
if (!$teachermarking) {
$sql .= ' AND usertimestamp > 0 ';
} else {
$sql .= ' AND teachermark = ' . CHECKLIST_TEACHERMARK_YES . ' ';
}
$sql .= ' GROUP BY item';
$countitems = $DB->get_records_sql($sql, array_merge($iparam, $uparam));
}
if (!empty($countitems)) {
$row++;
$columncount = count($checklistexportusercolumns);
for ($col = 0; $col < $columncount; $col++) {
$myxls->write_string($row, $col, '');
}
$countusers = count($users);
foreach ($items as $item) {
if ($item->itemoptional == CHECKLIST_OPTIONAL_HEADING) {
$percent = '';
} else if (empty($countitems[$item->id]->countitems)) {
$percent = '0%';
} else {
$percent = round(100 * $countitems[$item->id]->countitems / $countusers, 0) . '%';
}
$myxls->write_string($row, $col++, $percent);
}
}
}
// Go through each of the users.
$row++;
$itemoptional = str_replace('itemoptional', 'i.itemoptional', $itemoptional);
foreach ($users as $user) {
$sql = "
SELECT uf.shortname, ud.data
FROM {user_info_data} ud
JOIN {user_info_field} uf ON uf.id = ud.fieldid
WHERE ud.userid = ?
";
$extra = $DB->get_records_sql($sql, [$user->id]);
$groups = groups_get_all_groups($course->id, $user->id, 0, 'g.id, g.name');
if ($groups) {
$groups = array_values($groups);
$first = reset($groups);
$groupsstr = $first->name;
while ($next = next($groups)) {
$groupsstr .= ', ' . $next->name;
}
} else {
$groupsstr = '';
}
$col = 0;
$sql = "
SELECT i.id, i.itemoptional, c.usertimestamp, c.teachermark
FROM {checklist_item} i
LEFT JOIN (
SELECT ch.item, ch.usertimestamp, ch.teachermark
FROM {checklist_check} ch
WHERE ch.userid = ?
) c ON c.item = i.id
WHERE i.checklist = ? AND userid = 0 $itemoptional AND i.hidden = 0
ORDER BY i.position
";
$checks = $DB->get_records_sql($sql, [$user->id, $checklist->id]);
$userarray = (array)$user;
foreach ($checklistexportusercolumns as $field => $header) {
if ($field === '_groups') {
$myxls->write_string($row, $col++, $groupsstr);
} else if ($field === '_enroldate') {
$sql = 'SELECT ue.id, ue.timestart FROM {user_enrolments} ue, {enrol} e ';
$sql .= "WHERE e.id = ue.enrolid AND e.courseid = ? AND ue.userid = ? AND e.enrol <> 'guest' ";
$sql .= 'ORDER BY ue.timestart ASC ';
$enrolement = $DB->get_records_sql($sql, [$course->id, $user->id], 0, 1);
$datestr = '';
if (!empty($enrolement)) {
$enrolement = reset($enrolement);
$datestr = userdate($enrolement->timestart, get_string('strftimedate'));
}
$myxls->write_string($row, $col++, $datestr);
} else if ($field === '_startdate') {
$firstview = null;
$manager = get_log_manager();
$readers = $manager->get_readers('\core\log\sql_internal_table_reader');
/** @var \core\log\sql_internal_table_reader $reader */
$reader = reset($readers);
if ($reader) {
$tablename = $reader->get_internal_log_table_name();
$cond = [
'userid' => $user->id, 'courseid' => $course->id,
'target' => 'course', 'action' => 'viewed',
];
$firstview = $DB->get_field($tablename, 'MIN(timecreated)', $cond);
}
$datestr = '';
if ($firstview) {
$datestr = userdate($firstview, get_string('strftimedate'));
}
$myxls->write_string($row, $col++, $datestr);
} else if ($field === '_percent') {
$checked = 0;
$total = 0;
foreach ($checks as $check) {
if ($check->itemoptional != CHECKLIST_OPTIONAL_NO) {
continue; // Only count 'required' items.
}
$total++;
if (!$teachermarking) {
if ($check->usertimestamp > 0) {
$checked++;
}
} else { // Teacher / both => use teacher mark for percentage.
if ($check->teachermark == CHECKLIST_TEACHERMARK_YES) {
$checked++;
}
}
}
if ($checked == 0) {
$percent = '';
} else {
$percent = round(100 * $checked / $total, 0) . '%';
}
$myxls->write_string($row, $col++, $percent);
} else {
safe_write_string($myxls, $row, $col++, $userarray, $extra, $field);
}
}
foreach ($checks as $check) {
$out = '';
if ($check->itemoptional == CHECKLIST_OPTIONAL_HEADING) {
$checked = 0;
$item = $items[$check->id];
foreach ($item->subitems as $subitem) {
if (!$teachermarking) {
if ($checks[$subitem]->usertimestamp > 0) {
$checked++;
}
} else {
if ($checks[$subitem]->teachermark == CHECKLIST_TEACHERMARK_YES) {
$checked++;
}
}
}
if ($count = count($item->subitems)) {
$out .= round(100 * $checked / $count, 0) . '%';
}
} else {
if ($teachermarking) {
if ($check->teachermark == CHECKLIST_TEACHERMARK_NO) {
$out .= 'N';
} else if ($check->teachermark == CHECKLIST_TEACHERMARK_YES) {
$out .= 'Y';
}
}
if ($studentmarking && $check->usertimestamp > 0) {
$out .= '1';
}
}
$myxls->write_string($row, $col, $out);
$col++;
}
$row++;
}
$workbook->close();
exit;