Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace core\output;
18
 
19
/**
20
 * Renderable for the main page header.
21
 *
22
 * @package core
23
 * @category output
24
 * @since 2.9
25
 * @copyright 2015 Adrian Greeve <adrian@moodle.com>
26
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 */
28
class context_header implements renderable, templatable {
29
    /** @var string $heading Main heading */
30
    public $heading;
31
 
32
    /** @var int $headinglevel Main heading 'h' tag level */
33
    public $headinglevel;
34
 
35
    /** @var string|null $imagedata HTML code for the picture in the page header */
36
    public $imagedata;
37
 
38
    /**
39
     * @var array $additionalbuttons Additional buttons for the header e.g. Messaging button for the user header.
40
     *      array elements - title => alternate text for the image, or if no image is available the button text.
41
     *                       url => Link for the button to head to. Should be a moodle_url.
42
     *                       image => location to the image, or name of the image in /pix/t/{image name}.
43
     *                       linkattributes => additional attributes for the <a href> element.
44
     *                       page => page object. Don't include if the image is an external image.
45
     */
46
    public $additionalbuttons;
47
 
48
    /** @var string $prefix A string that is before the title */
49
    public $prefix;
50
 
51
    /**
52
     * Constructor.
53
     *
54
     * @param string $heading Main heading data.
55
     * @param int $headinglevel Main heading 'h' tag level.
56
     * @param string|null $imagedata HTML code for the picture in the page header.
57
     * @param string $additionalbuttons Buttons for the header e.g. Messaging button for the user header.
58
     * @param string $prefix Text that precedes the heading.
59
     */
60
    public function __construct($heading = null, $headinglevel = 1, $imagedata = null, $additionalbuttons = null, $prefix = null) {
61
        $this->heading = $heading;
62
        $this->headinglevel = $headinglevel;
63
        $this->imagedata = $imagedata;
64
        $this->additionalbuttons = $additionalbuttons;
65
        // If we have buttons then format them.
66
        if (isset($this->additionalbuttons)) {
67
            $this->format_button_images();
68
        }
69
        $this->prefix = $prefix;
70
    }
71
 
72
    /**
73
     * Adds an array element for a formatted image.
74
     */
75
    protected function format_button_images() {
76
 
77
        foreach ($this->additionalbuttons as $buttontype => $button) {
78
            $page = $button['page'];
79
            // If no image is provided then just use the title.
80
            if (!isset($button['image'])) {
81
                $this->additionalbuttons[$buttontype]['formattedimage'] = $button['title'];
82
            } else {
83
                // Check to see if this is an internal Moodle icon.
84
                $internalimage = $page->theme->resolve_image_location('t/' . $button['image'], 'moodle');
85
                if ($internalimage) {
86
                    $this->additionalbuttons[$buttontype]['formattedimage'] = 't/' . $button['image'];
87
                } else {
88
                    // Treat as an external image.
89
                    $this->additionalbuttons[$buttontype]['formattedimage'] = $button['image'];
90
                }
91
            }
92
 
93
            if (isset($button['linkattributes']['class'])) {
94
                $class = $button['linkattributes']['class'] . ' btn';
95
            } else {
96
                $class = 'btn';
97
            }
98
            // Add the bootstrap 'btn' class for formatting.
99
            $this->additionalbuttons[$buttontype]['linkattributes'] = array_merge(
100
                $button['linkattributes'],
101
                ['class' => $class]
102
            );
103
        }
104
    }
105
 
106
    /**
107
     * Export for template.
108
     *
109
     * @param renderer_base $output Renderer.
110
     * @return array
111
     */
112
    public function export_for_template(renderer_base $output): array {
113
        // Heading.
114
        $headingtext = isset($this->heading) ? $this->heading : $output->get_page()->heading;
115
        $heading = $output->heading($headingtext, $this->headinglevel, "h2 mb-0");
116
 
117
        // Buttons.
118
        if (isset($this->additionalbuttons)) {
119
            $additionalbuttons = [];
120
            foreach ($this->additionalbuttons as $button) {
121
                if (!isset($button->page)) {
122
                    // Include js for messaging.
123
                    if ($button['buttontype'] === 'togglecontact') {
124
                        \core_message\helper::togglecontact_requirejs();
125
                    }
126
                    if ($button['buttontype'] === 'message') {
127
                        \core_message\helper::messageuser_requirejs();
128
                    }
129
                }
130
                foreach ($button['linkattributes'] as $key => $value) {
131
                    $button['attributes'][] = ['name' => $key, 'value' => $value];
132
                }
133
                $additionalbuttons[] = $button;
134
            }
135
        }
136
 
137
        return [
138
            'heading' => $heading,
139
            'headinglevel' => $this->headinglevel,
140
            'imagedata' => $this->imagedata,
141
            'prefix' => $this->prefix,
142
            'hasadditionalbuttons' => !empty($additionalbuttons),
143
            'additionalbuttons' => $additionalbuttons ?? [],
144
        ];
145
    }
146
}
147
 
148
// Alias this class to the old name.
149
// This file will be autoloaded by the legacyclasses autoload system.
150
// In future all uses of this class will be corrected and the legacy references will be removed.
151
class_alias(context_header::class, \context_header::class);