Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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

   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 core_question\local\bank\question_version_status;
  20  use mod_quiz\output\view_page;
  21  use question_engine;
  22  
  23  defined('MOODLE_INTERNAL') || die();
  24  
  25  global $CFG;
  26  require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  27  
  28  /**
  29   * Tests for the quiz_attempt class.
  30   *
  31   * @package   mod_quiz
  32   * @category  test
  33   * @copyright 2014 Tim Hunt
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class attempt_test extends \advanced_testcase {
  37  
  38      /**
  39       * Create quiz and attempt data with layout.
  40       *
  41       * @param string $layout layout to set. Like quiz attempt.layout. E.g. '1,2,0,3,4,0,'.
  42       * @param string $navmethod quiz navigation method (defaults to free)
  43       * @return quiz_attempt the new quiz_attempt object
  44       */
  45      protected function create_quiz_and_attempt_with_layout($layout, $navmethod = QUIZ_NAVMETHOD_FREE) {
  46          $this->resetAfterTest(true);
  47  
  48          // Make a user to do the quiz.
  49          $user = $this->getDataGenerator()->create_user();
  50          $course = $this->getDataGenerator()->create_course();
  51          // Make a quiz.
  52          $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
  53          $quiz = $quizgenerator->create_instance(['course' => $course->id,
  54              'grade' => 100.0, 'sumgrades' => 2, 'layout' => $layout, 'navmethod' => $navmethod]);
  55  
  56          $quizobj = quiz_settings::create($quiz->id, $user->id);
  57  
  58  
  59          $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
  60          $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
  61  
  62          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
  63          $cat = $questiongenerator->create_question_category();
  64  
  65          $page = 1;
  66          foreach (explode(',', $layout) as $slot) {
  67              if ($slot == 0) {
  68                  $page += 1;
  69                  continue;
  70              }
  71  
  72              $question = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
  73              quiz_add_quiz_question($question->id, $quiz, $page);
  74          }
  75  
  76          $timenow = time();
  77          $attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $user->id);
  78          quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
  79          quiz_attempt_save_started($quizobj, $quba, $attempt);
  80  
  81          return quiz_attempt::create($attempt->id);
  82      }
  83  
  84      public function test_attempt_url() {
  85          $attempt = $this->create_quiz_and_attempt_with_layout('1,2,0,3,4,0,5,6,0');
  86  
  87          $attemptid = $attempt->get_attempt()->id;
  88          $cmid = $attempt->get_cmid();
  89          $url = '/mod/quiz/attempt.php';
  90          $params = ['attempt' => $attemptid, 'cmid' => $cmid, 'page' => 2];
  91  
  92          $this->assertEquals(new \moodle_url($url, $params), $attempt->attempt_url(null, 2));
  93  
  94          $params['page'] = 1;
  95          $this->assertEquals(new \moodle_url($url, $params), $attempt->attempt_url(3));
  96  
  97          $questionattempt = $attempt->get_question_attempt(4);
  98          $expecteanchor = $questionattempt->get_outer_question_div_unique_id();
  99          $this->assertEquals(new \moodle_url($url, $params, $expecteanchor), $attempt->attempt_url(4));
 100  
 101          $questionattempt = $attempt->get_question_attempt(3);
 102          $expecteanchor = '#' . $questionattempt->get_outer_question_div_unique_id();
 103          $this->assertEquals(new \moodle_url('#'), $attempt->attempt_url(null, 2, 2));
 104          $this->assertEquals(new \moodle_url($expecteanchor), $attempt->attempt_url(3, -1, 1));
 105  
 106          $questionattempt = $attempt->get_question_attempt(4);
 107          $expecteanchor = $questionattempt->get_outer_question_div_unique_id();
 108          $this->assertEquals(new \moodle_url(null, null, $expecteanchor, null), $attempt->attempt_url(4, -1, 1));
 109  
 110          // Summary page.
 111          $url = '/mod/quiz/summary.php';
 112          unset($params['page']);
 113          $this->assertEquals(new \moodle_url($url, $params), $attempt->summary_url());
 114  
 115          // Review page.
 116          $url = '/mod/quiz/review.php';
 117          $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url());
 118  
 119          $params['page'] = 1;
 120          $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(3, -1, false));
 121          $this->assertEquals(new \moodle_url($url, $params, $expecteanchor), $attempt->review_url(4, -1, false));
 122  
 123          unset($params['page']);
 124          $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(null, 2, true));
 125          $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(1, -1, true));
 126  
 127          $params['page'] = 2;
 128          $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(null, 2, false));
 129          unset($params['page']);
 130  
 131          $params['showall'] = 0;
 132          $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(null, 0, false));
 133          $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(1, -1, false));
 134  
 135          $params['page'] = 1;
 136          unset($params['showall']);
 137          $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(3, -1, false));
 138  
 139          $params['page'] = 2;
 140          $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(null, 2));
 141          $this->assertEquals(new \moodle_url('#'), $attempt->review_url(null, -1, null, 0));
 142  
 143          $questionattempt = $attempt->get_question_attempt(3);
 144          $expecteanchor = '#' . $questionattempt->get_outer_question_div_unique_id();
 145          $this->assertEquals(new \moodle_url($expecteanchor), $attempt->review_url(3, -1, null, 0));
 146  
 147          $questionattempt = $attempt->get_question_attempt(4);
 148          $expecteanchor = '#' . $questionattempt->get_outer_question_div_unique_id();
 149          $this->assertEquals(new \moodle_url($expecteanchor), $attempt->review_url(4, -1, null, 0));
 150          $this->assertEquals(new \moodle_url('#'), $attempt->review_url(null, 2, true, 0));
 151  
 152          $questionattempt = $attempt->get_question_attempt(1);
 153          $expecteanchor = '#' . $questionattempt->get_outer_question_div_unique_id();
 154          $this->assertEquals(new \moodle_url($expecteanchor), $attempt->review_url(1, -1, true, 0));
 155          $this->assertEquals(new \moodle_url($expecteanchor), $attempt->review_url(1, -1, false, 0));
 156          $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(null, 2, false, 0));
 157          $this->assertEquals(new \moodle_url('#'), $attempt->review_url(null, 0, false, 0));
 158  
 159          $params['page'] = 1;
 160          $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(3, -1, false, 0));
 161  
 162          // Setup another attempt.
 163          $attempt = $this->create_quiz_and_attempt_with_layout(
 164              '1,2,3,4,5,6,7,8,9,10,0,11,12,13,14,15,16,17,18,19,20,0,' .
 165              '21,22,23,24,25,26,27,28,29,30,0,31,32,33,34,35,36,37,38,39,40,0,' .
 166              '41,42,43,44,45,46,47,48,49,50,0,51,52,53,54,55,56,57,58,59,60,0');
 167  
 168          $attemptid = $attempt->get_attempt()->id;
 169          $cmid = $attempt->get_cmid();
 170          $params = ['attempt' => $attemptid, 'cmid' => $cmid];
 171          $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url());
 172  
 173          $params['page'] = 2;
 174          $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(null, 2));
 175  
 176          $params['page'] = 1;
 177          unset($params['showall']);
 178          $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(11, -1, false));
 179  
 180          $questionattempt = $attempt->get_question_attempt(12);
 181          $expecteanchor = $questionattempt->get_outer_question_div_unique_id();
 182          $this->assertEquals(new \moodle_url($url, $params, $expecteanchor), $attempt->review_url(12, -1, false));
 183  
 184          $params['showall'] = 1;
 185          unset($params['page']);
 186          $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(null, 2, true));
 187  
 188          $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(1, -1, true));
 189          $params['page'] = 2;
 190          unset($params['showall']);
 191          $this->assertEquals(new \moodle_url($url, $params),  $attempt->review_url(null, 2, false));
 192          unset($params['page']);
 193          $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(null, 0, false));
 194          $params['page'] = 1;
 195          $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(11, -1, false));
 196          $this->assertEquals(new \moodle_url($url, $params, $expecteanchor), $attempt->review_url(12, -1, false));
 197          $params['page'] = 2;
 198          $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(null, 2));
 199          $this->assertEquals(new \moodle_url('#'), $attempt->review_url(null, -1, null, 0));
 200  
 201          $questionattempt = $attempt->get_question_attempt(3);
 202          $expecteanchor = $questionattempt->get_outer_question_div_unique_id();
 203          $this->assertEquals(new \moodle_url(null, null, $expecteanchor), $attempt->review_url(3, -1, null, 0));
 204  
 205          $questionattempt = $attempt->get_question_attempt(4);
 206          $expecteanchor = $questionattempt->get_outer_question_div_unique_id();
 207          $this->assertEquals(new \moodle_url(null, null, $expecteanchor), $attempt->review_url(4, -1, null, 0));
 208  
 209          $questionattempt = $attempt->get_question_attempt(1);
 210          $expecteanchor = '#' . $questionattempt->get_outer_question_div_unique_id();
 211          $this->assertEquals(new \moodle_url($expecteanchor), $attempt->review_url(1, -1, true, 0));
 212          $this->assertEquals(new \moodle_url('#'), $attempt->review_url(null, 2, true, 0));
 213  
 214          $params['page'] = 2;
 215          $questionattempt = $attempt->get_question_attempt(1);
 216          $expecteanchor = '#' . $questionattempt->get_outer_question_div_unique_id();
 217          $this->assertEquals(new \moodle_url($expecteanchor), $attempt->review_url(1, -1, false, 0));
 218          $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(null, 2, false, 0));
 219          $this->assertEquals(new \moodle_url('#'), $attempt->review_url(null, 0, false, 0));
 220  
 221          $params['page'] = 1;
 222          $this->assertEquals(new \moodle_url($url, $params), $attempt->review_url(11, -1, false, 0));
 223      }
 224  
 225      /**
 226       * Tests attempt page titles when all questions are on a single page.
 227       */
 228      public function test_attempt_titles_single() {
 229          $attempt = $this->create_quiz_and_attempt_with_layout('1,2,0');
 230  
 231          // Attempt page.
 232          $this->assertEquals('Quiz 1', $attempt->attempt_page_title(0));
 233  
 234          // Summary page.
 235          $this->assertEquals('Quiz 1: Attempt summary', $attempt->summary_page_title());
 236  
 237          // Review page.
 238          $this->assertEquals('Quiz 1: Attempt review', $attempt->review_page_title(0));
 239      }
 240  
 241      /**
 242       * Tests attempt page titles when questions are on multiple pages, but are reviewed on a single page.
 243       */
 244      public function test_attempt_titles_multiple_single() {
 245          $attempt = $this->create_quiz_and_attempt_with_layout('1,2,0,3,4,0,5,6,0');
 246  
 247          // Attempt page.
 248          $this->assertEquals('Quiz 1 (page 1 of 3)', $attempt->attempt_page_title(0));
 249          $this->assertEquals('Quiz 1 (page 2 of 3)', $attempt->attempt_page_title(1));
 250          $this->assertEquals('Quiz 1 (page 3 of 3)', $attempt->attempt_page_title(2));
 251  
 252          // Summary page.
 253          $this->assertEquals('Quiz 1: Attempt summary', $attempt->summary_page_title());
 254  
 255          // Review page.
 256          $this->assertEquals('Quiz 1: Attempt review', $attempt->review_page_title(0, true));
 257      }
 258  
 259      /**
 260       * Tests attempt page titles when questions are on multiple pages, and they are reviewed on multiple pages as well.
 261       */
 262      public function test_attempt_titles_multiple_multiple() {
 263          $attempt = $this->create_quiz_and_attempt_with_layout(
 264                  '1,2,3,4,5,6,7,8,9,10,0,11,12,13,14,15,16,17,18,19,20,0,' .
 265                  '21,22,23,24,25,26,27,28,29,30,0,31,32,33,34,35,36,37,38,39,40,0,' .
 266                  '41,42,43,44,45,46,47,48,49,50,0,51,52,53,54,55,56,57,58,59,60,0');
 267  
 268          // Attempt page.
 269          $this->assertEquals('Quiz 1 (page 1 of 6)', $attempt->attempt_page_title(0));
 270          $this->assertEquals('Quiz 1 (page 2 of 6)', $attempt->attempt_page_title(1));
 271          $this->assertEquals('Quiz 1 (page 6 of 6)', $attempt->attempt_page_title(5));
 272  
 273          // Summary page.
 274          $this->assertEquals('Quiz 1: Attempt summary', $attempt->summary_page_title());
 275  
 276          // Review page.
 277          $this->assertEquals('Quiz 1: Attempt review (page 1 of 6)', $attempt->review_page_title(0));
 278          $this->assertEquals('Quiz 1: Attempt review (page 2 of 6)', $attempt->review_page_title(1));
 279          $this->assertEquals('Quiz 1: Attempt review (page 6 of 6)', $attempt->review_page_title(5));
 280  
 281          // When all questions are shown.
 282          $this->assertEquals('Quiz 1: Attempt review', $attempt->review_page_title(0, true));
 283          $this->assertEquals('Quiz 1: Attempt review', $attempt->review_page_title(1, true));
 284      }
 285  
 286      public function test_is_participant() {
 287          global $USER;
 288          $this->resetAfterTest();
 289          $this->setAdminUser();
 290          $course = $this->getDataGenerator()->create_course();
 291          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 292          $student2 = $this->getDataGenerator()->create_and_enrol($course, 'student', [], 'manual', 0, 0, ENROL_USER_SUSPENDED);
 293          $quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id]);
 294          $quizobj = quiz_settings::create($quiz->id);
 295  
 296          // Login as student.
 297          $this->setUser($student);
 298          // Convert to a lesson object.
 299          $this->assertEquals(true, $quizobj->is_participant($student->id),
 300              'Student is enrolled, active and can participate');
 301  
 302          // Login as student2.
 303          $this->setUser($student2);
 304          $this->assertEquals(false, $quizobj->is_participant($student2->id),
 305              'Student is enrolled, suspended and can NOT participate');
 306  
 307          // Login as an admin.
 308          $this->setAdminUser();
 309          $this->assertEquals(false, $quizobj->is_participant($USER->id),
 310              'Admin is not enrolled and can NOT participate');
 311  
 312          $this->getDataGenerator()->enrol_user(2, $course->id);
 313          $this->assertEquals(true, $quizobj->is_participant($USER->id),
 314              'Admin is enrolled and can participate');
 315  
 316          $this->getDataGenerator()->enrol_user(2, $course->id, [], 'manual', 0, 0, ENROL_USER_SUSPENDED);
 317          $this->assertEquals(true, $quizobj->is_participant($USER->id),
 318              'Admin is enrolled, suspended and can participate');
 319      }
 320  
 321      /**
 322       * Test quiz_prepare_and_start_new_attempt function
 323       */
 324      public function test_quiz_prepare_and_start_new_attempt() {
 325          global $USER;
 326          $this->resetAfterTest();
 327  
 328          // Create course.
 329          $course = $this->getDataGenerator()->create_course();
 330          // Create students.
 331          $student1 = $this->getDataGenerator()->create_and_enrol($course, 'student');
 332          $student2 = $this->getDataGenerator()->create_and_enrol($course, 'student');
 333          // Create quiz.
 334          $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
 335          $quiz = $quizgenerator->create_instance(['course' => $course->id, 'grade' => 100.0, 'sumgrades' => 2, 'layout' => '1,0']);
 336          // Create question and add it to quiz.
 337          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
 338          $cat = $questiongenerator->create_question_category();
 339          $question = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
 340          quiz_add_quiz_question($question->id, $quiz, 1);
 341  
 342          $quizobj = quiz_settings::create($quiz->id);
 343  
 344          // Login as student1.
 345          $this->setUser($student1);
 346          // Create attempt for student1.
 347          $attempt = quiz_prepare_and_start_new_attempt($quizobj, 1, null, false, [], []);
 348          $this->assertEquals($student1->id, $attempt->userid);
 349          $this->assertEquals(0, $attempt->preview);
 350  
 351          // Login as student2.
 352          $this->setUser($student2);
 353          // Create attempt for student2.
 354          $attempt = quiz_prepare_and_start_new_attempt($quizobj, 1, null, false, [], []);
 355          $this->assertEquals($student2->id, $attempt->userid);
 356          $this->assertEquals(0, $attempt->preview);
 357  
 358          // Login as admin.
 359          $this->setAdminUser();
 360          // Create attempt for student1.
 361          $attempt = quiz_prepare_and_start_new_attempt($quizobj, 2, null, false, [], [], $student1->id);
 362          $this->assertEquals($student1->id, $attempt->userid);
 363          $this->assertEquals(0, $attempt->preview);
 364          $student1attempt = $attempt; // Save for extra verification below.
 365          // Create attempt for student2.
 366          $attempt = quiz_prepare_and_start_new_attempt($quizobj, 2, null, false, [], [], $student2->id);
 367          $this->assertEquals($student2->id, $attempt->userid);
 368          $this->assertEquals(0, $attempt->preview);
 369          // Create attempt for user id that the same with current $USER->id.
 370          $attempt = quiz_prepare_and_start_new_attempt($quizobj, 2, null, false, [], [], $USER->id);
 371          $this->assertEquals($USER->id, $attempt->userid);
 372          $this->assertEquals(1, $attempt->preview);
 373  
 374          // Check that the userid stored in the first step is the user the attempt is for,
 375          // not the user who triggered the creation.
 376          $quba = question_engine::load_questions_usage_by_activity($student1attempt->uniqueid);
 377          $step = $quba->get_question_attempt(1)->get_step(0);
 378          $this->assertEquals($student1->id, $step->get_user_id());
 379      }
 380  
 381      /**
 382       * Test check_page_access function
 383       * @covers \quiz_attempt::check_page_access
 384       */
 385      public function test_check_page_access() {
 386          $timenow = time();
 387  
 388          // Free navigation.
 389          $attempt = $this->create_quiz_and_attempt_with_layout('1,0,2,0,3,0,4,0,5,0', QUIZ_NAVMETHOD_FREE);
 390  
 391          // Check access.
 392          $this->assertTrue($attempt->check_page_access(4));
 393          $this->assertTrue($attempt->check_page_access(3));
 394          $this->assertTrue($attempt->check_page_access(2));
 395          $this->assertTrue($attempt->check_page_access(1));
 396          $this->assertTrue($attempt->check_page_access(0));
 397          $this->assertTrue($attempt->check_page_access(2));
 398  
 399          // Access page 2.
 400          $attempt->set_currentpage(2);
 401          $attempt = quiz_attempt::create($attempt->get_attempt()->id);
 402  
 403          // Check access.
 404          $this->assertTrue($attempt->check_page_access(0));
 405          $this->assertTrue($attempt->check_page_access(1));
 406          $this->assertTrue($attempt->check_page_access(2));
 407          $this->assertTrue($attempt->check_page_access(3));
 408          $this->assertTrue($attempt->check_page_access(4));
 409  
 410          // Sequential navigation.
 411          $attempt = $this->create_quiz_and_attempt_with_layout('1,0,2,0,3,0,4,0,5,0', QUIZ_NAVMETHOD_SEQ);
 412  
 413          // Check access.
 414          $this->assertTrue($attempt->check_page_access(0));
 415          $this->assertTrue($attempt->check_page_access(1));
 416          $this->assertFalse($attempt->check_page_access(2));
 417          $this->assertFalse($attempt->check_page_access(3));
 418          $this->assertFalse($attempt->check_page_access(4));
 419  
 420          // Access page 1.
 421          $attempt->set_currentpage(1);
 422          $attempt = quiz_attempt::create($attempt->get_attempt()->id);
 423          $this->assertTrue($attempt->check_page_access(1));
 424  
 425          // Access page 2.
 426          $attempt->set_currentpage(2);
 427          $attempt = quiz_attempt::create($attempt->get_attempt()->id);
 428          $this->assertTrue($attempt->check_page_access(2));
 429  
 430          $this->assertTrue($attempt->check_page_access(3));
 431          $this->assertFalse($attempt->check_page_access(4));
 432          $this->assertFalse($attempt->check_page_access(1));
 433      }
 434  
 435      /**
 436       * Starting a new attempt with a question in draft status should throw an exception.
 437       *
 438       * @covers ::quiz_start_new_attempt()
 439       * @return void
 440       */
 441      public function test_start_new_attempt_with_draft(): void {
 442          $this->resetAfterTest();
 443  
 444          // Create course.
 445          $course = $this->getDataGenerator()->create_course();
 446          // Create students.
 447          $student1 = $this->getDataGenerator()->create_and_enrol($course, 'student');
 448          $student2 = $this->getDataGenerator()->create_and_enrol($course, 'student');
 449          // Create quiz.
 450          $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
 451          $quiz = $quizgenerator->create_instance(['course' => $course->id, 'grade' => 100.0, 'sumgrades' => 2, 'layout' => '1,0']);
 452          // Create question and add it to quiz.
 453          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
 454          $cat = $questiongenerator->create_question_category();
 455          $question = $questiongenerator->create_question('shortanswer', null,
 456                  ['category' => $cat->id, 'status' => question_version_status::QUESTION_STATUS_DRAFT]);
 457          quiz_add_quiz_question($question->id, $quiz, 1);
 458  
 459          $quizobj = quiz_settings::create($quiz->id);
 460          $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
 461          $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
 462          $attempt = quiz_create_attempt($quizobj, 1, false, time(), false, $student1->id);
 463  
 464          $this->expectExceptionObject(new \moodle_exception('questiondraftonly', 'mod_quiz', '', $question->name));
 465          quiz_start_new_attempt($quizobj, $quba, $attempt, 1, time());
 466      }
 467  
 468      /**
 469       * Starting a new attempt built on last with a question in draft status should throw an exception.
 470       *
 471       * @covers ::quiz_start_attempt_built_on_last()
 472       * @return void
 473       */
 474      public function test_quiz_start_attempt_built_on_last_with_draft(): void {
 475          global $DB;
 476          $this->resetAfterTest();
 477  
 478          // Create course.
 479          $course = $this->getDataGenerator()->create_course();
 480          // Create students.
 481          $student1 = $this->getDataGenerator()->create_and_enrol($course, 'student');
 482          $student2 = $this->getDataGenerator()->create_and_enrol($course, 'student');
 483          // Create quiz.
 484          $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
 485          $quiz = $quizgenerator->create_instance(['course' => $course->id, 'grade' => 100.0, 'sumgrades' => 2, 'layout' => '1,0']);
 486          // Create question and add it to quiz.
 487          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
 488          $cat = $questiongenerator->create_question_category();
 489          $question = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
 490          quiz_add_quiz_question($question->id, $quiz, 1);
 491  
 492          $quizobj = quiz_settings::create($quiz->id);
 493          $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
 494          $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
 495          $attempt = quiz_create_attempt($quizobj, 1, false, time(), false, $student1->id);
 496          $attempt = quiz_start_new_attempt($quizobj, $quba, $attempt, 1, time());
 497          $attempt = quiz_attempt_save_started($quizobj, $quba, $attempt);
 498          $DB->set_field('question_versions', 'status', question_version_status::QUESTION_STATUS_DRAFT,
 499                  ['questionid' => $question->id]);
 500          // We need to reset the cache since the question has been edited by changing its status to draft.
 501          \question_bank::notify_question_edited($question->id);
 502          $quizobj = quiz_settings::create($quiz->id);
 503          $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
 504          $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
 505          $newattempt = quiz_create_attempt($quizobj, 2, $attempt, time(), false, $student1->id);
 506  
 507          $this->expectExceptionObject(new \moodle_exception('questiondraftonly', 'mod_quiz', '', $question->name));
 508          quiz_start_attempt_built_on_last($quba, $newattempt, $attempt);
 509      }
 510  
 511      /**
 512       * Starting a new attempt and check the summary previous attempts table.
 513       *
 514       * @covers ::view_table()
 515       */
 516      public function test_view_table(): void {
 517          global $PAGE;
 518          $this->resetAfterTest();
 519  
 520          $timenow = time();
 521          // Create attempt object.
 522          $attempt = $this->create_quiz_and_attempt_with_layout('1,1,0');
 523          // Finish attempt.
 524          $attempt->process_finish($timenow, false);
 525  
 526          $quiz = $attempt->get_quiz();
 527          $context = $attempt->get_context();
 528  
 529          // Prepare view object.
 530          $viewobj = new view_page();
 531          $viewobj->attemptcolumn = true;
 532          $viewobj->markcolumn = true;
 533          $viewobj->gradecolumn = true;
 534          $viewobj->canreviewmine = true;
 535          $viewobj->mygrade = 0.00;
 536          $viewobj->feedbackcolumn = false;
 537          $viewobj->attempts = $attempt;
 538          $viewobj->attemptobjs[] = new quiz_attempt($attempt->get_attempt(),
 539              $quiz, $attempt->get_cm(), $attempt->get_course(), false);
 540          $viewobj->accessmanager = new access_manager($attempt->get_quizobj(), $timenow,
 541              has_capability('mod/quiz:ignoretimelimits', $context, null, false));
 542  
 543          // Render summary previous attempts table.
 544          $renderer = $PAGE->get_renderer('mod_quiz');
 545          $table = $renderer->view_table($quiz, $context, $viewobj);
 546          $captionpattern = '/<caption\b[^>]*>' . get_string('summaryofattempts', 'quiz') . '<\/caption>/';
 547  
 548          // Check caption existed.
 549          $this->assertMatchesRegularExpression($captionpattern, $table);
 550          // Check column attempt.
 551          $this->assertMatchesRegularExpression('/<td\b[^>]*>' . $attempt->get_attempt_number() . '<\/td>/', $table);
 552          // Check column state.
 553          $this->assertMatchesRegularExpression('/<td\b[^>]*>' . ucfirst($attempt->get_state()) . '.+?<\/td>/', $table);
 554          // Check column marks.
 555          $this->assertMatchesRegularExpression('/<td\b[^>]* c2.+?' .
 556              quiz_format_grade($quiz, $attempt->get_sum_marks()) .'<\/td>/', $table);
 557          // Check column grades.
 558          $this->assertMatchesRegularExpression('/<td\b[^>]* c2.+?0\.00<\/td>/', $table);
 559          // Check column review.
 560          $this->assertMatchesRegularExpression('/<td\b[^>]*>.+?Review<\/a><\/td>/', $table);
 561      }
 562  }