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   * 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  
 101          $qdata->options = new stdClass();
 102          $qdata->options->unitgradingtype = 0;
 103          $qdata->options->unitpenalty = 0.0;
 104          $qdata->options->showunits = qtype_numerical::UNITNONE;
 105          $qdata->options->unitsleft = 0;
 106          $qdata->options->synchronize = 0;
 107  
 108          $qdata->options->answers = array(
 109              13 => new qtype_numerical_answer(13, '{a} + {b}', 1.0, 'Very good.', FORMAT_HTML, 0.001),
 110              14 => new qtype_numerical_answer(14, '{a} - {b}', 0.0, 'Add. not subtract!.',
 111                      FORMAT_HTML, 0.001),
 112              17 => new qtype_numerical_answer(17, '*', 0.0, 'Completely wrong.', FORMAT_HTML, 0),
 113          );
 114          foreach ($qdata->options->answers as $answer) {
 115              $answer->correctanswerlength = 2;
 116              $answer->correctanswerformat = 1;
 117          }
 118  
 119          $qdata->options->units = array();
 120  
 121          return $qdata;
 122      }
 123  
 124      /**
 125       * Makes a calculated question about summing two numbers.
 126       * @return qtype_calculated_question
 127       */
 128      public function get_calculated_question_form_data_sum() {
 129          question_bank::load_question_definition_classes('calculated');
 130          $fromform = new stdClass();
 131  
 132          $fromform->name = 'Simple sum';
 133          $fromform->questiontext = 'What is {a} + {b}?';
 134          $fromform->defaultmark = 1.0;
 135          $fromform->generalfeedback = 'Generalfeedback: {={a} + {b}} is the right answer.';
 136  
 137          $fromform->unitrole = '3';
 138          $fromform->unitpenalty = 0.1;
 139          $fromform->unitgradingtypes = '1';
 140          $fromform->unitsleft = '0';
 141          $fromform->nounits = 1;
 142          $fromform->multiplier = array();
 143          $fromform->multiplier[0] = '1.0';
 144          $fromform->synchronize = 0;
 145          $fromform->answernumbering = 0;
 146          $fromform->shuffleanswers = 0;
 147  
 148          $fromform->noanswers = 6;
 149          $fromform->answer = array();
 150          $fromform->answer[0] = '{a} + {b}';
 151          $fromform->answer[1] = '{a} - {b}';
 152          $fromform->answer[2] = '*';
 153  
 154          $fromform->fraction = array();
 155          $fromform->fraction[0] = '1.0';
 156          $fromform->fraction[1] = '0.0';
 157          $fromform->fraction[2] = '0.0';
 158  
 159          $fromform->tolerance = array();
 160          $fromform->tolerance[0] = 0.001;
 161          $fromform->tolerance[1] = 0.001;
 162          $fromform->tolerance[2] = 0;
 163  
 164          $fromform->tolerancetype[0] = 1;
 165          $fromform->tolerancetype[1] = 1;
 166          $fromform->tolerancetype[2] = 1;
 167  
 168          $fromform->correctanswerlength[0] = 2;
 169          $fromform->correctanswerlength[1] = 2;
 170          $fromform->correctanswerlength[2] = 2;
 171  
 172          $fromform->correctanswerformat[0] = 1;
 173          $fromform->correctanswerformat[1] = 1;
 174          $fromform->correctanswerformat[2] = 1;
 175  
 176          $fromform->feedback = array();
 177          $fromform->feedback[0] = array();
 178          $fromform->feedback[0]['format'] = FORMAT_HTML;
 179          $fromform->feedback[0]['text'] = 'Very good.';
 180  
 181          $fromform->feedback[1] = array();
 182          $fromform->feedback[1]['format'] = FORMAT_HTML;
 183          $fromform->feedback[1]['text'] = 'Add. not subtract!';
 184  
 185          $fromform->feedback[2] = array();
 186          $fromform->feedback[2]['format'] = FORMAT_HTML;
 187          $fromform->feedback[2]['text'] = 'Completely wrong.';
 188  
 189          return $fromform;
 190      }
 191  }
 192  
 193  
 194  /**
 195   * Test implementation of {@link qtype_calculated_dataset_loader}. Gets the values
 196   * from an array passed to the constructor, rather than querying the database.
 197   *
 198   * @copyright  2011 The Open University
 199   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 200   */
 201  class qtype_calculated_test_dataset_loader extends qtype_calculated_dataset_loader{
 202      protected $valuesets;
 203      protected $aresynchronised = array();
 204  
 205      public function __construct($questionid, array $valuesets) {
 206          parent::__construct($questionid);
 207          $this->valuesets = $valuesets;
 208      }
 209  
 210      public function get_number_of_items() {
 211          return count($this->valuesets);
 212      }
 213  
 214      public function load_values($itemnumber) {
 215          return $this->valuesets[$itemnumber - 1];
 216      }
 217  
 218      public function datasets_are_synchronised($category) {
 219          return !empty($this->aresynchronised[$category]);
 220      }
 221  
 222      /**
 223       * Allows the test to mock the return value of {@link datasets_are_synchronised()}.
 224       * @param int $category
 225       * @param bool $aresychronised
 226       */
 227      public function set_are_synchronised($category, $aresychronised) {
 228          $this->aresynchronised[$category] = $aresychronised;
 229      }
 230  }