accesslib: MDL-17626 delete the context whenever a block is deleted. This includes a new helper function blocks_delete_all_on_page.

This commit is contained in:
tjhunt
2009-01-09 06:16:52 +00:00
parent 2a538623e4
commit c6c9b4faca
5 changed files with 24 additions and 5 deletions
+1 -1
View File
@@ -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...
+20 -1
View File
@@ -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
+1 -1
View File
@@ -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;
}
}
+1 -1
View File
@@ -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;
}
}
+1 -1
View File
@@ -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;
}
}