From f4d38d20fbe16dcd1cfbfb2c462e604388838a23 Mon Sep 17 00:00:00 2001 From: tjhunt Date: Fri, 8 May 2009 06:34:40 +0000 Subject: [PATCH] blocklib: MDL-19010 and now you can delete blocks too! --- blocks/moodleblock.class.php | 2 +- course/rest.php | 13 ++++----- lang/en_utf8/error.php | 1 + lib/blocklib.php | 52 +++++++++++++++++++++++------------- 4 files changed, 42 insertions(+), 26 deletions(-) diff --git a/blocks/moodleblock.class.php b/blocks/moodleblock.class.php index 41b8d6b482..a92534922a 100644 --- a/blocks/moodleblock.class.php +++ b/blocks/moodleblock.class.php @@ -455,7 +455,7 @@ class block_base { } $page = $this->page; - $script = $page->url->out(array('instanceid' => $this->instance->id, 'sesskey' => sesskey())); + $script = $page->url->out(false, array('instanceid' => $this->instance->id, 'sesskey' => sesskey())); $movebuttons .= '' . ''.$this->str->assignroles.''; diff --git a/course/rest.php b/course/rest.php index 5e803eb1f1..9454119809 100644 --- a/course/rest.php +++ b/course/rest.php @@ -29,12 +29,13 @@ $PAGE = page_create_object(PAGE_COURSE_VIEW, $course->id); $pageblocks = blocks_setup($PAGE, BLOCKS_PINNED_BOTH); if (!empty($instanceid)) { - $blockinstance = blocks_find_instance($instanceid, $pageblocks); - if (!$blockinstance || $blockinstance->pageid != $course->id - || $blockinstance->pagetype != 'course-view') { - error_log('AJAX commands.php: Bad block ID '.$instanceid); - die; - } + throw new moodle_exception('ajax blocks editing currently broken. MDL-19010'); +// $blockinstance = blocks_find_instance($instanceid, $pageblocks); +// if (!$blockinstance || $blockinstance->pageid != $course->id +// || $blockinstance->pagetype != 'course-view') { +// error_log('AJAX commands.php: Bad block ID '.$instanceid); +// die; +// } } $context = get_context_instance(CONTEXT_COURSE, $course->id); diff --git a/lang/en_utf8/error.php b/lang/en_utf8/error.php index aae3df3732..cc63f07081 100644 --- a/lang/en_utf8/error.php +++ b/lang/en_utf8/error.php @@ -6,6 +6,7 @@ $string['adminprimarynoedit'] = 'The primary admin cannot be edited by others'; $string['authnotexisting'] = 'The autorization plugin doesn\'t exist'; $string['authorizeerror'] = 'Authorize error'; $string['blockdoesnotexist'] = 'This block does not exist'; +$string['blockdoesnotexistonpage'] = 'This block (id=$a->instanceid) does not exist on this page ($a->url).'; $string['blockcannotinistantiate'] = 'Problem in instantiating block object'; $string['blockcannotread'] = 'Could not read data for blockid= $a '; $string['blockcannotconfig'] = 'This block does not support global configuration'; diff --git a/lib/blocklib.php b/lib/blocklib.php index 7df64a96ab..4f6208e317 100644 --- a/lib/blocklib.php +++ b/lib/blocklib.php @@ -45,6 +45,15 @@ define('BLOCKS_PINNED_BOTH',2); require_once($CFG->libdir.'/pagelib.php'); +class block_not_on_page_exception extends moodle_exception { + public function __construct($instanceid, $page) { + $a = new stdClass; + $a->instanceid = $instanceid; + $a->url = $page->url; + parent::__construct('blockdoesnotexistonpage', '', $page->url, $a); + } +} + /** * This class keeps track of the block that should appear on a moodle_page. * The page to work with as passed to the constructor. @@ -398,6 +407,23 @@ class block_manager implements ArrayAccess { } } + /** + * + * @param integer $instanceid + * @return unknown_type + */ + public function find_instance($instanceid) { + foreach ($this->regions as $region => $notused) { + $this->ensure_instances_exist($region); + foreach($this->blockinstances[$region] as $instance) { + if ($instance->instance->id == $instanceid) { + return $instance; + } + } + } + throw new block_not_on_page_exception($instanceid, $this->page); + } + /// Inner workings ============================================================= /** @@ -681,7 +707,9 @@ function blocks_name_allowed_in_format($name, $pageformat) { * @param $skipblockstables for internal use only. Makes @see blocks_delete_all_for_context() more efficient. */ function blocks_delete_instance($instance, $nolongerused = false, $skipblockstables = false) { - if ($block = block_instance($block->blockname, $instance)) { + global $DB; + + if ($block = block_instance($instance->blockname, $instance)) { $block->instance_delete(); } delete_context(CONTEXT_BLOCK, $instance->id); @@ -822,22 +850,10 @@ function blocks_find_block($blockid, $blocksarray) { return false; } -function blocks_find_instance($instanceid, $blocksarray) { - foreach($blocksarray as $subarray) { - foreach($subarray as $instance) { - if($instance->id == $instanceid) { - return $instance; - } - } - } - return false; -} - // Simple entry point for anyone that wants to use blocks function blocks_setup(&$page, $pinned = BLOCKS_PINNED_FALSE) { - // TODO deprecate. - blocks_execute_url_action($page, $blockmanager,($pinned==BLOCKS_PINNED_TRUE)); $page->blocks->load_blocks(); + blocks_execute_url_action($page, $page->blocks); return $page->blocks; } @@ -933,7 +949,7 @@ function blocks_execute_action($page, &$blockmanager, $blockaction, $instanceori if(empty($instance)) { print_error('invalidblockinstance', '', '', $blockaction); } - blocks_delete_instance($instance, $pinned); + blocks_delete_instance($instance->instance, $pinned); break; case 'moveup': if(empty($instance)) { @@ -1080,10 +1096,8 @@ function blocks_execute_url_action(&$PAGE, &$blockmanager,$pinned=false) { if (!empty($blockid)) { blocks_execute_action($PAGE, $blockmanager, strtolower($blockaction), $blockid, $pinned); - - } - else if (!empty($instanceid)) { - $instance = blocks_find_instance($instanceid, $blockmanager); + } else if (!empty($instanceid)) { + $instance = $blockmanager->find_instance($instanceid); blocks_execute_action($PAGE, $blockmanager, strtolower($blockaction), $instance, $pinned); } } -- 2.39.5