Proyectos de Subversion Moodle

Rev

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