weekly release 2.9dev
[moodle.git] / blocks / moodleblock.class.php
CommitLineData
ec972ade 1<?php
4ca6cfbf
PS
2// This file is part of Moodle - http://moodle.org/
3//
ec972ade 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.
8//
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.
4ca6cfbf 13//
ec972ade 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/>.
0f3fe4b6 16
c7e3bc51 17/**
ccd3dd0c 18 * This file contains the parent class for moodle blocks, block_base.
c7e3bc51 19 *
f25a6839 20 * @package core_block
78bfb562 21 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
c7e3bc51 22 */
23
24/// Constants
25
26/**
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.
28 */
0f3fe4b6 29define('BLOCK_TYPE_LIST', 1);
c7e3bc51 30
31/**
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.
33 */
0f3fe4b6 34define('BLOCK_TYPE_TEXT', 2);
7d2a0492 35/**
36 * Block type of tree. $this->content->items is a list of tree_item objects and $this->content->footer is a string.
37 */
38define('BLOCK_TYPE_TREE', 3);
c7e3bc51 39
c7e3bc51 40/**
e0134f84 41 * Class for describing a moodle block, all Moodle blocks derive from this class
c7e3bc51 42 *
43 * @author Jon Papaioannou
f25a6839 44 * @package core_block
c7e3bc51 45 */
e89d741a 46class block_base {
c7e3bc51 47
48 /**
49 * Internal var for storing/caching translated strings
50 * @var string $str
51 */
0f3fe4b6 52 var $str;
c7e3bc51 53
54 /**
f8582e3e 55 * The title of the block to be displayed in the block title area.
c7e3bc51 56 * @var string $title
57 */
9b4b78fd 58 var $title = NULL;
c7e3bc51 59
47332703
JF
60 /**
61 * The name of the block to be displayed in the block title area if the title is empty.
62 * @var string arialabel
63 */
64 var $arialabel = NULL;
65
c7e3bc51 66 /**
ccd3dd0c 67 * The type of content that this block creates. Currently support options - BLOCK_TYPE_LIST, BLOCK_TYPE_TEXT
c7e3bc51 68 * @var int $content_type
69 */
3ef642d9 70 var $content_type = BLOCK_TYPE_TEXT;
c7e3bc51 71
72 /**
73 * An object to contain the information to be displayed in the block.
74 * @var stdObject $content
75 */
9b4b78fd 76 var $content = NULL;
c7e3bc51 77
c7e3bc51 78 /**
79 * The initialized instance of this block object.
80 * @var block $instance
81 */
9b4b78fd 82 var $instance = NULL;
c7e3bc51 83
3179b000 84 /**
85 * The page that this block is appearing on.
86 * @var moodle_page
87 */
88 public $page = NULL;
89
e92c286c 90 /**
91 * This blocks's context.
92 * @var stdClass
93 */
94 public $context = NULL;
95
c7e3bc51 96 /**
97 * An object containing the instance configuration information for the current instance of this block.
98 * @var stdObject $config
99 */
9b4b78fd 100 var $config = NULL;
101
4ca6cfbf 102 /**
f9648e77 103 * How often the cronjob should run, 0 if not at all.
104 * @var int $cron
105 */
106
107 var $cron = NULL;
108
e0134f84 109/// Class Functions
c7e3bc51 110
73554752 111 /**
112 * Fake constructor to keep PHP5 happy
113 *
114 */
115 function __construct() {
8571833f 116 $this->init();
73554752 117 }
4ca6cfbf 118
44c55d46 119 /**
120 * Function that can be overridden to do extra cleanup before
121 * the database tables are deleted. (Called once per block, not per instance!)
122 */
123 function before_delete() {
124 }
4ca6cfbf 125
c7e3bc51 126 /**
127 * Returns the block name, as present in the class name,
128 * the database, the block directory, etc etc.
129 *
130 * @return string
131 */
0f3fe4b6 132 function name() {
133 // Returns the block name, as present in the class name,
134 // the database, the block directory, etc etc.
135 static $myname;
c7e3bc51 136 if ($myname === NULL) {
0f3fe4b6 137 $myname = strtolower(get_class($this));
138 $myname = substr($myname, strpos($myname, '_') + 1);
139 }
140 return $myname;
141 }
142
c7e3bc51 143 /**
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.
147 *
148 * @return stdObject
149 */
0f3fe4b6 150 function get_content() {
151 // This should be implemented by the derived class.
152 return NULL;
153 }
c7e3bc51 154
155 /**
156 * Returns the class $title var value.
157 *
4ca6cfbf 158 * Intentionally doesn't check if a title is set.
c7e3bc51 159 * This is already done in {@link _self_test()}
160 *
161 * @return string $this->title
162 */
0f3fe4b6 163 function get_title() {
6195e04a 164 // Intentionally doesn't check if a title is set. This is already done in _self_test()
0f3fe4b6 165 return $this->title;
166 }
c7e3bc51 167
168 /**
169 * Returns the class $content_type var value.
170 *
4ca6cfbf 171 * Intentionally doesn't check if content_type is set.
c7e3bc51 172 * This is already done in {@link _self_test()}
173 *
174 * @return string $this->content_type
175 */
0f3fe4b6 176 function get_content_type() {
6195e04a 177 // Intentionally doesn't check if a content_type is set. This is already done in _self_test()
0f3fe4b6 178 return $this->content_type;
179 }
c7e3bc51 180
3ef642d9 181 /**
182 * Returns true or false, depending on whether this block has any content to display
7e874772 183 * and whether the user has permission to view the block
3ef642d9 184 *
185 * @return boolean
186 */
187 function is_empty() {
e92c286c 188 if ( !has_capability('moodle/block:view', $this->context) ) {
7e874772 189 return true;
190 }
191
3ef642d9 192 $this->get_content();
193 return(empty($this->content->text) && empty($this->content->footer));
194 }
195
c7e3bc51 196 /**
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.
200 *
201 * @return stdObject
202 */
0f3fe4b6 203 function refresh_content() {
204 // Nothing special here, depends on content()
205 $this->content = NULL;
206 return $this->get_content();
207 }
c7e3bc51 208
209 /**
9c14c1c5 210 * Return a block_contents object representing the full contents of this block.
d4a03c00 211 *
212 * This internally calls ->get_content(), and then adds the editing controls etc.
213 *
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.
217 *
9c14c1c5 218 * @return block_contents a representation of the block, for rendering.
d4a03c00 219 * @since Moodle 2.0.
c7e3bc51 220 */
d4a03c00 221 public function get_content_for_output($output) {
222 global $CFG;
0a0bb380 223
dd72b308 224 $bc = new block_contents($this->html_attributes());
a69a7e89 225 $bc->attributes['data-block'] = $this->name();
d4a03c00 226 $bc->blockinstanceid = $this->instance->id;
227 $bc->blockpositionid = $this->instance->blockpositionid;
c7e3bc51 228
e54f4a78 229 if ($this->instance->visible) {
230 $bc->content = $this->formatted_contents($output);
231 if (!empty($this->content->footer)) {
232 $bc->footer = $this->content->footer;
233 }
234 } else {
235 $bc->add_class('invisible');
236 }
237
d4a03c00 238 if (!$this->hide_header()) {
239 $bc->title = $this->title;
240 }
47332703 241
91d941c3
SH
242 if (empty($bc->title)) {
243 $bc->arialabel = new lang_string('pluginname', get_class($this));
47332703 244 $this->arialabel = $bc->arialabel;
91d941c3 245 }
ae42ff6f 246
d4a03c00 247 if ($this->page->user_is_editing()) {
00a24d44 248 $bc->controls = $this->page->blocks->edit_controls($this);
9c14c1c5
PS
249 } else {
250 // we must not use is_empty on hidden blocks
251 if ($this->is_empty() && !$bc->controls) {
252 return null;
253 }
f674ec86 254 }
255
0885166f
JC
256 if (empty($CFG->allowuserblockhiding)
257 || (empty($bc->content) && empty($bc->footer))
258 || !$this->instance_can_be_collapsed()) {
d4a03c00 259 $bc->collapsible = block_contents::NOT_HIDEABLE;
260 } else if (get_user_preferences('block' . $bc->blockinstanceid . 'hidden', false)) {
261 $bc->collapsible = block_contents::HIDDEN;
262 } else {
263 $bc->collapsible = block_contents::VISIBLE;
0f3fe4b6 264 }
c4d951e1 265
84192d78
SH
266 if ($this->instance_can_be_docked() && !$this->hide_header()) {
267 $bc->dockable = true;
268 }
269
40928036 270 $bc->annotation = ''; // TODO MDL-19398 need to work out what to say here.
d4a03c00 271
272 return $bc;
0f3fe4b6 273 }
89adb174 274
c7e3bc51 275 /**
d4a03c00 276 * Convert the contents of the block to HTML.
c7e3bc51 277 *
d4a03c00 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.
282 *
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.
285 * @since Moodle 2.0.
c7e3bc51 286 */
d4a03c00 287 protected function formatted_contents($output) {
288 $this->get_content();
dde36f9d 289 $this->get_required_javascript();
d4a03c00 290 if (!empty($this->content->text)) {
291 return $this->content->text;
292 } else {
293 return '';
294 }
295 }
296
c7e3bc51 297 /**
298 * Tests if this block has been implemented correctly.
299 * Also, $errors isn't used right now
300 *
301 * @return boolean
302 */
4ca6cfbf 303
0f3fe4b6 304 function _self_test() {
305 // Tests if this block has been implemented correctly.
306 // Also, $errors isn't used right now
307 $errors = array();
308
309 $correct = true;
c7e3bc51 310 if ($this->get_title() === NULL) {
0f3fe4b6 311 $errors[] = 'title_not_set';
312 $correct = false;
313 }
7d2a0492 314 if (!in_array($this->get_content_type(), array(BLOCK_TYPE_LIST, BLOCK_TYPE_TEXT, BLOCK_TYPE_TREE))) {
0f3fe4b6 315 $errors[] = 'invalid_content_type';
316 $correct = false;
317 }
4ca6cfbf 318 //following selftest was not working when roles&capabilities were used from block
35a518c5 319/* if ($this->get_content() === NULL) {
0f3fe4b6 320 $errors[] = 'content_not_set';
321 $correct = false;
35a518c5 322 }*/
2b0d60ec 323 $formats = $this->applicable_formats();
c7e3bc51 324 if (empty($formats) || array_sum($formats) === 0) {
8a47e075 325 $errors[] = 'no_formats';
0f3fe4b6 326 $correct = false;
327 }
2b0d60ec 328
0f3fe4b6 329 return $correct;
330 }
331
c7e3bc51 332 /**
333 * Subclasses should override this and return true if the
4dd79132 334 * subclass block has a settings.php file.
c7e3bc51 335 *
336 * @return boolean
337 */
0f3fe4b6 338 function has_config() {
339 return false;
340 }
c7e3bc51 341
c7e3bc51 342 /**
343 * Default behavior: save all variables as $CFG properties
344 * You don't need to override this if you 're satisfied with the above
345 *
f8582e3e 346 * @param array $data
c7e3bc51 347 * @return boolean
348 */
f8582e3e 349 function config_save($data) {
f8582e3e 350 foreach ($data as $name => $value) {
9b4b78fd 351 set_config($name, $value);
352 }
353 return true;
0f3fe4b6 354 }
4ca6cfbf 355
c7e3bc51 356 /**
40928036 357 * Which page types this block may appear on.
358 *
359 * The information returned here is processed by the
360 * {@link blocks_name_allowed_in_format()} function. Look there if you need
361 * to know exactly how this works.
362 *
363 * Default case: everything except mod and tag.
364 *
365 * @return array page-type prefix => true/false.
c7e3bc51 366 */
0f3fe4b6 367 function applicable_formats() {
d232b8c7 368 // Default case: the block can be used in courses and site index, but not in activities
9591bc3c 369 return array('all' => true, 'mod' => false, 'tag' => false);
0f3fe4b6 370 }
4ca6cfbf 371
c7e3bc51 372
c7e3bc51 373 /**
374 * Default return is false - header will be shown
375 * @return boolean
376 */
0f3fe4b6 377 function hide_header() {
0f3fe4b6 378 return false;
379 }
c7e3bc51 380
381 /**
d4a03c00 382 * Return any HTML attributes that you want added to the outer <div> that
383 * of the block when it is output.
7d2a0492 384 *
385 * Because of the way certain JS events are wired it is a good idea to ensure
386 * that the default values here still get set.
387 * I found the easiest way to do this and still set anything you want is to
388 * override it within your block in the following way
389 *
390 * <code php>
391 * function html_attributes() {
392 * $attributes = parent::html_attributes();
393 * $attributes['class'] .= ' mynewclass';
394 * return $attributes;
395 * }
396 * </code>
397 *
d4a03c00 398 * @return array attribute name => value.
c7e3bc51 399 */
c5507e52 400 function html_attributes() {
9d897331 401 $attributes = array(
4ca6cfbf 402 'id' => 'inst' . $this->instance->id,
697f7bd7
SH
403 'class' => 'block_' . $this->name(). ' block',
404 'role' => $this->get_aria_role()
2f747639 405 );
dc3337b2
AG
406 if ($this->hide_header()) {
407 $attributes['class'] .= ' no-header';
408 }
d2c394f3 409 if ($this->instance_can_be_docked() && get_user_preferences('docked_block_instance_'.$this->instance->id, 0)) {
9d897331
SH
410 $attributes['class'] .= ' dock_on_load';
411 }
412 return $attributes;
c5507e52 413 }
d4a03c00 414
c7e3bc51 415 /**
40928036 416 * Set up a particular instance of this class given data from the block_insances
4ca6cfbf 417 * table and the current page. (See {@link block_manager::load_blocks()}.)
40928036 418 *
419 * @param stdClass $instance data from block_insances, block_positions, etc.
420 * @param moodle_page $the page this block is on.
c7e3bc51 421 */
3179b000 422 function _load_instance($instance, $page) {
c7e3bc51 423 if (!empty($instance->configdata)) {
9b4b78fd 424 $this->config = unserialize(base64_decode($instance->configdata));
425 }
9b4b78fd 426 $this->instance = $instance;
fe2fdd11 427 $this->context = context_block::instance($instance->id);
3179b000 428 $this->page = $page;
9b4b78fd 429 $this->specialization();
9d897331
SH
430 }
431
84192d78
SH
432 /**
433 * Allows the block to load any JS it requires into the page.
434 *
435 * By default this function simply permits the user to dock the block if it is dockable.
436 */
9d897331 437 function get_required_javascript() {
46de77b6 438 if ($this->instance_can_be_docked() && !$this->hide_header()) {
9d897331
SH
439 user_preference_allow_ajax_update('docked_block_instance_'.$this->instance->id, PARAM_INT);
440 }
9b4b78fd 441 }
59b29207 442
43038ce6 443 /**
444 * This function is called on your subclass right after an instance is loaded
445 * Use this function to act on instance data just after it's loaded and before anything else is done
67677908 446 * For instance: if your block will have different title's depending on location (site, course, blog, etc)
43038ce6 447 */
9b4b78fd 448 function specialization() {
449 // Just to make sure that this method exists.
9b4b78fd 450 }
451
c7e3bc51 452 /**
f032aa7a 453 * Is each block of this type going to have instance-specific configuration?
40928036 454 * Normally, this setting is controlled by {@link instance_allow_multiple()}: if multiple
f032aa7a 455 * instances are allowed, then each will surely need its own configuration. However, in some
456 * cases it may be necessary to provide instance configuration to blocks that do not want to
457 * allow multiple instances. In that case, make this function return true.
40928036 458 * I stress again that this makes a difference ONLY if {@link instance_allow_multiple()} returns false.
f032aa7a 459 * @return boolean
f032aa7a 460 */
461 function instance_allow_config() {
462 return false;
463 }
464
465 /**
466 * Are you going to allow multiple instances of each block?
c7e3bc51 467 * If yes, then it is assumed that the block WILL USE per-instance configuration
468 * @return boolean
c7e3bc51 469 */
9b4b78fd 470 function instance_allow_multiple() {
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
473 return false;
474 }
59b29207 475
c7e3bc51 476 /**
477 * Serialize and store config data
c7e3bc51 478 */
f474a4e5 479 function instance_config_save($data, $nolongerused = false) {
a5d424df 480 global $DB;
f474a4e5 481 $DB->set_field('block_instances', 'configdata', base64_encode(serialize($data)),
e03c0c1d 482 array('id' => $this->instance->id));
9b4b78fd 483 }
484
0144a0a7 485 /**
486 * Replace the instance's configuration data with those currently in $this->config;
0144a0a7 487 */
f474a4e5 488 function instance_config_commit($nolongerused = false) {
a5d424df 489 global $DB;
f474a4e5 490 $this->instance_config_save($this->config);
0144a0a7 491 }
492
d4a03c00 493 /**
b33dd23a 494 * Do any additional initialization you may need at the time a new block instance is created
495 * @return boolean
b33dd23a 496 */
497 function instance_create() {
498 return true;
499 }
500
d4a03c00 501 /**
b33dd23a 502 * Delete everything related to this instance if you have been using persistent storage other than the configdata field.
503 * @return boolean
b33dd23a 504 */
505 function instance_delete() {
506 return true;
507 }
508
d4a03c00 509 /**
11306331 510 * Allows the block class to have a say in the user's ability to edit (i.e., configure) blocks of this type.
511 * The framework has first say in whether this will be allowed (e.g., no editing allowed unless in edit mode)
512 * but if the framework does allow it, the block can still decide to refuse.
513 * @return boolean
11306331 514 */
515 function user_can_edit() {
03d9401e
MD
516 global $USER;
517
518 if (has_capability('moodle/block:edit', $this->context)) {
519 return true;
520 }
521
522 // The blocks in My Moodle are a special case. We want them to inherit from the user context.
523 if (!empty($USER->id)
524 && $this->instance->parentcontextid == $this->page->context->id // Block belongs to this page
525 && $this->page->context->contextlevel == CONTEXT_USER // Page belongs to a user
526 && $this->page->context->instanceid == $USER->id) { // Page belongs to this user
527 return has_capability('moodle/my:manageblocks', $this->page->context);
528 }
529
530 return false;
11306331 531 }
532
d4a03c00 533 /**
11306331 534 * Allows the block class to have a say in the user's ability to create new instances of this block.
535 * The framework has first say in whether this will be allowed (e.g., no adding allowed unless in edit mode)
536 * but if the framework does allow it, the block can still decide to refuse.
537 * This function has access to the complete page object, the creation related to which is being determined.
10a92c39
SH
538 *
539 * @param moodle_page $page
11306331 540 * @return boolean
11306331 541 */
15a00b4b 542 function user_can_addto($page) {
03d9401e
MD
543 global $USER;
544
03d9401e
MD
545 // The blocks in My Moodle are a special case and use a different capability.
546 if (!empty($USER->id)
b291b59d 547 && $page->context->contextlevel == CONTEXT_USER // Page belongs to a user
9b06b507
MN
548 && $page->context->instanceid == $USER->id // Page belongs to this user
549 && $page->pagetype == 'my-index') { // Ensure we are on the My Moodle page
b291b59d
MN
550 $capability = 'block/' . $this->name() . ':myaddinstance';
551 return $this->has_add_block_capability($page, $capability)
552 && has_capability('moodle/my:manageblocks', $page->context);
03d9401e
MD
553 }
554
464e00be
MN
555 $capability = 'block/' . $this->name() . ':addinstance';
556 if ($this->has_add_block_capability($page, $capability)
557 && has_capability('moodle/block:edit', $page->context)) {
558 return true;
559 }
560
03d9401e 561 return false;
11306331 562 }
563
b291b59d
MN
564 /**
565 * Returns true if the user can add a block to a page.
566 *
567 * @param moodle_page $page
568 * @param string $capability the capability to check
569 * @return boolean true if user can add a block, false otherwise.
570 */
571 private function has_add_block_capability($page, $capability) {
572 // Check if the capability exists.
573 if (!get_capability_info($capability)) {
574 // Debug warning that the capability does not exist, but no more than once per page.
575 static $warned = array();
576 if (!isset($warned[$this->name()])) {
577 debugging('The block ' .$this->name() . ' does not define the standard capability ' .
578 $capability , DEBUG_DEVELOPER);
579 $warned[$this->name()] = 1;
580 }
581 // If the capability does not exist, the block can always be added.
582 return true;
583 } else {
584 return has_capability($capability, $page->context);
585 }
586 }
587
324d210f 588 static function get_extra_capabilities() {
d14edf06 589 return array('moodle/block:view', 'moodle/block:edit');
f432bebf 590 }
d4a03c00 591
d8ef60bd
SH
592 /**
593 * Can be overridden by the block to prevent the block from being dockable.
9c14c1c5 594 *
d8ef60bd
SH
595 * @return bool
596 */
d2c394f3
SH
597 public function instance_can_be_docked() {
598 global $CFG;
598ee317 599 return (!empty($CFG->allowblockstodock) && $this->page->theme->enable_dock);
9d897331
SH
600 }
601
d8ef60bd 602 /**
0885166f 603 * If overridden and set to false by the block it will not be hidable when
d8ef60bd
SH
604 * editing is turned on.
605 *
606 * @return bool
607 */
608 public function instance_can_be_hidden() {
609 return true;
610 }
611
0885166f
JC
612 /**
613 * If overridden and set to false by the block it will not be collapsible.
614 *
615 * @return bool
616 */
617 public function instance_can_be_collapsed() {
618 return true;
619 }
620
1bcb7eb5 621 /** @callback callback functions for comments api */
622 public static function comment_template($options) {
623 $ret = <<<EOD
4ca6cfbf 624<div class="comment-userpicture">___picture___</div>
1bcb7eb5 625<div class="comment-content">
626 ___name___ - <span>___time___</span>
627 <div>___content___</div>
628</div>
629EOD;
630 return $ret;
631 }
632 public static function comment_permissions($options) {
633 return array('view'=>true, 'post'=>true);
634 }
635 public static function comment_url($options) {
636 return null;
637 }
a0e68ffc
DC
638 public static function comment_display($comments, $options) {
639 return $comments;
1bcb7eb5 640 }
641 public static function comment_add(&$comments, $options) {
642 return true;
643 }
697f7bd7
SH
644
645 /**
646 * Returns the aria role attribute that best describes this block.
647 *
648 * Region is the default, but this should be overridden by a block is there is a region child, or even better
649 * a landmark child.
650 *
651 * Options are as follows:
697f7bd7
SH
652 * - landmark
653 * - application
654 * - banner
655 * - complementary
656 * - contentinfo
657 * - form
658 * - main
659 * - navigation
660 * - search
697f7bd7
SH
661 *
662 * @return string
663 */
664 public function get_aria_role() {
5c4581fc 665 return 'complementary';
697f7bd7 666 }
0f3fe4b6 667}
668
c7e3bc51 669/**
3ef642d9 670 * Specialized class for displaying a block with a list of icons/text labels
671 *
d4a03c00 672 * The get_content method should set $this->content->items and (optionally)
673 * $this->content->icons, instead of $this->content->text.
674 *
3ef642d9 675 * @author Jon Papaioannou
f25a6839 676 * @package core_block
3ef642d9 677 */
678
679class block_list extends block_base {
680 var $content_type = BLOCK_TYPE_LIST;
681
682 function is_empty() {
e92c286c 683 if ( !has_capability('moodle/block:view', $this->context) ) {
25a7d980 684 return true;
685 }
686
3ef642d9 687 $this->get_content();
688 return (empty($this->content->items) && empty($this->content->footer));
689 }
690
d4a03c00 691 protected function formatted_contents($output) {
692 $this->get_content();
dde36f9d 693 $this->get_required_javascript();
d4a03c00 694 if (!empty($this->content->items)) {
695 return $output->list_block_contents($this->content->icons, $this->content->items);
3ef642d9 696 } else {
d4a03c00 697 return '';
3ef642d9 698 }
699 }
740952e4
SH
700
701 function html_attributes() {
702 $attributes = parent::html_attributes();
703 $attributes['class'] .= ' list_block';
704 return $attributes;
705 }
706
3ef642d9 707}
708
7d2a0492 709/**
710 * Specialized class for displaying a tree menu.
4ca6cfbf 711 *
7d2a0492 712 * The {@link get_content()} method involves setting the content of
713 * <code>$this->content->items</code> with an array of {@link tree_item}
714 * objects (these are the top-level nodes). The {@link tree_item::children}
715 * property may contain more tree_item objects, and so on. The tree_item class
716 * itself is abstract and not intended for use, use one of it's subclasses.
4ca6cfbf 717 *
7d2a0492 718 * Unlike {@link block_list}, the icons are specified as part of the items,
719 * not in a separate array.
720 *
721 * @author Alan Trick
f25a6839 722 * @package core_block
7d2a0492 723 * @internal this extends block_list so we get is_empty() for free
724 */
725class block_tree extends block_list {
4ca6cfbf 726
7d2a0492 727 /**
728 * @var int specifies the manner in which contents should be added to this
729 * block type. In this case <code>$this->content->items</code> is used with
730 * {@link tree_item}s.
731 */
732 public $content_type = BLOCK_TYPE_TREE;
4ca6cfbf 733
7d2a0492 734 /**
735 * Make the formatted HTML ouput.
4ca6cfbf 736 *
7d2a0492 737 * Also adds the required javascript call to the page output.
738 *
78946b9b 739 * @param core_renderer $output
7d2a0492 740 * @return string HTML
741 */
742 protected function formatted_contents($output) {
743 // based of code in admin_tree
744 global $PAGE; // TODO change this when there is a proper way for blocks to get stuff into head.
745 static $eventattached;
746 if ($eventattached===null) {
747 $eventattached = true;
748 }
749 if (!$this->content) {
750 $this->content = new stdClass;
751 $this->content->items = array();
752 }
d5cbccb3 753 $this->get_required_javascript();
7d2a0492 754 $this->get_content();
1ce15fda 755 $content = $output->tree_block_contents($this->content->items,array('class'=>'block_tree list'));
7d2a0492 756 if (isset($this->id) && !is_numeric($this->id)) {
757 $content = $output->box($content, 'block_tree_box', $this->id);
758 }
759 return $content;
760 }
a0e68ffc 761}