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.
   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  namespace qtype_calculatedsimple;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  require_once($CFG->dirroot . '/question/engine/upgrade/tests/helper.php');
  23  
  24  
  25  /**
  26   * Testing the upgrade of simple calculated question attempts.
  27   *
  28   * @package    qtype_calculatedsimple
  29   * @copyright  2011 The Open University
  30   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class upgrade_old_attempt_data_test extends \question_attempt_upgrader_test_base {
  33      public function test_calculatedsimple_adaptive_qsession95() {
  34          $quiz = (object) array(
  35              'id' => '4',
  36              'course' => '2',
  37              'name' => 'Calculated quiz',
  38              'intro' => '',
  39              'introformat' => '1',
  40              'timeopen' => '0',
  41              'timeclose' => '0',
  42              'attempts' => '0',
  43              'attemptonlast' => '0',
  44              'grademethod' => '1',
  45              'decimalpoints' => '2',
  46              'questiondecimalpoints' => '-1',
  47              'questionsperpage' => '1',
  48              'shufflequestions' => '0',
  49              'shuffleanswers' => '1',
  50              'sumgrades' => '3.00000',
  51              'grade' => '10.00000',
  52              'timecreated' => '0',
  53              'timemodified' => '1305648351',
  54              'timelimit' => '0',
  55              'password' => '',
  56              'subnet' => '',
  57              'popup' => '0',
  58              'delay1' => '0',
  59              'delay2' => '0',
  60              'showuserpicture' => '0',
  61              'showblocks' => '0',
  62              'preferredbehaviour' => 'adaptive',
  63              'reviewattempt' => '69888',
  64              'reviewcorrectness' => '69888',
  65              'reviewmarks' => '69888',
  66              'reviewspecificfeedback' => '69888',
  67              'reviewgeneralfeedback' => '69888',
  68              'reviewrightanswer' => '69888',
  69              'reviewoverallfeedback' => '4352',
  70          );
  71          $attempt = (object) array(
  72              'id' => '13',
  73              'uniqueid' => '13',
  74              'quiz' => '4',
  75              'userid' => '4',
  76              'attempt' => '1',
  77              'sumgrades' => '0.00000',
  78              'timestart' => '1305830650',
  79              'timefinish' => '1305830656',
  80              'timemodified' => '1305830656',
  81              'layout' => '16,0,17,0,18,0',
  82              'preview' => '0',
  83          );
  84          $question = (object) array(
  85              'id' => '16',
  86              'category' => '2',
  87              'parent' => '0',
  88              'name' => 'Calculated simple',
  89              'questiontext' => '<p>What is {={a}} + {={b}} ?</p>',
  90              'questiontextformat' => '1',
  91              'generalfeedback' => '',
  92              'generalfeedbackformat' => '1',
  93              'defaultmark' => '1.0000000',
  94              'penalty' => '0.1',
  95              'qtype' => 'calculatedsimple',
  96              'length' => '1',
  97              'stamp' => 'tjh238.vledev2.open.ac.uk+110517161007+2Barhu',
  98              'version' => 'tjh238.vledev2.open.ac.uk+110517161008+Mu6OQu',
  99              'hidden' => '0',
 100              'timecreated' => '1305648607',
 101              'timemodified' => '1305648607',
 102              'createdby' => '2',
 103              'modifiedby' => '2',
 104              'maxmark' => '1.0000000',
 105              'options' => (object) array(
 106                  'synchronize' => 0,
 107                  'single' => 0,
 108                  'answernumbering' => 'abc',
 109                  'shuffleanswers' => 0,
 110                  'correctfeedback' => '',
 111                  'partiallycorrectfeedback' => '',
 112                  'incorrectfeedback' => '',
 113                  'correctfeedbackformat' => 0,
 114                  'partiallycorrectfeedbackformat' => 0,
 115                  'incorrectfeedbackformat' => 0,
 116                  'answers' => array(
 117                      23 => (object) array(
 118                          'id' => '23',
 119                          'question' => '16',
 120                          'answer' => '{a} + {b}',
 121                          'answerformat' => '0',
 122                          'fraction' => '1.0000000',
 123                          'feedback' => '<p>Well done!</p>',
 124                          'feedbackformat' => '1',
 125                          'tolerance' => '0.01',
 126                          'tolerancetype' => '1',
 127                          'correctanswerlength' => '2',
 128                          'correctanswerformat' => '1',
 129                      ),
 130                  ),
 131                  'units' => array(
 132                  ),
 133                  'unitgradingtype' => '0',
 134                  'unitpenalty' => '0.1000000',
 135                  'showunits' => '3',
 136                  'unitsleft' => '0',
 137              ),
 138              'hints' => array(
 139              ),
 140          );
 141          $qsession = (object) array(
 142              'id' => '95',
 143              'attemptid' => '13',
 144              'questionid' => '16',
 145              'newest' => '256',
 146              'newgraded' => '256',
 147              'sumpenalty' => '0.1000000',
 148              'manualcomment' => '',
 149              'manualcommentformat' => '1',
 150              'flagged' => '0',
 151          );
 152          $qstates = array(
 153              253 => (object) array(
 154                  'id' => '253',
 155                  'attempt' => '13',
 156                  'question' => '16',
 157                  'seq_number' => '0',
 158                  'answer' => 'dataset7-|||||',
 159                  'timestamp' => '1305830650',
 160                  'event' => '0',
 161                  'grade' => '0.0000000',
 162                  'raw_grade' => '0.0000000',
 163                  'penalty' => '0.0000000',
 164              ),
 165              256 => (object) array(
 166                  'id' => '256',
 167                  'attempt' => '13',
 168                  'question' => '16',
 169                  'seq_number' => '1',
 170                  'answer' => 'dataset7-|||||',
 171                  'timestamp' => '1305830650',
 172                  'event' => '6',
 173                  'grade' => '0.0000000',
 174                  'raw_grade' => '0.0000000',
 175                  'penalty' => '0.1000000',
 176              ),
 177          );
 178          $this->loader->put_dataset_in_cache($question->id, 7, array('a' => '3', 'b' => '6'));
 179  
 180          $qa = $this->updater->convert_question_attempt($quiz, $attempt, $question, $qsession, $qstates);
 181  
 182          $expectedqa = (object) array(
 183              'behaviour' => 'adaptive',
 184              'questionid' => 16,
 185              'variant' => 7,
 186              'maxmark' => 1.0000000,
 187              'minfraction' => 0,
 188              'maxfraction' => 1,
 189              'flagged' => 0,
 190              'questionsummary' => 'What is 3 + 6 ?',
 191              'rightanswer' => '9',
 192              'responsesummary' => '',
 193              'timemodified' => 1305830650,
 194              'steps' => array(
 195                  0 => (object) array(
 196                      'sequencenumber' => 0,
 197                      'state' => 'todo',
 198                      'fraction' => null,
 199                      'timecreated' => 1305830650,
 200                      'userid' => 4,
 201                      'data' => array('_separators' => '.$,',
 202                              '_var_a' => '3', '_var_b' => '6'),
 203                  ),
 204                  1 => (object) array(
 205                      'sequencenumber' => 1,
 206                      'state' => 'gradedwrong',
 207                      'fraction' => 0,
 208                      'timecreated' => 1305830650,
 209                      'userid' => 4,
 210                      'data' => array('answer' => '', '-finish' => 1, '-_try' => 1, '-_rawfraction' => 0),
 211                  ),
 212              ),
 213          );
 214  
 215          $this->compare_qas($expectedqa, $qa);
 216      }
 217  
 218      public function test_calculatedsimple_adaptive_qsession98() {
 219          $quiz = (object) array(
 220              'id' => '4',
 221              'course' => '2',
 222              'name' => 'Calculated quiz',
 223              'intro' => '',
 224              'introformat' => '1',
 225              'timeopen' => '0',
 226              'timeclose' => '0',
 227              'attempts' => '0',
 228              'attemptonlast' => '0',
 229              'grademethod' => '1',
 230              'decimalpoints' => '2',
 231              'questiondecimalpoints' => '-1',
 232              'questionsperpage' => '1',
 233              'shufflequestions' => '0',
 234              'shuffleanswers' => '1',
 235              'sumgrades' => '3.00000',
 236              'grade' => '10.00000',
 237              'timecreated' => '0',
 238              'timemodified' => '1305648351',
 239              'timelimit' => '0',
 240              'password' => '',
 241              'subnet' => '',
 242              'popup' => '0',
 243              'delay1' => '0',
 244              'delay2' => '0',
 245              'showuserpicture' => '0',
 246              'showblocks' => '0',
 247              'preferredbehaviour' => 'adaptive',
 248              'reviewattempt' => '69888',
 249              'reviewcorrectness' => '69888',
 250              'reviewmarks' => '69888',
 251              'reviewspecificfeedback' => '69888',
 252              'reviewgeneralfeedback' => '69888',
 253              'reviewrightanswer' => '69888',
 254              'reviewoverallfeedback' => '4352',
 255          );
 256          $attempt = (object) array(
 257              'id' => '14',
 258              'uniqueid' => '14',
 259              'quiz' => '4',
 260              'userid' => '4',
 261              'attempt' => '2',
 262              'sumgrades' => '2.80000',
 263              'timestart' => '1305830661',
 264              'timefinish' => '1305830729',
 265              'timemodified' => '1305830729',
 266              'layout' => '16,0,17,0,18,0',
 267              'preview' => '0',
 268          );
 269          $question = (object) array(
 270              'id' => '16',
 271              'category' => '2',
 272              'parent' => '0',
 273              'name' => 'Calculated simple',
 274              'questiontext' => '<p>What is {={a}} + {={b}} ?</p>',
 275              'questiontextformat' => '1',
 276              'generalfeedback' => '',
 277              'generalfeedbackformat' => '1',
 278              'defaultmark' => '1.0000000',
 279              'penalty' => '0.1',
 280              'qtype' => 'calculatedsimple',
 281              'length' => '1',
 282              'stamp' => 'tjh238.vledev2.open.ac.uk+110517161007+2Barhu',
 283              'version' => 'tjh238.vledev2.open.ac.uk+110517161008+Mu6OQu',
 284              'hidden' => '0',
 285              'timecreated' => '1305648607',
 286              'timemodified' => '1305648607',
 287              'createdby' => '2',
 288              'modifiedby' => '2',
 289              'maxmark' => '1.0000000',
 290              'options' => (object) array(
 291                  'synchronize' => 0,
 292                  'single' => 0,
 293                  'answernumbering' => 'abc',
 294                  'shuffleanswers' => 0,
 295                  'correctfeedback' => '',
 296                  'partiallycorrectfeedback' => '',
 297                  'incorrectfeedback' => '',
 298                  'correctfeedbackformat' => 0,
 299                  'partiallycorrectfeedbackformat' => 0,
 300                  'incorrectfeedbackformat' => 0,
 301                  'answers' => array(
 302                      23 => (object) array(
 303                          'id' => '23',
 304                          'question' => '16',
 305                          'answer' => '{a} + {b}',
 306                          'answerformat' => '0',
 307                          'fraction' => '1.0000000',
 308                          'feedback' => '<p>Well done!</p>',
 309                          'feedbackformat' => '1',
 310                          'tolerance' => '0.01',
 311                          'tolerancetype' => '1',
 312                          'correctanswerlength' => '2',
 313                          'correctanswerformat' => '1',
 314                      ),
 315                  ),
 316                  'units' => array(
 317                  ),
 318                  'unitgradingtype' => '0',
 319                  'unitpenalty' => '0.1000000',
 320                  'showunits' => '3',
 321                  'unitsleft' => '0',
 322              ),
 323              'hints' => array(
 324              ),
 325          );
 326          $qsession = (object) array(
 327              'id' => '98',
 328              'attemptid' => '14',
 329              'questionid' => '16',
 330              'newest' => '267',
 331              'newgraded' => '267',
 332              'sumpenalty' => '0.3000000',
 333              'manualcomment' => '',
 334              'manualcommentformat' => '1',
 335              'flagged' => '0',
 336          );
 337          $qstates = array(
 338              259 => (object) array(
 339                  'id' => '259',
 340                  'attempt' => '14',
 341                  'question' => '16',
 342                  'seq_number' => '0',
 343                  'answer' => 'dataset4-|||||',
 344                  'timestamp' => '1305830661',
 345                  'event' => '0',
 346                  'grade' => '0.0000000',
 347                  'raw_grade' => '0.0000000',
 348                  'penalty' => '0.0000000',
 349              ),
 350              262 => (object) array(
 351                  'id' => '262',
 352                  'attempt' => '14',
 353                  'question' => '16',
 354                  'seq_number' => '1',
 355                  'answer' => 'dataset4-9.00|||||',
 356                  'timestamp' => '1305830668',
 357                  'event' => '3',
 358                  'grade' => '0.0000000',
 359                  'raw_grade' => '0.0000000',
 360                  'penalty' => '0.1000000',
 361              ),
 362              263 => (object) array(
 363                  'id' => '263',
 364                  'attempt' => '14',
 365                  'question' => '16',
 366                  'seq_number' => '2',
 367                  'answer' => 'dataset4-15.40|||||',
 368                  'timestamp' => '1305830679',
 369                  'event' => '3',
 370                  'grade' => '0.9000000',
 371                  'raw_grade' => '1.0000000',
 372                  'penalty' => '0.1000000',
 373              ),
 374              267 => (object) array(
 375                  'id' => '267',
 376                  'attempt' => '14',
 377                  'question' => '16',
 378                  'seq_number' => '3',
 379                  'answer' => 'dataset4-15.40|||||',
 380                  'timestamp' => '1305830679',
 381                  'event' => '6',
 382                  'grade' => '0.9000000',
 383                  'raw_grade' => '1.0000000',
 384                  'penalty' => '0.1000000',
 385              ),
 386          );
 387          $this->loader->put_dataset_in_cache($question->id, 4, array('a' => '6.4', 'b' => '9'));
 388  
 389          $qa = $this->updater->convert_question_attempt($quiz, $attempt, $question, $qsession, $qstates);
 390  
 391          $expectedqa = (object) array(
 392              'behaviour' => 'adaptive',
 393              'questionid' => 16,
 394              'variant' => 4,
 395              'maxmark' => 1.0000000,
 396              'minfraction' => 0,
 397              'maxfraction' => 1,
 398              'flagged' => 0,
 399              'questionsummary' => 'What is 6.4 + 9 ?',
 400              'rightanswer' => '15.4',
 401              'responsesummary' => '15.40',
 402              'timemodified' => 1305830679,
 403              'steps' => array(
 404                  0 => (object) array(
 405                      'sequencenumber' => 0,
 406                      'state' => 'todo',
 407                      'fraction' => null,
 408                      'timecreated' => 1305830661,
 409                      'userid' => 4,
 410                      'data' => array('_separators' => '.$,',
 411                              '_var_a' => '6.4', '_var_b' => '9'),
 412                  ),
 413                  1 => (object) array(
 414                      'sequencenumber' => 1,
 415                      'state' => 'todo',
 416                      'fraction' => 0,
 417                      'timecreated' => 1305830668,
 418                      'userid' => 4,
 419                      'data' => array('answer' => '9.00', '-submit' => 1, '-_try' => 1, '-_rawfraction' => 0),
 420                  ),
 421                  2 => (object) array(
 422                      'sequencenumber' => 2,
 423                      'state' => 'todo',
 424                      'fraction' => 0.9,
 425                      'timecreated' => 1305830679,
 426                      'userid' => 4,
 427                      'data' => array('answer' => '15.40', '-submit' => 1, '-_try' => 2, '-_rawfraction' => 1),
 428                  ),
 429                  3 => (object) array(
 430                      'sequencenumber' => 3,
 431                      'state' => 'gradedright',
 432                      'fraction' => 0.9,
 433                      'timecreated' => 1305830679,
 434                      'userid' => 4,
 435                      'data' => array('answer' => '15.40', '-finish' => 1, '-_try' => 2, '-_rawfraction' => 1),
 436                  ),
 437              ),
 438          );
 439  
 440          $this->compare_qas($expectedqa, $qa);
 441      }
 442  
 443      public function test_calculatedsimple_adaptive_qsession101() {
 444          $quiz = (object) array(
 445              'id' => '4',
 446              'course' => '2',
 447              'name' => 'Calculated quiz',
 448              'intro' => '',
 449              'introformat' => '1',
 450              'timeopen' => '0',
 451              'timeclose' => '0',
 452              'attempts' => '0',
 453              'attemptonlast' => '0',
 454              'grademethod' => '1',
 455              'decimalpoints' => '2',
 456              'questiondecimalpoints' => '-1',
 457              'questionsperpage' => '1',
 458              'shufflequestions' => '0',
 459              'shuffleanswers' => '1',
 460              'sumgrades' => '3.00000',
 461              'grade' => '10.00000',
 462              'timecreated' => '0',
 463              'timemodified' => '1305648351',
 464              'timelimit' => '0',
 465              'password' => '',
 466              'subnet' => '',
 467              'popup' => '0',
 468              'delay1' => '0',
 469              'delay2' => '0',
 470              'showuserpicture' => '0',
 471              'showblocks' => '0',
 472              'preferredbehaviour' => 'adaptive',
 473              'reviewattempt' => '69888',
 474              'reviewcorrectness' => '69888',
 475              'reviewmarks' => '69888',
 476              'reviewspecificfeedback' => '69888',
 477              'reviewgeneralfeedback' => '69888',
 478              'reviewrightanswer' => '69888',
 479              'reviewoverallfeedback' => '4352',
 480          );
 481          $attempt = (object) array(
 482              'id' => '15',
 483              'uniqueid' => '15',
 484              'quiz' => '4',
 485              'userid' => '3',
 486              'attempt' => '1',
 487              'sumgrades' => '0.70000',
 488              'timestart' => '1305830744',
 489              'timefinish' => '0',
 490              'timemodified' => '1305830792',
 491              'layout' => '16,0,17,0,18,0',
 492              'preview' => '0',
 493          );
 494          $question = (object) array(
 495              'id' => '16',
 496              'category' => '2',
 497              'parent' => '0',
 498              'name' => 'Calculated simple',
 499              'questiontext' => '<p>What is {={a}} + {={b}} ?</p>',
 500              'questiontextformat' => '1',
 501              'generalfeedback' => '',
 502              'generalfeedbackformat' => '1',
 503              'defaultmark' => '1.0000000',
 504              'penalty' => '0.1',
 505              'qtype' => 'calculatedsimple',
 506              'length' => '1',
 507              'stamp' => 'tjh238.vledev2.open.ac.uk+110517161007+2Barhu',
 508              'version' => 'tjh238.vledev2.open.ac.uk+110517161008+Mu6OQu',
 509              'hidden' => '0',
 510              'timecreated' => '1305648607',
 511              'timemodified' => '1305648607',
 512              'createdby' => '2',
 513              'modifiedby' => '2',
 514              'maxmark' => '1.0000000',
 515              'options' => (object) array(
 516                  'synchronize' => 0,
 517                  'single' => 0,
 518                  'answernumbering' => 'abc',
 519                  'shuffleanswers' => 0,
 520                  'correctfeedback' => '',
 521                  'partiallycorrectfeedback' => '',
 522                  'incorrectfeedback' => '',
 523                  'correctfeedbackformat' => 0,
 524                  'partiallycorrectfeedbackformat' => 0,
 525                  'incorrectfeedbackformat' => 0,
 526                  'answers' => array(
 527                      23 => (object) array(
 528                          'id' => '23',
 529                          'question' => '16',
 530                          'answer' => '{a} + {b}',
 531                          'answerformat' => '0',
 532                          'fraction' => '1.0000000',
 533                          'feedback' => '<p>Well done!</p>',
 534                          'feedbackformat' => '1',
 535                          'tolerance' => '0.01',
 536                          'tolerancetype' => '1',
 537                          'correctanswerlength' => '2',
 538                          'correctanswerformat' => '1',
 539                      ),
 540                  ),
 541                  'units' => array(
 542                  ),
 543                  'unitgradingtype' => '0',
 544                  'unitpenalty' => '0.1000000',
 545                  'showunits' => '3',
 546                  'unitsleft' => '0',
 547              ),
 548              'hints' => array(
 549              ),
 550          );
 551          $qsession = (object) array(
 552              'id' => '101',
 553              'attemptid' => '15',
 554              'questionid' => '16',
 555              'newest' => '273',
 556              'newgraded' => '270',
 557              'sumpenalty' => '0.0000000',
 558              'manualcomment' => '',
 559              'manualcommentformat' => '1',
 560              'flagged' => '0',
 561          );
 562          $qstates = array(
 563              270 => (object) array(
 564                  'id' => '270',
 565                  'attempt' => '15',
 566                  'question' => '16',
 567                  'seq_number' => '0',
 568                  'answer' => 'dataset6-|||||',
 569                  'timestamp' => '1305830744',
 570                  'event' => '0',
 571                  'grade' => '0.0000000',
 572                  'raw_grade' => '0.0000000',
 573                  'penalty' => '0.0000000',
 574              ),
 575              273 => (object) array(
 576                  'id' => '273',
 577                  'attempt' => '15',
 578                  'question' => '16',
 579                  'seq_number' => '1',
 580                  'answer' => 'dataset6-13.1|||||',
 581                  'timestamp' => '1305830755',
 582                  'event' => '2',
 583                  'grade' => '0.0000000',
 584                  'raw_grade' => '1.0000000',
 585                  'penalty' => '0.1000000',
 586              ),
 587          );
 588          $this->loader->put_dataset_in_cache($question->id, 6, array('a' => '6.1', 'b' => '7'));
 589  
 590          $qa = $this->updater->convert_question_attempt($quiz, $attempt, $question, $qsession, $qstates);
 591  
 592          $expectedqa = (object) array(
 593              'behaviour' => 'adaptive',
 594              'questionid' => 16,
 595              'variant' => 6,
 596              'maxmark' => 1.0000000,
 597              'minfraction' => 0,
 598              'maxfraction' => 1,
 599              'flagged' => 0,
 600              'questionsummary' => 'What is 6.1 + 7 ?',
 601              'rightanswer' => '13.1',
 602              'responsesummary' => '13.1',
 603              'timemodified' => 1305830755,
 604              'steps' => array(
 605                  0 => (object) array(
 606                      'sequencenumber' => 0,
 607                      'state' => 'todo',
 608                      'fraction' => null,
 609                      'timecreated' => 1305830744,
 610                      'userid' => 3,
 611                      'data' => array('_separators' => '.$,',
 612                              '_var_a' => '6.1', '_var_b' => '7'),
 613                  ),
 614                  1 => (object) array(
 615                      'sequencenumber' => 1,
 616                      'state' => 'complete',
 617                      'fraction' => null,
 618                      'timecreated' => 1305830755,
 619                      'userid' => 3,
 620                      'data' => array('answer' => 13.1),
 621                  ),
 622              ),
 623          );
 624  
 625          $this->compare_qas($expectedqa, $qa);
 626      }
 627  }