From fe094a3705ac57a6a99781f160ec697265d48363 Mon Sep 17 00:00:00 2001
From: Anupama Sarjoshi <anupama.sarjoshi@open.ac.uk>
Date: Fri, 9 Dec 2022 18:52:51 +0000
Subject: [PATCH] MDL-76026 Question bank: Fix errors related to categories
 renaming

This part of the code in 'question_category_object.php' 'update_category' method was used before Moodle 4.0 version for renaming
the random questions in an updated category. For Moodle 4.1, it is unnecessary as the details of random questions are no more
stored in 'question' table but in 'question_set_references' table. The method call move_question_set_references handles the same.
---
 mod/quiz/tests/behat/random_question.feature  | 19 +++++++++++++++++++
 .../classes/question_category_object.php      | 17 -----------------
 .../tests/behat/question_categories.feature   |  4 ++++
 3 files changed, 23 insertions(+), 17 deletions(-)

diff --git a/mod/quiz/tests/behat/random_question.feature b/mod/quiz/tests/behat/random_question.feature
index 3aa05a7b9fb..a6f3562640d 100644
--- a/mod/quiz/tests/behat/random_question.feature
+++ b/mod/quiz/tests/behat/random_question.feature
@@ -51,3 +51,22 @@ Feature: Moving a question to another category should not affect 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"
+
+  @javascript
+  Scenario: Renaming a random question category should update 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 am on the "Course 1" "core_question > course question categories" page
+    And I click on "Edit this category" "link" in the "Used category" "list_item"
+    And I set the following fields to these values:
+      | Name            | Used category new |
+      | Category info   | I was edited      |
+    And I press "Save changes"
+    Then I should see "Used category new"
+    And I should see "I was edited" in the "Used category new" "list_item"
+    And I am on the "Quiz 1" "mod_quiz > Edit" page
+    And I should see "Random (Used category new)" on quiz page "1"
diff --git a/question/bank/managecategories/classes/question_category_object.php b/question/bank/managecategories/classes/question_category_object.php
index da067490321..98f8c769bac 100644
--- a/question/bank/managecategories/classes/question_category_object.php
+++ b/question/bank/managecategories/classes/question_category_object.php
@@ -474,23 +474,6 @@ class question_category_object {
         $event = \core\event\question_category_updated::create_from_question_category_instance($cat);
         $event->trigger();
 
-        // If the category name has changed, rename any random questions in that category.
-        if ($oldcat->name != $cat->name) {
-            // Get the question ids for each question category.
-            $questionids = $this->get_real_question_ids_in_category($cat->id);
-
-            foreach ($questionids as $question) {
-                $where = "qtype = 'random' AND id = ? AND " . $DB->sql_compare_text('questiontext') . " = ?";
-
-                $randomqtype = question_bank::get_qtype('random');
-                $randomqname = $randomqtype->question_name($cat, false);
-                $DB->set_field_select('question', 'name', $randomqname, $where, [$question->id, '0']);
-
-                $randomqname = $randomqtype->question_name($cat, true);
-                $DB->set_field_select('question', 'name', $randomqname, $where, [$question->id, '1']);
-            }
-        }
-
         if ($oldcat->contextid != $tocontextid) {
             // Moving to a new context. Must move files belonging to questions.
             question_move_category_to_context($cat->id, $oldcat->contextid, $tocontextid);
diff --git a/question/bank/managecategories/tests/behat/question_categories.feature b/question/bank/managecategories/tests/behat/question_categories.feature
index 4b76c27e1ff..217754e092b 100644
--- a/question/bank/managecategories/tests/behat/question_categories.feature
+++ b/question/bank/managecategories/tests/behat/question_categories.feature
@@ -46,6 +46,10 @@ Feature: A teacher can put questions in categories in the question bank
 
   Scenario: A question category can be edited
     When I am on the "Course 1" "core_question > course question categories" page
+    # There have been bugs which only happened if a question category was not empty, so add a question.
+    And the following "questions" exist:
+      | questioncategory | qtype | name                                | questiontext                  |
+      | Subcategory      | essay | Test question for renaming category | Write about whatever you want |
     And I click on "Edit this category" "link" in the "Subcategory" "list_item"
     And the field "parent" matches value "&nbsp;&nbsp;&nbsp;Default for C1"
     And I set the following fields to these values: