From 959c34de04bb413ab5716ddab2fce6601acb4afd Mon Sep 17 00:00:00 2001 From: Amaia Anabitarte Date: Wed, 4 Jan 2023 16:58:04 +0100 Subject: [PATCH] MDL-76766 mod_quiz: Allow shuffling quizes with no questions --- mod/quiz/classes/event/section_shuffle_updated.php | 8 +------- mod/quiz/classes/structure.php | 4 +--- .../behat/editing_add_from_question_bank.feature | 11 +++++++++++ mod/quiz/tests/event/events_test.php | 1 - mod/quiz/upgrade.txt | 4 +++- 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/mod/quiz/classes/event/section_shuffle_updated.php b/mod/quiz/classes/event/section_shuffle_updated.php index 56aee42e15e..b43dba63597 100644 --- a/mod/quiz/classes/event/section_shuffle_updated.php +++ b/mod/quiz/classes/event/section_shuffle_updated.php @@ -32,7 +32,6 @@ namespace mod_quiz\event; * * - int quizid: the id of the quiz. * - bool shuffle: shuffle option value. - * - int firstslotid: id of the slot which is right after the section break. * - int firstslotnumber: slot number of the slot which is right after the section break. * } * @@ -53,7 +52,7 @@ class section_shuffle_updated extends \core\event\base { public function get_description() { return "The user with id '$this->userid' updated the section with id '{$this->objectid}' " . - "before the slot with id '{$this->other['firstslotid']}' and slot number '{$this->other['firstslotnumber']}' " . + "before the slot number '{$this->other['firstslotnumber']}' " . "belonging to the quiz with course module id '$this->contextinstanceid'. " . "Its shuffle option was set to '{$this->other['shuffle']}'."; } @@ -79,10 +78,6 @@ class section_shuffle_updated extends \core\event\base { throw new \coding_exception('The \'quizid\' value must be set in other.'); } - if (!isset($this->other['firstslotid'])) { - throw new \coding_exception('The \'firstslotid\' value must be set in other.'); - } - if (!isset($this->other['firstslotnumber'])) { throw new \coding_exception('The \'firstslotnumber\' value must be set in other.'); } @@ -100,7 +95,6 @@ class section_shuffle_updated extends \core\event\base { public static function get_other_mapping() { $othermapped = []; $othermapped['quizid'] = ['db' => 'quiz', 'restore' => 'quiz']; - $othermapped['firstslotid'] = ['db' => 'quiz_slots', 'restore' => 'quiz_question_instance']; return $othermapped; } diff --git a/mod/quiz/classes/structure.php b/mod/quiz/classes/structure.php index f6f0b309e2d..5b6e32ab027 100644 --- a/mod/quiz/classes/structure.php +++ b/mod/quiz/classes/structure.php @@ -1205,14 +1205,12 @@ class structure { $DB->update_record('quiz_sections', $section); // Log section shuffle updated event. - $firstslot = $DB->get_record('quiz_slots', array('quizid' => $this->get_quizid(), 'slot' => $section->firstslot)); $event = \mod_quiz\event\section_shuffle_updated::create([ 'context' => $this->quizobj->get_context(), 'objectid' => $id, 'other' => [ 'quizid' => $this->get_quizid(), - 'firstslotid' => $firstslot->id, - 'firstslotnumber' => $firstslot->slot, + 'firstslotnumber' => $section->firstslot, 'shuffle' => $shuffle ] ]); diff --git a/mod/quiz/tests/behat/editing_add_from_question_bank.feature b/mod/quiz/tests/behat/editing_add_from_question_bank.feature index bc3be4b4963..7630ad2e845 100644 --- a/mod/quiz/tests/behat/editing_add_from_question_bank.feature +++ b/mod/quiz/tests/behat/editing_add_from_question_bank.feature @@ -126,3 +126,14 @@ Feature: Adding questions to a quiz from the question bank Then "question 01 name" "text" should appear before "question 03 name" "text" And I follow "Sort by Question type descending" Then "question 03 name" "text" should appear before "question 01 name" "text" + + Scenario: Shuffle option could be set before adding any question to the quiz + Given the following "questions" exist: + | questioncategory | qtype | name | questiontext | + | Test questions | essay | question 03 name | question 03 text | + And I log in as "teacher1" + And I am on the "Quiz 1" "mod_quiz > Edit" page + When I set the field "Shuffle" to "1" + And I open the "last" add to quiz menu + And I follow "from question bank" + Then I should see "question 01 name" diff --git a/mod/quiz/tests/event/events_test.php b/mod/quiz/tests/event/events_test.php index 5229e87b96b..cce2d0fb7a3 100644 --- a/mod/quiz/tests/event/events_test.php +++ b/mod/quiz/tests/event/events_test.php @@ -1136,7 +1136,6 @@ class events_test extends \advanced_testcase { 'context' => context_module::instance($quizobj->get_cmid()), 'other' => [ 'quizid' => $quizobj->get_quizid(), - 'firstslotid' => 1, 'firstslotnumber' => 2, 'shuffle' => true ] diff --git a/mod/quiz/upgrade.txt b/mod/quiz/upgrade.txt index c412c99f6fa..558ed6f5e5b 100644 --- a/mod/quiz/upgrade.txt +++ b/mod/quiz/upgrade.txt @@ -1,10 +1,12 @@ This files describes API changes in the quiz code. +=== 4.1.1 === +* 'firstslotid' value is not used in section_shuffle_updated event anymore. + === 4.1 === * quiz_has_question_use is now deprecated. Use mod_quiz\structure::has_use_capability istead. - === 4.0.3, 4.1 === * If a quiz is sequential the external functions mod_quiz_external::get_attempt_data, mod_quiz_external::get_attempt_summary will not return any data