Skip to content

Commit

Permalink
allow deleting of block when original code not present
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Sep 3, 2006
1 parent 460a7a6 commit e9a2075
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
14 changes: 9 additions & 5 deletions admin/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,24 @@
} else {
// Inform block it's about to be deleted
$blockobject = block_instance($block->name);
$blockobject->before_delete();

// Delete block
if (!delete_records('block', 'id', $block->id)) {
notify("Error occurred while deleting the $strblockname record from blocks table");
if ($blockobject) {
$blockobject->before_delete(); //only if we can create instance, block might have been already removed
}

// First delete instances and then block
$instances = get_records('block_instance', 'blockid', $block->id);
if(!empty($instances)) {
foreach($instances as $instance) {
blocks_delete_instance($instance);
blocks_delete_instance($instance, true);
}
}

// Delete block
if (!delete_records('block', 'id', $block->id)) {
notify("Error occurred while deleting the $strblockname record from blocks table");
}

// Then the tables themselves

if ($tables = $db->Metatables()) {
Expand Down
2 changes: 1 addition & 1 deletion admin/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

page_map_class(PAGE_ADMIN, 'page_admin');

$PAGE = page_create_object(PAGE_ADMIN, 0); // there must be some id number
$PAGE = page_create_object(PAGE_ADMIN, 0); // there must be any constant id number

$section = optional_param('section', '', PARAM_ALPHAEXT);

Expand Down
2 changes: 1 addition & 1 deletion lib/adminlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2188,7 +2188,7 @@ function admin_externalpage_setup($section, $adminroot) {

page_map_class(PAGE_ADMIN, 'page_admin');

$PAGE = page_create_object(PAGE_ADMIN, 0); // there must be some id number
$PAGE = page_create_object(PAGE_ADMIN, 0); // there must be any constant id number

$PAGE->init_extra($section); // hack alert!

Expand Down
16 changes: 7 additions & 9 deletions lib/blocklib.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
function block_is_compatible($blockname) {
global $CFG;

$file = file($CFG->dirroot.'/blocks/'.$blockname.'/block_'.$blockname.'.php');
$file = @file($CFG->dirroot.'/blocks/'.$blockname.'/block_'.$blockname.'.php'); // ignore errors when file does not exist
if(empty($file)) {
return NULL;
}
Expand Down Expand Up @@ -119,7 +119,7 @@ function block_load_class($blockname) {
}

require_once($CFG->dirroot.'/blocks/moodleblock.class.php');
include_once($CFG->dirroot.'/blocks/'.$blockname.'/block_'.$blockname.'.php');
@include_once($CFG->dirroot.'/blocks/'.$blockname.'/block_'.$blockname.'.php'); // do not throw errors if block code not present

return class_exists($classname);
}
Expand Down Expand Up @@ -189,16 +189,14 @@ function blocks_name_allowed_in_format($name, $pageformat) {
function blocks_delete_instance($instance,$pinned=false) {
global $CFG;

// Get the block object and call instance_delete() first
// Get the block object and call instance_delete() if possible
if(!$record = blocks_get_record($instance->blockid)) {
return false;
}
if(!$obj = block_instance($record->name, $instance)) {
return false;
if(!$obj = block_instance($record->name, $instance)) {
// Return value ignored
$obj->instance_delete();
}
}

// Return value ignored
$obj->instance_delete();
if (!empty($pinned)) {
delete_records('block_pinned', 'id', $instance->id);
// And now, decrement the weight of all blocks after this one
Expand Down

0 comments on commit e9a2075

Please sign in to comment.