From 19f5b2dbeeceec32f5fca0f43df463ce72716c8a Mon Sep 17 00:00:00 2001 From: tjhunt Date: Fri, 9 Jan 2009 06:16:36 +0000 Subject: [PATCH] accesslib: MDL-17626 delete the context whenever a block is deleted. This includes a new helper function blocks_delete_all_on_page. --- backup/restorelib.php | 2 +- lib/blocklib.php | 26 +++++++++++++++++++++++--- mod/chat/lib.php | 2 +- mod/lesson/lib.php | 2 +- mod/quiz/lib.php | 2 +- 5 files changed, 27 insertions(+), 7 deletions(-) diff --git a/backup/restorelib.php b/backup/restorelib.php index 7418b4e650..365745bba0 100644 --- a/backup/restorelib.php +++ b/backup/restorelib.php @@ -792,7 +792,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3); global $CFG, $DB; $status = true; - $DB->delete_records('block_instance', array('pageid'=>$restore->course_id, 'pagetype'=>PAGE_COURSE_VIEW)); + blocks_delete_all_on_page(PAGE_COURSE_VIEW, $restore->course_id); if (empty($backup_block_format)) { // This is a backup from Moodle < 1.5 if (empty($blockinfo)) { // Looks like it's from Moodle < 1.3. Let's give the course default blocks... diff --git a/lib/blocklib.php b/lib/blocklib.php index eb9aa3cc20..0c090d6fd6 100644 --- a/lib/blocklib.php +++ b/lib/blocklib.php @@ -210,6 +210,7 @@ function blocks_delete_instance($instance,$pinned=false) { } else { // Now kill the db record; $DB->delete_records('block_instance', array('id'=>$instance->id)); + delete_context(CONTEXT_BLOCK, $instance->id); // And now, decrement the weight of all blocks after this one $sql = "UPDATE {block_instance} SET weight = weight - 1 @@ -502,7 +503,7 @@ function blocks_execute_action($page, &$pageblocks, $blockaction, $instanceorid, // Define the data we're going to silently include in the instance config form here, // so we can strip them from the submitted data BEFORE serializing it. $hiddendata = array( - 'sesskey' => $USER->sesskey, + 'sesskey' => sesskey(), 'instanceid' => $instance->id, 'blockaction' => 'config' ); @@ -981,12 +982,31 @@ function blocks_print_adminblock(&$page, &$pageblocks) { } asort($menu); - $target = $page->url_get_full(array('sesskey' => $USER->sesskey, 'blockaction' => 'add')); + $target = $page->url_get_full(array('sesskey' => sesskey(), 'blockaction' => 'add')); $content = popup_form($target.'&blockid=', $menu, 'add_block', '', $stradd .'...', '', '', true); print_side_block($strblocks, $content, NULL, NULL, NULL, array('class' => 'block_adminblock')); } } +/** + * Delete all the blocks from a particular page. + * + * @param string $pagetype the page type. + * @param integer $pageid the page id. + * @return success of failure. + */ +function blocks_delete_all_on_page($pagetype, $pageid) { + global $DB; + if ($instances = $DB->get_records('block_instance', array('pageid' => $pageid, 'pagetype' => $pagetype))) { + foreach ($instances as $instance) { + delete_context(CONTEXT_BLOCK, $instance->id); // Ingore any failures here. + } + } + return $DB->delete_records('block_instance', array('pageid' => $pageid, 'pagetype' => $pagetype)); +} + +// Dispite what this function is called, it seems to be mostly used to populate +// the default blocks when a new course (or whatever) is created. function blocks_repopulate_page($page) { global $CFG, $DB; @@ -1018,7 +1038,7 @@ function blocks_repopulate_page($page) { // indexed and the indexes match, so we can work straight away... but CAREFULLY! // Ready to start creating block instances, but first drop any existing ones - $DB->delete_records('block_instance', array('pageid'=>$page->get_id(), 'pagetype'=>$page->get_type())); + blocks_delete_all_on_page($page->get_type(), $page->get_id()); // Here we slyly count $posblocks and NOT $positions. This can actually make a difference // if the textual representation has undefined slots in the end. So we only work with as many diff --git a/mod/chat/lib.php b/mod/chat/lib.php index 1b3feb4060..212e4bc263 100644 --- a/mod/chat/lib.php +++ b/mod/chat/lib.php @@ -153,7 +153,7 @@ function chat_delete_instance($id) { $pagetypes = page_import_types('mod/chat/'); foreach($pagetypes as $pagetype) { - if (!$DB->delete_records('block_instance', array('pageid'=>$chat->id, 'pagetype'=>$pagetype))) { + if(!blocks_delete_all_on_page($pagetype, $chat->id)) { $result = false; } } diff --git a/mod/lesson/lib.php b/mod/lesson/lib.php index 1d7ab950ed..693d59cc6b 100644 --- a/mod/lesson/lib.php +++ b/mod/lesson/lib.php @@ -119,7 +119,7 @@ function lesson_delete_instance($id) { } $pagetypes = page_import_types('mod/lesson/'); foreach ($pagetypes as $pagetype) { - if (!$DB->delete_records('block_instance', array('pageid'=>$lesson->id, 'pagetype'=>$pagetype))) { + if (!blocks_delete_all_on_page($pagetype, $lesson->id)) { $result = false; } } diff --git a/mod/quiz/lib.php b/mod/quiz/lib.php index 177837dca1..89dc61f9ff 100644 --- a/mod/quiz/lib.php +++ b/mod/quiz/lib.php @@ -179,7 +179,7 @@ function quiz_delete_instance($id) { $pagetypes = page_import_types('mod/quiz/'); foreach($pagetypes as $pagetype) { - if (!$DB->delete_records('block_instance', array('pageid'=>$quiz->id, 'pagetype'=>$pagetype))) { + if(!blocks_delete_all_on_page($pagetype, $quiz->id)) { $result = false; } } -- 2.39.5