Skip to content

Commit

Permalink
MDL-66633 quiz: allow to create attempts for different users
Browse files Browse the repository at this point in the history
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
  • Loading branch information
HuongNV13 authored and stronk7 committed Sep 18, 2019
1 parent 7424134 commit 8dc0150
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
13 changes: 10 additions & 3 deletions mod/quiz/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
48 changes: 48 additions & 0 deletions mod/quiz/tests/attempt_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,52 @@ public function test_is_participant() {
$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);
}
}

0 comments on commit 8dc0150

Please sign in to comment.