diff --git a/backup/moodle2/restore_stepslib.php b/backup/moodle2/restore_stepslib.php index 917438db1a54c..e7558090ea607 100644 --- a/backup/moodle2/restore_stepslib.php +++ b/backup/moodle2/restore_stepslib.php @@ -5388,6 +5388,7 @@ protected function define_execution() { $top = question_get_top_category($newcontext->newitemid, true); $oldtopid = 0; $categoryids = []; + $questioncategories = []; 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. @@ -5396,14 +5397,22 @@ protected function define_execution() { $oldtopid = $modulecat->newitemid; $modulecat->newitemid = $top->id; } else { - $cat = new stdClass(); - $cat->id = $modulecat->newitemid; - $cat->contextid = $newcontext->newitemid; - if (empty($info->parent)) { - $cat->parent = $top->id; + $catid = $modulecat->newitemid; + $cat = $DB->get_record('question_categories', ['id' => $catid]); + if ($cat) { + $oldcontextid = $cat->contextid; + $cat->contextid = $newcontext->newitemid; + if (empty($info->parent)) { + $cat->parent = $top->id; + } + $DB->update_record('question_categories', $cat); + $questioncategories[$catid] = [ + 'id' => $catid, + 'oldcontextid' => $oldcontextid, + 'newcontextid' => $newcontext->newitemid, + ]; } - $DB->update_record('question_categories', $cat); - $categoryids[] = (int)$cat->id; + $categoryids[] = (int)$catid; } // And set new contextid (and maybe update newitemid) also in question_category mapping (will be @@ -5434,6 +5443,19 @@ protected function define_execution() { ]; $params += $categoryidparams; $DB->execute($sqlupdate, $params); + + // Update questionscontextid in question_set_references. + foreach ($questioncategories as $questioncategory) { + $references = $DB->get_records('question_set_references', ['questionscontextid' => $oldcontextid]); + foreach ($references as $reference) { + $filtercondition = json_decode($reference->filtercondition); + if (!empty($filtercondition->questioncategoryid) + && $filtercondition->questioncategoryid == $questioncategory['id']) { + $reference->questionscontextid = $questioncategory['newcontextid']; + $DB->update_record('question_set_references', $reference); + } + } + } } // Now set the parent id for the question categories that were in the top category in the course context diff --git a/backup/moodle2/tests/behat/restore_random_question.feature b/backup/moodle2/tests/behat/restore_random_question.feature new file mode 100644 index 0000000000000..5485cb09d9f85 --- /dev/null +++ b/backup/moodle2/tests/behat/restore_random_question.feature @@ -0,0 +1,19 @@ +@core @core_backup +Feature: Restore random question + + Background: + Given the following "courses" exist: + | fullname | shortname | category | + | Course 1 | C1 | 0 | + + @javascript @_file_upload + Scenario: Restore the quiz from 3.9 containing random questions + Given I am on the "Course 1" "restore" page logged in as "admin" + And I press "Manage course backups" + And I upload "backup/moodle2/tests/fixtures/test_random_question_39.mbz" file to "Files" filemanager + And I press "Save changes" + And I restore "test_random_question_39.mbz" backup into "Course 1" course using this options: + When I am on the "Test MDL-78902 quiz" "mod_quiz > edit" page logged in as admin + Then I should see "See questions" + And I click on "See questions" "link" + Then I should see "Test MDL-78902 T/F question" diff --git a/backup/moodle2/tests/fixtures/test_random_question_39.mbz b/backup/moodle2/tests/fixtures/test_random_question_39.mbz new file mode 100644 index 0000000000000..ef2a668db9015 Binary files /dev/null and b/backup/moodle2/tests/fixtures/test_random_question_39.mbz differ