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 simple calculated question type.
  19   *
  20   * @package    qtype
  21   * @subpackage calculatedsimple
  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/tests/helper.php');
  31  
  32  
  33  /**
  34   * Test helper class for the simple calculated question type.
  35   *
  36   * @copyright  2011 The Open University
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class qtype_calculatedsimple_test_helper extends question_test_helper {
  40      public function get_test_questions() {
  41          return array('sum', 'sumwithvariants');
  42      }
  43  
  44      /**
  45       * Makes a simple calculated question about summing two numbers.
  46       * @return qtype_calculatedsimple_question
  47       */
  48      public function make_calculatedsimple_question_sum() {
  49          question_bank::load_question_definition_classes('calculatedsimple');
  50          $q = new qtype_calculatedsimple_question();
  51          test_question_maker::initialise_a_question($q);
  52          $q->name = 'Simple sum';
  53          $q->questiontext = 'What is {a} + {b}?';
  54          $q->generalfeedback = 'Generalfeedback: {={a} + {b}} is the right answer.';
  55  
  56          $q->answers = array(
  57              13 => new qtype_numerical_answer(13, '{a} + {b}', 1.0, 'Very good.', FORMAT_HTML, 0),
  58              14 => new qtype_numerical_answer(14, '{a} - {b}', 0.0, 'Add. not subtract!.',
  59                      FORMAT_HTML, 0),
  60              17 => new qtype_numerical_answer(17, '*', 0.0, 'Completely wrong.', FORMAT_HTML, 0),
  61          );
  62          foreach ($q->answers as $answer) {
  63              $answer->correctanswerlength = 2;
  64              $answer->correctanswerformat = 1;
  65          }
  66  
  67          $q->qtype = question_bank::get_qtype('calculated');
  68          $q->unitdisplay = qtype_numerical::UNITNONE;
  69          $q->unitgradingtype = 0;
  70          $q->unitpenalty = 0;
  71          $q->ap = new qtype_numerical_answer_processor(array());
  72  
  73          $q->datasetloader = new qtype_calculated_test_dataset_loader(0, array(
  74              array('a' => 1, 'b' => 5),
  75              array('a' => 3, 'b' => 4),
  76          ));
  77  
  78          return $q;
  79      }
  80  
  81  
  82      public function get_calculatedsimple_question_form_data_sumwithvariants() {
  83          $form = new stdClass();
  84  
  85          $form->name = 'Calculated simple';
  86  
  87          $form->qtype = 'calculatedsimple';
  88  
  89          $form->questiontext = array();
  90          $form->questiontext['text'] = '<p>This is a simple sum of two variables.</p>';
  91          $form->questiontext['format'] = '1';
  92  
  93          $form->defaultmark = 1;
  94          $form->generalfeedback = array();
  95          $form->generalfeedback['text'] = '<p>The answer is  {a} + {b}</p>';
  96          $form->generalfeedback['format'] = '1';
  97  
  98          $form->synchronize = 0;
  99          $form->initialcategory = 1;
 100          $form->reload = 1;
 101          $form->mform_isexpanded_id_answerhdr = 1;
 102          $form->noanswers = 1;
 103          $form->answer = array('{a} + {b}');
 104  
 105          $form->fraction = array('1.0');
 106  
 107          $form->tolerance = array(0.01);
 108          $form->tolerancetype = array('1');
 109  
 110          $form->correctanswerlength = array('2');
 111          $form->correctanswerformat = array('1');
 112  
 113          $form->feedback = array();
 114          $form->feedback[0] = array();
 115          $form->feedback[0]['text'] = '';
 116          $form->feedback[0]['format'] = '1';
 117  
 118          $form->unitrole = '3';
 119          $form->unitpenalty = 0.1;
 120          $form->unitgradingtypes = '1';
 121          $form->unitsleft = '0';
 122          $form->nounits = 1;
 123          $form->multiplier = array('1.0');
 124  
 125          $form->penalty = '0.3333333';
 126          $form->numhints = 2;
 127          $form->hint = array();
 128          $form->hint[0] = array();
 129          $form->hint[0]['text'] = '';
 130          $form->hint[0]['format'] = '1';
 131  
 132          $form->hint[1] = array();
 133          $form->hint[1]['text'] = '';
 134          $form->hint[1]['format'] = '1';
 135  
 136          $form->calcmin = array();
 137          $form->calcmin[1] = 1;
 138          $form->calcmin[2] = 1;
 139  
 140          $form->calcmax = array();
 141          $form->calcmax[1] = 10;
 142          $form->calcmax[2] = 10;
 143  
 144          $form->calclength = array();
 145          $form->calclength[1] = '1';
 146          $form->calclength[2] = '1';
 147  
 148          $form->calcdistribution = array();
 149          $form->calcdistribution[1] = 0;
 150          $form->calcdistribution[2] = 0;
 151  
 152          $form->datasetdef = array();
 153          $form->datasetdef[1] = '1-0-a';
 154          $form->datasetdef[2] = '1-0-b';
 155  
 156          $form->defoptions = array();
 157          $form->defoptions[1] = '';
 158          $form->defoptions[2] = '';
 159  
 160          $form->selectadd = '10';
 161          $form->selectshow = '10';
 162          $form->number = array();
 163          $form->number[1] = '2.3';
 164          $form->number[2] = '7.6';
 165          $form->number[3] = '2.1';
 166          $form->number[4] = '6.4';
 167          $form->number[5] = '1.4';
 168          $form->number[6] = '1.9';
 169          $form->number[7] = '9.9';
 170          $form->number[8] = '9.5';
 171          $form->number[9] = '9.0';
 172          $form->number[10] = '5.2';
 173          $form->number[11] = '2.1';
 174          $form->number[12] = '7.3';
 175          $form->number[13] = '7.9';
 176          $form->number[14] = '1.2';
 177          $form->number[15] = '2.3';
 178          $form->number[16] = '3.4';
 179          $form->number[17] = '1.9';
 180          $form->number[18] = '5.2';
 181          $form->number[19] = '3.4';
 182          $form->number[20] = '3.4';
 183  
 184          $form->itemid = array_fill(1, 20, 0);
 185  
 186          $form->definition = array();
 187          $form->definition[1] = '1-0-b';
 188          $form->definition[2] = '1-0-a';
 189          $form->definition[3] = '1-0-b';
 190          $form->definition[4] = '1-0-a';
 191          $form->definition[5] = '1-0-b';
 192          $form->definition[6] = '1-0-a';
 193          $form->definition[7] = '1-0-b';
 194          $form->definition[8] = '1-0-a';
 195          $form->definition[9] = '1-0-b';
 196          $form->definition[10] = '1-0-a';
 197          $form->definition[11] = '1-0-b';
 198          $form->definition[12] = '1-0-a';
 199          $form->definition[13] = '1-0-b';
 200          $form->definition[14] = '1-0-a';
 201          $form->definition[15] = '1-0-b';
 202          $form->definition[16] = '1-0-a';
 203          $form->definition[17] = '1-0-b';
 204          $form->definition[18] = '1-0-a';
 205          $form->definition[19] = '1-0-b';
 206          $form->definition[20] = '1-0-a';
 207  
 208          return $form;
 209      }
 210  
 211      public function get_calculatedsimple_question_data_sumwithvariants() {
 212          global $USER;
 213          $q = new stdClass();
 214  
 215          $q->name = 'Calculated simple';
 216          $q->createdby = $USER->id;
 217          $q->questiontext = '<p>This is a simple sum of two variables.</p>';
 218          $q->questiontextformat = '1';
 219          $q->generalfeedback = '<p>The answer is  {a} + {b}</p>';
 220          $q->generalfeedbackformat = '1';
 221          $q->defaultmark = 1;
 222          $q->penalty = 0.3333333;
 223          $q->qtype = 'calculatedsimple';
 224          $q->length = '1';
 225          $q->hidden = '0';
 226          $q->options = new stdClass();
 227          $q->options->synchronize = 0;
 228          $q->options->single = 0;
 229          $q->options->answernumbering = 'abc';
 230          $q->options->shuffleanswers = 0;
 231          $q->options->correctfeedback = '';
 232          $q->options->partiallycorrectfeedback = '';
 233          $q->options->incorrectfeedback = '';
 234          $q->options->correctfeedbackformat = 0;
 235          $q->options->partiallycorrectfeedbackformat = 0;
 236          $q->options->incorrectfeedbackformat = 0;
 237          $q->options->answers = array();
 238          $q->options->answers[0] = new stdClass();
 239          $q->options->answers[0]->id = '6977';
 240          $q->options->answers[0]->question = '3379';
 241          $q->options->answers[0]->answer = '{a} + {b}';
 242          $q->options->answers[0]->answerformat = '0';
 243          $q->options->answers[0]->fraction = 1.0;
 244          $q->options->answers[0]->feedback = '';
 245          $q->options->answers[0]->feedbackformat = '1';
 246          $q->options->answers[0]->tolerance = '0.01';
 247          $q->options->answers[0]->tolerancetype = '1';
 248          $q->options->answers[0]->correctanswerlength = '2';
 249          $q->options->answers[0]->correctanswerformat = '1';
 250  
 251          $q->options->units = array();
 252  
 253          $q->options->unitgradingtype = '0';
 254          $q->options->unitpenalty = 0.1;
 255          $q->options->showunits = '3';
 256          $q->options->unitsleft = '0';
 257  
 258          $q->hints = array();
 259  
 260          return $q;
 261      }
 262  }