From 5cfefc9b024b801d483162a3e40c0ae55115c8d0 Mon Sep 17 00:00:00 2001 From: skodak Date: Sun, 23 Mar 2008 15:54:42 +0000 Subject: [PATCH] MDL-8270 full block backup/restore - based on patches by Mark Nielsen and code in HEAD by Tim Hunt; merged from MOODLE_19_STABLE --- backup/backuplib.php | 85 ++++++++++++++++++++++-------------- backup/restorelib.php | 2 +- blocks/html/block_html.php | 49 ++++++++++++++++++--- blocks/moodleblock.class.php | 64 ++++++++++++++++++++------- 4 files changed, 145 insertions(+), 55 deletions(-) diff --git a/backup/backuplib.php b/backup/backuplib.php index f6b0347ad3..d504357550 100644 --- a/backup/backuplib.php +++ b/backup/backuplib.php @@ -929,27 +929,31 @@ $pages = array(); $pages[] = page_create_object(PAGE_COURSE_VIEW, $preferences->backup_course); - // Let's see if we have to backup blocks from modules - $modulerecords = get_records_sql('SELECT name, id FROM '.$CFG->prefix.'modules'); + if (!empty($CFG->showblocksonmodpages)) { + // get course structure + $course = get_record('course', 'id', $preferences->backup_course); + $modinfo =& get_fast_modinfo($course); - foreach($preferences->mods as $module) { - if(!$module->backup) { - continue; - } + foreach($preferences->mods as $module) { + if (!$module->backup) { + continue; + } - $cmods = get_records_select('course_modules', 'course = '.$preferences->backup_course.' AND module = '.$modulerecords[$module->name]->id); - if(empty($cmods)) { - continue; - } + if (empty($modinfo->instances[$module->name])) { + continue; + } - $pagetypes = page_import_types('mod/'.$module->name.'/'); - if(empty($pagetypes)) { - continue; - } + $pagetypes = page_import_types('mod/'.$module->name.'/'); + if (empty($pagetypes)) { + continue; + } - foreach($pagetypes as $pagetype) { - foreach($cmods as $cmod) { - $pages[] = page_create_object($pagetype, $cmod->instance); + foreach($pagetypes as $pagetype) { + foreach($modinfo->instances[$module->name] as $cm) { + if (!empty($module->instances[$cm->instance]->backup)) { + $pages[] = page_create_object($pagetype, $cm->instance); + } + } } } } @@ -957,7 +961,7 @@ //Blocks open tag fwrite ($bf,start_tag('BLOCKS',2,true)); - while($page = array_pop($pages)) { + foreach($pages as $page) { if ($instances = blocks_get_by_page($page)) { //Iterate over every block foreach ($instances as $position) { @@ -967,32 +971,28 @@ if(empty($blocks[$instance->blockid]->name)) { continue; } - - if (!$blockobj = block_instance($blocks[$instance->blockid]->name, $instance)) { + $blockname = $blocks[$instance->blockid]->name; + + if (!$blockobj = block_instance($blockname, $instance)) { // Invalid block continue; - } - - //Give the block a chance to process any links in configdata. - if (!isset($blocks[$instance->blockid]->blockobject)) { - $blocks[$instance->blockid]->blockobject = block_instance($blocks[$instance->blockid]->name); } - $config = unserialize(base64_decode($instance->configdata)); - $blocks[$instance->blockid]->blockobject->backup_encode_absolute_links_in_config($config); - $instance->configdata = base64_encode(serialize($config)); + + // encode absolute links in block config + $instance->configdata = $blockobj->get_backup_encoded_config(); //Begin Block fwrite ($bf,start_tag('BLOCK',3,true)); fwrite ($bf,full_tag('ID', 4, false,$instance->id)); - fwrite ($bf,full_tag('NAME',4,false,$blocks[$instance->blockid]->name)); + fwrite ($bf,full_tag('NAME',4,false,$blockname)); fwrite ($bf,full_tag('PAGEID',4,false,$instance->pageid)); fwrite ($bf,full_tag('PAGETYPE',4,false,$instance->pagetype)); fwrite ($bf,full_tag('POSITION',4,false,$instance->position)); fwrite ($bf,full_tag('WEIGHT',4,false,$instance->weight)); fwrite ($bf,full_tag('VISIBLE',4,false,$instance->visible)); - fwrite ($bf,full_tag('CONFIGDATA',4,false,$instance->configdata)); - // Write instance data if needed+ - if ($blockobj->backuprestore_enabled()) { + fwrite ($bf,full_tag('CONFIGDATA',4,false,$instance->configdata)); + // Write instance data if needed + if ($blockobj->backuprestore_instancedata_used()) { fwrite ($bf,start_tag('INSTANCEDATA',4,true)); $status = $blockobj->instance_backup($bf, $preferences); fwrite ($bf,end_tag('INSTANCEDATA',4,true)); @@ -1494,7 +1494,7 @@ JOIN {$CFG->prefix}grade_items gi ON (gi.iteminstance = gc.id) WHERE gc.courseid = $preferences->backup_course - AND (gi.itemtype='course' OR gi.itemtype='category') + AND (gi.itemtype='course' OR gi.itemtype='category') ORDER BY gi.sortorder ASC"); if ($grade_categories) { @@ -2237,6 +2237,25 @@ } } + // For each block, call its encode_content_links method. + // This encodes forexample links to blocks/something/viewphp?id=666 + // that are stored in other activities. + static $blockobjects = null; + if (!isset($blockobjects)) { + $blockobjects = array(); + if ($blocks = get_records('block', 'visible', 1)) { + foreach ($blocks as $block) { + if ($blockobject = block_instance($block->name)) { + $blockobjects[] = $blockobject; + } + } + } + } + + foreach ($blockobjects as $blockobject) { + $result = $blockobject->encode_content_links($result,$mypreferences); + } + if ($result != $content) { debugging('

