[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1