From 229f852a8afddd2947a4be4d8effddda868ee1f7 Mon Sep 17 00:00:00 2001 From: stronk7 Date: Tue, 20 Apr 2004 23:28:46 +0000 Subject: [PATCH] Now blockinfo is fully supported in backup & restore. For old backups (without blockinfo field, default blocks are applied). Every block visibility is mantained in the process. Bump versions a bit (2004042100) :-) --- backup/backuplib.php | 1 + backup/restorelib.php | 9 +++- backup/version.php | 2 +- blocks/version.php | 2 +- lib/blocklib.php | 112 ++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 118 insertions(+), 8 deletions(-) diff --git a/backup/backuplib.php b/backup/backuplib.php index e8724293ca..eccc3513f2 100644 --- a/backup/backuplib.php +++ b/backup/backuplib.php @@ -551,6 +551,7 @@ fwrite ($bf,full_tag("SUMMARY",3,false,$course->summary)); fwrite ($bf,full_tag("FORMAT",3,false,$course->format)); fwrite ($bf,full_tag("SHOWGRADES",3,false,$course->showgrades)); + fwrite ($bf,full_tag("BLOCKINFO",3,false,blocks_get_block_names($course->blockinfo))); fwrite ($bf,full_tag("NEWSITEMS",3,false,$course->newsitems)); fwrite ($bf,full_tag("TEACHER",3,false,$course->teacher)); fwrite ($bf,full_tag("TEACHERS",3,false,$course->teachers)); diff --git a/backup/restorelib.php b/backup/restorelib.php index 787d0eccad..54f51e587e 100644 --- a/backup/restorelib.php +++ b/backup/restorelib.php @@ -383,8 +383,8 @@ $course->timecreated = addslashes($course_header->course_timecreated); $course->timemodified = addslashes($course_header->course_timemodified); //Adjust blockinfo field. - //NOTE: For now, it's imposible to find it in backup files because it isn't saved, - // so, we always rebuid it with defaults. + //If the info doesn't exist in backup, we create defaults, else we recode it + //to current site blocks. if (!$course->blockinfo) { //Create blockinfo default content if ($course->format == "social") { @@ -393,6 +393,8 @@ //For topics and weeks formats (default built in the function) $course->blockinfo = blocks_get_default_blocks(); } + } else { + $course->blockinfo = blocks_get_block_ids($course->blockinfo); } //Now insert the record $newid = insert_record("course",$course); @@ -1989,6 +1991,9 @@ case "SHOWGRADES": $this->info->course_showgrades = $this->getContents(); break; + case "BLOCKINFO": + $this->info->blockinfo = $this->getContents(); + break; case "NEWSITEMS": $this->info->course_newsitems = $this->getContents(); break; diff --git a/backup/version.php b/backup/version.php index b32bfb1229..9a207b5a82 100644 --- a/backup/version.php +++ b/backup/version.php @@ -5,6 +5,6 @@ // database (backup_version) to determine whether upgrades should // be performed (see db/backup_*.php) -$backup_version = 2004041800; // The current version is a date (YYYYMMDDXX) +$backup_version = 2004042100; // The current version is a date (YYYYMMDDXX) $backup_release = "1.3 development"; // User-friendly version number diff --git a/blocks/version.php b/blocks/version.php index 5ed36ebe55..3ccd3c1d9b 100644 --- a/blocks/version.php +++ b/blocks/version.php @@ -5,4 +5,4 @@ // database (blocks_version) to determine whether upgrades should // be performed (see db/backup_*.php) -$blocks_version = 2004041800; // The current version is a date (YYYYMMDDXX) +$blocks_version = 2004042100; // The current version is a date (YYYYMMDDXX) diff --git a/lib/blocklib.php b/lib/blocklib.php index a6fa564779..7453f1ba66 100644 --- a/lib/blocklib.php +++ b/lib/blocklib.php @@ -736,11 +736,16 @@ function blocks_get_default_blocks ($courseid = NULL, $blocknames="participants, if ($leftblocksn) { foreach($leftblocksn as $leftblockn) { //Convert blockname to id - $leftblock = block_get_id_by_name($leftblockn); + $leftblock = block_get_id_by_name(str_replace("-","",$leftblockn)); if ($leftblock) { //Check it's visible if($block = get_record("blocks","id",$leftblock,"visible","1")) { - $leftblocks[] = $leftblock; + //Check if the module was hidden at course level + if (substr($leftblockn,0,1) == "-") { + $leftblocks[] = -$leftblock; + } else { + $leftblocks[] = $leftblock; + } } } } @@ -749,11 +754,16 @@ function blocks_get_default_blocks ($courseid = NULL, $blocknames="participants, if ($rightblocksn) { foreach($rightblocksn as $rightblockn) { //Convert blockname to id - $rightblock = block_get_id_by_name($rightblockn); + $rightblock = block_get_id_by_name(str_replace("-","",$rightblockn)); if ($rightblock) { //Check it's visible if($block = get_record("blocks","id",$rightblock,"visible","1")) { - $rightblocks[] = $rightblock; + //Check if the module was hidden at course level + if (substr($rightblockn,0,1) == "-") { + $rightblocks[] = -$rightblock; + } else { + $rightblocks[] = $rightblock; + } } } } @@ -781,4 +791,98 @@ function blocks_get_default_blocks ($courseid = NULL, $blocknames="participants, return $blockinfo; } +//This function will return the names representation of the blockinfo field. +//It's used to include that info in backups. To restore we'll use the +//blocks_get_block_ids() function. It makes the opposite conversion +//(from names to ids) +function blocks_get_block_names ($blockinfo) { + + //Calculate left and right blocks + $blocksn = $blockinfo; + $delimpos = strpos($blocksn, ':'); + + if($delimpos === false) { + // No ':' found, we have all left blocks + $leftblocksn = explode(',', $blocksn); + $rightblocksn = array(); + } else if($delimpos === 0) { + // ':' at start of string, we have all right blocks + $blocksn = substr($blocksn, 1); + $leftblocksn = array(); + $rightblocksn = explode(',', $blocksn); + } + else { + // Both left and right blocks + $leftpartn = substr($blocksn, 0, $delimpos); + $rightpartn = substr($blocksn, $delimpos + 1); + $leftblocksn = explode(',', $leftpartn); + $rightblocksn = explode(',', $rightpartn); + } + + //Now I have blocks separated + + $leftblocks = array(); + $rightblocks = array(); + + if ($leftblocksn) { + foreach($leftblocksn as $leftblockn) { + //Convert id to blockname + $leftblock = block_get_name_by_id(abs($leftblockn)); + if ($leftblock) { + //Check it's visible + if($block = get_record("blocks","name",$leftblock,"visible","1")) { + //Check if it's hidden oe no in the course + if($leftblockn<0) { + $leftblocks[] = '-'.$leftblock; + } else { + $leftblocks[] = $leftblock; + } + } + } + } + } + + if ($rightblocksn) { + foreach($rightblocksn as $rightblockn) { + //Convert id to blockname + $rightblock = block_get_name_by_id(abs($rightblockn)); + if ($rightblock) { + //Check it's visible + if($block = get_record("blocks","name",$rightblock,"visible","1")) { + //Check if it's hidden oe no in the course + if($rightblockn<0) { + $rightblocks[] = '-'.$rightblock; + } else { + $rightblocks[] = $rightblock; + } + } + } + } + } + + //Calculate the blockinfo field + if ($leftblocks || $rightblocks) { + $blockinfo = ''; + if ($leftblocks) { + $blockinfo .= implode(",", $leftblocks); + } + if ($rightblocks) { + $blockinfo .= ':'.implode(",",$rightblocks); + } + } else { + $blockinfo = ''; + } + + //Returns the blockinfo + return $blockinfo; +} + +//This function will return the ids representation of the blockinfo field. +//It's used to load that info from backups. This function is the opposite +//to the blocks_get_block_names() used in backup +function blocks_get_block_ids ($blockinfo) { + + //Just call this with the appropiate parammeters. + return blocks_get_default_blocks(NULL,$blockinfo); +} ?> -- 2.39.5