From 8f5d6695e6f67ceb974e5268bdb6c8f62c30a9bd Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Wed, 13 Jan 2016 18:44:41 +0800 Subject: [PATCH] MDL-50851 block_tags: map context and colleciton in restore --- .../moodle2/restore_tags_block_task.class.php | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 blocks/tags/backup/moodle2/restore_tags_block_task.class.php diff --git a/blocks/tags/backup/moodle2/restore_tags_block_task.class.php b/blocks/tags/backup/moodle2/restore_tags_block_task.class.php new file mode 100644 index 00000000000..11d2087b919 --- /dev/null +++ b/blocks/tags/backup/moodle2/restore_tags_block_task.class.php @@ -0,0 +1,88 @@ +. + +/** + * @package block_tags + * @copyright 2016 Marina Glancy + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Specialised restore task for the tags block + * (using execute_after_tasks for recoding of tag collection id) + * + * @package block_tags + * @copyright 2016 Marina Glancy + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class restore_tags_block_task extends restore_block_task { + + protected function define_my_settings() { + } + + protected function define_my_steps() { + } + + public function get_fileareas() { + return array(); // No associated fileareas. + } + + public function get_configdata_encoded_attributes() { + return array(); // No special handling of configdata. + } + + /** + * This function, executed after all the tasks in the plan + * have been executed, will remove tag collection reference in case block was restored into another site. + * Also get mapping of contextid. + */ + public function after_restore() { + global $DB; + + // Get the blockid. + $blockid = $this->get_blockid(); + + // Extract block configdata and remove tag collection reference if this is another site. Also map contextid. + if ($configdata = $DB->get_field('block_instances', 'configdata', array('id' => $blockid))) { + $config = unserialize(base64_decode($configdata)); + $changed = false; + if (!empty($config->tagcoll) && $config->tagcoll > 1 && !$this->is_samesite()) { + $config->tagcoll = 0; + $changed = true; + } + if (!empty($config->ctx)) { + if ($ctxmap = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'context', $config->ctx)) { + $config->ctx = $ctxmap->newitemid; + } else { + $config->ctx = 0; + } + $changed = true; + } + if ($changed) { + $configdata = base64_encode(serialize($config)); + $DB->set_field('block_instances', 'configdata', $configdata, array('id' => $blockid)); + } + } + } + + static public function define_decode_contents() { + return array(); + } + + static public function define_decode_rules() { + return array(); + } +} -- 2.43.0