From 57682b43ae24b178279ce8670773b193e49021d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luca=20B=C3=B6sch?= Date: Tue, 22 Nov 2022 15:21:49 +0100 Subject: [PATCH] MDL-76375 quiz generator: Allow more than one attempt by Behat tests. --- mod/quiz/tests/behat/reattempt_quiz.feature | 55 +++++++++++++++++++++ mod/quiz/tests/generator/lib.php | 17 ++++--- 2 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 mod/quiz/tests/behat/reattempt_quiz.feature diff --git a/mod/quiz/tests/behat/reattempt_quiz.feature b/mod/quiz/tests/behat/reattempt_quiz.feature new file mode 100644 index 0000000000000..2680b62ea9c4e --- /dev/null +++ b/mod/quiz/tests/behat/reattempt_quiz.feature @@ -0,0 +1,55 @@ +@mod @mod_quiz +Feature: Several attempts in a quiz + As a student + In order to demonstrate what I know + I need to be able to attempt quizzes and sometimes take multiple attempts + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | + | student1 | Student | One | student1@example.com | + | student2 | Student | One | student2@example.com | + | teacher | Teacher | One | teacher@example.com | + And the following "courses" exist: + | fullname | shortname | category | + | Course 1 | C1 | 0 | + And the following "course enrolments" exist: + | user | course | role | + | student1 | C1 | student | + | student2 | C1 | student | + | teacher | C1 | teacher | + And the following "question categories" exist: + | contextlevel | reference | name | + | Course | C1 | Test questions | + And the following "questions" exist: + | questioncategory | qtype | name | questiontext | + | Test questions | truefalse | TF1 | First question | + | Test questions | truefalse | TF2 | Second question | + And the following "activities" exist: + | activity | name | intro | course | idnumber | preferredbehaviour | navmethod | + | quiz | Quiz 1 | Quiz 1 description | C1 | quiz1 | immediatefeedback | free | + And quiz "Quiz 1" contains the following questions: + | question | page | requireprevious | + | TF1 | 1 | 1 | + | TF2 | 2 | 1 | + # Add some attempts + And user "student1" has attempted "Quiz 1" with responses: + | slot | response | + | 1 | True | + | 2 | False | + And user "student2" has attempted "Quiz 1" with responses: + | slot | response | + | 1 | True | + | 2 | True | + # Add a second attempt by student1 + And user "student1" has attempted "Quiz 1" with responses: + | slot | response | + | 1 | False | + | 2 | False | + + @javascript + Scenario: The redo question buttons are visible after 2 attempts are preset for student1. + Given I am on the "Quiz 1" "mod_quiz > View" page logged in as "student1" + Then "Re-attempt quiz" "button" should exist + And "1" row "Marks / 2.00" column of "quizattemptsummary" table should contain "1.00" + And "2" row "Marks / 2.00" column of "quizattemptsummary" table should contain "0.00" diff --git a/mod/quiz/tests/generator/lib.php b/mod/quiz/tests/generator/lib.php index 2f3818d9b6a31..cccdaf2f9df0d 100644 --- a/mod/quiz/tests/generator/lib.php +++ b/mod/quiz/tests/generator/lib.php @@ -103,9 +103,6 @@ public function create_instance($record = null, array $options = null) { /** * Create a quiz attempt for a particular user at a particular course. * - * Currently this method can only create a first attempt for each - * user at each quiz. TODO remove this limitation. - * * @param int $quizid the quiz id (from the mdl_quit table, not cmid). * @param int $userid the user id. * @param array $forcedrandomquestions slot => questionid. Optional, @@ -120,12 +117,18 @@ public function create_attempt($quizid, $userid, array $forcedrandomquestions = // Build quiz object and load questions. $quizobj = quiz::create($quizid, $userid); - if (quiz_get_user_attempts($quizid, $userid, 'all', true)) { - throw new coding_exception('mod_quiz_generator is currently limited to only ' . - 'be able to create one attempt for each user. (This should be fixed.)'); + $attemptnumber = 1; + $attempt = null; + + if ($attempts = quiz_get_user_attempts($quizid, $userid, 'all', true)) { + // There is/are already an attempt/some attempts. + // Take the last attempt. + $attempt = end($attempts); + // Take the attempt number of the last attempt and increase it. + $attemptnumber = $attempt->attempt + 1; } - return quiz_prepare_and_start_new_attempt($quizobj, 1, null, false, + return quiz_prepare_and_start_new_attempt($quizobj, $attemptnumber, $attempt, false, $forcedrandomquestions, $forcedvariants); }