blocklib: MDL-19010 and now you can delete blocks too!
This commit is contained in:
@@ -455,7 +455,7 @@ class block_base {
|
||||
}
|
||||
|
||||
$page = $this->page;
|
||||
$script = $page->url->out(array('instanceid' => $this->instance->id, 'sesskey' => sesskey()));
|
||||
$script = $page->url->out(false, array('instanceid' => $this->instance->id, 'sesskey' => sesskey()));
|
||||
|
||||
$movebuttons .= '<a class="icon roles" title="'. $this->str->assignroles .'" href="'.$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$context->id.'">' .
|
||||
'<img src="'.$CFG->pixpath.'/i/roles.gif" alt="'.$this->str->assignroles.'" /></a>';
|
||||
|
||||
+7
-6
@@ -29,12 +29,13 @@ $PAGE = page_create_object(PAGE_COURSE_VIEW, $course->id);
|
||||
$pageblocks = blocks_setup($PAGE, BLOCKS_PINNED_BOTH);
|
||||
|
||||
if (!empty($instanceid)) {
|
||||
$blockinstance = blocks_find_instance($instanceid, $pageblocks);
|
||||
if (!$blockinstance || $blockinstance->pageid != $course->id
|
||||
|| $blockinstance->pagetype != 'course-view') {
|
||||
error_log('AJAX commands.php: Bad block ID '.$instanceid);
|
||||
die;
|
||||
}
|
||||
throw new moodle_exception('ajax blocks editing currently broken. MDL-19010');
|
||||
// $blockinstance = blocks_find_instance($instanceid, $pageblocks);
|
||||
// if (!$blockinstance || $blockinstance->pageid != $course->id
|
||||
// || $blockinstance->pagetype != 'course-view') {
|
||||
// error_log('AJAX commands.php: Bad block ID '.$instanceid);
|
||||
// die;
|
||||
// }
|
||||
}
|
||||
|
||||
$context = get_context_instance(CONTEXT_COURSE, $course->id);
|
||||
|
||||
@@ -6,6 +6,7 @@ $string['adminprimarynoedit'] = 'The primary admin cannot be edited by others';
|
||||
$string['authnotexisting'] = 'The autorization plugin doesn\'t exist';
|
||||
$string['authorizeerror'] = 'Authorize error';
|
||||
$string['blockdoesnotexist'] = 'This block does not exist';
|
||||
$string['blockdoesnotexistonpage'] = 'This block (id=$a->instanceid) does not exist on this page ($a->url).';
|
||||
$string['blockcannotinistantiate'] = 'Problem in instantiating block object';
|
||||
$string['blockcannotread'] = 'Could not read data for blockid= $a ';
|
||||
$string['blockcannotconfig'] = 'This block does not support global configuration';
|
||||
|
||||
+33
-19
@@ -45,6 +45,15 @@ define('BLOCKS_PINNED_BOTH',2);
|
||||
|
||||
require_once($CFG->libdir.'/pagelib.php');
|
||||
|
||||
class block_not_on_page_exception extends moodle_exception {
|
||||
public function __construct($instanceid, $page) {
|
||||
$a = new stdClass;
|
||||
$a->instanceid = $instanceid;
|
||||
$a->url = $page->url;
|
||||
parent::__construct('blockdoesnotexistonpage', '', $page->url, $a);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This class keeps track of the block that should appear on a moodle_page.
|
||||
* The page to work with as passed to the constructor.
|
||||
@@ -398,6 +407,23 @@ class block_manager implements ArrayAccess {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param integer $instanceid
|
||||
* @return unknown_type
|
||||
*/
|
||||
public function find_instance($instanceid) {
|
||||
foreach ($this->regions as $region => $notused) {
|
||||
$this->ensure_instances_exist($region);
|
||||
foreach($this->blockinstances[$region] as $instance) {
|
||||
if ($instance->instance->id == $instanceid) {
|
||||
return $instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new block_not_on_page_exception($instanceid, $this->page);
|
||||
}
|
||||
|
||||
/// Inner workings =============================================================
|
||||
|
||||
/**
|
||||
@@ -681,7 +707,9 @@ function blocks_name_allowed_in_format($name, $pageformat) {
|
||||
* @param $skipblockstables for internal use only. Makes @see blocks_delete_all_for_context() more efficient.
|
||||
*/
|
||||
function blocks_delete_instance($instance, $nolongerused = false, $skipblockstables = false) {
|
||||
if ($block = block_instance($block->blockname, $instance)) {
|
||||
global $DB;
|
||||
|
||||
if ($block = block_instance($instance->blockname, $instance)) {
|
||||
$block->instance_delete();
|
||||
}
|
||||
delete_context(CONTEXT_BLOCK, $instance->id);
|
||||
@@ -822,22 +850,10 @@ function blocks_find_block($blockid, $blocksarray) {
|
||||
return false;
|
||||
}
|
||||
|
||||
function blocks_find_instance($instanceid, $blocksarray) {
|
||||
foreach($blocksarray as $subarray) {
|
||||
foreach($subarray as $instance) {
|
||||
if($instance->id == $instanceid) {
|
||||
return $instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Simple entry point for anyone that wants to use blocks
|
||||
function blocks_setup(&$page, $pinned = BLOCKS_PINNED_FALSE) {
|
||||
// TODO deprecate.
|
||||
blocks_execute_url_action($page, $blockmanager,($pinned==BLOCKS_PINNED_TRUE));
|
||||
$page->blocks->load_blocks();
|
||||
blocks_execute_url_action($page, $page->blocks);
|
||||
return $page->blocks;
|
||||
}
|
||||
|
||||
@@ -933,7 +949,7 @@ function blocks_execute_action($page, &$blockmanager, $blockaction, $instanceori
|
||||
if(empty($instance)) {
|
||||
print_error('invalidblockinstance', '', '', $blockaction);
|
||||
}
|
||||
blocks_delete_instance($instance, $pinned);
|
||||
blocks_delete_instance($instance->instance, $pinned);
|
||||
break;
|
||||
case 'moveup':
|
||||
if(empty($instance)) {
|
||||
@@ -1080,10 +1096,8 @@ function blocks_execute_url_action(&$PAGE, &$blockmanager,$pinned=false) {
|
||||
|
||||
if (!empty($blockid)) {
|
||||
blocks_execute_action($PAGE, $blockmanager, strtolower($blockaction), $blockid, $pinned);
|
||||
|
||||
}
|
||||
else if (!empty($instanceid)) {
|
||||
$instance = blocks_find_instance($instanceid, $blockmanager);
|
||||
} else if (!empty($instanceid)) {
|
||||
$instance = $blockmanager->find_instance($instanceid);
|
||||
blocks_execute_action($PAGE, $blockmanager, strtolower($blockaction), $instance, $pinned);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user