forked from marcusgreen/moodle-report_advancedgrading
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocallib.php
executable file
·420 lines (385 loc) · 13.6 KB
/
locallib.php
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
<?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/>.
/**
* Shared code for the advancedgrading methods
*
*
* @package report_advancedgrading
* @copyright 2022 Marcus Green
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Color;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/mod/assign/locallib.php');
defined('MOODLE_INTERNAL') || die;
/**
* Get the structure of this grading definition
*
* @param int $assignid
* @return \stdClass
*/
function get_grading_definition(int $assignid): \stdClass {
global $DB;
$sql = "SELECT gdef.id AS definitionid, ga.activemethod, gdef.name AS definition
FROM {assign} assign
JOIN {course_modules} cm ON cm.instance = assign.id
JOIN {context} ctx ON ctx.instanceid = cm.id
JOIN {grading_areas} ga ON ctx.id=ga.contextid
JOIN {grading_definitions} gdef ON ga.id = gdef.areaid
WHERE assign.id = :assignid";
$definition = $DB->get_record_sql($sql, ['assignid' => $assignid]);
return $definition;
}
/**
* Get object with list of groups each user is in.
* Credit to Dan Marsden for this idea/function
* @param int $courseid
*/
function report_advancedgrading_get_user_groups($courseid): array {
global $DB;
$sql = "SELECT gm.userid,g.id, g.name
FROM {groups} g
JOIN {groups_members} gm ON gm.groupid = g.id
WHERE g.courseid = :courseid
ORDER BY gm.userid";
$rs = $DB->get_recordset_sql($sql, ['courseid' => $courseid]);
foreach ($rs as $row) {
if (!isset($groupsbyuser[$row->userid])) {
$groupsbyuser[$row->userid] = [];
}
$groupsbyuser[$row->userid][] = $row->name;
}
$rs->close();
return $groupsbyuser ?? [];
}
/**
* Get the descriptive header fields that detail
* the details of the grading setup
*
* @param array $data
* @param array $criteria
* @param \stdClass $course
* @param \cm_info $assign
* @param \stdClass $gdef
* @return array
*/
function header_fields(array $data, array $criteria, \stdClass $course, \cm_info $assign, \stdClass $gdef): array {
foreach ($criteria as $criterion) {
$data['criteria'][] = [
'description' => $criterion
];
}
$data['header'] = [
'coursename' => format_string($course->fullname),
'assignment' => format_string($assign->name),
'gradingmethod' => get_string('pluginname', 'gradingform_'.$gdef->activemethod),
'definition' => $gdef->definition
];
$criterion = [];
$data['studentheaders'] = "";
foreach ($data['profilefields'] as $field) {
$data['studentheaders'] .= "<th " . $data['headerstyle'] . "><b>" . get_string($field) . "</b></th>";
}
return $data;
}
/**
* Assemble the profile fields as configured in
* settings and also the criterion
*
* @param array $data
* @param array $dbrecords
* @return array
*/
function user_fields(array $data, array $dbrecords): array {
foreach ($dbrecords as $grade) {
$student['userid'] = $grade->userid;
foreach ($data['profilefields'] as $field) {
if ($field == 'groups') {
continue;
}
$student[$field] = $grade->$field;
}
$data['students'][$grade->userid] = $student;
}
return $data;
}
/**
* Send output either to the browser or
* to a file download
*
* @param string $form
* @param string $dload
* @param array $data
* @param string $page
* @return void
*/
function send_output(string $form, string $dload, array $data, string $page) : void {
global $OUTPUT, $PAGE;
$PAGE->set_cm($data['cm']);
if ($dload) {
download($page, $data);
echo $OUTPUT->header();
} else {
$html = $form . $page;
$PAGE->set_pagelayout('report');
echo $OUTPUT->header();
echo $OUTPUT->container($html, 'advancedgrading-main');
}
echo $OUTPUT->footer();
}
/**
* Initialise stuff common to all grading methods
*
* @param array $data
* @return array
*/
function init(array $data): array {
global $PAGE, $DB;
$profileconfig = trim(get_config('report_advancedgrading', 'profilefields'));
$data['courseidvalue'] = 'value='.$data['courseid'];
$data['profilefields'] = empty($profileconfig) ? [] : explode(',', $profileconfig);
$data['studentspan'] = count($data['profilefields']);
$data['colcount'] = count($data['profilefields']);
$data['colcount'] += 4; // Always 4 cols in the summary.
$modinfo = get_fast_modinfo($data['courseid']);
$data['cm'] = $modinfo->get_cm($data['modid']);
$data['course'] = $DB->get_record('course', array('id' => $data['courseid']), '*', MUST_EXIST);
$data['gradingdefinition'] = get_grading_definition($data['cm']->instance);
$data['context'] = \context_module::instance($data['cm']->id);
$data['assign'] = new assign($data['context'], $data['cm'], $data['cm']->get_course());
if ($data['grademethod'] == 'rubric_ranges') {
$criteriatable = 'gradingform_' . $data['grademethod'] . '_c';
} else {
$criteriatable = 'gradingform_' . $data['grademethod'] . '_criteria';
}
$data['criteriarecord'] = $DB->get_records_menu($criteriatable,
['definitionid' => (int) $data['gradingdefinition']->definitionid], 'sortorder', 'id, description');
$data = header_fields($data, $data['criteriarecord'], $data['course'], $data['cm'], $data['gradingdefinition']);
$data['definition'] = get_grading_definition($data['cm']->instance);
$data['formaction'] = 'action='.$data['grademethod'] .'.php?id='.$data['courseid'].'&modid='.$data['modid'];
// Summary always has 4 columns.
$data['summaryspan'] = 4;
// TODO check if headerspanis actually used.
$data['headerspan'] = $data['colcount'];
$event = \report_advancedgrading\event\report_viewed::create(array(
'context' => $data['context'],
'other' => array(
'gradingmethod' => $data['grademethod']
)
));
$event->add_record_snapshot('course_modules', $data['cm']);
$event->trigger();
$urlparams['id'] = $data['courseid'];
$urlparams['modid'] = $data['modid'];
$url = new moodle_url('/report/advancedgrading/' . $data['grademethod'] . '.php', $urlparams);
$PAGE->set_url($url, $urlparams);
$returnurl = new moodle_url('/mod/assign/view.php', ['id' => $data['cm']->id]);
$PAGE->navbar->add($data['cm']->name, $returnurl);
$PAGE->navbar->add($data['reportname']);
$PAGE->set_context($data['context']);
$PAGE->set_pagelayout('report');
$PAGE->set_title($data['reportname']);
return $data;
}
/**
* Assemble the grades into the data array from
* the returned deatabase records.
*
* @param array $data
* @param array $dbrecords
* @return array
*/
function get_grades(array $data, array $dbrecords): array {
global $DB;
$gradeoutof = reset($dbrecords)->gradeoutof;
if ($gradeoutof < 0) {
$scale = $DB->get_record('scale', ['id' => - ($gradeoutof)], 'scale');
$scaleoptions = make_menu_from_list($scale->scale);
}
foreach ($dbrecords as $grade) {
$data['criterion'][$grade->criterionid] = $grade->description;
$g[$grade->userid][$grade->criterionid] = [
'userid' => $grade->userid,
'score' => number_format($grade->grade_rub_range ?? $grade->score, 2),
'definition' => $grade->definition ?? "",
'feedback' => $grade->remark
];
if (isset($scaleoptions)) {
$formattedgrade = $scaleoptions[round($grade->grade)] ?? $scaleoptions[1];
} else {
$formattedgrade = number_format($grade->grade, 2);
}
$gi = [
'overallfeedback' => $grade->overallfeedback,
'grader' => $grade->grader,
'timegraded' => $grade->modified,
'grade' => $formattedgrade
];
foreach ($data['students'] as $student) {
if ($student['userid'] == $grade->userid) {
$data['students'][$grade->userid]['grades'] = $g[$grade->userid];
$data['students'][$grade->userid]['gradeinfo'] = $gi;
}
}
}
return $data;
}
/**
* Add group membership to student data
*
* @param array $data
* @param int $courseid
* @return array
*/
function add_groups(array $data, int $courseid): array {
$groups = report_advancedgrading_get_user_groups($courseid);
foreach (array_keys($data['students']) as $userid) {
if (isset($groups[$userid])) {
$data['students'][$userid]['groups'] = implode(" ", $groups[$userid]);
} else {
$data['students'][$userid]['groups'] = "";
}
}
return $data;
}
/**
* Get student data for each
* student column
*
* @param array $data
* @param array $student
* @return string
*/
function get_student_cells(array $data, array $student) :string {
$cell = '';
foreach ($data['profilefields'] as $field) {
$cell .= '<td>' . $student[$field] . '</td>';
}
return $cell;
}
/**
* Obscure the identify of students when blind marking
* is enabled. These identities will match those shown
* in gthe gradebook.
*
* @param array $data
* @param [type] $assign
* @param [type] $cm
* @return array
*/
function set_blindmarking(array $data, $assign, $cm): array {
if ($assign->is_blind_marking()) {
foreach ($data as &$user) {
$anonymousid = get_string('participant', 'report_advancedgrading') .
' ' . \assign::get_uniqueid_for_user_static($cm->instance, $user->userid);
$user->username = $anonymousid;
$user->firstname = $anonymousid;
$user->lastname = $anonymousid;
$user->email = $anonymousid;
$user->idnumber = $anonymousid;
}
}
return $data;
}
/**
* Columns shown at the end of the tabale summarisng
* The marking and who did it.
* @param mixed $student
* @return string
*/
function get_summary_cells($student) : string {
$cell = '<td>' . $student['gradeinfo']['overallfeedback'] . '</td>';
$cell .= '<td>' . $student['gradeinfo']['grade'] . '</td>';
$cell .= '<td>' . $student['gradeinfo']['grader'] . '</td>';
$cell .= '<td>' . \userdate($student['gradeinfo']['timegraded'], "%d %b %Y %I:%M %p") . '</td>';
return $cell;
}
/**
* Download the formatted spreadsheet or
* CSV (comma separated values) file.
* with the name of the grading method
*
* @param string $spreadsheet
* @param array $data
* @return void
*/
function download(string $spreadsheet, array $data) {
$filename = $data['grademethod'];
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Html();
$spreadsheet = $reader->loadFromString($spreadsheet);
$csvdownload = optional_param('csvdownload', '', PARAM_TEXT);
$viewsubmissions = optional_param('viewsubmissions', '', PARAM_TEXT);
if ($viewsubmissions > "") {
$params = [
'id' => $data['modid'],
'action' => 'grading'
];
redirect(new moodle_url("/mod/assign/view.php?", $params));
}
if ($csvdownload > "") {
$filetype = 'Csv';
} else {
$filetype = 'Xlsx';
}
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, $filetype);
if ($filetype == 'Xlsx') {
$sheet = $writer->getSpreadsheet()->getActiveSheet();
$colcount = $data['colcount'];
$colcount = $data['colcount'];
// Generate the alphabet based on the column quantity.
$alphabet = [];
for ($l = 'A', $i = 0; $i <= $colcount + 1; $l++, $i++) {
$alphabet[] = $l;
}
$lastcol = $alphabet[$colcount + 1];
// Merge the header cells containing metadata like course name etc.
$sheet->mergeCells('A1:'.$lastcol.'1');
$sheet->mergeCells('A2:'.$lastcol.'2');
$sheet->mergeCells('A3:'.$lastcol.'3');
$sheet->mergeCells('A4:'.$lastcol.'4');
$color = new Color();
$color->setRGB('CDCDCD');
$color2 = new Color();
$color2->setRGB('D3D3D3');
$sheet->getStyle('A6:'.$lastcol.'7')->getFill()->setFillType(Fill::FILL_GRADIENT_LINEAR);
$sheet->getStyle('A6:'.$lastcol.'7')->getFill()->setStartColor($color);
$sheet->getStyle('A6:'.$lastcol.'7')->getFill()->setEndColor($color2);
$sheet->getColumnDimension($lastcol)->setAutoSize(true);
$sheet->setTitle($filename);
}
output_header($filename, $filetype);
$writer->save('php://output');
exit();
}
/**
* Output the http header
*
* @param string $filename
* @param string $filetype
* @return boolean
*/
function output_header(string $filename, string$filetype) : bool {
if ($filetype == 'Xlsx') {
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
} else {
header('Content-Type: application/text/csv');
}
$filetype = strtolower($filetype);
$filename = $filename . "." . $filetype;
header('Content-Disposition: attachment;filename="' . $filename);
return true;
}