} 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()) {
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);
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!
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;
}
}
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);
}
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