From 1345403a7c593c9423d3fb2cb3d9611926dc0ace Mon Sep 17 00:00:00 2001
From: defacer This Appendix will discuss the base class block_base from which all other block classes derive, and present each and every method that can be overridden by block developers in detail. Methods that should NOT be overridden are explicitly referred to as such. After reading this Appendix, you will have a clear understanding of every method which you should or could override to implement functionality for your block. This Appendix will discuss the base class block_base from which all other block classes derive, and present each and every method that can be overridden by block developers in detail. Methods that should not be overridden are explicitly referred to as such. After reading this Appendix, you will have a clear understanding of every method which you should or could override to implement functionality for your block. The methods are divided into three categories: those you may use and override in your block, those that you may NOT override but might want to use, and those that should NEITHER be used NOR overridden. In each category, methods are presented in alphabetical order. The methods are divided into three categories: those you may use and override in your block, those that you may not override but might want to use, and those internal methods that should neither be used nor overridden. In each category, methods are presented in alphabetical order. This method allows you to control which formats your block can be added to. Currently "formats" refers to course formats only, but in the future it may also include other kinds of pages than courses as Moodle is developed. You should return an array with keys being course format names and values being boolean true or false, denoting if your block is to be allowed in each specific format. This method allows you to control which formats your block can be added to. Currently "formats" refers to course formats only, but in the future it may also include other kinds of pages than courses as Moodle is developed. You should return an array with the keys being page format names and the values being booleans (true or false). Your block is only allowed to appear in those formats where the value is true. Valid options for format names are: "social", "topics", "weeks" (referring to the three standard course formats), "site" (referring to the front page) and "all" (this will be used for those formats you have not explicitly allowed or disallowed). If a format does not appear at all in the array keys and there is no "all" key either, then your block will not be allowed in that format. This method allows you to choose how to display the global configuration screen for your block. Override it if you need something much more complex than the default implementation allows you to do. However, keep these points in mind: This method allows you to choose how to display the global configuration screen for your block. This is the screen that the administrator is presented with when he chooses "Settings..." for a specific block. Override it if you need something much more complex than the default implementation allows you to do. However, keep these points in mind: You should return a boolean value denoting the success or failure of your method's actions. This method allows you to override the storage mechanism for your global configuration data. The received argument is an associative array, with the keys being setting names and the values being setting values. The default implementation saves everything as Moodle $CFG variables. Note that what you receive as an argument will NOT be all of the POST data; Moodle will automatically strip out some bits it added itself (such as "sesskey"), so it's quite safe to save everything you receive. Note that $data does not hold all of the submitted POST data because Moodle adds some hidden fields to the form in order to be able to process it. However, before calling this method it strips the hidden fields from the received data and so when this method is called only the "real" configuration data remain. You should return a boolean value denoting the success or failure of your method's actions. This method should, when called, populate the $this->content variable of your block. Populating the variable means:
- EITHER EITHER OR OR If you set ALL of these variables to their default "empty" values (empty arrays for the arrays and empty strings for the strings), the block will NOT be displayed at all except to editing users. This is a good way of having your block NOT displayed if there is no reason for it. If you set all of these variables to their default "empty" values (empty arrays for the arrays and empty strings for the strings), the block will not be displayed at all except to editing users. This is a good way of having your block hide itself to unclutter the screen if there is no reason to have it displayed. Your function should return the fully constructed $this->content variable. You should also include a check that, if this variable is not exactly equal to NULL, returns the existing value instead of calculating it once more. If you fail to do this, Moodle will suffer a performance hit. Before starting to populate $this->content, you should also include a simple caching check. If $this->content is exactly equal to NULL then proceed as normally, while if it is not, return the existing value instead of calculating it once more. If you fail to do this, Moodle will suffer a performance hit. In any case, your method should return the fully constructed $this->content variable. This method should return a boolean value taht denotes whether your block wants to present a configuration interface to site admins or not. The configuration that this interface offers will impact all instances of the block equally. To fully implement this configuration you will need to take some additional steps apart from overriding this method; refer to the full guide for more information. This method should return a boolean value that denotes whether your block wants to present a configuration interface to site admins or not. The configuration that this interface offers will impact all instances of the block equally. To actually implement the configuration interface, you will either need to rely on the default config_print method or override it. The full guide contains more information on this. This method should return a boolean value that denotes whether your block wants to hide its title part. Thus, if you override it to return true, your block will not display a title unless the current user is in editing mode. This method should return a boolean value that denotes whether your block wants to hide its header (or title). Thus, if you override it to return true, your block will not display a title unless the current user is in editing mode. This method should return an associative array of HTML attributes that will be given to your block's container element when Moodle sends the page to the user's browser. No sanitization will be performed in these elements at all. You should always include those attributes that the default implementation sets, but this is not strictly obligatory and you can choose to ignore it if there is reason to (is there?). This method should return an associative array of HTML attributes that will be given to your block's container element when Moodle constructs the output HTML. No sanitization will be performed in these elements at all. If you intend to override this method, you should return the default attributes as well as those you add yourself. The recommended way to do this is: This method must be implemented for all blocks. It has to assign meaningful values to the object variables $this->title, $this->content_type (which must be either BLOCK_TYPE_TEXT or BLOCK_TYPE_LIST) and $this->version (which is used by Moodle for performing automatic updates when available). No return value is expected. This method must be implemented for all blocks. It has to assign meaningful values to the object variables $this->title, $this->content_type (which must be either BLOCK_TYPE_TEXT or BLOCK_TYPE_LIST) and $this->version (which is used by Moodle for performing automatic updates when available). No return value is expected from this method. This method should return a boolean value. True indicates that your block wants to have per-instance configuration, while false means it does not. If you do want to implement instance configuration, you will need to take some additional steps apart from overriding this method; refer to the full guide for more information. This method should return a boolean value. True indicates that your block wants to have per-instance configuration, while false means it does not. If you do want to implement instance configuration, you will need to take some additional steps apart from overriding this method; refer to the full guide for more information. This method's return value is irrelevant if instance_allow_multiple() returns true; it is assumed that if you want multiple instances then each instance needs its own configuration. This method's return value is irrelevant if instance_allow_multiple returns true; it is assumed that if you want multiple instances then each instance needs its own configuration. This method should return a boolean value, indicating whether you want to allow multiple instances of this block in a single course or not. If you do allow multiple instances, it is assumed that you will also be providing per-instance configuration for the block. Thus, you will need to take some additional steps apart from overriding this method; refer to the full guide for more information. This method should return a boolean value, indicating whether you want to allow multiple instances of this block in the same page or not. If you do allow multiple instances, it is assumed that you will also be providing per-instance configuration for the block. Thus, you will need to take some additional steps apart from overriding this method; refer to the full guide for more information. The configuration must be stored in the "configdata" field of your instance record in the database so that Moodle can auto-load it when your block is constructed. However, you may still want to override this method if you need to take some additional action apart from saving the data. In that case, you really should do what data processing you want and then call parent::instance_config_save($data) with your new $data array. This will keep your block from becoming broken if the default implementation of instance_config_save changes in the future. Note that what you receive as an argument will not be all of the POST data; Moodle will automatically strip out some bits it added itself (such as "sesskey"), so it's quite safe to save everything you receive. Note that $data does not hold all of the submitted POST data because Moodle adds some hidden fields to the form in order to be able to process it. However, before calling this method it strips the hidden fields from the received data and so when this method is called only the "real" configuration data remain. You should return a boolean value denoting the success or failure of your method's actions. This method should return an integer value, which is the number of pixels of width your block wants to take up when displayed. Moodle will try to honor your request, but as this is actually up to the implementation of the format of the page your block is being displayed in, there is no guarantee. You might actually get exactly what you want or any other width. This method should return an integer value, which is the number of pixels of width your block wants to take up when displayed. Moodle will try to honor your request, but this is actually up to the implementation of the format of the page your block is being displayed in and therefore no guarantee is given. You might get exactly what you want or any other width the format decides to give you, although obviously an effort to accomodate your block will be made. Most display logic at this point allocates the maximum width requested by the blocks that are going to be displayed, bounding it both downwards and upwards to avoid having a bad-behaving block break the format. This method should cause your block to recalculate its content immediately. If you follow the guidelines for get_content, which say to respect the current content value unless it is NULL, then the default implementation will do the job just fine. This method should cause your block to recalculate its content immediately. If you follow the guidelines for get_content, which say to respect the current content value unless it is NULL, then the default implementation will do the job just fine. You should return the new value of $this->content after refreshing it. This method is automatically called by the framework immediately after your instance data (which includes the page type and id and all instance configuration data) is loaded from the database. If there is some action that you need to take as soon as this data becomes available and which cannot be taken earlier, you should override this method. The instance data will be available in the variables $this->instance and $this->config. This method should not return anything at all. This method returns the value of $this->content_type, and is the preferred way of accessing that variable. It is guaranteed to always work, now and forever. Directly accessing the variable is not recommended; future library changes may break compatibility with code that does so. This method returns the value of $this->title, and is the preferred way of accessing that variable. It is guaranteed to always work, now and forever. Directly accessing the variable is not recommended; future library changes may break compatibility with code that does so. This method returns the value of $this->version, and is the preferred way of accessing that variable. It is guaranteed to always work, now and forever. Directly accessing the variable is not recommended; future library changes may break compatibility with code that does so. This method returns the internal name of your block inside Moodle, without the block_ prefix. Obtaining the name of a block object is sometimes useful because it can be used to write code that is agnostic to the actual block's name (and thus more generic and reusable). For an example of this technique, see the config_print method. This is a private method; no description is given. This is a private method; no description is given. This is a private method; no description is given. This is a private method; no description is given. This is a private method; no description is given. The class block_base also has a few standard member variables which its methods manipulate. These variables and their uses are explained hereafter: The class block_base also has a few standard member variables which its methods manipulate. These variables, the purpose of each and the type of data they are expected to hold is explained in the next section of this Appendix. This variable holds all the specialized instance configuration data that have been provided for this specific block instance (object). It is an object of type stdClass, with member variables directly corresponding to the HTML <input> elements in the block's config_instance.html file. The variable is initialized just after the block object is constructed, immediately before specialization is called for the object. It is possible that the block has no instance configuration, in which case the variable will be NULL. It is obvious that there is a direct relationship between this variable and the configdata field in the mdl_block_instance table. However, it is strongly advised that you refrain from acessing the configdata field yourself. If you absolutely must update its value at any time, it is recommended that you call the method instance_config_save to do the actual work. This variable should be a string that contains the human-readable name of the block. It is used to refer to blocks of that type everywhere, for example in the administrator's block configuration screen. The variable is expected to have a valid value after the framework calls the init method for each block. In the case of blocks which may want to configure their title dynamically through instance configuration, it is still essential to provide a valid title inside init. This title may then be overridden when the specialization method is called by the framework. This variable instructs Moodle on what type of content it should assume the block provides. It is essential that it has a meaningful value, as Moodle depends on this for correctly displaying the block on screen. Consequently, this variable is closely tied with the varaible $this->content. The variable is expected to have a valid value after the framework calls the init method for each block. This variable instructs Moodle on what type of content it should assume the block has. It is essential that it has a meaningful value, as Moodle depends on this for correctly displaying the block on screen. Consequently, this variable is closely tied with the varaible $this->content. The variable is expected to have a valid value after the framework calls the init method for each block. The only valid values for this variable are the two named constants BLOCK_TYPE_TEXT and BLOCK_TYPE_LIST. !!!UNFINISHED!!! This variable instructs Moodle on what type of content it should assume the block provides. It is essential that it has a meaningful value, as Moodle depends on this for correctly displaying the block on screen. Consequently, this variable is closely tied with the varaible $this->content. The variable is expected to have a valid value after the framework calls the init method for each block. The only valid values for this variable are the two named constants BLOCK_TYPE_TEXT and BLOCK_TYPE_LIST. This variable holds all the actual content that is displayed inside each block. Valid values for it are either NULL or an object of class stdClass, which must have specific member variables set as explained below. Normally, it begins life with a value of NULL and it becomes fully constructed (i.e., an object) when the get_content is called. After it is fully constructed, this object is expected to have certain properties, depending on the value of $this->content_type. Specifically:
+
+ Configure That Out
@@ -329,7 +354,7 @@ function instance_allow_multiple() {
The Effects of Globalization
@@ -542,13 +567,13 @@ function get_content() {
Appendix A: Reference
-
+
-
Methods you can freely use and override:
@@ -562,10 +587,12 @@ function applicable_formats() {
return array('all' => true);
}
-
defining $this->content->text and $this->content->footer if your block is of type BLOCK_TYPE_TEXT. Both of these should be strings which can contain arbitrary HTML.
defining $this->content->text and $this->content->footer if your block is of type BLOCK_TYPE_TEXT. Both of these should be strings, and can contain arbitrary HTML.
defining $this->content->items, $this->content->icons and $this->content->footer if your block is of type BLOCK_TYPE_LIST. The first two should be numerically indexed arrays with the same number of elements. $this->content->items can contain arbitrary HTML while $this->content->icons should only contain fully-qualified HTML img tags. $this->content->footer is a string, as above.
defining $this->content->items, $this->content->icons and $this->content->footer if your block is of type BLOCK_TYPE_LIST. The first two should be numerically indexed arrays having the exact same number of elements. $this->content->items is an array of strings that can contain arbitrary HTML while $this->content->icons also contains should strings, but those must be fully-qualified HTML <img> tags and nothing else. $this->content->footer is a string, as above.
+function html_attributes() {
+ $attrs = parent::html_attributes();
+ // Add your own attributes here, e.g.
+ // $attrs['width'] = '50%';
+ return $attrs;
+}
@@ -693,7 +733,9 @@ function init() {
$this->version = 2004111200;
}
- Methods which you should not override but may want to use:
+function get_content_type() {
+ return $this->content_type;
+}
+
+
+function get_title() {
+ return $this->title;
+}
+
+
+function get_version() {
+ return $this->version;
+}
+
+
+function name() {
+ static $myname;
+ if ($myname === NULL) {
+ $myname = strtolower(get_class($this));
+ $myname = substr($myname, strpos($myname, '_') + 1);
+ }
+ return $myname;
+}
+
+ Methods which you should not override and not use at all:
Class variables:
+
+
+
+
-
+
+
+
+
+
+
+
This member variable holds all the specific information that differentiates one block instance (i.e., the PHP object that embodies it) from another. It is an object of type stdClass retrieved by calling get_record on the table mdl_block_instance. Its member variables, then, directly correspond to the fields of that table. It is initialized immediately after the block object itself is constructed.
+ +This variable is a string that contains the human-readable name of the block. It is used to refer to blocks of that type throughout Moodle, for example in the administrator's block configuration screen and in the editing teacher's add block menu. It is also the title that is printed when the block is displayed on screen, although blocks can specifically change this title to something else if they wish (see below). The variable is expected to have a valid value after the framework calls the init method for each object.
+ +In the case of blocks which may want to configure their title dynamically through instance configuration, it is still essential to provide a valid title inside init. This title may then be overridden when the specialization method is called by the framework:
+ ++function specialization() { + // At this point, $this->instance and $this->config are available + // for use. We can now change the title to whatever we want. + $this->title = $this->config->variable_holding_the_title; +}+
This variable should hold each block's version number in the form YYYYMMDDXX, as per the convention throughout Moodle. The version number is used by Moodle to detect when a block has been upgraded and it consequently needs to run the block's upgrade code to bring the "old" version of the block's data up to date. The variable is expected to have a valid value after the framework calls the init method for each block.
-Most blocks do not keep complex data of their own in the database the way that modules do, so in most cases nothing actually happens during a block version upgrade. However, the version number is displayed in the administration interface for blocks. It is good practice therefore to change your block's version number when it gains new functionality or receives important bug fixes, to enable site administrators to easily see if they have the latest version or they need to upgrade.
-UNFINISHED!!!
+ +This variable should hold each block's version number in the form YYYYMMDDXX, as per the convention throughout Moodle. The version number is used by Moodle to detect when a block has been upgraded and it consequently needs to run the block's upgrade code to bring the "old" version of the block's data up to date. The variable is expected to have a valid value after the framework calls the init method for each block.
+ +Most blocks do not keep complex data of their own in the database the way that modules do, so in most cases nothing actually happens during a block version upgrade. However, the version number is displayed in the administration interface for blocks. It is good practice therefore to change your block's version number when it gains new functionality or receives important bug fixes, to enable site administrators to easily identify the exact version of the block they are working with.
+Appearing throughout the code related to the Blocks API, there is a number of predefined constants that are utilized to avoid the use of "magic numbers" in the code. These constants are:
-This variable should be a string that contains the human-readable name of the block. It is used to refer to blocks of that type everywhere, for example in the administrator's block configuration screen. The variable is expected to have a valid value after the framework calls the init method for each block.
+ +This is one of the two valid values for the $this->content_type member variable of every block. Its value specifies the exact requirements that Moodle will then have for $this->content.
+This variable should be a string that contains the human-readable name of the block. It is used to refer to blocks of that type everywhere, for example in the administrator's block configuration screen. The variable is expected to have a valid value after the framework calls the init method for each block.
+ +This is one of the two valid values for the $this->content_type member variable of every block. Its value specifies the exact requirements that Moodle will then have for $this->content.
+In Moodle 1.4, all block classes were required to have a name like CourseBlock_something and the base class from which the derived was MoodleBlock. This has changed in Moodle 1.5, to bring the naming conventions in line with other object-oriented aspects of Moodle (for example there are classes enrolment_base, resource_base etc). The new block classes should instead be named like block_something and derive from block_base. This means that in order to make a block compatible with Moodle 1.5, you need to change the class definition +
In Moodle 1.4, all block classes were required to have a name like CourseBlock_something and the base class from which the derived was MoodleBlock. This has changed in Moodle 1.5, to bring the naming conventions in line with other object-oriented aspects of Moodle (for example there are classes enrolment_base, resource_base etc). The new block classes should instead be named like block_something and derive from block_base. This means that in order to make a block compatible with Moodle 1.5, you need to change the class definition
class CourseBlock_online_users extends MoodleBlock { ... }-
to
+to
class block_online_users extends block_base { ... } @@ -983,14 +1141,19 @@ class block_online_users extends block_base { ... }In Moodle 1.4, blocks could only have what are now (in Moodle 1.5) called "global configuration" options, to differentiate from the new "instance configuration" options. If your block has support for configuration, you will need to take these steps:
--
+ + + \ No newline at end of file diff --git a/blocks/moodleblock.class.php b/blocks/moodleblock.class.php index be502709ef..0d4195362e 100644 --- a/blocks/moodleblock.class.php +++ b/blocks/moodleblock.class.php @@ -60,7 +60,7 @@ class block_base { var $content = NULL; /** - * A string generated by {@link add_edit_controls()} to display block manipulation links when the user is in editing mode. + * A string generated by {@link _add_edit_controls()} to display block manipulation links when the user is in editing mode. * @var string $edit_controls */ var $edit_controls = NULL; @@ -186,7 +186,7 @@ class block_base { /** * Display the block! */ - function print_block() { + function _print_block() { // Wrap the title in a floating DIV, in case we have edit controls to display // These controls will always be wrapped on a right-floating DIV $title = '- Rename your config.html file to config_global.html
-- Edit the newly renamed file and REMOVE the <form> tag (Moodle now wraps your configuration in a form automatically)
-- If you are using any HTML <input> tags other than those that directly affect your configuration (for example, "sesskey"), REMOVE those too (Moodle will add them automatically as required)
-- If you have overridden print_config(), rename your method to config_print()
-- If you have overridden handle_config(), rename your method to config_save()
++
- Rename your config.html file to config_global.html.
+- Edit the newly renamed file and completely remove the <form> tag (Moodle now wraps your configuration in a form automatically).
+- If you are using any HTML <input> tags other than those that directly affect your configuration (for example, "sesskey"), REMOVE those too (Moodle will add them automatically as required).
+- If you have overridden print_config, rename your method to config_print.
+- If you have overridden handle_config, rename your method to config_save.
That's everything; your block will now be ready for use in Moodle 1.5!
+ +'.$this->title.''; @@ -208,7 +208,7 @@ class block_base { break; } else { // No content but editing, so show something at least - $this->print_shadow(); + $this->_print_shadow(); } } else { if ($this->hide_header() && empty($this->edit_controls)) { @@ -227,7 +227,7 @@ class block_base { break; } else { // No content but editing, so show something at least - $this->print_shadow(); + $this->_print_shadow(); } } else { if ($this->hide_header() && empty($this->edit_controls)) { @@ -247,7 +247,7 @@ class block_base { * edit controls are accessbile to the user and they are aware that this * block is in place, even if empty. */ - function print_shadow() { + function _print_shadow() { $title = ''.$this->title.''; if ($this->edit_controls !== NULL) { $title .= $this->edit_controls; @@ -264,7 +264,7 @@ class block_base { * @param stdObject $options ? * @todo complete documenting this function. Define $options. */ - function add_edit_controls($options) { + function _add_edit_controls($options) { global $CFG, $THEME, $USER; if (!isset($this->str)) { @@ -467,7 +467,7 @@ class block_base { * @param block $instance * @todo add additional documentation to further explain the format of instance and config */ - function load_instance($instance) { + function _load_instance($instance) { if (!empty($instance->configdata)) { $this->config = unserialize(base64_decode($instance->configdata)); } diff --git a/lib/blocklib.php b/lib/blocklib.php index 820bc55286..416e7cb86a 100644 --- a/lib/blocklib.php +++ b/lib/blocklib.php @@ -72,7 +72,7 @@ function block_instance($blockname, $instance = NULL) { $classname = 'block_'.$blockname; $retval = new $classname; if($instance !== NULL) { - $retval->load_instance($instance); + $retval->_load_instance($instance); } return $retval; } @@ -221,16 +221,16 @@ function blocks_print_group(&$page, &$instances) { // allows instance configuration (multiple instances override that one). It doesn't have anything to do with what the // administrator has allowed for this block in the site admin options. $options |= BLOCK_CONFIGURE * ( $obj->instance_allow_multiple() || $obj->instance_allow_config() ); - $obj->add_edit_controls($options); + $obj->_add_edit_controls($options); } if(!$instance->visible) { if($isediting) { - $obj->print_shadow(); + $obj->_print_shadow(); } } else { - $obj->print_block(); + $obj->_print_block(); } } } -- 2.39.5