Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   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   * Textarea type form element
  20   *
  21   * Contains HTML class for a textarea 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/textarea.php');
  29  require_once ('templatable_form_element.php');
  30  
  31  /**
  32   * Textarea type form element
  33   *
  34   * HTML class for a textarea 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_textarea extends HTML_QuickForm_textarea implements templatable {
  42      use templatable_form_element {
  43          export_for_template as export_for_template_base;
  44      }
  45  
  46      /** @var string Need to store id of form as we may need it for helpbutton */
  47      var $_formid = '';
  48  
  49      /** @var string html for help button, if empty then no help */
  50      var $_helpbutton='';
  51  
  52      /** @var bool if true label will be hidden */
  53      var $_hiddenLabel=false;
  54  
  55      /** @var bool Whether to force the display of this element to flow LTR. */
  56      protected $forceltr = false;
  57  
  58      /**
  59       * constructor
  60       *
  61       * @param string $elementName (optional) name of the text field
  62       * @param string $elementLabel (optional) text field label
  63       * @param string $attributes (optional) Either a typical HTML attribute string or an associative array
  64       */
  65      public function __construct($elementName=null, $elementLabel=null, $attributes=null) {
  66          parent::__construct($elementName, $elementLabel, $attributes);
  67      }
  68  
  69      /**
  70       * Old syntax of class constructor. Deprecated in PHP7.
  71       *
  72       * @deprecated since Moodle 3.1
  73       */
  74      public function MoodleQuickForm_textarea($elementName=null, $elementLabel=null, $attributes=null) {
  75          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  76          self::__construct($elementName, $elementLabel, $attributes);
  77      }
  78  
  79      /**
  80       * get html for help button
  81       *
  82       * @return string html for help button
  83       */
  84      function getHelpButton(){
  85          return $this->_helpbutton;
  86      }
  87  
  88      /**
  89       * Sets label to be hidden
  90       *
  91       * @param bool $hiddenLabel sets if label should be hidden
  92       */
  93      function setHiddenLabel($hiddenLabel){
  94          $this->_hiddenLabel = $hiddenLabel;
  95      }
  96  
  97      /**
  98       * Returns HTML for this form element.
  99       *
 100       * @return string
 101       */
 102      function toHtml(){
 103  
 104          // Add the class at the last minute.
 105          if ($this->get_force_ltr()) {
 106              if (!isset($this->_attributes['class'])) {
 107                  $this->_attributes['class'] = 'text-ltr';
 108              } else {
 109                  $this->_attributes['class'] .= ' text-ltr';
 110              }
 111          }
 112  
 113          if ($this->_hiddenLabel){
 114              $this->_generateId();
 115              return '<label class="accesshide" for="' . $this->getAttribute('id') . '" >' .
 116                      $this->getLabel() . '</label>' . parent::toHtml();
 117          } else {
 118              return parent::toHtml();
 119          }
 120      }
 121  
 122      /**
 123       * Called by HTML_QuickForm whenever form event is made on this element
 124       *
 125       * @param string $event Name of event
 126       * @param mixed $arg event arguments
 127       * @param object $caller calling object
 128       */
 129      function onQuickFormEvent($event, $arg, &$caller)
 130      {
 131          switch ($event) {
 132              case 'createElement':
 133                  $this->_formid = $caller->getAttribute('id');
 134                  break;
 135          }
 136          return parent::onQuickFormEvent($event, $arg, $caller);
 137      }
 138  
 139      /**
 140       * Slightly different container template when frozen.
 141       *
 142       * @return string
 143       */
 144      function getElementTemplateType(){
 145          if ($this->_flagFrozen){
 146              return 'static';
 147          } else {
 148              return 'default';
 149          }
 150      }
 151  
 152      /**
 153       * Get force LTR option.
 154       *
 155       * @return bool
 156       */
 157      public function get_force_ltr() {
 158          return $this->forceltr;
 159      }
 160  
 161      /**
 162       * Force the field to flow left-to-right.
 163       *
 164       * This is useful for fields such as code or configuration snippets.
 165       *
 166       * @param bool $value The value to set the option to.
 167       */
 168      public function set_force_ltr($value) {
 169          $this->forceltr = (bool) $value;
 170      }
 171  
 172      public function export_for_template(renderer_base $output) {
 173          $context = $this->export_for_template_base($output);
 174          $context['value'] = $this->getValue();
 175  
 176          return $context;
 177      }
 178  }