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