MDL-22061 get_mimetype_description() does not rely on get_string() returning square...
[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 *
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
4ca6cfbf 109 /**
f9648e77 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 }
4ca6cfbf 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 }
4ca6cfbf 140
a82e9bc3 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
4ca6cfbf 144 * stores, if the $restore->course_startdateoffset is set.
a82e9bc3 145 */
146 function after_restore($restore) {
147 }
73554752 148
b486ef1a 149 /**
5cfefc9b 150 * Enable custom instance data section in backup and restore.
4ca6cfbf 151 *
b486ef1a 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 /**
4ca6cfbf
PS
163 * Allows the block class to have a backup routine. Handy
164 * when the block has its own tables that have foreign keys to
b486ef1a 165 * other tables (example: user table).
4ca6cfbf
PS
166 *
167 * Note: at the time of writing this comment, the indent level
b486ef1a 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.
4ca6cfbf
PS
180 *
181 * Should not return false if data is empty
b486ef1a 182 * because old backups would not contain block instance backup data.
4ca6cfbf 183 *
b486ef1a 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()}
4ca6cfbf
PS
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 *
5cfefc9b 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 *
4ca6cfbf 280 * Intentionally doesn't check if a title is set.
c7e3bc51 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 *
4ca6cfbf 293 * Intentionally doesn't check if content_type is set.
c7e3bc51 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 *
4ca6cfbf 306 * Intentionally doesn't check if a version is set.
c7e3bc51 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
dd72b308
PS
359 $bc = new block_contents($this->html_attributes());
360
d4a03c00 361 $bc->blockinstanceid = $this->instance->id;
362 $bc->blockpositionid = $this->instance->blockpositionid;
c7e3bc51 363
e54f4a78 364 if ($this->instance->visible) {
365 $bc->content = $this->formatted_contents($output);
366 if (!empty($this->content->footer)) {
367 $bc->footer = $this->content->footer;
368 }
369 } else {
370 $bc->add_class('invisible');
371 }
372
d4a03c00 373 if (!$this->hide_header()) {
374 $bc->title = $this->title;
375 }
ae42ff6f 376
d4a03c00 377 if ($this->page->user_is_editing()) {
00a24d44 378 $bc->controls = $this->page->blocks->edit_controls($this);
d4a03c00 379 }
c4d951e1 380
f674ec86 381 if ($this->is_empty() && !$bc->controls) {
382 return null;
383 }
384
ae42ff6f 385 if (empty($CFG->allowuserblockhiding) ||
386 (empty($bc->content) && empty($bc->footer))) {
d4a03c00 387 $bc->collapsible = block_contents::NOT_HIDEABLE;
388 } else if (get_user_preferences('block' . $bc->blockinstanceid . 'hidden', false)) {
389 $bc->collapsible = block_contents::HIDDEN;
390 } else {
391 $bc->collapsible = block_contents::VISIBLE;
0f3fe4b6 392 }
c4d951e1 393
40928036 394 $bc->annotation = ''; // TODO MDL-19398 need to work out what to say here.
d4a03c00 395
396 return $bc;
0f3fe4b6 397 }
89adb174 398
c7e3bc51 399 /**
d4a03c00 400 * Convert the contents of the block to HTML.
c7e3bc51 401 *
d4a03c00 402 * This is used by block base classes like block_list to convert the structured
403 * $this->content->list and $this->content->icons arrays to HTML. So, in most
404 * blocks, you probaby want to override the {@link get_contents()} method,
405 * which generates that structured representation of the contents.
406 *
407 * @param $output The core_renderer to use when generating the output.
408 * @return string the HTML that should appearn in the body of the block.
409 * @since Moodle 2.0.
c7e3bc51 410 */
d4a03c00 411 protected function formatted_contents($output) {
412 $this->get_content();
413 if (!empty($this->content->text)) {
d5cbccb3 414 $this->get_required_javascript();
d4a03c00 415 return $this->content->text;
416 } else {
417 return '';
418 }
419 }
420
c7e3bc51 421 /**
422 * Tests if this block has been implemented correctly.
423 * Also, $errors isn't used right now
424 *
425 * @return boolean
426 */
4ca6cfbf 427
0f3fe4b6 428 function _self_test() {
429 // Tests if this block has been implemented correctly.
430 // Also, $errors isn't used right now
431 $errors = array();
432
433 $correct = true;
c7e3bc51 434 if ($this->get_title() === NULL) {
0f3fe4b6 435 $errors[] = 'title_not_set';
436 $correct = false;
437 }
7d2a0492 438 if (!in_array($this->get_content_type(), array(BLOCK_TYPE_LIST, BLOCK_TYPE_TEXT, BLOCK_TYPE_TREE))) {
0f3fe4b6 439 $errors[] = 'invalid_content_type';
440 $correct = false;
441 }
4ca6cfbf 442 //following selftest was not working when roles&capabilities were used from block
35a518c5 443/* if ($this->get_content() === NULL) {
0f3fe4b6 444 $errors[] = 'content_not_set';
445 $correct = false;
35a518c5 446 }*/
c7e3bc51 447 if ($this->get_version() === NULL) {
0f3fe4b6 448 $errors[] = 'version_not_set';
449 $correct = false;
450 }
2b0d60ec 451
452 $formats = $this->applicable_formats();
c7e3bc51 453 if (empty($formats) || array_sum($formats) === 0) {
8a47e075 454 $errors[] = 'no_formats';
0f3fe4b6 455 $correct = false;
456 }
2b0d60ec 457
0f3fe4b6 458 $width = $this->preferred_width();
c7e3bc51 459 if (!is_int($width) || $width <= 0) {
0f3fe4b6 460 $errors[] = 'invalid_width';
461 $correct = false;
462 }
463 return $correct;
464 }
465
c7e3bc51 466 /**
467 * Subclasses should override this and return true if the
468 * subclass block has a config_global.html file.
469 *
470 * @return boolean
471 */
0f3fe4b6 472 function has_config() {
473 return false;
474 }
c7e3bc51 475
c7e3bc51 476 /**
477 * Default behavior: save all variables as $CFG properties
478 * You don't need to override this if you 're satisfied with the above
479 *
f8582e3e 480 * @param array $data
c7e3bc51 481 * @return boolean
482 */
f8582e3e 483 function config_save($data) {
f8582e3e 484 foreach ($data as $name => $value) {
9b4b78fd 485 set_config($name, $value);
486 }
487 return true;
0f3fe4b6 488 }
4ca6cfbf 489
c7e3bc51 490 /**
40928036 491 * Which page types this block may appear on.
492 *
493 * The information returned here is processed by the
494 * {@link blocks_name_allowed_in_format()} function. Look there if you need
495 * to know exactly how this works.
496 *
497 * Default case: everything except mod and tag.
498 *
499 * @return array page-type prefix => true/false.
c7e3bc51 500 */
0f3fe4b6 501 function applicable_formats() {
d232b8c7 502 // Default case: the block can be used in courses and site index, but not in activities
9591bc3c 503 return array('all' => true, 'mod' => false, 'tag' => false);
0f3fe4b6 504 }
4ca6cfbf 505
c7e3bc51 506
c7e3bc51 507 /**
508 * Default return is false - header will be shown
509 * @return boolean
510 */
0f3fe4b6 511 function hide_header() {
0f3fe4b6 512 return false;
513 }
c7e3bc51 514
515 /**
d4a03c00 516 * Return any HTML attributes that you want added to the outer <div> that
517 * of the block when it is output.
7d2a0492 518 *
519 * Because of the way certain JS events are wired it is a good idea to ensure
520 * that the default values here still get set.
521 * I found the easiest way to do this and still set anything you want is to
522 * override it within your block in the following way
523 *
524 * <code php>
525 * function html_attributes() {
526 * $attributes = parent::html_attributes();
527 * $attributes['class'] .= ' mynewclass';
528 * return $attributes;
529 * }
530 * </code>
531 *
d4a03c00 532 * @return array attribute name => value.
c7e3bc51 533 */
c5507e52 534 function html_attributes() {
9d897331 535 $attributes = array(
4ca6cfbf 536 'id' => 'inst' . $this->instance->id,
dd72b308 537 'class' => 'block_' . $this->name(). ' sideblock'
2f747639 538 );
d2c394f3 539 if ($this->instance_can_be_docked() && get_user_preferences('docked_block_instance_'.$this->instance->id, 0)) {
9d897331
SH
540 $attributes['class'] .= ' dock_on_load';
541 }
542 return $attributes;
c5507e52 543 }
d4a03c00 544
c7e3bc51 545 /**
40928036 546 * Set up a particular instance of this class given data from the block_insances
4ca6cfbf 547 * table and the current page. (See {@link block_manager::load_blocks()}.)
40928036 548 *
549 * @param stdClass $instance data from block_insances, block_positions, etc.
550 * @param moodle_page $the page this block is on.
c7e3bc51 551 */
3179b000 552 function _load_instance($instance, $page) {
c7e3bc51 553 if (!empty($instance->configdata)) {
9b4b78fd 554 $this->config = unserialize(base64_decode($instance->configdata));
555 }
9b4b78fd 556 $this->instance = $instance;
4f0c2d00 557 $this->context = get_context_instance(CONTEXT_BLOCK, $instance->id);
3179b000 558 $this->page = $page;
9b4b78fd 559 $this->specialization();
9d897331
SH
560 }
561
562 function get_required_javascript() {
46de77b6 563 if ($this->instance_can_be_docked() && !$this->hide_header()) {
53fc3e70 564 $this->page->requires->js_init_call('M.core_dock.init_genericblock', array($this->instance->id));
9d897331
SH
565 user_preference_allow_ajax_update('docked_block_instance_'.$this->instance->id, PARAM_INT);
566 }
9b4b78fd 567 }
59b29207 568
43038ce6 569 /**
570 * This function is called on your subclass right after an instance is loaded
571 * Use this function to act on instance data just after it's loaded and before anything else is done
67677908 572 * For instance: if your block will have different title's depending on location (site, course, blog, etc)
43038ce6 573 */
9b4b78fd 574 function specialization() {
575 // Just to make sure that this method exists.
9b4b78fd 576 }
577
c7e3bc51 578 /**
f032aa7a 579 * Is each block of this type going to have instance-specific configuration?
40928036 580 * Normally, this setting is controlled by {@link instance_allow_multiple()}: if multiple
f032aa7a 581 * instances are allowed, then each will surely need its own configuration. However, in some
582 * cases it may be necessary to provide instance configuration to blocks that do not want to
583 * allow multiple instances. In that case, make this function return true.
40928036 584 * I stress again that this makes a difference ONLY if {@link instance_allow_multiple()} returns false.
f032aa7a 585 * @return boolean
f032aa7a 586 */
587 function instance_allow_config() {
588 return false;
589 }
590
591 /**
592 * Are you going to allow multiple instances of each block?
c7e3bc51 593 * If yes, then it is assumed that the block WILL USE per-instance configuration
594 * @return boolean
c7e3bc51 595 */
9b4b78fd 596 function instance_allow_multiple() {
597 // Are you going to allow multiple instances of each block?
598 // If yes, then it is assumed that the block WILL USE per-instance configuration
599 return false;
600 }
59b29207 601
c7e3bc51 602 /**
603 * Default behavior: print the config_instance.html file
604 * You don't need to override this if you're satisfied with the above
605 *
40928036 606 * @deprecated since Moodle 2.0.
607 * @return boolean whether anything was done. Blocks should use edit_form.php.
c7e3bc51 608 */
9b4b78fd 609 function instance_config_print() {
25c56af8 610 global $CFG, $DB, $OUTPUT;
9b4b78fd 611 // Default behavior: print the config_instance.html file
e1b8bdf8 612 // You don't need to override this if you're satisfied with the above
f032aa7a 613 if (!$this->instance_allow_multiple() && !$this->instance_allow_config()) {
9b4b78fd 614 return false;
615 }
9b4b78fd 616
c7e3bc51 617 if (is_file($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html')) {
25c56af8 618 echo $OUTPUT->box_start('generalbox boxaligncenter blockconfiginstance');
6195e04a 619 include($CFG->dirroot .'/blocks/'. $this->name() .'/config_instance.html');
25c56af8 620 echo $OUTPUT->box_end();
6195e04a 621 } else {
9b4b78fd 622 notice(get_string('blockconfigbad'), str_replace('blockaction=', 'dummy=', qualified_me()));
623 }
40928036 624
9b4b78fd 625 return true;
626 }
c7e3bc51 627
628 /**
629 * Serialize and store config data
c7e3bc51 630 */
f474a4e5 631 function instance_config_save($data, $nolongerused = false) {
a5d424df 632 global $DB;
f474a4e5 633 $DB->set_field('block_instances', 'configdata', base64_encode(serialize($data)),
e03c0c1d 634 array('id' => $this->instance->id));
9b4b78fd 635 }
636
0144a0a7 637 /**
638 * Replace the instance's configuration data with those currently in $this->config;
0144a0a7 639 */
f474a4e5 640 function instance_config_commit($nolongerused = false) {
a5d424df 641 global $DB;
f474a4e5 642 $this->instance_config_save($this->config);
0144a0a7 643 }
644
d4a03c00 645 /**
b33dd23a 646 * Do any additional initialization you may need at the time a new block instance is created
647 * @return boolean
b33dd23a 648 */
649 function instance_create() {
650 return true;
651 }
652
d4a03c00 653 /**
b33dd23a 654 * Delete everything related to this instance if you have been using persistent storage other than the configdata field.
655 * @return boolean
b33dd23a 656 */
657 function instance_delete() {
658 return true;
659 }
660
d4a03c00 661 /**
11306331 662 * Allows the block class to have a say in the user's ability to edit (i.e., configure) blocks of this type.
663 * The framework has first say in whether this will be allowed (e.g., no editing allowed unless in edit mode)
664 * but if the framework does allow it, the block can still decide to refuse.
665 * @return boolean
11306331 666 */
667 function user_can_edit() {
d14edf06 668 return has_capability('moodle/block:edit', $this->context);
11306331 669 }
670
d4a03c00 671 /**
11306331 672 * Allows the block class to have a say in the user's ability to create new instances of this block.
673 * The framework has first say in whether this will be allowed (e.g., no adding allowed unless in edit mode)
674 * but if the framework does allow it, the block can still decide to refuse.
675 * This function has access to the complete page object, the creation related to which is being determined.
676 * @return boolean
11306331 677 */
15a00b4b 678 function user_can_addto($page) {
d14edf06 679 return has_capability('moodle/block:edit', $page->context);
11306331 680 }
681
f432bebf 682 function get_extra_capabilities() {
d14edf06 683 return array('moodle/block:view', 'moodle/block:edit');
f432bebf 684 }
d4a03c00 685
686 // Methods deprecated in Moodle 2.0 ========================================
687
688 /**
689 * Default case: the block wants to be 180 pixels wide
690 * @deprecated since Moodle 2.0.
691 * @return int
692 */
693 function preferred_width() {
694 return 180;
695 }
696
697 /** @deprecated since Moodle 2.0. */
698 function _print_block() {
699 throw new coding_exception('_print_block is no longer used. It was a private ' .
700 'method of the block class, only for use by the blocks system. You ' .
701 'should not have been calling it anyway.');
702 }
703
704 /** @deprecated since Moodle 2.0. */
705 function _print_shadow() {
706 throw new coding_exception('_print_shadow is no longer used. It was a private ' .
707 'method of the block class, only for use by the blocks system. You ' .
708 'should not have been calling it anyway.');
709 }
710
711 /** @deprecated since Moodle 2.0. */
712 function _title_html() {
713 throw new coding_exception('_title_html is no longer used. It was a private ' .
714 'method of the block class, only for use by the blocks system. You ' .
715 'should not have been calling it anyway.');
716 }
717
718 /** @deprecated since Moodle 2.0. */
719 function _add_edit_controls() {
720 throw new coding_exception('_add_edit_controls is no longer used. It was a private ' .
721 'method of the block class, only for use by the blocks system. You ' .
722 'should not have been calling it anyway.');
723 }
724
7cf51142 725 /** @deprecated since Moodle 2.0. */
726 function config_print() {
727 throw new coding_exception('config_print() can no longer be used. Blocks should use a settings.php file.');
728 }
9d897331 729
d2c394f3
SH
730 public function instance_can_be_docked() {
731 global $CFG;
598ee317 732 return (!empty($CFG->allowblockstodock) && $this->page->theme->enable_dock);
9d897331
SH
733 }
734
1bcb7eb5 735 /** @callback callback functions for comments api */
736 public static function comment_template($options) {
737 $ret = <<<EOD
4ca6cfbf 738<div class="comment-userpicture">___picture___</div>
1bcb7eb5 739<div class="comment-content">
740 ___name___ - <span>___time___</span>
741 <div>___content___</div>
742</div>
743EOD;
744 return $ret;
745 }
746 public static function comment_permissions($options) {
747 return array('view'=>true, 'post'=>true);
748 }
749 public static function comment_url($options) {
750 return null;
751 }
a0e68ffc
DC
752 public static function comment_display($comments, $options) {
753 return $comments;
1bcb7eb5 754 }
755 public static function comment_add(&$comments, $options) {
756 return true;
757 }
0f3fe4b6 758}
759
c7e3bc51 760/**
3ef642d9 761 * Specialized class for displaying a block with a list of icons/text labels
762 *
d4a03c00 763 * The get_content method should set $this->content->items and (optionally)
764 * $this->content->icons, instead of $this->content->text.
765 *
3ef642d9 766 * @author Jon Papaioannou
767 * @package blocks
768 */
769
770class block_list extends block_base {
771 var $content_type = BLOCK_TYPE_LIST;
772
773 function is_empty() {
e92c286c 774 if ( !has_capability('moodle/block:view', $this->context) ) {
25a7d980 775 return true;
776 }
777
3ef642d9 778 $this->get_content();
779 return (empty($this->content->items) && empty($this->content->footer));
780 }
781
d4a03c00 782 protected function formatted_contents($output) {
783 $this->get_content();
784 if (!empty($this->content->items)) {
d5cbccb3 785 $this->get_required_javascript();
d4a03c00 786 return $output->list_block_contents($this->content->icons, $this->content->items);
3ef642d9 787 } else {
d4a03c00 788 return '';
3ef642d9 789 }
790 }
3ef642d9 791}
792
7d2a0492 793/**
794 * Specialized class for displaying a tree menu.
4ca6cfbf 795 *
7d2a0492 796 * The {@link get_content()} method involves setting the content of
797 * <code>$this->content->items</code> with an array of {@link tree_item}
798 * objects (these are the top-level nodes). The {@link tree_item::children}
799 * property may contain more tree_item objects, and so on. The tree_item class
800 * itself is abstract and not intended for use, use one of it's subclasses.
4ca6cfbf 801 *
7d2a0492 802 * Unlike {@link block_list}, the icons are specified as part of the items,
803 * not in a separate array.
804 *
805 * @author Alan Trick
806 * @package blocks
807 * @internal this extends block_list so we get is_empty() for free
808 */
809class block_tree extends block_list {
4ca6cfbf 810
7d2a0492 811 /**
812 * @var int specifies the manner in which contents should be added to this
813 * block type. In this case <code>$this->content->items</code> is used with
814 * {@link tree_item}s.
815 */
816 public $content_type = BLOCK_TYPE_TREE;
4ca6cfbf 817
7d2a0492 818 /**
819 * Make the formatted HTML ouput.
4ca6cfbf 820 *
7d2a0492 821 * Also adds the required javascript call to the page output.
822 *
78946b9b 823 * @param core_renderer $output
7d2a0492 824 * @return string HTML
825 */
826 protected function formatted_contents($output) {
827 // based of code in admin_tree
828 global $PAGE; // TODO change this when there is a proper way for blocks to get stuff into head.
829 static $eventattached;
830 if ($eventattached===null) {
831 $eventattached = true;
832 }
833 if (!$this->content) {
834 $this->content = new stdClass;
835 $this->content->items = array();
836 }
d5cbccb3 837 $this->get_required_javascript();
7d2a0492 838 $this->get_content();
1ce15fda 839 $content = $output->tree_block_contents($this->content->items,array('class'=>'block_tree list'));
7d2a0492 840 if (isset($this->id) && !is_numeric($this->id)) {
841 $content = $output->box($content, 'block_tree_box', $this->id);
842 }
843 return $content;
844 }
a0e68ffc 845}