]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-8270 full block backup/restore - based on patches by Mark Nielsen and code in...
authorskodak <skodak>
Sun, 23 Mar 2008 15:54:42 +0000 (15:54 +0000)
committerskodak <skodak>
Sun, 23 Mar 2008 15:54:42 +0000 (15:54 +0000)
backup/backuplib.php
backup/restorelib.php
blocks/html/block_html.php
blocks/moodleblock.class.php

index f6b0347ad34c9e932329cda4ca796708c0fa6f5d..d5043575502af5d9fa2411b1aa94818b7a0b1ce4 100644 (file)
         $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);
+                        }
+                    }
                 }
             }
         }
         //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) {
                         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));
                                                     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) {
             }
         }
 
+        // 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('<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />');
         }
index 221b6c8d5ae869ba05ed94f5ab40a074f830fd55..bbabf3781bb1beeb05cf704f448190ac9d5657e7 100644 (file)
@@ -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);
index 8e610049fe8739eb5f6a9c45f794655da292162a..f0a60ef8b8be03c0ae7eeb4568879a4be098d860 100755 (executable)
@@ -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;
     }
 }
 ?>
index d43a1e42449845e6c7580a0fc0fad3ee37db360e..4afe7d797573b7c8ae421c7543e4528e3e342ca5 100644 (file)
@@ -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;
     }
 
     /**