moodleblock.class.php 25.5 KB
Newer Older
tjhunt's avatar
tjhunt committed
1
<?php
2
3
// This file is part of Moodle - http://moodle.org/
//
tjhunt's avatar
tjhunt committed
4
5
6
7
8
9
10
11
12
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
13
//
tjhunt's avatar
tjhunt committed
14
15
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16

17
/**
defacer's avatar
   
defacer committed
18
 * This file contains the parent class for moodle blocks, block_base.
19
 *
20
 * @package    core_block
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU Public License
22
23
24
25
26
27
28
 */

/// Constants

/**
 * 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
define('BLOCK_TYPE_LIST',    1);
30
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
define('BLOCK_TYPE_TEXT',    2);
35
36
37
38
/**
 * Block type of tree. $this->content->items is a list of tree_item objects and $this->content->footer is a string.
 */
define('BLOCK_TYPE_TREE',    3);
39
40

/**
41
 * Class for describing a moodle block, all Moodle blocks derive from this class
42
43
 *
 * @author Jon Papaioannou
44
 * @package core_block
45
 */
defacer's avatar
   
defacer committed
46
class block_base {
47
48
49
50
51

    /**
     * Internal var for storing/caching translated strings
     * @var string $str
     */
52
    var $str;
53
54

    /**
55
     * The title of the block to be displayed in the block title area.
56
57
     * @var string $title
     */
58
    var $title         = NULL;
59

60
61
62
63
64
65
    /**
     * The name of the block to be displayed in the block title area if the title is empty.
     * @var string arialabel
     */
    var $arialabel         = NULL;

66
    /**
defacer's avatar
   
defacer committed
67
     * The type of content that this block creates. Currently support options - BLOCK_TYPE_LIST, BLOCK_TYPE_TEXT
68
69
     * @var int $content_type
     */
defacer's avatar
   
defacer committed
70
    var $content_type  = BLOCK_TYPE_TEXT;
71
72
73
74
75

    /**
     * An object to contain the information to be displayed in the block.
     * @var stdObject $content
     */
76
    var $content       = NULL;
77
78
79
80
81

    /**
     * The initialized instance of this block object.
     * @var block $instance
     */
82
    var $instance      = NULL;
83

84
85
86
87
88
89
    /**
     * The page that this block is appearing on.
     * @var moodle_page
     */
    public $page       = NULL;

90
91
92
93
94
95
    /**
     * This blocks's context.
     * @var stdClass
     */
    public $context    = NULL;

96
97
98
99
    /**
     * An object containing the instance configuration information for the current instance of this block.
     * @var stdObject $config
     */
100
101
    var $config        = NULL;

102
    /**
103
104
105
106
107
108
     * How often the cronjob should run, 0 if not at all.
     * @var int $cron
     */

    var $cron          = NULL;

109
/// Class Functions
110

111
112
113
114
115
    /**
     * Fake constructor to keep PHP5 happy
     *
     */
    function __construct() {
116
        $this->init();
117
    }
118

119
120
121
122
123
124
    /**
     * Function that can be overridden to do extra cleanup before
     * the database tables are deleted. (Called once per block, not per instance!)
     */
    function before_delete() {
    }
125

126
127
128
129
130
131
    /**
     * Returns the block name, as present in the class name,
     * the database, the block directory, etc etc.
     *
     * @return string
     */
132
133
134
135
    function name() {
        // Returns the block name, as present in the class name,
        // the database, the block directory, etc etc.
        static $myname;
136
        if ($myname === NULL) {
137
138
139
140
141
142
            $myname = strtolower(get_class($this));
            $myname = substr($myname, strpos($myname, '_') + 1);
        }
        return $myname;
    }

143
144
145
146
147
148
149
    /**
     * Parent class version of this function simply returns NULL
     * This should be implemented by the derived class to return
     * the content object.
     *
     * @return stdObject
     */
150
151
152
153
    function get_content() {
        // This should be implemented by the derived class.
        return NULL;
    }
154
155
156
157

    /**
     * Returns the class $title var value.
     *
158
     * Intentionally doesn't check if a title is set.
159
160
161
162
     * This is already done in {@link _self_test()}
     *
     * @return string $this->title
     */
163
    function get_title() {
164
        // Intentionally doesn't check if a title is set. This is already done in _self_test()
165
166
        return $this->title;
    }
167
168
169
170

