See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]
1 <?php 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 17 /** 18 * Multiple choice question definition classes. 19 * 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 24 */ 25 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 require_once($CFG->dirroot . '/question/type/questionbase.php'); 30 31 /** 32 * Base class for multiple choice questions. The parts that are common to 33 * single select and multiple select. 34 * 35 * @copyright 2009 The Open University 36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 37 */ 38 abstract class qtype_multichoice_base extends question_graded_automatically { 39 const LAYOUT_DROPDOWN = 0; 40 const LAYOUT_VERTICAL = 1; 41 const LAYOUT_HORIZONTAL = 2; 42 43 public $answers; 44 45 public $shuffleanswers; 46 public $answernumbering; 47 /** 48 * @var int standard instruction to be displayed if enabled. 49 */ 50 public $showstandardinstruction = 0; 51 public $layout = self::LAYOUT_VERTICAL; 52 53 public $correctfeedback; 54 public $correctfeedbackformat; 55 public $partiallycorrectfeedback; 56 public $partiallycorrectfeedbackformat; 57 public $incorrectfeedback; 58 public $incorrectfeedbackformat; 59 60 protected $order = null; 61 62 public function start_attempt(question_attempt_step $step, $variant) { 63 $this->order = array_keys($this->answers); 64 if ($this->shuffleanswers) { 65 shuffle($this->order); 66 } 67 $step->set_qt_var('_order', implode(',', $this->order)); 68 } 69 70 public function apply_attempt_state(question_attempt_step $step) { 71 $this->order = explode(',', $step->get_qt_var('_order')); 72 73 // Add any missing answers. Sometimes people edit questions after they 74 // have been attempted which breaks things. 75 foreach ($this->order as $ansid) { 76 if (isset($this->answers[$ansid])) { 77 continue; 78 } 79 $a = new stdClass(); 80 $a->id = 0; 81 $a->answer = html_writer::span(get_string('deletedchoice', 'qtype_multichoice'), 82 'notifyproblem'); 83 $a->answerformat = FORMAT_HTML; 84 $a->fraction = 0; 85 $a->feedback = ''; 86 $a->feedbackformat = FORMAT_HTML; 87 $this->answers[$ansid] = $this->qtype->make_answer($a); 88 $this->answers[$ansid]->answerformat = FORMAT_HTML; 89 } 90 } 91 92 public function get_question_summary() { 93 $question = $this->html_to_text($this->questiontext, $this->questiontextformat); 94 $choices = array(); 95 foreach ($this->order as $ansid) { 96 $choices[] = $this->html_to_text($this->answers[$ansid]->answer, 97 $this->answers[$ansid]->answerformat); 98 } 99 return $question . ': ' . implode('; ', $choices); 100 } 101 102 public function get_order(question_attempt $qa) { 103 $this->init_order($qa); 104 return $this->order; 105 } 106 107 protected function init_order(question_attempt $qa) { 108 if (is_null($this->order)) { 109 $this->order = explode(',', $qa->get_step(0)->get_qt_var('_order')); 110 } 111 } 112 113 public abstract function get_response(question_attempt $qa); 114 115 public abstract function is_choice_selected($response, $value); 116 117 public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) { 118 if ($component == 'question' && in_array($filearea, 119 array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback'))) { 120 return $this->check_combined_feedback_file_access($qa, $options, $filearea, $args); 121 122 } else if ($component == 'question' && $filearea == 'answer') { 123 $answerid = reset($args); // Itemid is answer id. 124 return in_array($answerid, $this->order); 125 126 } else if ($component == 'question' && $filearea == 'answerfeedback') { 127 $answerid = reset($args); // Itemid is answer id. 128 $response = $this->get_response($qa); 129 $isselected = false; 130 foreach ($this->order as $value => $ansid) { 131 if ($ansid == $answerid) { 132 $isselected = $this->is_choice_selected($response, $value); 133 break; 134 } 135 } 136 // Param $options->suppresschoicefeedback is a hack specific to the 137 // oumultiresponse question type. It would be good to refactor to 138 // avoid refering to it here. 139 return $options->feedback && empty($options->suppresschoicefeedback) && 140 $isselected; 141 142 } else if ($component == 'question' && $filearea == 'hint') { 143 return $this->check_hint_file_access($qa, $options, $args); 144 145 } else { 146 return parent::check_file_access($qa, $options, $component, $filearea, 147 $args, $forcedownload); 148 } 149 } 150 } 151 152 153 /** 154 * Represents a multiple choice question where only one choice should be selected. 155 * 156 * @copyright 2009 The Open University 157 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 158 */ 159 class qtype_multichoice_single_question extends qtype_multichoice_base { 160 public function get_renderer(moodle_page $page) { 161 return $page->get_renderer('qtype_multichoice', 'single'); 162 } 163 164 public function get_min_fraction() { 165 $minfraction = 0; 166 foreach ($this->answers as $ans) { 167 $minfraction = min($minfraction, $ans->fraction); 168 } 169 return $minfraction; 170 } 171 172 /** 173 * Return an array of the question type variables that could be submitted 174 * as part of a question of this type, with their types, so they can be 175 * properly cleaned. 176 * @return array variable name => PARAM_... constant. 177 */ 178 public function get_expected_data() { 179 return array('answer' => PARAM_INT); 180 } 181 182 public function summarise_response(array $response) { 183 if (!$this->is_complete_response($response)) { 184 return null; 185 } 186 $ansid = $this->order[$response['answer']]; 187 return $this->html_to_text($this->answers[$ansid]->answer, 188 $this->answers[$ansid]->answerformat); 189 } 190 191 public function classify_response(array $response) { 192 if (!$this->is_complete_response($response)) { 193 return array($this->id => question_classified_response::no_response()); 194 } 195 $choiceid = $this->order[$response['answer']]; 196 $ans = $this->answers[$choiceid]; 197 return array($this->id => new question_classified_response($choiceid, 198 $this->html_to_text($ans->answer, $ans->answerformat), $ans->fraction)); 199 } 200 201 public function get_correct_response() { 202 foreach ($this->order as $key => $answerid) { 203 if (question_state::graded_state_for_fraction( 204 $this->answers[$answerid]->fraction)->is_correct()) { 205 return array('answer' => $key); 206 } 207 } 208 return array(); 209 } 210 211 public function prepare_simulated_post_data($simulatedresponse) { 212 $ansid = 0; 213 foreach ($this->answers as $answer) { 214 if (clean_param($answer->answer, PARAM_NOTAGS) == $simulatedresponse['answer']) { 215 $ansid = $answer->id; 216 } 217 } 218 if ($ansid) { 219 return array('answer' => array_search($ansid, $this->order)); 220 } else { 221 return array(); 222 } 223 } 224 225 public function get_student_response_values_for_simulation($postdata) { 226 if (!isset($postdata['answer'])) { 227 return array(); 228 } else { 229 $answer = $this->answers[$this->order[$postdata['answer']]]; 230 return array('answer' => clean_param($answer->answer, PARAM_NOTAGS)); 231 } 232 } 233 234 public function is_same_response(array $prevresponse, array $newresponse) { 235 if (!$this->is_complete_response($prevresponse)) { 236 $prevresponse = []; 237 } 238 if (!$this->is_complete_response($newresponse)) { 239 $newresponse = []; 240 } 241 return question_utils::arrays_same_at_key($prevresponse, $newresponse, 'answer'); 242 } 243 244 public function is_complete_response(array $response) { 245 return array_key_exists('answer', $response) && $response['answer'] !== '' 246 && (string) $response['answer'] !== '-1'; 247 } 248 249 public function is_gradable_response(array $response) { 250 return $this->is_complete_response($response); 251 } 252 253 public function grade_response(array $response) { 254 if (array_key_exists('answer', $response) && 255 array_key_exists($response['answer'], $this->order)) { 256 $fraction = $this->answers[$this->order[$response['answer']]]->fraction; 257 } else { 258 $fraction = 0; 259 } 260 return array($fraction, question_state::graded_state_for_fraction($fraction)); 261 } 262 263 public function get_validation_error(array $response) { 264 if ($this->is_gradable_response($response)) { 265 return ''; 266 } 267 return get_string('pleaseselectananswer', 'qtype_multichoice'); 268 } 269 270 public function get_response(question_attempt $qa) { 271 return $qa->get_last_qt_var('answer', -1); 272 } 273 274 public function is_choice_selected($response, $value) { 275 return (string) $response === (string) $value; 276 } 277 } 278 279 280 /** 281 * Represents a multiple choice question where multiple choices can be selected. 282 * 283 * @copyright 2009 The Open University 284 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 285 */ 286 class qtype_multichoice_multi_question extends qtype_multichoice_base { 287 public function get_renderer(moodle_page $page) { 288 return $page->get_renderer('qtype_multichoice', 'multi'); 289 } 290 291 public function get_min_fraction() { 292 return 0; 293 } 294 295 public function clear_wrong_from_response(array $response) { 296 foreach ($this->order as $key => $ans) { 297 if (array_key_exists($this->field($key), $response) && 298 question_state::graded_state_for_fraction( 299 $this->answers[$ans]->fraction)->is_incorrect()) { 300 $response[$this->field($key)] = 0; 301 } 302 } 303 return $response; 304 } 305 306 public function get_num_parts_right(array $response) { 307 $numright = 0; 308 foreach ($this->order as $key => $ans) { 309 $fieldname = $this->field($key); 310 if (!array_key_exists($fieldname, $response) || !$response[$fieldname]) { 311 continue; 312 } 313 314 if (!question_state::graded_state_for_fraction( 315 $this->answers[$ans]->fraction)->is_incorrect()) { 316 $numright += 1; 317 } 318 } 319 return array($numright, count($this->order)); 320 } 321 322 /** 323 * @param int $key choice number 324 * @return string the question-type variable name. 325 */ 326 protected function field($key) { 327 return 'choice' . $key; 328 } 329 330 public function get_expected_data() { 331 $expected = array(); 332 foreach ($this->order as $key => $notused) { 333 $expected[$this->field($key)] = PARAM_BOOL; 334 } 335 return $expected; 336 } 337 338 public function summarise_response(array $response) { 339 $selectedchoices = array(); 340 foreach ($this->order as $key => $ans) { 341 $fieldname = $this->field($key); 342 if (array_key_exists($fieldname, $response) && $response[$fieldname]) { 343 $selectedchoices[] = $this->html_to_text($this->answers[$ans]->answer, 344 $this->answers[$ans]->answerformat); 345 } 346 } 347 if (empty($selectedchoices)) { 348 return null; 349 } 350 return implode('; ', $selectedchoices); 351 } 352 353 public function classify_response(array $response) { 354 $selectedchoices = array(); 355 foreach ($this->order as $key => $ansid) { 356 $fieldname = $this->field($key); 357 if (array_key_exists($fieldname, $response) && $response[$fieldname]) { 358 $selectedchoices[$ansid] = 1; 359 } 360 } 361 $choices = array(); 362 foreach ($this->answers as $ansid => $ans) { 363 if (isset($selectedchoices[$ansid])) { 364 $choices[$ansid] = new question_classified_response($ansid, 365 $this->html_to_text($ans->answer, $ans->answerformat), $ans->fraction); 366 } 367 } 368 return $choices; 369 } 370 371 public function get_correct_response() { 372 $response = array(); 373 foreach ($this->order as $key => $ans) { 374 if (!question_state::graded_state_for_fraction( 375 $this->answers[$ans]->fraction)->is_incorrect()) { 376 $response[$this->field($key)] = 1; 377 } 378 } 379 return $response; 380 } 381 382 public function prepare_simulated_post_data($simulatedresponse) { 383 $postdata = array(); 384 foreach ($simulatedresponse as $ans => $checked) { 385 foreach ($this->answers as $ansid => $answer) { 386 if (clean_param($answer->answer, PARAM_NOTAGS) == $ans) { 387 $fieldno = array_search($ansid, $this->order); 388 $postdata[$this->field($fieldno)] = $checked; 389 break; 390 } 391 } 392 } 393 return $postdata; 394 } 395 396 public function get_student_response_values_for_simulation($postdata) { 397 $simulatedresponse = array(); 398 foreach ($this->order as $fieldno => $ansid) { 399 if (isset($postdata[$this->field($fieldno)])) { 400 $checked = $postdata[$this->field($fieldno)]; 401 $simulatedresponse[clean_param($this->answers[$ansid]->answer, PARAM_NOTAGS)] = $checked; 402 } 403 } 404 ksort($simulatedresponse); 405 return $simulatedresponse; 406 } 407 408 public function is_same_response(array $prevresponse, array $newresponse) { 409 foreach ($this->order as $key => $notused) { 410 $fieldname = $this->field($key); 411 if (!question_utils::arrays_same_at_key_integer($prevresponse, $newresponse, $fieldname)) { 412 return false; 413 } 414 } 415 return true; 416 } 417 418 public function is_complete_response(array $response) { 419 foreach ($this->order as $key => $notused) { 420 if (!empty($response[$this->field($key)])) { 421 return true; 422 } 423 } 424 return false; 425 } 426 427 public function is_gradable_response(array $response) { 428 return $this->is_complete_response($response); 429 } 430 431 /** 432 * @param array $response responses, as returned by 433 * {@link question_attempt_step::get_qt_data()}. 434 * @return int the number of choices that were selected. in this response. 435 */ 436 public function get_num_selected_choices(array $response) { 437 $numselected = 0; 438 foreach ($response as $key => $value) { 439 // Response keys starting with _ are internal values like _order, so ignore them. 440 if (!empty($value) && $key[0] != '_') { 441 $numselected += 1; 442 } 443 } 444 return $numselected; 445 } 446 447 /** 448 * @return int the number of choices that are correct. 449 */ 450 public function get_num_correct_choices() { 451 $numcorrect = 0; 452 foreach ($this->answers as $ans) { 453 if (!question_state::graded_state_for_fraction($ans->fraction)->is_incorrect()) { 454 $numcorrect += 1; 455 } 456 } 457 return $numcorrect; 458 } 459 460 public function grade_response(array $response) { 461 $fraction = 0; 462 foreach ($this->order as $key => $ansid) { 463 if (!empty($response[$this->field($key)])) { 464 $fraction += $this->answers[$ansid]->fraction; 465 } 466 } 467 $fraction = min(max(0, $fraction), 1.0); 468 return array($fraction, question_state::graded_state_for_fraction($fraction)); 469 } 470 471 public function get_validation_error(array $response) { 472 if ($this->is_gradable_response($response)) { 473 return ''; 474 } 475 return get_string('pleaseselectatleastoneanswer', 'qtype_multichoice'); 476 } 477 478 /** 479 * Disable those hint settings that we don't want when the student has selected 480 * more choices than the number of right choices. This avoids giving the game away. 481 * @param question_hint_with_parts $hint a hint. 482 */ 483 protected function disable_hint_settings_when_too_many_selected( 484 question_hint_with_parts $hint) { 485 $hint->clearwrong = false; 486 } 487 488 public function get_hint($hintnumber, question_attempt $qa) { 489 $hint = parent::get_hint($hintnumber, $qa); 490 if (is_null($hint)) { 491 return $hint; 492 } 493 494 if ($this->get_num_selected_choices($qa->get_last_qt_data()) > 495 $this->get_num_correct_choices()) { 496 $hint = clone($hint); 497 $this->disable_hint_settings_when_too_many_selected($hint); 498 } 499 return $hint; 500 } 501 502 public function get_response(question_attempt $qa) { 503 return $qa->get_last_qt_data(); 504 } 505 506 public function is_choice_selected($response, $value) { 507 return !empty($response['choice' . $value]); 508 } 509 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body