Index: mod/chat/lib.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/chat/lib.php,v retrieving revision 1.99.2.13 diff -u -r1.99.2.13 lib.php --- mod/chat/lib.php 8 Oct 2008 06:41:55 -0000 1.99.2.13 +++ mod/chat/lib.php 22 Dec 2008 05:28:07 -0000 @@ -126,7 +126,7 @@ $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; } } Index: backup/restorelib.php =================================================================== RCS file: /cvsroot/moodle/moodle/backup/restorelib.php,v retrieving revision 1.283.2.56 diff -u -r1.283.2.56 restorelib.php --- backup/restorelib.php 18 Dec 2008 00:18:44 -0000 1.283.2.56 +++ backup/restorelib.php 22 Dec 2008 05:27:57 -0000 @@ -824,7 +824,7 @@ 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... Index: course/lib.php =================================================================== RCS file: /cvsroot/moodle/moodle/course/lib.php,v retrieving revision 1.538.2.66 diff -u -r1.538.2.66 lib.php --- course/lib.php 17 Dec 2008 12:40:09 -0000 1.538.2.66 +++ course/lib.php 22 Dec 2008 05:27:59 -0000 @@ -2393,6 +2393,7 @@ } } + delete_context(CONTEXT_MODULE, $cm->id); return delete_records('course_modules', 'id', $cm->id); } Index: lib/accesslib.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/accesslib.php,v retrieving revision 1.421.2.94 diff -u -r1.421.2.94 accesslib.php --- lib/accesslib.php 19 Dec 2008 08:56:01 -0000 1.421.2.94 +++ lib/accesslib.php 22 Dec 2008 05:28:02 -0000 @@ -2259,6 +2259,7 @@ if ($context = get_record('context', 'contextlevel', $contextlevel, 'instanceid', $instanceid)) { $result = delete_records('role_assignments', 'contextid', $context->id) && delete_records('role_capabilities', 'contextid', $context->id) && + delete_records('role_names', 'contextid', $context->id) && delete_records('context', 'id', $context->id); // do not mark dirty contexts if parents unknown Index: lib/blocklib.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/blocklib.php,v retrieving revision 1.129.2.5 diff -u -r1.129.2.5 blocklib.php --- lib/blocklib.php 19 Dec 2008 09:49:10 -0000 1.129.2.5 +++ lib/blocklib.php 22 Dec 2008 05:28:03 -0000 @@ -208,6 +208,7 @@ } 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 @@ } } +/** + * 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 @@ // 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 Index: mod/lesson/lib.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/lesson/lib.php,v retrieving revision 1.41.2.6 diff -u -r1.41.2.6 lib.php --- mod/lesson/lib.php 26 Nov 2008 14:30:22 -0000 1.41.2.6 +++ mod/lesson/lib.php 22 Dec 2008 05:28:07 -0000 @@ -107,7 +107,7 @@ } $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; } } Index: admin/modules.php =================================================================== RCS file: /cvsroot/moodle/moodle/admin/modules.php,v retrieving revision 1.44.2.7 diff -u -r1.44.2.7 modules.php --- admin/modules.php 30 Apr 2008 13:08:46 -0000 1.44.2.7 +++ admin/modules.php 22 Dec 2008 05:27:52 -0000 @@ -123,6 +123,13 @@ if (!delete_records("course_modules", "module", $module->id)) { notify("Error occurred while deleting all $strmodulename records in course_modules table"); } + if ($coursemods) { + foreach ($coursemods as $coursemod) { + if (!delete_context(CONTEXT_MODULE, $coursemod->id)) { + notify("Could not delete the context for $strmodulename with id = $coursemod->id"); + } + } + } // Then delete all the logs if (!delete_records("log", "module", $module->name)) { Index: mod/quiz/lib.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/quiz/lib.php,v retrieving revision 1.282.2.17 diff -u -r1.282.2.17 lib.php --- mod/quiz/lib.php 10 Dec 2008 02:11:24 -0000 1.282.2.17 +++ mod/quiz/lib.php 22 Dec 2008 05:28:07 -0000 @@ -158,7 +158,7 @@ $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; } }