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