From 8dc015050e638887bce8d7e6827dcfe8c0ef1fd2 Mon Sep 17 00:00:00 2001 From: Huong Nguyen Date: Mon, 9 Sep 2019 14:55:39 +0700 Subject: [PATCH] MDL-66633 quiz: allow to create attempts for different users By adding a new optional param to quiz_prepare_and_start_new_attempt() it can be used by the surrounding code to create an attempt for anybody else (different from current $USER) quiz attempt API should let you create an attempt for a different user --- mod/quiz/locallib.php | 13 ++++++++--- mod/quiz/tests/attempt_test.php | 48 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/mod/quiz/locallib.php b/mod/quiz/locallib.php index 12b54ecb9b1..aee52a7fc87 100644 --- a/mod/quiz/locallib.php +++ b/mod/quiz/locallib.php @@ -2403,22 +2403,29 @@ function quiz_validate_new_attempt(quiz $quizobj, quiz_access_manager $accessman * to force the choice of a particular actual question. Intended for testing purposes only. * @param array $forcedvariants slot number => variant. Used for questions with variants, * to force the choice of a particular variant. Intended for testing purposes only. + * @param int $userid Specific user id to create an attempt for that user, null for current logged in user * @return object the new attempt * @since Moodle 3.1 */ function quiz_prepare_and_start_new_attempt(quiz $quizobj, $attemptnumber, $lastattempt, - $offlineattempt = false, $forcedrandomquestions = [], $forcedvariants = []) { + $offlineattempt = false, $forcedrandomquestions = [], $forcedvariants = [], $userid = null) { global $DB, $USER; + $ispreviewuser = false; + // Delete any previous preview attempts belonging to this user. - quiz_delete_previews($quizobj->get_quiz(), $USER->id); + if ($userid === null) { + $userid = $USER->id; + $ispreviewuser = $quizobj->is_preview_user(); + } + quiz_delete_previews($quizobj->get_quiz(), $userid); $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context()); $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour); // Create the new attempt and initialize the question sessions $timenow = time(); // Update time now, in case the server is running really slowly. - $attempt = quiz_create_attempt($quizobj, $attemptnumber, $lastattempt, $timenow, $quizobj->is_preview_user()); + $attempt = quiz_create_attempt($quizobj, $attemptnumber, $lastattempt, $timenow, $ispreviewuser, $userid); if (!($quizobj->get_quiz()->attemptonlast && $lastattempt)) { $attempt = quiz_start_new_attempt($quizobj, $quba, $attempt, $attemptnumber, $timenow, diff --git a/mod/quiz/tests/attempt_test.php b/mod/quiz/tests/attempt_test.php index 96e41b45a5e..91b68e0b7e3 100644 --- a/mod/quiz/tests/attempt_test.php +++ b/mod/quiz/tests/attempt_test.php @@ -311,4 +311,52 @@ class mod_quiz_attempt_testcase extends advanced_testcase { $this->assertEquals(true, $quizobj->is_participant($USER->id), 'Admin is enrolled, suspended and can participate'); } + + /** + * Test quiz_prepare_and_start_new_attempt function + */ + public function test_quiz_prepare_and_start_new_attempt() { + $this->resetAfterTest(); + + // Create course. + $course = $this->getDataGenerator()->create_course(); + // Create students. + $student1 = $this->getDataGenerator()->create_and_enrol($course, 'student'); + $student2 = $this->getDataGenerator()->create_and_enrol($course, 'student'); + // Create quiz. + $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz'); + $quiz = $quizgenerator->create_instance(['course' => $course->id, 'grade' => 100.0, 'sumgrades' => 2, 'layout' => '1,0']); + // Create question and add it to quiz. + $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); + $cat = $questiongenerator->create_question_category(); + $question = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]); + quiz_add_quiz_question($question->id, $quiz, 1); + + $quizobj = quiz::create($quiz->id); + + // Login as student1. + $this->setUser($student1); + // Create attempt for student1. + $attempt = quiz_prepare_and_start_new_attempt($quizobj, 1, null, false, [], []); + $this->assertEquals($student1->id, $attempt->userid); + $this->assertEquals(0, $attempt->preview); + + // Login as student2. + $this->setUser($student2); + // Create attempt for student2. + $attempt = quiz_prepare_and_start_new_attempt($quizobj, 1, null, false, [], []); + $this->assertEquals($student2->id, $attempt->userid); + $this->assertEquals(0, $attempt->preview); + + // Login as admin. + $this->setAdminUser(); + // Create attempt for student1. + $attempt = quiz_prepare_and_start_new_attempt($quizobj, 2, null, false, [], [], $student1->id); + $this->assertEquals($student1->id, $attempt->userid); + $this->assertEquals(0, $attempt->preview); + // Create attempt for student2. + $attempt = quiz_prepare_and_start_new_attempt($quizobj, 2, null, false, [], [], $student2->id); + $this->assertEquals($student2->id, $attempt->userid); + $this->assertEquals(0, $attempt->preview); + } } -- 2.11.4.GIT