Skip to content

Commit

Permalink
MDL-76026 Question bank: Fix errors related to categories renaming
Browse files Browse the repository at this point in the history
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.0 and later versions, 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.
  • Loading branch information
AnupamaSarjoshi committed Dec 13, 2022
1 parent 40a89d8 commit 89287ec
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
19 changes: 19 additions & 0 deletions mod/quiz/tests/behat/random_question.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -474,23 +474,6 @@ public function update_category($updateid, $newparent, $newname, $newinfo, $newi
$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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 89287ec

Please sign in to comment.