From 0fcea11234a9d5016e7c55fa8e51621ac218a878 Mon Sep 17 00:00:00 2001 From: Safat Shahin Date: Tue, 17 May 2022 09:30:44 +1000 Subject: [PATCH] MDL-74768 core_question: Fix for question category movement --- lib/questionlib.php | 3 -- mod/quiz/tests/behat/random_question.feature | 53 +++++++++++++++++++ .../classes/question_category_object.php | 2 + 3 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 mod/quiz/tests/behat/random_question.feature diff --git a/lib/questionlib.php b/lib/questionlib.php index abfda93f9b7..19de3ddc505 100644 --- a/lib/questionlib.php +++ b/lib/questionlib.php @@ -721,9 +721,6 @@ function question_move_questions_to_category($questionids, $newcategoryid): bool question_bank::get_qtype($question->qtype)->move_files( $question->id, $question->contextid, $newcategorydata->contextid); } - // Move set_reference records to new category. - move_question_set_references($question->category, $newcategoryid, - $question->contextid, $newcategorydata->contextid, true); // Check whether there could be a clash of idnumbers in the new category. list($idnumberclash, $rec) = idnumber_exist_in_question_category($question->idnumber, $newcategoryid); if ($idnumberclash) { diff --git a/mod/quiz/tests/behat/random_question.feature b/mod/quiz/tests/behat/random_question.feature new file mode 100644 index 00000000000..3aa05a7b9fb --- /dev/null +++ b/mod/quiz/tests/behat/random_question.feature @@ -0,0 +1,53 @@ +@mod @mod_quiz + +Feature: Moving a question to another category should not affect random questions in a quiz + In order for a quiz with random questions to work as expected + Teachers should be able to + Move a question to a different category without affecting the category the random questions in the quiz reference to + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | + | teacher1 | Teacher | 1 | teacher1@example.com | + And the following "courses" exist: + | fullname | shortname | format | + | Course 1 | C1 | weeks | + And the following "course enrolments" exist: + | user | course | role | + | teacher1 | C1 | editingteacher | + And the following "activities" exist: + | activity | name | intro | course | idnumber | + | quiz | Quiz 1 | Quiz 1 for testing the Add random question form | C1 | quiz1 | + And the following "question categories" exist: + | contextlevel | reference | questioncategory | name | + | Course | C1 | Top | top | + | Course | C1 | top | Default for C1 | + | Course | C1 | Default for C1 | Subcategory | + | Course | C1 | top | Used category | + And the following "questions" exist: + | questioncategory | qtype | name | questiontext | + | Used category | essay | Test question to be moved | Write about whatever you want | + + @javascript + Scenario: Moving a question should not change the random question + Given I am on the "Quiz 1" "mod_quiz > Edit" page logged in as "teacher1" + When I open the "last" add to quiz menu + And I follow "a random question" + And I set the field "Category" to "Used category" + And I press "Add random question" + And I should see "Random (Used category)" on quiz page "1" + And I click on "(See questions)" "link" + And I should see "Used category" + And I click on "Test question to be moved" "checkbox" in the "Test question to be moved" "table_row" + And I click on "With selected" "button" + And I click on question bulk action "move" + And I set the field "Question category" to "Subcategory" + And I press "Move to" + Then I should see "Test question to be moved" + And the field "Select a category" matches value "      Subcategory (1)" + And the "Select a category" select box should contain "Used category" + And the "Select a category" select box should not contain "Used category (1)" + And I am on the "Quiz 1" "mod_quiz > Edit" page + And I should see "Random (Used category)" on quiz page "1" + And I click on "(See questions)" "link" + And I should see "Used category" diff --git a/question/bank/managecategories/classes/question_category_object.php b/question/bank/managecategories/classes/question_category_object.php index b84dfcc288d..da067490321 100644 --- a/question/bank/managecategories/classes/question_category_object.php +++ b/question/bank/managecategories/classes/question_category_object.php @@ -467,6 +467,8 @@ class question_category_object { $cat->stamp = make_unique_id_code(); } $DB->update_record('question_categories', $cat); + // Update the set_reference records when moving a category to a different context. + move_question_set_references($cat->id, $cat->id, $oldcat->contextid, $tocontextid); // Log the update of this category. $event = \core\event\question_category_updated::create_from_question_category_instance($cat);