Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 16... Línea 16...
16
 
16
 
Línea 17... Línea 17...
17
declare(strict_types=1);
17
declare(strict_types=1);
Línea 18... Línea -...
18
 
-
 
19
namespace core\output;
-
 
20
 
18
 
21
use renderer_base;
19
namespace core\output;
22
 
20
 
23
/**
21
/**
24
 * A single-select combobox widget that is functionally similar to an HTML select element.
22
 * A single-select combobox widget that is functionally similar to an HTML select element.
25
 *
23
 *
26
 * @package   core
24
 * @package   core
27
 * @category  output
25
 * @category  output
28
 * @copyright 2022 Shamim Rezaie <shamim@moodle.com>
26
 * @copyright 2022 Shamim Rezaie <shamim@moodle.com>
29
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
28
 */
Línea 31... Línea 29...
31
class select_menu implements \renderable, \templatable {
29
class select_menu implements renderable, templatable {
32
    /** @var array List of options. */
30
    /** @var array List of options. */
Línea 39... Línea 37...
39
    protected $label;
37
    protected $label;
Línea 40... Línea 38...
40
 
38
 
41
    /** @var array Button label's attributes */
39
    /** @var array Button label's attributes */
Línea -... Línea 40...
-
 
40
    protected $labelattributes = [];
-
 
41
 
-
 
42
    /** @var bool Whether the label is inline or not */
42
    protected $labelattributes = [];
43
    protected $inlinelabel = false;
43
 
44
 
Línea 44... Línea 45...
44
    /** @var string Name of the combobox element */
45
    /** @var string Name of the combobox element */
45
    protected $name;
46
    protected $name;
46
 
47
 
47
    /**
48
    /**
48
     * select_menu constructor.
49
     * select_menu constructor.
49
     *
50
     *
50
     * @param string $name Name of the combobox element
51
     * @param string $name Name of the combobox element
51
     * @param array $options List of options in an associative array format like ['val' => 'Option'].
52
     * @param array $options List of options in an associative array format like ['val' => 'Option'].
52
     *                       Supports grouped options as well.
53
     *                       Supports grouped options as well. Empty string or null values will be rendered as dividers.
53
     * @param string|null $selected The value of the preselected option.
54
     * @param string|null $selected The value of the preselected option.
54
     */
55
     */
55
    public function __construct(string $name, array $options, string $selected = null) {
56
    public function __construct(string $name, array $options, ?string $selected = null) {
56
        $this->name = $name;
57
        $this->name = $name;
Línea 57... Línea 58...
57
        $this->options = $options;
58
        $this->options = $options;
58
        $this->selected = $selected;
59
        $this->selected = $selected;
59
    }
60
    }
60
 
61
 
61
    /**
62
    /**
-
 
63
     * Sets the select menu's label.
62
     * Sets the select menu's label.
64
     *
63
     *
65
     * @param string $label The label.
64
     * @param string $label The label.
66
     * @param array $attributes List of attributes to apply on the label element.
65
     * @param array $attributes List of attributes to apply on the label element.
67
     * @param bool $inlinelabel Whether the label is inline or not.
-
 
68
     */
66
     */
69
    public function set_label(string $label, array $attributes = [], bool $inlinelabel = false) {
Línea 67... Línea 70...
67
    public function set_label(string $label, array $attributes = []) {
70
        $this->label = $label;
68
        $this->label = $label;
71
        $this->labelattributes = $attributes;
69
        $this->labelattributes = $attributes;
72
        $this->inlinelabel = $inlinelabel;
Línea 83... Línea 86...
83
                    if (!isset($flattened[$groupname])) {
86
                    if (!isset($flattened[$groupname])) {
84
                        $flattened[$groupname] = [
87
                        $flattened[$groupname] = [
85
                            'name' => $groupname,
88
                            'name' => $groupname,
86
                            'isgroup' => true,
89
                            'isgroup' => true,
87
                            'id' => \html_writer::random_id('select-menu-group'),
90
                            'id' => \html_writer::random_id('select-menu-group'),
88
                            'options' => []
91
                            'options' => [],
89
                        ];
92
                        ];
90
                    }
93
                    }
91
                    foreach ($optoptions as $optvalue => $optoption) {
94
                    foreach ($optoptions as $optvalue => $optoption) {
-
 
95
                        if (empty($optoption)) {
-
 
96
                            $flattened[$groupname]['options'][$optvalue] = ['isdivider' => true];
-
 
97
                        } else {
92
                        $flattened[$groupname]['options'][$optvalue] = [
98
                            $flattened[$groupname]['options'][$optvalue] = [
93
                            'name' => $optoption,
99
                                'name' => $optoption,
94
                            'value' => $optvalue,
100
                                'value' => $optvalue,
95
                            'selected' => $this->selected == $optvalue,
101
                                'selected' => $this->selected == $optvalue,
96
                            'id' => \html_writer::random_id('select-menu-option'),
102
                                'id' => \html_writer::random_id('select-menu-option'),
-
 
103
                            ];
97
                        ];
104
                        }
98
                    }
105
                    }
99
                }
106
                }
100
            } else {
107
            } else {
-
 
108
                if (empty($option)) {
-
 
109
                    $flattened[$value] = ['isdivider' => true];
-
 
110
                } else {
101
                $flattened[$value] = [
111
                    $flattened[$value] = [
102
                    'name' => $option,
112
                        'name' => $option,
103
                    'value' => $value,
113
                        'value' => $value,
104
                    'selected' => $this->selected == $value,
114
                        'selected' => $this->selected == $value,
105
                    'id' => \html_writer::random_id('select-menu-option'),
115
                        'id' => \html_writer::random_id('select-menu-option'),
-
 
116
                    ];
106
                ];
117
                }
107
            }
118
            }
108
        }
119
        }
Línea 109... Línea 120...
109
 
120
 
110
        // Make non-associative array.
121
        // Make non-associative array.
Línea 153... Línea 164...
153
     */
164
     */
154
    public function export_for_template(renderer_base $output): \stdClass {
165
    public function export_for_template(renderer_base $output): \stdClass {
155
        $data = new \stdClass();
166
        $data = new \stdClass();
156
        $data->baseid = \html_writer::random_id('select-menu');
167
        $data->baseid = \html_writer::random_id('select-menu');
157
        $data->label = $this->label;
168
        $data->label = $this->label;
-
 
169
        $data->inlinelabel = $this->inlinelabel;
158
        $data->options = $this->flatten_options();
170
        $data->options = $this->flatten_options();
159
        $data->selectedoption = $this->get_selected_option();
171
        $data->selectedoption = $this->get_selected_option();
160
        $data->name = $this->name;
172
        $data->name = $this->name;
161
        $data->value = $this->selected;
173
        $data->value = $this->selected;