Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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   * Question type class for the short answer question type.
  19   *
  20   * @package    qtype
  21   * @subpackage shortanswer
  22   * @copyright  1999 onwards Martin Dougiamas  {@link http://moodle.com}
  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->libdir . '/questionlib.php');
  30  require_once($CFG->dirroot . '/question/engine/lib.php');
  31  require_once($CFG->dirroot . '/question/type/shortanswer/question.php');
  32  
  33  
  34  /**
  35   * The short answer question type.
  36   *
  37   * @copyright  1999 onwards Martin Dougiamas  {@link http://moodle.com}
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class qtype_shortanswer extends question_type {
  41      public function extra_question_fields() {
  42          return array('qtype_shortanswer_options', 'usecase');
  43      }
  44  
  45      public function move_files($questionid, $oldcontextid, $newcontextid) {
  46          parent::move_files($questionid, $oldcontextid, $newcontextid);
  47          $this->move_files_in_answers($questionid, $oldcontextid, $newcontextid);
  48          $this->move_files_in_hints($questionid, $oldcontextid, $newcontextid);
  49      }
  50  
  51      protected function delete_files($questionid, $contextid) {
  52          parent::delete_files($questionid, $contextid);
  53          $this->delete_files_in_answers($questionid, $contextid);
  54          $this->delete_files_in_hints($questionid, $contextid);
  55      }
  56  
  57      public function save_question_options($question) {
  58          global $DB;
  59          $result = new stdClass();
  60  
  61          // Perform sanity checks on fractional grades.
  62          $maxfraction = -1;
  63          foreach ($question->answer as $key => $answerdata) {
  64              if ($question->fraction[$key] > $maxfraction) {
  65                  $maxfraction = $question->fraction[$key];
  66              }
  67          }
  68  
  69          if ($maxfraction != 1) {
  70              $result->error = get_string('fractionsnomax', 'question', $maxfraction * 100);
  71              return $result;
  72          }
  73  
  74          parent::save_question_options($question);
  75  
  76          $this->save_question_answers($question);
  77  
  78          $this->save_hints($question);
  79      }
  80  
  81      protected function fill_answer_fields($answer, $questiondata, $key, $context) {
  82          $answer = parent::fill_answer_fields($answer, $questiondata, $key, $context);
  83          $answer->answer = trim($answer->answer);
  84          return $answer;
  85      }
  86  
  87      protected function initialise_question_instance(question_definition $question, $questiondata) {
  88          parent::initialise_question_instance($question, $questiondata);
  89          $this->initialise_question_answers($question, $questiondata);
  90      }
  91  
  92      public function get_random_guess_score($questiondata) {
  93          foreach ($questiondata->options->answers as $aid => $answer) {
  94              if ('*' == trim($answer->answer)) {
  95                  return $answer->fraction;
  96              }
  97          }
  98          return 0;
  99      }
 100  
 101      public function get_possible_responses($questiondata) {
 102          $responses = array();
 103  
 104          $starfound = false;
 105          foreach ($questiondata->options->answers as $aid => $answer) {
 106              $responses[$aid] = new question_possible_response($answer->answer,
 107                      $answer->fraction);
 108              if ($answer->answer === '*') {
 109                  $starfound = true;
 110              }
 111          }
 112  
 113          if (!$starfound) {
 114              $responses[0] = new question_possible_response(
 115                      get_string('didnotmatchanyanswer', 'question'), 0);
 116          }
 117  
 118          $responses[null] = question_possible_response::no_response();
 119  
 120          return array($questiondata->id => $responses);
 121      }
 122  }