mirrored from git://git.moodle.org/moodle.git
/
question.php
410 lines (366 loc) · 16.2 KB
/
question.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
<?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/>.
/**
* Multianswer question definition class.
*
* @package qtype
* @subpackage multianswer
* @copyright 2010 Pierre Pichet
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->dirroot . '/question/type/questionbase.php');
require_once($CFG->dirroot . '/question/type/shortanswer/question.php');
require_once($CFG->dirroot . '/question/type/numerical/question.php');
require_once($CFG->dirroot . '/question/type/multichoice/question.php');
/**
* Represents a multianswer question.
*
* A multi-answer question is made of of several subquestions of various types.
* You can think of it as an application of the composite pattern to qusetion
* types.
*
* @copyright 2010 Pierre Pichet
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_multianswer_question extends question_graded_automatically_with_countback {
/** @var array of question_graded_automatically. */
public $subquestions = array();
/**
* @var array place number => insex in the $subquestions array. Places are
* numbered from 1.
*/
public $places;
/**
* @var array of strings, one longer than $places, which is achieved by
* indexing from 0. The bits of question text that go between the subquestions.
*/
public $textfragments;
/**
* Get a question_attempt_step_subquestion_adapter
* @param question_attempt_step $step the step to adapt.
* @param int $i the subquestion index.
* @return question_attempt_step_subquestion_adapter.
*/
protected function get_substep($step, $i) {
return new question_attempt_step_subquestion_adapter($step, 'sub' . $i . '_');
}
public function start_attempt(question_attempt_step $step, $variant) {
foreach ($this->subquestions as $i => $subq) {
$subq->start_attempt($this->get_substep($step, $i), $variant);
}
}
public function apply_attempt_state(question_attempt_step $step) {
foreach ($this->subquestions as $i => $subq) {
$subq->apply_attempt_state($this->get_substep($step, $i));
}
}
public function validate_can_regrade_with_other_version(question_definition $otherversion): ?string {
$basemessage = parent::validate_can_regrade_with_other_version($otherversion);
if ($basemessage) {
return $basemessage;
}
if (count($this->subquestions) != count($otherversion->subquestions)) {
return get_string('regradeissuenumsubquestionschanged', 'qtype_multianswer');
}
foreach ($this->subquestions as $i => $subq) {
$subqmessage = $subq->validate_can_regrade_with_other_version($otherversion->subquestions[$i]);
if ($subqmessage) {
return $subqmessage;
}
}
return null;
}
public function update_attempt_state_data_for_new_version(
question_attempt_step $oldstep, question_definition $oldquestion) {
parent::update_attempt_state_data_for_new_version($oldstep, $oldquestion);
$result = [];
foreach ($this->subquestions as $i => $subq) {
$substep = $this->get_substep($oldstep, $i);
$statedata = $subq->update_attempt_state_data_for_new_version(
$substep, $oldquestion->subquestions[$i]);
foreach ($statedata as $name => $value) {
$result[$substep->add_prefix($name)] = $value;
}
}
return $result;
}
public function get_question_summary() {
$summary = $this->html_to_text($this->questiontext, $this->questiontextformat);
foreach ($this->subquestions as $i => $subq) {
switch ($subq->qtype->name()) {
case 'multichoice':
$choices = array();
$dummyqa = new question_attempt($subq, $this->contextid);
foreach ($subq->get_order($dummyqa) as $ansid) {
$choices[] = $this->html_to_text($subq->answers[$ansid]->answer,
$subq->answers[$ansid]->answerformat);
}
$answerbit = '{' . implode('; ', $choices) . '}';
break;
case 'numerical':
case 'shortanswer':
$answerbit = '_____';
break;
default:
$answerbit = '{ERR unknown sub-question type}';
}
$summary = str_replace('{#' . $i . '}', $answerbit, $summary);
}
return $summary;
}
public function get_min_fraction() {
$fractionsum = 0;
$fractionmax = 0;
foreach ($this->subquestions as $i => $subq) {
$fractionmax += $subq->defaultmark;
$fractionsum += $subq->defaultmark * $subq->get_min_fraction();
}
return $fractionsum / (!empty($this->subquestions) ? $fractionmax : 1);
}
public function get_max_fraction() {
$fractionsum = 0;
$fractionmax = 0;
foreach ($this->subquestions as $i => $subq) {
$fractionmax += $subq->defaultmark;
$fractionsum += $subq->defaultmark * $subq->get_max_fraction();
}
return $fractionsum / (!empty($this->subquestions) ? $fractionmax : 1);
}
public function get_expected_data() {
$expected = array();
foreach ($this->subquestions as $i => $subq) {
$substep = $this->get_substep(null, $i);
foreach ($subq->get_expected_data() as $name => $type) {
if ($subq->qtype->name() == 'multichoice' &&
$subq->layout == qtype_multichoice_base::LAYOUT_DROPDOWN) {
// Hack or MC inline does not work.
$expected[$substep->add_prefix($name)] = PARAM_RAW;
} else {
$expected[$substep->add_prefix($name)] = $type;
}
}
}
return $expected;
}
public function get_correct_response() {
$right = array();
foreach ($this->subquestions as $i => $subq) {
$substep = $this->get_substep(null, $i);
foreach ($subq->get_correct_response() as $name => $type) {
$right[$substep->add_prefix($name)] = $type;
}
}
return $right;
}
public function prepare_simulated_post_data($simulatedresponse) {
$postdata = array();
foreach ($this->subquestions as $i => $subq) {
$substep = $this->get_substep(null, $i);
foreach ($subq->prepare_simulated_post_data($simulatedresponse[$i]) as $name => $value) {
$postdata[$substep->add_prefix($name)] = $value;
}
}
return $postdata;
}
public function get_student_response_values_for_simulation($postdata) {
$simulatedresponse = array();
foreach ($this->subquestions as $i => $subq) {
$substep = $this->get_substep(null, $i);
$subqpostdata = $substep->filter_array($postdata);
$subqsimulatedresponse = $subq->get_student_response_values_for_simulation($subqpostdata);
foreach ($subqsimulatedresponse as $subresponsekey => $responsevalue) {
$simulatedresponse[$i.'.'.$subresponsekey] = $responsevalue;
}
}
ksort($simulatedresponse);
return $simulatedresponse;
}
public function is_complete_response(array $response) {
foreach ($this->subquestions as $i => $subq) {
$substep = $this->get_substep(null, $i);
if (!$subq->is_complete_response($substep->filter_array($response))) {
return false;
}
}
return true;
}
public function is_gradable_response(array $response) {
foreach ($this->subquestions as $i => $subq) {
$substep = $this->get_substep(null, $i);
if ($subq->is_gradable_response($substep->filter_array($response))) {
return true;
}
}
return false;
}
public function is_same_response(array $prevresponse, array $newresponse) {
foreach ($this->subquestions as $i => $subq) {
$substep = $this->get_substep(null, $i);
if (!$subq->is_same_response($substep->filter_array($prevresponse),
$substep->filter_array($newresponse))) {
return false;
}
}
return true;
}
public function get_validation_error(array $response) {
if ($this->is_complete_response($response)) {
return '';
}
return get_string('pleaseananswerallparts', 'qtype_multianswer');
}
/**
* Used by grade_response to combine the states of the subquestions.
* The combined state is accumulates in $overallstate. That will be right
* if all the separate states are right; and wrong if all the separate states
* are wrong, otherwise, it will be partially right.
* @param question_state $overallstate the result so far.
* @param question_state $newstate the new state to add to the combination.
* @return question_state the new combined state.
*/
protected function combine_states($overallstate, $newstate) {
if (is_null($overallstate)) {
return $newstate;
} else if ($overallstate == question_state::$gaveup &&
$newstate == question_state::$gaveup) {
return question_state::$gaveup;
} else if ($overallstate == question_state::$gaveup &&
$newstate == question_state::$gradedwrong) {
return question_state::$gradedwrong;
} else if ($overallstate == question_state::$gradedwrong &&
$newstate == question_state::$gaveup) {
return question_state::$gradedwrong;
} else if ($overallstate == question_state::$gradedwrong &&
$newstate == question_state::$gradedwrong) {
return question_state::$gradedwrong;
} else if ($overallstate == question_state::$gradedright &&
$newstate == question_state::$gradedright) {
return question_state::$gradedright;
} else {
return question_state::$gradedpartial;
}
}
public function grade_response(array $response) {
$overallstate = null;
$fractionsum = 0;
$fractionmax = 0;
foreach ($this->subquestions as $i => $subq) {
$fractionmax += $subq->defaultmark;
$substep = $this->get_substep(null, $i);
$subresp = $substep->filter_array($response);
if (!$subq->is_gradable_response($subresp)) {
$overallstate = $this->combine_states($overallstate, question_state::$gaveup);
} else {
list($subfraction, $newstate) = $subq->grade_response($subresp);
$fractionsum += $subfraction * $subq->defaultmark;
$overallstate = $this->combine_states($overallstate, $newstate);
}
}
return array($fractionsum / $fractionmax, $overallstate);
}
public function clear_wrong_from_response(array $response) {
foreach ($this->subquestions as $i => $subq) {
$substep = $this->get_substep(null, $i);
$subresp = $substep->filter_array($response);
list($subfraction, $newstate) = $subq->grade_response($subresp);
if ($newstate != question_state::$gradedright) {
foreach ($subresp as $ind => $resp) {
if ($subq->qtype == 'multichoice' && ($subq->layout == qtype_multichoice_base::LAYOUT_VERTICAL
|| $subq->layout == qtype_multichoice_base::LAYOUT_HORIZONTAL)) {
$response[$substep->add_prefix($ind)] = '-1';
} else {
$response[$substep->add_prefix($ind)] = '';
}
}
}
}
return $response;
}
public function get_num_parts_right(array $response) {
$numright = 0;
foreach ($this->subquestions as $i => $subq) {
$substep = $this->get_substep(null, $i);
$subresp = $substep->filter_array($response);
list($subfraction, $newstate) = $subq->grade_response($subresp);
if ($newstate == question_state::$gradedright) {
$numright += 1;
}
}
return array($numright, count($this->subquestions));
}
public function compute_final_grade($responses, $totaltries) {
$fractionsum = 0;
$fractionmax = 0;
foreach ($this->subquestions as $i => $subq) {
$fractionmax += $subq->defaultmark;
$lastresponse = array();
$lastchange = 0;
$subfraction = 0;
foreach ($responses as $responseindex => $response) {
$substep = $this->get_substep(null, $i);
$subresp = $substep->filter_array($response);
if ($subq->is_same_response($lastresponse, $subresp)) {
continue;
}
$lastresponse = $subresp;
$lastchange = $responseindex;
list($subfraction, $newstate) = $subq->grade_response($subresp);
}
$fractionsum += $subq->defaultmark * max(0, $subfraction - $lastchange * $this->penalty);
}
return $fractionsum / $fractionmax;
}
public function summarise_response(array $response) {
$summary = array();
foreach ($this->subquestions as $i => $subq) {
$substep = $this->get_substep(null, $i);
$a = new stdClass();
$a->i = $i;
$a->response = $subq->summarise_response($substep->filter_array($response));
$summary[] = get_string('subqresponse', 'qtype_multianswer', $a);
}
return implode('; ', $summary);
}
public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
if ($component == 'question' && $filearea == 'answer') {
return true;
} else if ($component == 'question' && $filearea == 'answerfeedback') {
// Full logic to control which feedbacks a student can see is too complex.
// Just allow access to all images. There is a theoretical chance the
// students could see files they are not meant to see by guessing URLs,
// but it is remote.
return $options->feedback;
} 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);
}
}
/**
* Return the question settings that define this question as structured data.
*
* @param question_attempt $qa the current attempt for which we are exporting the settings.
* @param question_display_options $options the question display options which say which aspects of the question
* should be visible.
* @return mixed structure representing the question settings. In web services, this will be JSON-encoded.
*/
public function get_question_definition_for_external_rendering(question_attempt $qa, question_display_options $options) {
// Empty implementation for now in order to avoid debugging in core questions (generated in the parent class),
// ideally, we should return as much as settings as possible (depending on the state and display options).
return null;
}
}