Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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 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->hidden = 0;
  63          $q->createdby = $USER->id;
  64          $q->modifiedby = $USER->id;
  65  
  66          $q->options = new stdClass();
  67          $q->options->shuffleanswers = 1;
  68          test_question_maker::set_standard_combined_feedback_fields($q->options);
  69  
  70          $q->options->subquestions = array(
  71              14 => (object) array(
  72                  'id' => 14,
  73                  'questiontext' => 'frog',
  74                  'questiontextformat' => FORMAT_HTML,
  75                  'answertext' => 'amphibian'),
  76              15 => (object) array(
  77                  'id' => 15,
  78                  'questiontext' => 'cat',
  79                  'questiontextformat' => FORMAT_HTML,
  80                  'answertext' => 'mammal'),
  81              16 => (object) array(
  82                  'id' => 16,
  83                  'questiontext' => 'newt',
  84                  'questiontextformat' => FORMAT_HTML,
  85                  'answertext' => 'amphibian'),
  86              17 => (object) array(
  87                  'id' => 17,
  88                  'questiontext' => '',
  89                  'questiontextformat' => FORMAT_HTML,
  90                  'answertext' => 'insect'),
  91          );
  92  
  93          return $q;
  94      }
  95  
  96      /**
  97       * Makes a match question about completing two blanks in some text.
  98       * @return object the question definition data, as it might be returned from
  99       *      the question editing form.
 100       */
 101      public function get_match_question_form_data_foursubq() {
 102          $q = new stdClass();
 103          $q->name = 'Matching question';
 104          $q->questiontext = array('text' => 'Classify the animals.', 'format' => FORMAT_HTML);
 105          $q->generalfeedback = array('text' => 'General feedback.', 'format' => FORMAT_HTML);
 106          $q->defaultmark = 1;
 107          $q->penalty = 0.3333333;
 108  
 109          $q->shuffleanswers = 1;
 110          test_question_maker::set_standard_combined_feedback_form_data($q);
 111  
 112          $q->subquestions = array(
 113              0 => array('text' => 'frog', 'format' => FORMAT_HTML),
 114              1 => array('text' => 'cat', 'format' => FORMAT_HTML),
 115              2 => array('text' => 'newt', 'format' => FORMAT_HTML),
 116              3 => array('text' => '', 'format' => FORMAT_HTML));
 117  
 118          $q->subanswers = array(
 119              0 => 'amphibian',
 120              1 => 'mammal',
 121              2 => 'amphibian',
 122              3 => 'insect'
 123          );
 124  
 125          $q->noanswers = 4;
 126  
 127          return $q;
 128      }
 129  
 130      /**
 131       * Makes a matching question to classify 'Dog', 'Frog', 'Toad' and 'Cat' as
 132       * 'Mammal', 'Amphibian' or 'Insect'.
 133       * defaultmark 1. Stems are shuffled by default.
 134       * @return qtype_match_question
 135       */
 136      public static function make_match_question_foursubq() {
 137          question_bank::load_question_definition_classes('match');
 138          $match = new qtype_match_question();
 139          test_question_maker::initialise_a_question($match);
 140          $match->name = 'Matching question';
 141          $match->questiontext = 'Classify the animals.';
 142          $match->generalfeedback = 'Frogs and toads are amphibians, the others are mammals.';
 143          $match->qtype = question_bank::get_qtype('match');
 144  
 145          $match->shufflestems = 1;
 146  
 147          test_question_maker::set_standard_combined_feedback_fields($match);
 148  
 149          // Using unset to get 1-based arrays.
 150          $match->stems = array('', 'Dog', 'Frog', 'Toad', 'Cat');
 151          $match->stemformat = array('', FORMAT_HTML, FORMAT_HTML, FORMAT_HTML, FORMAT_HTML);
 152          $match->choices = array('', 'Mammal', 'Amphibian', 'Insect');
 153          $match->right = array('', 1, 2, 2, 1);
 154          unset($match->stems[0]);
 155          unset($match->stemformat[0]);
 156          unset($match->choices[0]);
 157          unset($match->right[0]);
 158  
 159          return $match;
 160      }
 161  
 162      /**
 163       * Makes a matching question with choices including '0' and '0.0'.
 164       *
 165       * @return object the question definition data, as it might be returned from
 166       * get_question_options.
 167       */
 168      public function get_match_question_data_trickynums() {
 169          global $USER;
 170  
 171          $q = new stdClass();
 172          test_question_maker::initialise_question_data($q);
 173          $q->name = 'Java matching';
 174          $q->qtype = 'match';
 175          $q->parent = 0;
 176          $q->questiontext = 'What is the output of each of these lines of code?';
 177          $q->questiontextformat = FORMAT_HTML;
 178          $q->generalfeedback = 'Java has some advantages over PHP I guess!';
 179          $q->generalfeedbackformat = FORMAT_HTML;
 180          $q->defaultmark = 1;
 181          $q->penalty = 0.3333333;
 182          $q->length = 1;
 183          $q->hidden = 0;
 184          $q->createdby = $USER->id;
 185          $q->modifiedby = $USER->id;
 186  
 187          $q->options = new stdClass();
 188          $q->options->shuffleanswers = 1;
 189          test_question_maker::set_standard_combined_feedback_fields($q->options);
 190  
 191          $q->options->subquestions = array(
 192                  14 => (object) array(
 193                          'id' => 14,
 194                          'questiontext' => 'System.out.println(0);',
 195                          'questiontextformat' => FORMAT_HTML,
 196                          'answertext' => '0'),
 197                  15 => (object) array(
 198                          'id' => 15,
 199                          'questiontext' => 'System.out.println(0.0);',
 200                          'questiontextformat' => FORMAT_HTML,
 201                          'answertext' => '0.0'),
 202                  16 => (object) array(
 203                          'id' => 16,
 204                          'questiontext' => '',
 205                          'questiontextformat' => FORMAT_HTML,
 206                          'answertext' => 'NULL'),
 207          );
 208  
 209          return $q;
 210      }
 211  
 212      /**
 213       * Makes a match question about completing two blanks in some text.
 214       * @return object the question definition data, as it might be returned from
 215       *      the question editing form.
 216       */
 217      public function get_match_question_form_data_trickynums() {
 218          $q = new stdClass();
 219          $q->name = 'Java matching';
 220          $q->questiontext = ['text' => 'What is the output of each of these lines of code?', 'format' => FORMAT_HTML];
 221          $q->generalfeedback = ['text' => 'Java has some advantages over PHP I guess!', 'format' => FORMAT_HTML];
 222          $q->defaultmark = 1;
 223          $q->penalty = 0.3333333;
 224  
 225          $q->shuffleanswers = 1;
 226          test_question_maker::set_standard_combined_feedback_form_data($q);
 227  
 228          $q->subquestions = array(
 229              0 => array('text' => 'System.out.println(0);', 'format' => FORMAT_HTML),
 230              1 => array('text' => 'System.out.println(0.0);', 'format' => FORMAT_HTML),
 231              2 => array('text' => '', 'format' => FORMAT_HTML),
 232          );
 233  
 234          $q->subanswers = array(
 235              0 => '0',
 236              1 => '0.0',
 237              2 => 'NULL',
 238          );
 239  
 240          $q->noanswers = 3;
 241  
 242          return $q;
 243      }
 244  
 245      /**
 246       * Makes a matching question with choices including '0' and '0.0'.
 247       *
 248       * @return qtype_match_question
 249       */
 250      public static function make_match_question_trickynums() {
 251          question_bank::load_question_definition_classes('match');
 252          $match = new qtype_match_question();
 253          test_question_maker::initialise_a_question($match);
 254          $match->name = 'Java matching';
 255          $match->questiontext = 'What is the output of each of these lines of code?';
 256          $match->generalfeedback = 'Java has some advantages over PHP I guess!';
 257          $match->qtype = question_bank::get_qtype('match');
 258  
 259          $match->shufflestems = 1;
 260  
 261          test_question_maker::set_standard_combined_feedback_fields($match);
 262  
 263          // Using unset to get 1-based arrays.
 264          $match->stems = array('', 'System.out.println(0);', 'System.out.println(0.0);');
 265          $match->stemformat = array('', FORMAT_HTML, FORMAT_HTML);
 266          $match->choices = array('', '0', '0.0', 'NULL');
 267          $match->right = array('', 1, 2);
 268          unset($match->stems[0]);
 269          unset($match->stemformat[0]);
 270          unset($match->choices[0]);
 271          unset($match->right[0]);
 272  
 273          return $match;
 274      }
 275  }