[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/question/type/multichoice/ -> question.php (source)

   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  
  30  /**
  31   * Base class for multiple choice questions. The parts that are common to
  32   * single select and multiple select.
  33   *
  34   * @copyright  2009 The Open University
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  abstract 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;
  47  
  48      public $correctfeedback;
  49      public $correctfeedbackformat;
  50      public $partiallycorrectfeedback;
  51      public $partiallycorrectfeedbackformat;
  52      public $incorrectfeedback;
  53      public $incorrectfeedbackformat;
  54  
  55      protected $order = null;
  56  
  57      public function start_attempt(question_attempt_step $step, $variant) {
  58          $this->order = array_keys($this->answers);
  59          if ($this->shuffleanswers) {
  60              shuffle($this->order);
  61          }
  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'));
  67      }
  68  
  69      public function get_question_summary() {
  70          $question = $this->html_to_text($this->questiontext, $this->questiontextformat);
  71          $choices = array();
  72          foreach ($this->order as $ansid) {
  73              $choices[] = $this->html_to_text($this->answers[$ansid]->answer,
  74                      $this->answers[$ansid]->answerformat);
  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  
  90      public abstract function get_response(question_attempt $qa);
  91  
  92      public abstract function is_choice_selected($response, $value);
  93  
  94      public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
  95          if ($component == 'question' && in_array($filearea,
  96                  array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback'))) {
  97              return $this->check_combined_feedback_file_access($qa, $options, $filearea);
  98  
  99          } else if ($component == 'question' && $filearea == 'answer') {
 100              $answerid = reset($args); // Itemid is answer id.
 101              return  in_array($answerid, $this->order);
 102  
 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              // Param $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  
 119          } else if ($component == 'question' && $filearea == 'hint') {
 120              return $this->check_hint_file_access($qa, $options, $args);
 121  
 122          } else {
 123              return parent::check_file_access($qa, $options, $component, $filearea,
 124                      $args, $forcedownload);
 125          }
 126      }
 127  
 128      public function make_html_inline($html) {
 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);
 133      }
 134  }
 135  
 136  
 137  /**
 138   * Represents a multiple choice question where only one choice should be selected.
 139   *
 140   * @copyright  2009 The Open University
 141   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 142   */
 143  class qtype_multichoice_single_question extends qtype_multichoice_base {
 144      public function get_renderer(moodle_page $page) {
 145          return $page->get_renderer('qtype_multichoice', 'single');
 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']];
 172          return $this->html_to_text($this->answers[$ansid]->answer,
 173                  $this->answers[$ansid]->answerformat);
 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,
 184                  $this->html_to_text($ans->answer, $ans->answerformat), $ans->fraction));
 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 prepare_simulated_post_data($simulatedresponse) {
 198          $ansid = 0;
 199          foreach ($this->answers as $answer) {
 200              if (clean_param($answer->answer, PARAM_NOTAGS) == $simulatedresponse['answer']) {
 201                  $ansid = $answer->id;
 202              }
 203          }
 204          if ($ansid) {
 205              return array('answer' => array_search($ansid, $this->order));
 206          } else {
 207              return array();
 208          }
 209      }
 210  
 211      public function get_student_response_values_for_simulation($postdata) {
 212          if (!isset($postdata['answer'])) {
 213              return array();
 214          } else {
 215              $answer = $this->answers[$this->order[$postdata['answer']]];
 216              return array('answer' => clean_param($answer->answer, PARAM_NOTAGS));
 217          }
 218      }
 219  
 220      public function is_same_response(array $prevresponse, array $newresponse) {
 221          return question_utils::arrays_same_at_key($prevresponse, $newresponse, 'answer');
 222      }
 223  
 224      public function is_complete_response(array $response) {
 225          return array_key_exists('answer', $response) && $response['answer'] !== '';
 226      }
 227  
 228      public function is_gradable_response(array $response) {
 229          return $this->is_complete_response($response);
 230      }
 231  
 232      public function grade_response(array $response) {
 233          if (array_key_exists('answer', $response) &&
 234                  array_key_exists($response['answer'], $this->order)) {
 235              $fraction = $this->answers[$this->order[$response['answer']]]->fraction;
 236          } else {
 237              $fraction = 0;
 238          }
 239          return array($fraction, question_state::graded_state_for_fraction($fraction));
 240      }
 241  
 242      public function get_validation_error(array $response) {
 243          if ($this->is_gradable_response($response)) {
 244              return '';
 245          }
 246          return get_string('pleaseselectananswer', 'qtype_multichoice');
 247      }
 248  
 249      public function get_response(question_attempt $qa) {
 250          return $qa->get_last_qt_var('answer', -1);
 251      }
 252  
 253      public function is_choice_selected($response, $value) {
 254          return (string) $response === (string) $value;
 255      }
 256  }
 257  
 258  
 259  /**
 260   * Represents a multiple choice question where multiple choices can be selected.
 261   *
 262   * @copyright  2009 The Open University
 263   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 264   */
 265  class qtype_multichoice_multi_question extends qtype_multichoice_base {
 266      public function get_renderer(moodle_page $page) {
 267          return $page->get_renderer('qtype_multichoice', 'multi');
 268      }
 269  
 270      public function get_min_fraction() {
 271          return 0;
 272      }
 273  
 274      public function clear_wrong_from_response(array $response) {
 275          foreach ($this->order as $key => $ans) {
 276              if (array_key_exists($this->field($key), $response) &&
 277                      question_state::graded_state_for_fraction(
 278                      $this->answers[$ans]->fraction)->is_incorrect()) {
 279                  $response[$this->field($key)] = 0;
 280              }
 281          }
 282          return $response;
 283      }
 284  
 285      public function get_num_parts_right(array $response) {
 286          $numright = 0;
 287          foreach ($this->order as $key => $ans) {
 288              $fieldname = $this->field($key);
 289              if (!array_key_exists($fieldname, $response) || !$response[$fieldname]) {
 290                  continue;
 291              }
 292  
 293              if (!question_state::graded_state_for_fraction(
 294                      $this->answers[$ans]->fraction)->is_incorrect()) {
 295                  $numright += 1;
 296              }
 297          }
 298          return array($numright, count($this->order));
 299      }
 300  
 301      /**
 302       * @param int $key choice number
 303       * @return string the question-type variable name.
 304       */
 305      protected function field($key) {
 306          return 'choice' . $key;
 307      }
 308  
 309      public function get_expected_data() {
 310          $expected = array();
 311          foreach ($this->order as $key => $notused) {
 312              $expected[$this->field($key)] = PARAM_BOOL;
 313          }
 314          return $expected;
 315      }
 316  
 317      public function summarise_response(array $response) {
 318          $selectedchoices = array();
 319          foreach ($this->order as $key => $ans) {
 320              $fieldname = $this->field($key);
 321              if (array_key_exists($fieldname, $response) && $response[$fieldname]) {
 322                  $selectedchoices[] = $this->html_to_text($this->answers[$ans]->answer,
 323                          $this->answers[$ans]->answerformat);
 324              }
 325          }
 326          if (empty($selectedchoices)) {
 327              return null;
 328          }
 329          return implode('; ', $selectedchoices);
 330      }
 331  
 332      public function classify_response(array $response) {
 333          $selectedchoices = array();
 334          foreach ($this->order as $key => $ansid) {
 335              $fieldname = $this->field($key);
 336              if (array_key_exists($fieldname, $response) && $response[$fieldname]) {
 337                  $selectedchoices[$ansid] = 1;
 338              }
 339          }
 340          $choices = array();
 341          foreach ($this->answers as $ansid => $ans) {
 342              if (isset($selectedchoices[$ansid])) {
 343                  $choices[$ansid] = new question_classified_response($ansid,
 344                          $this->html_to_text($ans->answer, $ans->answerformat), $ans->fraction);
 345              }
 346          }
 347          return $choices;
 348      }
 349  
 350      public function get_correct_response() {
 351          $response = array();
 352          foreach ($this->order as $key => $ans) {
 353              if (!question_state::graded_state_for_fraction(
 354                      $this->answers[$ans]->fraction)->is_incorrect()) {
 355                  $response[$this->field($key)] = 1;
 356              }
 357          }
 358          return $response;
 359      }
 360  
 361      public function prepare_simulated_post_data($simulatedresponse) {
 362          $postdata = array();
 363          foreach ($simulatedresponse as $ans => $checked) {
 364              foreach ($this->answers as $ansid => $answer) {
 365                  if (clean_param($answer->answer, PARAM_NOTAGS) == $ans) {
 366                      $fieldno = array_search($ansid, $this->order);
 367                      $postdata[$this->field($fieldno)] = $checked;
 368                      break;
 369                  }
 370              }
 371          }
 372          return $postdata;
 373      }
 374  
 375      public function get_student_response_values_for_simulation($postdata) {
 376          $simulatedresponse = array();
 377          foreach ($this->order as $fieldno => $ansid) {
 378              if (isset($postdata[$this->field($fieldno)])) {
 379                  $checked = $postdata[$this->field($fieldno)];
 380                  $simulatedresponse[clean_param($this->answers[$ansid]->answer, PARAM_NOTAGS)] = $checked;
 381              }
 382          }
 383          ksort($simulatedresponse);
 384          return $simulatedresponse;
 385      }
 386  
 387      public function is_same_response(array $prevresponse, array $newresponse) {
 388          foreach ($this->order as $key => $notused) {
 389              $fieldname = $this->field($key);
 390              if (!question_utils::arrays_same_at_key_integer($prevresponse, $newresponse, $fieldname)) {
 391                  return false;
 392              }
 393          }
 394          return true;
 395      }
 396  
 397      public function is_complete_response(array $response) {
 398          foreach ($this->order as $key => $notused) {
 399              if (!empty($response[$this->field($key)])) {
 400                  return true;
 401              }
 402          }
 403          return false;
 404      }
 405  
 406      public function is_gradable_response(array $response) {
 407          return $this->is_complete_response($response);
 408      }
 409  
 410      /**
 411       * @param array $response responses, as returned by
 412       *      {@link question_attempt_step::get_qt_data()}.
 413       * @return int the number of choices that were selected. in this response.
 414       */
 415      public function get_num_selected_choices(array $response) {
 416          $numselected = 0;
 417          foreach ($response as $key => $value) {
 418              // Response keys starting with _ are internal values like _order, so ignore them.
 419              if (!empty($value) && $key[0] != '_') {
 420                  $numselected += 1;
 421              }
 422          }
 423          return $numselected;
 424      }
 425  
 426      /**
 427       * @return int the number of choices that are correct.
 428       */
 429      public function get_num_correct_choices() {
 430          $numcorrect = 0;
 431          foreach ($this->answers as $ans) {
 432              if (!question_state::graded_state_for_fraction($ans->fraction)->is_incorrect()) {
 433                  $numcorrect += 1;
 434              }
 435          }
 436          return $numcorrect;
 437      }
 438  
 439      public function grade_response(array $response) {
 440          $fraction = 0;
 441          foreach ($this->order as $key => $ansid) {
 442              if (!empty($response[$this->field($key)])) {
 443                  $fraction += $this->answers[$ansid]->fraction;
 444              }
 445          }
 446          $fraction = min(max(0, $fraction), 1.0);
 447          return array($fraction, question_state::graded_state_for_fraction($fraction));
 448      }
 449  
 450      public function get_validation_error(array $response) {
 451          if ($this->is_gradable_response($response)) {
 452              return '';
 453          }
 454          return get_string('pleaseselectatleastoneanswer', 'qtype_multichoice');
 455      }
 456  
 457      /**
 458       * Disable those hint settings that we don't want when the student has selected
 459       * more choices than the number of right choices. This avoids giving the game away.
 460       * @param question_hint_with_parts $hint a hint.
 461       */
 462      protected function disable_hint_settings_when_too_many_selected(
 463              question_hint_with_parts $hint) {
 464          $hint->clearwrong = false;
 465      }
 466  
 467      public function get_hint($hintnumber, question_attempt $qa) {
 468          $hint = parent::get_hint($hintnumber, $qa);
 469          if (is_null($hint)) {
 470              return $hint;
 471          }
 472  
 473          if ($this->get_num_selected_choices($qa->get_last_qt_data()) >
 474                  $this->get_num_correct_choices()) {
 475              $hint = clone($hint);
 476              $this->disable_hint_settings_when_too_many_selected($hint);
 477          }
 478          return $hint;
 479      }
 480  
 481      public function get_response(question_attempt $qa) {
 482          return $qa->get_last_qt_data();
 483      }
 484  
 485      public function is_choice_selected($response, $value) {
 486          return !empty($response['choice' . $value]);
 487      }
 488  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1