    /**
     * Returns the class $content_type var value.
     *
171
     * Intentionally doesn't check if content_type is set.
172
173
174
175
     * This is already done in {@link _self_test()}
     *
     * @return string $this->content_type
     */
176
    function get_content_type() {
177
        // Intentionally doesn't check if a content_type is set. This is already done in _self_test()
178
179
        return $this->content_type;
    }
180

defacer's avatar
   
defacer committed
181
182
    /**
     * Returns true or false, depending on whether this block has any content to display
183
     * and whether the user has permission to view the block
defacer's avatar
   
defacer committed
184
185
186
187
     *
     * @return boolean
     */
    function is_empty() {
188
        if ( !has_capability('moodle/block:view', $this->context) ) {
189
190
191
            return true;
        }

defacer's avatar
   
defacer committed
192
193
194
195
        $this->get_content();
        return(empty($this->content->text) && empty($this->content->footer));
    }

196
197
198
199
200
201
202
    /**
     * First sets the current value of $this->content to NULL
     * then calls the block's {@link get_content()} function
     * to set its value back.
     *
     * @return stdObject
     */
203
204
205
206
207
    function refresh_content() {
        // Nothing special here, depends on content()
        $this->content = NULL;
        return $this->get_content();
    }
208
209

    /**
210
     * Return a block_contents object representing the full contents of this block.
211
212
213
214
215
216
217
     *
     * This internally calls ->get_content(), and then adds the editing controls etc.
     *
     * You probably should not override this method, but instead override
     * {@link html_attributes()}, {@link formatted_contents()} or {@link get_content()},
     * {@link hide_header()}, {@link (get_edit_controls)}, etc.
     *
218
     * @return block_contents a representation of the block, for rendering.
219
     * @since Moodle 2.0.
220
     */
221
222
    public function get_content_for_output($output) {
        global $CFG;
223

224
        $bc = new block_contents($this->html_attributes());
225
        $bc->attributes['data-block'] = $this->name();
226
227
        $bc->blockinstanceid = $this->instance->id;
        $bc->blockpositionid = $this->instance->blockpositionid;
228

229
230
231
232
233
234
235
236
237
        if ($this->instance->visible) {
            $bc->content = $this->formatted_contents($output);
            if (!empty($this->content->footer)) {
                $bc->footer = $this->content->footer;
            }
        } else {
            $bc->add_class('invisible');
        }

238
239
240
        if (!$this->hide_header()) {
            $bc->title = $this->title;
        }
241

242
243
        if (empty($bc->title)) {
            $bc->arialabel = new lang_string('pluginname', get_class($this));
244
            $this->arialabel = $bc->arialabel;
245
        }
246

247
        if ($this->page->user_is_editing()) {
248
            $bc->controls = $this->page->blocks->edit_controls($this);
249
250
251
252
253
        } else {
            // we must not use is_empty on hidden blocks
            if ($this->is_empty() && !$bc->controls) {
                return null;
            }
254
255
        }

256
257
258
        if (empty($CFG->allowuserblockhiding)
                || (empty($bc->content) && empty($bc->footer))
                || !$this->instance_can_be_collapsed()) {
259
260
261
262
263
            $bc->collapsible = block_contents::NOT_HIDEABLE;
        } else if (get_user_preferences('block' . $bc->blockinstanceid . 'hidden', false)) {
            $bc->collapsible = block_contents::HIDDEN;
        } else {
            $bc->collapsible = block_contents::VISIBLE;
264
        }
defacer's avatar
   
defacer committed
265

266
267
268
269
        if ($this->instance_can_be_docked() && !$this->hide_header()) {
            $bc->dockable = true;
        }

tjhunt's avatar
tjhunt committed
270
        $bc->annotation = ''; // TODO MDL-19398 need to work out what to say here.
271
272

        return $bc;
273
    }
274

275
    /**
276
     * Convert the contents of the block to HTML.
277
     *
278
279
280
281
282
283
284
285
     * This is used by block base classes like block_list to convert the structured
     * $this->content->list and $this->content->icons arrays to HTML. So, in most
     * blocks, you probaby want to override the {@link get_contents()} method,
     * which generates that structured representation of the contents.
     *
     * @param $output The core_renderer to use when generating the output.
     * @return string the HTML that should appearn in the body of the block.
     * @since Moodle 2.0.
286
     */
287
288
    protected function formatted_contents($output) {
        $this->get_content();
289
        $this->get_required_javascript();
290
291
292
293
294
295
296
        if (!empty($this->content->text)) {
            return $this->content->text;
        } else {
            return '';
        }
    }

297
298
299
300
301
302
    /**
     * Tests if this block has been implemented correctly.
     * Also, $errors isn't used right now
     *
     * @return boolean
     */
303

304
305
306
307
308
309
    function _self_test() {
        // Tests if this block has been implemented correctly.
        // Also, $errors isn't used right now
        $errors = array();

        $correct = true;
310
        if ($this->get_title() === NULL) {
311
312
313
            $errors[] = 'title_not_set';
            $correct = false;
        }
314
        if (!in_array($this->get_content_type(), array(BLOCK_TYPE_LIST, BLOCK_TYPE_TEXT, BLOCK_TYPE_TREE))) {
315
316
317
            $errors[] = 'invalid_content_type';
            $correct = false;
        }
318
        //following selftest was not working when roles&capabilities were used from block
319
/*        if ($this->get_content() === NULL) {
320
321
            $errors[] = 'content_not_set';
            $correct = false;
322
        }*/
defacer's avatar
defacer committed
323
        $formats = $this->applicable_formats();
324
        if (empty($formats) || array_sum($formats) === 0) {
defacer's avatar
   
defacer committed
325
            $errors[] = 'no_formats';
326
327
            $correct = false;
        }
defacer's avatar
defacer committed
328

329
330
331
        return $correct;
    }

332
333
    /**
     * Subclasses should override this and return true if the
334
     * subclass block has a settings.php file.
335
336
337
     *
     * @return boolean
     */
338
339
340
    function has_config() {
        return false;
    }
341
342
343
344
345

