MDL-85069 question: Fix return URL parameters after moving questions

When calling the core_question_move_questions external function, the
returned $returnurl paramater kept the original `cat`, `category` and
`lastchanged` parameters.

If the question was moved to another question bank, this meant the user
would be redirected to that bank, but the URL would still refer to the
category in the original bank. Opening the bulk move dialogue again
would then show the original bank and category selected by default, not
the current one.

This change ensures the cat parameter is updated to match the new
category, unsets the `category` parameter as it is redundant. If a single
question is moved, "lastchanged" is updated to that question, otherwise
it is unset.
This commit is contained in:
Mark Johnson
2025-06-30 15:27:36 +01:00
parent 043980d193
commit 3f4478a7b7
2 changed files with 47 additions and 1 deletions
@@ -122,6 +122,9 @@ Feature: Use the qbank plugin manager page for bulkmove
And I should see "Question 1"
And I should see "Question 2"
And I should not see "Question 3"
# No questions are highlighted when bulk-moved.
And the "class" attribute of "Question 1" "table_row" should not contain "highlight"
And the "class" attribute of "Question 2" "table_row" should not contain "highlight"
@javascript
Scenario: Unable to bulk move questions from history page
@@ -131,3 +134,37 @@ Feature: Use the qbank plugin manager page for bulkmove
And I click on "With selected" "button"
Then I should see question bulk action "deleteselected"
And I should not see question bulk action "move"
@javascript
Scenario: Questions can be moved to a different bank
Given I am on the "Test quiz" "mod_quiz > question bank" page logged in as "teacher1"
And I press "Create a new question ..."
And I set the field "item_qtype_truefalse" to "1"
# Manually create a new question so additional parameters are included in the URL, and we can test they are handled correctly
# during the move operation.
And I click on "Add" "button" in the "Choose a question type to add" "dialogue"
And I set the following fields to these values:
| Question name | Seventh question |
| Question text | test |
And I press "id_submitbutton"
And I click on "Seventh question" "checkbox"
And I click on "With selected" "button"
And I click on "move" "button"
And the field "searchbanks" matches value "C1 - Test quiz"
And the field "searchcategories" matches value "Test questions 1"
And I open the autocomplete suggestions list in the ".search-banks" "css_element"
And I click on "C1 - Question bank 1" item in the autocomplete list
And I open the autocomplete suggestions list in the ".search-categories" "css_element"
And I click on "Test questions 2" item in the autocomplete list
And I click on "Move questions" "button"
And I should see "Are you sure you want to move these questions?"
When I click on "Confirm" "button"
Then I should see "Questions successfully moved"
# The move dialogue should default to the new bank and category.
And I click on "Seventh question" "checkbox"
And I click on "With selected" "button"
And I click on "move" "button"
And the field "searchbanks" matches value "C1 - Question bank 1"
And the field "searchcategories" matches value "Test questions 2"
# The moved question should be highlighted
And the "class" attribute of "Seventh question" "table_row" should contain "highlight"
+10 -1
View File
@@ -47,7 +47,7 @@ class move_questions extends external_api {
'newcontextid' => new external_value(PARAM_INT, 'Contextid of the target question bank'),
'newcategoryid' => new external_value(PARAM_INT, 'ID of the target question category'),
'questionids' => new external_value(PARAM_SEQUENCE, 'Comma separated list of question ids to move'),
'returnurl' => new external_value(PARAM_URL,
'returnurl' => new external_value(PARAM_LOCALURL,
desc: 'A URL to add/update the filter param with the new category',
default: ''
),
@@ -112,6 +112,15 @@ class move_questions extends external_api {
if ($returnurlstring) {
$returnurl = new moodle_url($returnurlstring);
$returnurl->param('cmid', $newcontext->instanceid);
$returnurl->param('cat', "{$newcategoryid},{$newcontextid}");
$returnurl->remove_params('category');
// We can only highlight 1 question, so only highlight if we're moving a single question.
$qids = explode(',', $questionids);
if (count($qids) === 1) {
$returnurl->param('lastchanged', reset($qids));
} else {
$returnurl->remove_params('lastchanged');
};
$filter = $returnurl->param('filter');
if ($filter) {
$returnfilters = filter_condition_manager::update_filter_param_to_category(