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