Skip to content

Commit

Permalink
MDL-76375 quiz generator: Allow more than one attempt by Behat tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaboesch committed Jan 4, 2023
1 parent 12e9d9e commit 57682b4
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
55 changes: 55 additions & 0 deletions 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"
17 changes: 10 additions & 7 deletions mod/quiz/tests/generator/lib.php
Expand Up @@ -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,
Expand All @@ -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);
}

Expand Down

0 comments on commit 57682b4

Please sign in to comment.