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
use core\context\system as context_system;
20
use moodle_url;
21
use stdClass;
22
 
23
/**
24
 * Custom menu item
25
 *
26
 * This class is used to represent one item within a custom menu that may or may
27
 * not have children.
28
 *
29
 * @copyright 2010 Sam Hemelryk
30
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 * @since Moodle 2.0
32
 * @package core
33
 * @category output
34
 */
35
class custom_menu_item implements renderable, templatable {
36
    /**
37
     * @var string The text to show for the item
38
     */
39
    protected $text;
40
 
41
    /**
42
     * @var string A title to apply to the item. By default the text
43
     */
44
    protected $title;
45
 
46
    /**
47
     * @var int A sort order for the item, not necessary if you order things in
48
     * the CFG var.
49
     */
50
    protected $sort;
51
 
52
    /**
53
     * @var array A array in which to store children this item has.
54
     */
55
    protected $children = [];
56
 
57
    /**
58
     * @var int A reference to the sort var of the last child that was added
59
     */
60
    protected $lastsort = 0;
61
 
62
    /**
63
     * Constructs the new custom menu item
64
     *
65
     * @param string $text
66
     * @param null|moodle_url $url A moodle url to apply as the link for this item [Optional]
67
     * @param string $title A title to apply to this item [Optional]
68
     * @param int $sort A sort or to use if we need to sort differently [Optional]
69
     * @param null|custom_menu_item $parent A reference to the parent custom_menu_item this child
70
     *        belongs to, only if the child has a parent. [Optional]
71
     * @param array $attributes Array of other HTML attributes for the custom menu item.
72
     */
73
    public function __construct(
74
        $text,
75
        /** @var moodle_url The link to give the icon if it has no children */
76
        protected ?moodle_url $url = null,
77
        $title = null,
78
        $sort = null,
79
        /**
80
         * @var custom_menu_item A reference to the parent for this item or NULL if
81
         * it is a top level item
82
         */
83
        protected ?custom_menu_item $parent = null,
84
        /** @var array Array of other HTML attributes for the custom menu item. */
85
        protected array $attributes = [],
86
    ) {
87
 
88
        // Use class setter method for text to ensure it's always a string type.
89
        $this->set_text($text);
90
 
91
        $this->title = $title;
92
        $this->sort = (int)$sort;
93
    }
94
 
95
    /**
96
     * Adds a custom menu item as a child of this node given its properties.
97
     *
98
     * @param string $text
99
     * @param null|moodle_url $url
100
     * @param string $title
101
     * @param int $sort
102
     * @param array $attributes Array of other HTML attributes for the custom menu item.
103
     * @return custom_menu_item
104
     */
105
    public function add(
106
        $text,
107
        ?moodle_url $url = null,
108
        $title = null,
109
        $sort = null,
110
        $attributes = [],
111
    ) {
112
        $key = count($this->children);
113
        if (empty($sort)) {
114
            $sort = $this->lastsort + 1;
115
        }
116
        $this->children[$key] = new custom_menu_item($text, $url, $title, $sort, $this, $attributes);
117
        $this->lastsort = (int)$sort;
118
        return $this->children[$key];
119
    }
120
 
121
    /**
122
     * Removes a custom menu item that is a child or descendant to the current menu.
123
     *
124
     * Returns true if child was found and removed.
125
     *
126
     * @param custom_menu_item $menuitem
127
     * @return bool
128
     */
129
    public function remove_child(custom_menu_item $menuitem) {
130
        $removed = false;
131
        if (($key = array_search($menuitem, $this->children)) !== false) {
132
            unset($this->children[$key]);
133
            $this->children = array_values($this->children);
134
            $removed = true;
135
        } else {
136
            foreach ($this->children as $child) {
137
                if ($removed = $child->remove_child($menuitem)) {
138
                    break;
139
                }
140
            }
141
        }
142
        return $removed;
143
    }
144
 
145
    /**
146
     * Returns the text for this item
147
     * @return string
148
     */
149
    public function get_text() {
150
        return $this->text;
151
    }
152
 
153
    /**
154
     * Returns the url for this item
155
     * @return moodle_url
156
     */
157
    public function get_url() {
158
        return $this->url;
159
    }
160
 
161
    /**
162
     * Returns the title for this item
163
     * @return string
164
     */
165
    public function get_title() {
166
        return $this->title;
167
    }
168
 
169
    /**
170
     * Sorts and returns the children for this item
171
     * @return array
172
     */
173
    public function get_children() {
174
        $this->sort();
175
        return $this->children;
176
    }
177
 
178
    /**
179
     * Gets the sort order for this child
180
     * @return int
181
     */
182
    public function get_sort_order() {
183
        return $this->sort;
184
    }
185
 
186
    /**
187
     * Gets the parent this child belong to
188
     * @return custom_menu_item
189
     */
190
    public function get_parent() {
191
        return $this->parent;
192
    }
193
 
194
    /**
195
     * Sorts the children this item has
196
     */
197
    public function sort() {
198
        usort($this->children, ['custom_menu', 'sort_custom_menu_items']);
199
    }
200
 
201
    /**
202
     * Returns true if this item has any children
203
     * @return bool
204
     */
205
    public function has_children() {
206
        return (count($this->children) > 0);
207
    }
208
 
209
    /**
210
     * Sets the text for the node
211
     * @param string $text
212
     */
213
    public function set_text($text) {
214
        $this->text = (string)$text;
215
    }
216
 
217
    /**
218
     * Sets the title for the node
219
     * @param string $title
220
     */
221
    public function set_title($title) {
222
        $this->title = (string)$title;
223
    }
224
 
225
    /**
226
     * Sets the url for the node
227
     * @param moodle_url $url
228
     */
229
    public function set_url(moodle_url $url) {
230
        $this->url = $url;
231
    }
232
 
233
    /**
234
     * Export this data so it can be used as the context for a mustache template.
235
     *
236
     * @param renderer_base $output Used to do a final render of any components that need to be rendered for export.
237
     * @return stdClass
238
     */
239
    public function export_for_template(renderer_base $output) {
240
        $syscontext = context_system::instance();
241
 
242
        $context = new stdClass();
243
        $context->moremenuid = uniqid();
244
        $context->text = \core_external\util::format_string($this->text, $syscontext->id);
245
        $context->url = $this->url ? $this->url->out() : null;
246
        // No need for the title if it's the same with text.
247
        if ($this->text !== $this->title) {
248
            // Show the title attribute only if it's different from the text.
249
            $context->title = \core_external\util::format_string($this->title, $syscontext->id);
250
        }
251
        $context->sort = $this->sort;
252
        if (!empty($this->attributes)) {
253
            $context->attributes = $this->attributes;
254
        }
255
        $context->children = [];
256
        if (preg_match("/^#+$/", $this->text)) {
257
            $context->divider = true;
258
        }
259
        $context->haschildren = !empty($this->children) && (count($this->children) > 0);
260
        foreach ($this->children as $child) {
261
            $child = $child->export_for_template($output);
262
            array_push($context->children, $child);
263
        }
264
 
265
        return $context;
266
    }
267
}
268
 
269
// Alias this class to the old name.
270
// This file will be autoloaded by the legacyclasses autoload system.
271
// In future all uses of this class will be corrected and the legacy references will be removed.
272
class_alias(custom_menu_item::class, \custom_menu_item::class);