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.
   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   * checkbox form element
  20   *
  21   * Contains HTML class for a checkbox 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/checkbox.php');
  29  require_once ('templatable_form_element.php');
  30  
  31  /**
  32   * HTML class for a checkbox type element
  33   *
  34   * Overloaded {@link HTML_QuickForm_checkbox} to add help button. Also, fixes bug in quickforms
  35   * checkbox, which lets previous set value override submitted value if checkbox is not checked
  36   * and no value is submitted
  37   *
  38   * @package   core_form
  39   * @category  form
  40   * @copyright 2007 Jamie Pratt <me@jamiep.org>
  41   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class MoodleQuickForm_checkbox extends HTML_QuickForm_checkbox implements templatable {
  44  
  45      use templatable_form_element {
  46          export_for_template as export_for_template_base;
  47      }
  48  
  49      /** @var string html for help button, if empty then no help */
  50      var $_helpbutton='';
  51  
  52      /**
  53       * Constructor
  54       *
  55       * @param string $elementName (optional) name of the checkbox
  56       * @param string $elementLabel (optional) checkbox label
  57       * @param string $text (optional) Text to put after the checkbox
  58       * @param mixed $attributes (optional) Either a typical HTML attribute string
  59       *              or an associative array
  60       */
  61      public function __construct($elementName=null, $elementLabel=null, $text='', $attributes=null) {
  62          parent::__construct($elementName, $elementLabel, $text, $attributes);
  63      }
  64  
  65      /**
  66       * Old syntax of class constructor. Deprecated in PHP7.
  67       *
  68       * @deprecated since Moodle 3.1
  69       */
  70      public function MoodleQuickForm_checkbox($elementName=null, $elementLabel=null, $text='', $attributes=null) {
  71          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  72          self::__construct($elementName, $elementLabel, $text, $attributes);
  73      }
  74  
  75      /**
  76       * get html for help button
  77       *
  78       * @return string html for help button
  79       */
  80      function getHelpButton(){
  81          return $this->_helpbutton;
  82      }
  83  
  84      /**
  85       * Called by HTML_QuickForm whenever form event is made on this element
  86       *
  87       * @param string $event Name of event
  88       * @param mixed $arg event arguments
  89       * @param object $caller calling object
  90       * @return bool
  91       */
  92      function onQuickFormEvent($event, $arg, &$caller)
  93      {
  94          //fixes bug in quickforms which lets previous set value override submitted value if checkbox is not checked
  95          // and no value is submitted
  96          switch ($event) {
  97              case 'updateValue':
  98                  // constant values override both default and submitted ones
  99                  // default values are overriden by submitted
 100                  $value = $this->_findValue($caller->_constantValues);
 101                  if (null === $value) {
 102                      // if no boxes were checked, then there is no value in the array
 103                      // yet we don't want to display default value in this case
 104                      if ($caller->isSubmitted() && !$caller->is_new_repeat($this->getName())) {
 105                          $value = $this->_findValue($caller->_submitValues);
 106                      } else {
 107  
 108                          $value = $this->_findValue($caller->_defaultValues);
 109                      }
 110                  }
 111                  //fix here. setChecked should not be conditional
 112                  $this->setChecked($value);
 113                  break;
 114              default:
 115                  parent::onQuickFormEvent($event, $arg, $caller);
 116          }
 117          return true;
 118      }
 119  
 120      /**
 121       * Returns HTML for checbox form element.
 122       *
 123       * @return string
 124       */
 125      function toHtml()
 126      {
 127          return '<span>' . parent::toHtml() . '</span>';
 128      }
 129  
 130      /**
 131       * Returns the disabled field. Accessibility: the return "[ ]" from parent
 132       * class is not acceptable for screenreader users, and we DO want a label.
 133       *
 134       * @return string
 135       */
 136      function getFrozenHtml()
 137      {
 138          //$this->_generateId();
 139          $output = '<input type="checkbox" disabled="disabled" id="'.$this->getAttribute('id').'" ';
 140          if ($this->getChecked()) {
 141              $output .= 'checked="checked" />'.$this->_getPersistantData();
 142          } else {
 143              $output .= '/>';
 144          }
 145          return $output;
 146      }
 147  
 148      public function export_for_template(renderer_base $output) {
 149          $context = $this->export_for_template_base($output);
 150          $context['frozenvalue'] = $this->getValue();
 151          return $context;
 152      }
 153  }