2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * This file contains the parent class for moodle blocks, block_base.
21 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
27 * Block type of list. Contents of block should be set as an associative array in the content object as items ($this->content->items). Optionally include footer text in $this->content->footer.
29 define('BLOCK_TYPE_LIST', 1);
32 * Block type of text. Contents of block should be set to standard html text in the content object as items ($this->content->text). Optionally include footer text in $this->content->footer.
34 define('BLOCK_TYPE_TEXT', 2);
36 * Block type of tree. $this->content->items is a list of tree_item objects and $this->content->footer is a string.
38 define('BLOCK_TYPE_TREE', 3);
41 * Class for describing a moodle block, all Moodle blocks derive from this class
43 * @author Jon Papaioannou
49 * Internal var for storing/caching translated strings
55 * The title of the block to be displayed in the block title area.
61 * The name of the block to be displayed in the block title area if the title is empty.
62 * @var string arialabel
64 var $arialabel = NULL;
67 * The type of content that this block creates. Currently support options - BLOCK_TYPE_LIST, BLOCK_TYPE_TEXT
68 * @var int $content_type
70 var $content_type = BLOCK_TYPE_TEXT
;
73 * An object to contain the information to be displayed in the block.
74 * @var stdObject $content
79 * The initialized instance of this block object.
80 * @var block $instance
85 * The page that this block is appearing on.
91 * This blocks's context.
94 public $context = NULL;
97 * An object containing the instance configuration information for the current instance of this block.
98 * @var stdObject $config
103 * How often the cronjob should run, 0 if not at all.
112 * Fake constructor to keep PHP5 happy
115 function __construct() {
120 * Function that can be overridden to do extra cleanup before
121 * the database tables are deleted. (Called once per block, not per instance!)
123 function before_delete() {
127 * Returns the block name, as present in the class name,
128 * the database, the block directory, etc etc.
133 // Returns the block name, as present in the class name,
134 // the database, the block directory, etc etc.
136 if ($myname === NULL) {
137 $myname = strtolower(get_class($this));
138 $myname = substr($myname, strpos($myname, '_') +
1);
144 * Parent class version of this function simply returns NULL
145 * This should be implemented by the derived class to return
146 * the content object.
150 function get_content() {
151 // This should be implemented by the derived class.
156 * Returns the class $title var value.
158 * Intentionally doesn't check if a title is set.
159 * This is already done in {@link _self_test()}
161 * @return string $this->title
163 function get_title() {
164 // Intentionally doesn't check if a title is set. This is already done in _self_test()
169 * Returns the class $content_type var value.
171 * Intentionally doesn't check if content_type is set.
172 * This is already done in {@link _self_test()}
174 * @return string $this->content_type
176 function get_content_type() {
177 // Intentionally doesn't check if a content_type is set. This is already done in _self_test()
178 return $this->content_type
;
182 * Returns true or false, depending on whether this block has any content to display
183 * and whether the user has permission to view the block
187 function is_empty() {
188 if ( !has_capability('moodle/block:view', $this->context
) ) {
192 $this->get_content();
193 return(empty($this->content
->text
) && empty($this->content
->footer
));
197 * First sets the current value of $this->content to NULL
198 * then calls the block's {@link get_content()} function
199 * to set its value back.
203 function refresh_content() {
204 // Nothing special here, depends on content()
205 $this->content
= NULL;
206 return $this->get_content();
210 * Return a block_contents object representing the full contents of this block.
212 * This internally calls ->get_content(), and then adds the editing controls etc.
214 * You probably should not override this method, but instead override
215 * {@link html_attributes()}, {@link formatted_contents()} or {@link get_content()},
216 * {@link hide_header()}, {@link (get_edit_controls)}, etc.
218 * @return block_contents a representation of the block, for rendering.
221 public function get_content_for_output($output) {
224 $bc = new block_contents($this->html_attributes());
225 $bc->attributes
['data-block'] = $this->name();
226 $bc->blockinstanceid
= $this->instance
->id
;
227 $bc->blockpositionid
= $this->instance
->blockpositionid
;
229 if ($this->instance
->visible
) {
230 $bc->content
= $this->formatted_contents($output);
231 if (!empty($this->content
->footer
)) {
232 $bc->footer
= $this->content
->footer
;
235 $bc->add_class('invisible');
238 if (!$this->hide_header()) {
239 $bc->title
= $this->title
;
242 if (empty($bc->title
)) {
243 $bc->arialabel
= new lang_string('pluginname', get_class($this));
244 $this->arialabel
= $bc->arialabel
;
247 if ($this->page
->user_is_editing()) {
248 $bc->controls
= $this->page
->blocks
->edit_controls($this);
250 // we must not use is_empty on hidden blocks
251 if ($this->is_empty() && !$bc->controls
) {
256 if (empty($CFG->allowuserblockhiding
)
257 ||
(empty($bc->content
) && empty($bc->footer
))
258 ||
!$this->instance_can_be_collapsed()) {
259 $bc->collapsible
= block_contents
::NOT_HIDEABLE
;
260 } else if (get_user_preferences('block' . $bc->blockinstanceid
. 'hidden', false)) {
261 $bc->collapsible
= block_contents
::HIDDEN
;
263 $bc->collapsible
= block_contents
::VISIBLE
;
266 if ($this->instance_can_be_docked() && !$this->hide_header()) {
267 $bc->dockable
= true;
270 $bc->annotation
= ''; // TODO MDL-19398 need to work out what to say here.
276 * Convert the contents of the block to HTML.
278 * This is used by block base classes like block_list to convert the structured
279 * $this->content->list and $this->content->icons arrays to HTML. So, in most
280 * blocks, you probaby want to override the {@link get_contents()} method,
281 * which generates that structured representation of the contents.
283 * @param $output The core_renderer to use when generating the output.
284 * @return string the HTML that should appearn in the body of the block.
287 protected function formatted_contents($output) {
288 $this->get_content();
289 $this->get_required_javascript();
290 if (!empty($this->content
->text
)) {
291 return $this->content
->text
;
298 * Tests if this block has been implemented correctly.
299 * Also, $errors isn't used right now
304 function _self_test() {
305 // Tests if this block has been implemented correctly.
306 // Also, $errors isn't used right now
310 if ($this->get_title() === NULL) {
311 $errors[] = 'title_not_set';
314 if (!in_array($this->get_content_type(), array(BLOCK_TYPE_LIST
, BLOCK_TYPE_TEXT
, BLOCK_TYPE_TREE
))) {
315 $errors[] = 'invalid_content_type';
318 //following selftest was not working when roles&capabilities were used from block
319 /* if ($this->get_content() === NULL) {
320 $errors[] = 'content_not_set';
323 $formats = $this->applicable_formats();
324 if (empty($formats) ||
array_sum($formats) === 0) {
325 $errors[] = 'no_formats';
329 $width = $this->preferred_width();
330 if (!is_int($width) ||
$width <= 0) {
331 $errors[] = 'invalid_width';
338 * Subclasses should override this and return true if the
339 * subclass block has a settings.php file.
343 function has_config() {
348 * Default behavior: save all variables as $CFG properties
349 * You don't need to override this if you 're satisfied with the above
354 function config_save($data) {
355 foreach ($data as $name => $value) {
356 set_config($name, $value);
362 * Which page types this block may appear on.
364 * The information returned here is processed by the
365 * {@link blocks_name_allowed_in_format()} function. Look there if you need
366 * to know exactly how this works.
368 * Default case: everything except mod and tag.
370 * @return array page-type prefix => true/false.
372 function applicable_formats() {
373 // Default case: the block can be used in courses and site index, but not in activities
374 return array('all' => true, 'mod' => false, 'tag' => false);
379 * Default return is false - header will be shown
382 function hide_header() {
387 * Return any HTML attributes that you want added to the outer <div> that
388 * of the block when it is output.
390 * Because of the way certain JS events are wired it is a good idea to ensure
391 * that the default values here still get set.
392 * I found the easiest way to do this and still set anything you want is to
393 * override it within your block in the following way
396 * function html_attributes() {
397 * $attributes = parent::html_attributes();
398 * $attributes['class'] .= ' mynewclass';
399 * return $attributes;
403 * @return array attribute name => value.
405 function html_attributes() {
407 'id' => 'inst' . $this->instance
->id
,
408 'class' => 'block_' . $this->name(). ' block',
409 'role' => $this->get_aria_role()
411 if ($this->hide_header()) {
412 $attributes['class'] .= ' no-header';
414 if ($this->instance_can_be_docked() && get_user_preferences('docked_block_instance_'.$this->instance
->id
, 0)) {
415 $attributes['class'] .= ' dock_on_load';
421 * Set up a particular instance of this class given data from the block_insances
422 * table and the current page. (See {@link block_manager::load_blocks()}.)
424 * @param stdClass $instance data from block_insances, block_positions, etc.
425 * @param moodle_page $the page this block is on.
427 function _load_instance($instance, $page) {
428 if (!empty($instance->configdata
)) {
429 $this->config
= unserialize(base64_decode($instance->configdata
));
431 $this->instance
= $instance;
432 $this->context
= context_block
::instance($instance->id
);
434 $this->specialization();
438 * Allows the block to load any JS it requires into the page.
440 * By default this function simply permits the user to dock the block if it is dockable.
442 function get_required_javascript() {
443 if ($this->instance_can_be_docked() && !$this->hide_header()) {
444 user_preference_allow_ajax_update('docked_block_instance_'.$this->instance
->id
, PARAM_INT
);
449 * This function is called on your subclass right after an instance is loaded
450 * Use this function to act on instance data just after it's loaded and before anything else is done
451 * For instance: if your block will have different title's depending on location (site, course, blog, etc)
453 function specialization() {
454 // Just to make sure that this method exists.
458 * Is each block of this type going to have instance-specific configuration?
459 * Normally, this setting is controlled by {@link instance_allow_multiple()}: if multiple
460 * instances are allowed, then each will surely need its own configuration. However, in some
461 * cases it may be necessary to provide instance configuration to blocks that do not want to
462 * allow multiple instances. In that case, make this function return true.
463 * I stress again that this makes a difference ONLY if {@link instance_allow_multiple()} returns false.
466 function instance_allow_config() {
471 * Are you going to allow multiple instances of each block?
472 * If yes, then it is assumed that the block WILL USE per-instance configuration
475 function instance_allow_multiple() {
476 // Are you going to allow multiple instances of each block?
477 // If yes, then it is assumed that the block WILL USE per-instance configuration
482 * Serialize and store config data
484 function instance_config_save($data, $nolongerused = false) {
486 $DB->set_field('block_instances', 'configdata', base64_encode(serialize($data)),
487 array('id' => $this->instance
->id
));
491 * Replace the instance's configuration data with those currently in $this->config;
493 function instance_config_commit($nolongerused = false) {
495 $this->instance_config_save($this->config
);
499 * Do any additional initialization you may need at the time a new block instance is created
502 function instance_create() {
507 * Delete everything related to this instance if you have been using persistent storage other than the configdata field.
510 function instance_delete() {
515 * Allows the block class to have a say in the user's ability to edit (i.e., configure) blocks of this type.
516 * The framework has first say in whether this will be allowed (e.g., no editing allowed unless in edit mode)
517 * but if the framework does allow it, the block can still decide to refuse.
520 function user_can_edit() {
523 if (has_capability('moodle/block:edit', $this->context
)) {
527 // The blocks in My Moodle are a special case. We want them to inherit from the user context.
528 if (!empty($USER->id
)
529 && $this->instance
->parentcontextid
== $this->page
->context
->id
// Block belongs to this page
530 && $this->page
->context
->contextlevel
== CONTEXT_USER
// Page belongs to a user
531 && $this->page
->context
->instanceid
== $USER->id
) { // Page belongs to this user
532 return has_capability('moodle/my:manageblocks', $this->page
->context
);
539 * Allows the block class to have a say in the user's ability to create new instances of this block.
540 * The framework has first say in whether this will be allowed (e.g., no adding allowed unless in edit mode)
541 * but if the framework does allow it, the block can still decide to refuse.
542 * This function has access to the complete page object, the creation related to which is being determined.
544 * @param moodle_page $page
547 function user_can_addto($page) {
550 // The blocks in My Moodle are a special case and use a different capability.
551 if (!empty($USER->id
)
552 && $page->context
->contextlevel
== CONTEXT_USER
// Page belongs to a user
553 && $page->context
->instanceid
== $USER->id
// Page belongs to this user
554 && $page->pagetype
== 'my-index') { // Ensure we are on the My Moodle page
556 // If the block cannot be displayed on /my it is ok if the myaddinstance capability is not defined.
557 $formats = $this->applicable_formats();
558 // Is 'my' explicitly forbidden?
559 // If 'all' has not been allowed, has 'my' been explicitly allowed?
560 if ((isset($formats['my']) && $formats['my'] == false)
561 ||
(empty($formats['all']) && empty($formats['my']))) {
563 // Block cannot be added to /my regardless of capabilities.
566 $capability = 'block/' . $this->name() . ':myaddinstance';
567 return $this->has_add_block_capability($page, $capability)
568 && has_capability('moodle/my:manageblocks', $page->context
);
572 $capability = 'block/' . $this->name() . ':addinstance';
573 if ($this->has_add_block_capability($page, $capability)
574 && has_capability('moodle/block:edit', $page->context
)) {
582 * Returns true if the user can add a block to a page.
584 * @param moodle_page $page
585 * @param string $capability the capability to check
586 * @return boolean true if user can add a block, false otherwise.
588 private function has_add_block_capability($page, $capability) {
589 // Check if the capability exists.
590 if (!get_capability_info($capability)) {
591 // Debug warning that the capability does not exist, but no more than once per page.
592 static $warned = array();
593 if (!isset($warned[$this->name()])) {
594 debugging('The block ' .$this->name() . ' does not define the standard capability ' .
595 $capability , DEBUG_DEVELOPER
);
596 $warned[$this->name()] = 1;
598 // If the capability does not exist, the block can always be added.
601 return has_capability($capability, $page->context
);
605 static function get_extra_capabilities() {
606 return array('moodle/block:view', 'moodle/block:edit');
609 // Methods deprecated in Moodle 2.0 ========================================
612 * Default case: the block wants to be 180 pixels wide
613 * @deprecated since Moodle 2.0.
616 function preferred_width() {
621 * Can be overridden by the block to prevent the block from being dockable.
625 public function instance_can_be_docked() {
627 return (!empty($CFG->allowblockstodock
) && $this->page
->theme
->enable_dock
);
631 * If overridden and set to false by the block it will not be hidable when
632 * editing is turned on.
636 public function instance_can_be_hidden() {
641 * If overridden and set to false by the block it will not be collapsible.
645 public function instance_can_be_collapsed() {
649 /** @callback callback functions for comments api */
650 public static function comment_template($options) {
652 <div class="comment-userpicture">___picture___</div>
653 <div class="comment-content">
654 ___name___ - <span>___time___</span>
655 <div>___content___</div>
660 public static function comment_permissions($options) {
661 return array('view'=>true, 'post'=>true);
663 public static function comment_url($options) {
666 public static function comment_display($comments, $options) {
669 public static function comment_add(&$comments, $options) {
674 * Returns the aria role attribute that best describes this block.
676 * Region is the default, but this should be overridden by a block is there is a region child, or even better
679 * Options are as follows:
692 public function get_aria_role() {
693 return 'complementary';
698 * Specialized class for displaying a block with a list of icons/text labels
700 * The get_content method should set $this->content->items and (optionally)
701 * $this->content->icons, instead of $this->content->text.
703 * @author Jon Papaioannou
704 * @package core_block
707 class block_list
extends block_base
{
708 var $content_type = BLOCK_TYPE_LIST
;
710 function is_empty() {
711 if ( !has_capability('moodle/block:view', $this->context
) ) {
715 $this->get_content();
716 return (empty($this->content
->items
) && empty($this->content
->footer
));
719 protected function formatted_contents($output) {
720 $this->get_content();
721 $this->get_required_javascript();
722 if (!empty($this->content
->items
)) {
723 return $output->list_block_contents($this->content
->icons
, $this->content
->items
);
729 function html_attributes() {
730 $attributes = parent
::html_attributes();
731 $attributes['class'] .= ' list_block';
738 * Specialized class for displaying a tree menu.
740 * The {@link get_content()} method involves setting the content of
741 * <code>$this->content->items</code> with an array of {@link tree_item}
742 * objects (these are the top-level nodes). The {@link tree_item::children}
743 * property may contain more tree_item objects, and so on. The tree_item class
744 * itself is abstract and not intended for use, use one of it's subclasses.
746 * Unlike {@link block_list}, the icons are specified as part of the items,
747 * not in a separate array.
750 * @package core_block
751 * @internal this extends block_list so we get is_empty() for free
753 class block_tree
extends block_list
{
756 * @var int specifies the manner in which contents should be added to this
757 * block type. In this case <code>$this->content->items</code> is used with
758 * {@link tree_item}s.
760 public $content_type = BLOCK_TYPE_TREE
;
763 * Make the formatted HTML ouput.
765 * Also adds the required javascript call to the page output.
767 * @param core_renderer $output
768 * @return string HTML
770 protected function formatted_contents($output) {
771 // based of code in admin_tree
772 global $PAGE; // TODO change this when there is a proper way for blocks to get stuff into head.
773 static $eventattached;
774 if ($eventattached===null) {
775 $eventattached = true;
777 if (!$this->content
) {
778 $this->content
= new stdClass
;
779 $this->content
->items
= array();
781 $this->get_required_javascript();
782 $this->get_content();
783 $content = $output->tree_block_contents($this->content
->items
,array('class'=>'block_tree list'));
784 if (isset($this->id
) && !is_numeric($this->id
)) {
785 $content = $output->box($content, 'block_tree_box', $this->id
);