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.
This commit is contained in:
Anupama Sarjoshi 2022-12-09 18:52:51 +00:00
parent 6b24f59302
commit fe094a3705
3 changed files with 23 additions and 17 deletions

View File

@ -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"

View File

@ -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);

View File

@ -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 "   Default for C1"
And I set the following fields to these values: