Skip to content

Commit

Permalink
MDL-76193 Questions: Backup and Restore with tags fails
Browse files Browse the repository at this point in the history
  • Loading branch information
JBThong committed Dec 5, 2022
1 parent 1d863c3 commit 79d1b45
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
26 changes: 26 additions & 0 deletions backup/moodle2/restore_stepslib.php
Expand Up @@ -5293,6 +5293,7 @@ protected function define_execution() {
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.
Expand All @@ -5308,6 +5309,7 @@ protected function define_execution() {
$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
Expand All @@ -5316,6 +5318,30 @@ protected function define_execution() {
$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) {
Expand Down
24 changes: 24 additions & 0 deletions 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"
Binary file not shown.

0 comments on commit 79d1b45

Please sign in to comment.