'.s($content).'
changed to
'.s($result).'

'); } diff --git a/backup/restorelib.php b/backup/restorelib.php index 221b6c8d5a..bbabf3781b 100644 --- a/backup/restorelib.php +++ b/backup/restorelib.php @@ -765,7 +765,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3); //It calls selectively to restore_create_block_instances() for 1.5 //and above backups. Upwards compatible with old blocks. function restore_create_blocks($restore, $backup_block_format, $blockinfo, $xml_file) { - + global $CFG; $status = true; delete_records('block_instance', 'pageid', $restore->course_id, 'pagetype', PAGE_COURSE_VIEW); diff --git a/blocks/html/block_html.php b/blocks/html/block_html.php index 8e610049fe..f0a60ef8b8 100755 --- a/blocks/html/block_html.php +++ b/blocks/html/block_html.php @@ -36,14 +36,51 @@ class block_html extends block_base { return $this->content; } - function backup_encode_absolute_links_in_config(&$config) { - $config->text = backup_encode_absolute_links($config->text); + /** + * Will be called before an instance of this block is backed up, so that any links in + * any links in any HTML fields on config can be encoded. + * @return string + */ + function get_backup_encoded_config() { + $data = clone($this->config); + $data->text = backup_encode_absolute_links($data->text); + return base64_encode(serialize($data)); } - function restore_decode_absolute_links_in_config(&$config) { - $oldtext = $config->text; - $config->text = restore_decode_absolute_links($oldtext); - return $config->text != $oldtext; + /** + * This function makes all the necessary calls to {@link restore_decode_content_links_worker()} + * function in order to decode contents of this block from the backup + * format to destination site/course in order to mantain inter-activities + * working in the backup/restore process. + * + * This is called from {@link restore_decode_content_links()} function in the restore process. + * + * NOTE: There is no block instance when this method is called. + * + * @param object $restore Standard restore object + * @return boolean + **/ + function decode_content_links_caller($restore) { + global $CFG; + + if ($restored_blocks = get_records_select("backup_ids","table_name = 'block_instance' AND backup_code = $restore->backup_unique_code AND new_id > 0", "", "new_id")) { + $restored_blocks = implode(',', array_keys($restored_blocks)); + $sql = "SELECT bi.* + FROM {$CFG->prefix}block_instance bi + JOIN {$CFG->prefix}block b ON b.id = bi.blockid + WHERE b.name = 'html' AND bi.id IN ($restored_blocks)"; + + if ($instances = get_records_sql($sql)) { + foreach ($instances as $instance) { + $blockobject = block_instance('html', $instance); + $blockobject->config->text = restore_decode_absolute_links($blockobject->config->text); + $blockobject->config->text = restore_decode_content_links_worker($blockobject->config->text, $restore); + $blockobject->instance_config_commit($blockobject->pinned); + } + } + } + + return true; } } ?> diff --git a/blocks/moodleblock.class.php b/blocks/moodleblock.class.php index d43a1e4244..4afe7d7975 100644 --- a/blocks/moodleblock.class.php +++ b/blocks/moodleblock.class.php @@ -130,7 +130,7 @@ class block_base { } /** - * Enable the block for backup and restore. + * Enable custom instance data section in backup and restore. * * If return true, then {@link instance_backup()} and * {@link instance_restore()} will be called during @@ -138,7 +138,7 @@ class block_base { * * @return boolean **/ - function backuprestore_enabled() { + function backuprestore_instancedata_used() { return false; } @@ -174,24 +174,58 @@ class block_base { /** * Will be called before an instance of this block is backed up, so that any links in - * any links in any HTML fields on config can be encoded. For example, for the HTML block - * we need to do $config->text = backup_encode_absolute_links($config->text);. - * - * @param object $config the config field for an insance of this class. + * in config can be encoded. For example config->text, for the HTML block + * @return string */ - function backup_encode_absolute_links_in_config(&$config) { + function get_backup_encoded_config() { + return base64_encode(serialize($this->config)); } /** - * Undo the effect of backup_encode_absolute_links_in_config. For exmaple, in the - * HTML block we need to do $config->text = restore_decode_absolute_links($config->text); + * Return the content encoded to support interactivities linking. This function is + * called automatically from the backup procedure by {@link backup_encode_absolute_links()}. * - * @param object $config the config field for an insance of this class. - * @return boolean return true if something has changed, so the database can be updated, - * false if not, for efficiency reasons. - */ - function restore_decode_absolute_links_in_config(&$config) { - return false; + * NOTE: There is no block instance when this method is called. + * + * @param string $content Content to be encoded + * @param object $restore Restore preferences object + * @return string The encoded content + **/ + function encode_content_links($content, $restore) { + return $content; + } + + /** + * This function makes all the necessary calls to {@link restore_decode_content_links_worker()} + * function in order to decode contents of this block from the backup + * format to destination site/course in order to mantain inter-activities + * working in the backup/restore process. + * + * This is called from {@link restore_decode_content_links()} function in the restore process. + * + * NOTE: There is no block instance when this method is called. + * + * @param object $restore Standard restore object + * @return boolean + **/ + function decode_content_links_caller($restore) { + return true; + } + + /** + * Return content decoded to support interactivities linking. + * This is called automatically from + * {@link restore_decode_content_links_worker()} function + * in the restore process. + * + * NOTE: There is no block instance when this method is called. + * + * @param string $content Content to be dencoded + * @param object $restore Restore preferences object + * @return string The dencoded content + **/ + function decode_content_links($content, $restore) { + return $content; } /** -- 2.39.5