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  /* vim: set expandtab tabstop=4 shiftwidth=4: */
   3  // +----------------------------------------------------------------------+
   4  // | PHP version 4.0                                                      |
   5  // +----------------------------------------------------------------------+
   6  // | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group             |
   7  // +----------------------------------------------------------------------+
   8  // | This source file is subject to version 2.0 of the PHP license,       |
   9  // | that is bundled with this package in the file LICENSE, and is        |
  10  // | available at through the world-wide-web at                           |
  11  // | http://www.php.net/license/2_02.txt.                                 |
  12  // | If you did not receive a copy of the PHP license and are unable to   |
  13  // | obtain it through the world-wide-web, please send a note to          |
  14  // | license@php.net so we can mail you a copy immediately.               |
  15  // +----------------------------------------------------------------------+
  16  // | Authors: Adam Daniel <adaniel1@eesus.jnj.com>                        |
  17  // |          Bertrand Mansion <bmansion@mamasam.com>                     |
  18  // +----------------------------------------------------------------------+
  19  //
  20  // $Id$
  21  
  22  require_once('HTML/QuickForm/checkbox.php');
  23  
  24  /**
  25   * HTML class for an advanced checkbox type field
  26   *
  27   * Basically this fixes a problem that HTML has had
  28   * where checkboxes can only pass a single value (the
  29   * value of the checkbox when checked).  A value for when
  30   * the checkbox is not checked cannot be passed, and
  31   * furthermore the checkbox variable doesn't even exist if
  32   * the checkbox was submitted unchecked.
  33   *
  34   * It works by prepending a hidden field with the same name and
  35   * another "unchecked" value to the checbox. If the checkbox is
  36   * checked, PHP overwrites the value of the hidden field with
  37   * its value.
  38   *
  39   * @author       Jason Rust <jrust@php.net>
  40   * @since        2.0
  41   * @access       public
  42   */
  43  class HTML_QuickForm_advcheckbox extends HTML_QuickForm_checkbox
  44  {
  45      // {{{ properties
  46  
  47      /**
  48       * The values passed by the hidden elment
  49       *
  50       * @var array
  51       * @access private
  52       */
  53      var $_values = null;
  54  
  55      /**
  56       * The default value
  57       *
  58       * @var boolean
  59       * @access private
  60       */
  61      var $_currentValue = null;
  62  
  63      // }}}
  64      // {{{ constructor
  65  
  66      /**
  67       * Class constructor
  68       *
  69       * @param     string    $elementName    (optional)Input field name attribute
  70       * @param     string    $elementLabel   (optional)Input field label
  71       * @param     string    $text           (optional)Text to put after the checkbox
  72       * @param     mixed     $attributes     (optional)Either a typical HTML attribute string
  73       *                                      or an associative array
  74       * @param     mixed     $values         (optional)Values to pass if checked or not checked
  75       *
  76       * @since     1.0
  77       * @access    public
  78       * @return    void
  79       */
  80      public function __construct($elementName=null, $elementLabel=null, $text=null, $attributes=null, $values=null) {
  81          parent::__construct($elementName, $elementLabel, $text, $attributes);
  82          $this->setValues($values);
  83      } //end constructor
  84  
  85      /**
  86       * Old syntax of class constructor. Deprecated in PHP7.
  87       *
  88       * @deprecated since Moodle 3.1
  89       */
  90      public function HTML_QuickForm_advcheckbox($elementName=null, $elementLabel=null, $text=null, $attributes=null, $values=null) {
  91          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  92          self::__construct($elementName, $elementLabel, $text, $attributes, $values);
  93      }
  94  
  95      // }}}
  96      // {{{ getPrivateName()
  97  
  98      /**
  99       * Gets the private name for the element
 100       *
 101       * @param   string  $elementName The element name to make private
 102       *
 103       * @access public
 104       * @return string
 105       *
 106       * @deprecated          Deprecated since 3.2.6, both generated elements have the same name
 107       */
 108      function getPrivateName($elementName)
 109      {
 110          return '__'.$elementName;
 111      }
 112  
 113      // }}}
 114      // {{{ getOnclickJs()
 115  
 116      /**
 117       * Create the javascript for the onclick event which will
 118       * set the value of the hidden field
 119       *
 120       * @param     string    $elementName    The element name
 121       *
 122       * @access public
 123       * @return string
 124       *
 125       * @deprecated          Deprecated since 3.2.6, this element no longer uses any javascript
 126       */
 127      function getOnclickJs($elementName)
 128      {
 129          $onclickJs = 'if (this.checked) { this.form[\''.$elementName.'\'].value=\''.addcslashes($this->_values[1], '\'').'\'; }';
 130          $onclickJs .= 'else { this.form[\''.$elementName.'\'].value=\''.addcslashes($this->_values[0], '\'').'\'; }';
 131          return $onclickJs;
 132      }
 133  
 134      // }}}
 135      // {{{ setValues()
 136  
 137      /**
 138       * Sets the values used by the hidden element
 139       *
 140       * @param   mixed   $values The values, either a string or an array
 141       *
 142       * @access public
 143       * @return void
 144       */
 145      function setValues($values)
 146      {
 147          if (empty($values)) {
 148              // give it default checkbox behavior
 149              $this->_values = array('', 1);
 150          } elseif (is_scalar($values)) {
 151              // if it's string, then assume the value to
 152              // be passed is for when the element is checked
 153              $this->_values = array('', $values);
 154          } else {
 155              $this->_values = $values;
 156          }
 157          $this->updateAttributes(array('value' => $this->_values[1]));
 158          $this->setChecked($this->_currentValue == $this->_values[1]);
 159      }
 160  
 161      // }}}
 162      // {{{ setValue()
 163  
 164     /**
 165      * Sets the element's value
 166      *
 167      * @param    mixed   Element's value
 168      * @access   public
 169      */
 170      function setValue($value)
 171      {
 172          $this->setChecked(isset($this->_values[1]) && $value == $this->_values[1]);
 173          $this->_currentValue = $value;
 174      }
 175  
 176      // }}}
 177      // {{{ getValue()
 178  
 179     /**
 180      * Returns the element's value
 181      *
 182      * @access   public
 183      * @return   mixed
 184      */
 185      function getValue()
 186      {
 187          if (is_array($this->_values)) {
 188              return $this->_values[$this->getChecked()? 1: 0];
 189          } else {
 190              return null;
 191          }
 192      }
 193  
 194      // }}}
 195      // {{{ toHtml()
 196  
 197      /**
 198       * Returns the checkbox element in HTML
 199       * and the additional hidden element in HTML
 200       *
 201       * @access    public
 202       * @return    string
 203       */
 204      function toHtml()
 205      {
 206          if ($this->_flagFrozen) {
 207              return parent::toHtml();
 208          } else {
 209              return '<input' . $this->_getAttrString(array(
 210                          'type'  => 'hidden',
 211                          'name'  => $this->getName(),
 212                          'value' => $this->_values[0]
 213                     )) . ' />' . parent::toHtml();
 214  
 215          }
 216      } //end func toHtml
 217  
 218      // }}}
 219      // {{{ getFrozenHtml()
 220  
 221     /**
 222      * Unlike checkbox, this has to append a hidden input in both
 223      * checked and non-checked states
 224      */
 225      function getFrozenHtml()
 226      {
 227          return ($this->getChecked()? '<tt>[x]</tt>': '<tt>[ ]</tt>') .
 228                 $this->_getPersistantData();
 229      }
 230  
 231      // }}}
 232      // {{{ onQuickFormEvent()
 233  
 234      /**
 235       * Called by HTML_QuickForm whenever form event is made on this element
 236       *
 237       * @param     string    $event  Name of event
 238       * @param     mixed     $arg    event arguments
 239       * @param     object    $caller calling object
 240       * @since     1.0
 241       * @access    public
 242       * @return    void
 243       */
 244      function onQuickFormEvent($event, $arg, &$caller)
 245      {
 246          switch ($event) {
 247              case 'updateValue':
 248                  // constant values override both default and submitted ones
 249                  // default values are overriden by submitted
 250                  $value = $this->_findValue($caller->_constantValues);
 251                  if (null === $value) {
 252                      $value = $this->_findValue($caller->_submitValues);
 253                      if (null === $value) {
 254                          $value = $this->_findValue($caller->_defaultValues);
 255                      }
 256                  }
 257                  if (null !== $value) {
 258                      $this->setValue($value);
 259                  }
 260                  break;
 261              default:
 262                  parent::onQuickFormEvent($event, $arg, $caller);
 263          }
 264          return true;
 265      } // end func onQuickFormLoad
 266  
 267      // }}}
 268      // {{{ exportValue()
 269  
 270     /**
 271      * This element has a value even if it is not checked, thus we override
 272      * checkbox's behaviour here
 273      */
 274      function exportValue(&$submitValues, $assoc = false)
 275      {
 276          $value = $this->_findValue($submitValues);
 277          if (null === $value) {
 278              $value = $this->getValue();
 279          } elseif (is_array($this->_values) && ($value != $this->_values[0]) && ($value != $this->_values[1])) {
 280              $value = null;
 281          }
 282          return $this->_prepareValue($value, $assoc);
 283      }
 284      // }}}
 285  } //end class HTML_QuickForm_advcheckbox
 286  ?>