Skip to content

Commit

Permalink
MDL-46455 restore: added restore support for 'other' in events
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Oct 12, 2015
1 parent 6920d39 commit 901a7ff
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,32 @@ protected function process_log($data) {
}
}
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;
Expand Down
39 changes: 39 additions & 0 deletions lib/classes/event/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,45 @@ function get_objectid_mapping().', DEBUG_DEVELOPER);
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.
Expand Down

0 comments on commit 901a7ff

Please sign in to comment.