From c6c9b4facacbadf91b3fd57888f2003e6f79ce48 Mon Sep 17 00:00:00 2001 From: tjhunt Date: Fri, 9 Jan 2009 06:16:52 +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 | 21 ++++++++++++++++++++- mod/chat/lib.php | 2 +- mod/lesson/lib.php | 2 +- mod/quiz/lib.php | 2 +- 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/backup/restorelib.php b/backup/restorelib.php index 472d7d7a4e2..0a26a75f5ad 100644 --- a/backup/restorelib.php +++ b/backup/restorelib.php @@ -824,7 +824,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3); global $CFG; $status = true; - delete_records('block_instance', '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 e178e1858b9..6fe517745ef 100644 --- a/lib/blocklib.php +++ b/lib/blocklib.php @@ -208,6 +208,7 @@ function blocks_delete_instance($instance,$pinned=false) { } else { // Now kill the db record; delete_records('block_instance', 'id', $instance->id); + delete_context(CONTEXT_BLOCK, $instance->id); // And now, decrement the weight of all blocks after this one execute_sql('UPDATE '.$CFG->prefix.'block_instance SET weight = weight - 1 WHERE pagetype = \''.$instance->pagetype. '\' AND pageid = '.$instance->pageid.' AND position = \''.$instance->position. @@ -972,6 +973,24 @@ function blocks_print_adminblock(&$page, &$pageblocks) { } } +/** + * 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) { + if ($instances = get_records_select('block_instance', 'pageid = ' . $pageid . ' AND pagetype = ' . $pagetype)) { + foreach ($instances as $instance) { + delete_context(CONTEXT_BLOCK, $instance->id); // Ingore any failures here. + } + } + return delete_records('block_instance', '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; @@ -1003,7 +1022,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 - delete_records('block_instance', '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 de1fc5c9133..1b0f6bd46e0 100644 --- a/mod/chat/lib.php +++ b/mod/chat/lib.php @@ -126,7 +126,7 @@ function chat_delete_instance($id) { $pagetypes = page_import_types('mod/chat/'); foreach($pagetypes as $pagetype) { - if(!delete_records('block_instance', '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 cdf71bb8acf..9c36b08db9d 100644 --- a/mod/lesson/lib.php +++ b/mod/lesson/lib.php @@ -107,7 +107,7 @@ function lesson_delete_instance($id) { } $pagetypes = page_import_types('mod/lesson/'); foreach ($pagetypes as $pagetype) { - if (!delete_records('block_instance', '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 05bc11627dc..3bb465e46ad 100644 --- a/mod/quiz/lib.php +++ b/mod/quiz/lib.php @@ -158,7 +158,7 @@ function quiz_delete_instance($id) { $pagetypes = page_import_types('mod/quiz/'); foreach($pagetypes as $pagetype) { - if(!delete_records('block_instance', 'pageid', $quiz->id, 'pagetype', $pagetype)) { + if(!blocks_delete_all_on_page($pagetype, $quiz->id)) { $result = false; } }