MDL-36571 qtype multichoice: don't corrupt unicode characters.
[moodle.git] / question / type / multichoice / question.php
CommitLineData
c9c989a0 1<?php
c9c989a0
TH
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
c9c989a0
TH
17/**
18 * Multiple choice question definition classes.
19 *
7764183a
TH
20 * @package qtype
21 * @subpackage multichoice
22 * @copyright 2009 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
c9c989a0
TH
24 */
25
26
a17b297d
TH
27defined('MOODLE_INTERNAL') || die();
28
29
c9c989a0
TH
30/**
31 * Base class for multiple choice questions. The parts that are common to
32 * single select and multiple select.
33 *
7764183a
TH
34 * @copyright 2009 The Open University
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
c9c989a0
TH
36 */
37abstract class qtype_multichoice_base extends question_graded_automatically {
38 const LAYOUT_DROPDOWN = 0;
39 const LAYOUT_VERTICAL = 1;
40 const LAYOUT_HORIZONTAL = 2;
41
42 public $answers;
43
44 public $shuffleanswers;
45 public $answernumbering;
46 public $layout = self::LAYOUT_VERTICAL;
1c2ed7c5 47
c9c989a0 48 public $correctfeedback;
1c2ed7c5 49 public $correctfeedbackformat;
c9c989a0 50 public $partiallycorrectfeedback;
1c2ed7c5 51 public $partiallycorrectfeedbackformat;
c9c989a0 52 public $incorrectfeedback;
1c2ed7c5 53 public $incorrectfeedbackformat;
c9c989a0
TH
54
55 protected $order = null;
56
1da821bb 57 public function start_attempt(question_attempt_step $step, $variant) {
ef31a283
TH
58 $this->order = array_keys($this->answers);
59 if ($this->shuffleanswers) {
60 shuffle($this->order);
c9c989a0 61 }
ef31a283
TH
62 $step->set_qt_var('_order', implode(',', $this->order));
63 }
64
65 public function apply_attempt_state(question_attempt_step $step) {
66 $this->order = explode(',', $step->get_qt_var('_order'));
c9c989a0
TH
67 }
68
69 public function get_question_summary() {
22cebed5 70 $question = $this->html_to_text($this->questiontext, $this->questiontextformat);
c9c989a0
TH
71 $choices = array();
72 foreach ($this->order as $ansid) {
22cebed5
TH
73 $choices[] = $this->html_to_text($this->answers[$ansid]->answer,
74 $this->answers[$ansid]->answerformat);
c9c989a0
TH
75 }
76 return $question . ': ' . implode('; ', $choices);
77 }
78
79 public function get_order(question_attempt $qa) {
80 $this->init_order($qa);
81 return $this->order;
82 }
83
84 protected function init_order(question_attempt $qa) {
85 if (is_null($this->order)) {
86 $this->order = explode(',', $qa->get_step(0)->get_qt_var('_order'));
87 }
88 }
89
c7df5006 90 public abstract function get_response(question_attempt $qa);
c9c989a0 91
c7df5006 92 public abstract function is_choice_selected($response, $value);
c9c989a0 93
93cadb1e
TH
94 public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
95 if ($component == 'question' && in_array($filearea,
c9c989a0 96 array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback'))) {
93cadb1e 97 return $this->check_combined_feedback_file_access($qa, $options, $filearea);
c9c989a0 98
2a6c5c52 99 } else if ($component == 'question' && $filearea == 'answer') {
100 $answerid = reset($args); // itemid is answer id.
101 return in_array($answerid, $this->order);
102
c9c989a0
TH
103 } else if ($component == 'question' && $filearea == 'answerfeedback') {
104 $answerid = reset($args); // itemid is answer id.
105 $response = $this->get_response($qa);
106 $isselected = false;
107 foreach ($this->order as $value => $ansid) {
108 if ($ansid == $answerid) {
109 $isselected = $this->is_choice_selected($response, $value);
110 break;
111 }
112 }
113 // $options->suppresschoicefeedback is a hack specific to the
114 // oumultiresponse question type. It would be good to refactor to
115 // avoid refering to it here.
116 return $options->feedback && empty($options->suppresschoicefeedback) &&
117 $isselected;
118
93cadb1e
TH
119 } else if ($component == 'question' && $filearea == 'hint') {
120 return $this->check_hint_file_access($qa, $options, $args);
121
c9c989a0 122 } else {
12039f2e
TH
123 return parent::check_file_access($qa, $options, $component, $filearea,
124 $args, $forcedownload);
c9c989a0
TH
125 }
126 }
35c9b652
TH
127
128 public function make_html_inline($html) {
4c1a35e3
TH
129 $html = preg_replace('~\s*<p>\s*~u', '', $html);
130 $html = preg_replace('~\s*</p>\s*~u', '<br />', $html);
131 $html = preg_replace('~(<br\s*/?>)+$~u', '', $html);
132 return trim($html);
35c9b652 133 }
c9c989a0
TH
134}
135
136
137/**
138 * Represents a multiple choice question where only one choice should be selected.
139 *
7764183a
TH
140 * @copyright 2009 The Open University
141 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
c9c989a0
TH
142 */
143class qtype_multichoice_single_question extends qtype_multichoice_base {
2daffca5
TH
144 public function get_renderer(moodle_page $page) {
145 return $page->get_renderer('qtype_multichoice', 'single');
c9c989a0
TH
146 }
147
148 public function get_min_fraction() {
149 $minfraction = 0;
150 foreach ($this->answers as $ans) {
151 $minfraction = min($minfraction, $ans->fraction);
152 }
153 return $minfraction;
154 }
155
156 /**
157 * Return an array of the question type variables that could be submitted
158 * as part of a question of this type, with their types, so they can be
159 * properly cleaned.
160 * @return array variable name => PARAM_... constant.
161 */
162 public function get_expected_data() {
163 return array('answer' => PARAM_INT);
164 }
165
166 public function summarise_response(array $response) {
167 if (!array_key_exists('answer', $response) ||
168 !array_key_exists($response['answer'], $this->order)) {
169 return null;
170 }
171 $ansid = $this->order[$response['answer']];
22cebed5
TH
172 return $this->html_to_text($this->answers[$ansid]->answer,
173 $this->answers[$ansid]->answerformat);
c9c989a0
TH
174 }
175
176 public function classify_response(array $response) {
177 if (!array_key_exists('answer', $response) ||
178 !array_key_exists($response['answer'], $this->order)) {
179 return array($this->id => question_classified_response::no_response());
180 }
181 $choiceid = $this->order[$response['answer']];
182 $ans = $this->answers[$choiceid];
183 return array($this->id => new question_classified_response($choiceid,
22cebed5 184 $this->html_to_text($ans->answer, $ans->answerformat), $ans->fraction));
c9c989a0
TH
185 }
186
187 public function get_correct_response() {
188 foreach ($this->order as $key => $answerid) {
189 if (question_state::graded_state_for_fraction(
190 $this->answers[$answerid]->fraction)->is_correct()) {
191 return array('answer' => $key);
192 }
193 }
194 return array();
195 }
196
197 public function is_same_response(array $prevresponse, array $newresponse) {
198 return question_utils::arrays_same_at_key($prevresponse, $newresponse, 'answer');
199 }
200
201 public function is_complete_response(array $response) {
0656c0a4 202 return array_key_exists('answer', $response) && $response['answer'] !== '';
c9c989a0
TH
203 }
204
205 public function is_gradable_response(array $response) {
206 return $this->is_complete_response($response);
207 }
208
209 public function grade_response(array $response) {
210 if (array_key_exists('answer', $response) &&
211 array_key_exists($response['answer'], $this->order)) {
212 $fraction = $this->answers[$this->order[$response['answer']]]->fraction;
213 } else {
214 $fraction = 0;
215 }
216 return array($fraction, question_state::graded_state_for_fraction($fraction));
217 }
218
219 public function get_validation_error(array $response) {
220 if ($this->is_gradable_response($response)) {
221 return '';
222 }
223 return get_string('pleaseselectananswer', 'qtype_multichoice');
224 }
225
226 public function get_response(question_attempt $qa) {
227 return $qa->get_last_qt_var('answer', -1);
228 }
229
230 public function is_choice_selected($response, $value) {
7ac7977c 231 return (string) $response === (string) $value;
c9c989a0
TH
232 }
233}
234
235
236/**
237 * Represents a multiple choice question where multiple choices can be selected.
238 *
7764183a
TH
239 * @copyright 2009 The Open University
240 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
c9c989a0
TH
241 */
242class qtype_multichoice_multi_question extends qtype_multichoice_base {
2daffca5
TH
243 public function get_renderer(moodle_page $page) {
244 return $page->get_renderer('qtype_multichoice', 'multi');
c9c989a0
TH
245 }
246
247 public function get_min_fraction() {
248 return 0;
249 }
250
251 public function clear_wrong_from_response(array $response) {
252 foreach ($this->order as $key => $ans) {
253 if (array_key_exists($this->field($key), $response) &&
254 question_state::graded_state_for_fraction(
255 $this->answers[$ans]->fraction)->is_incorrect()) {
256 $response[$this->field($key)] = 0;
257 }
258 }
259 return $response;
260 }
261
262 public function get_num_parts_right(array $response) {
263 $numright = 0;
264 foreach ($this->order as $key => $ans) {
265 $fieldname = $this->field($key);
266 if (!array_key_exists($fieldname, $response) || !$response[$fieldname]) {
267 continue;
268 }
269
270 if (!question_state::graded_state_for_fraction(
271 $this->answers[$ans]->fraction)->is_incorrect()) {
272 $numright += 1;
273 }
274 }
275 return array($numright, count($this->order));
276 }
277
278 /**
f7970e3c 279 * @param int $key choice number
c9c989a0
TH
280 * @return string the question-type variable name.
281 */
282 protected function field($key) {
283 return 'choice' . $key;
284 }
285
286 public function get_expected_data() {
287 $expected = array();
288 foreach ($this->order as $key => $notused) {
289 $expected[$this->field($key)] = PARAM_BOOL;
290 }
291 return $expected;
292 }
293
294 public function summarise_response(array $response) {
295 $selectedchoices = array();
296 foreach ($this->order as $key => $ans) {
297 $fieldname = $this->field($key);
298 if (array_key_exists($fieldname, $response) && $response[$fieldname]) {
22cebed5
TH
299 $selectedchoices[] = $this->html_to_text($this->answers[$ans]->answer,
300 $this->answers[$ans]->answerformat);
c9c989a0
TH
301 }
302 }
303 if (empty($selectedchoices)) {
304 return null;
305 }
306 return implode('; ', $selectedchoices);
307 }
308
309 public function classify_response(array $response) {
310 $selectedchoices = array();
311 foreach ($this->order as $key => $ansid) {
312 $fieldname = $this->field($key);
313 if (array_key_exists($fieldname, $response) && $response[$fieldname]) {
314 $selectedchoices[$ansid] = 1;
315 }
316 }
317 $choices = array();
318 foreach ($this->answers as $ansid => $ans) {
319 if (isset($selectedchoices[$ansid])) {
320 $choices[$ansid] = new question_classified_response($ansid,
22cebed5 321 $this->html_to_text($ans->answer, $ans->answerformat), $ans->fraction);
c9c989a0
TH
322 }
323 }
324 return $choices;
325 }
326
327 public function get_correct_response() {
328 $response = array();
329 foreach ($this->order as $key => $ans) {
330 if (!question_state::graded_state_for_fraction(
331 $this->answers[$ans]->fraction)->is_incorrect()) {
332 $response[$this->field($key)] = 1;
333 }
334 }
335 return $response;
336 }
337
338 public function is_same_response(array $prevresponse, array $newresponse) {
339 foreach ($this->order as $key => $notused) {
340 $fieldname = $this->field($key);
341 if (!question_utils::arrays_same_at_key($prevresponse, $newresponse, $fieldname)) {
342 return false;
343 }
344 }
345 return true;
346 }
347
348 public function is_complete_response(array $response) {
349 foreach ($this->order as $key => $notused) {
350 if (!empty($response[$this->field($key)])) {
351 return true;
352 }
353 }
354 return false;
355 }
356
357 public function is_gradable_response(array $response) {
358 return $this->is_complete_response($response);
359 }
360
361 /**
12039f2e
TH
362 * @param array $response responses, as returned by
363 * {@link question_attempt_step::get_qt_data()}.
f7970e3c 364 * @return int the number of choices that were selected. in this response.
c9c989a0
TH
365 */
366 public function get_num_selected_choices(array $response) {
367 $numselected = 0;
368 foreach ($response as $key => $value) {
369 if (!empty($value)) {
370 $numselected += 1;
371 }
372 }
373 return $numselected;
374 }
375
376 /**
f7970e3c 377 * @return int the number of choices that are correct.
c9c989a0
TH
378 */
379 public function get_num_correct_choices() {
380 $numcorrect = 0;
381 foreach ($this->answers as $ans) {
382 if (!question_state::graded_state_for_fraction($ans->fraction)->is_incorrect()) {
383 $numcorrect += 1;
384 }
385 }
386 return $numcorrect;
387 }
388
389 public function grade_response(array $response) {
390 $fraction = 0;
391 foreach ($this->order as $key => $ansid) {
392 if (!empty($response[$this->field($key)])) {
393 $fraction += $this->answers[$ansid]->fraction;
394 }
395 }
396 $fraction = min(max(0, $fraction), 1.0);
397 return array($fraction, question_state::graded_state_for_fraction($fraction));
398 }
399
400 public function get_validation_error(array $response) {
401 if ($this->is_gradable_response($response)) {
402 return '';
403 }
404 return get_string('pleaseselectatleastoneanswer', 'qtype_multichoice');
405 }
406
407 /**
408 * Disable those hint settings that we don't want when the student has selected
409 * more choices than the number of right choices. This avoids giving the game away.
410 * @param question_hint_with_parts $hint a hint.
411 */
12039f2e
TH
412 protected function disable_hint_settings_when_too_many_selected(
413 question_hint_with_parts $hint) {
c9c989a0
TH
414 $hint->clearwrong = false;
415 }
416
417 public function get_hint($hintnumber, question_attempt $qa) {
418 $hint = parent::get_hint($hintnumber, $qa);
419 if (is_null($hint)) {
420 return $hint;
421 }
422
423 if ($this->get_num_selected_choices($qa->get_last_qt_data()) >
424 $this->get_num_correct_choices()) {
425 $hint = clone($hint);
426 $this->disable_hint_settings_when_too_many_selected($hint);
427 }
428 return $hint;
429 }
430
431 public function get_response(question_attempt $qa) {
432 return $qa->get_last_qt_data();
433 }
434
435 public function is_choice_selected($response, $value) {
436 return !empty($response['choice' . $value]);
437 }
438}