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