Index: restorelib.php
===================================================================
--- restorelib.php	(revision 2)
+++ restorelib.php	(working copy)
@@ -85,12 +85,27 @@
                 }
             }
         }
+        // Now run through the blocks
+        if (!empty($CFG->restore_blockinstanceids)) {
+            if ($instances = get_records_list('block_instance', 'id', implode(',', $CFG->restore_blockinstanceids))) {
+                foreach ($instances as $instance) {
+                    $block = blocks_get_record($instance->blockid);
+
+                    if (!$blockobj = block_instance($block->name, $instance)) {
+                        // Invalid block
+                        continue;
+                    }
+                    if (!defined('RESTORE_SILENTLY')) {
+                        echo '<li>'.get_string ('from')." $blockobj->title</li>";
+                    }
+                    $status = $blockobj->decode_content_links_caller($restore);
+                }
+            }
+        }
         if (!defined('RESTORE_SILENTLY')) {
             echo "</ul>";
         }
         
-        // TODO: process all html text also in blocks too
-        
         return $status;
     }
     
@@ -104,6 +119,12 @@
                 $content = $function_name($content,$restore);
             }
         }
+        if ($blocks = blocks_get_record()) {
+            foreach ($blocks as $block) {
+                $obj     = block_instance($block->name);
+                $content = $obj->decode_content_links($content,$restore);
+            }
+        }
         return $content;
     }
 
@@ -191,10 +212,10 @@
     }
 
     //This function read the xml file and store its data from the blocks in a object
-    function restore_read_xml_blocks ($xml_file) {
+    function restore_read_xml_blocks ($restore,$xml_file) {
 
         //We call the main read_xml function, with todo = BLOCKS
-        $info = restore_read_xml ($xml_file,'BLOCKS',false);
+        $info = restore_read_xml ($xml_file,'BLOCKS',$restore);
 
         return $info;
     }
@@ -710,7 +731,9 @@
     //This function creates all the block_instances from xml when restoring in a
     //new course
     function restore_create_block_instances($restore,$xml_file) {
+        global $CFG;
 
+        $CFG->restore_blockinstanceids = array();
         $status = true;
         //Check it exists
         if (!file_exists($xml_file)) {
@@ -718,7 +741,7 @@
         }
         //Get info from xml
         if ($status) {
-            $info = restore_read_xml_blocks($xml_file);
+            $info = restore_read_xml_blocks($restore,$xml_file);
         }
 
         if(empty($info->instances)) {
@@ -799,20 +822,41 @@
 
                     //Add this instance
                     $instance->blockid = $blocks[$instance->name]->id;
-                    
-                    if ($newid = insert_record('block_instance', $instance)) {
-                        if (!empty($instance->id)) { // this will only be set if we come from 1.7 and above backups
-                            backup_putid ($restore->backup_unique_code,"block_instance",$instance->id,$newid);
+
+                    // This will only be set if we come from 1.7 and above backups
+                    //  Also, must do this before insert (insert_record unsets id)
+                    if (!empty($instance->id)) { 
+                        $oldid = $instance->id;
+                    } else {
+                        $oldid = 0;
+                    }
+
+                    if ($instance->id = insert_record('block_instance', $instance)) {
+                        // Save the new ID for later
+                        $CFG->restore_blockinstanceids[] = $instance->id;
+                        // Create block instance
+                        if (!$blockobj = block_instance($instance->name, $instance)) {
+                            $status = false;
+                            break;
                         }
+                        // Run the block restore if needed
+                        if ($blockobj->backuprestore_enabled()) {
+                            // Get restore information
+                            $data = backup_getid($restore->backup_unique_code,'block_instance',$oldid);
+                            $data->new_id = $instance->id;  // For completeness
+                            if (!$blockobj->instance_restore($restore, $data)) {
+                                $status = false;
+                                break;
+                            }
+                        }
+                        // Save oldid after block restore process because info will be over-written with blank string
+                        if ($oldid) {
+                            backup_putid ($restore->backup_unique_code,"block_instance",$oldid,$instance->id);
+                        }
                     } else {
                         $status = false;
                         break;                      
                     }
-                    
-                    //Get an object for the block and tell it it's been restored so it can update dates
-                    //etc. if necessary
-                    $blockobj=block_instance($instance->name,$instance);
-                    $blockobj->after_restore($restore);
     
                     //Now we can increment the weight counter
                     ++$maxweights[$instance->position];
@@ -3080,6 +3124,16 @@
             //Check if we are into BLOCKS zone
             //if ($this->tree[3] == "BLOCKS")                                                         //Debug
             //    echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br />\n";   //Debug
+
+            //If we are under a BLOCK tag under a BLOCKS zone, accumule it
+            if (isset($this->tree[4]) and isset($this->tree[3])) {  // 
+                if ($this->tree[4] == "BLOCK" and $this->tree[3] == "BLOCKS") {
+                    if (!isset($this->temp)) {
+                        $this->temp = "";
+                    }
+                    $this->temp .= "<".$tagName.">";
+                }
+            }
         }
 
         //This is the startTag handler we use where we are reading the sections zone (todo="SECTIONS")
@@ -3739,6 +3793,13 @@
                 //if (trim($this->content))                                                                     //Debug
                 //    echo "C".str_repeat("&nbsp;",($this->level+2)*2).$this->getContents()."<br />\n";           //Debug
                 //echo $this->level.str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br />\n";          //Debug
+
+                // Collect everything into $this->temp
+                if (!isset($this->temp)) {
+                    $this->temp = "";
+                }
+                $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
+
                 //Dependig of different combinations, do different things
                 if ($this->level == 4) {
                     switch ($tagName) {
@@ -3746,6 +3807,28 @@
                             //We've finalized a block, get it
                             $this->info->instances[] = $this->info->tempinstance;
                             unset($this->info->tempinstance);
+                            
+                            //Also, xmlize INSTANCEDATA and save to db
+                            //Prepend XML standard header to info gathered
+                            $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
+                            //Call to xmlize for this portion of xml data (one BLOCK)
+                            //echo "-XMLIZE: ".strftime ("%X",time()),"-";                                                //Debug
+                            $data = xmlize($xml_data,0);         
+                            //echo strftime ("%X",time())."<p>";                                                          //Debug
+                            //traverse_xmlize($data);                                                                     //Debug
+                            //print_object ($GLOBALS['traverse_array']);                                                  //Debug
+                            //$GLOBALS['traverse_array']="";                                                              //Debug
+                            //Check for instancedata, is exists, then save to DB
+                            if (isset($data['BLOCK']['#']['INSTANCEDATA']['0']['#'])) {
+                                //Get old id
+                                $oldid = $data['BLOCK']['#']['ID']['0']['#'];
+                                //Get instancedata
+                                $data = $data['BLOCK']['#']['INSTANCEDATA']['0']['#'];
+                                //Save to DB, we will use it later
+                                $status = backup_putid($this->preferences->backup_unique_code,'block_instance',$oldid,null,$data);
+                            }
+                            //Reset temp
+                            unset($this->temp);
                             break;
                         default:
                             die($tagName);
@@ -6150,7 +6233,7 @@
             if (!defined('RESTORE_SILENTLY')) {
                 echo "<li>".get_string("creatingblocksroles").'</li>';
             }
-            $blocks = restore_read_xml_blocks($xmlfile);
+            $blocks = restore_read_xml_blocks($restore, $xmlfile);
             if (isset($blocks->instances)) {
                 foreach ($blocks->instances as $instance) {
                     if (isset($instance->roleassignments)) {
