Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 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   * 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  
  79          $qdata->options = new stdClass();
  80          $qdata->options->usecase = 0;
  81          $qdata->options->answers = array(
  82              13 => new question_answer(13, 'frog', 1.0, 'Frog is a very good answer.', FORMAT_HTML),
  83              14 => new question_answer(14, 'toad', 0.8, 'Toad is an OK good answer.', FORMAT_HTML),
  84              15 => new question_answer(15, '*', 0.0, 'That is a bad answer.', FORMAT_HTML),
  85          );
  86  
  87          return $qdata;
  88      }
  89  
  90      /**
  91       * Gets the question form data for a shortanswer question with with correct
  92       * answer 'frog', partially correct answer 'toad' and defaultmark 1.
  93       * This question also has a '*' match anything answer.
  94       * @return stdClass
  95       */
  96      public function get_shortanswer_question_form_data_frogtoad() {
  97          $form = new stdClass();
  98  
  99          $form->name = 'Short answer question';
 100          $form->questiontext = array('text' => 'Name an amphibian: __________', 'format' => FORMAT_HTML);
 101          $form->defaultmark = 1.0;
 102          $form->generalfeedback = array('text' => 'Generalfeedback: frog or toad would have been OK.', 'format' => FORMAT_HTML);
 103          $form->usecase = false;
 104          $form->answer = array('frog', 'toad', '*');
 105          $form->fraction = array('1.0', '0.8', '0.0');
 106          $form->feedback = array(
 107              array('text' => 'Frog is a very good answer.', 'format' => FORMAT_HTML),
 108              array('text' => 'Toad is an OK good answer.', 'format' => FORMAT_HTML),
 109              array('text' => 'That is a bad answer.', 'format' => FORMAT_HTML),
 110          );
 111  
 112          return $form;
 113      }
 114  
 115      /**
 116       * Makes a shortanswer question with just the correct ansewer 'frog', and
 117       * no other answer matching.
 118       * @return qtype_shortanswer_question
 119       */
 120      public function make_shortanswer_question_frogonly() {
 121          question_bank::load_question_definition_classes('shortanswer');
 122          $sa = new qtype_shortanswer_question();
 123          test_question_maker::initialise_a_question($sa);
 124          $sa->name = 'Short answer question';
 125          $sa->questiontext = 'Name the best amphibian: __________';
 126          $sa->generalfeedback = 'Generalfeedback: you should have said frog.';
 127          $sa->usecase = false;
 128          $sa->answers = array(
 129              13 => new question_answer(13, 'frog', 1.0, 'Frog is right.', FORMAT_HTML),
 130          );
 131          $sa->qtype = question_bank::get_qtype('shortanswer');
 132  
 133          return $sa;
 134      }
 135  
 136      /**
 137       * Gets the question data for a shortanswer questionwith just the correct
 138       * ansewer 'frog', and no other answer matching.
 139       * @return stdClass
 140       */
 141      public function get_shortanswer_question_data_frogonly() {
 142          $qdata = new stdClass();
 143          test_question_maker::initialise_question_data($qdata);
 144  
 145          $qdata->qtype = 'shortanswer';
 146          $qdata->name = 'Short answer question';
 147          $qdata->questiontext = 'Name the best amphibian: __________';
 148          $qdata->generalfeedback = 'Generalfeedback: you should have said frog.';
 149  
 150          $qdata->options = new stdClass();
 151          $qdata->options->usecase = false;
 152          $qdata->options->answers = array(
 153              13 => new question_answer(13, 'frog', 1.0, 'Frog is right.', FORMAT_HTML),
 154          );
 155  
 156          return $qdata;
 157      }
 158  
 159      /**
 160       * Makes a shortanswer question with just the correct ansewer 'frog', and
 161       * no other answer matching.
 162       * @return qtype_shortanswer_question
 163       */
 164      public function make_shortanswer_question_escapedwildcards() {
 165          question_bank::load_question_definition_classes('shortanswer');
 166          $sa = new qtype_shortanswer_question();
 167          test_question_maker::initialise_a_question($sa);
 168          $sa->name = 'Question with escaped * in the answer.';
 169          $sa->questiontext = 'How to you write x times y in C? __________';
 170          $sa->generalfeedback = 'In C, this expression is written x * y.';
 171          $sa->usecase = false;
 172          $sa->answers = array(
 173              13 => new question_answer(13, '*x\*y*', 1.0, 'Well done.', FORMAT_HTML),
 174          );
 175          $sa->qtype = question_bank::get_qtype('shortanswer');
 176  
 177          return $sa;
 178      }
 179  }