Skip to content

Commit

Permalink
MDL-78902 backup: fix restore random question.
Browse files Browse the repository at this point in the history
  • Loading branch information
TomoTsuyuki committed Dec 19, 2023
1 parent 513f3b0 commit 233759c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
36 changes: 29 additions & 7 deletions backup/moodle2/restore_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions backup/moodle2/tests/behat/restore_random_question.feature
Original file line number Diff line number Diff line change
@@ -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"
Binary file not shown.

0 comments on commit 233759c

Please sign in to comment.