From 0e9af9179475545885c9954ca12e8a0717a5943c Mon Sep 17 00:00:00 2001 From: defacer Date: Sun, 22 Aug 2004 16:48:28 +0000 Subject: [PATCH] Fix for bug 1678: Now when the format of a course is changed, all blocks which are not applicable for it anymore are automatically purged. --- lib/blocklib.php | 71 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 4 deletions(-) diff --git a/lib/blocklib.php b/lib/blocklib.php index b86919bc74..50318b94ea 100644 --- a/lib/blocklib.php +++ b/lib/blocklib.php @@ -9,10 +9,73 @@ define('BLOCK_MOVE_RIGHT', 0x02); define('BLOCK_MOVE_UP', 0x04); define('BLOCK_MOVE_DOWN', 0x08); -define('COURSE_FORMAT_SITE', 0x01); -define('COURSE_FORMAT_WEEKS', 0x02); -define('COURSE_FORMAT_TOPICS', 0x04); -define('COURSE_FORMAT_SOCIAL', 0x08); +function block_formats_to_array($formatstring) { + $retval = array(); + + $formats = preg_split('/[+\-]/', $formatstring, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE); + if(!empty($formats)) { + foreach($formats as $format) { + if($format[1] > 0 && substr ($str, $format[1] - 1, 1) === '-') { + $retval[$format[0]] = false; + } + else { + $retval[$format[0]] = true; + } + } + } + + return $retval; +} + +function block_remove_inappropriate_from_course(&$course) { + $blocks = $course->blockinfo; + + $delimpos = strpos($blocks, ':'); + + if($delimpos === false) { + // No ':' found, we have all left blocks + $leftblocks = explode(',', $blocks); + $rightblocks = array(); + } + else if($delimpos === 0) { + // ':' at start of string, we have all right blocks + $blocks = substr($blocks, 1); + $leftblocks = array(); + $rightblocks = explode(',', $blocks); + } + else { + // Both left and right blocks + $leftpart = substr($blocks, 0, $delimpos); + $rightpart = substr($blocks, $delimpos + 1); + $leftblocks = explode(',', $leftpart); + $rightblocks = explode(',', $rightpart); + } + + $allblocks = get_records('blocks'); + if(!empty($leftblocks)) { + foreach($leftblocks as $key => $id) { + $positiveid = abs($id); + $formats = block_method_result($allblocks[$positiveid]->name, 'applicable_formats'); + if( !(isset($formats[$course->format]) ? $formats[$course->format] : !empty($formats['all']))) { + unset($leftblocks[$key]); + } + } + } + if(!empty($rightblocks)) { + foreach($rightblocks as $key => $id) { + $positiveid = abs($id); + $formats = block_method_result($allblocks[$positiveid]->name, 'applicable_formats'); + if( !(isset($formats[$course->format]) ? $formats[$course->format] : !empty($formats['all']))) { + unset($rightblocks[$key]); + } + } + } + + $course->blockinfo = implode(',', $leftblocks); + if(!empty($rightblocks)) { + $course->blockinfo .= ':' . implode(',', $rightblocks); + } +} // Returns the case-sensitive name of the class' constructor function. This includes both // PHP5- and PHP4-style constructors. If no appropriate constructor can be found, returns NULL. -- 2.39.5