    /**
     * Default behavior: save all variables as $CFG properties
     * You don't need to override this if you 're satisfied with the above
     *
346
     * @deprecated since Moodle 2.9 MDL-49385 - Please use Admin Settings functionality to save block configuration.
347
     * @todo MDL-49553 This will be deleted in Moodle 3.1
348
     * @param array $data
349
350
     * @return boolean
     */
351
    function config_save($data) {
352
        debugging('config_save($data) is deprecated, use Admin Settings functionality to save block configuration.', DEBUG_DEVELOPER);
353
        foreach ($data as $name => $value) {
354
355
356
            set_config($name, $value);
        }
        return true;
357
    }
358

359
    /**
tjhunt's avatar
tjhunt committed
360
361
362
363
364
365
366
367
368
     * Which page types this block may appear on.
     *
     * The information returned here is processed by the
     * {@link blocks_name_allowed_in_format()} function. Look there if you need
     * to know exactly how this works.
     *
     * Default case: everything except mod and tag.
     *
     * @return array page-type prefix => true/false.
369
     */
370
    function applicable_formats() {
defacer's avatar
   
defacer committed
371
        // Default case: the block can be used in courses and site index, but not in activities
372
        return array('all' => true, 'mod' => false, 'tag' => false);
373
    }
374

375
376
377
378
379

    /**
     * Default return is false - header will be shown
     * @return boolean
     */
380
381
382
    function hide_header() {
        return false;
    }
383
384

