MDL-86524 questions: Update set reference context after category move

When a category is restored, it is first created in the course context,
them moved it its module context once that has been restored.

When this happens, we also need to update set references that refer to
that category to use the new context. However, this was using the legacy
category fields so was not updating them correctly. It now does.
This commit is contained in:
Mark Johnson
2026-02-23 11:08:17 +00:00
parent 1805a1f21f
commit c88264eccb
+13 -7
View File
@@ -5696,13 +5696,19 @@ class restore_move_module_questions_categories extends restore_execution_step {
// We need to check all the question_set_references belonging to this context_module.
$references = $DB->get_records('question_set_references', ['usingcontextid' => $newcontext->newitemid]);
foreach ($references as $reference) {
$filtercondition = json_decode($reference->filtercondition);
$categoryid = reset($filtercondition->filter->category->values);
if (!empty($categoryid) && in_array($categoryid, $categoryids)) {
// This is one of ours, update the questionscontextid.
$DB->set_field('question_set_references',
'questionscontextid', $newcontext->newitemid,
['id' => $reference->id]);
$filtercondition = json_decode($reference->filtercondition, true);
if (!array_key_exists('filter', $filtercondition)) {
$filtercondition = \core_question\question_reference_manager::convert_legacy_set_reference_filter_condition(
$filtercondition,
);
}
$questioncategoryid = $filtercondition['filter']['category']['values'][0];
if (in_array($questioncategoryid, $categoryids)) {
// This is one of ours, update the questionscontextid and filtercondition fields.
$reference->questionscontextid = $newcontext->newitemid;
$filtercondition['cat'] = "{$questioncategoryid},{$newcontext->newitemid}";
$reference->filtercondition = json_encode($filtercondition);
$DB->update_record('question_set_references', $reference);
}
}
}