-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquestiontype.php
594 lines (538 loc) · 24 KB
/
questiontype.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
use qtype_oumatrix\column;
use qtype_oumatrix\row;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/questionlib.php');
/**
* Class that represents the oumatrix question type.
*
* The class loads, saves and deletes questions of the type oumatrix
* to and from the database and provides methods to help with editing questions
* of this type. It can also provide the implementation for import and export
* in various formats.
*
* @package qtype_oumatrix
* @copyright 2023 The Open University
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_oumatrix extends question_type {
#[\Override]
public function get_question_options($question) {
global $DB, $OUTPUT;
parent::get_question_options($question);
if (!$question->options = $DB->get_record('qtype_oumatrix_options', ['questionid' => $question->id])) {
echo $OUTPUT->notification('Error: Missing matrix question options!');
return false;
}
if (!$question->columns = $DB->get_records('qtype_oumatrix_columns', ['questionid' => $question->id], 'id')) {
echo $OUTPUT->notification('Error: Missing question columns!');
return false;
}
if (!$question->rows = $DB->get_records('qtype_oumatrix_rows', ['questionid' => $question->id], 'id')) {
echo $OUTPUT->notification('Error: Missing question rows!');
return false;
}
return true;
}
#[\Override]
public function save_defaults_for_new_questions(stdClass $fromform): void {
parent::save_defaults_for_new_questions($fromform);
$this->set_default_value('inputtype', $fromform->inputtype);
$this->set_default_value('grademethod', $fromform->grademethod);
$this->set_default_value('shuffleanswers', $fromform->shuffleanswers);
$this->set_default_value('shownumcorrect', $fromform->shownumcorrect);
}
#[\Override]
public function save_question_options($question) {
global $DB;
$context = $question->context;
$options = $DB->get_record('qtype_oumatrix_options', ['questionid' => $question->id]);
if (!$options) {
$options = new stdClass();
$options->questionid = $question->id;
$options->inputtype = '';
$options->grademethod = '';
$options->shuffleanswers = 0;
$options->correctfeedback = '';
$options->partiallycorrectfeedback = '';
$options->incorrectfeedback = '';
$options->shownumcorrect = 0;
$options->id = $DB->insert_record('qtype_oumatrix_options', $options);
}
$options->questionid = $question->id;
$options->inputtype = $question->inputtype;
$options->grademethod = $question->grademethod;
$options->shuffleanswers = $question->shuffleanswers;
$options = $this->save_combined_feedback_helper($options, $question, $context, true);
$DB->update_record('qtype_oumatrix_options', $options);
$columnslist = $this->save_columns($question);
$this->save_rows($question, $columnslist);
$this->save_hints($question, true);
}
/**
* Save the question columns and return a list of columns to be used in the save_rows function.
*
* @param stdClass $question This holds the information from the editing form.
* @return array The list of columns created.
*/
public function save_columns(stdClass $question): array {
global $DB;
$numcolumns = count($question->columnname);
$columnslist = [];
// Insert column input data.
$columnnumber = 1;
for ($i = 0; $i < $numcolumns; $i++) {
if (trim(($question->columnname[$i]) ?? '') === '') {
continue;
}
$column = new stdClass();
$column->questionid = $question->id;
$column->number = $columnnumber;
$column->name = $question->columnname[$i];
$column->id = $DB->insert_record('qtype_oumatrix_columns', $column);
$columnslist[$column->number] = $column;
$columnnumber++;
}
return $columnslist;
}
/**
* Save the question rows.
*
* @param stdClass $question This holds the information from the editing form
* @param array $columnslist
*/
public function save_rows(stdClass $question, array $columnslist) {
global $DB;
$context = $question->context;
$numrows = count($question->rowname);
// Insert row input data.
$rownumber = 1;
for ($i = 0; $i < $numrows; $i++) {
$answerslist = [];
if (trim($question->rowname[$i] ?? '') === '') {
continue;
}
$questionrow = new stdClass();
$questionrow->questionid = $question->id;
$questionrow->number = $rownumber;
$questionrow->name = $question->rowname[$i];
// Prepare correct answers.
for ($c = 0; $c < count($columnslist); $c++) {
if ($question->inputtype == 'single') {
$columnnumber = preg_replace("/[^0-9]/", "", $question->rowanswers[$i]);
$answerslist[] = $columnnumber;
break;
} else {
$rowanswerslabel = "rowanswers" . 'a' . ($c + 1);
if (!isset($question->$rowanswerslabel) || !array_key_exists($i, $question->$rowanswerslabel)) {
continue;
}
$answerslist[] = $c + 1;
}
}
$questionrow->correctanswers = implode(',', $answerslist);
$questionrow->feedback = $question->feedback[$i]['text'];
$questionrow->feedbackformat = FORMAT_HTML;
$questionrow->id = $DB->insert_record('qtype_oumatrix_rows', $questionrow);
if ($question->feedback[$i]['text'] != '') {
$questionrow->feedback = $this->import_or_save_files($question->feedback[$i],
$context, 'qtype_oumatrix', 'feedback', $questionrow->id);
$questionrow->feedbackformat = $question->feedback[$i]['format'];
$DB->update_record('qtype_oumatrix_rows', $questionrow);
}
$rownumber++;
}
}
#[\Override]
protected function make_question_instance($questiondata) {
question_bank::load_question_definition_classes($this->name());
if ($questiondata->options->inputtype === 'single') {
$class = 'qtype_oumatrix_single';
} else {
$class = 'qtype_oumatrix_multiple';
}
return new $class();
}
#[\Override]
protected function initialise_question_instance(question_definition $question, $questiondata) {
parent::initialise_question_instance($question, $questiondata);
$question->grademethod = $questiondata->options->grademethod;
$question->shuffleanswers = $questiondata->options->shuffleanswers;
$this->initialise_question_columns($question, $questiondata);
$this->initialise_question_rows($question, $questiondata);
$this->initialise_combined_feedback($question, $questiondata, true);
}
/**
* Initialise the question columns.
*
* @param question_definition $question the question_definition we are creating.
* @param stdClass $questiondata the question data loaded from the database.
*/
protected function initialise_question_columns(question_definition $question, stdClass $questiondata): void {
foreach ($questiondata->columns as $column) {
$question->columns[$column->number] = $this->make_column($column);
}
}
/**
* Make a column from raw data from the DB.
*
* @param stdClass $columndata
* @return column
*/
protected function make_column(stdClass $columndata): column {
return new column($columndata->questionid, $columndata->number, $columndata->name, $columndata->id);
}
/**
* Initialise the question rows.
*
* @param question_definition $question the question_definition we are creating.
* @param stdClass $questiondata the question data loaded from the database.
*/
protected function initialise_question_rows(question_definition $question, stdClass $questiondata): void {
foreach ($questiondata->rows as $row) {
$newrow = $this->make_row($row);
$correctanswers = [];
foreach ($questiondata->columns as $column) {
if (in_array($column->number, $newrow->correctanswers)) {
if ($questiondata->options->inputtype == 'single') {
$anslabel = 'a' . $column->number;
$correctanswers[$column->number] = $anslabel;
} else {
$correctanswers[$column->number] = '1';
}
}
}
$newrow->correctanswers = $correctanswers;
$question->rows[$row->number] = $newrow;
}
}
/**
* Make a row from raw data from the DB.
*
* @param stdClass $rowdata
* @return row
*/
public function make_row(stdClass $rowdata): row {
return new row($rowdata->id, $rowdata->questionid, $rowdata->number, $rowdata->name,
explode(',', $rowdata->correctanswers), $rowdata->feedback, $rowdata->feedbackformat);
}
#[\Override]
protected function make_hint($hint) {
return question_hint_with_parts::load_from_record($hint);
}
#[\Override]
public function delete_question($questionid, $contextid) {
global $DB;
$DB->delete_records('qtype_oumatrix_options', ['questionid' => $questionid]);
$DB->delete_records('qtype_oumatrix_columns', ['questionid' => $questionid]);
$DB->delete_records('qtype_oumatrix_rows', ['questionid' => $questionid]);
parent::delete_question($questionid, $contextid);
}
#[\Override]
public function get_random_guess_score($questiondata) {
// We compute the random guess score here on the assumption we are using
// the deferred feedback behaviour, and the question text tells the
// student how many of the responses are correct.
// Amazingly, the forumla for this works out to be
// # correct choices / total # choices in all cases.
$numberofcolumns = count($questiondata->columns);
if (!$numberofcolumns) {
return null;
}
if ($questiondata->options->inputtype === 'single') {
return 1 / $numberofcolumns;
} else {
// TODO: We agreed to return null for 'multiple' until we worked it
// out all combinations with regards to grading methods.
return null;
// The follwoing code within 'else' is not excuted(It should not get to here.).
if (!$this->get_num_correct_choices($questiondata) ||
!$this->get_total_number_of_choices($questiondata)) {
return null;
}
return $this->get_num_correct_choices($questiondata) / $this->get_total_number_of_choices($questiondata);
}
return null;
}
/**
* Return total number if choices for both (single, multiple) matrix choices.
*
* @param stdClass $questiondata
* @return int|null
*/
public function get_total_number_of_choices(stdClass $questiondata): ?int {
// If rows or columns are not set return null.
if (count($questiondata->columns) === 0 || count($questiondata->rows) === 0) {
return null;
}
// Total number of choices for each row is the number of columns,
// therefore the total number of choices for the question is.
return count($questiondata->columns) * count($questiondata->rows);
}
/**
* Returns the count of correct answers for the question.
*
* @param stdClass $questiondata The question data
* @return int the number of choices that are correct.
*/
public function get_num_correct_choices(stdClass $questiondata): int {
$numright = 0;
foreach ($questiondata->rows as $row) {
$numright += count((array)$row->correctanswers);
}
return $numright;
}
#[\Override]
public function get_possible_responses($questiondata) {
if ($questiondata->options->inputtype == 'single') {
return $this->get_possible_responses_single($questiondata);
} else {
return $this->get_possible_responses_multiple($questiondata);
}
}
/**
* Do the radio button case of get_possible_responses.
*
* @param stdClass $questiondata the question definition data.
* @return array as for get_possible_responses.
*/
protected function get_possible_responses_single(stdClass $questiondata): array {
$parts = [];
foreach ($questiondata->rows as $row) {
$responses = [];
foreach ($questiondata->columns as $column) {
$responses[$column->number] = new question_possible_response(
format_string($column->name),
(int) ($column->number == $row->correctanswers)
);
}
$responses[null] = question_possible_response::no_response();
$parts[shorten_text($row->number . '. ' . format_string($row->name), 100)] = $responses;
}
return $parts;
}
/**
* Do the checkbox button case of get_possible_responses.
*
* @param stdClass $questiondata the question definition data.
* @return array as for get_possible_responses.
*/
protected function get_possible_responses_multiple(stdClass $questiondata): array {
$parts = [];
foreach ($questiondata->rows as $row) {
$rowname = format_string($row->name);
$correctanswer = explode(',', $row->correctanswers);
foreach ($questiondata->columns as $column) {
$parts[shorten_text($row->number . '. ' . format_string($rowname), 50) .
shorten_text(': ' . format_string($column->name), 50)] = [
1 => new question_possible_response(
get_string('selected', 'qtype_oumatrix'),
(int) in_array($column->number, $correctanswer) / count($correctanswer),
),
];
}
}
return $parts;
}
#[\Override]
public function import_from_xml($data, $question, qformat_xml $format, $extra = null) {
if (!isset($data['@']['type']) || $data['@']['type'] != 'oumatrix') {
return false;
}
$question = $format->import_headers($data);
$question->qtype = 'oumatrix';
$question->inputtype = $format->import_text(
$format->getpath($data, ['#', 'inputtype'], 'single'));
$question->grademethod = $format->import_text(
$format->getpath($data, ['#', 'grademethod'], 'partial'));
$question->shuffleanswers = $format->trans_single(
$format->getpath($data, ['#', 'shuffleanswers', 0, '#'], 1));
$columns = $format->getpath($data, ['#', 'columns', 0, '#', 'column'], false);
if ($columns) {
$this->import_columns($format, $question, $columns);
}
$rows = $format->getpath($data, ['#', 'rows', 0, '#', 'row'], false);
if ($rows) {
$this->import_rows($format, $question, $rows);
}
$format->import_combined_feedback($question, $data, true);
$format->import_hints($question, $data, true, true,
$format->get_format($question->questiontextformat));
// Get extra choicefeedback setting from each hint.
if (!empty($question->hintoptions)) {
foreach ($question->hintoptions as $key => $options) {
$question->hintshowrowfeedback[$key] = !empty($options);
}
}
return $question;
}
/**
* Import question columns from the Moodle XML format.
*
* @param qformat_xml $format
* @param stdClass $question
* @param array $columns
*/
public function import_columns(qformat_xml $format, stdClass $question, array $columns) {
foreach ($columns as $column) {
static $indexno = 0;
$question->columns[$indexno]['name'] =
$format->import_text($format->getpath($column, ['#', 'text'], ''));
$question->columns[$indexno]['number'] = $format->getpath($column, ['@', 'number'], $indexno);
$question->columnname[$indexno] =
$format->import_text($format->getpath($column, ['#', 'text'], ''));
$indexno++;
}
}
/**
* Import question rows from the Moodle XML format
*
* @param qformat_xml $format
* @param stdClass $question
* @param array $rows
*/
public function import_rows(qformat_xml $format, stdClass $question, array $rows) {
foreach ($rows as $row) {
static $indexno = 0;
$question->rows[$indexno]['number'] = $format->getpath($row, ['@', 'number'], $indexno);
$question->rows[$indexno]['name'] =
$format->import_text($format->getpath($row, ['#', 'name', 0, '#', 'text'], ''));
$correctanswer = $format->getpath($row, ['#', 'correctanswers', 0, '#', 'text', 0, '#'], '');
$answerslist = explode(',', $correctanswer);
foreach ($question->columns as $col) {
if (in_array($col['number'], $answerslist)) {
// Import correct answers for single choice.
if ($question->inputtype == 'single') {
$question->rowanswers[$indexno] = $col['number'];
break;
} else {
// Import correct answers for multiple choice.
$rowanswerslabel = "rowanswers" . 'a' . $col['number'];
if (in_array($col['number'], $answerslist)) {
$question->{$rowanswerslabel}[$indexno] = "1";
}
}
}
}
$question->rowname[$indexno] =
$format->import_text($format->getpath($row, ['#', 'name', 0, '#', 'text'], ''));
$question->feedback[$indexno] = $format->import_text_with_files($row, ['#', 'feedback', 0], '', 'html');
$indexno++;
}
}
#[\Override]
public function export_to_xml($question, qformat_xml $format, $extra = null) {
$output = '';
$output .= ' <inputtype>' . $format->xml_escape($question->options->inputtype)
. "</inputtype>\n";
$output .= ' <grademethod>' . $format->xml_escape($question->options->grademethod)
. "</grademethod>\n";
$output .= " <shuffleanswers>" . $format->get_single(
$question->options->shuffleanswers) . "</shuffleanswers>\n";
// Export columns data.
$output .= " <columns>\n";
ksort($question->columns);
foreach ($question->columns as $columnkey => $column) {
$output .= " <column number=\"{$column->number}\">\n";
$output .= $format->writetext($column->name, 4);
$output .= " </column>\n";
}
$output .= " </columns>\n";
// Export rows data.
$fs = get_file_storage();
$output .= " <rows>\n";
ksort($question->rows);
$indent = 5;
foreach ($question->rows as $rowkey => $row) {
$output .= " <row number=\"{$row->number}\">\n";
$output .= " <name>\n";
$output .= $format->writetext($row->name, $indent);
$output .= " </name>\n";
$output .= " <correctanswers>\n";
$output .= $format->writetext($row->correctanswers, $indent);
$output .= " </correctanswers>\n";
if (($row->feedback ?? '') != '') {
$output .= ' <feedback ' . $format->format($row->feedbackformat) . ">\n";
$output .= $format->writetext($row->feedback, $indent);
$files = $fs->get_area_files($question->contextid, 'qtype_oumatrix', 'feedback', $row->id);
$output .= $format->write_files($files);
$output .= " </feedback>\n";
}
$output .= " </row>\n";
}
$output .= " </rows>\n";
$output .= $format->write_combined_feedback($question->options,
$question->id,
$question->contextid);
return $output;
}
#[\Override]
public function move_files($questionid, $oldcontextid, $newcontextid) {
$fs = get_file_storage();
parent::move_files($questionid, $oldcontextid, $newcontextid);
$this->move_files_in_row_feedback($questionid, $oldcontextid, $newcontextid);
$this->move_files_in_hints($questionid, $oldcontextid, $newcontextid);
$fs->move_area_files_to_new_context($oldcontextid,
$newcontextid, 'question', 'correctfeedback', $questionid);
$fs->move_area_files_to_new_context($oldcontextid,
$newcontextid, 'question', 'partiallycorrectfeedback', $questionid);
$fs->move_area_files_to_new_context($oldcontextid,
$newcontextid, 'question', 'incorrectfeedback', $questionid);
}
/**
* Move the feedback files related to the row feedback.
*
* @param int $questionid the question being moved.
* @param int $oldcontextid the context it is moving from.
* @param int $newcontextid the context it is moving to.
*/
protected function move_files_in_row_feedback(int $questionid, int $oldcontextid, int $newcontextid) {
global $DB;
$fs = get_file_storage();
$rowids = $DB->get_records_menu('qtype_oumatrix_rows',
['questionid' => $questionid], 'id', 'id,1');
foreach ($rowids as $rowid => $notused) {
$fs->move_area_files_to_new_context($oldcontextid,
$newcontextid, 'qtype_oumatrix', 'feedback', $rowid);
}
}
#[\Override]
protected function delete_files($questionid, $contextid) {
$fs = get_file_storage();
parent::delete_files($questionid, $contextid);
$this->delete_files_in_row_feedback($questionid, $contextid);
$this->delete_files_in_hints($questionid, $contextid);
$fs->delete_area_files($contextid, 'question', 'correctfeedback', $questionid);
$fs->delete_area_files($contextid, 'question', 'partiallycorrectfeedback', $questionid);
$fs->delete_area_files($contextid, 'question', 'incorrectfeedback', $questionid);
}
/**
* Delete all the files belonging to this question's row feedbacks.
*
* @param int $questionid the question being deleted.
* @param int $contextid the context the question is in.
*/
protected function delete_files_in_row_feedback(int $questionid, int $contextid) {
global $DB;
$fs = get_file_storage();
$rowids = $DB->get_records_menu('qtype_oumatrix_rows',
['questionid' => $questionid], 'id', 'id,1');
foreach ($rowids as $rowid => $notused) {
$fs->delete_area_files($contextid, 'qtype_oumatrix', 'feedback', $rowid);
}
}
}