Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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 renderable;
20
use renderer_base;
21
use core\output\named_templatable;
22
 
23
/**
24
 * A generic user choice output class.
25
 *
26
 * This class can be used as a generic user choice data structure for any dropdown,  modal, or any
27
 * other component that offers choices to the user.
28
 *
29
 * @package    core
30
 * @copyright  2023 Ferran Recio <ferran@moodle.com>
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class choicelist implements renderable, named_templatable {
34
 
35
    /** @var object[] The user choices. */
36
    protected $options = [];
37
 
38
    /** @var string the selected option. */
39
    protected $selected = null;
40
 
41
    /** @var string the choice description. */
42
    protected $description = null;
43
 
44
    /** @var bool if the selected value can be empty. */
45
    protected $allowempty = null;
46
 
47
    /**
48
     * Constructor.
49
     *
50
     * @param string $description the choice description.
51
     */
52
    public function __construct(?string $description = null) {
53
        $this->description = $description;
54
    }
55
 
56
    /**
57
     * Add option to the user choice.
58
     *
59
     * The definition object could contain the following keys:
60
     * - string description: the description of the option.
61
     * - \moodle_url url: the URL to link to.
62
     * - \pix_icon icon: the icon to display.
63
     * - bool disabled: whether the option is disabled.
64
     * - bool selected: whether the option is selected.
65
     * - array extras: an array of HTML attributes to add to the option (attribute => value).
66
     *
67
     * @param string $value
68
     * @param string $name
69
     * @param array $definition an optional array of definition for the option.
70
     */
71
    public function add_option(string $value, string $name, array $definition = []) {
72
        $option = [
73
            'value' => $value,
74
            'name' => $name,
75
            'description' => $definition['description'] ?? null,
76
            'url' => $definition['url'] ?? null,
77
            'icon' => $definition['icon'] ?? null,
78
            'disabled' => (!empty($definition['disabled'])) ? true : false,
79
        ];
80
        if (!empty($definition['selected'])) {
81
            $this->selected = $value;
82
        }
83
        $this->options[$value] = $option;
84
        if (isset($definition['extras'])) {
85
            $this->set_option_extras($value, $definition['extras']);
86
        }
87
    }
88
 
89
    /**
90
     * Get the number of options added to the choice list.
91
     * @return int
92
     */
93
    public function count_options(): int {
94
        return count($this->options);
95
    }
96
 
97
    /**
98
     * Get the selectable options.
99
     *
100
     * This method returns an array of options that are selectable, excluding the selected option and any disabled options.
101
     *
102
     * @return \stdClass[]
103
     */
104
    public function get_selectable_options(): array {
105
        $selectableOptions = [];
106
        foreach ($this->options as $option) {
107
            if ($option['value'] !== $this->selected && !$option['disabled']) {
108
                $selectableOptions[] = (object) $option;
109
            }
110
        }
111
        return $selectableOptions;
112
    }
113
 
114
    /**
115
     * Set the selected option.
116
     *
117
     * @param string $value The value of the selected option.
118
     */
119
    public function set_selected_value(string $value) {
120
        $this->selected = $value;
121
    }
122
 
123
    /**
124
     * Get the selected option.
125
     *
126
     * @return string|null The value of the selected option.
127
     */
128
    public function get_selected_value(): ?string {
129
        if (empty($this->selected) && !$this->allowempty && !empty($this->options)) {
130
            return array_key_first($this->options);
131
        }
132
        return $this->selected;
133
    }
134
 
135
    /**
136
     * Set the allow empty option.
137
     * @param bool $allowempty Whether the selected value can be empty.
138
     */
139
    public function set_allow_empty(bool $allowempty) {
140
        $this->allowempty = $allowempty;
141
    }
142
 
143
    /**
144
     * Get the allow empty option.
145
     * @return bool Whether the selected value can be empty.
146
     */
147
    public function get_allow_empty(): bool {
148
        return $this->allowempty;
149
    }
150
 
151
    /**
152
     * Check if the value is in the options.
153
     * @param string $value The value to check.
154
     * @return bool
155
     */
156
    public function has_value(string $value): bool {
157
        return isset($this->options[$value]);
158
    }
159
 
160
    /**
161
     * Set the general choice description option.
162
     *
163
     * @param string $value the new description.
164
     */
165
    public function set_description(string $value) {
166
        $this->description = $value;
167
    }
168
 
