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 numerical question type.
  19   *
  20   * @package    qtype
  21   * @subpackage numerical
  22   * @copyright  2011 The Open University
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  
  30  /**
  31   * Test helper class for the numerical question type.
  32   *
  33   * @copyright  2011 The Open University
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class qtype_numerical_test_helper extends question_test_helper {
  37      public function get_test_questions() {
  38          return array('pi', 'unit', 'currency', 'pi3tries');
  39      }
  40  
  41      /**
  42       * Makes a numerical question with correct ansewer 3.14, and various incorrect
  43       * answers with different feedback.
  44       * @return qtype_numerical_question
  45       */
  46      public function make_numerical_question_pi() {
  47          question_bank::load_question_definition_classes('numerical');
  48          $num = new qtype_numerical_question();
  49          test_question_maker::initialise_a_question($num);
  50          $num->name = 'Pi to two d.p.';
  51          $num->questiontext = 'What is pi to two d.p.?';
  52          $num->generalfeedback = 'Generalfeedback: 3.14 is the right answer.';
  53          $num->answers = array(
  54              13 => new qtype_numerical_answer(13, '3.14',  1.0, 'Very good.',
  55                      FORMAT_HTML, 0),
  56              14 => new qtype_numerical_answer(14, '3.142', 0.0, 'Too accurate.',
  57                      FORMAT_HTML, 0.005),
  58              15 => new qtype_numerical_answer(15, '3.1',   0.0, 'Not accurate enough.',
  59                      FORMAT_HTML, 0.05),
  60              16 => new qtype_numerical_answer(16, '3',     0.0, 'Not accurate enough.',
  61                      FORMAT_HTML, 0.5),
  62              17 => new qtype_numerical_answer(17, '*',     0.0, 'Completely wrong.',
  63                      FORMAT_HTML, 0),
  64          );
  65          $num->qtype = question_bank::get_qtype('numerical');
  66          $num->unitdisplay = qtype_numerical::UNITOPTIONAL;
  67          $num->unitgradingtype = 0;
  68          $num->unitpenalty = 0.2;
  69          $num->ap = new qtype_numerical_answer_processor(array());
  70  
  71          return $num;
  72      }
  73  
  74      /**
  75       * Get the form data that corresponds to saving a numerical question.
  76       *
  77       * This question asks for Pi to two decimal places. It has feedback
  78       * for various wrong responses. There is hint data there, but
  79       * it is all blank, so no hints are created if this question is saved.
  80       *
  81       * @return stdClass simulated question form data.
  82       */
  83      public function get_numerical_question_form_data_pi() {
  84          $form = new stdClass();
  85          $form->name = 'Pi to two d.p.';
  86          $form->questiontext = array();
  87          $form->questiontext['format'] = '1';
  88          $form->questiontext['text'] = 'What is pi to two d.p.?';
  89  
  90          $form->defaultmark = 1;
  91          $form->generalfeedback = array();
  92          $form->generalfeedback['format'] = '1';
  93          $form->generalfeedback['text'] = 'Generalfeedback: 3.14 is the right answer.';
  94  
  95          $form->noanswers = 6;
  96          $form->answer = array();
  97          $form->answer[0] = '3.14';
  98          $form->answer[1] = '3.142';
  99          $form->answer[2] = '3.1';
 100          $form->answer[3] = '3';
 101          $form->answer[4] = '*';
 102          $form->answer[5] = '';
 103  
 104          $form->tolerance = array();
 105          $form->tolerance[0] = 0;
 106          $form->tolerance[1] = 0;
 107          $form->tolerance[2] = 0;
 108          $form->tolerance[3] = 0;
 109          $form->tolerance[4] = 0;
 110          $form->tolerance[5] = 0;
 111  
 112          $form->fraction = array();
 113          $form->fraction[0] = '1.0';
 114          $form->fraction[1] = '0.0';
 115          $form->fraction[2] = '0.0';
 116          $form->fraction[3] = '0.0';
 117          $form->fraction[4] = '0.0';
 118          $form->fraction[5] = '0.0';
 119  
 120          $form->feedback = array();
 121          $form->feedback[0] = array();
 122          $form->feedback[0]['format'] = '1';
 123          $form->feedback[0]['text'] = 'Very good.';
 124  
 125          $form->feedback[1] = array();
 126          $form->feedback[1]['format'] = '1';
 127          $form->feedback[1]['text'] = 'Too accurate.';
 128  
 129          $form->feedback[2] = array();
 130          $form->feedback[2]['format'] = '1';
 131          $form->feedback[2]['text'] = 'Not accurate enough.';
 132  
 133          $form->feedback[3] = array();
 134          $form->feedback[3]['format'] = '1';
 135          $form->feedback[3]['text'] = 'Not accurate enough.';
 136  
 137          $form->feedback[4] = array();
 138          $form->feedback[4]['format'] = '1';
 139          $form->feedback[4]['text'] = 'Completely wrong.';
 140  
 141          $form->feedback[5] = array();
 142          $form->feedback[5]['format'] = '1';
 143          $form->feedback[5]['text'] = '';
 144  
 145          $form->unitrole = '3';
 146          $form->unitpenalty = 0.1;
 147          $form->unitgradingtypes = '1';
 148          $form->unitsleft = '0';
 149          $form->nounits = 1;
 150          $form->multiplier = array();
 151          $form->multiplier[0] = '1.0';
 152  
 153          $form->penalty = '0.3333333';
 154          $form->numhints = 2;
 155          $form->hint = array();
 156          $form->hint[0] = array();
 157          $form->hint[0]['format'] = '1';
 158          $form->hint[0]['text'] = '';
 159  
 160          $form->hint[1] = array();
 161          $form->hint[1]['format'] = '1';
 162          $form->hint[1]['text'] = '';
 163  
 164          $form->qtype = 'numerical';
 165          $form->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
 166  
 167          return $form;
 168      }
 169  
 170      /**
 171       * Get the form data that corresponds to saving a numerical question.
 172       *
 173       * Like {@link get_numerical_question_form_data_pi()}, but
 174       * this time with two hints, making this suitable for use
 175       * with the Interactive with multiple tries behaviour.
 176       *
 177       * @return stdClass simulated question form data.
 178       */
 179      public function get_numerical_question_form_data_pi3tries() {
 180          $form = $this->get_numerical_question_form_data_pi();
 181          $form->hint[0]['text'] = 'First hint';
 182          $form->hint[1]['text'] = 'Second hint';
 183          return $form;
 184      }
 185  
 186      public function get_numerical_question_data_pi() {
 187          $q = new stdClass();
 188          $q->name = 'Pi to two d.p.';
 189          $q->questiontext = 'What is pi to two d.p.?';
 190          $q->questiontextformat = FORMAT_HTML;
 191          $q->generalfeedback = 'Generalfeedback: 3.14 is the right answer.';
 192          $q->generalfeedbackformat = FORMAT_HTML;
 193          $q->defaultmark = 1;
 194          $q->penalty = 0.3333333;
 195          $q->qtype = 'numerical';
 196          $q->length = '1';
 197          $q->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
 198          $q->version = 1;
 199          $q->createdby = '2';
 200          $q->modifiedby = '2';
 201          $q->options = new stdClass();
 202          $q->options->answers = array();
 203          $q->options->answers[0] = new stdClass();
 204          $q->options->answers[0]->answer = '3.14';
 205          $q->options->answers[0]->fraction = 1.0;
 206          $q->options->answers[0]->feedback = 'Very good.';
 207          $q->options->answers[0]->feedbackformat = FORMAT_HTML;
 208          $q->options->answers[0]->tolerance = '0';
 209  
 210          $q->options->answers[1] = new stdClass();
 211          $q->options->answers[1]->answer = '3.142';
 212          $q->options->answers[1]->fraction = 0.0;
 213          $q->options->answers[1]->feedback = 'Too accurate.';
 214          $q->options->answers[1]->feedbackformat = FORMAT_HTML;
 215          $q->options->answers[1]->tolerance = '0';
 216  
 217          $q->options->answers[2] = new stdClass();
 218          $q->options->answers[2]->answer = '3.1';
 219          $q->options->answers[2]->fraction = 0.0;
 220          $q->options->answers[2]->feedback = 'Not accurate enough.';
 221          $q->options->answers[2]->feedbackformat = FORMAT_HTML;
 222          $q->options->answers[2]->tolerance = '0';
 223  
 224          $q->options->answers[3] = new stdClass();
 225          $q->options->answers[3]->answer = '3';
 226          $q->options->answers[3]->answerformat = '0';
 227          $q->options->answers[3]->fraction = 0.0;
 228          $q->options->answers[3]->feedback = 'Not accurate enough.';
 229          $q->options->answers[3]->feedbackformat = FORMAT_HTML;
 230          $q->options->answers[3]->tolerance = '0';
 231  
 232          $q->options->answers[4] = new stdClass();
 233          $q->options->answers[4]->answer = '*';
 234          $q->options->answers[4]->answerformat = '0';
 235          $q->options->answers[4]->fraction = 0.0;
 236          $q->options->answers[4]->feedback = 'Completely wrong.';
 237          $q->options->answers[4]->feedbackformat = FORMAT_HTML;
 238          $q->options->answers[4]->tolerance = '0';
 239  
 240          $q->options->units = array();
 241  
 242          $q->options->unitgradingtype = '0';
 243          $q->options->unitpenalty = 0.1;
 244          $q->options->showunits = '3';
 245          $q->options->unitsleft = '0';
 246  
 247          return $q;
 248      }
 249  
 250          /**
 251       * Makes a numerical question with a choice (select menu) of units.
 252       * @return qtype_numerical_question
 253       */
 254      public function make_numerical_question_unit() {
 255          question_bank::load_question_definition_classes('numerical');
 256          $num = new qtype_numerical_question();
 257          test_question_maker::initialise_a_question($num);
 258          $num->name = 'Numerical with units';
 259          $num->questiontext = 'What is 1 m + 25 cm?';
 260          $num->generalfeedback = 'Generalfeedback: 1.25m or 125cm is the right answer.';
 261          $num->answers = array(
 262              13 => new qtype_numerical_answer(13, '1.25', 1.0, 'Very good.', FORMAT_HTML, 0),
 263              14 => new qtype_numerical_answer(14, '1.25', 0.5, 'Vaguely right.', FORMAT_HTML, 0.05),
 264              17 => new qtype_numerical_answer(17, '*', 0.0, 'Completely wrong.', FORMAT_HTML, 0),
 265          );
 266          $num->qtype = question_bank::get_qtype('numerical');
 267          $num->unitdisplay = qtype_numerical::UNITSELECT;
 268          $num->unitgradingtype = qtype_numerical::UNITGRADEDOUTOFMARK;
 269          $num->unitpenalty = 0.5;
 270          $num->ap = new qtype_numerical_answer_processor(array('m' => 1, 'cm' => 100));
 271  
 272          return $num;
 273      }
 274  
 275      /**
 276       * Makes a numerical question with correct ansewer 3.14, and various incorrect
 277       * answers with different feedback.
 278       * @return qtype_numerical_question
 279       */
 280      public function make_numerical_question_currency() {
 281          question_bank::load_question_definition_classes('numerical');
 282          $num = new qtype_numerical_question();
 283          test_question_maker::initialise_a_question($num);
 284          $num->name = 'Add money';
 285          $num->questiontext = 'What is $666 + $666?';
 286          $num->generalfeedback = 'Generalfeedback: $1,332 is the right answer.';
 287          $num->answers = array(
 288              13 => new qtype_numerical_answer(13, '1332', 1.0, 'Very good.', FORMAT_HTML, 0),
 289              14 => new qtype_numerical_answer(14, '*', 0.0, 'Wrong.', FORMAT_HTML, 0),
 290          );
 291          $num->qtype = question_bank::get_qtype('numerical');
 292          $num->unitdisplay = qtype_numerical::UNITINPUT;
 293          $num->unitgradingtype = qtype_numerical::UNITGRADEDOUTOFMAX;
 294          $num->unitpenalty = 0.2;
 295          $num->ap = new qtype_numerical_answer_processor(array('$' => 1), true);
 296  
 297          return $num;
 298      }
 299  }