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