169
    /**
170
     * Get the choice description option.
171
     *
172
     * @return string|null the current description.
173
     */
174
    public function get_description(): ?string {
175
        return $this->description;
176
    }
177
 
178
    /**
179
     * Set the option disabled.
180
     *
181
     * @param string $value The value of the option.
182
     * @param bool $disabled Whether the option is disabled.
183
     */
184
    public function set_option_disabled(string $value, bool $disabled) {
185
        if (isset($this->options[$value])) {
186
            $this->options[$value]['disabled'] = $disabled;
187
        }
188
    }
189
 
190
    /**
191
     * Sets the HTML attributes to the option.
192
     *
193
     * This method will remove any previous extra attributes.
194
     *
195
     * @param string $value The value of the option.
196
     * @param array $extras an array to add HTML attributes to the option (attribute => value).
197
     */
198
    public function set_option_extras(string $value, array $extras) {
199
        if (!isset($this->options[$value])) {
200
            return;
201
        }
202
        $this->options[$value]['extras'] = [];
203
        $this->add_option_extras($value, $extras);
204
    }
205
 
206
    /**
207
     * Add HTML attributes to the option.
208
     * @param string $value The value of the option.
209
     * @param array $extras an array to add HTML attributes to the option (attribute => value).
210
     */
211
    public function add_option_extras(string $value, array $extras) {
212
        if (!isset($this->options[$value])) {
213
            return;
214
        }
215
        if (!isset($this->options[$value]['extras'])) {
216
            $this->options[$value]['extras'] = [];
217
        }
218
        foreach ($extras as $attribute => $attributevalue) {
219
            $this->options[$value]['extras'][] = [
220
                'attribute' => $attribute,
221
                'value' => $attributevalue,
222
            ];
223
        }
224
    }
225
 
226
    /**
227
     * Retrieves the HTML attributes for a given value from the options array.
228
 
229
     * @param string $value The value for which to retrieve the extras.
230
     * @return array an array of HTML attributes of the option (attribute => value).
231
     */
232
    public function get_option_extras(string $value): array {
233
        if (!isset($this->options[$value]) || !isset($this->options[$value]['extras'])) {
234
            return [];
235
        }
236
        $result = [];
237
        foreach ($this->options[$value]['extras'] as $extra) {
238
            $result[$extra['attribute']] = $extra['value'];
239
        }
240
        return $result;
241
    }
242
 
243
    /**
244
     * Get the selected option HTML.
245
     *
246
     * This method is used to display the selected option and the option icon.
247
     *
248
     * @param renderer_base $output The renderer.
249
     * @return string
250
     */
251
    public function get_selected_content(renderer_base $output): string {
252
        if (empty($this->selected)) {
253
            return '';
254
        }
255
        $option = $this->options[$this->selected];
256
        $icon = '';
257
        if (!empty($option['icon'])) {
258
            $icon = $output->render($option['icon']);
259
        }
260
        return $icon . $option['name'];
261
    }
262
 
263
    /**
264
     * Export for template.
265
     *
266
     * @param renderer_base $output The renderer.
267
     * @return array
268
     */
269
    public function export_for_template(renderer_base $output): array {
270
        $options = [];
271
        foreach ($this->options as $option) {
272
            if (!empty($option['icon'])) {
273
                $option['icon'] = $option['icon']->export_for_pix($output);
274
            }
275
            $option['hasicon'] = !empty($option['icon']);
276
 
277
            if (!empty($option['url'])) {
278
                $option['url'] = $option['url']->out(true);
279
            }
280
            $option['hasurl'] = !empty($option['url']);
281
 
282
            if ($option['value'] == $this->get_selected_value()) {
283
                $option['selected'] = true;
284
            }
285
 
286
            $option['optionnumber'] = count($options) + 1;
287
            $option['first'] = count($options) === 0;
288
            $option['optionuniqid'] = \html_writer::random_id('choice_option_');
289
 
290
            $options[] = $option;
291
        }
292
        return [
293
            'description' => $this->description,
294
            'options' => $options,
295
            'hasoptions' => !empty($options),
296
        ];
297
    }
298
 
299
    /**
300
     * Get the name of the template to use for this templatable.
301
     *
302
     * @param renderer_base $renderer The renderer requesting the template name
303
     * @return string
304
     */
305
    public function get_template_name(renderer_base $renderer): string {
306
        return 'core/local/choicelist';
307
    }
308
}