Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402] [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 moodle_url; 20 use question_bank; 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 * Quiz attempt walk through. 30 * 31 * @package mod_quiz 32 * @category test 33 * @copyright 2013 The Open University 34 * @author Jamie Pratt <me@jamiep.org> 35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 36 * @covers \mod_quiz\quiz_attempt 37 */ 38 class attempt_walkthrough_test extends \advanced_testcase { 39 40 /** 41 * Create a quiz with questions and walk through a quiz attempt. 42 */ 43 public function test_quiz_attempt_walkthrough() { 44 global $SITE; 45 46 $this->resetAfterTest(true); 47 48 // Make a quiz. 49 $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz'); 50 51 $quiz = $quizgenerator->create_instance(['course' => $SITE->id, 'questionsperpage' => 0, 'grade' => 100.0, 52 'sumgrades' => 3]); 53 54 // Create a couple of questions. 55 $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); 56 57 $cat = $questiongenerator->create_question_category(); 58 $saq = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]); 59 $numq = $questiongenerator->create_question('numerical', null, ['category' => $cat->id]); 60 $matchq = $questiongenerator->create_question('match', null, ['category' => $cat->id]); 61 $description = $questiongenerator->create_question('description', null, ['category' => $cat->id]); 62 63 // Add them to the quiz. 64 quiz_add_quiz_question($saq->id, $quiz); 65 quiz_add_quiz_question($numq->id, $quiz); 66 quiz_add_quiz_question($matchq->id, $quiz); 67 quiz_add_quiz_question($description->id, $quiz); 68 69 // Make a user to do the quiz. 70 $user1 = $this->getDataGenerator()->create_user(); 71 72 $quizobj = quiz_settings::create($quiz->id, $user1->id); 73 74 // Start the attempt. 75 $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context()); 76 $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour); 77 78 $timenow = time(); 79 $attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $user1->id); 80 81 quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow); 82 $this->assertEquals('1,2,3,4,0', $attempt->layout); 83 84 quiz_attempt_save_started($quizobj, $quba, $attempt); 85 86 // Process some responses from the student. 87 $attemptobj = quiz_attempt::create($attempt->id); 88 $this->assertFalse($attemptobj->has_response_to_at_least_one_graded_question()); 89 // The student has not answered any questions. 90 $this->assertEquals(3, $attemptobj->get_number_of_unanswered_questions()); 91 92 $tosubmit = [1 => ['answer' => 'frog'], 93 2 => ['answer' => '3.14']]; 94 95 $attemptobj->process_submitted_actions($timenow, false, $tosubmit); 96 // The student has answered two questions, and only one remaining. 97 $this->assertEquals(1, $attemptobj->get_number_of_unanswered_questions()); 98 99 $tosubmit = [ 100 3 => [ 101 'frog' => 'amphibian', 102 'cat' => 'mammal', 103 'newt' => '' 104 ] 105 ]; 106 107 $attemptobj->process_submitted_actions($timenow, false, $tosubmit); 108 // The student has answered three questions but one is invalid, so there is still one remaining. 109 $this->assertEquals(1, $attemptobj->get_number_of_unanswered_questions()); 110 111 $tosubmit = [ 112 3 => [ 113 'frog' => 'amphibian', 114 'cat' => 'mammal', 115 'newt' => 'amphibian' 116 ] 117 ]; 118 119 $attemptobj->process_submitted_actions($timenow, false, $tosubmit); 120 // The student has answered three questions, so there are no remaining. 121 $this->assertEquals(0, $attemptobj->get_number_of_unanswered_questions()); 122 123 // Finish the attempt. 124 $attemptobj = quiz_attempt::create($attempt->id); 125 $this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question()); 126 $attemptobj->process_finish($timenow, false); 127 128 // Re-load quiz attempt data. 129 $attemptobj = quiz_attempt::create($attempt->id); 130 131 // Check that results are stored as expected. 132 $this->assertEquals(1, $attemptobj->get_attempt_number()); 133 $this->assertEquals(3, $attemptobj->get_sum_marks()); 134 $this->assertEquals(true, $attemptobj->is_finished()); 135 $this->assertEquals($timenow, $attemptobj->get_submitted_date()); 136 $this->assertEquals($user1->id, $attemptobj->get_userid()); 137 $this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question()); 138 $this->assertEquals(0, $attemptobj->get_number_of_unanswered_questions()); 139 140 // Check quiz grades. 141 $grades = quiz_get_user_grades($quiz, $user1->id); 142 $grade = array_shift($grades); 143 $this->assertEquals(100.0, $grade->rawgrade); 144 145 // Check grade book. 146 $gradebookgrades = grade_get_grades($SITE->id, 'mod', 'quiz', $quiz->id, $user1->id); 147 $gradebookitem = array_shift($gradebookgrades->items); 148 $gradebookgrade = array_shift($gradebookitem->grades); 149 $this->assertEquals(100, $gradebookgrade->grade); 150 } 151 152 /** 153 * Create a quiz containing one question and a close time. 154 * 155 * The question is the standard shortanswer test question. 156 * The quiz is set to close 1 hour from now. 157 * The quiz is set to use a grade period of 1 hour once time expires. 158 * 159 * @param string $overduehandling value for the overduehandling quiz setting. 160 * @return \stdClass the quiz that was created. 161 */ 162 protected function create_quiz_with_one_question(string $overduehandling = 'graceperiod'): \stdClass { 163 global $SITE; 164 $this->resetAfterTest(); 165 166 // Make a quiz. 167 $timeclose = time() + HOURSECS; 168 $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz'); 169 170 $quiz = $quizgenerator->create_instance( 171 ['course' => $SITE->id, 'timeclose' => $timeclose, 172 'overduehandling' => $overduehandling, 'graceperiod' => HOURSECS]); 173 174 // Create a question. 175 /** @var \core_question_generator $questiongenerator */ 176 $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); 177 $cat = $questiongenerator->create_question_category(); 178 $saq = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]); 179 180 // Add them to the quiz. 181 $quizobj = quiz_settings::create($quiz->id); 182 quiz_add_quiz_question($saq->id, $quiz, 0, 1); 183 $quizobj->get_grade_calculator()->recompute_quiz_sumgrades(); 184 185 return $quiz; 186 } 187 188 public function test_quiz_attempt_walkthrough_submit_time_recorded_correctly_when_overdue() { 189 190 $quiz = $this->create_quiz_with_one_question(); 191 192 // Make a user to do the quiz. 193 $user = $this->getDataGenerator()->create_user(); 194 $this->setUser($user); 195 $quizobj = quiz_settings::create($quiz->id, $user->id); 196 197 // Start the attempt. 198 $attempt = quiz_prepare_and_start_new_attempt($quizobj, 1, null); 199 200 // Process some responses from the student. 201 $attemptobj = quiz_attempt::create($attempt->id); 202 $this->assertEquals(1, $attemptobj->get_number_of_unanswered_questions()); 203 $attemptobj->process_submitted_actions($quiz->timeclose - 30 * MINSECS, false, [1 => ['answer' => 'frog']]); 204 205 // Attempt goes overdue (e.g. if cron ran). 206 $attemptobj = quiz_attempt::create($attempt->id); 207 $attemptobj->process_going_overdue($quiz->timeclose + 2 * get_config('quiz', 'graceperiodmin'), false); 208 209 // Verify the attempt state. 210 $attemptobj = quiz_attempt::create($attempt->id); 211 $this->assertEquals(1, $attemptobj->get_attempt_number()); 212 $this->assertEquals(false, $attemptobj->is_finished()); 213 $this->assertEquals(0, $attemptobj->get_submitted_date()); 214 $this->assertEquals($user->id, $attemptobj->get_userid()); 215 $this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question()); 216 $this->assertEquals(0, $attemptobj->get_number_of_unanswered_questions()); 217 218 // Student submits the attempt during the grace period. 219 $attemptobj = quiz_attempt::create($attempt->id); 220 $attemptobj->process_attempt($quiz->timeclose + 30 * MINSECS, true, false, 1); 221 222 // Verify the attempt state. 223 $attemptobj = quiz_attempt::create($attempt->id); 224 $this->assertEquals(1, $attemptobj->get_attempt_number()); 225 $this->assertEquals(true, $attemptobj->is_finished()); 226 $this->assertEquals($quiz->timeclose + 30 * MINSECS, $attemptobj->get_submitted_date()); 227 $this->assertEquals($user->id, $attemptobj->get_userid()); 228 $this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question()); 229 $this->assertEquals(0, $attemptobj->get_number_of_unanswered_questions()); 230 } 231 232 public function test_quiz_attempt_walkthrough_close_time_extended_at_last_minute() { 233 global $DB; 234 235 $quiz = $this->create_quiz_with_one_question(); 236 $originaltimeclose = $quiz->timeclose; 237 238 // Make a user to do the quiz. 239 $user = $this->getDataGenerator()->create_user(); 240 $this->setUser($user); 241 $quizobj = quiz_settings::create($quiz->id, $user->id); 242 243 // Start the attempt. 244 $attempt = quiz_prepare_and_start_new_attempt($quizobj, 1, null); 245 246 // Process some responses from the student during the attempt. 247 $attemptobj = quiz_attempt::create($attempt->id); 248 $attemptobj->process_submitted_actions($originaltimeclose - 30 * MINSECS, false, [1 => ['answer' => 'frog']]); 249 250 // Teacher edits the quiz to extend the time-limit by one minute. 251 $DB->set_field('quiz', 'timeclose', $originaltimeclose + MINSECS, ['id' => $quiz->id]); 252 \course_modinfo::clear_instance_cache($quiz->course); 253 254 // Timer expires in the student browser and thinks it is time to submit the quiz. 255 // This sets $finishattempt to false - since the student did not click the button, and $timeup to true. 256 $attemptobj = quiz_attempt::create($attempt->id); 257 $attemptobj->process_attempt($originaltimeclose, false, true, 1); 258 259 // Verify the attempt state - the $timeup was ignored becuase things have changed server-side. 260 $attemptobj = quiz_attempt::create($attempt->id); 261 $this->assertEquals(1, $attemptobj->get_attempt_number()); 262 $this->assertFalse($attemptobj->is_finished()); 263 $this->assertEquals(quiz_attempt::IN_PROGRESS, $attemptobj->get_state()); 264 $this->assertEquals(0, $attemptobj->get_submitted_date()); 265 $this->assertEquals($user->id, $attemptobj->get_userid()); 266 } 267 268 /** 269 * Create a quiz with a random as well as other questions and walk through quiz attempts. 270 */ 271 public function test_quiz_with_random_question_attempt_walkthrough() { 272 global $SITE; 273 274 $this->resetAfterTest(true); 275 question_bank::get_qtype('random')->clear_caches_before_testing(); 276 277 $this->setAdminUser(); 278 279 // Make a quiz. 280 $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz'); 281 282 $quiz = $quizgenerator->create_instance(['course' => $SITE->id, 'questionsperpage' => 2, 'grade' => 100.0, 283 'sumgrades' => 4]); 284 285 $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); 286 287 // Add two questions to question category. 288 $cat = $questiongenerator->create_question_category(); 289 $saq = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]); 290 $numq = $questiongenerator->create_question('numerical', null, ['category' => $cat->id]); 291 292 // Add random question to the quiz. 293 quiz_add_random_questions($quiz, 0, $cat->id, 1, false); 294 295 // Make another category. 296 $cat2 = $questiongenerator->create_question_category(); 297 $match = $questiongenerator->create_question('match', null, ['category' => $cat->id]); 298 299 quiz_add_quiz_question($match->id, $quiz, 0); 300 301 $multichoicemulti = $questiongenerator->create_question('multichoice', 'two_of_four', ['category' => $cat->id]); 302 303 quiz_add_quiz_question($multichoicemulti->id, $quiz, 0); 304 305 $multichoicesingle = $questiongenerator->create_question('multichoice', 'one_of_four', ['category' => $cat->id]); 306 307 quiz_add_quiz_question($multichoicesingle->id, $quiz, 0); 308 309 foreach ([$saq->id => 'frog', $numq->id => '3.14'] as $randomqidtoselect => $randqanswer) { 310 // Make a new user to do the quiz each loop. 311 $user1 = $this->getDataGenerator()->create_user(); 312 $this->setUser($user1); 313 314 $quizobj = quiz_settings::create($quiz->id, $user1->id); 315 316 // Start the attempt. 317 $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context()); 318 $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour); 319 320 $timenow = time(); 321 $attempt = quiz_create_attempt($quizobj, 1, false, $timenow); 322 323 quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow, [1 => $randomqidtoselect]); 324 $this->assertEquals('1,2,0,3,4,0', $attempt->layout); 325 326 quiz_attempt_save_started($quizobj, $quba, $attempt); 327 328 // Process some responses from the student. 329 $attemptobj = quiz_attempt::create($attempt->id); 330 $this->assertFalse($attemptobj->has_response_to_at_least_one_graded_question()); 331 $this->assertEquals(4, $attemptobj->get_number_of_unanswered_questions()); 332 333 $tosubmit = []; 334 $selectedquestionid = $quba->get_question_attempt(1)->get_question_id(); 335 $tosubmit[1] = ['answer' => $randqanswer]; 336 $tosubmit[2] = [ 337 'frog' => 'amphibian', 338 'cat' => 'mammal', 339 'newt' => 'amphibian']; 340 $tosubmit[3] = ['One' => '1', 'Two' => '0', 'Three' => '1', 'Four' => '0']; // First and third choice. 341 $tosubmit[4] = ['answer' => 'One']; // The first choice. 342 343 $attemptobj->process_submitted_actions($timenow, false, $tosubmit); 344 345 // Finish the attempt. 346 $attemptobj = quiz_attempt::create($attempt->id); 347 $this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question()); 348 $this->assertEquals(0, $attemptobj->get_number_of_unanswered_questions()); 349 $attemptobj->process_finish($timenow, false); 350 351 // Re-load quiz attempt data. 352 $attemptobj = quiz_attempt::create($attempt->id); 353 354 // Check that results are stored as expected. 355 $this->assertEquals(1, $attemptobj->get_attempt_number()); 356 $this->assertEquals(4, $attemptobj->get_sum_marks()); 357 $this->assertEquals(true, $attemptobj->is_finished()); 358 $this->assertEquals($timenow, $attemptobj->get_submitted_date()); 359 $this->assertEquals($user1->id, $attemptobj->get_userid()); 360 $this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question()); 361 $this->assertEquals(0, $attemptobj->get_number_of_unanswered_questions()); 362 363 // Check quiz grades. 364 $grades = quiz_get_user_grades($quiz, $user1->id); 365 $grade = array_shift($grades); 366 $this->assertEquals(100.0, $grade->rawgrade); 367 368 // Check grade book. 369 $gradebookgrades = grade_get_grades($SITE->id, 'mod', 'quiz', $quiz->id, $user1->id); 370 $gradebookitem = array_shift($gradebookgrades->items); 371 $gradebookgrade = array_shift($gradebookitem->grades); 372 $this->assertEquals(100, $gradebookgrade->grade); 373 } 374 } 375 376 377 public function get_correct_response_for_variants() { 378 return [[1, 9.9], [2, 8.5], [5, 14.2], [10, 6.8, true]]; 379 } 380 381 protected $quizwithvariants = null; 382 383 /** 384 * Create a quiz with a single question with variants and walk through quiz attempts. 385 * 386 * @dataProvider get_correct_response_for_variants 387 */ 388 public function test_quiz_with_question_with_variants_attempt_walkthrough($variantno, $correctresponse, $done = false) { 389 global $SITE; 390 391 $this->resetAfterTest($done); 392 393 $this->setAdminUser(); 394 395 if ($this->quizwithvariants === null) { 396 // Make a quiz. 397 $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz'); 398 399 $this->quizwithvariants = $quizgenerator->create_instance(['course' => $SITE->id, 400 'questionsperpage' => 0, 401 'grade' => 100.0, 402 'sumgrades' => 1]); 403 404 $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); 405 406 $cat = $questiongenerator->create_question_category(); 407 $calc = $questiongenerator->create_question('calculatedsimple', 'sumwithvariants', ['category' => $cat->id]); 408 quiz_add_quiz_question($calc->id, $this->quizwithvariants, 0); 409 } 410 411 412 // Make a new user to do the quiz. 413 $user1 = $this->getDataGenerator()->create_user(); 414 $this->setUser($user1); 415 $quizobj = quiz_settings::create($this->quizwithvariants->id, $user1->id); 416 417 // Start the attempt. 418 $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context()); 419 $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour); 420 421 $timenow = time(); 422 $attempt = quiz_create_attempt($quizobj, 1, false, $timenow); 423 424 // Select variant. 425 quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow, [], [1 => $variantno]); 426 $this->assertEquals('1,0', $attempt->layout); 427 quiz_attempt_save_started($quizobj, $quba, $attempt); 428 429 // Process some responses from the student. 430 $attemptobj = quiz_attempt::create($attempt->id); 431 $this->assertFalse($attemptobj->has_response_to_at_least_one_graded_question()); 432 $this->assertEquals(1, $attemptobj->get_number_of_unanswered_questions()); 433 434 $tosubmit = [1 => ['answer' => $correctresponse]]; 435 $attemptobj->process_submitted_actions($timenow, false, $tosubmit); 436 437 // Finish the attempt. 438 $attemptobj = quiz_attempt::create($attempt->id); 439 $this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question()); 440 $this->assertEquals(0, $attemptobj->get_number_of_unanswered_questions()); 441 442 $attemptobj->process_finish($timenow, false); 443 444 // Re-load quiz attempt data. 445 $attemptobj = quiz_attempt::create($attempt->id); 446 447 // Check that results are stored as expected. 448 $this->assertEquals(1, $attemptobj->get_attempt_number()); 449 $this->assertEquals(1, $attemptobj->get_sum_marks()); 450 $this->assertEquals(true, $attemptobj->is_finished()); 451 $this->assertEquals($timenow, $attemptobj->get_submitted_date()); 452 $this->assertEquals($user1->id, $attemptobj->get_userid()); 453 $this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question()); 454 $this->assertEquals(0, $attemptobj->get_number_of_unanswered_questions()); 455 456 // Check quiz grades. 457 $grades = quiz_get_user_grades($this->quizwithvariants, $user1->id); 458 $grade = array_shift($grades); 459 $this->assertEquals(100.0, $grade->rawgrade); 460 461 // Check grade book. 462 $gradebookgrades = grade_get_grades($SITE->id, 'mod', 'quiz', $this->quizwithvariants->id, $user1->id); 463 $gradebookitem = array_shift($gradebookgrades->items); 464 $gradebookgrade = array_shift($gradebookitem->grades); 465 $this->assertEquals(100, $gradebookgrade->grade); 466 } 467 468 public function test_quiz_attempt_walkthrough_abandoned_attempt_reopened_with_timelimit_override() { 469 global $DB; 470 471 $quiz = $this->create_quiz_with_one_question('autoabandon'); 472 $originaltimeclose = $quiz->timeclose; 473 474 // Make a user to do the quiz. 475 $user = $this->getDataGenerator()->create_user(); 476 $this->setUser($user); 477 $quizobj = quiz_settings::create($quiz->id, $user->id); 478 479 // Start the attempt. 480 $attempt = quiz_prepare_and_start_new_attempt($quizobj, 1, null); 481 482 // Process some responses from the student during the attempt. 483 $attemptobj = quiz_attempt::create($attempt->id); 484 $attemptobj->process_submitted_actions($originaltimeclose - 30 * MINSECS, false, [1 => ['answer' => 'frog']]); 485 486 // Student leaves, so cron closes the attempt when time expires. 487 $attemptobj->process_abandon($originaltimeclose + 5 * MINSECS, false); 488 489 // Verify the attempt state. 490 $attemptobj = quiz_attempt::create($attempt->id); 491 $this->assertEquals(quiz_attempt::ABANDONED, $attemptobj->get_state()); 492 $this->assertEquals(0, $attemptobj->get_submitted_date()); 493 $this->assertEquals($user->id, $attemptobj->get_userid()); 494 495 // The teacher feels kind, so adds an override for the student, and re-opens the attempt. 496 $sink = $this->redirectEvents(); 497 $overriddentimeclose = $originaltimeclose + HOURSECS; 498 $DB->insert_record('quiz_overrides', [ 499 'quiz' => $quiz->id, 500 'userid' => $user->id, 501 'timeclose' => $overriddentimeclose, 502 ]); 503 $attemptobj = quiz_attempt::create($attempt->id); 504 $reopentime = $originaltimeclose + 10 * MINSECS; 505 $attemptobj->process_reopen_abandoned($reopentime); 506 507 // Verify the attempt state. 508 $attemptobj = quiz_attempt::create($attempt->id); 509 $this->assertEquals(1, $attemptobj->get_attempt_number()); 510 $this->assertFalse($attemptobj->is_finished()); 511 $this->assertEquals(quiz_attempt::IN_PROGRESS, $attemptobj->get_state()); 512 $this->assertEquals(0, $attemptobj->get_submitted_date()); 513 $this->assertEquals($user->id, $attemptobj->get_userid()); 514 $this->assertEquals($overriddentimeclose, 515 $attemptobj->get_access_manager($reopentime)->get_end_time($attemptobj->get_attempt())); 516 517 // Verify this was logged correctly. 518 $events = $sink->get_events(); 519 $this->assertCount(1, $events); 520 521 $reopenedevent = array_shift($events); 522 $this->assertInstanceOf('\mod_quiz\event\attempt_reopened', $reopenedevent); 523 $this->assertEquals($attemptobj->get_context(), $reopenedevent->get_context()); 524 $this->assertEquals(new moodle_url('/mod/quiz/review.php', ['attempt' => $attemptobj->get_attemptid()]), 525 $reopenedevent->get_url()); 526 } 527 528 public function test_quiz_attempt_walkthrough_abandoned_attempt_reopened_after_close_time() { 529 $quiz = $this->create_quiz_with_one_question('autoabandon'); 530 $originaltimeclose = $quiz->timeclose; 531 532 // Make a user to do the quiz. 533 $user = $this->getDataGenerator()->create_user(); 534 $this->setUser($user); 535 $quizobj = quiz_settings::create($quiz->id, $user->id); 536 537 // Start the attempt. 538 $attempt = quiz_prepare_and_start_new_attempt($quizobj, 1, null); 539 540 // Process some responses from the student during the attempt. 541 $attemptobj = quiz_attempt::create($attempt->id); 542 $attemptobj->process_submitted_actions($originaltimeclose - 30 * MINSECS, false, [1 => ['answer' => 'frog']]); 543 544 // Student leaves, so cron closes the attempt when time expires. 545 $attemptobj->process_abandon($originaltimeclose + 5 * MINSECS, false); 546 547 // Verify the attempt state. 548 $attemptobj = quiz_attempt::create($attempt->id); 549 $this->assertEquals(quiz_attempt::ABANDONED, $attemptobj->get_state()); 550 $this->assertEquals(0, $attemptobj->get_submitted_date()); 551 $this->assertEquals($user->id, $attemptobj->get_userid()); 552 553 // The teacher reopens the attempt without granting more time, so previously submitted responess are graded. 554 $sink = $this->redirectEvents(); 555 $reopentime = $originaltimeclose + 10 * MINSECS; 556 $attemptobj->process_reopen_abandoned($reopentime); 557 558 // Verify the attempt state. 559 $attemptobj = quiz_attempt::create($attempt->id); 560 $this->assertEquals(1, $attemptobj->get_attempt_number()); 561 $this->assertTrue($attemptobj->is_finished()); 562 $this->assertEquals(quiz_attempt::FINISHED, $attemptobj->get_state()); 563 $this->assertEquals($originaltimeclose, $attemptobj->get_submitted_date()); 564 $this->assertEquals($user->id, $attemptobj->get_userid()); 565 $this->assertEquals(1, $attemptobj->get_sum_marks()); 566 567 // Verify this was logged correctly - there are some gradebook events between the two we want to check. 568 $events = $sink->get_events(); 569 $this->assertGreaterThanOrEqual(2, $events); 570 571 $reopenedevent = array_shift($events); 572 $this->assertInstanceOf('\mod_quiz\event\attempt_reopened', $reopenedevent); 573 $this->assertEquals($attemptobj->get_context(), $reopenedevent->get_context()); 574 $this->assertEquals(new moodle_url('/mod/quiz/review.php', ['attempt' => $attemptobj->get_attemptid()]), 575 $reopenedevent->get_url()); 576 577 $submittedevent = array_pop($events); 578 $this->assertInstanceOf('\mod_quiz\event\attempt_submitted', $submittedevent); 579 $this->assertEquals($attemptobj->get_context(), $submittedevent->get_context()); 580 $this->assertEquals(new moodle_url('/mod/quiz/review.php', ['attempt' => $attemptobj->get_attemptid()]), 581 $submittedevent->get_url()); 582 } 583 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body