MDL-51360 core_grades: Improve documentation of grade_get_grades().
[moodle.git] / lib / form / choicedropdown.php
blobe01a752ac65e3e1a8311c5196d32ce12c2d45658
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.
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/>.
17 defined('MOODLE_INTERNAL') || die();
19 use core\output\choicelist;
20 use core\output\local\dropdown\status;
22 require_once('HTML/QuickForm/select.php');
23 require_once('templatable_form_element.php');
25 /**
26 * User choice using a dropdown type form element.
28 * @package core_form
29 * @category form
30 * @copyright 2023 Ferran Recio <ferran@moodle.com>
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 class MoodleQuickForm_choicedropdown extends HTML_QuickForm_select implements templatable {
35 use templatable_form_element {
36 export_for_template as export_for_template_base;
39 /**
40 * @var string html for help button, if empty then no help.
42 protected string $_helpbutton = '';
44 /**
45 * @var bool if true label will be hidden.
47 protected bool $_hiddenLabel = false;
49 /**
50 * @var choicelist the user choices.
52 protected ?choicelist $choice = null;
54 /**
55 * @var string[] Dropdown dialog width.
57 public const WIDTH = status::WIDTH;
59 /**
60 * @var string the dropdown width (from core\output\local\dropdown\status::WIDTH).
62 protected string $dropdownwidth = status::WIDTH['small'];
64 /**
65 * Constructor.
67 * @param string $elementname Select name attribute
68 * @param mixed $elementlabel Label(s) for the select
69 * @param choicelist $options Data to be used to populate options
70 * @param mixed $attributes Either a typical HTML attribute string or an associative array
72 public function __construct(
73 $elementname = null,
74 $elementlabel = null,
75 choicelist $options = null,
76 $attributes = null
77 ) {
78 parent::__construct($elementname, $elementlabel, $options, $attributes);
79 $this->_type = 'choicedropdown';
82 /**
83 * Set the dropdown width.
85 * @param string $width
87 public function set_dialog_width(string $width) {
88 $this->dropdownwidth = $width;
91 /**
92 * Loads options from a choicelist.
94 * @param choicelist $choice Options source currently supports assoc array or DB_result
95 * @param string|null $value optional value (in case it is not defined in the choicelist)
96 * @param string|null $unused2 unused
97 * @param string|null $unused3 unused
98 * @param string|null $unused4 unused
99 * @return bool
101 public function load(&$choice, $value = null, $unused2 = null, $unused3 = null, $unused4 = null): bool {
102 if (!$choice instanceof choicelist) {
103 throw new coding_exception('Choice must be instance of choicelist');
105 $this->choice = $choice;
106 $this->choice->set_allow_empty(false);
107 if ($value !== null && is_string($value)) {
108 $choice->set_selected_value($value);
110 $this->setSelected($choice->get_selected_value());
111 return true;
115 * Sets label to be hidden
117 * @param bool $hiddenLabel sets if label should be hidden
119 public function setHiddenLabel($hiddenLabel) {
120 $this->_hiddenLabel = $hiddenLabel;
124 * Returns HTML for select form element.
126 * This method is only needed when forms renderer is forces via
127 * $GLOBALS['_HTML_QuickForm_default_renderer']. Otherwise the
128 * renderer will use mustache templates.
130 * @return string
132 public function toHtml(): string {
133 $html = '';
134 if ($this->_hiddenLabel) {
135 $this->_generateId();
136 $html .= '<label class="accesshide" for="'.$this->getAttribute('id').'" >'.$this->getLabel().'</label>';
138 $html .= parent::toHtml();
139 return $html;
143 * get html for help button
145 * @return string html for help button
147 public function getHelpButton(): string {
148 return $this->_helpbutton;
152 * Slightly different container template when frozen. Don't want to use a label tag
153 * with a for attribute in that case for the element label but instead use a div.
154 * Templates are defined in renderer constructor.
156 * @return string
158 public function getElementTemplateType(): string {
159 if ($this->_flagFrozen) {
160 return 'static';
161 } else {
162 return 'default';
167 * We check the options and return only the values that _could_ have been
168 * selected. We also return a scalar value if select is not "multiple"
170 * @param string $submitvalues submitted values
171 * @param bool $assoc if true the returned value is associated array
172 * @return string|null
174 public function exportValue(&$submitvalues, $assoc = false) {
175 $value = $this->_findValue($submitvalues) ?? $this->getValue();
176 if (is_array($value)) {
177 $value = reset($value);
179 if ($value === null) {
180 return $this->_prepareValue($value, $assoc);
182 if (!$this->choice->has_value($value)) {
183 $value = $this->choice->get_selected_value();
185 return $this->_prepareValue($value, $assoc);
188 public function export_for_template(renderer_base $output): array {
189 $context = $this->export_for_template_base($output);
191 if (!empty($this->_values)) {
192 $this->choice->set_selected_value(reset($this->_values));
195 $dialog = new status(
196 $this->choice->get_selected_content($output),
197 $this->choice,
199 'extras' => ['data-form-controls' => $context['id']],
200 'buttonsync' => true,
201 'updatestatus' => true,
202 'dialogwidth' => $this->dropdownwidth,
205 $context['dropdown'] = $dialog->export_for_template($output);
206 $context['select'] = $this->choice->export_for_template($output);
207 $context['nameraw'] = $this->getName();
208 return $context;