Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
/lib/form/ -> text.php (source)
   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  
  18  /**
  19   * Text type form element
  20   *
  21   * Contains HTML class for a text type element
  22   *
  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
  26   */
  27  
  28  require_once("HTML/QuickForm/text.php");
  29  require_once ('templatable_form_element.php');
  30  
  31  /**
  32   * Text type form element
  33   *
  34   * HTML class for a text type element
  35   *
  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
  40   */
  41  class MoodleQuickForm_text extends HTML_QuickForm_text implements templatable {
  42      use templatable_form_element;
  43  
  44      /** @var string html for help button, if empty then no help */
  45      var $_helpbutton='';
  46  
  47      /** @var bool if true label will be hidden */
  48      var $_hiddenLabel=false;
  49  
  50      /** @var bool Whether to force the display of this element to flow LTR. */
  51      protected $forceltr = false;
  52  
  53      /**
  54       * constructor
  55       *
  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
  59       */
  60      public function __construct($elementName=null, $elementLabel=null, $attributes=null) {
  61          parent::__construct($elementName, $elementLabel, $attributes);
  62      }
  63  
  64      /**
  65       * Old syntax of class constructor. Deprecated in PHP7.
  66       *
  67       * @deprecated since Moodle 3.1
  68       */
  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);
  72      }
  73  
  74      /**
  75       * Sets label to be hidden
  76       *
  77       * @param bool $hiddenLabel sets if label should be hidden
  78       */
  79      function setHiddenLabel($hiddenLabel){
  80          $this->_hiddenLabel = $hiddenLabel;
  81      }
  82  
  83      /**
  84       * Freeze the element so that only its value is returned and set persistantfreeze to false
  85       *
  86       * @since     Moodle 2.4
  87       * @access    public
  88       * @return    void
  89       */
  90      function freeze()
  91      {
  92          $this->_flagFrozen = true;
  93          // No hidden element is needed refer MDL-30845
  94          $this->setPersistantFreeze(false);
  95      } //end func freeze
  96  
  97      /**
  98       * Returns the html to be used when the element is frozen
  99       *
 100       * @since     Moodle 2.4
 101       * @return    string Frozen html
 102       */
 103      function getFrozenHtml()
 104      {
 105          $attributes = array('readonly' => 'readonly');
 106          $this->updateAttributes($attributes);
 107          return $this->_getTabs() . '<input' . $this->_getAttrString($this->_attributes) . ' />' . $this->_getPersistantData();
 108      } //end func getFrozenHtml
 109  
 110      /**
 111       * Returns HTML for this form element.
 112       *
 113       * @return string
 114       */
 115      public function toHtml() {
 116  
 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';
 123              }
 124          }
 125  
 126          $this->_generateId();
 127          if ($this->_flagFrozen) {
 128              return $this->getFrozenHtml();
 129          }
 130          $html = $this->_getTabs() . '<input' . $this->_getAttrString($this->_attributes) . ' />';
 131  
 132          if ($this->_hiddenLabel){
 133              return '<label class="accesshide" for="'.$this->getAttribute('id').'" >'.
 134                          $this->getLabel() . '</label>' . $html;
 135          } else {
 136               return $html;
 137          }
 138      }
 139  
 140      /**
 141       * get html for help button
 142       *
 143       * @return string html for help button
 144       */
 145      function getHelpButton(){
 146          return $this->_helpbutton;
 147      }
 148  
 149      /**
 150       * Get force LTR option.
 151       *
 152       * @return bool
 153       */
 154      public function get_force_ltr() {
 155          return $this->forceltr;
 156      }
 157  
 158      /**
 159       * Force the field to flow left-to-right.
 160       *
 161       * This is useful for fields such as URLs, passwords, settings, etc...
 162       *
 163       * @param bool $value The value to set the option to.
 164       */
 165      public function set_force_ltr($value) {
 166          $this->forceltr = (bool) $value;
 167      }
 168  }