MDL-78962 core/loadingicon: remove jQuery requirement in the API
[moodle.git] / lib / form / text.php
blob38bf5b1061aca7a610d832e831f4b61eb63cb9c9
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/>.
18 /**
19 * Text type form element
21 * Contains HTML class for a text type element
23 * @package core_form
24 * @copyright 2006 Jamie Pratt <me@jamiep.org>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 require_once("HTML/QuickForm/text.php");
29 require_once('templatable_form_element.php');
31 /**
32 * Text type form element
34 * HTML class for a text type element
36 * @package core_form
37 * @category form
38 * @copyright 2006 Jamie Pratt <me@jamiep.org>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 class MoodleQuickForm_text extends HTML_QuickForm_text implements templatable {
42 use templatable_form_element;
44 /** @var string html for help button, if empty then no help */
45 var $_helpbutton='';
47 /** @var bool if true label will be hidden */
48 var $_hiddenLabel=false;
50 /** @var bool Whether to force the display of this element to flow LTR. */
51 protected $forceltr = false;
53 /**
54 * constructor
56 * @param string $elementName (optional) name of the text field
57 * @param string $elementLabel (optional) text field label
58 * @param string $attributes (optional) Either a typical HTML attribute string or an associative array
60 public function __construct($elementName=null, $elementLabel=null, $attributes=null) {
61 parent::__construct($elementName, $elementLabel, $attributes);
64 /**
65 * Old syntax of class constructor. Deprecated in PHP7.
67 * @deprecated since Moodle 3.1
69 public function MoodleQuickForm_text($elementName=null, $elementLabel=null, $attributes=null) {
70 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
71 self::__construct($elementName, $elementLabel, $attributes);
74 /**
75 * Sets label to be hidden
77 * @param bool $hiddenLabel sets if label should be hidden
79 function setHiddenLabel($hiddenLabel){
80 $this->_hiddenLabel = $hiddenLabel;
83 /**
84 * Freeze the element so that only its value is returned and set persistantfreeze to false
86 * @since Moodle 2.4
87 * @access public
88 * @return void
90 function freeze()
92 $this->_flagFrozen = true;
93 // No hidden element is needed refer MDL-30845
94 $this->setPersistantFreeze(false);
95 } //end func freeze
97 /**
98 * Returns the html to be used when the element is frozen
100 * @since Moodle 2.4
101 * @return string Frozen html
103 function getFrozenHtml()
105 $attributes = array('readonly' => 'readonly');
106 $this->updateAttributes($attributes);
107 return $this->_getTabs() . '<input' . $this->_getAttrString($this->_attributes) . ' />' . $this->_getPersistantData();
108 } //end func getFrozenHtml
111 * Returns HTML for this form element.
113 * @return string
115 public function toHtml() {
117 // Add the class at the last minute.
118 if ($this->get_force_ltr()) {
119 if (!isset($this->_attributes['class'])) {
120 $this->_attributes['class'] = 'text-ltr';
121 } else {
122 $this->_attributes['class'] .= ' text-ltr';
126 $this->_generateId();
127 if ($this->_flagFrozen) {
128 return $this->getFrozenHtml();
130 $html = $this->_getTabs() . '<input' . $this->_getAttrString($this->_attributes) . ' />';
132 if ($this->_hiddenLabel){
133 return '<label class="accesshide" for="'.$this->getAttribute('id').'" >'.
134 $this->getLabel() . '</label>' . $html;
135 } else {
136 return $html;
141 * get html for help button
143 * @return string html for help button
145 function getHelpButton(){
146 return $this->_helpbutton;
150 * Get force LTR option.
152 * @return bool
154 public function get_force_ltr() {
155 return $this->forceltr;
159 * Force the field to flow left-to-right.
161 * This is useful for fields such as URLs, passwords, settings, etc...
163 * @param bool $value The value to set the option to.
165 public function set_force_ltr($value) {
166 $this->forceltr = (bool) $value;