    /**
385
386
     * Return any HTML attributes that you want added to the outer <div> that
     * of the block when it is output.
387
388
389
390
391
392
393
394
395
396
397
398
399
400
     *
     * Because of the way certain JS events are wired it is a good idea to ensure
     * that the default values here still get set.
     * I found the easiest way to do this and still set anything you want is to
     * override it within your block in the following way
     *
     * <code php>
     * function html_attributes() {
     *    $attributes = parent::html_attributes();
     *    $attributes['class'] .= ' mynewclass';
     *    return $attributes;
     * }
     * </code>
     *
401
     * @return array attribute name => value.
402
     */
403
    function html_attributes() {
404
        $attributes = array(
405
            'id' => 'inst' . $this->instance->id,
406
407
            'class' => 'block_' . $this->name(). '  block',
            'role' => $this->get_aria_role()
408
        );
409
410
411
        if ($this->hide_header()) {
            $attributes['class'] .= ' no-header';
        }
412
        if ($this->instance_can_be_docked() && get_user_preferences('docked_block_instance_'.$this->instance->id, 0)) {
413
414
415
            $attributes['class'] .= ' dock_on_load';
        }
        return $attributes;
416
    }
417

418
    /**
tjhunt's avatar
tjhunt committed
419
     * Set up a particular instance of this class given data from the block_insances
420
     * table and the current page. (See {@link block_manager::load_blocks()}.)
tjhunt's avatar
tjhunt committed
421
422
423
     *
     * @param stdClass $instance data from block_insances, block_positions, etc.
     * @param moodle_page $the page this block is on.
424
     */
425
    function _load_instance($instance, $page) {
426
        if (!empty($instance->configdata)) {
427
428
429
            $this->config = unserialize(base64_decode($instance->configdata));
        }
        $this->instance = $instance;
430
        $this->context = context_block::instance($instance->id);
431
        $this->page = $page;
432
        $this->specialization();
433
434
    }

435
436
437
438
439
    /**
     * Allows the block to load any JS it requires into the page.
     *
     * By default this function simply permits the user to dock the block if it is dockable.
     */
440
    function get_required_javascript() {
441
        if ($this->instance_can_be_docked() && !$this->hide_header()) {
442
443
            user_preference_allow_ajax_update('docked_block_instance_'.$this->instance->id, PARAM_INT);
        }
444
    }
dhawes's avatar
dhawes committed
445

446
447
448
    /**
     * This function is called on your subclass right after an instance is loaded
     * Use this function to act on instance data just after it's loaded and before anything else is done
449
     * For instance: if your block will have different title's depending on location (site, course, blog, etc)
450
     */
451
452
453
454
    function specialization() {
        // Just to make sure that this method exists.
    }

455
    /**
defacer's avatar
   
defacer committed
456
     * Is each block of this type going to have instance-specific configuration?
tjhunt's avatar
tjhunt committed
457
     * Normally, this setting is controlled by {@link instance_allow_multiple()}: if multiple
defacer's avatar
   
defacer committed
458
459
460
     * instances are allowed, then each will surely need its own configuration. However, in some
     * cases it may be necessary to provide instance configuration to blocks that do not want to
     * allow multiple instances. In that case, make this function return true.
tjhunt's avatar
tjhunt committed
461
     * I stress again that this makes a difference ONLY if {@link instance_allow_multiple()} returns false.
defacer's avatar
   
defacer committed
462
463
464
465
466
467
468
469
     * @return boolean
     */
    function instance_allow_config() {
        return false;
    }

