| 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 | defined('MOODLE_INTERNAL') || die();
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 | use core\output\choicelist;
 | 
        
           |  |  | 20 | use core\output\local\dropdown\status;
 | 
        
           |  |  | 21 |   | 
        
           |  |  | 22 | require_once('HTML/QuickForm/select.php');
 | 
        
           |  |  | 23 | require_once('templatable_form_element.php');
 | 
        
           |  |  | 24 |   | 
        
           |  |  | 25 | /**
 | 
        
           |  |  | 26 |  * User choice using a dropdown type form element.
 | 
        
           |  |  | 27 |  *
 | 
        
           |  |  | 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
 | 
        
           |  |  | 32 |  */
 | 
        
           |  |  | 33 | class MoodleQuickForm_choicedropdown extends HTML_QuickForm_select implements templatable {
 | 
        
           |  |  | 34 |   | 
        
           |  |  | 35 |     use templatable_form_element {
 | 
        
           |  |  | 36 |         export_for_template as export_for_template_base;
 | 
        
           |  |  | 37 |     }
 | 
        
           |  |  | 38 |   | 
        
           |  |  | 39 |     /**
 | 
        
           |  |  | 40 |      * @var string html for help button, if empty then no help.
 | 
        
           |  |  | 41 |      */
 | 
        
           |  |  | 42 |     protected string $_helpbutton = '';
 | 
        
           |  |  | 43 |   | 
        
           |  |  | 44 |     /**
 | 
        
           |  |  | 45 |      * @var bool if true label will be hidden.
 | 
        
           |  |  | 46 |      */
 | 
        
           |  |  | 47 |     protected bool $_hiddenLabel = false;
 | 
        
           |  |  | 48 |   | 
        
           |  |  | 49 |     /**
 | 
        
           |  |  | 50 |      * @var choicelist the user choices.
 | 
        
           |  |  | 51 |      */
 | 
        
           |  |  | 52 |     protected ?choicelist $choice = null;
 | 
        
           |  |  | 53 |   | 
        
           |  |  | 54 |     /**
 | 
        
           |  |  | 55 |      * @var string[] Dropdown dialog width.
 | 
        
           |  |  | 56 |      */
 | 
        
           |  |  | 57 |     public const WIDTH = status::WIDTH;
 | 
        
           |  |  | 58 |   | 
        
           |  |  | 59 |     /**
 | 
        
           |  |  | 60 |      * @var string the dropdown width (from core\output\local\dropdown\status::WIDTH).
 | 
        
           |  |  | 61 |      */
 | 
        
           |  |  | 62 |     protected string $dropdownwidth = status::WIDTH['small'];
 | 
        
           |  |  | 63 |   | 
        
           |  |  | 64 |     /**
 | 
        
           |  |  | 65 |      * Constructor.
 | 
        
           |  |  | 66 |      *
 | 
        
           |  |  | 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
 | 
        
           |  |  | 71 |      */
 | 
        
           |  |  | 72 |     public function __construct(
 | 
        
           |  |  | 73 |         $elementname = null,
 | 
        
           |  |  | 74 |         $elementlabel = null,
 | 
        
           | 1441 | ariadna | 75 |         ?choicelist $options = null,
 | 
        
           | 1 | efrain | 76 |         $attributes = null
 | 
        
           |  |  | 77 |     ) {
 | 
        
           |  |  | 78 |         parent::__construct($elementname, $elementlabel, $options, $attributes);
 | 
        
           |  |  | 79 |         $this->_type = 'choicedropdown';
 | 
        
           |  |  | 80 |     }
 | 
        
           |  |  | 81 |   | 
        
           |  |  | 82 |     /**
 | 
        
           |  |  | 83 |      * Set the dropdown width.
 | 
        
           |  |  | 84 |      *
 | 
        
           |  |  | 85 |      * @param string $width
 | 
        
           |  |  | 86 |      */
 | 
        
           |  |  | 87 |     public function set_dialog_width(string $width) {
 | 
        
           |  |  | 88 |         $this->dropdownwidth = $width;
 | 
        
           |  |  | 89 |     }
 | 
        
           |  |  | 90 |   | 
        
           |  |  | 91 |     /**
 | 
        
           |  |  | 92 |      * Loads options from a choicelist.
 | 
        
           |  |  | 93 |      *
 | 
        
           |  |  | 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
 | 
        
           |  |  | 100 |      */
 | 
        
           |  |  | 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');
 | 
        
           |  |  | 104 |         }
 | 
        
           |  |  | 105 |         $this->choice = $choice;
 | 
        
           |  |  | 106 |         $this->choice->set_allow_empty(false);
 | 
        
           |  |  | 107 |         if ($value !== null && is_string($value)) {
 | 
        
           |  |  | 108 |             $choice->set_selected_value($value);
 | 
        
           |  |  | 109 |         }
 | 
        
           |  |  | 110 |         $this->setSelected($choice->get_selected_value());
 | 
        
           |  |  | 111 |         return true;
 | 
        
           |  |  | 112 |     }
 | 
        
           |  |  | 113 |   | 
        
           |  |  | 114 |     /**
 | 
        
           |  |  | 115 |      * Sets label to be hidden
 | 
        
           |  |  | 116 |      *
 | 
        
           |  |  | 117 |      * @param bool $hiddenLabel sets if label should be hidden
 | 
        
           |  |  | 118 |      */
 | 
        
           |  |  | 119 |     public function setHiddenLabel($hiddenLabel) {
 | 
        
           |  |  | 120 |         $this->_hiddenLabel = $hiddenLabel;
 | 
        
           |  |  | 121 |     }
 | 
        
           |  |  | 122 |   | 
        
           |  |  | 123 |     /**
 | 
        
           |  |  | 124 |      * Returns HTML for select form element.
 | 
        
           |  |  | 125 |      *
 | 
        
           |  |  | 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.
 | 
        
           |  |  | 129 |      *
 | 
        
           |  |  | 130 |      * @return string
 | 
        
           |  |  | 131 |      */
 | 
        
           |  |  | 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>';
 | 
        
           |  |  | 137 |         }
 | 
        
           |  |  | 138 |         $html .= parent::toHtml();
 | 
        
           |  |  | 139 |         return $html;
 | 
        
           |  |  | 140 |     }
 | 
        
           |  |  | 141 |   | 
        
           |  |  | 142 |     /**
 | 
        
           |  |  | 143 |      * get html for help button
 | 
        
           |  |  | 144 |      *
 | 
        
           |  |  | 145 |      * @return string html for help button
 | 
        
           |  |  | 146 |      */
 | 
        
           |  |  | 147 |     public function getHelpButton(): string {
 | 
        
           |  |  | 148 |         return $this->_helpbutton;
 | 
        
           |  |  | 149 |     }
 | 
        
           |  |  | 150 |   | 
        
           |  |  | 151 |     /**
 | 
        
           |  |  | 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.
 | 
        
           |  |  | 155 |      *
 | 
        
           |  |  | 156 |      * @return string
 | 
        
           |  |  | 157 |      */
 | 
        
           |  |  | 158 |     public function getElementTemplateType(): string {
 | 
        
           |  |  | 159 |         if ($this->_flagFrozen) {
 | 
        
           |  |  | 160 |             return 'static';
 | 
        
           |  |  | 161 |         } else {
 | 
        
           |  |  | 162 |             return 'default';
 | 
        
           |  |  | 163 |         }
 | 
        
           |  |  | 164 |     }
 | 
        
           |  |  | 165 |   | 
        
           |  |  | 166 |     /**
 | 
        
           |  |  | 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"
 | 
        
           |  |  | 169 |      *
 | 
        
           |  |  | 170 |      * @param string $submitvalues submitted values
 | 
        
           |  |  | 171 |      * @param bool $assoc if true the returned value is associated array
 | 
        
           |  |  | 172 |      * @return string|null
 | 
        
           |  |  | 173 |      */
 | 
        
           |  |  | 174 |     public function exportValue(&$submitvalues, $assoc = false) {
 | 
        
           |  |  | 175 |         $value = $this->_findValue($submitvalues) ?? $this->getValue();
 | 
        
           |  |  | 176 |         if (is_array($value)) {
 | 
        
           |  |  | 177 |             $value = reset($value);
 | 
        
           |  |  | 178 |         }
 | 
        
           |  |  | 179 |         if ($value === null) {
 | 
        
           |  |  | 180 |             return $this->_prepareValue($value, $assoc);
 | 
        
           |  |  | 181 |         }
 | 
        
           |  |  | 182 |         if (!$this->choice->has_value($value)) {
 | 
        
           |  |  | 183 |             $value = $this->choice->get_selected_value();
 | 
        
           |  |  | 184 |         }
 | 
        
           |  |  | 185 |         return $this->_prepareValue($value, $assoc);
 | 
        
           |  |  | 186 |     }
 | 
        
           |  |  | 187 |   | 
        
           |  |  | 188 |     public function export_for_template(renderer_base $output): array {
 | 
        
           |  |  | 189 |         $context = $this->export_for_template_base($output);
 | 
        
           |  |  | 190 |   | 
        
           |  |  | 191 |         if (!empty($this->_values)) {
 | 
        
           |  |  | 192 |             $this->choice->set_selected_value(reset($this->_values));
 | 
        
           |  |  | 193 |         }
 | 
        
           |  |  | 194 |   | 
        
           |  |  | 195 |         $dialog = new status(
 | 
        
           |  |  | 196 |             $this->choice->get_selected_content($output),
 | 
        
           |  |  | 197 |             $this->choice,
 | 
        
           |  |  | 198 |             [
 | 
        
           |  |  | 199 |                 'extras' => ['data-form-controls' => $context['id']],
 | 
        
           |  |  | 200 |                 'buttonsync' => true,
 | 
        
           |  |  | 201 |                 'updatestatus' => true,
 | 
        
           |  |  | 202 |                 'dialogwidth' => $this->dropdownwidth,
 | 
        
           |  |  | 203 |             ]
 | 
        
           |  |  | 204 |         );
 | 
        
           |  |  | 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;
 | 
        
           |  |  | 209 |     }
 | 
        
           |  |  | 210 | }
 |