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] [Versions 401 and 402] [Versions 401 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 calculated question type.
  19   *
  20   * @package    qtype
  21   * @subpackage calculated
  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  global $CFG;
  30  require_once($CFG->dirroot . '/question/type/calculated/question.php');
  31  require_once($CFG->dirroot . '/question/type/numerical/question.php');
  32  require_once($CFG->dirroot . '/question/type/numerical/questiontype.php');
  33  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  34  
  35  
  36  /**
  37   * Test helper class for the calculated question type.
  38   *
  39   * @copyright  2011 The Open University
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class qtype_calculated_test_helper extends question_test_helper {
  43      public function get_test_questions() {
  44          return array('sum');
  45      }
  46  
  47      /**
  48       * Makes a calculated question about summing two numbers.
  49       * @return qtype_calculated_question
  50       */
  51      public function make_calculated_question_sum() {
  52          question_bank::load_question_definition_classes('calculated');
  53          $q = new qtype_calculated_question();
  54          test_question_maker::initialise_a_question($q);
  55          $q->name = 'Simple sum';
  56          $q->questiontext = 'What is {a} + {b}?';
  57          $q->generalfeedback = 'Generalfeedback: {={a} + {b}} is the right answer.';
  58  
  59          $q->answers = array(
  60              13 => new qtype_numerical_answer(13, '{a} + {b}', 1.0, 'Very good.', FORMAT_HTML, 0),
  61              14 => new qtype_numerical_answer(14, '{a} - {b}', 0.0, 'Add. not subtract!.',
  62                      FORMAT_HTML, 0),
  63              17 => new qtype_numerical_answer(17, '*', 0.0, 'Completely wrong.', FORMAT_HTML, 0),
  64          );
  65          foreach ($q->answers as $answer) {
  66              $answer->correctanswerlength = 2;
  67              $answer->correctanswerformat = 1;
  68          }
  69  
  70          $q->qtype = question_bank::get_qtype('calculated');
  71          $q->unitdisplay = qtype_numerical::UNITNONE;
  72          $q->unitgradingtype = 0;
  73          $q->unitpenalty = 0;
  74          $q->ap = new qtype_numerical_answer_processor(array());
  75          $q->synchronised = false;
  76  
  77          $q->datasetloader = new qtype_calculated_test_dataset_loader(0, array(
  78              array('a' => 1, 'b' => 5),
  79              array('a' => 3, 'b' => 4),
  80              array('a' => 3, 'b' => 0.01416),
  81              array('a' => 31, 'b' => 0.01416),
  82          ));
  83  
  84          return $q;
  85      }
  86  
  87      /**
  88       * Makes a calculated question about summing two numbers.
  89       * @return qtype_calculated_question
  90       */
  91      public function get_calculated_question_data_sum() {
  92          question_bank::load_question_definition_classes('calculated');
  93          $qdata = new stdClass();
  94          test_question_maker::initialise_question_data($qdata);
  95  
  96          $qdata->qtype = 'calculated';
  97          $qdata->name = 'Simple sum';
  98          $qdata->questiontext = 'What is {a} + {b}?';
  99          $qdata->generalfeedback = 'Generalfeedback: {={a} + {b}} is the right answer.';
 100          $qdata->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
 101  
 102          $qdata->options = new stdClass();
 103          $qdata->options->unitgradingtype = 0;
 104          $qdata->options->unitpenalty = 0.0;
 105          $qdata->options->showunits = qtype_numerical::UNITNONE;
 106          $qdata->options->unitsleft = 0;
 107          $qdata->options->synchronize = 0;
 108  
 109          $qdata->options->answers = array(
 110              13 => new qtype_numerical_answer(13, '{a} + {b}', 1.0, 'Very good.', FORMAT_HTML, 0.001),
 111              14 => new qtype_numerical_answer(14, '{a} - {b}', 0.0, 'Add. not subtract!.',
 112                      FORMAT_HTML, 0.001),
 113              17 => new qtype_numerical_answer(17, '*', 0.0, 'Completely wrong.', FORMAT_HTML, 0),
 114          );
 115          foreach ($qdata->options->answers as $answer) {
 116              $answer->correctanswerlength = 2;
 117              $answer->correctanswerformat = 1;
 118          }
 119  
 120          $qdata->options->units = array();
 121  
 122          return $qdata;
 123      }
 124  
 125      /**
 126       * Makes a calculated question about summing two numbers.
 127       * @return qtype_calculated_question
 128       */
 129      public function get_calculated_question_form_data_sum() {
 130          question_bank::load_question_definition_classes('calculated');
 131          $fromform = new stdClass();
 132  
 133          $fromform->name = 'Simple sum';
 134          $fromform->questiontext = 'What is {a} + {b}?';
 135          $fromform->defaultmark = 1.0;
 136          $fromform->generalfeedback = 'Generalfeedback: {={a} + {b}} is the right answer.';
 137  
 138          $fromform->unitrole = '3';
 139          $fromform->unitpenalty = 0.1;
 140          $fromform->unitgradingtypes = '1';
 141          $fromform->unitsleft = '0';
 142          $fromform->nounits = 1;
 143          $fromform->multiplier = array();
 144          $fromform->multiplier[0] = '1.0';
 145          $fromform->synchronize = 0;
 146          $fromform->answernumbering = 0;
 147          $fromform->shuffleanswers = 0;
 148  
 149          $fromform->noanswers = 6;
 150          $fromform->answer = array();
 151          $fromform->answer[0] = '{a} + {b}';
 152          $fromform->answer[1] = '{a} - {b}';
 153          $fromform->answer[2] = '*';
 154  
 155          $fromform->fraction = array();
 156          $fromform->fraction[0] = '1.0';
 157          $fromform->fraction[1] = '0.0';
 158          $fromform->fraction[2] = '0.0';
 159  
 160          $fromform->tolerance = array();
 161          $fromform->tolerance[0] = 0.001;
 162          $fromform->tolerance[1] = 0.001;
 163          $fromform->tolerance[2] = 0;
 164  
 165          $fromform->tolerancetype[0] = 1;
 166          $fromform->tolerancetype[1] = 1;
 167          $fromform->tolerancetype[2] = 1;
 168  
 169          $fromform->correctanswerlength[0] = 2;
 170          $fromform->correctanswerlength[1] = 2;
 171          $fromform->correctanswerlength[2] = 2;
 172  
 173          $fromform->correctanswerformat[0] = 1;
 174          $fromform->correctanswerformat[1] = 1;
 175          $fromform->correctanswerformat[2] = 1;
 176  
 177          $fromform->feedback = array();
 178          $fromform->feedback[0] = array();
 179          $fromform->feedback[0]['format'] = FORMAT_HTML;
 180          $fromform->feedback[0]['text'] = 'Very good.';
 181  
 182          $fromform->feedback[1] = array();
 183          $fromform->feedback[1]['format'] = FORMAT_HTML;
 184          $fromform->feedback[1]['text'] = 'Add. not subtract!';
 185  
 186          $fromform->feedback[2] = array();
 187          $fromform->feedback[2]['format'] = FORMAT_HTML;
 188          $fromform->feedback[2]['text'] = 'Completely wrong.';
 189  
 190          $fromform->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
 191  
 192          return $fromform;
 193      }
 194  }
 195  
 196  
 197  /**
 198   * Test implementation of {@link qtype_calculated_dataset_loader}. Gets the values
 199   * from an array passed to the constructor, rather than querying the database.
 200   *
 201   * @copyright  2011 The Open University
 202   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 203   */
 204  class qtype_calculated_test_dataset_loader extends qtype_calculated_dataset_loader{
 205      protected $valuesets;
 206      protected $aresynchronised = array();
 207  
 208      public function __construct($questionid, array $valuesets) {
 209          parent::__construct($questionid);
 210          $this->valuesets = $valuesets;
 211      }
 212  
 213      public function get_number_of_items() {
 214          return count($this->valuesets);
 215      }
 216  
 217      public function load_values($itemnumber) {
 218          return $this->valuesets[$itemnumber - 1];
 219      }
 220  
 221      public function datasets_are_synchronised($category) {
 222          return !empty($this->aresynchronised[$category]);
 223      }
 224  
 225      /**
 226       * Allows the test to mock the return value of {@link datasets_are_synchronised()}.
 227       * @param int $category
 228       * @param bool $aresychronised
 229       */
 230      public function set_are_synchronised($category, $aresychronised) {
 231          $this->aresynchronised[$category] = $aresychronised;
 232      }
 233  }