diff --git a/backup/moodle2/restore_stepslib.php b/backup/moodle2/restore_stepslib.php index d56e5d6b7c0..e61c2d139d8 100644 --- a/backup/moodle2/restore_stepslib.php +++ b/backup/moodle2/restore_stepslib.php @@ -5293,6 +5293,7 @@ class restore_move_module_questions_categories extends restore_execution_step { AND parentitemid = ?", array($this->get_restoreid(), $contextid)); $top = question_get_top_category($newcontext->newitemid, true); $oldtopid = 0; + $categoryids = []; foreach ($modulecats as $modulecat) { // Before 3.5, question categories could be created at top level. // From 3.5 onwards, all question categories should be a child of a special category called the "top" category. @@ -5308,6 +5309,7 @@ class restore_move_module_questions_categories extends restore_execution_step { $cat->parent = $top->id; } $DB->update_record('question_categories', $cat); + $categoryids[] = (int)$cat->id; } // And set new contextid (and maybe update newitemid) also in question_category mapping (will be @@ -5316,6 +5318,30 @@ class restore_move_module_questions_categories extends restore_execution_step { $modulecat->newitemid, $newcontext->newitemid); } + // Update the context id of any tags applied to any questions in these categories. + if ($categoryids) { + [$categorysql, $categoryidparams] = $DB->get_in_or_equal($categoryids, SQL_PARAMS_NAMED); + $sqlupdate = "UPDATE {tag_instance} + SET contextid = :newcontext + WHERE component = :component + AND itemtype = :itemtype + AND itemid IN (SELECT DISTINCT bi.newitemid as questionid + FROM {backup_ids_temp} bi + JOIN {question} q ON q.id = bi.newitemid + JOIN {question_versions} qv ON qv.questionid = q.id + JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid + WHERE bi.backupid = :backupid AND bi.itemname = 'question_created' + AND qbe.questioncategoryid {$categorysql}) "; + $params = [ + 'newcontext' => $newcontext->newitemid, + 'component' => 'core_question', + 'itemtype' => 'question', + 'backupid' => $this->get_restoreid(), + ]; + $params += $categoryidparams; + $DB->execute($sqlupdate, $params); + } + // Now set the parent id for the question categories that were in the top category in the course context // and have been moved now. if ($oldtopid) { diff --git a/backup/moodle2/tests/behat/backup_restore_question_tags.feature b/backup/moodle2/tests/behat/backup_restore_question_tags.feature new file mode 100644 index 00000000000..1aa62ca18e1 --- /dev/null +++ b/backup/moodle2/tests/behat/backup_restore_question_tags.feature @@ -0,0 +1,24 @@ +@core @core_backup +Feature: Backup and restore of the question that was tagged + + Background: + Given the following "courses" exist: + | fullname | shortname | category | + | Course 1 | C1 | 0 | + + @javascript @_file_upload + Scenario: Restore the quiz containing the question that was tagged + Given I am on the "Course 1" "restore" page logged in as "admin" + And I press "Manage backup files" + And I upload "backup/moodle2/tests/fixtures/test_tags_backup.mbz" file to "Files" filemanager + And I press "Save changes" + And I restore "test_tags_backup.mbz" backup into a new course using this options: + | Schema | Course name | Course 2 | + | Schema | Course short name | C2 | + When I am on the "TF1" "core_question > edit" page logged in as admin + And I expand all fieldsets + Then I should see "Tag1-TF1" + And I should see "Tag2-TF1" + And I am on the "TF2" "core_question > edit" page logged in as admin + And I expand all fieldsets + And I should see "Tag1-TF2" diff --git a/backup/moodle2/tests/fixtures/test_tags_backup.mbz b/backup/moodle2/tests/fixtures/test_tags_backup.mbz new file mode 100644 index 00000000000..9df52316a49 Binary files /dev/null and b/backup/moodle2/tests/fixtures/test_tags_backup.mbz differ