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