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 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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   * Contains the helper class for the select missing words question type tests.
  19   *
  20   * @package   qtype_gapselect
  21   * @copyright 2011 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 select missing words 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_gapselect_test_helper extends question_test_helper {
  36  
  37      public function get_test_questions() {
  38          return array('fox', 'maths', 'currency', 'multilang', 'missingchoiceno');
  39      }
  40  
  41      /**
  42       * Get data you would get by loading a typical select missing words question.
  43       *
  44       * @return stdClass as returned by question_bank::load_question_data for this qtype.
  45       */
  46      public static function get_gapselect_question_data_fox() {
  47          global $USER;
  48  
  49          $gapselect = new stdClass();
  50          $gapselect->id = 0;
  51          $gapselect->category = 0;
  52          $gapselect->contextid = 0;
  53          $gapselect->parent = 0;
  54          $gapselect->questiontextformat = FORMAT_HTML;
  55          $gapselect->generalfeedbackformat = FORMAT_HTML;
  56          $gapselect->defaultmark = 1;
  57          $gapselect->penalty = 0.3333333;
  58          $gapselect->length = 1;
  59          $gapselect->stamp = make_unique_id_code();
  60          $gapselect->version = make_unique_id_code();
  61          $gapselect->hidden = 0;
  62          $gapselect->idnumber = null;
  63          $gapselect->timecreated = time();
  64          $gapselect->timemodified = time();
  65          $gapselect->createdby = $USER->id;
  66          $gapselect->modifiedby = $USER->id;
  67  
  68          $gapselect->name = 'Selection from drop down list question';
  69          $gapselect->questiontext = 'The [[1]] brown [[2]] jumped over the [[3]] dog.';
  70          $gapselect->generalfeedback = 'This sentence uses each letter of the alphabet.';
  71          $gapselect->qtype = 'gapselect';
  72  
  73          $gapselect->options = new stdClass();
  74          $gapselect->options->shuffleanswers = true;
  75  
  76          test_question_maker::set_standard_combined_feedback_fields($gapselect->options);
  77  
  78          $gapselect->options->answers = array(
  79              (object) array('answer' => 'quick', 'feedback' => '1'),
  80              (object) array('answer' => 'fox', 'feedback' => '2'),
  81              (object) array('answer' => 'lazy', 'feedback' => '3'),
  82              (object) array('answer' => 'assiduous', 'feedback' => '3'),
  83              (object) array('answer' => 'dog', 'feedback' => '2'),
  84              (object) array('answer' => 'slow', 'feedback' => '1'),
  85          );
  86  
  87          return $gapselect;
  88      }
  89  
  90      /**
  91       * Get data required to save a select missing words question where
  92       * the author missed out one of the group numbers.
  93       *
  94       * @return stdClass data to create a gapselect question.
  95       */
  96      public function get_gapselect_question_form_data_missingchoiceno() {
  97          $fromform = new stdClass();
  98  
  99          $fromform->name = 'Select missing words question';
 100          $fromform->questiontext = ['text' => 'The [[1]] sat on the [[3]].', 'format' => FORMAT_HTML];
 101          $fromform->defaultmark = 1.0;
 102          $fromform->generalfeedback = ['text' => 'The right answer is: "The cat sat on the mat."', 'format' => FORMAT_HTML];
 103          $fromform->choices = [
 104                  ['answer' => 'cat', 'choicegroup' => '1'],
 105                  ['answer' => '',    'choicegroup' => '1'],
 106                  ['answer' => 'mat', 'choicegroup' => '1'],
 107          ];
 108          test_question_maker::set_standard_combined_feedback_form_data($fromform);
 109          $fromform->shownumcorrect = 0;
 110          $fromform->penalty = 0.3333333;
 111  
 112          return $fromform;
 113      }
 114  
 115      /**
 116       * Get an example gapselect question to use for testing. This examples has one of each item.
 117       * @return qtype_gapselect_question
 118       */
 119      public static function make_gapselect_question_fox() {
 120          question_bank::load_question_definition_classes('gapselect');
 121          $gapselect = new qtype_gapselect_question();
 122  
 123          test_question_maker::initialise_a_question($gapselect);
 124  
 125          $gapselect->name = 'Selection from drop down list question';
 126          $gapselect->questiontext = 'The [[1]] brown [[2]] jumped over the [[3]] dog.';
 127          $gapselect->generalfeedback = 'This sentence uses each letter of the alphabet.';
 128          $gapselect->qtype = question_bank::get_qtype('gapselect');
 129  
 130          $gapselect->shufflechoices = true;
 131  
 132          test_question_maker::set_standard_combined_feedback_fields($gapselect);
 133  
 134          $gapselect->choices = array(
 135              1 => array(
 136                  1 => new qtype_gapselect_choice('quick', 1),
 137                  2 => new qtype_gapselect_choice('slow', 1)),
 138              2 => array(
 139                  1 => new qtype_gapselect_choice('fox', 2),
 140                  2 => new qtype_gapselect_choice('dog', 2)),
 141              3 => array(
 142                  1 => new qtype_gapselect_choice('lazy', 3),
 143                  2 => new qtype_gapselect_choice('assiduous', 3)),
 144          );
 145  
 146          $gapselect->places = array(1 => 1, 2 => 2, 3 => 3);
 147          $gapselect->rightchoices = array(1 => 1, 2 => 1, 3 => 1);
 148          $gapselect->textfragments = array('The ', ' brown ', ' jumped over the ', ' dog.');
 149  
 150          return $gapselect;
 151      }
 152  
 153      /**
 154       * Get an example gapselect question to use for testing. This exmples had unlimited items.
 155       * @return qtype_gapselect_question
 156       */
 157      public static function make_gapselect_question_maths() {
 158          question_bank::load_question_definition_classes('gapselect');
 159          $gapselect = new qtype_gapselect_question();
 160  
 161          test_question_maker::initialise_a_question($gapselect);
 162  
 163          $gapselect->name = 'Selection from drop down list question';
 164          $gapselect->questiontext = 'Fill in the operators to make this equation work: ' .
 165                  '7 [[1]] 11 [[2]] 13 [[1]] 17 [[2]] 19 = 3';
 166          $gapselect->generalfeedback = 'This sentence uses each letter of the alphabet.';
 167          $gapselect->qtype = question_bank::get_qtype('gapselect');
 168  
 169          $gapselect->shufflechoices = true;
 170  
 171          test_question_maker::set_standard_combined_feedback_fields($gapselect);
 172  
 173          $gapselect->choices = array(
 174              1 => array(
 175                  1 => new qtype_gapselect_choice('+', 1),
 176                  2 => new qtype_gapselect_choice('-', 1),
 177                  3 => new qtype_gapselect_choice('*', 1),
 178                  4 => new qtype_gapselect_choice('/', 1),
 179              ));
 180  
 181          $gapselect->places = array(1 => 1, 2 => 1, 3 => 1, 4 => 1);
 182          $gapselect->rightchoices = array(1 => 1, 2 => 2, 3 => 1, 4 => 2);
 183          $gapselect->textfragments = array('7 ', ' 11 ', ' 13 ', ' 17 ', ' 19 = 3');
 184  
 185          return $gapselect;
 186      }
 187  
 188      /**
 189       * Get an example gapselect question with multilang entries to use for testing.
 190       * @return qtype_gapselect_question
 191       */
 192      public static function make_gapselect_question_multilang() {
 193          question_bank::load_question_definition_classes('gapselect');
 194          $gapselect = new qtype_gapselect_question();
 195  
 196          test_question_maker::initialise_a_question($gapselect);
 197  
 198          $gapselect->name = 'Multilang select missing words question';
 199          $gapselect->questiontext = '<span lang="en" class="multilang">The </span><span lang="ru" class="multilang"></span>[[1]] ' .
 200              '<span lang="en" class="multilang">sat on the</span><span lang="ru" class="multilang">сидела на</span> [[2]].';
 201          $gapselect->generalfeedback = 'This sentence uses each letter of the alphabet.';
 202          $gapselect->qtype = question_bank::get_qtype('gapselect');
 203  
 204          $gapselect->shufflechoices = true;
 205  
 206          test_question_maker::set_standard_combined_feedback_fields($gapselect);
 207  
 208          $gapselect->choices = array(
 209                  1 => array(
 210                      1 => new qtype_gapselect_choice('<span lang="en" class="multilang">cat</span><span lang="ru" ' .
 211                          'class="multilang">кошка</span>', 1),
 212                      2 => new qtype_gapselect_choice('<span lang="en" class="multilang">dog</span><span lang="ru" ' .
 213                          'class="multilang">пес</span>', 1)),
 214                  2 => array(
 215                      1 => new qtype_gapselect_choice('<span lang="en" class="multilang">mat</span><span lang="ru" ' .
 216                          'class="multilang">коврике</span>', 2),
 217                      2 => new qtype_gapselect_choice('<span lang="en" class="multilang">bat</span><span lang="ru" ' .
 218                          'class="multilang">бита</span>', 2))
 219                  );
 220  
 221          $gapselect->places = array(1 => 1, 2 => 2);
 222          $gapselect->rightchoices = array(1 => 1, 2 => 1);
 223          $gapselect->textfragments = array('<span lang="en" class="multilang">The </span><span lang="ru" class="multilang"></span>',
 224              ' <span lang="en" class="multilang">sat on the</span><span lang="ru" class="multilang">сидела на</span> ', '.');
 225  
 226          return $gapselect;
 227      }
 228  
 229      /**
 230       * This examples includes choices with currency like options.
 231       * @return qtype_gapselect_question
 232       */
 233      public static function make_gapselect_question_currency() {
 234          question_bank::load_question_definition_classes('gapselect');
 235          $gapselect = new qtype_gapselect_question();
 236  
 237          test_question_maker::initialise_a_question($gapselect);
 238  
 239          $gapselect->name = 'Selection from currency like choices';
 240          $gapselect->questiontext = 'The price of the ball is [[1]] approx.';
 241          $gapselect->generalfeedback = 'The choice is yours';
 242          $gapselect->qtype = question_bank::get_qtype('gapselect');
 243  
 244          $gapselect->shufflechoices = true;
 245  
 246          test_question_maker::set_standard_combined_feedback_fields($gapselect);
 247  
 248          $gapselect->choices = [
 249                  1 => [
 250                          1 => new qtype_gapselect_choice('$2', 1),
 251                          2 => new qtype_gapselect_choice('$3', 1),
 252                          3 => new qtype_gapselect_choice('$4.99', 1),
 253                          4 => new qtype_gapselect_choice('-1', 1)
 254                  ]
 255          ];
 256  
 257          $gapselect->places = array(1 => 1);
 258          $gapselect->rightchoices = array(1 => 1);
 259          $gapselect->textfragments = array('The price of the ball is ', ' approx.');
 260  
 261          return $gapselect;
 262      }
 263  
 264      /**
 265       * Just for backwards compatibility.
 266       *
 267       * @return qtype_gapselect_question
 268       */
 269      public static function make_a_gapselect_question() {
 270          debugging('qtype_gapselect_test_helper::make_a_gapselect_question is deprecated. ' .
 271                  "Please use test_question_maker::make_question('gapselect') instead.");
 272          return self::make_gapselect_question_fox();
 273      }
 274  
 275      /**
 276       * Just for backwards compatibility.
 277       *
 278       * @return qtype_gapselect_question
 279       */
 280      public static function make_a_maths_gapselect_question() {
 281          debugging('qtype_gapselect_test_helper::make_a_maths_gapselect_question is deprecated. ' .
 282                  "Please use test_question_maker::make_question('gapselect', 'maths') instead.");
 283          return self::make_gapselect_question_maths();
 284      }
 285  
 286      /**
 287       * Just for backwards compatibility.
 288       *
 289       * @return qtype_gapselect_question
 290       */
 291      public static function make_a_currency_gapselect_question() {
 292          debugging('qtype_gapselect_test_helper::make_a_currency_gapselect_question is deprecated. ' .
 293                  "Please use test_question_maker::make_question('gapselect', 'currency') instead.");
 294          return self::make_gapselect_question_currency();
 295      }
 296  
 297      /**
 298       * Just for backwards compatibility.
 299       *
 300       * @return qtype_gapselect_question
 301       */
 302      public static function make_a_multilang_gapselect_question() {
 303          debugging('qtype_gapselect_test_helper::make_a_multilang_gapselect_question is deprecated. ' .
 304                  "Please use test_question_maker::make_question('gapselect', 'multilang') instead.");
 305          return self::make_gapselect_question_multilang();
 306      }
 307  }