[ Index ]

PHP Cross Reference of moodle-2.8

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 <[email protected]>
  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 <[email protected]>
  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      function MoodleQuickForm_checkbox($elementName=null, $elementLabel=null, $text='', $attributes=null) {
  56          parent::HTML_QuickForm_checkbox($elementName, $elementLabel, $text, $attributes);
  57      }
  58  
  59      /**
  60       * get html for help button
  61       *
  62       * @return string html for help button
  63       */
  64      function getHelpButton(){
  65          return $this->_helpbutton;
  66      }
  67  
  68      /**
  69       * Called by HTML_QuickForm whenever form event is made on this element
  70       *
  71       * @param string $event Name of event
  72       * @param mixed $arg event arguments
  73       * @param object $caller calling object
  74       * @return bool
  75       */
  76      function onQuickFormEvent($event, $arg, &$caller)
  77      {
  78          //fixes bug in quickforms which lets previous set value override submitted value if checkbox is not checked
  79          // and no value is submitted
  80          switch ($event) {
  81              case 'updateValue':
  82                  // constant values override both default and submitted ones
  83                  // default values are overriden by submitted
  84                  $value = $this->_findValue($caller->_constantValues);
  85                  if (null === $value) {
  86                      // if no boxes were checked, then there is no value in the array
  87                      // yet we don't want to display default value in this case
  88                      if ($caller->isSubmitted()) {
  89                          $value = $this->_findValue($caller->_submitValues);
  90                      } else {
  91  
  92                          $value = $this->_findValue($caller->_defaultValues);
  93                      }
  94                  }
  95                  //fix here. setChecked should not be conditional
  96                  $this->setChecked($value);
  97                  break;
  98              default:
  99                  parent::onQuickFormEvent($event, $arg, $caller);
 100          }
 101          return true;
 102      }
 103  
 104      /**
 105       * Returns HTML for checbox form element.
 106       *
 107       * @return string
 108       */
 109      function toHtml()
 110      {
 111          return '<span>' . parent::toHtml() . '</span>';
 112      }
 113  
 114      /**
 115       * Returns the disabled field. Accessibility: the return "[ ]" from parent
 116       * class is not acceptable for screenreader users, and we DO want a label.
 117       *
 118       * @return string
 119       */
 120      function getFrozenHtml()
 121      {
 122          //$this->_generateId();
 123          $output = '<input type="checkbox" disabled="disabled" id="'.$this->getAttribute('id').'" ';
 124          if ($this->getChecked()) {
 125              $output .= 'checked="checked" />'.$this->_getPersistantData();
 126          } else {
 127              $output .= '/>';
 128          }
 129          return $output;
 130      }
 131  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1