    /**
     * Are you going to allow multiple instances of each block?
470
471
472
     * If yes, then it is assumed that the block WILL USE per-instance configuration
     * @return boolean
     */
473
474
475
476
477
    function instance_allow_multiple() {
        // Are you going to allow multiple instances of each block?
        // If yes, then it is assumed that the block WILL USE per-instance configuration
        return false;
    }
dhawes's avatar
dhawes committed
478

479
480
481
    /**
     * Serialize and store config data
     */
482
    function instance_config_save($data, $nolongerused = false) {
483
        global $DB;
484
        $DB->set_field('block_instances', 'configdata', base64_encode(serialize($data)),
485
                array('id' => $this->instance->id));
486
487
    }

defacer's avatar
   
defacer committed
488
489
490
    /**
     * Replace the instance's configuration data with those currently in $this->config;
     */
491
    function instance_config_commit($nolongerused = false) {
492
        global $DB;
493
        $this->instance_config_save($this->config);
defacer's avatar
   
defacer committed
494
495
    }

496
    /**
defacer's avatar
   
defacer committed
497
498
499
500
501
502
503
     * Do any additional initialization you may need at the time a new block instance is created
     * @return boolean
     */
    function instance_create() {
        return true;
    }

504
505
506
507
508
509
510
511
512
    /**
     * Copy any block-specific data when copying to a new block instance.
     * @param int $fromid the id number of the block instance to copy from
     * @return boolean
     */
    public function instance_copy($fromid) {
        return true;
    }

513
    /**
defacer's avatar
   
defacer committed
514
515
516
517
518
519
520
     * Delete everything related to this instance if you have been using persistent storage other than the configdata field.
     * @return boolean
     */
    function instance_delete() {
        return true;
    }

521
    /**
defacer's avatar
   
defacer committed
522
523
524
525
526
527
     * Allows the block class to have a say in the user's ability to edit (i.e., configure) blocks of this type.
     * The framework has first say in whether this will be allowed (e.g., no editing allowed unless in edit mode)
     * but if the framework does allow it, the block can still decide to refuse.
     * @return boolean
     */
    function user_can_edit() {
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
        global $USER;

        if (has_capability('moodle/block:edit', $this->context)) {
            return true;
        }

        // The blocks in My Moodle are a special case.  We want them to inherit from the user context.
        if (!empty($USER->id)
            && $this->instance->parentcontextid == $this->page->context->id   // Block belongs to this page
            && $this->page->context->contextlevel == CONTEXT_USER             // Page belongs to a user
            && $this->page->context->instanceid == $USER->id) {               // Page belongs to this user
            return has_capability('moodle/my:manageblocks', $this->page->context);
        }

        return false;
defacer's avatar
   
defacer committed
543
544
    }

545
    /**
defacer's avatar
   
defacer committed
546
547
548
549
     * Allows the block class to have a say in the user's ability to create new instances of this block.
     * The framework has first say in whether this will be allowed (e.g., no adding allowed unless in edit mode)
     * but if the framework does allow it, the block can still decide to refuse.
     * This function has access to the complete page object, the creation related to which is being determined.
Sam Hemelryk's avatar
Sam Hemelryk committed
550
551
     *
     * @param moodle_page $page
defacer's avatar
   
defacer committed
552
553
     * @return boolean
     */
554
    function user_can_addto($page) {
555
556
557
558
        global $USER;

        // The blocks in My Moodle are a special case and use a different capability.
        if (!empty($USER->id)
559
            && $page->context->contextlevel == CONTEXT_USER // Page belongs to a user
560
561
            && $page->context->instanceid == $USER->id // Page belongs to this user
            && $page->pagetype == 'my-index') { // Ensure we are on the My Moodle page
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576

            // If the block cannot be displayed on /my it is ok if the myaddinstance capability is not defined.
            $formats = $this->applicable_formats();
            // Is 'my' explicitly forbidden?
            // If 'all' has not been allowed, has 'my' been explicitly allowed?
            if ((isset($formats['my']) && $formats['my'] == false)
                || (empty($formats['all']) && empty($formats['my']))) {

                // Block cannot be added to /my regardless of capabilities.
                return false;
            } else {
                $capability = 'block/' . $this->name() . ':myaddinstance';
                return $this->has_add_block_capability($page, $capability)
                       && has_capability('moodle/my:manageblocks', $page->context);
            }
577
578
        }

579
580
581
582
583
584
        $capability = 'block/' . $this->name() . ':addinstance';
        if ($this->has_add_block_capability($page, $capability)
                && has_capability('moodle/block:edit', $page->context)) {
            return true;
        }

585
        return false;
defacer's avatar
   
defacer committed
586
587
    }

588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
    /**
     * Returns true if the user can add a block to a page.
     *
     * @param moodle_page $page
     * @param string $capability the capability to check
     * @return boolean true if user can add a block, false otherwise.
     */
    private function has_add_block_capability($page, $capability) {
        // Check if the capability exists.
        if (!get_capability_info($capability)) {
            // Debug warning that the capability does not exist, but no more than once per page.
            static $warned = array();
            if (!isset($warned[$this->name()])) {
                debugging('The block ' .$this->name() . ' does not define the standard capability ' .
                        $capability , DEBUG_DEVELOPER);
                $warned[$this->name()] = 1;
            }
            // If the capability does not exist, the block can always be added.
            return true;
        } else {
            return has_capability($capability, $page->context);
        }
    }

Petr Skoda's avatar
Petr Skoda committed
612
    static function get_extra_capabilities() {
613
        return array('moodle/block:view', 'moodle/block:edit');
614
    }
615

616
617
    /**
     * Can be overridden by the block to prevent the block from being dockable.
618
     *
619
620
     * @return bool
     */
621
622
    public function instance_can_be_docked() {
        global $CFG;
623
        return (!empty($CFG->allowblockstodock) && $this->page->theme->enable_dock);
624
625
    }

626
    /**
627
     * If overridden and set to false by the block it will not be hidable when
628
629
630
631
632
633
634
635
     * editing is turned on.
     *
     * @return bool
     */
    public function instance_can_be_hidden() {
        return true;
    }

636
637
638
639
640
641
642
643
644
    /**
     * If overridden and set to false by the block it will not be collapsible.
     *
     * @return bool
     */
    public function instance_can_be_collapsed() {
        return true;
    }

dongsheng's avatar
dongsheng committed
645
646
647
    /** @callback callback functions for comments api */
    public static function comment_template($options) {
        $ret = <<<EOD
648
<div class="comment-userpicture">___picture___</div>
dongsheng's avatar
dongsheng committed
649
650
651
652
653
654
655
656
657
658
659
660
661
<div class="comment-content">
    ___name___ - <span>___time___</span>
    <div>___content___</div>
</div>
EOD;
        return $ret;
    }
    public static function comment_permissions($options) {
        return array('view'=>true, 'post'=>true);
    }
    public static function comment_url($options) {
        return null;
    }
662
663
    public static function comment_display($comments, $options) {
        return $comments;
dongsheng's avatar
dongsheng committed
664
665
666
667
    }
    public static function comment_add(&$comments, $options) {
        return true;
    }
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688

