MDL-58822 backup: backup and restore user override events
authorRyan Wyllie <ryan@moodle.com>
Mon, 8 May 2017 02:13:14 +0000 (02:13 +0000)
committerRyan Wyllie <ryan@moodle.com>
Tue, 9 May 2017 05:48:40 +0000 (05:48 +0000)
backup/moodle2/backup_stepslib.php
backup/moodle2/restore_stepslib.php
mod/assign/backup/moodle2/restore_assign_stepslib.php

index 8855d58..596259e 100644 (file)
@@ -876,6 +876,14 @@ class backup_calendarevents_structure_step extends backup_structure_step {
                         AND (eventtype = 'course' OR eventtype = 'group')";
             $calendar_items_params = array('courseid'=>backup::VAR_COURSEID);
             $event->set_source_sql($calendar_items_sql, $calendar_items_params);
+        } else if ($this->name == 'activity_calendar') {
+            $params = array('instance' => backup::VAR_ACTIVITYID, 'modulename' => backup::VAR_MODNAME);
+            // If we don't want to include the userinfo in the backup then setting the courseid
+            // will filter out all of the user override events (which have a course id of zero).
+            if (!$this->get_setting_value('userinfo')) {
+                $params['courseid'] = backup::VAR_COURSEID;
+            }
+            $event->set_source_table('event', $params);
         } else {
             $event->set_source_table('event', array('courseid' => backup::VAR_COURSEID, 'instance' => backup::VAR_ACTIVITYID, 'modulename' => backup::VAR_MODNAME));
         }
index d4757be..89b1592 100644 (file)
@@ -2653,6 +2653,17 @@ class restore_calendarevents_structure_step extends restore_structure_step {
         $data = (object)$data;
         $oldid = $data->id;
         $restorefiles = true; // We'll restore the files
+        // User overrides for activities are identified by having a courseid of zero with
+        // both a modulename and instance value set.
+        $isuseroverride = !$data->courseid && $data->modulename && $data->instance;
+
+
+        // If we don't want to include user data and this record is a user override event
+        // for an activity then we should not create it.
+        if (!$this->task->get_setting_value('userinfo') && $isuseroverride) {
+            return;
+        }
+
         // Find the userid and the groupid associated with the event.
         $data->userid = $this->get_mappingid('user', $data->userid);
         if ($data->userid === false) {
@@ -2688,7 +2699,9 @@ class restore_calendarevents_structure_step extends restore_structure_step {
                 'name'           => $data->name,
                 'description'    => $data->description,
                 'format'         => $data->format,
-                'courseid'       => $this->get_courseid(),
+                // User overrides in activities use a course id of zero. All other event types
+                // must use the mapped course id.
+                'courseid'       => $data->courseid ? $this->get_courseid() : 0,
                 'groupid'        => $data->groupid,
                 'userid'         => $data->userid,
                 'repeatid'       => $this->get_mappingid('event', $data->repeatid),
@@ -2710,10 +2723,11 @@ class restore_calendarevents_structure_step extends restore_structure_step {
                  WHERE " . $DB->sql_compare_text('name', 255) . " = " . $DB->sql_compare_text('?', 255) . "
                    AND courseid = ?
                    AND modulename = ?
+                   AND instance = ?
                    AND timestart = ?
                    AND timeduration = ?
                    AND " . $DB->sql_compare_text('description', 255) . " = " . $DB->sql_compare_text('?', 255);
-        $arg = array ($params['name'], $params['courseid'], $params['modulename'], $params['timestart'], $params['timeduration'], $params['description']);
+        $arg = array ($params['name'], $params['courseid'], $params['modulename'], $params['instance'], $params['timestart'], $params['timeduration'], $params['description']);
         $result = $DB->record_exists_sql($sql, $arg);
         if (empty($result)) {
             $newitemid = $DB->insert_record('event', $params);
@@ -5512,4 +5526,4 @@ class restore_completion_defaults_structure_step extends restore_structure_step
         // Save id mapping for restoring associated events.
         $this->set_mapping('course_completion_defaults', $oldid, $newid);
     }
-}
\ No newline at end of file
+}
index e1e170d..cfbb868 100644 (file)
@@ -65,8 +65,9 @@ class restore_assign_activity_structure_step extends restore_activity_structure_
             $userflag = new restore_path_element('assign_userflag',
                                                    '/activity/assign/userflags/userflag');
             $paths[] = $userflag;
-            $paths[] = new restore_path_element('assign_override', '/activity/assign/overrides/override');
         }
+
+        $paths[] = new restore_path_element('assign_override', '/activity/assign/overrides/override');
         $paths[] = new restore_path_element('assign_plugin_config',
                                             '/activity/assign/plugin_configs/plugin_config');