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
declare(strict_types=1);
18
 
19
namespace core\output;
20
 
21
/**
22
 * A single-select combobox widget that is functionally similar to an HTML select element.
23
 *
24
 * @package   core
25
 * @category  output
26
 * @copyright 2022 Shamim Rezaie <shamim@moodle.com>
27
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
1441 ariadna 29
class select_menu implements renderable, templatable {
1 efrain 30
    /** @var array List of options. */
31
    protected $options;
32
 
33
    /** @var string|null The value of the preselected option. */
34
    protected $selected;
35
 
36
    /** @var string The combobox label */
37
    protected $label;
38
 
39
    /** @var array Button label's attributes */
40
    protected $labelattributes = [];
41
 
1441 ariadna 42
    /** @var bool Whether the label is inline or not */
43
    protected $inlinelabel = false;
44
 
1 efrain 45
    /** @var string Name of the combobox element */
46
    protected $name;
47
 
48
    /**
49
     * select_menu constructor.
50
     *
51
     * @param string $name Name of the combobox element
52
     * @param array $options List of options in an associative array format like ['val' => 'Option'].
1441 ariadna 53
     *                       Supports grouped options as well. Empty string or null values will be rendered as dividers.
1 efrain 54
     * @param string|null $selected The value of the preselected option.
55
     */
1441 ariadna 56
    public function __construct(string $name, array $options, ?string $selected = null) {
1 efrain 57
        $this->name = $name;
58
        $this->options = $options;
59
        $this->selected = $selected;
60
    }
61
 
62
    /**
63
     * Sets the select menu's label.
64
     *
65
     * @param string $label The label.
66
     * @param array $attributes List of attributes to apply on the label element.
1441 ariadna 67
     * @param bool $inlinelabel Whether the label is inline or not.
1 efrain 68
     */
1441 ariadna 69
    public function set_label(string $label, array $attributes = [], bool $inlinelabel = false) {
1 efrain 70
        $this->label = $label;
71
        $this->labelattributes = $attributes;
1441 ariadna 72
        $this->inlinelabel = $inlinelabel;
1 efrain 73
    }
74
 
75
    /**
76
     * Flatten the options for Mustache.
77
     *
78
     * @return array
79
     */
80
    protected function flatten_options(): array {
81
        $flattened = [];
82
 
83
        foreach ($this->options as $value => $option) {
84
            if (is_array($option)) {
85
                foreach ($option as $groupname => $optoptions) {
86
                    if (!isset($flattened[$groupname])) {
87
                        $flattened[$groupname] = [
88
                            'name' => $groupname,
89
                            'isgroup' => true,
90
                            'id' => \html_writer::random_id('select-menu-group'),
1441 ariadna 91
                            'options' => [],
1 efrain 92
                        ];
93
                    }
94
                    foreach ($optoptions as $optvalue => $optoption) {
1441 ariadna 95
                        if (empty($optoption)) {
96
                            $flattened[$groupname]['options'][$optvalue] = ['isdivider' => true];
97
                        } else {
98
                            $flattened[$groupname]['options'][$optvalue] = [
99
                                'name' => $optoption,
100
                                'value' => $optvalue,
101
                                'selected' => $this->selected == $optvalue,
102
                                'id' => \html_writer::random_id('select-menu-option'),
103
                            ];
104
                        }
1 efrain 105
                    }
106
                }
107
            } else {
1441 ariadna 108
                if (empty($option)) {
109
                    $flattened[$value] = ['isdivider' => true];
110
                } else {
111
                    $flattened[$value] = [
112
                        'name' => $option,
113
                        'value' => $value,
114
                        'selected' => $this->selected == $value,
115
                        'id' => \html_writer::random_id('select-menu-option'),
116
                    ];
117
                }
1 efrain 118
            }
119
        }
120
 
121
        // Make non-associative array.
122
        foreach ($flattened as $key => $value) {
123
            if (!empty($value['options'])) {
124
                $flattened[$key]['options'] = array_values($value['options']);
125
            }
126
        }
127
        $flattened = array_values($flattened);
128
 
129
        return $flattened;
130
    }
131
 
132
    /**
133
     * Return the name of the selected option.
134
     *
135
     * @return string|null The name of the selected option or null.
136
     */
137
    private function get_selected_option(): ?string {
138
        foreach ($this->options as $value => $option) {
139
            if (is_array($option)) {  // This is a group.
140
                foreach ($option as $groupname => $optoptions) {
141
                    // Loop through the options within the group to check whether any of them matches the 'selected' value.
142
                    foreach ($optoptions as $optvalue => $optoption) {
143
                        // If the value of the option matches the 'selected' value, return the name of the option.
144
                        if ($this->selected == $optvalue) {
145
                            return $optoption;
146
                        }
147
                    }
148
                }
149
            } else { // This is a standard option item.
150
                // If the value of the option matches the 'selected' value, return the name of the option.
151
                if ($this->selected == $value) {
152
                    return $option;
153
                }
154
            }
155
        }
156
        return null;
157
    }
158
 
159
    /**
160
     * Export for template.
161
     *
162
     * @param renderer_base $output The renderer.
163
     * @return \stdClass
164
     */
165
    public function export_for_template(renderer_base $output): \stdClass {
166
        $data = new \stdClass();
167
        $data->baseid = \html_writer::random_id('select-menu');
168
        $data->label = $this->label;
1441 ariadna 169
        $data->inlinelabel = $this->inlinelabel;
1 efrain 170
        $data->options = $this->flatten_options();
171
        $data->selectedoption = $this->get_selected_option();
172
        $data->name = $this->name;
173
        $data->value = $this->selected;
174
 
175
        // Label attributes.
176
        $data->labelattributes = [];
177
        // Map the label attributes.
178
        foreach ($this->labelattributes as $key => $value) {
179
            $data->labelattributes[] = ['name' => $key, 'value' => $value];
180
        }
181
 
182
        return $data;
183
    }
184
}