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 moodle_exception;
20
use renderable;
21
use renderer_base;
22
use templatable;
23
 
24
/**
25
 * Renderable class for the comboboxsearch.
26
 *
27
 * @package    core_output
28
 * @copyright  2022 Mathew May <Mathew.solutions>
29
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
31
class comboboxsearch implements renderable, templatable {
32
 
33
    /** @var bool $renderlater Should the dropdown render straightaway? We sometimes need to output the component without all of the
34
     * data and leave the rendering of any defaults and actual data to the caller. We will give you a basic placeholder that can
35
     * then be easily replaced.*/
36
    protected $renderlater;
37
 
38
    /** @var string $buttoncontent What is the content of the "Button" that users will always see. */
39
    protected $buttoncontent;
40
 
41
    /** @var null|string $dropdowncontent The content that can be passed in to render immediately. */
42
    protected $dropdowncontent;
43
 
44
    /** @var null|string $parentclasses Any special classes to put on the HTMLElement that contains the BS events. */
45
    protected $parentclasses;
46
 
47
    /** @var null|string $buttonclasses Any special classes to put on the HTMLElement that triggers the dropdown. */
48
    protected $buttonclasses;
49
 
50
    /** @var null|string $dropdownclasses Any special classes to put on the HTMLElement that contains the actual dropdown. */
51
    protected $dropdownclasses;
52
 
53
    /** @var null|string $buttonheader If the button item in the tertiary nav needs an extra top header for context. */
54
    protected $buttonheader;
55
 
56
    /** @var boolean $usesbutton Whether to provide a A11y button. */
57
    protected $usesbutton;
58
 
59
    /** @var null|string $label The label of the combobox. */
60
    protected $label;
61
 
62
    /** @var null|string $name The name of the input element representing the combobox. */
63
    protected $name;
64
 
65
    /** @var null|string $value The value of the input element representing the combobox. */
66
    protected $value;
67
 
68
    /**
69
     * The class constructor.
70
     *
71
     * @param bool $renderlater How we figure out if we should render the template instantly.
72
     * @param string $buttoncontent What gets placed in the button.
73
     * @param ?string $dropdowncontent What will be placed in the dropdown if we are rendering now.
74
     * @param ?string $parentclasses The classes that can be added that the bootstrap events are attached to.
75
     * @param ?string $buttonclasses Any special classes that may be needed.
76
     * @param ?string $dropdownclasses Any special classes that may be needed.
77
     * @param ?string $buttonheader Sometimes we want extra context for a button before it is shown, basically a pseudo header.
78
     * @param ?bool $usebutton If we want the mustache to add the button roles for us or do we have another aria role node?
79
     * @param ?string $label The label of the combobox.
80
     * @param ?string $name The name of the input element representing the combobox.
81
     * @param ?string $value The value of the input element representing the combobox.
82
     * @throws moodle_exception If the implementor incorrectly calls this module.
83
     */
84
    public function __construct(
85
        bool $renderlater,
86
        string $buttoncontent,
87
        ?string $dropdowncontent = null,
88
        ?string $parentclasses = null,
89
        ?string $buttonclasses = null,
90
        ?string $dropdownclasses = null,
91
        ?string $buttonheader = null,
92
        ?bool $usebutton = true,
93
        ?string $label = null,
94
        ?string $name = null,
95
        ?string $value = null
96
    ) {
97
        // Ensure implementors cant request to render the content now and not provide us any to show.
98
        if (!$renderlater && empty($dropdowncontent)) {
99
            throw new moodle_exception(
100
                'incorrectdropdownvars',
101
                'core',
102
                '', null,
103
                'Dropdown content must be set to render later.'
104
            );
105
        }
106
 
107
        if ($usebutton && !$label) {
108
            debugging(
109
                    'You have requested to use the button but have not provided a label for the combobox.',
110
                    DEBUG_DEVELOPER
111
            );
112
        }
113
 
114
        if ($usebutton && !$name) {
115
            debugging(
116
                'You have requested to use the button but have not provided a name for the input element.',
117
                DEBUG_DEVELOPER
118
            );
119
        }
120
 
121
        $this->renderlater = $renderlater;
122
        $this->buttoncontent = $buttoncontent;
123
        $this->dropdowncontent = $dropdowncontent;
124
        $this->parentclasses = $parentclasses;
125
        $this->buttonclasses = $buttonclasses;
126
        $this->dropdownclasses = $dropdownclasses;
127
        $this->buttonheader = $buttonheader;
128
        $this->usesbutton = $usebutton;
129
        $this->label = $label;
130
        $this->name = $name;
131
        $this->value = $value;
132
    }
133
 
134
    /**
135
     * Export the data for the mustache template.
136
     *
137
     * @param renderer_base $output renderer to be used to render the action bar elements.
138
     * @return array
139
     */
140
    public function export_for_template(renderer_base $output): array {
141
        return [
142
            'renderlater' => $this->renderlater,
143
            'buttoncontent' => $this->buttoncontent ,
144
            'dropdowncontent' => $this->dropdowncontent,
145
            'parentclasses' => $this->parentclasses,
146
            'buttonclasses' => $this->buttonclasses,
147
            'dropdownclasses' => $this->dropdownclasses,
148
            'buttonheader' => $this->buttonheader,
149
            'usebutton' => $this->usesbutton,
150
            'instance' => rand(), // Template uniqid is per render out so sometimes these conflict.
151
            'label' => $this->label,
152
            'name' => $this->name,
153
            'value' => $this->value,
154
        ];
155
    }
156
 
157
    /**
158
     * Returns the standard template for the dropdown.
159
     *
160
     * @return string
161
     */
162
    public function get_template(): string {
163
        return 'core/comboboxsearch';
164
    }
165
}