#102 Handle images during backup/restore

This commit is contained in:
Mark Nelson 2017-04-27 21:54:10 +08:00
parent 074ae948c6
commit 9b96b2c9b3
3 changed files with 66 additions and 0 deletions

View file

@ -94,6 +94,10 @@ class backup_customcert_activity_structure_step extends backup_activity_structur
// Annotate the user id's where required.
$issue->annotate_ids('user', 'userid');
// Define file annotations.
$customcert->annotate_files('mod_customcert', 'intro', null);
$customcert->annotate_files('mod_customcert', 'image', null, context_course::instance($this->get_courseid())->id);
// Return the root element (customcert), wrapped into standard activity structure.
return $this->prepare_activity_structure($customcert);
}

View file

@ -163,4 +163,17 @@ class restore_customcert_activity_structure_step extends restore_activity_struct
$newitemid = $DB->insert_record('customcert_issues', $data);
$this->set_mapping('customcert_issue', $oldid, $newitemid);
}
/**
* Called immediately after all the other restore functions.
*/
protected function after_execute() {
parent::after_execute();
// Add the files.
$this->add_related_files('mod_customcert', 'intro', null);
// Note - we can't use get_old_contextid() as it refers to the module context.
$this->add_related_files('mod_customcert', 'image', null, $this->get_task()->get_info()->original_course_contextid);
}
}

View file

@ -260,6 +260,55 @@ class element extends \mod_customcert\element {
parent::definition_after_data($mform);
}
/**
* This function is responsible for handling the restoration process of the element.
*
* We will want to update the file's pathname hash.
*
* @param \restore_customcert_activity_task $restore
*/
public function after_restore($restore) {
global $DB;
// Check the files that have been processed in the backup file that belong to the custom certificate activity.
if ($files = $DB->get_records('backup_files_temp', array('backupid' => $restore->get_restoreid(),
'component' => 'mod_customcert', 'filearea' => 'image'))) {
// Get the current data we have stored for this element.
$elementinfo = json_decode($this->element->data);
// Create a file storage instance we are going to use to create pathname hashes.
$fs = get_file_storage();
// Loop through each of the files found and compare them with this element.
foreach ($files as $file) {
if ($fileinfo = \backup_controller_dbops::decode_backup_temp_info($file->info)) {
// Create the array to identify the image by.
$fileidentifier = [
$file->contextid,
'mod_customcert',
'image',
0,
$fileinfo->filepath,
$fileinfo->filename
];
$pathnamehash = $fs->get_pathname_hash(...$fileidentifier);
// Check if we found the image, if so we need to update the pathname hash.
if ($pathnamehash == $elementinfo->pathnamehash) {
// Change the context id to get the new hash.
$fileidentifier[0] = $file->newcontextid;
$newpathnamehash = $fs->get_pathname_hash(...$fileidentifier);
// Save the data now.
$datatosave = new \stdClass();
$datatosave->image = $newpathnamehash;
$datatosave->width = $elementinfo->width;
$datatosave->height = $elementinfo->height;
$DB->set_field('customcert_elements', 'data', self::save_unique_data($datatosave),
array('id' => $this->element->id));
}
}
}
}
}
/**
* Return the list of possible images to use.
*