NEW FEATURE:

Per-block access controls for creating and editing block instances. Defaults
behave the same as before, and the framework has final say as before.

See relevant discussion at http://moodle.org/mod/forum/discuss.php?d=36444



Other minor changes: Converted "continue" to "break" inside switch statements
(more to the point, although equivalent), change erroneous (but harmless)
"return false" to "break" on failed addition of block instance, removed some
inline comments from block_base (they were duplicated in PHPdoc)
This commit is contained in:
defacer
2005-12-17 04:37:55 +00:00
parent a9033ad5f8
commit 1130633117
2 changed files with 47 additions and 12 deletions
+28 -7
View File
@@ -288,7 +288,7 @@ class block_base {
$movebuttons .= '<a class="icon hide" title="'. $title .'" href="'.$script.'&amp;blockaction=toggle">' .
'<img src="'. $CFG->pixpath.$icon .'" alt="'.$title.'" /></a>';
if ($options & BLOCK_CONFIGURE) {
if ($options & BLOCK_CONFIGURE && $this->user_can_edit()) {
$movebuttons .= '<a class="icon edit" title="'. $this->str->configure .'" href="'.$script.'&amp;blockaction=config">' .
'<img src="'. $CFG->pixpath .'/t/edit.gif" alt="'. $this->str->configure .'" /></a>';
}
@@ -399,8 +399,6 @@ class block_base {
* @return boolean
*/
function config_save($data) {
// Default behavior: save all variables as $CFG properties
// You don't need to override this if you 're satisfied with the above
foreach ($data as $name => $value) {
set_config($name, $value);
}
@@ -423,7 +421,6 @@ class block_base {
* @return int
*/
function preferred_width() {
// Default case: the block wants to be 180 pixels wide
return 180;
}
@@ -432,17 +429,15 @@ class block_base {
* @return boolean
*/
function hide_header() {
//Default, false--> the header is shown
return false;
}
/**
* Default case: just an id for the block, with our name in it
* Default case: an id with the instance and a class with our name in it
* @return array
* @todo finish documenting this function
*/
function html_attributes() {
// Default case: an id with the instance and a class with our name in it
return array('id' => 'inst'.$this->instance->id, 'class' => 'block_'. $this->name());
}
@@ -573,6 +568,32 @@ class block_base {
return true;
}
/**
* Allows the block class to have a say in the user's ability to edit (i.e., configure) blocks of this type.
* The framework has first say in whether this will be allowed (e.g., no editing allowed unless in edit mode)
* but if the framework does allow it, the block can still decide to refuse.
* @return boolean
* @todo finish documenting this function
*/
function user_can_edit() {
return true;
}
/**
* Allows the block class to have a say in the user's ability to create new instances of this block.
* The framework has first say in whether this will be allowed (e.g., no adding allowed unless in edit mode)
* but if the framework does allow it, the block can still decide to refuse.
* This function has access to the complete page object, the creation related to which is being determined.
* @return boolean
* @todo finish documenting this function
*/
function user_can_addto(&$page) {
if(empty($page)) {
die('a');
}
return true;
}
}
/**
+19 -5
View File
@@ -83,11 +83,11 @@ function get_class_constructor($classname) {
}
//This function retrieves a method-defined property of a class WITHOUT instantiating an object
function block_method_result($blockname, $method) {
function block_method_result($blockname, $method, $param = NULL) {
if(!block_load_class($blockname)) {
return NULL;
}
return call_user_func(array('block_'.$blockname, $method));
return call_user_func(array('block_'.$blockname, $method), $param);
}
//This function creates a new object of the specified block class
@@ -417,8 +417,14 @@ function blocks_execute_action($page, &$pageblocks, $blockaction, $instanceorid,
// Create the object WITHOUT instance data.
$blockobject = block_instance($block->name);
if ($blockobject === false) {
continue;
break;
}
// First of all check to see if the block wants to be edited
if(!$blockobject->user_can_edit()) {
break;
}
// Now get the title and AFTER that load up the instance
$blocktitle = $blockobject->get_title();
$blockobject->_load_instance($instance);
@@ -587,12 +593,17 @@ function blocks_execute_action($page, &$pageblocks, $blockaction, $instanceorid,
if(empty($block) || !$block->visible) {
// Only allow adding if the block exists and is enabled
return false;
break;
}
if(!$block->multiple && blocks_find_block($blockid, $pageblocks) !== false) {
// If no multiples are allowed and we already have one, return now
return false;
break;
}
if(!block_method_result($block->name, 'user_can_addto', $page)) {
// If the block doesn't want to be added...
break;
}
$newpos = $page->blocks_default_position();
@@ -779,6 +790,9 @@ function blocks_print_adminblock(&$page, &$pageblocks) {
if ($blockobject === false) {
continue;
}
if(!$blockobject->user_can_addto($page)) {
continue;
}
$menu[$block->id] = $blockobject->get_title();
}
asort($menu);