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.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   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 mod_quiz;
  18  
  19  use question_bank;
  20  use question_engine;
  21  use quiz;
  22  use quiz_attempt;
  23  
  24  defined('MOODLE_INTERNAL') || die();
  25  
  26  global $CFG;
  27  require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  28  
  29  /**
  30   * Quiz attempt walk through.
  31   *
  32   * @package    mod_quiz
  33   * @category   test
  34   * @copyright  2013 The Open University
  35   * @author     Jamie Pratt <me@jamiep.org>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   * @covers \quiz_attempt
  38   */
  39  class attempt_walkthrough_test extends \advanced_testcase {
  40  
  41      /**
  42       * Create a quiz with questions and walk through a quiz attempt.
  43       */
  44      public function test_quiz_attempt_walkthrough() {
  45          global $SITE;
  46  
  47          $this->resetAfterTest(true);
  48  
  49          // Make a quiz.
  50          $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
  51  
  52          $quiz = $quizgenerator->create_instance(array('course'=>$SITE->id, 'questionsperpage' => 0, 'grade' => 100.0,
  53                                                        'sumgrades' => 2));
  54  
  55          // Create a couple of questions.
  56          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
  57  
  58          $cat = $questiongenerator->create_question_category();
  59          $saq = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id));
  60          $numq = $questiongenerator->create_question('numerical', null, array('category' => $cat->id));
  61  
  62          // Add them to the quiz.
  63          quiz_add_quiz_question($saq->id, $quiz);
  64          quiz_add_quiz_question($numq->id, $quiz);
  65  
  66          // Make a user to do the quiz.
  67          $user1 = $this->getDataGenerator()->create_user();
  68  
  69          $quizobj = quiz::create($quiz->id, $user1->id);
  70  
  71          // Start the attempt.
  72          $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
  73          $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
  74  
  75          $timenow = time();
  76          $attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $user1->id);
  77  
  78          quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
  79          $this->assertEquals('1,2,0', $attempt->layout);
  80  
  81          quiz_attempt_save_started($quizobj, $quba, $attempt);
  82  
  83          // Process some responses from the student.
  84          $attemptobj = quiz_attempt::create($attempt->id);
  85          $this->assertFalse($attemptobj->has_response_to_at_least_one_graded_question());
  86  
  87          $prefix1 = $quba->get_field_prefix(1);
  88          $prefix2 = $quba->get_field_prefix(2);
  89  
  90          $tosubmit = array(1 => array('answer' => 'frog'),
  91                            2 => array('answer' => '3.14'));
  92  
  93          $attemptobj->process_submitted_actions($timenow, false, $tosubmit);
  94  
  95          // Finish the attempt.
  96          $attemptobj = quiz_attempt::create($attempt->id);
  97          $this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question());
  98          $attemptobj->process_finish($timenow, false);
  99  
 100          // Re-load quiz attempt data.
 101          $attemptobj = quiz_attempt::create($attempt->id);
 102  
 103          // Check that results are stored as expected.
 104          $this->assertEquals(1, $attemptobj->get_attempt_number());
 105          $this->assertEquals(2, $attemptobj->get_sum_marks());
 106          $this->assertEquals(true, $attemptobj->is_finished());
 107          $this->assertEquals($timenow, $attemptobj->get_submitted_date());
 108          $this->assertEquals($user1->id, $attemptobj->get_userid());
 109          $this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question());
 110  
 111          // Check quiz grades.
 112          $grades = quiz_get_user_grades($quiz, $user1->id);
 113          $grade = array_shift($grades);
 114          $this->assertEquals(100.0, $grade->rawgrade);
 115  
 116          // Check grade book.
 117          $gradebookgrades = grade_get_grades($SITE->id, 'mod', 'quiz', $quiz->id, $user1->id);
 118          $gradebookitem = array_shift($gradebookgrades->items);
 119          $gradebookgrade = array_shift($gradebookitem->grades);
 120          $this->assertEquals(100, $gradebookgrade->grade);
 121      }
 122  
 123      /**
 124       * Create a quiz containing one question and a close time.
 125       *
 126       * The question is the standard shortanswer test question.
 127       * The quiz is set to close 1 hour from now.
 128       * The quiz is set to use a grade period of 1 hour once time expires.
 129       *
 130       * @return \stdClass the quiz that was created.
 131       */
 132      protected function create_quiz_with_one_question(): \stdClass {
 133          global $SITE;
 134          $this->resetAfterTest();
 135  
 136          // Make a quiz.
 137          $timeclose = time() + HOURSECS;
 138          $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
 139  
 140          $quiz = $quizgenerator->create_instance(
 141                  ['course' => $SITE->id, 'timeclose' => $timeclose,
 142                          'overduehandling' => 'graceperiod', 'graceperiod' => HOURSECS]);
 143  
 144          // Create a question.
 145          /** @var \core_question_generator $questiongenerator */
 146          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
 147          $cat = $questiongenerator->create_question_category();
 148          $saq = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id));
 149  
 150          // Add them to the quiz.
 151          quiz_add_quiz_question($saq->id, $quiz, 0, 1);
 152          quiz_update_sumgrades($quiz);
 153  
 154          return $quiz;
 155      }
 156  
 157      public function test_quiz_attempt_walkthrough_submit_time_recorded_correctly_when_overdue() {
 158  
 159          $quiz = $this->create_quiz_with_one_question();
 160  
 161          // Make a user to do the quiz.
 162          $user = $this->getDataGenerator()->create_user();
 163          $this->setUser($user);
 164          $quizobj = quiz::create($quiz->id, $user->id);
 165  
 166          // Start the attempt.
 167          $attempt = quiz_prepare_and_start_new_attempt($quizobj, 1, null);
 168  
 169          // Process some responses from the student.
 170          $attemptobj = quiz_attempt::create($attempt->id);
 171          $attemptobj->process_submitted_actions($quiz->timeclose - 30 * MINSECS, false, [1 => ['answer' => 'frog']]);
 172  
 173          // Attempt goes overdue (e.g. if cron ran).
 174          $attemptobj = quiz_attempt::create($attempt->id);
 175          $attemptobj->process_going_overdue($quiz->timeclose + 2 * get_config('quiz', 'graceperiodmin'), false);
 176  
 177          // Verify the attempt state.
 178          $attemptobj = quiz_attempt::create($attempt->id);
 179          $this->assertEquals(1, $attemptobj->get_attempt_number());
 180          $this->assertEquals(false, $attemptobj->is_finished());
 181          $this->assertEquals(0, $attemptobj->get_submitted_date());
 182          $this->assertEquals($user->id, $attemptobj->get_userid());
 183          $this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question());
 184  
 185          // Student submits the attempt during the grace period.
 186          $attemptobj = quiz_attempt::create($attempt->id);
 187          $attemptobj->process_attempt($quiz->timeclose + 30 * MINSECS, true, false, 1);
 188  
 189          // Verify the attempt state.
 190          $attemptobj = quiz_attempt::create($attempt->id);
 191          $this->assertEquals(1, $attemptobj->get_attempt_number());
 192          $this->assertEquals(true, $attemptobj->is_finished());
 193          $this->assertEquals($quiz->timeclose + 30 * MINSECS, $attemptobj->get_submitted_date());
 194          $this->assertEquals($user->id, $attemptobj->get_userid());
 195          $this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question());
 196      }
 197  
 198      public function test_quiz_attempt_walkthrough_close_time_extended_at_last_minute() {
 199          global $DB;
 200  
 201          $quiz = $this->create_quiz_with_one_question();
 202          $originaltimeclose = $quiz->timeclose;
 203  
 204          // Make a user to do the quiz.
 205          $user = $this->getDataGenerator()->create_user();
 206          $this->setUser($user);
 207          $quizobj = quiz::create($quiz->id, $user->id);
 208  
 209          // Start the attempt.
 210          $attempt = quiz_prepare_and_start_new_attempt($quizobj, 1, null);
 211  
 212          // Process some responses from the student during the attempt.
 213          $attemptobj = quiz_attempt::create($attempt->id);
 214          $attemptobj->process_submitted_actions($originaltimeclose - 30 * MINSECS, false, [1 => ['answer' => 'frog']]);
 215  
 216          // Teacher edits the quiz to extend the time-limit by one minute.
 217          $DB->set_field('quiz', 'timeclose', $originaltimeclose + MINSECS, ['id' => $quiz->id]);
 218          \course_modinfo::clear_instance_cache($quiz->course);
 219  
 220          // Timer expires in the student browser and thinks it is time to submit the quiz.
 221          // This sets $finishattempt to false - since the student did not click the button, and $timeup to true.
 222          $attemptobj = quiz_attempt::create($attempt->id);
 223          $attemptobj->process_attempt($originaltimeclose, false, true, 1);
 224  
 225          // Verify the attempt state - the $timeup was ignored becuase things have changed server-side.
 226          $attemptobj = quiz_attempt::create($attempt->id);
 227          $this->assertEquals(1, $attemptobj->get_attempt_number());
 228          $this->assertFalse($attemptobj->is_finished());
 229          $this->assertEquals(quiz_attempt::IN_PROGRESS, $attemptobj->get_state());
 230          $this->assertEquals(0, $attemptobj->get_submitted_date());
 231          $this->assertEquals($user->id, $attemptobj->get_userid());
 232      }
 233  
 234      /**
 235       * Create a quiz with a random as well as other questions and walk through quiz attempts.
 236       */
 237      public function test_quiz_with_random_question_attempt_walkthrough() {
 238          global $SITE;
 239  
 240          $this->resetAfterTest(true);
 241          question_bank::get_qtype('random')->clear_caches_before_testing();
 242  
 243          $this->setAdminUser();
 244  
 245          // Make a quiz.
 246          $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
 247  
 248          $quiz = $quizgenerator->create_instance(array('course' => $SITE->id, 'questionsperpage' => 2, 'grade' => 100.0,
 249                                                        'sumgrades' => 4));
 250  
 251          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
 252  
 253          // Add two questions to question category.
 254          $cat = $questiongenerator->create_question_category();
 255          $saq = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id));
 256          $numq = $questiongenerator->create_question('numerical', null, array('category' => $cat->id));
 257  
 258          // Add random question to the quiz.
 259          quiz_add_random_questions($quiz, 0, $cat->id, 1, false);
 260  
 261          // Make another category.
 262          $cat2 = $questiongenerator->create_question_category();
 263          $match = $questiongenerator->create_question('match', null, array('category' => $cat->id));
 264  
 265          quiz_add_quiz_question($match->id, $quiz, 0);
 266  
 267          $multichoicemulti = $questiongenerator->create_question('multichoice', 'two_of_four', array('category' => $cat->id));
 268  
 269          quiz_add_quiz_question($multichoicemulti->id, $quiz, 0);
 270  
 271          $multichoicesingle = $questiongenerator->create_question('multichoice', 'one_of_four', array('category' => $cat->id));
 272  
 273          quiz_add_quiz_question($multichoicesingle->id, $quiz, 0);
 274  
 275          foreach (array($saq->id => 'frog', $numq->id => '3.14') as $randomqidtoselect => $randqanswer) {
 276              // Make a new user to do the quiz each loop.
 277              $user1 = $this->getDataGenerator()->create_user();
 278              $this->setUser($user1);
 279  
 280              $quizobj = quiz::create($quiz->id, $user1->id);
 281  
 282              // Start the attempt.
 283              $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
 284              $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
 285  
 286              $timenow = time();
 287              $attempt = quiz_create_attempt($quizobj, 1, false, $timenow);
 288  
 289              quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow, array(1 => $randomqidtoselect));
 290              $this->assertEquals('1,2,0,3,4,0', $attempt->layout);
 291  
 292              quiz_attempt_save_started($quizobj, $quba, $attempt);
 293  
 294              // Process some responses from the student.
 295              $attemptobj = quiz_attempt::create($attempt->id);
 296              $this->assertFalse($attemptobj->has_response_to_at_least_one_graded_question());
 297  
 298              $tosubmit = array();
 299              $selectedquestionid = $quba->get_question_attempt(1)->get_question_id();
 300              $tosubmit[1] = array('answer' => $randqanswer);
 301              $tosubmit[2] = array(
 302                  'frog' => 'amphibian',
 303                  'cat'  => 'mammal',
 304                  'newt' => 'amphibian');
 305              $tosubmit[3] = array('One' => '1', 'Two' => '0', 'Three' => '1', 'Four' => '0'); // First and third choice.
 306              $tosubmit[4] = array('answer' => 'One'); // The first choice.
 307  
 308              $attemptobj->process_submitted_actions($timenow, false, $tosubmit);
 309  
 310              // Finish the attempt.
 311              $attemptobj = quiz_attempt::create($attempt->id);
 312              $this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question());
 313              $attemptobj->process_finish($timenow, false);
 314  
 315              // Re-load quiz attempt data.
 316              $attemptobj = quiz_attempt::create($attempt->id);
 317  
 318              // Check that results are stored as expected.
 319              $this->assertEquals(1, $attemptobj->get_attempt_number());
 320              $this->assertEquals(4, $attemptobj->get_sum_marks());
 321              $this->assertEquals(true, $attemptobj->is_finished());
 322              $this->assertEquals($timenow, $attemptobj->get_submitted_date());
 323              $this->assertEquals($user1->id, $attemptobj->get_userid());
 324              $this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question());
 325  
 326              // Check quiz grades.
 327              $grades = quiz_get_user_grades($quiz, $user1->id);
 328              $grade = array_shift($grades);
 329              $this->assertEquals(100.0, $grade->rawgrade);
 330  
 331              // Check grade book.
 332              $gradebookgrades = grade_get_grades($SITE->id, 'mod', 'quiz', $quiz->id, $user1->id);
 333              $gradebookitem = array_shift($gradebookgrades->items);
 334              $gradebookgrade = array_shift($gradebookitem->grades);
 335              $this->assertEquals(100, $gradebookgrade->grade);
 336          }
 337      }
 338  
 339  
 340      public function get_correct_response_for_variants() {
 341          return array(array(1, 9.9), array(2, 8.5), array(5, 14.2), array(10, 6.8, true));
 342      }
 343  
 344      protected $quizwithvariants = null;
 345  
 346      /**
 347       * Create a quiz with a single question with variants and walk through quiz attempts.
 348       *
 349       * @dataProvider get_correct_response_for_variants
 350       */
 351      public function test_quiz_with_question_with_variants_attempt_walkthrough($variantno, $correctresponse, $done = false) {
 352          global $SITE;
 353  
 354          $this->resetAfterTest($done);
 355  
 356          $this->setAdminUser();
 357  
 358          if ($this->quizwithvariants === null) {
 359              // Make a quiz.
 360              $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
 361  
 362              $this->quizwithvariants = $quizgenerator->create_instance(array('course'=>$SITE->id,
 363                                                                              'questionsperpage' => 0,
 364                                                                              'grade' => 100.0,
 365                                                                              'sumgrades' => 1));
 366  
 367              $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
 368  
 369              $cat = $questiongenerator->create_question_category();
 370              $calc = $questiongenerator->create_question('calculatedsimple', 'sumwithvariants', array('category' => $cat->id));
 371              quiz_add_quiz_question($calc->id, $this->quizwithvariants, 0);
 372          }
 373  
 374  
 375          // Make a new user to do the quiz.
 376          $user1 = $this->getDataGenerator()->create_user();
 377          $this->setUser($user1);
 378          $quizobj = quiz::create($this->quizwithvariants->id, $user1->id);
 379  
 380          // Start the attempt.
 381          $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
 382          $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
 383  
 384          $timenow = time();
 385          $attempt = quiz_create_attempt($quizobj, 1, false, $timenow);
 386  
 387          // Select variant.
 388          quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow, array(), array(1 => $variantno));
 389          $this->assertEquals('1,0', $attempt->layout);
 390          quiz_attempt_save_started($quizobj, $quba, $attempt);
 391  
 392          // Process some responses from the student.
 393          $attemptobj = quiz_attempt::create($attempt->id);
 394          $this->assertFalse($attemptobj->has_response_to_at_least_one_graded_question());
 395  
 396          $tosubmit = array(1 => array('answer' => $correctresponse));
 397          $attemptobj->process_submitted_actions($timenow, false, $tosubmit);
 398  
 399          // Finish the attempt.
 400          $attemptobj = quiz_attempt::create($attempt->id);
 401          $this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question());
 402  
 403          $attemptobj->process_finish($timenow, false);
 404  
 405          // Re-load quiz attempt data.
 406          $attemptobj = quiz_attempt::create($attempt->id);
 407  
 408          // Check that results are stored as expected.
 409          $this->assertEquals(1, $attemptobj->get_attempt_number());
 410          $this->assertEquals(1, $attemptobj->get_sum_marks());
 411          $this->assertEquals(true, $attemptobj->is_finished());
 412          $this->assertEquals($timenow, $attemptobj->get_submitted_date());
 413          $this->assertEquals($user1->id, $attemptobj->get_userid());
 414          $this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question());
 415  
 416          // Check quiz grades.
 417          $grades = quiz_get_user_grades($this->quizwithvariants, $user1->id);
 418          $grade = array_shift($grades);
 419          $this->assertEquals(100.0, $grade->rawgrade);
 420  
 421          // Check grade book.
 422          $gradebookgrades = grade_get_grades($SITE->id, 'mod', 'quiz', $this->quizwithvariants->id, $user1->id);
 423          $gradebookitem = array_shift($gradebookgrades->items);
 424          $gradebookgrade = array_shift($gradebookitem->grades);
 425          $this->assertEquals(100, $gradebookgrade->grade);
 426      }
 427  }