2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * autocomplete type form element
21 * Contains HTML class for a autocomplete type element
24 * @copyright 2015 Damyon Wiese <damyon@moodle.com>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 require_once($CFG->libdir
. '/form/select.php');
33 * Autocomplete as you type form element
35 * HTML class for a autocomplete type element
38 * @copyright 2015 Damyon Wiese <damyon@moodle.com>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 class MoodleQuickForm_autocomplete
extends MoodleQuickForm_select
{
43 /** @var boolean $tags Should we allow typing new entries to the field? */
44 protected $tags = false;
45 /** @var string $ajax Name of an AMD module to send/process ajax requests. */
47 /** @var string $placeholder Placeholder text for an empty list. */
48 protected $placeholder = '';
49 /** @var bool $casesensitive Whether the search has to be case-sensitive. */
50 protected $casesensitive = false;
51 /** @var bool $showsuggestions Show suggestions by default - but this can be turned off. */
52 protected $showsuggestions = true;
53 /** @var string $noselectionstring String that is shown when there are no selections. */
54 protected $noselectionstring = '';
59 * @param string $elementName Select name attribute
60 * @param mixed $elementLabel Label(s) for the select
61 * @param mixed $options Data to be used to populate options
62 * @param mixed $attributes Either a typical HTML attribute string or an associative array. Special options
63 * "tags", "placeholder", "ajax", "multiple", "casesensitive" are supported.
65 public function __construct($elementName=null, $elementLabel=null, $options=null, $attributes=null) {
66 // Even if the constructor gets called twice we do not really want 2x options (crazy forms!).
67 $this->_options
= array();
68 if ($attributes === null) {
69 $attributes = array();
71 if (isset($attributes['tags'])) {
72 $this->tags
= $attributes['tags'];
73 unset($attributes['tags']);
75 if (isset($attributes['showsuggestions'])) {
76 $this->showsuggestions
= $attributes['showsuggestions'];
77 unset($attributes['showsuggestions']);
79 $this->placeholder
= get_string('search');
80 if (isset($attributes['placeholder'])) {
81 $this->placeholder
= $attributes['placeholder'];
82 unset($attributes['placeholder']);
84 $this->noselectionstring
= get_string('noselection', 'form');
85 if (isset($attributes['noselectionstring'])) {
86 $this->noselectionstring
= $attributes['noselectionstring'];
87 unset($attributes['noselectionstring']);
90 if (isset($attributes['ajax'])) {
91 $this->ajax
= $attributes['ajax'];
92 unset($attributes['ajax']);
94 if (isset($attributes['casesensitive'])) {
95 $this->casesensitive
= $attributes['casesensitive'] ?
true : false;
96 unset($attributes['casesensitive']);
98 parent
::__construct($elementName, $elementLabel, $options, $attributes);
100 $this->_type
= 'autocomplete';
104 * Old syntax of class constructor. Deprecated in PHP7.
106 * @deprecated since Moodle 3.1
108 public function MoodleQuickForm_autocomplete($elementName=null, $elementLabel=null, $options=null, $attributes=null) {
109 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER
);
110 self
::__construct($elementName, $elementLabel, $options, $attributes);
114 * Returns HTML for select form element.
121 // Enhance the select with javascript.
122 $this->_generateId();
123 $id = $this->getAttribute('id');
124 $PAGE->requires
->js_call_amd('core/form-autocomplete', 'enhance', $params = array('#' . $id, $this->tags
, $this->ajax
,
125 $this->placeholder
, $this->casesensitive
, $this->showsuggestions
, $this->noselectionstring
));
127 return parent
::toHTML();
131 * Search the current list of options to see if there are any options with this value.
132 * @param string $value to search
135 function optionExists($value) {
136 foreach ($this->_options
as $option) {
137 if (isset($option['attr']['value']) && ($option['attr']['value'] == $value)) {
145 * Set the value of this element. If values can be added or are unknown, we will
146 * make sure they exist in the options array.
147 * @param mixed string|array $value The value to set.
150 function setValue($value) {
151 $values = (array) $value;
152 foreach ($values as $onevalue) {
153 if (($this->tags ||
$this->ajax
) &&
154 (!$this->optionExists($onevalue)) &&
155 ($onevalue !== '_qf__force_multiselect_submission')) {
156 $this->addOption($onevalue, $onevalue);
159 return parent
::setValue($value);
163 * Returns a 'safe' element's value
165 * @param array array of submitted values to search
166 * @param bool whether to return the value as associative array
170 function exportValue(&$submitValues, $assoc = false) {
171 if ($this->ajax ||
$this->tags
) {
172 // When this was an ajax request, we do not know the allowed list of values.
173 $value = $this->_findValue($submitValues);
174 if (null === $value) {
175 $value = $this->getValue();
177 // Quickforms inserts a duplicate element in the form with
178 // this value so that a value is always submitted for this form element.
179 // Normally this is cleaned as a side effect of it not being a valid option,
180 // but in this case we need to detect and skip it manually.
181 if ($value === '_qf__force_multiselect_submission' ||
$value === null) {
184 return $this->_prepareValue($value, $assoc);
186 return parent
::exportValue($submitValues, $assoc);
191 * Called by HTML_QuickForm whenever form event is made on this element
193 * @param string $event Name of event
194 * @param mixed $arg event arguments
195 * @param object $caller calling object
198 function onQuickFormEvent($event, $arg, &$caller)
201 case 'createElement':
202 $caller->setType($arg[0], PARAM_TAGLIST
);
205 return parent
::onQuickFormEvent($event, $arg, $caller);