forked from maths/moodle-qtype_stack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
questiontestreport.php
403 lines (367 loc) · 14 KB
/
questiontestreport.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
<?php
// This file is part of Stack - http://stack.maths.ed.ac.uk/
//
// Stack 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.
//
// Stack 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 STACK. If not, see <http://www.gnu.org/licenses/>.
/**
* This script lets the user see what attempts have been made at this question.
*
* The script loops over summarise_response data from the database, and does not
* re-generate reports. The script is designed to let a question author improve feedback
* and assessment by looking at what students type, easily and without going through a quiz report.
*
* @copyright 2020 the University of Edinburgh
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__.'/../../../config.php');
require_once($CFG->libdir . '/questionlib.php');
// Get the parameters from the URL.
$questionid = required_param('questionid', PARAM_INT);
// Load the necessary data.
$questiondata = question_bank::load_question_data($questionid);
if (!$questiondata) {
print_error('questiondoesnotexist', 'question');
}
$question = question_bank::load_question($questionid);
// Process any other URL parameters, and do require_login.
list($context, $seed, $urlparams) = qtype_stack_setup_question_test_page($question);
// Check permissions.
question_require_capability_on($questiondata, 'view');
$canedit = question_has_capability_on($questiondata, 'edit');
// Initialise $PAGE.
$PAGE->set_url('/question/type/stack/questiontestreport.php', $urlparams);
$title = stack_string('basicquestionreport');
$PAGE->set_title($title);
$PAGE->set_heading($title);
$PAGE->set_pagelayout('popup');
$testquestionlink = new moodle_url('/question/type/stack/questiontestrun.php', $urlparams);
require_login();
// Start output.
echo $OUTPUT->header();
$renderer = $PAGE->get_renderer('qtype_stack');
echo $OUTPUT->heading($question->name, 2);
$out = html_writer::link($testquestionlink, stack_string('runquestiontests'), array('target' => '_blank'));
// If question has no random variants.
if (empty($question->deployedseeds)) {
if ($question->has_random_variants()) {
$out .= ' ' . stack_string('questionnotdeployedyet');
}
}
echo html_writer::tag('p', $out . ' ' .
$OUTPUT->action_icon(question_preview_url($questionid, null, null, null, null, $context),
new pix_icon('t/preview', get_string('preview'))));
flush();
$query = 'SELECT qa.*, qas_last.*
FROM {question_attempts} qa
LEFT JOIN {question_attempt_steps} qas_last ON qas_last.questionattemptid = qa.id
/* attach another copy of qas to those rows with the most recent timecreated,
using method from https://stackoverflow.com/a/28090544 */
LEFT JOIN {question_attempt_steps} qas_prev
ON qas_last.questionattemptid = qas_prev.questionattemptid
AND (qas_last.sequencenumber < qas_prev.sequencenumber
OR (qas_last.sequencenumber = qas_prev.sequencenumber
AND qas_last.id < qas_prev.id))
LEFT JOIN {user} u ON qas_last.userid = u.id
WHERE
qas_prev.timecreated IS NULL
AND qa.questionid = ' . $questionid . '
ORDER BY u.username, qas_last.timecreated';
global $DB;
$result = $DB->get_records_sql($query);
$summary = array();
foreach ($result as $qattempt) {
if (!array_key_exists($qattempt->variant, $summary)) {
$summary[$qattempt->variant] = array();
}
$rsummary = trim($qattempt->responsesummary);
if ($rsummary !== '') {
if (array_key_exists($rsummary, $summary[$qattempt->variant])) {
$summary[$qattempt->variant][$rsummary] += 1;
} else {
$summary[$qattempt->variant][$rsummary] = 1;
}
}
}
foreach ($summary as $vkey => $variant) {
arsort($variant);
$summary[$vkey] = $variant;
}
// Create blank arrays in which to store data.
$qinputs = array_flip(array_keys($question->inputs));
foreach ($qinputs as $key => $val) {
$qinputs[$key] = array('score' => array(), 'valid' => array(), 'invalid' => array(), 'other' => array());
}
$inputreport = array();
// The inputreportsummary is used to store inputs, regardless of variant.
// Multi-part questions may have inputs which are not subject to randomisation.
$inputreportsummary = $qinputs;
$inputtotals = array();
$qprts = array_flip(array_keys($question->prts));
foreach ($qprts as $key => $notused) {
$qprts[$key] = array();
}
$prtreport = array();
// Create a summary of the data without different variants.
$prtreportsummary = array();
foreach ($summary as $variant => $vdata) {
$inputreport[$variant] = $qinputs;
$prtreport[$variant] = $qprts;
foreach ($vdata as $attemptsummary => $num) {
$rawdat = explode(';', $attemptsummary);
foreach ($rawdat as $data) {
$data = trim($data);
foreach ($qinputs as $input => $notused) {
if (substr($data, 0, strlen($input)) === $input) {
$datas = trim(substr($data, strlen($input . ':')));
$status = 'other';
if (strpos($datas, '[score]') !== false) {
$status = 'score';
$datas = trim(substr($datas, 0, -7));
} else if (strpos($datas, '[valid]') !== false) {
$status = 'valid';
$datas = trim(substr($datas, 0, -7));
} else if (strpos($datas, '[invalid]') !== false) {
$status = 'invalid';
$datas = trim(substr($datas, 0, -9));
}
if (array_key_exists($datas, $inputreport[$variant][$input][$status])) {
$inputreport[$variant][$input][$status][$datas] += (int) $num;
} else {
$inputreport[$variant][$input][$status][$datas] = $num;
}
if (array_key_exists($datas, $inputreportsummary[$input][$status])) {
$inputreportsummary[$input][$status][$datas] += (int) $num;
} else {
$inputreportsummary[$input][$status][$datas] = $num;
}
// Count the total numbers in this array.
if (array_key_exists($input, $inputtotals)) {
$inputtotals[$input] += (int) $num;
} else {
$inputtotals[$input] = $num;
}
}
}
foreach ($qprts as $prt => $notused) {
if (substr($data, 0, strlen($prt)) === $prt) {
$datas = trim(substr($data, strlen($prt . ':')));
if (array_key_exists($datas, $prtreport[$variant][$prt])) {
$prtreport[$variant][$prt][$datas] += (int) $num;
} else {
$prtreport[$variant][$prt][$datas] = $num;
}
if (!array_key_exists($prt, $prtreportsummary)) {
$prtreportsummary[$prt] = array();
}
if (array_key_exists($datas, $prtreportsummary[$prt])) {
$prtreportsummary[$prt][$datas] += (int) $num;
} else {
$prtreportsummary[$prt][$datas] = $num;
}
}
}
}
}
}
// Sort the values.
foreach ($inputreport as $variant => $vdata) {
foreach ($vdata as $input => $idata) {
foreach ($idata as $key => $value) {
arsort($value);
$inputreport[$variant][$input][$key] = $value;
}
}
}
foreach ($inputreportsummary as $input => $idata) {
foreach ($idata as $key => $value) {
arsort($value);
$inputreportsummary[$input][$key] = $value;
}
}
foreach ($prtreport as $variant => $vdata) {
foreach ($vdata as $prt => $tdata) {
arsort($tdata);
$prtreport[$variant][$prt] = $tdata;
}
}
$notesummary = array();
foreach ($prtreportsummary as $prt => $tdata) {
ksort($tdata);
$prtreportsummary[$prt] = $tdata;
if (!array_key_exists($prt, $notesummary)) {
$notesummary[$prt] = array();
}
foreach ($tdata as $rawnote => $num) {
$notes = explode('|', $rawnote);
foreach ($notes as $note) {
$note = trim($note);
if (array_key_exists($note, $notesummary[$prt])) {
$notesummary[$prt][$note] += (int) $num;
} else {
$notesummary[$prt][$note] = $num;
}
}
}
}
foreach ($notesummary as $prt => $tdata) {
ksort($tdata);
$notesummary[$prt] = $tdata;
}
$sumout = '';
foreach ($prtreportsummary as $prt => $data) {
$sumouti = '';
$tot = 0;
$pad = max($data);
foreach ($data as $key => $val) {
$tot += $val;
}
if ($data !== array()) {
foreach ($data as $dat => $num) {
$sumouti .= str_pad($num, strlen((string) $pad) + 1) . '(' .
str_pad(number_format((float) 100 * $num / $tot, 2, '.', ''), 6, ' ', STR_PAD_LEFT) .
'%); ' . $dat . "\n";
}
}
if (trim($sumouti) !== '') {
$sumout .= '## ' . $prt . ' ('. $tot . ")\n" . $sumouti . "\n";;
}
}
if (trim($sumout) !== '') {
echo html_writer::tag('h3', stack_string('basicreportnotes'));
echo html_writer::tag('pre', trim($sumout));
}
$sumout = '';
foreach ($notesummary as $prt => $data) {
$sumouti = '';
if ($data !== array()) {
foreach ($data as $dat => $num) {
// Use the old $tot, to give meaningful percentages of which individual notes occur overall.
$sumouti .= str_pad($num, strlen((string) $pad) + 1) . '(' .
str_pad(number_format((float) 100 * $num / $tot, 2, '.', ''), 6, ' ', STR_PAD_LEFT) .
'%); ' . $dat . "\n";
}
}
if (trim($sumouti) !== '') {
$sumout .= '## ' . $prt . ' ('. $tot . ")\n" . $sumouti . "\n";;
}
}
if (trim($sumout) !== '') {
echo html_writer::tag('h3', stack_string('basicreportnotessplit'));
echo html_writer::tag('pre', trim($sumout));
}
$sumout = '';
foreach ($inputreportsummary as $input => $idata) {
$sumouti = '';
$tot = 0;
foreach ($idata as $key => $data) {
foreach ($data as $dat => $num) {
$tot += $num;
}
}
foreach ($idata as $key => $data) {
if ($data !== array()) {
$sumouti .= '### ' . $key . "\n";
$pad = max($data);
foreach ($data as $dat => $num) {
$sumouti .= str_pad($num, strlen((string) $pad) + 1) . '(' .
str_pad(number_format((float) 100 * $num / $tot, 2, '.', ''), 6, ' ', STR_PAD_LEFT) .
'%); ' . $dat . "\n";
}
$sumouti .= "\n";
}
}
if (trim($sumouti) !== '') {
$sumout .= '## ' . $input . ' ('. $tot . ")\n" . $sumouti;
}
}
if (trim($sumout) !== '') {
echo html_writer::tag('h3', stack_string('basicreportinputsummary'));
echo html_writer::tag('pre', $sumout);
}
// Output a report.
if (array_keys($summary) !== array()) {
echo html_writer::tag('h2', stack_string('basicreportvariants'));
}
foreach (array_keys($summary) as $variant) {
// TODO: how do we go from a variant to a seed (if there is one....)?
echo html_writer::tag('h3', $variant);
$sumout = '';
foreach ($inputreport[$variant] as $input => $idata) {
$sumouti = '';
$tot = 0;
foreach ($idata as $key => $data) {
foreach ($data as $dat => $num) {
$tot += $num;
}
}
foreach ($idata as $key => $data) {
if ($data !== array()) {
$sumouti .= '### ' . $key . "\n";
$pad = max($data);
foreach ($data as $dat => $num) {
$sumouti .= str_pad($num, strlen((string) $pad) + 1) . '(' .
str_pad(number_format((float) 100 * $num / $tot, 2, '.', ''), 6, ' ', STR_PAD_LEFT) .
'%); ' . $dat . "\n";
}
$sumouti .= "\n";
}
}
if (trim($sumouti) !== '') {
$sumout .= '## ' . $input . ' ('. $tot . ")\n" . $sumouti;
}
}
if (trim($sumout) !== '') {
echo html_writer::tag('pre', $sumout);
}
$sumout = '';
foreach ($prtreport[$variant] as $prt => $idata) {
$pad = 0;
$tot = 0;
foreach ($idata as $dat => $num) {
$tot += $num;
}
if ($idata !== array()) {
$sumout .= '## ' . $prt . ' ('. $tot . ")\n";
$pad = max($idata);
}
foreach ($idata as $dat => $num) {
$sumout .= str_pad($num, strlen((string) $pad) + 1) . '(' .
str_pad(number_format((float) 100 * $num / $tot, 2, '.', ''), 6, ' ', STR_PAD_LEFT) .
'%); ' . $dat . "\n";
}
$sumout .= "\n";
}
if (trim($sumout) !== '') {
echo html_writer::tag('pre', $sumout);
}
}
echo html_writer::tag('h2', stack_string('basicreportraw'));
$sumout = '';
foreach ($summary as $variant => $vdata) {
if ($vdata !== array()) {
$tot = 0;
foreach ($vdata as $dat => $num) {
$tot += $num;
}
$pad = max($vdata);
$sumout .= "\n# " . $variant . ' ('. $tot . ")\n";
foreach ($vdata as $dat => $num) {
$sumout .= str_pad($num, strlen((string) $pad) + 1) . '(' .
str_pad(number_format((float) 100 * $num / $tot, 2, '.', ''), 6, ' ', STR_PAD_LEFT) .
'%); ' . $dat . "\n";
}
}
}
echo html_writer::tag('pre', $sumout);
// Finish output.
echo $OUTPUT->footer();