Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   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_defaults_for_new_questions(stdClass $fromform): void {
  58          parent::save_defaults_for_new_questions($fromform);
  59          $this->set_default_value('usecase', $fromform->usecase);
  60      }
  61  
  62      public function save_question_options($question) {
  63          global $DB;
  64          $result = new stdClass();
  65  
  66          // Perform sanity checks on fractional grades.
  67          $maxfraction = -1;
  68          foreach ($question->answer as $key => $answerdata) {
  69              if ($question->fraction[$key] > $maxfraction) {
  70                  $maxfraction = $question->fraction[$key];
  71              }
  72          }
  73  
  74          if ($maxfraction != 1) {
  75              $result->error = get_string('fractionsnomax', 'question', $maxfraction * 100);
  76              return $result;
  77          }
  78  
  79          parent::save_question_options($question);
  80  
  81          $this->save_question_answers($question);
  82  
  83          $this->save_hints($question);
  84      }
  85  
  86      protected function fill_answer_fields($answer, $questiondata, $key, $context) {
  87          $answer = parent::fill_answer_fields($answer, $questiondata, $key, $context);
  88          $answer->answer = trim($answer->answer);
  89          return $answer;
  90      }
  91  
  92      protected function initialise_question_instance(question_definition $question, $questiondata) {
  93          parent::initialise_question_instance($question, $questiondata);
  94          $this->initialise_question_answers($question, $questiondata);
  95      }
  96  
  97      public function get_random_guess_score($questiondata) {
  98          foreach ($questiondata->options->answers as $aid => $answer) {
  99              if ('*' == trim($answer->answer)) {
 100                  return $answer->fraction;
 101              }
 102          }
 103          return 0;
 104      }
 105  
 106      public function get_possible_responses($questiondata) {
 107          $responses = array();
 108  
 109          $starfound = false;
 110          foreach ($questiondata->options->answers as $aid => $answer) {
 111              $responses[$aid] = new question_possible_response($answer->answer,
 112                      $answer->fraction);
 113              if ($answer->answer === '*') {
 114                  $starfound = true;
 115              }
 116          }
 117  
 118          if (!$starfound) {
 119              $responses[0] = new question_possible_response(
 120                      get_string('didnotmatchanyanswer', 'question'), 0);
 121          }
 122  
 123          $responses[null] = question_possible_response::no_response();
 124  
 125          return array($questiondata->id => $responses);
 126      }
 127  }