if(empty($blocks[$instance->blockid]->name)) {
continue;
}
+
+ if (!$blockobj = block_instance($blocks[$instance->blockid]->name, $instance)) {
+ // Invalid block
+ continue;
+ }
//Give the block a chance to process any links in configdata.
if (!isset($blocks[$instance->blockid]->blockobject)) {
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));
-
+ fwrite ($bf,full_tag('CONFIGDATA',4,false,$instance->configdata));
+ // Write instance data if needed+
+ if ($blockobj->backuprestore_enabled()) {
+ fwrite ($bf,start_tag('INSTANCEDATA',4,true));
+ $status = $blockobj->instance_backup($bf, $preferences);
+ fwrite ($bf,end_tag('INSTANCEDATA',4,true));
+ }
$context = get_context_instance(CONTEXT_BLOCK, $instance->id);
write_role_overrides_xml($bf, $context, 4);
/// write role_assign code here
}
//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;
}
//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;
$status = true;
+ $CFG->restore_blockinstanceids = array();
// Tracks which blocks we create during the restore.
// This is used in restore_decode_content_links.
}
//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)) {
//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);
}
$restore->blockinstanceids[] = $newid;
} else {
//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);
+ if ($blockobj = block_instance($instance->name,$instance)) {
+ $blockobj->after_restore($restore);
+ }
//Now we can increment the weight counter
++$maxweights[$instance->position];
//Check if we are into BLOCKS zone
//if ($this->tree[3] == "BLOCKS") //Debug
// echo $this->level.str_repeat(" ",$this->level*2)."<".$tagName."><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")
//if (trim($this->content)) //Debug
// echo "C".str_repeat(" ",($this->level+2)*2).$this->getContents()."<br />\n"; //Debug
//echo $this->level.str_repeat(" ",$this->level*2)."</".$tagName."><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) {
//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
+
+ if ($data = $data['BLOCK']['#']['INSTANCEDATA']['0']['#']) {
+ //Restore code calls this multiple times - so might already have the newid
+ if ($newid = backup_getid($this->preferences->backup_unique_code,'block_instance',$oldid)) {
+ $newid = $newid->new_id;
+ } else {
+ $newid = null;
+ }
+ //Save to DB, we will use it later
+ $status = backup_putid($this->preferences->backup_unique_code,'block_instance',$oldid,$newid,$data);
+ }
+ }
+ //Reset temp
+ unset($this->temp);
+
break;
default:
die($tagName);
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) && !$isimport) {
function after_restore($restore) {
}
+ /**
+ * Enable the block for backup and restore.
+ *
+ * If return true, then {@link instance_backup()} and
+ * {@link instance_restore()} will be called during
+ * backup/restore routines.
+ *
+ * @return boolean
+ **/
+ function backuprestore_enabled() {
+ return false;
+ }
+
+ /**
+ * Allows the block class to have a backup routine. Handy
+ * when the block has its own tables that have foreign keys to
+ * other tables (example: user table).
+ *
+ * Note: at the time of writing this comment, the indent level
+ * for the {@link full_tag()} should start at 5.
+ *
+ * @param resource $bf Backup File
+ * @param object $preferences Backup preferences
+ * @return boolean
+ **/
+ function instance_backup($bf, $preferences) {
+ return true;
+ }
+
+ /**
+ * Allows the block class to restore its backup routine.
+ *
+ * Should not return false if data is empty
+ * because old backups would not contain block instance backup data.
+ *
+ * @param object $restore Standard restore object
+ * @param object $data Object from backup_getid for this block instance
+ * @return boolean
+ **/
+ function instance_restore($restore, $data) {
+ return true;
+ }
+
/**
* 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