Skip to content

Commit

Permalink
accesslib: MDL-17626 delete the context whenever a block is deleted. …
Browse files Browse the repository at this point in the history
…This includes a new helper function blocks_delete_all_on_page.
  • Loading branch information
tjhunt committed Jan 9, 2009
1 parent a3c9960 commit 19f5b2d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion backup/restorelib.php
Expand Up @@ -792,7 +792,7 @@ function restore_create_blocks($restore, $backup_block_format, $blockinfo, $xml_
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...
Expand Down
26 changes: 23 additions & 3 deletions lib/blocklib.php
Expand Up @@ -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
Expand Down Expand Up @@ -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'
);
Expand Down Expand Up @@ -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.'&amp;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;

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion mod/chat/lib.php
Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion mod/lesson/lib.php
Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion mod/quiz/lib.php
Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 19f5b2d

Please sign in to comment.