Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   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   * Test helpers for the shortanswer question type.
  19   *
  20   * @package    qtype_shortanswer
  21   * @copyright  2012 The Open University
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  
  29  /**
  30   * Test helper class for the shortanswer question type.
  31   *
  32   * @copyright  2011 The Open University
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class qtype_shortanswer_test_helper extends question_test_helper {
  36      public function get_test_questions() {
  37          return array('frogtoad', 'frogonly', 'escapedwildcards');
  38      }
  39  
  40      /**
  41       * Makes a shortanswer question with correct ansewer 'frog', partially
  42       * correct answer 'toad' and defaultmark 1. This question also has a
  43       * '*' match anything answer.
  44       * @return qtype_shortanswer_question
  45       */
  46      public function make_shortanswer_question_frogtoad() {
  47          question_bank::load_question_definition_classes('shortanswer');
  48          $sa = new qtype_shortanswer_question();
  49          test_question_maker::initialise_a_question($sa);
  50          $sa->name = 'Short answer question';
  51          $sa->questiontext = 'Name an amphibian: __________';
  52          $sa->generalfeedback = 'Generalfeedback: frog or toad would have been OK.';
  53          $sa->usecase = false;
  54          $sa->answers = array(
  55              13 => new question_answer(13, 'frog', 1.0, 'Frog is a very good answer.', FORMAT_HTML),
  56              14 => new question_answer(14, 'toad', 0.8, 'Toad is an OK good answer.', FORMAT_HTML),
  57              15 => new question_answer(15, '*', 0.0, 'That is a bad answer.', FORMAT_HTML),
  58          );
  59          $sa->qtype = question_bank::get_qtype('shortanswer');
  60  
  61          return $sa;
  62      }
  63  
  64      /**
  65       * Gets the question data for a shortanswer question with with correct
  66       * ansewer 'frog', partially correct answer 'toad' and defaultmark 1.
  67       * This question also has a '*' match anything answer.
  68       * @return stdClass
  69       */
  70      public function get_shortanswer_question_data_frogtoad() {
  71          $qdata = new stdClass();
  72          test_question_maker::initialise_question_data($qdata);
  73  
  74          $qdata->qtype = 'shortanswer';
  75          $qdata->name = 'Short answer question';
  76          $qdata->questiontext = 'Name an amphibian: __________';
  77          $qdata->generalfeedback = 'Generalfeedback: frog or toad would have been OK.';
  78          $qdata->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
  79  
  80          $qdata->options = new stdClass();
  81          $qdata->options->usecase = 0;
  82          $qdata->options->answers = array(
  83              13 => new question_answer(13, 'frog', 1.0, 'Frog is a very good answer.', FORMAT_HTML),
  84              14 => new question_answer(14, 'toad', 0.8, 'Toad is an OK good answer.', FORMAT_HTML),
  85              15 => new question_answer(15, '*', 0.0, 'That is a bad answer.', FORMAT_HTML),
  86          );
  87  
  88          return $qdata;
  89      }
  90  
  91      /**
  92       * Gets the question form data for a shortanswer question with with correct
  93       * answer 'frog', partially correct answer 'toad' and defaultmark 1.
  94       * This question also has a '*' match anything answer.
  95       * @return stdClass
  96       */
  97      public function get_shortanswer_question_form_data_frogtoad() {
  98          $form = new stdClass();
  99  
 100          $form->name = 'Short answer question';
 101          $form->questiontext = array('text' => 'Name an amphibian: __________', 'format' => FORMAT_HTML);
 102          $form->defaultmark = 1.0;
 103          $form->generalfeedback = array('text' => 'Generalfeedback: frog or toad would have been OK.', 'format' => FORMAT_HTML);
 104          $form->usecase = false;
 105          $form->answer = array('frog', 'toad', '*');
 106          $form->fraction = array('1.0', '0.8', '0.0');
 107          $form->feedback = array(
 108              array('text' => 'Frog is a very good answer.', 'format' => FORMAT_HTML),
 109              array('text' => 'Toad is an OK good answer.', 'format' => FORMAT_HTML),
 110              array('text' => 'That is a bad answer.', 'format' => FORMAT_HTML),
 111          );
 112          $form->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
 113  
 114          return $form;
 115      }
 116  
 117      /**
 118       * Makes a shortanswer question with just the correct ansewer 'frog', and
 119       * no other answer matching.
 120       * @return qtype_shortanswer_question
 121       */
 122      public function make_shortanswer_question_frogonly() {
 123          question_bank::load_question_definition_classes('shortanswer');
 124          $sa = new qtype_shortanswer_question();
 125          test_question_maker::initialise_a_question($sa);
 126          $sa->name = 'Short answer question';
 127          $sa->questiontext = 'Name the best amphibian: __________';
 128          $sa->generalfeedback = 'Generalfeedback: you should have said frog.';
 129          $sa->usecase = false;
 130          $sa->answers = array(
 131              13 => new question_answer(13, 'frog', 1.0, 'Frog is right.', FORMAT_HTML),
 132          );
 133          $sa->qtype = question_bank::get_qtype('shortanswer');
 134  
 135          return $sa;
 136      }
 137  
 138      /**
 139       * Gets the question data for a shortanswer questionwith just the correct
 140       * ansewer 'frog', and no other answer matching.
 141       * @return stdClass
 142       */
 143      public function get_shortanswer_question_data_frogonly() {
 144          $qdata = new stdClass();
 145          test_question_maker::initialise_question_data($qdata);
 146  
 147          $qdata->qtype = 'shortanswer';
 148          $qdata->name = 'Short answer question';
 149          $qdata->questiontext = 'Name the best amphibian: __________';
 150          $qdata->generalfeedback = 'Generalfeedback: you should have said frog.';
 151          $qdata->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
 152  
 153          $qdata->options = new stdClass();
 154          $qdata->options->usecase = false;
 155          $qdata->options->answers = array(
 156              13 => new question_answer(13, 'frog', 1.0, 'Frog is right.', FORMAT_HTML),
 157          );
 158  
 159          return $qdata;
 160      }
 161  
 162      /**
 163       * Makes a shortanswer question with just the correct ansewer 'frog', and
 164       * no other answer matching.
 165       * @return qtype_shortanswer_question
 166       */
 167      public function make_shortanswer_question_escapedwildcards() {
 168          question_bank::load_question_definition_classes('shortanswer');
 169          $sa = new qtype_shortanswer_question();
 170          test_question_maker::initialise_a_question($sa);
 171          $sa->name = 'Question with escaped * in the answer.';
 172          $sa->questiontext = 'How to you write x times y in C? __________';
 173          $sa->generalfeedback = 'In C, this expression is written x * y.';
 174          $sa->usecase = false;
 175          $sa->answers = array(
 176              13 => new question_answer(13, '*x\*y*', 1.0, 'Well done.', FORMAT_HTML),
 177          );
 178          $sa->qtype = question_bank::get_qtype('shortanswer');
 179  
 180          return $sa;
 181      }
 182  }