    /**
     * Returns the aria role attribute that best describes this block.
     *
     * Region is the default, but this should be overridden by a block is there is a region child, or even better
     * a landmark child.
     *
     * Options are as follows:
     *    - landmark
     *      - application
     *      - banner
     *      - complementary
     *      - contentinfo
     *      - form
     *      - main
     *      - navigation
     *      - search
     *
     * @return string
     */
    public function get_aria_role() {
689
        return 'complementary';
690
    }
691
692
}

693
/**
defacer's avatar
   
defacer committed
694
695
 * Specialized class for displaying a block with a list of icons/text labels
 *
696
697
698
 * The get_content method should set $this->content->items and (optionally)
 * $this->content->icons, instead of $this->content->text.
 *
defacer's avatar
   
defacer committed
699
 * @author Jon Papaioannou
700
 * @package core_block
defacer's avatar
   
defacer committed
701
702
703
704
705
706
 */

class block_list extends block_base {
    var $content_type  = BLOCK_TYPE_LIST;

    function is_empty() {
707
        if ( !has_capability('moodle/block:view', $this->context) ) {
708
709
710
            return true;
        }

defacer's avatar
   
defacer committed
711
712
713
714
        $this->get_content();
        return (empty($this->content->items) && empty($this->content->footer));
    }

715
716
    protected function formatted_contents($output) {
        $this->get_content();
717
        $this->get_required_javascript();
718
719
        if (!empty($this->content->items)) {
            return $output->list_block_contents($this->content->icons, $this->content->items);
defacer's avatar
   
defacer committed
720
        } else {
721
            return '';
defacer's avatar
   
defacer committed
722
723
        }
    }
724
725
726
727
728
729
730

    function html_attributes() {
        $attributes = parent::html_attributes();
        $attributes['class'] .= ' list_block';
        return $attributes;
    }

defacer's avatar
   
defacer committed
731
732
}

733
734
/**
 * Specialized class for displaying a tree menu.
735
 *
736
737
738
739
740
 * The {@link get_content()} method involves setting the content of
 * <code>$this->content->items</code> with an array of {@link tree_item}
 * objects (these are the top-level nodes). The {@link tree_item::children}
 * property may contain more tree_item objects, and so on. The tree_item class
 * itself is abstract and not intended for use, use one of it's subclasses.
741
 *
742
743
744
745
 * Unlike {@link block_list}, the icons are specified as part of the items,
 * not in a separate array.
 *
 * @author Alan Trick
746
 * @package core_block
747
748
749
 * @internal this extends block_list so we get is_empty() for free
 */
class block_tree extends block_list {
750

751
752
753
754
755
756
    /**
     * @var int specifies the manner in which contents should be added to this
     * block type. In this case <code>$this->content->items</code> is used with
     * {@link tree_item}s.
     */
    public $content_type = BLOCK_TYPE_TREE;
757

758
759
    /**
     * Make the formatted HTML ouput.
760
     *
761
762
     * Also adds the required javascript call to the page output.
     *
763
     * @param core_renderer $output
764
765
766
767
768
769
770
771
772
773
774
775
776
     * @return string HTML
     */
    protected function formatted_contents($output) {
        // based of code in admin_tree
        global $PAGE; // TODO change this when there is a proper way for blocks to get stuff into head.
        static $eventattached;
        if ($eventattached===null) {
            $eventattached = true;
        }
        if (!$this->content) {
            $this->content = new stdClass;
            $this->content->items = array();
        }
777
        $this->get_required_javascript();
778
        $this->get_content();
779
        $content = $output->tree_block_contents($this->content->items,array('class'=>'block_tree list'));
780
781
782
783
784
        if (isset($this->id) && !is_numeric($this->id)) {
            $content = $output->box($content, 'block_tree_box', $this->id);
        }
        return $content;
    }
785
}