diff --git a/backup/moodle2/restore_stepslib.php b/backup/moodle2/restore_stepslib.php index d303ba1009dae..e07b50f81f32b 100644 --- a/backup/moodle2/restore_stepslib.php +++ b/backup/moodle2/restore_stepslib.php @@ -4331,6 +4331,14 @@ protected function process_question_category($data) { } $data->contextid = $mapping->parentitemid; + // Before 3.1, the 'stamp' field could be erroneously duplicated. + // From 3.1 onwards, there's a unique index of (contextid, stamp). + // If we encounter a duplicate in an old restore file, just generate a new stamp. + // This is the same as what happens during an upgrade to 3.1+ anyway. + if ($DB->record_exists('question_categories', ['stamp' => $data->stamp, 'contextid' => $data->contextid])) { + $data->stamp = make_unique_id_code(); + } + // Let's create the question_category and save mapping $newitemid = $DB->insert_record('question_categories', $data); $this->set_mapping('question_category', $oldid, $newitemid);