mirror of
https://github.com/moodle/moodle.git
synced 2025-04-24 18:04:43 +02:00
MDL-66633 Quiz: Improve preview user checking
This commit is contained in:
parent
5a14a1c422
commit
0fd6032ce8
mod/quiz
@ -2411,13 +2411,13 @@ function quiz_prepare_and_start_new_attempt(quiz $quizobj, $attemptnumber, $last
|
||||
$offlineattempt = false, $forcedrandomquestions = [], $forcedvariants = [], $userid = null) {
|
||||
global $DB, $USER;
|
||||
|
||||
$ispreviewuser = false;
|
||||
|
||||
// Delete any previous preview attempts belonging to this user.
|
||||
if ($userid === null) {
|
||||
$userid = $USER->id;
|
||||
$ispreviewuser = $quizobj->is_preview_user();
|
||||
} else {
|
||||
$ispreviewuser = has_capability('mod/quiz:preview', $quizobj->get_context(), $userid);
|
||||
}
|
||||
// Delete any previous preview attempts belonging to this user.
|
||||
quiz_delete_previews($quizobj->get_quiz(), $userid);
|
||||
|
||||
$quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
|
||||
|
@ -316,6 +316,7 @@ class mod_quiz_attempt_testcase extends advanced_testcase {
|
||||
* Test quiz_prepare_and_start_new_attempt function
|
||||
*/
|
||||
public function test_quiz_prepare_and_start_new_attempt() {
|
||||
global $USER;
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Create course.
|
||||
@ -358,5 +359,9 @@ class mod_quiz_attempt_testcase extends advanced_testcase {
|
||||
$attempt = quiz_prepare_and_start_new_attempt($quizobj, 2, null, false, [], [], $student2->id);
|
||||
$this->assertEquals($student2->id, $attempt->userid);
|
||||
$this->assertEquals(0, $attempt->preview);
|
||||
// Create attempt for user id that the same with current $USER->id.
|
||||
$attempt = quiz_prepare_and_start_new_attempt($quizobj, 2, null, false, [], [], $USER->id);
|
||||
$this->assertEquals($USER->id, $attempt->userid);
|
||||
$this->assertEquals(1, $attempt->preview);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user