diff --git a/blocks/moodleblock.class.php b/blocks/moodleblock.class.php
index b00f39609e5..4357c6ff397 100644
--- a/blocks/moodleblock.class.php
+++ b/blocks/moodleblock.class.php
@@ -288,7 +288,7 @@ class block_base {
$movebuttons .= '' .
'
';
- if ($options & BLOCK_CONFIGURE) {
+ if ($options & BLOCK_CONFIGURE && $this->user_can_edit()) {
$movebuttons .= '' .
'
';
}
@@ -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;
+ }
+
}
/**
diff --git a/lib/blocklib.php b/lib/blocklib.php
index 58b7cc3505b..9872ae0e09e 100644
--- a/lib/blocklib.php
+++ b/lib/blocklib.php
@@ -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);