Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

   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 match question type.
  19   *
  20   * @package    qtype_match
  21   * @copyright  2013 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  global $CFG;
  29  require_once($CFG->dirroot . '/question/type/match/question.php');
  30  
  31  
  32  /**
  33   * Test helper class for the match question type.
  34   *
  35   * @copyright  2013 The Open University
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class qtype_match_test_helper extends question_test_helper {
  39      public function get_test_questions() {
  40          return array('foursubq', 'trickynums');
  41      }
  42  
  43      /**
  44       * Makes a match question about completing two blanks in some text.
  45       * @return object the question definition data, as it might be returned from
  46       * get_question_options.
  47       */
  48      public function get_match_question_data_foursubq() {
  49          global $USER;
  50          $q = new stdClass();
  51          test_question_maker::initialise_question_data($q);
  52          $q->name = 'Matching question';
  53          $q->qtype = 'match';
  54          $q->parent = 0;
  55          $q->questiontext = 'Classify the animals.';
  56          $q->questiontextformat = FORMAT_HTML;
  57          $q->generalfeedback = 'General feedback.';
  58          $q->generalfeedbackformat = FORMAT_HTML;
  59          $q->defaultmark = 1;
  60          $q->penalty = 0.3333333;
  61          $q->length = 1;
  62          $q->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
  63          $q->versionid = 0;
  64          $q->version = 1;
  65          $q->questionbankentryid = 0;
  66          $q->createdby = $USER->id;
  67          $q->modifiedby = $USER->id;
  68  
  69          $q->options = new stdClass();
  70          $q->options->shuffleanswers = 1;
  71          test_question_maker::set_standard_combined_feedback_fields($q->options);
  72  
  73          $q->options->subquestions = array(
  74              14 => (object) array(
  75                  'id' => 14,
  76                  'questiontext' => 'frog',
  77                  'questiontextformat' => FORMAT_HTML,
  78                  'answertext' => 'amphibian'),
  79              15 => (object) array(
  80                  'id' => 15,
  81                  'questiontext' => 'cat',
  82                  'questiontextformat' => FORMAT_HTML,
  83                  'answertext' => 'mammal'),
  84              16 => (object) array(
  85                  'id' => 16,
  86                  'questiontext' => 'newt',
  87                  'questiontextformat' => FORMAT_HTML,
  88                  'answertext' => 'amphibian'),
  89              17 => (object) array(
  90                  'id' => 17,
  91                  'questiontext' => '',
  92                  'questiontextformat' => FORMAT_HTML,
  93                  'answertext' => 'insect'),
  94          );
  95  
  96          return $q;
  97      }
  98  
  99      /**
 100       * Makes a match question about completing two blanks in some text.
 101       * @return object the question definition data, as it might be returned from
 102       *      the question editing form.
 103       */
 104      public function get_match_question_form_data_foursubq() {
 105          $q = new stdClass();
 106          $q->name = 'Matching question';
 107          $q->questiontext = array('text' => 'Classify the animals.', 'format' => FORMAT_HTML);
 108          $q->generalfeedback = array('text' => 'General feedback.', 'format' => FORMAT_HTML);
 109          $q->defaultmark = 1;
 110          $q->penalty = 0.3333333;
 111          $q->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
 112          $q->versionid = 0;
 113          $q->version = 1;
 114          $q->questionbankentryid = 0;
 115  
 116          $q->shuffleanswers = 1;
 117          test_question_maker::set_standard_combined_feedback_form_data($q);
 118  
 119          $q->subquestions = array(
 120              0 => array('text' => 'frog', 'format' => FORMAT_HTML),
 121              1 => array('text' => 'cat', 'format' => FORMAT_HTML),
 122              2 => array('text' => 'newt', 'format' => FORMAT_HTML),
 123              3 => array('text' => '', 'format' => FORMAT_HTML));
 124  
 125          $q->subanswers = array(
 126              0 => 'amphibian',
 127              1 => 'mammal',
 128              2 => 'amphibian',
 129              3 => 'insect'
 130          );
 131  
 132          $q->noanswers = 4;
 133  
 134          return $q;
 135      }
 136  
 137      /**
 138       * Makes a matching question to classify 'Dog', 'Frog', 'Toad' and 'Cat' as
 139       * 'Mammal', 'Amphibian' or 'Insect'.
 140       * defaultmark 1. Stems are shuffled by default.
 141       * @return qtype_match_question
 142       */
 143      public static function make_match_question_foursubq() {
 144          question_bank::load_question_definition_classes('match');
 145          $match = new qtype_match_question();
 146          test_question_maker::initialise_a_question($match);
 147          $match->name = 'Matching question';
 148          $match->questiontext = 'Classify the animals.';
 149          $match->generalfeedback = 'Frogs and toads are amphibians, the others are mammals.';
 150          $match->qtype = question_bank::get_qtype('match');
 151  
 152          $match->shufflestems = 1;
 153  
 154          test_question_maker::set_standard_combined_feedback_fields($match);
 155  
 156          // Using unset to get 1-based arrays.
 157          $match->stems = array('', 'Dog', 'Frog', 'Toad', 'Cat');
 158          $match->stemformat = array('', FORMAT_HTML, FORMAT_HTML, FORMAT_HTML, FORMAT_HTML);
 159          $match->choices = array('', 'Mammal', 'Amphibian', 'Insect');
 160          $match->right = array('', 1, 2, 2, 1);
 161          unset($match->stems[0]);
 162          unset($match->stemformat[0]);
 163          unset($match->choices[0]);
 164          unset($match->right[0]);
 165  
 166          return $match;
 167      }
 168  
 169      /**
 170       * Makes a matching question with choices including '0' and '0.0'.
 171       *
 172       * @return object the question definition data, as it might be returned from
 173       * get_question_options.
 174       */
 175      public function get_match_question_data_trickynums() {
 176          global $USER;
 177  
 178          $q = new stdClass();
 179          test_question_maker::initialise_question_data($q);
 180          $q->name = 'Java matching';
 181          $q->qtype = 'match';
 182          $q->parent = 0;
 183          $q->questiontext = 'What is the output of each of these lines of code?';
 184          $q->questiontextformat = FORMAT_HTML;
 185          $q->generalfeedback = 'Java has some advantages over PHP I guess!';
 186          $q->generalfeedbackformat = FORMAT_HTML;
 187          $q->defaultmark = 1;
 188          $q->penalty = 0.3333333;
 189          $q->length = 1;
 190          $q->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
 191          $q->versionid = 0;
 192          $q->version = 1;
 193          $q->questionbankentryid = 0;
 194          $q->createdby = $USER->id;
 195          $q->modifiedby = $USER->id;
 196  
 197          $q->options = new stdClass();
 198          $q->options->shuffleanswers = 1;
 199          test_question_maker::set_standard_combined_feedback_fields($q->options);
 200  
 201          $q->options->subquestions = array(
 202                  14 => (object) array(
 203                          'id' => 14,
 204                          'questiontext' => 'System.out.println(0);',
 205                          'questiontextformat' => FORMAT_HTML,
 206                          'answertext' => '0'),
 207                  15 => (object) array(
 208                          'id' => 15,
 209                          'questiontext' => 'System.out.println(0.0);',
 210                          'questiontextformat' => FORMAT_HTML,
 211                          'answertext' => '0.0'),
 212                  16 => (object) array(
 213                          'id' => 16,
 214                          'questiontext' => '',
 215                          'questiontextformat' => FORMAT_HTML,
 216                          'answertext' => 'NULL'),
 217          );
 218  
 219          return $q;
 220      }
 221  
 222      /**
 223       * Makes a match question about completing two blanks in some text.
 224       * @return object the question definition data, as it might be returned from
 225       *      the question editing form.
 226       */
 227      public function get_match_question_form_data_trickynums() {
 228          $q = new stdClass();
 229          $q->name = 'Java matching';
 230          $q->questiontext = ['text' => 'What is the output of each of these lines of code?', 'format' => FORMAT_HTML];
 231          $q->generalfeedback = ['text' => 'Java has some advantages over PHP I guess!', 'format' => FORMAT_HTML];
 232          $q->defaultmark = 1;
 233          $q->penalty = 0.3333333;
 234          $q->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
 235          $q->versionid = 0;
 236          $q->version = 1;
 237          $q->questionbankentryid = 0;
 238  
 239          $q->shuffleanswers = 1;
 240          test_question_maker::set_standard_combined_feedback_form_data($q);
 241  
 242          $q->subquestions = array(
 243              0 => array('text' => 'System.out.println(0);', 'format' => FORMAT_HTML),
 244              1 => array('text' => 'System.out.println(0.0);', 'format' => FORMAT_HTML),
 245              2 => array('text' => '', 'format' => FORMAT_HTML),
 246          );
 247  
 248          $q->subanswers = array(
 249              0 => '0',
 250              1 => '0.0',
 251              2 => 'NULL',
 252          );
 253  
 254          $q->noanswers = 3;
 255  
 256          return $q;
 257      }
 258  
 259      /**
 260       * Makes a matching question with choices including '0' and '0.0'.
 261       *
 262       * @return qtype_match_question
 263       */
 264      public static function make_match_question_trickynums() {
 265          question_bank::load_question_definition_classes('match');
 266          $match = new qtype_match_question();
 267          test_question_maker::initialise_a_question($match);
 268          $match->name = 'Java matching';
 269          $match->questiontext = 'What is the output of each of these lines of code?';
 270          $match->generalfeedback = 'Java has some advantages over PHP I guess!';
 271          $match->qtype = question_bank::get_qtype('match');
 272  
 273          $match->shufflestems = 1;
 274  
 275          test_question_maker::set_standard_combined_feedback_fields($match);
 276  
 277          // Using unset to get 1-based arrays.
 278          $match->stems = array('', 'System.out.println(0);', 'System.out.println(0.0);');
 279          $match->stemformat = array('', FORMAT_HTML, FORMAT_HTML);
 280          $match->choices = array('', '0', '0.0', 'NULL');
 281          $match->right = array('', 1, 2);
 282          unset($match->stems[0]);
 283          unset($match->stemformat[0]);
 284          unset($match->choices[0]);
 285          unset($match->right[0]);
 286  
 287          return $match;
 288      }
 289  }