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   * randomsamatch questions.
  20   *
  21   * @package    qtype_randomsamatch
  22   * @copyright  2013 Jean-Michel Vedrine
  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/engine/upgrade/tests/helper.php');
  31  
  32  
  33  /**
  34   * Testing the upgrade of randomsamatch question attempts.
  35   *
  36   * @copyright  2013 Jean-Michel Vedrine
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class qtype_randomsamatch_attempt_upgrader_test extends question_attempt_upgrader_test_base {
  40      public function test_randomsamatch_deferredfeedback_qsession1() {
  41          $quiz = (object) array(
  42              'id' => '1',
  43              'course' => '2',
  44              'name' => 'random short answer matching deferred quiz',
  45              'intro' => '<p>To test random shortanswer matching questions.</p>',
  46              'introformat' => '1',
  47              'timeopen' => '0',
  48              'timeclose' => '0',
  49              'attempts' => '0',
  50              'attemptonlast' => '0',
  51              'grademethod' => '1',
  52              'decimalpoints' => '2',
  53              'questiondecimalpoints' => '-1',
  54              'review' => '4459503',
  55              'questionsperpage' => '1',
  56              'shufflequestions' => '0',
  57              'shuffleanswers' => '1',
  58              'questions' => '5,0',
  59              'sumgrades' => '1.00000',
  60              'grade' => '100.00000',
  61              'timecreated' => '0',
  62              'timemodified' => '1368446711',
  63              'timelimit' => '0',
  64              'password' => '',
  65              'subnet' => '',
  66              'popup' => '0',
  67              'delay1' => '0',
  68              'delay2' => '0',
  69              'showuserpicture' => '0',
  70              'showblocks' => '0',
  71              'preferredbehaviour' => 'deferredfeedback',
  72          );
  73          $attempt = (object) array(
  74              'id' => '1',
  75              'uniqueid' => '1',
  76              'quiz' => '1',
  77              'userid' => '3',
  78              'attempt' => '1',
  79              'sumgrades' => '0.66667',
  80              'timestart' => '1368446755',
  81              'timefinish' => '1368446789',
  82              'timemodified' => '1368446789',
  83              'layout' => '5,0',
  84              'preview' => '0',
  85              'needsupgradetonewqe' => 1,
  86          );
  87          $question = (object) array(
  88              'id' => '5',
  89              'category' => '1',
  90              'parent' => '0',
  91              'name' => 'Random shortanswer matching question animals',
  92              'questiontext' => 'For each of the following questions, select the matching answer from the menu.',
  93              'questiontextformat' => '1',
  94              'generalfeedback' => '',
  95              'generalfeedbackformat' => '1',
  96              'penalty' => '0.1000000',
  97              'qtype' => 'randomsamatch',
  98              'length' => '1',
  99              'stamp' => 'localhost+130513115611+72Efbk',
 100              'version' => 'localhost+130513115611+0REXHW',
 101              'hidden' => '0',
 102              'timecreated' => '1368446171',
 103              'timemodified' => '1368446171',
 104              'createdby' => '2',
 105              'modifiedby' => '2',
 106              'maxmark' => '1.0000000',
 107              'options' => (object) array(
 108                  'id' => '1',
 109                  'question' => '5',
 110                  'choose' => '3',
 111                  'subcats' => 1,
 112              ),
 113              'defaultmark' => '1.0000000',
 114          );
 115          $qsession = (object) array(
 116              'id' => '1',
 117              'attemptid' => '1',
 118              'questionid' => '5',
 119              'newest' => '3',
 120              'newgraded' => '3',
 121              'sumpenalty' => '0.1000000',
 122              'manualcomment' => '',
 123              'manualcommentformat' => '1',
 124              'flagged' => '0',
 125          );
 126          $qstates = array(
 127              1 => (object) array(
 128                  'id' => '1',
 129                  'attempt' => '1',
 130                  'question' => '5',
 131                  'seq_number' => '0',
 132                  'answer' => '2-0,3-0,6-0',
 133                  'timestamp' => '1368446755',
 134                  'event' => '0',
 135                  'grade' => '0.0000000',
 136                  'raw_grade' => '0.0000000',
 137                  'penalty' => '0.0000000',
 138              ),
 139              2 => (object) array(
 140                  'id' => '2',
 141                  'attempt' => '1',
 142                  'question' => '5',
 143                  'seq_number' => '1',
 144                  'answer' => '2-3,3-5,6-3',
 145                  'timestamp' => '1368446783',
 146                  'event' => '2',
 147                  'grade' => '0.0000000',
 148                  'raw_grade' => '0.6666667',
 149                  'penalty' => '0.1000000',
 150              ),
 151              3 => (object) array(
 152                  'id' => '3',
 153                  'attempt' => '1',
 154                  'question' => '5',
 155                  'seq_number' => '2',
 156                  'answer' => '2-3,3-5,6-3',
 157                  'timestamp' => '1368446783',
 158                  'event' => '6',
 159                  'grade' => '0.6666667',
 160                  'raw_grade' => '0.6666667',
 161                  'penalty' => '0.1000000',
 162              ),
 163          );
 164          $sa1 = (object) array(
 165              'id' => '2',
 166              'category' => '1',
 167              'parent' => '0',
 168              'name' => 'animal 1',
 169              'questiontext' => 'Dog',
 170              'questiontextformat' => '1',
 171              'defaultmark' => '1',
 172              'penalty' => '0.1',
 173              'qtype' => 'shortanswer',
 174              'length' => '1',
 175              'stamp' => 'localhost+090227173002+mbdE0X',
 176              'version' => 'localhost+090304190917+xAB5Nf',
 177              'hidden' => '0',
 178              'generalfeedback' => '',
 179              'generalfeedbackformat' => '1',
 180              'timecreated' => '1235755802',
 181              'timemodified' => '1236193757',
 182              'createdby' => '25299',
 183              'modifiedby' => '25299',
 184              'unlimited' => '0',
 185              'options' => (object) array(
 186                  'id' => '15211',
 187                  'question' => '2',
 188                  'layout' => '0',
 189                  'answers' => array(
 190                      7 => (object) array(
 191                          'question' => '2',
 192                          'answer' => 'Amphibian',
 193                          'fraction' => '0',
 194                          'feedback' => '',
 195                          'id' => 7,
 196                      ),
 197                      3 => (object) array(
 198                          'question' => '2',
 199                          'answer' => 'Mammal',
 200                          'fraction' => '1',
 201                          'feedback' => '',
 202                          'id' => 3,
 203                      ),
 204                      22 => (object) array(
 205                          'question' => '2',
 206                          'answer' => '*',
 207                          'fraction' => '0',
 208                          'feedback' => '',
 209                          'id' => 22,
 210                      ),
 211                  ),
 212                  'single' => '1',
 213                  'shuffleanswers' => '1',
 214                  'correctfeedback' => 'Your answer is correct. Well done.',
 215                  'partiallycorrectfeedback' => '',
 216                  'incorrectfeedback' => 'Your answer is incorrect. The correct answer is: Mammal.',
 217                  'answernumbering' => 'abc',
 218              ),
 219          );
 220  
 221          $sa2 = (object) array(
 222              'id' => '3',
 223              'category' => '1',
 224              'parent' => '0',
 225              'name' => 'animal 2',
 226              'questiontext' => 'Frog',
 227              'questiontextformat' => '1',
 228              'defaultmark' => '1',
 229              'penalty' => '0.1',
 230              'qtype' => 'shortanswer',
 231              'length' => '1',
 232              'stamp' => 'localhost+090227173002+mbdE0X',
 233              'version' => 'localhost+090304190917+xAB5Nf',
 234              'hidden' => '0',
 235              'generalfeedback' => '',
 236              'generalfeedbackformat' => '1',
 237              'timecreated' => '1235755802',
 238              'timemodified' => '1236193757',
 239              'createdby' => '25299',
 240              'modifiedby' => '25299',
 241              'unlimited' => '0',
 242              'options' => (object) array(
 243                  'id' => '15214',
 244                  'question' => '3',
 245                  'layout' => '0',
 246                  'answers' => array(
 247                      5 => (object) array(
 248                          'question' => '3',
 249                          'answer' => 'Amphibian',
 250                          'fraction' => '1',
 251                          'feedback' => '',
 252                          'id' => 5,
 253                      ),
 254                      11 => (object) array(
 255                          'question' => '3',
 256                          'answer' => 'Mammal',
 257                          'fraction' => '0',
 258                          'feedback' => '',
 259                          'id' => 11,
 260                      ),
 261                      27 => (object) array(
 262                          'question' => '3',
 263                          'answer' => '*',
 264                          'fraction' => '0',
 265                          'feedback' => '',
 266                          'id' => 27,
 267                      ),
 268                  ),
 269                  'single' => '1',
 270                  'shuffleanswers' => '1',
 271                  'correctfeedback' => 'Your answer is correct. Well done.',
 272                  'partiallycorrectfeedback' => '',
 273                  'incorrectfeedback' => 'Your answer is incorrect. The correct answer is: Mammal.',
 274                  'answernumbering' => 'abc',
 275              ),
 276          );
 277  
 278          $sa3 = (object) array(
 279              'id' => '6',
 280              'category' => '1',
 281              'parent' => '0',
 282              'name' => 'animal 3',
 283              'questiontext' => 'Toad',
 284              'questiontextformat' => '1',
 285              'defaultmark' => '1',
 286              'penalty' => '0.1',
 287              'qtype' => 'shortanswer',
 288              'length' => '1',
 289              'stamp' => 'localhost+090227173002+mbdE0X',
 290              'version' => 'localhost+090304190917+xAB5Nf',
 291              'hidden' => '0',
 292              'generalfeedback' => '',
 293              'generalfeedbackformat' => '1',
 294              'timecreated' => '1235755802',
 295              'timemodified' => '1236193757',
 296              'createdby' => '25299',
 297              'modifiedby' => '25299',
 298              'unlimited' => '0',
 299              'options' => (object) array(
 300                  'id' => '4578',
 301                  'question' => '6',
 302                  'layout' => '0',
 303                  'answers' => array(
 304                      9 => (object) array(
 305                          'question' => '6',
 306                          'answer' => 'Amphibian',
 307                          'fraction' => '1',
 308                          'feedback' => '',
 309                          'id' => 9,
 310                      ),
 311                      18 => (object) array(
 312                          'question' => '6',
 313                          'answer' => 'Mammal',
 314                          'fraction' => '0',
 315                          'feedback' => '',
 316                          'id' => 18,
 317                      ),
 318                      32 => (object) array(
 319                          'question' => '6',
 320                          'answer' => '*',
 321                          'fraction' => '0',
 322                          'feedback' => '',
 323                          'id' => 32,
 324                      ),
 325                  ),
 326                  'single' => '1',
 327                  'shuffleanswers' => '1',
 328                  'correctfeedback' => 'Your answer is correct. Well done.',
 329                  'partiallycorrectfeedback' => '',
 330                  'incorrectfeedback' => 'Your answer is incorrect. The correct answer is: Mammal.',
 331                  'answernumbering' => 'abc',
 332              ),
 333          );
 334  
 335          $this->loader->put_question_in_cache($sa2);
 336          $this->loader->put_question_in_cache($sa1);
 337          $this->loader->put_question_in_cache($sa3);
 338          $qa = $this->updater->convert_question_attempt($quiz, $attempt, $question, $qsession, $qstates);
 339  
 340          $expectedqa = (object) array(
 341              'behaviour' => 'deferredfeedback',
 342              'questionid' => 5,
 343              'variant' => 1,
 344              'maxmark' => 1.0000000,
 345              'minfraction' => 0,
 346              'maxfraction' => 1,
 347              'flagged' => 0,
 348              'questionsummary' => 'For each of the following questions, select the matching answer from the menu.{Dog;Frog;Toad}->{Mammal;Amphibian}',
 349              'rightanswer' => 'Dog -> Mammal; Frog -> Amphibian; Toad -> Amphibian',
 350              'responsesummary' => 'Dog->Mammal;Frog->Amphibian;Toad->Mammal',
 351              'timemodified' => 1368446783,
 352              'steps' => array(
 353                  0 => (object) array(
 354                      'sequencenumber' => 0,
 355                      'state' => 'todo',
 356                      'fraction' => null,
 357                      'timecreated' => 1368446755,
 358                      'userid' => 3,
 359                      'data' => array(
 360                          '_choice_3' => 'Mammal',
 361                          '_stem_2' => 'Dog',
 362                          '_stemformat_2' => '1',
 363                          '_right_2' => 3,
 364                          '_choice_5' => 'Amphibian',
 365                          '_stem_3' => 'Frog',
 366                          '_stemformat_3' => '1',
 367                          '_right_3' => 5,
 368                          '_stem_6' => 'Toad',
 369                          '_stemformat_6' => '1',
 370                          '_right_6' => 5,
 371                          '_stemorder' => '2,3,6',
 372                          '_choiceorder' => '3,5',
 373                      ),
 374                  ),
 375                  1 => (object) array(
 376                      'sequencenumber' => 1,
 377                      'state' => 'complete',
 378                      'fraction' => null,
 379                      'timecreated' => 1368446783,
 380                      'userid' => 3,
 381                      'data' => array(
 382                          'sub0' => 1,
 383                          'sub1' => 2,
 384                          'sub2' => 1,
 385                      ),
 386                  ),
 387                  2 => (object) array(
 388                      'sequencenumber' => 2,
 389                      'state' => 'gradedpartial',
 390                      'fraction' => 0.6666667,
 391                      'timecreated' => 1368446783,
 392                      'userid' => 3,
 393                      'data' => array(
 394                          'sub0' => 1,
 395                          'sub1' => 2,
 396                          'sub2' => 1,
 397                          '-finish' => 1,
 398  
 399                      ),
 400                  ),
 401              ),
 402          );
 403  
 404          $this->compare_qas($expectedqa, $qa);
 405      }
 406  }