forked from ethz-let/moodle-qtype_kprime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion.php
More file actions
557 lines (496 loc) · 18 KB
/
question.php
File metadata and controls
557 lines (496 loc) · 18 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
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
<?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/>.
/**
* @package qtype_kprime
* @author Amr Hourani (amr.hourani@id.ethz.ch)
* @author Martin Hanusch (martin.hanusch@let.ethz.ch)
* @author Jürgen Zimmer (juergen.zimmer@edaktik.at)
* @author Andreas Hruska (andreas.hruska@edaktik.at)
* @copyright 2016 ETHZ {@link http://ethz.ch/}
* @copyright 2014 eDaktik GmbH {@link http://www.edaktik.at}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
class qtype_kprime_question extends question_graded_automatically_with_countback {
public $rows;
public $columns;
public $weights;
public $scoringmethod;
public $shuffleanswers;
public $numberofrows;
public $numberofcols;
public $order = null;
public $editedquestion;
// All the methods needed for option shuffling.
/**
* (non-PHPdoc).
*
* @see question_definition::start_attempt()
*/
public function start_attempt(question_attempt_step $step, $variant) {
$this->order = array_keys($this->rows);
if ($this->shuffleanswers) {
shuffle($this->order);
}
$step->set_qt_var('_order', implode(',', $this->order));
}
/**
* (non-PHPdoc).
*
* @see question_definition::apply_attempt_state()
*/
public function apply_attempt_state(question_attempt_step $step) {
$this->order = explode(',', $step->get_qt_var('_order'));
// Add any missing answers. Sometimes people edit questions after they
// have been attempted which breaks things.
// Retrieve the question rows (kprime options).
for ($i = 0; $i < count($this->order); $i++) {
if (isset($this->rows[$this->order[$i]])) {
continue;
}
$a = new stdClass();
$a->id = 0;
$a->questionid = $this->id;
$a->number = -1;
$a->optiontext = html_writer::span(get_string('deletedchoice', 'qtype_kprime'), 'notifyproblem');
$a->optiontextformat = FORMAT_HTML;
$a->optionfeedback = "";
$a->optionfeedbackformat = FORMAT_HTML;
$this->rows[$this->order[$i]] = $a;
$this->editedquestion = 1;
}
}
/**
*
* @param question_attempt $qa
*
* @return multitype:
*/
public function get_order(question_attempt $qa) {
$this->init_order($qa);
return $this->order;
}
/**
* Initialises the order (if it is not set yet) by decoding
* the question attempt variable '_order'.
*
* @param question_attempt $qa
*/
protected function init_order(question_attempt $qa) {
if (is_null($this->order)) {
$this->order = explode(',', $qa->get_step(0)->get_qt_var('_order'));
}
}
/**
* Returns the name field name for input cells in the questiondisplay.
* The column parameter is ignored for now since we don't use multiple answers.
*
* @param mixed $row
* @param mixed $col
*
* @return type
*/
public function field($key) {
return 'option' . $key;
}
/**
* Checks whether an row is answered by a given response.
*
* @param type $response
* @param type $row
* @param type $col
*
* @return bool
*/
public function is_answered($response, $rownumber) {
$field = $this->field($rownumber);
// Get the value of the radiobutton array, if it exists in the response.
return isset($response[$field]) && !empty($response[$field]);
}
/**
* Checks whether a given column (response) is the correct answer for a given row (option).
*
* @param string $row The row number.
* @param string $col The column number
*
* @return bool
*/
public function is_correct($row, $col) {
$weight = $this->weight($row, $col);
if ($weight > 0.0) {
return 1;
} else {
return 0;
}
}
/**
* Returns the weight for the given row and column.
*
* @param mixed $row A row object or a row number.
* @param mixed $col A column object or a column number.
*
* @return float
*/
public function weight($row = null, $col = null) {
$rownumber = is_object($row) ? $row->number : $row;
$colnumber = is_object($col) ? $col->number : $col;
if (isset($this->weights[$rownumber][$colnumber])) {
$weight = (float) $this->weights[$rownumber][$colnumber]->weight;
} else {
$weight = 0;
}
return $weight;
}
public function is_row_selected($response, $rownumber) {
return isset($response[$this->field($rownumber)]);
}
public function get_response(question_attempt $qa) {
return $qa->get_last_qt_data();
}
/**
* Used by many of the behaviours, to work out whether the student's
* response to the question is complete.
* That is, whether the question attempt
* should move to the COMPLETE or INCOMPLETE state.
*
* @param array $response responses, as returned by
* {@link question_attempt_step::get_qt_data()}.
*
* @return bool whether this response is a complete answer to this question.
*/
public function is_complete_response(array $response) {
if (count($response) == count($this->rows)) {
return true;
} else {
return false;
}
}
/**
* (non-PHPdoc).
*
* @see question_graded_automatically::is_gradable_response()
*/
public function is_gradable_response(array $response) {
unset($response['_order']);
if ($this->scoringmethod == 'subpoints' || $this->scoringmethod == 'kprime') {
if (count($response) > 0) {
return true;
} else {
return false;
}
} else {
return $this->is_complete_response($response);
}
}
/**
* In situations where is_gradable_response() returns false, this method
* should generate a description of what the problem is.
*
* @return string the message.
*/
public function get_validation_error(array $response) {
$isgradable = $this->is_gradable_response($response);
if ($isgradable) {
return '';
}
return get_string('oneanswerperrow', 'qtype_kprime');
}
/**
*
* @param array $response responses, as returned by
* {@link question_attempt_step::get_qt_data()}.
* @return int the number of choices that were selected. in this response.
*/
public function get_num_selected_choices(array $response) {
$numselected = 0;
foreach ($response as $key => $value) {
// Response keys starting with _ are internal values like _order, so ignore them.
if (!empty($value) && $key[0] != '_') {
$numselected += 1;
}
}
return $numselected;
}
/**
* Produce a plain text summary of a response.
*
* @param $response a response, as might be passed to {@link grade_response()}.
*
* @return string a plain text summary of that response, that could be used in reports.
*/
public function summarise_response(array $response) {
$result = array();
foreach ($this->order as $key => $rowid) {
$field = $this->field($key);
$row = $this->rows[$rowid];
if (isset($response[$field])) {
foreach ($this->columns as $column) {
if ($column->number == $response[$field]) {
$result[] = $this->html_to_text($row->optiontext, $row->optiontextformat) .
': ' . $this->html_to_text($column->responsetext,
$column->responsetextformat);
}
}
}
}
return implode('; ', $result);
}
/**
* (non-PHPdoc).
*
* @see question_with_responses::classify_response()
*/
public function classify_response(array $response) {
// See which column numbers have been selected.
$selectedcolumns = array();
foreach ($this->order as $key => $rowid) {
$field = $this->field($key);
$row = $this->rows[$rowid];
if (array_key_exists($field, $response) && $response[$field]) {
$selectedcolumns[$rowid] = $response[$field];
} else {
$selectedcolumns[$rowid] = 0;
}
}
// Now calculate the classification.
$parts = array();
foreach ($this->rows as $rowid => $row) {
$field = $this->field($key);
if (empty($selectedcolumns[$rowid])) {
$parts[$rowid] = question_classified_response::no_response();
continue;
}
// Find the chosen column by columnnumber.
$column = null;
foreach ($this->columns as $colid => $col) {
if ($col->number == $selectedcolumns[$rowid]) {
$column = $col;
break;
}
}
// Calculate the partial credit.
if ($this->scoringmethod == 'subpoints') {
$partialcredit = 0.0;
} else {
$partialcredit = -999; // Due to non-linear math.
}
if ($this->scoringmethod == 'subpoints' &&
$this->weights[$row->number][$column->number]->weight > 0) {
$partialcredit = 1 / count($this->rows);
}
$parts[$rowid] = new question_classified_response($column->id, $column->responsetext,
$partialcredit);
}
return $parts;
}
/**
* Use by many of the behaviours to determine whether the student's
* response has changed.
* This is normally used to determine that a new set
* of responses can safely be discarded.
*
* @param array $prevresponse the responses previously recorded for this question,
* as returned by {@link question_attempt_step::get_qt_data()}
* @param array $newresponse the new responses, in the same format.
*
* @return bool whether the two sets of responses are the same - that is
* whether the new set of responses can safely be discarded.
*/
public function is_same_response(array $prevresponse, array $newresponse) {
if (count($prevresponse) != count($newresponse)) {
return false;
}
foreach ($prevresponse as $field => $previousvalue) {
if (!isset($newresponse[$field])) {
return false;
}
$newvalue = $newresponse[$field];
if ($newvalue != $previousvalue) {
return false;
}
}
return true;
}
/**
* What data would need to be submitted to get this question correct.
* If there is more than one correct answer, this method should just
* return one possibility.
*
* @return array parameter name => value.
*/
public function get_correct_response($rowidindex = false) {
$result = array();
foreach ($this->order as $key => $rowid) {
$row = $this->rows[$rowid];
$field = $this->field($key);
foreach ($this->columns as $column) {
$weight = $this->weight($row, $column);
if ($weight > 0) {
if ($rowidindex) {
$result[$rowid] = $column->id;
} else {
$result[$field] = $column->number;
}
}
}
}
return $result;
}
/**
* Returns an instance of the grading class according to the scoringmethod of the question.
*
* @return The grading object.
*/
public function grading() {
global $CFG;
$type = $this->scoringmethod;
$gradingclass = 'qtype_kprime_grading_' . $type;
require_once($CFG->dirroot . '/question/type/kprime/grading/' . $gradingclass . '.class.php');
return new $gradingclass();
}
/**
* Grade a response to the question, returning a fraction between
* get_min_fraction() and 1.0, and the corresponding {@link question_state}
* right, partial or wrong.
*
* @param array $response responses, as returned by
* {@link question_attempt_step::get_qt_data()}.
*
* @return array (number, integer) the fraction, and the state.
*/
public function grade_response(array $response) {
$grade = $this->grading()->grade_question($this, $response);
$state = question_state::graded_state_for_fraction($grade);
return array($grade, $state
);
}
/**
* What data may be included in the form submission when a student submits
* this question in its current state?
*
* This information is used in calls to optional_param. The parameter name
* has {@link question_attempt::get_field_prefix()} automatically prepended.
*
* @return array|string variable name => PARAM_... constant, or, as a special case
* that should only be used in unavoidable, the constant question_attempt::USE_RAW_DATA
* meaning take all the raw submitted data belonging to this question.
*/
public function get_expected_data() {
$result = array();
foreach ($this->order as $key => $notused) {
$field = $this->field($key);
$result[$field] = PARAM_INT;
}
return $result;
}
/**
* Returns an array where keys are the cell names and the values
* are the weights.
*
* @return array
*/
public function cells() {
$result = array();
foreach ($this->order as $key => $rowid) {
$row = $this->rows[$rowid];
$field = $this->field($key);
foreach ($this->columns as $column) {
$result[$field] = $this->weight($row->number, $column->number);
}
}
return $result;
}
/**
* Makes HTML text (e.g.
* option or feedback texts) suitable for inline presentation in renderer.php.
*
* @param string html The HTML code.
*
* @return string the purified HTML code without paragraph elements and line breaks.
*/
public function make_html_inline($html) {
$html = preg_replace('~\s*<p>\s*~u', '', $html);
$html = preg_replace('~\s*</p>\s*~u', '<br />', $html);
$html = preg_replace('~(<br\s*/?>)+$~u', '', $html);
return trim($html);
}
/**
* Convert some part of the question text to plain text.
* This might be used,
* for example, by get_response_summary().
*
* @param string $text The HTML to reduce to plain text.
* @param int $format the FORMAT_... constant.
*
* @return string the equivalent plain text.
*/
public function html_to_text($text, $format) {
return question_utils::to_plain_text($text, $format);
}
/**
* Computes the final grade when "Multiple Attempts" or "Hints" are enabled
*
* @param array $responses Contains the user responses. 1st dimension = attempt, 2nd dimension = answers
* @param int $totaltries Not needed
*/
public function compute_final_grade($responses, $totaltries) {
$lastresponse = count($responses) - 1;
$numpoints = isset($responses[$lastresponse]) ? $this->grading()->grade_question($this, $responses[$lastresponse]) : 0;
return max(0, $numpoints - max(0, $lastresponse) * $this->penalty);
}
/**
* Disable those hint settings that we don't want when the student has selected
* more choices than the number of right choices.
* This avoids giving the game away.
*
* @param question_hint_with_parts $hint a hint.
*/
protected function disable_hint_settings_when_too_many_selected(question_hint_with_parts $hint) {
$hint->clearwrong = false;
}
public function get_hint($hintnumber, question_attempt $qa) {
$hint = parent::get_hint($hintnumber, $qa);
if (is_null($hint)) {
return $hint;
}
return $hint;
}
/**
* (non-PHPdoc).
*
* @see question_definition::check_file_access()
*/
public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
if ($component == 'qtype_kprime' && $filearea == 'optiontext') {
return true;
} else if ($component == 'qtype_kprime' && $filearea == 'feedbacktext') {
return true;
} else if ($component == 'question' && in_array($filearea,
array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback'
))) {
if ($this->editedquestion == 1) {
return true;
} else {
return $this->check_combined_feedback_file_access($qa, $options, $filearea);
}
} else if ($component == 'question' && $filearea == 'hint') {
return $this->check_hint_file_access($qa, $options, $args);
} else {
return parent::check_file_access($qa, $options, $component, $filearea, $args,
$forcedownload);
}
}
}