diff --git a/backup/moodle2/restore_stepslib.php b/backup/moodle2/restore_stepslib.php index 65b3ded98ee..dee1bdbc48b 100644 --- a/backup/moodle2/restore_stepslib.php +++ b/backup/moodle2/restore_stepslib.php @@ -1386,13 +1386,31 @@ class restore_section_structure_step extends restore_structure_step { public function process_course_format_options($data) { global $DB; - $data = (object)$data; - $oldid = $data->id; - unset($data->id); - $data->sectionid = $this->task->get_sectionid(); - $data->courseid = $this->get_courseid(); - $newid = $DB->insert_record('course_format_options', $data); - $this->set_mapping('course_format_options', $oldid, $newid); + static $courseformats = array(); + $courseid = $this->get_courseid(); + if (!array_key_exists($courseid, $courseformats)) { + // It is safe to have a static cache of course formats because format can not be changed after this point. + $courseformats[$courseid] = $DB->get_field('course', 'format', array('id' => $courseid)); + } + $data = (array)$data; + if ($courseformats[$courseid] === $data['format']) { + // Import section format options only if both courses (the one that was backed up + // and the one we are restoring into) have same formats. + $params = array( + 'courseid' => $this->get_courseid(), + 'sectionid' => $this->task->get_sectionid(), + 'format' => $data['format'], + 'name' => $data['name'] + ); + if ($record = $DB->get_record('course_format_options', $params, 'id, value')) { + // Do not overwrite existing information. + $newid = $record->id; + } else { + $params['value'] = $data['value']; + $newid = $DB->insert_record('course_format_options', $params); + } + $this->set_mapping('course_format_options', $data['id'], $newid); + } } protected function after_execute() {