From 9b96b2c9b338ab2fd3035b1eb625c45c33e0cd29 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Thu, 27 Apr 2017 21:54:10 +0800 Subject: [PATCH] #102 Handle images during backup/restore --- backup/moodle2/backup_customcert_stepslib.php | 4 ++ .../moodle2/restore_customcert_stepslib.php | 13 +++++ element/image/classes/element.php | 49 +++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/backup/moodle2/backup_customcert_stepslib.php b/backup/moodle2/backup_customcert_stepslib.php index 8ab6a3b..e720b81 100644 --- a/backup/moodle2/backup_customcert_stepslib.php +++ b/backup/moodle2/backup_customcert_stepslib.php @@ -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); } diff --git a/backup/moodle2/restore_customcert_stepslib.php b/backup/moodle2/restore_customcert_stepslib.php index 4b3a596..ae9f40d 100644 --- a/backup/moodle2/restore_customcert_stepslib.php +++ b/backup/moodle2/restore_customcert_stepslib.php @@ -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); + } } diff --git a/element/image/classes/element.php b/element/image/classes/element.php index faf3e5b..5ee6871 100644 --- a/element/image/classes/element.php +++ b/element/image/classes/element.php @@ -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. *