diff --git a/backup/moodle2/restore_stepslib.php b/backup/moodle2/restore_stepslib.php index e07b50f81f3..b7e4ef7c0bb 100644 --- a/backup/moodle2/restore_stepslib.php +++ b/backup/moodle2/restore_stepslib.php @@ -2670,7 +2670,7 @@ class restore_calendarevents_structure_step extends restore_structure_step { 'courseid' => $this->get_courseid(), 'groupid' => $data->groupid, 'userid' => $data->userid, - 'repeatid' => $data->repeatid, + 'repeatid' => $this->get_mappingid('event', $data->repeatid), 'modulename' => $data->modulename, 'eventtype' => $data->eventtype, 'timestart' => $this->apply_date_offset($data->timestart), @@ -2688,18 +2688,27 @@ class restore_calendarevents_structure_step extends restore_structure_step { FROM {event} WHERE " . $DB->sql_compare_text('name', 255) . " = " . $DB->sql_compare_text('?', 255) . " AND courseid = ? - AND repeatid = ? AND modulename = ? AND timestart = ? AND timeduration = ? AND " . $DB->sql_compare_text('description', 255) . " = " . $DB->sql_compare_text('?', 255); - $arg = array ($params['name'], $params['courseid'], $params['repeatid'], $params['modulename'], $params['timestart'], $params['timeduration'], $params['description']); + $arg = array ($params['name'], $params['courseid'], $params['modulename'], $params['timestart'], $params['timeduration'], $params['description']); $result = $DB->record_exists_sql($sql, $arg); if (empty($result)) { $newitemid = $DB->insert_record('event', $params); $this->set_mapping('event', $oldid, $newitemid); $this->set_mapping('event_description', $oldid, $newitemid, $restorefiles); } + // With repeating events, each event has the repeatid pointed at the first occurrence. + // Since the repeatid will be empty when the first occurrence is restored, + // Get the repeatid from the second occurrence of the repeating event and use that to update the first occurrence. + // Then keep a list of repeatids so we only perform this update once. + static $repeatids = array(); + if (!empty($params['repeatid']) && !in_array($params['repeatid'], $repeatids)) { + // This entry is repeated so the repeatid field must be set. + $DB->set_field('event', 'repeatid', $params['repeatid'], array('id' => $params['repeatid'])); + $repeatids[] = $params['repeatid']; + } } protected function after_execute() {