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.
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

< /** < * Quiz attempt walk through tests. < * < * @package mod_quiz < * @category phpunit < * @copyright 2013 The Open University < * @author Jamie Pratt <me@jamiep.org> < * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later < */
> namespace mod_quiz; > > use question_bank; > use question_engine; > use quiz; > use quiz_attempt;
defined('MOODLE_INTERNAL') || die(); global $CFG; require_once($CFG->dirroot . '/mod/quiz/locallib.php'); /** * Quiz attempt walk through. * * @package mod_quiz
< * @category phpunit
> * @category test
* @copyright 2013 The Open University * @author Jamie Pratt <me@jamiep.org> * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
> * @covers \quiz_attempt
*/
< class mod_quiz_attempt_walkthrough_testcase extends advanced_testcase {
> class attempt_walkthrough_test extends \advanced_testcase {
/** * Create a quiz with questions and walk through a quiz attempt. */ public function test_quiz_attempt_walkthrough() { global $SITE; $this->resetAfterTest(true); // Make a quiz. $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz'); $quiz = $quizgenerator->create_instance(array('course'=>$SITE->id, 'questionsperpage' => 0, 'grade' => 100.0, 'sumgrades' => 2)); // Create a couple of questions. $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); $cat = $questiongenerator->create_question_category(); $saq = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id)); $numq = $questiongenerator->create_question('numerical', null, array('category' => $cat->id)); // Add them to the quiz. quiz_add_quiz_question($saq->id, $quiz); quiz_add_quiz_question($numq->id, $quiz); // Make a user to do the quiz. $user1 = $this->getDataGenerator()->create_user(); $quizobj = quiz::create($quiz->id, $user1->id); // Start the attempt. $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context()); $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour); $timenow = time(); $attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $user1->id); quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow); $this->assertEquals('1,2,0', $attempt->layout); quiz_attempt_save_started($quizobj, $quba, $attempt); // Process some responses from the student. $attemptobj = quiz_attempt::create($attempt->id); $this->assertFalse($attemptobj->has_response_to_at_least_one_graded_question()); $prefix1 = $quba->get_field_prefix(1); $prefix2 = $quba->get_field_prefix(2); $tosubmit = array(1 => array('answer' => 'frog'), 2 => array('answer' => '3.14')); $attemptobj->process_submitted_actions($timenow, false, $tosubmit); // Finish the attempt. $attemptobj = quiz_attempt::create($attempt->id); $this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question()); $attemptobj->process_finish($timenow, false); // Re-load quiz attempt data. $attemptobj = quiz_attempt::create($attempt->id); // Check that results are stored as expected. $this->assertEquals(1, $attemptobj->get_attempt_number()); $this->assertEquals(2, $attemptobj->get_sum_marks()); $this->assertEquals(true, $attemptobj->is_finished()); $this->assertEquals($timenow, $attemptobj->get_submitted_date()); $this->assertEquals($user1->id, $attemptobj->get_userid()); $this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question()); // Check quiz grades. $grades = quiz_get_user_grades($quiz, $user1->id); $grade = array_shift($grades); $this->assertEquals(100.0, $grade->rawgrade); // Check grade book. $gradebookgrades = grade_get_grades($SITE->id, 'mod', 'quiz', $quiz->id, $user1->id); $gradebookitem = array_shift($gradebookgrades->items); $gradebookgrade = array_shift($gradebookitem->grades); $this->assertEquals(100, $gradebookgrade->grade);
> } } > > /** /** > * Create a quiz containing one question and a close time. * Create a quiz with a random as well as other questions and walk through quiz attempts. > * */ > * The question is the standard shortanswer test question. public function test_quiz_with_random_question_attempt_walkthrough() { > * The quiz is set to close 1 hour from now. global $SITE; > * The quiz is set to use a grade period of 1 hour once time expires. > * $this->resetAfterTest(true); > * @return \stdClass the quiz that was created. question_bank::get_qtype('random')->clear_caches_before_testing(); > */ > protected function create_quiz_with_one_question(): \stdClass { $this->setAdminUser(); > global $SITE; > $this->resetAfterTest(); // Make a quiz. > $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz'); > // Make a quiz. > $timeclose = time() + HOURSECS; $quiz = $quizgenerator->create_instance(array('course' => $SITE->id, 'questionsperpage' => 2, 'grade' => 100.0, > $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz'); 'sumgrades' => 4)); > > $quiz = $quizgenerator->create_instance( $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); > ['course' => $SITE->id, 'timeclose' => $timeclose, > 'overduehandling' => 'graceperiod', 'graceperiod' => HOURSECS]); // Add two questions to question category. > $cat = $questiongenerator->create_question_category(); > // Create a question. $saq = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id)); > /** @var \core_question_generator $questiongenerator */ $numq = $questiongenerator->create_question('numerical', null, array('category' => $cat->id)); > $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); > $cat = $questiongenerator->create_question_category(); // Add random question to the quiz. > $saq = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id)); quiz_add_random_questions($quiz, 0, $cat->id, 1, false); > > // Add them to the quiz. // Make another category. > quiz_add_quiz_question($saq->id, $quiz, 0, 1); $cat2 = $questiongenerator->create_question_category(); > quiz_update_sumgrades($quiz); $match = $questiongenerator->create_question('match', null, array('category' => $cat->id)); > > return $quiz; quiz_add_quiz_question($match->id, $quiz, 0); > } > $multichoicemulti = $questiongenerator->create_question('multichoice', 'two_of_four', array('category' => $cat->id)); > public function test_quiz_attempt_walkthrough_submit_time_recorded_correctly_when_overdue() { > quiz_add_quiz_question($multichoicemulti->id, $quiz, 0); > $quiz = $this->create_quiz_with_one_question(); > $multichoicesingle = $questiongenerator->create_question('multichoice', 'one_of_four', array('category' => $cat->id)); > // Make a user to do the quiz. > $user = $this->getDataGenerator()->create_user(); quiz_add_quiz_question($multichoicesingle->id, $quiz, 0); > $this->setUser($user); > $quizobj = quiz::create($quiz->id, $user->id); foreach (array($saq->id => 'frog', $numq->id => '3.14') as $randomqidtoselect => $randqanswer) { > // Make a new user to do the quiz each loop. > // Start the attempt. $user1 = $this->getDataGenerator()->create_user(); > $attempt = quiz_prepare_and_start_new_attempt($quizobj, 1, null); $this->setUser($user1); > > // Process some responses from the student. $quizobj = quiz::create($quiz->id, $user1->id); > $attemptobj = quiz_attempt::create($attempt->id); > $attemptobj->process_submitted_actions($quiz->timeclose - 30 * MINSECS, false, [1 => ['answer' => 'frog']]); // Start the attempt. > $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context()); > // Attempt goes overdue (e.g. if cron ran). $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour); > $attemptobj = quiz_attempt::create($attempt->id); > $attemptobj->process_going_overdue($quiz->timeclose + 2 * get_config('quiz', 'graceperiodmin'), false); $timenow = time(); > $attempt = quiz_create_attempt($quizobj, 1, false, $timenow); > // Verify the attempt state. > $attemptobj = quiz_attempt::create($attempt->id); quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow, array(1 => $randomqidtoselect)); > $this->assertEquals(1, $attemptobj->get_attempt_number()); $this->assertEquals('1,2,0,3,4,0', $attempt->layout); > $this->assertEquals(false, $attemptobj->is_finished()); > $this->assertEquals(0, $attemptobj->get_submitted_date()); quiz_attempt_save_started($quizobj, $quba, $attempt); > $this->assertEquals($user->id, $attemptobj->get_userid()); > $this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question()); // Process some responses from the student. > $attemptobj = quiz_attempt::create($attempt->id); > // Student submits the attempt during the grace period. $this->assertFalse($attemptobj->has_response_to_at_least_one_graded_question()); > $attemptobj = quiz_attempt::create($attempt->id); > $attemptobj->process_attempt($quiz->timeclose + 30 * MINSECS, true, false, 1); $tosubmit = array(); > $selectedquestionid = $quba->get_question_attempt(1)->get_question_id(); > // Verify the attempt state. $tosubmit[1] = array('answer' => $randqanswer); > $attemptobj = quiz_attempt::create($attempt->id); $tosubmit[2] = array( > $this->assertEquals(1, $attemptobj->get_attempt_number()); 'frog' => 'amphibian', > $this->assertEquals(true, $attemptobj->is_finished()); 'cat' => 'mammal', > $this->assertEquals($quiz->timeclose + 30 * MINSECS, $attemptobj->get_submitted_date()); 'newt' => 'amphibian'); > $this->assertEquals($user->id, $attemptobj->get_userid()); $tosubmit[3] = array('One' => '1', 'Two' => '0', 'Three' => '1', 'Four' => '0'); // First and third choice. > $this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question()); $tosubmit[4] = array('answer' => 'One'); // The first choice. > } > $attemptobj->process_submitted_actions($timenow, false, $tosubmit); > public function test_quiz_attempt_walkthrough_close_time_extended_at_last_minute() { > global $DB; // Finish the attempt. > $attemptobj = quiz_attempt::create($attempt->id); > $quiz = $this->create_quiz_with_one_question(); $this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question()); > $originaltimeclose = $quiz->timeclose; $attemptobj->process_finish($timenow, false); > > // Make a user to do the quiz. // Re-load quiz attempt data. > $user = $this->getDataGenerator()->create_user(); $attemptobj = quiz_attempt::create($attempt->id); > $this->setUser($user); > $quizobj = quiz::create($quiz->id, $user->id); // Check that results are stored as expected. > $this->assertEquals(1, $attemptobj->get_attempt_number()); > // Start the attempt. $this->assertEquals(4, $attemptobj->get_sum_marks()); > $attempt = quiz_prepare_and_start_new_attempt($quizobj, 1, null); $this->assertEquals(true, $attemptobj->is_finished()); > $this->assertEquals($timenow, $attemptobj->get_submitted_date()); > // Process some responses from the student during the attempt. $this->assertEquals($user1->id, $attemptobj->get_userid()); > $attemptobj = quiz_attempt::create($attempt->id); $this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question()); > $attemptobj->process_submitted_actions($originaltimeclose - 30 * MINSECS, false, [1 => ['answer' => 'frog']]); > // Check quiz grades. > // Teacher edits the quiz to extend the time-limit by one minute. $grades = quiz_get_user_grades($quiz, $user1->id); > $DB->set_field('quiz', 'timeclose', $originaltimeclose + MINSECS, ['id' => $quiz->id]); $grade = array_shift($grades); > \course_modinfo::clear_instance_cache($quiz->course); $this->assertEquals(100.0, $grade->rawgrade); > > // Timer expires in the student browser and thinks it is time to submit the quiz. // Check grade book. > // This sets $finishattempt to false - since the student did not click the button, and $timeup to true. $gradebookgrades = grade_get_grades($SITE->id, 'mod', 'quiz', $quiz->id, $user1->id); > $attemptobj = quiz_attempt::create($attempt->id); $gradebookitem = array_shift($gradebookgrades->items); > $attemptobj->process_attempt($originaltimeclose, false, true, 1); $gradebookgrade = array_shift($gradebookitem->grades); > $this->assertEquals(100, $gradebookgrade->grade); > // Verify the attempt state - the $timeup was ignored becuase things have changed server-side. } > $attemptobj = quiz_attempt::create($attempt->id); } > $this->assertEquals(1, $attemptobj->get_attempt_number()); > $this->assertFalse($attemptobj->is_finished()); > $this->assertEquals(quiz_attempt::IN_PROGRESS, $attemptobj->get_state()); public function get_correct_response_for_variants() { > $this->assertEquals(0, $attemptobj->get_submitted_date()); return array(array(1, 9.9), array(2, 8.5), array(5, 14.2), array(10, 6.8, true)); > $this->assertEquals($user->id, $attemptobj->get_userid());
} protected $quizwithvariants = null; /** * Create a quiz with a single question with variants and walk through quiz attempts. * * @dataProvider get_correct_response_for_variants */ public function test_quiz_with_question_with_variants_attempt_walkthrough($variantno, $correctresponse, $done = false) { global $SITE; $this->resetAfterTest($done); $this->setAdminUser(); if ($this->quizwithvariants === null) { // Make a quiz. $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz'); $this->quizwithvariants = $quizgenerator->create_instance(array('course'=>$SITE->id, 'questionsperpage' => 0, 'grade' => 100.0, 'sumgrades' => 1)); $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); $cat = $questiongenerator->create_question_category(); $calc = $questiongenerator->create_question('calculatedsimple', 'sumwithvariants', array('category' => $cat->id)); quiz_add_quiz_question($calc->id, $this->quizwithvariants, 0); } // Make a new user to do the quiz. $user1 = $this->getDataGenerator()->create_user(); $this->setUser($user1); $quizobj = quiz::create($this->quizwithvariants->id, $user1->id); // Start the attempt. $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context()); $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour); $timenow = time(); $attempt = quiz_create_attempt($quizobj, 1, false, $timenow); // Select variant. quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow, array(), array(1 => $variantno)); $this->assertEquals('1,0', $attempt->layout); quiz_attempt_save_started($quizobj, $quba, $attempt); // Process some responses from the student. $attemptobj = quiz_attempt::create($attempt->id); $this->assertFalse($attemptobj->has_response_to_at_least_one_graded_question()); $tosubmit = array(1 => array('answer' => $correctresponse)); $attemptobj->process_submitted_actions($timenow, false, $tosubmit); // Finish the attempt. $attemptobj = quiz_attempt::create($attempt->id); $this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question()); $attemptobj->process_finish($timenow, false); // Re-load quiz attempt data. $attemptobj = quiz_attempt::create($attempt->id); // Check that results are stored as expected. $this->assertEquals(1, $attemptobj->get_attempt_number()); $this->assertEquals(1, $attemptobj->get_sum_marks()); $this->assertEquals(true, $attemptobj->is_finished()); $this->assertEquals($timenow, $attemptobj->get_submitted_date()); $this->assertEquals($user1->id, $attemptobj->get_userid()); $this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question()); // Check quiz grades. $grades = quiz_get_user_grades($this->quizwithvariants, $user1->id); $grade = array_shift($grades); $this->assertEquals(100.0, $grade->rawgrade); // Check grade book. $gradebookgrades = grade_get_grades($SITE->id, 'mod', 'quiz', $this->quizwithvariants->id, $user1->id); $gradebookitem = array_shift($gradebookgrades->items); $gradebookgrade = array_shift($gradebookitem->grades); $this->assertEquals(100, $gradebookgrade->grade); } }