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.
   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   * Tests of the upgrade to the new Moodle question engine for attempts at
  19   * numerical questions.
  20   *
  21   * @package    qtype
  22   * @subpackage numerical
  23   * @copyright  2009 The Open University
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  global $CFG;
  31  require_once($CFG->dirroot . '/question/engine/upgrade/tests/helper.php');
  32  
  33  
  34  /**
  35   * Testing the upgrade of numerical question attempts.
  36   *
  37   * @copyright  2009 The Open University
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class qtype_numerical_attempt_upgrader_test extends question_attempt_upgrader_test_base {
  41  
  42      public function test_numerical_deferredfeedback_history620() {
  43          $quiz = (object) array(
  44              'id' => '221',
  45              'course' => '187',
  46              'name' => 'Practice CTMA04',
  47              'intro' => 'This is the Practice CTMA04. Your mark for this CTMA <span style="font-style: italic">does not </span>contribute to your continuous assessment mark for B680.<br /><br />This CTMA covers material primarily in Books 10 and 11, however some of the questions may return to material you covered in Books 0 to 9. There are 100 questions in total.<br /><br />It is a function of this testing software that it cannot provide for every possible spelling error that you make. The onus is therefore on you, the student, to ensure that your spelling is correct. ',
  48              'introformat' => FORMAT_HTML,
  49              'questiondecimalpoints' => '-1',
  50              'showuserpicture' => '1',
  51              'showblocks' => '1',
  52              'timeopen' => '1178492400',
  53              'timeclose' => '1193875200',
  54              'preferredbehaviour' => 'deferredfeedback',
  55              'attempts' => '0',
  56              'attemptonlast' => '0',
  57              'grademethod' => '1',
  58              'decimalpoints' => '2',
  59              'review' => '71727591',
  60              'questionsperpage' => '1',
  61              'shufflequestions' => '0',
  62              'shuffleanswers' => '0',
  63              'sumgrades' => '100',
  64              'grade' => '100',
  65              'timecreated' => '0',
  66              'timemodified' => '1195232889',
  67              'password' => '',
  68              'subnet' => '',
  69              'popup' => '0',
  70              'delay1' => '0',
  71              'delay2' => '0',
  72              'timelimit' => '0',
  73          );
  74          $attempt = (object) array(
  75              'id' => '4034',
  76              'uniqueid' => '4034',
  77              'quiz' => '221',
  78              'userid' => '405',
  79              'attempt' => '1',
  80              'sumgrades' => '59',
  81              'timestart' => '1178564323',
  82              'timefinish' => '1178575685',
  83              'timemodified' => '1178571037',
  84              'layout' => '4184,0,4185,0,4154,0,4186,0,4187,0,4188,0,4189,0,4190,0,4162,0,4191,0,4192,0,4193,0,4254,0,4195,0,4196,0,4163,0,4197,0,4198,0,4199,0,4164,0,4200,0,4165,0,4201,0,4202,0,4166,0,4203,0,4204,0,4205,0,4167,0,4155,0,4168,0,4206,0,4207,0,4208,0,4209,0,4210,0,4211,0,4212,0,4213,0,4214,0,4156,0,4215,0,4216,0,4217,0,4169,0,4170,0,4157,0,4218,0,4219,0,4220,0,4171,0,4221,0,4172,0,4222,0,4223,0,4224,0,4225,0,4226,0,4227,0,4228,0,4173,0,4229,0,4230,0,4231,0,4232,0,4174,0,4233,0,4234,0,4235,0,4236,0,4237,0,4238,0,4239,0,4240,0,4241,0,4242,0,4158,0,4243,0,4244,0,4245,0,4246,0,4159,0,4175,0,4247,0,4176,0,4248,0,4177,0,4160,0,4249,0,4178,0,4250,0,4161,0,4251,0,4179,0,4252,0,4180,0,4181,0,4182,0,4253,0,4183,0',
  85              'preview' => '0',
  86          );
  87          $question = (object) array(
  88              'id' => '4165',
  89              'category' => '204',
  90              'parent' => '0',
  91              'name' => '4ccP22 Book 11 Electronic Activity 6.3 Screen F2 NPV calcn(7)',
  92              'questiontext' => 'Calculate your answer to the nearest WHOLE £ and enter it in the blank space provided.
  93          <p>[Note: Please <b><i>DO NOT</i></b> enter commas, spaces or £ signs within the number you enter.] </p>
  94          <p>The cash flows of a project are set out below: </p>
  95          <p>
  96              <table width="500" border="1"><tbody>
  97                  <tr>
  98                      <th valign="top" width="50%" halign="CENTER">year
  99                      </th>
 100                      <th valign="top" width="50%">£
 101                      </th>
 102                  </tr>
 103                  <tr>
 104                      <td>Year 0
 105                      </td>
 106                      <td>(28,000)
 107                      </td>
 108                  </tr>
 109                  <tr>
 110                      <td>Year 1
 111                      </td>
 112                      <td>18,000
 113                      </td>
 114                  </tr>
 115                  <tr>
 116                      <td>Year 2
 117                      </td>
 118                      <td>16,000
 119                      </td>
 120                  </tr></tbody>
 121              </table></p>
 122          <p>Using the Table in Book 11, what is the project\'s net present value using a discount rate of 10%? </p>
 123          <p>If it is negative, put a minus sign before the number you enter. </p>
 124          <p>£ </p>',
 125              'questiontextformat' => '1',
 126              'defaultmark' => '1',
 127              'penalty' => '1',
 128              'qtype' => 'numerical',
 129              'length' => '1',
 130              'stamp' => 'learn.open.ac.uk+070417143727+5hOqJ8',
 131              'version' => 'learn.open.ac.uk+070417154843+o0iHPi',
 132              'hidden' => '0',
 133              'generalfeedback' => '',
 134              'generalfeedbackformat' => '1',
 135              'timecreated' => '0',
 136              'timemodified' => '0',
 137              'createdby' => null,
 138              'modifiedby' => null,
 139              'unlimited' => null,
 140              'maxmark' => '1',
 141              'options' => (object) array(
 142                  'answers' => array(
 143                      12729 => (object) array(
 144                          'question' => '4165',
 145                          'answer' => '1586',
 146                          'fraction' => '1',
 147                          'feedback' => 'Yes, the correct answer is £1,586. Well done!
 148          <p>Note to the answer:<br />£(18,000 x 0.9091) + (£16,000 x 0.8264) - £28,000 = £1,586.2 (the factors are found in the Table 1 of the Appendix to Book 11) </p>
 149          <p>Book 11 Electronic Activity 6.3 Screen F2 </p>',
 150                          'tolerance' => '0',
 151                          'id' => 12729,
 152                      ),
 153                      12730 => (object) array(
 154                          'question' => '4165',
 155                          'answer' => '*',
 156                          'fraction' => '0',
 157                          'feedback' => 'The correct answer is £1,586.
 158          <p>Note to the answer:<br />£(18,000 x 0.9091) + (£16,000 x 0.8264) - £28,000 = £1,586.2 (the factors are found in the Table 1 of the Appendix to Book 11) </p>
 159          <p>Book 11 Electronic Activity 6.3 Screen F2 </p>',
 160                          'tolerance' => '0',
 161                          'id' => 12730,
 162                      ),
 163                  ),
 164                  'units' => array(
 165                  ),
 166              ),
 167              'hints' => false,
 168          );
 169          $qsession = (object) array(
 170              'id' => '39882',
 171              'attemptid' => '4034',
 172              'questionid' => '4165',
 173              'newest' => '92971',
 174              'newgraded' => '92971',
 175              'sumpenalty' => '1',
 176              'manualcomment' => '',
 177              'manualcommentformat' => '1',
 178              'flagged' => '1',
 179          );
 180          $qstates = array(
 181              92694 => (object) array(
 182                  'attempt' => '4034',
 183                  'question' => '4165',
 184                  'originalquestion' => '0',
 185                  'seq_number' => '0',
 186                  'answer' => '',
 187                  'timestamp' => '1178564323',
 188                  'event' => '0',
 189                  'grade' => '0',
 190                  'raw_grade' => '0',
 191                  'penalty' => '0',
 192                  'id' => 92694,
 193              ),
 194              92764 => (object) array(
 195                  'attempt' => '4034',
 196                  'question' => '4165',
 197                  'originalquestion' => '0',
 198                  'seq_number' => '1',
 199                  'answer' => '17520',
 200                  'timestamp' => '1178567143',
 201                  'event' => '2',
 202                  'grade' => '0',
 203                  'raw_grade' => '0',
 204                  'penalty' => '1',
 205                  'id' => 92764,
 206              ),
 207              92971 => (object) array(
 208                  'attempt' => '4034',
 209                  'question' => '4165',
 210                  'originalquestion' => '0',
 211                  'seq_number' => '2',
 212                  'answer' => '17520',
 213                  'timestamp' => '1178567143',
 214                  'event' => '6',
 215                  'grade' => '0',
 216                  'raw_grade' => '0',
 217                  'penalty' => '1',
 218                  'id' => 92971,
 219              ),
 220          );
 221  
 222          $qa = $this->updater->convert_question_attempt($quiz, $attempt, $question, $qsession, $qstates);
 223  
 224          $expectedqa = (object) array(
 225              'behaviour' => 'deferredfeedback',
 226              'questionid' => 4165,
 227              'variant' => 1,
 228              'maxmark' => 1,
 229              'minfraction' => 0,
 230              'maxfraction' => 1,
 231              'flagged' => 0,
 232              'questionsummary' => "Calculate your answer to the nearest WHOLE £ and enter it in the blank space provided. \n\n[Note: Please _DO NOT_ enter commas, spaces or £ signs within the number you enter.]  \n\nThe cash flows of a project are set out below: \n\n \t\tYEAR \n \t\t£ \n\n \t\tYear 0 \n \t\t(28,000) \n\n \t\tYear 1 \n \t\t18,000 \n\n \t\tYear 2 \n \t\t16,000 \n\nUsing the Table in Book 11, what is the project's net present value using a discount rate of 10%?  \n\nIf it is negative, put a minus sign before the number you enter.  \n\n£",
 233              'rightanswer' => '1586',
 234              'responsesummary' => '17520',
 235              'timemodified' => 1178567143,
 236              'steps' => array(
 237                  0 => (object) array(
 238                      'sequencenumber' => 0,
 239                      'state' => 'todo',
 240                      'fraction' => null,
 241                      'timecreated' => 1178564323,
 242                      'userid' => 405,
 243                      'data' => array('_separators' => '.$,'),
 244                  ),
 245                  1 => (object) array(
 246                      'sequencenumber' => 1,
 247                      'state' => 'complete',
 248                      'fraction' => null,
 249                      'timecreated' => 1178567143,
 250                      'userid' => 405,
 251                      'data' => array('answer' => 17520),
 252                  ),
 253                  2 => (object) array(
 254                      'sequencenumber' => 2,
 255                      'state' => 'gradedwrong',
 256                      'fraction' => 0,
 257                      'timecreated' => 1178567143,
 258                      'userid' => 405,
 259                      'data' => array('answer' => 17520, '-finish' => 1),
 260                  ),
 261              ),
 262          );
 263  
 264          $this->compare_qas($expectedqa, $qa);
 265      }
 266  
 267      public function test_numerical_deferredfeedback_required_units() {
 268          $quiz = (object) array(
 269              'id' => '2',
 270              'course' => '2',
 271              'name' => 'Numerical quiz',
 272              'intro' => '',
 273              'introformat' => '1',
 274              'timeopen' => '0',
 275              'timeclose' => '0',
 276              'attempts' => '0',
 277              'attemptonlast' => '0',
 278              'grademethod' => '1',
 279              'decimalpoints' => '2',
 280              'questiondecimalpoints' => '-1',
 281              'review' => '4459503',
 282              'questionsperpage' => '1',
 283              'shufflequestions' => '0',
 284              'shuffleanswers' => '1',
 285              'sumgrades' => '5.00000',
 286              'grade' => '10.00000',
 287              'timecreated' => '0',
 288              'timemodified' => '1305273177',
 289              'timelimit' => '0',
 290              'password' => '',
 291              'subnet' => '',
 292              'popup' => '0',
 293              'delay1' => '0',
 294              'delay2' => '0',
 295              'showuserpicture' => '0',
 296              'showblocks' => '0',
 297              'preferredbehaviour' => 'deferredfeedback',
 298          );
 299          $attempt = (object) array(
 300              'id' => '6',
 301              'uniqueid' => '6',
 302              'quiz' => '2',
 303              'userid' => '2',
 304              'attempt' => '1',
 305              'sumgrades' => '4.00000',
 306              'timestart' => '1305273566',
 307              'timefinish' => '1305273600',
 308              'timemodified' => '1305273600',
 309              'layout' => '6,12,13,15,14,0',
 310              'preview' => '1',
 311          );
 312          $question = (object) array(
 313              'id' => '15',
 314              'category' => '2',
 315              'parent' => '0',
 316              'name' => 'Required units',
 317              'questiontext' => '<p>What is twice 1.5 m?</p>',
 318              'questiontextformat' => '1',
 319              'generalfeedback' => '',
 320              'generalfeedbackformat' => '1',
 321              'penalty' => '0.1000000',
 322              'qtype' => 'numerical',
 323              'length' => '1',
 324              'stamp' => 'tjh238.vledev2.open.ac.uk+110513075857+rrvakr',
 325              'version' => 'tjh238.vledev2.open.ac.uk+110513075857+PH0loi',
 326              'hidden' => '0',
 327              'timecreated' => '1305273537',
 328              'timemodified' => '1305273537',
 329              'createdby' => '2',
 330              'modifiedby' => '2',
 331              'maxmark' => '1.0000000',
 332              'options' => (object) array(
 333                  'answers' => array(
 334                      22 => (object) array(
 335                          'id' => '22',
 336                          'question' => '15',
 337                          'answer' => '3',
 338                          'answerformat' => '0',
 339                          'fraction' => '1.0000000',
 340                          'feedback' => '',
 341                          'feedbackformat' => '1',
 342                          'tolerance' => '',
 343                      ),
 344                  ),
 345                  'units' => array(
 346                      0 => (object) array(
 347                          'id' => '7',
 348                          'question' => '15',
 349                          'multiplier' => 1,
 350                          'unit' => 'm',
 351                      ),
 352                      1 => (object) array(
 353                          'id' => '8',
 354                          'question' => '15',
 355                          'multiplier' => 100,
 356                          'unit' => 'cm',
 357                      ),
 358                  ),
 359                  'unitgradingtype' => '2',
 360                  'unitpenalty' => '0.5000000',
 361                  'showunits' => '0',
 362                  'unitsleft' => '0',
 363                  'instructions' => '<p>Write an answer like 3 m.</p>',
 364                  'instructionsformat' => '1',
 365              ),
 366              'defaultmark' => '1.0000000',
 367          );
 368          $qsession = (object) array(
 369              'id' => '49',
 370              'attemptid' => '6',
 371              'questionid' => '15',
 372              'newest' => '139',
 373              'newgraded' => '139',
 374              'sumpenalty' => '0.1000000',
 375              'manualcomment' => '',
 376              'manualcommentformat' => '1',
 377              'flagged' => '0',
 378          );
 379          $qstates = array(
 380              129 => (object) array(
 381                  'id' => '129',
 382                  'attempt' => '6',
 383                  'question' => '15',
 384                  'seq_number' => '0',
 385                  'answer' => '|||||',
 386                  'timestamp' => '1305273566',
 387                  'event' => '0',
 388                  'grade' => '0.0000000',
 389                  'raw_grade' => '0.0000000',
 390                  'penalty' => '0.0000000',
 391              ),
 392              134 => (object) array(
 393                  'id' => '134',
 394                  'attempt' => '6',
 395                  'question' => '15',
 396                  'seq_number' => '1',
 397                  'answer' => '3|||||m',
 398                  'timestamp' => '1305273595',
 399                  'event' => '2',
 400                  'grade' => '0.0000000',
 401                  'raw_grade' => '1.0000000',
 402                  'penalty' => '0.1000000',
 403              ),
 404              139 => (object) array(
 405                  'id' => '139',
 406                  'attempt' => '6',
 407                  'question' => '15',
 408                  'seq_number' => '2',
 409                  'answer' => '3|||||m',
 410                  'timestamp' => '1305273595',
 411                  'event' => '6',
 412                  'grade' => '1.0000000',
 413                  'raw_grade' => '1.0000000',
 414                  'penalty' => '0.1000000',
 415              ),
 416          );
 417  
 418          $qa = $this->updater->convert_question_attempt($quiz, $attempt, $question, $qsession, $qstates);
 419  
 420          $expectedqa = (object) array(
 421              'behaviour' => 'deferredfeedback',
 422              'questionid' => 15,
 423              'variant' => 1,
 424              'maxmark' => 1.0000000,
 425              'minfraction' => 0,
 426              'maxfraction' => 1,
 427              'flagged' => 0,
 428              'questionsummary' => 'What is twice 1.5 m?',
 429              'rightanswer' => '3 m',
 430              'responsesummary' => '3 m',
 431              'timemodified' => 1305273595,
 432              'steps' => array(
 433                  0 => (object) array(
 434                      'sequencenumber' => 0,
 435                      'state' => 'todo',
 436                      'fraction' => null,
 437                      'timecreated' => 1305273566,
 438                      'userid' => 2,
 439                      'data' => array('_separators' => '.$,'),
 440                  ),
 441                  1 => (object) array(
 442                      'sequencenumber' => 1,
 443                      'state' => 'complete',
 444                      'fraction' => null,
 445                      'timecreated' => 1305273595,
 446                      'userid' => 2,
 447                      'data' => array('answer' => '3 m'),
 448                  ),
 449                  2 => (object) array(
 450                      'sequencenumber' => 2,
 451                      'state' => 'gradedright',
 452                      'fraction' => 1,
 453                      'timecreated' => 1305273595,
 454                      'userid' => 2,
 455                      'data' => array('answer' => '3 m', '-finish' => '1'),
 456                  ),
 457              ),
 458          );
 459  
 460          $this->compare_qas($expectedqa, $qa);
 461      }
 462  }