Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]

   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   * Button form element
  20   *
  21   * Contains HTML class for a button type element
  22   *
  23   * @package   core_form
  24   * @copyright 2007 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/button.php");
  29  require_once (__DIR__ . '/../outputcomponents.php');
  30  require_once ('templatable_form_element.php');
  31  
  32  /**
  33   * HTML class for a button type element
  34   *
  35   * Overloaded {@link HTML_QuickForm_button} to add help button
  36   *
  37   * @package   core_form
  38   * @category  form
  39   * @copyright 2007 Jamie Pratt <me@jamiep.org>
  40   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class MoodleQuickForm_button extends HTML_QuickForm_button implements templatable
  43  {
  44      use templatable_form_element {
  45          export_for_template as export_for_template_base;
  46      }
  47  
  48      /** @var string html for help button, if empty then no help */
  49      var $_helpbutton='';
  50  
  51      /** @var bool if true label will be hidden. */
  52      protected $_hiddenLabel = false;
  53  
  54      /**
  55       * Any class apart from 'btn' would be overridden with this content.
  56       *
  57       * By default, buttons will utilize the btn-secondary class. However, there are cases where we
  58       * require a button with different stylings (e.g. btn-primary). In these cases, $customclassoverride will override
  59       * the defaults mentioned previously and utilize the provided class(es).
  60       *
  61       * @var null|string $customclassoverride Custom class override for the input element
  62       */
  63      protected $customclassoverride;
  64  
  65      /**
  66       * constructor
  67       *
  68       * @param string $elementName (optional) name for the button
  69       * @param string $value (optional) value for the button
  70       * @param mixed $attributes (optional) Either a typical HTML attribute string
  71       *              or an associative array
  72       * @param array $options Options to further customise the button. Currently accepted options are:
  73       *                  customclassoverride String The CSS class to use for the button instead of the standard
  74       *                                             btn-primary and btn-secondary classes.
  75       */
  76      public function __construct($elementName=null, $value=null, $attributes=null, $options = []) {
  77          parent::__construct($elementName, $value, $attributes);
  78  
  79          $this->customclassoverride = $options['customclassoverride'] ?? null;
  80      }
  81  
  82      /**
  83       * Old syntax of class constructor. Deprecated in PHP7.
  84       *
  85       * @deprecated since Moodle 3.1
  86       */
  87      public function MoodleQuickForm_button($elementName=null, $value=null, $attributes=null) {
  88          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  89          self::__construct($elementName, $value, $attributes);
  90      }
  91  
  92      /**
  93       * get html for help button
  94       *
  95       * @return string html for help button
  96       */
  97      function getHelpButton(){
  98          return $this->_helpbutton;
  99      }
 100  
 101      /**
 102       * Slightly different container template when frozen.
 103       *
 104       * @return string
 105       */
 106      function getElementTemplateType(){
 107          if ($this->_flagFrozen){
 108              return 'nodisplay';
 109          } else {
 110              return 'default';
 111          }
 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      public function export_for_template(renderer_base $output) {
 124          $context = $this->export_for_template_base($output);
 125  
 126          if ($this->customclassoverride) {
 127              $context['customclassoverride'] = $this->customclassoverride;
 128          }
 129          return $context;
 130      }
 131  }