From 901a7ff7f2c9228e395c01f276ffeff410b25a47 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Tue, 22 Sep 2015 16:25:19 -0700 Subject: [PATCH] MDL-46455 restore: added restore support for 'other' in events --- ...tore_tool_log_logstore_subplugin.class.php | 28 ++++++++++++- lib/classes/event/base.php | 39 +++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/admin/tool/log/backup/moodle2/restore_tool_log_logstore_subplugin.class.php b/admin/tool/log/backup/moodle2/restore_tool_log_logstore_subplugin.class.php index 348b71ac2ab..c25693f58c3 100644 --- a/admin/tool/log/backup/moodle2/restore_tool_log_logstore_subplugin.class.php +++ b/admin/tool/log/backup/moodle2/restore_tool_log_logstore_subplugin.class.php @@ -105,8 +105,32 @@ abstract class restore_tool_log_logstore_subplugin extends restore_subplugin { } } if (!empty($data->other)) { - // TODO: Call to the resolver. - return; + // Check if there is an available class for this event we can use to map this value. + $eventclass = $data->eventname; + if (class_exists($eventclass)) { + $othermapping = $eventclass::get_other_mapping(); + if ($othermapping) { + // Go through the data we have. + foreach ($data->other as $key => $value) { + // Check if there is a corresponding key we can use to map to. + if (isset($othermapping[$key])) { + // Ok, let's map this. + $mapping = $othermapping[$key]; + // Check if it can not be mapped. + if ((is_int($mapping) && $mapping === \core\event\base::NOT_MAPPED) || + ($mapping['restore'] === \core\event\base::NOT_MAPPED)) { + $data->other[$key] = \core\event\base::NOT_MAPPED; + } else { + $data->other[$key] = $this->get_mappingid($mapping['restore'], $value); + } + } + } + } + // Now we want to serialize it so we can store it in the DB. + $data->other = serialize($data->other); + } else { + return; // No such class, can not restore. + } } return $data; diff --git a/lib/classes/event/base.php b/lib/classes/event/base.php index 298b88cb178..ee2be4041c5 100644 --- a/lib/classes/event/base.php +++ b/lib/classes/event/base.php @@ -517,6 +517,45 @@ abstract class base implements \IteratorAggregate { return false; } + /** + * This is used when restoring course logs where it is required that we + * map the information in 'other' to it's new value in the new course. + * + * Does nothing in the base class except display a debugging message warning + * the user that the event does not contain the required functionality to + * map this information. For events that do not store any other information this + * won't be called, so no debugging message will be displayed. + * + * Example of usage: + * + * $othermapped = array(); + * $othermapped['discussionid'] = array('db' => 'forum_discussions', 'restore' => 'forum_discussion'); + * $othermapped['forumid'] = array('db' => 'forum', 'restore' => 'forum'); + * return $othermapped; + * + * If an id can not be mapped during restore we set it to \core\event\base::NOT_MAPPED, example - + * + * $othermapped = array(); + * $othermapped['someid'] = array('db' => 'some_table', 'restore' => \core\event\base::NOT_MAPPED); + * return $othermapped; + * + * Note - it isn't necessary to specify the 'db' and 'restore' values in this case, so you can also use - + * + * $othermapped = array(); + * $othermapped['someid'] = \core\event\base::NOT_MAPPED; + * return $othermapped; + * + * The 'db' key refers to the database table and the 'restore' key refers to + * the name of the restore element the other value is associated with. In many + * cases these will be the same. + * + * @return array an array of other values and their corresponding mapping + */ + public static function get_other_mapping() { + debugging('In order to restore course logs accurately the event must define the + function get_other_mapping().', DEBUG_DEVELOPER); + } + /** * Get static information about an event. * This is used in reports and is not for general use.