Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.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/input.php');
  23  
  24  /**
  25   * HTML class for a radio type element
  26   * 
  27   * @author       Adam Daniel <adaniel1@eesus.jnj.com>
  28   * @author       Bertrand Mansion <bmansion@mamasam.com>
  29   * @version      1.1
  30   * @since        PHP4.04pl1
  31   * @access       public
  32   */
  33  class HTML_QuickForm_radio extends HTML_QuickForm_input
  34  {
  35      // {{{ properties
  36  
  37      /**
  38       * Radio display text
  39       * @var       string
  40       * @since     1.1
  41       * @access    private
  42       */
  43      var $_text = '';
  44  
  45      // }}}
  46      // {{{ constructor
  47  
  48      /**
  49       * Class constructor
  50       * 
  51       * @param     string    Input field name attribute
  52       * @param     mixed     Label(s) for a field
  53       * @param     string    Text to display near the radio
  54       * @param     string    Input field value
  55       * @param     mixed     Either a typical HTML attribute string or an associative array
  56       * @since     1.0
  57       * @access    public
  58       * @return    void
  59       */
  60      public function __construct($elementName=null, $elementLabel=null, $text=null, $value=null, $attributes=null) {
  61          // TODO MDL-52313 Replace with the call to parent::__construct().
  62          HTML_QuickForm_element::__construct($elementName, $elementLabel, $attributes);
  63          if (isset($value)) {
  64              $this->setValue($value);
  65          }
  66          $this->_persistantFreeze = true;
  67          $this->setType('radio');
  68          $this->_text = $text;
  69      } //end constructor
  70  
  71      /**
  72       * Old syntax of class constructor. Deprecated in PHP7.
  73       *
  74       * @deprecated since Moodle 3.1
  75       */
  76      public function HTML_QuickForm_radio($elementName=null, $elementLabel=null, $text=null, $value=null, $attributes=null) {
  77          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  78          self::__construct($elementName, $elementLabel, $text, $value, $attributes);
  79      }
  80  
  81      // }}}
  82  
  83      function _generateId() {
  84          // Override the standard implementation, since you can have multiple
  85          // check-boxes with the same name on a form. Therefore, add the
  86          // (cleaned up) value to the id.
  87  
  88          if ($this->getAttribute('id')) {
  89              return;
  90          }
  91  
  92          parent::_generateId();
  93          $id = $this->getAttribute('id') . '_' . clean_param($this->getValue(), PARAM_ALPHANUMEXT);
  94          $this->updateAttributes(array('id' => $id));
  95      }
  96  
  97      // {{{ setChecked()
  98  
  99      /**
 100       * Sets whether radio button is checked
 101       * 
 102       * @param     bool    $checked  Whether the field is checked or not
 103       * @since     1.0
 104       * @access    public
 105       * @return    void
 106       */
 107      function setChecked($checked)
 108      {
 109          if (!$checked) {
 110              $this->removeAttribute('checked');
 111          } else {
 112              $this->updateAttributes(array('checked'=>'checked'));
 113          }
 114      } //end func setChecked
 115  
 116      // }}}
 117      // {{{ getChecked()
 118  
 119      /**
 120       * Returns whether radio button is checked
 121       * 
 122       * @since     1.0
 123       * @access    public
 124       * @return    string
 125       */
 126      function getChecked()
 127      {
 128          return $this->getAttribute('checked');
 129      } //end func getChecked
 130          
 131      // }}}
 132      // {{{ toHtml()
 133  
 134      /**
 135       * Returns the radio element in HTML
 136       * 
 137       * @since     1.0
 138       * @access    public
 139       * @return    string
 140       */
 141      function toHtml()
 142      {
 143          if (0 == strlen($this->_text)) {
 144              $label = '';
 145          } elseif ($this->_flagFrozen) {
 146              $label = $this->_text;
 147          } else {
 148              $label = '<label for="' . $this->getAttribute('id') . '">' . $this->_text . '</label>';
 149          }
 150          return HTML_QuickForm_input::toHtml() . $label;
 151      } //end func toHtml
 152      
 153      // }}}
 154      // {{{ getFrozenHtml()
 155  
 156      /**
 157       * Returns the value of field without HTML tags
 158       * 
 159       * @since     1.0
 160       * @access    public
 161       * @return    string
 162       */
 163      function getFrozenHtml()
 164      {
 165          if ($this->getChecked()) {
 166              return '<tt>(x)</tt>' .
 167                     $this->_getPersistantData();
 168          } else {
 169              return '<tt>( )</tt>';
 170          }
 171      } //end func getFrozenHtml
 172  
 173      // }}}
 174      // {{{ setText()
 175  
 176      /**
 177       * Sets the radio text
 178       * 
 179       * @param     string    $text  Text to display near the radio button
 180       * @since     1.1
 181       * @access    public
 182       * @return    void
 183       */
 184      function setText($text)
 185      {
 186          $this->_text = $text;
 187      } //end func setText
 188  
 189      // }}}
 190      // {{{ getText()
 191  
 192      /**
 193       * Returns the radio text 
 194       * 
 195       * @since     1.1
 196       * @access    public
 197       * @return    string
 198       */
 199      function getText()
 200      {
 201          return $this->_text;
 202      } //end func getText
 203  
 204      // }}}
 205      // {{{ onQuickFormEvent()
 206  
 207      /**
 208       * Called by HTML_QuickForm whenever form event is made on this element
 209       *
 210       * @param     string    $event  Name of event
 211       * @param     mixed     $arg    event arguments
 212       * @param     object    $caller calling object
 213       * @since     1.0
 214       * @access    public
 215       * @return    void
 216       */
 217      function onQuickFormEvent($event, $arg, &$caller)
 218      {
 219          switch ($event) {
 220              case 'updateValue':
 221                  // constant values override both default and submitted ones
 222                  // default values are overriden by submitted
 223                  $value = $this->_findValue($caller->_constantValues);
 224                  if (null === $value) {
 225                      $value = $this->_findValue($caller->_submitValues);
 226                      if (null === $value) {
 227                          $value = $this->_findValue($caller->_defaultValues);
 228                      }
 229                  }
 230                  if ($value == $this->getValue()) {
 231                      $this->setChecked(true);
 232                  } else {
 233                      $this->setChecked(false);
 234                  }
 235                  break;
 236              case 'setGroupValue':
 237                  if ($arg == $this->getValue()) {
 238                      $this->setChecked(true);
 239                  } else {
 240                      $this->setChecked(false);
 241                  }
 242                  break;
 243              default:
 244                  parent::onQuickFormEvent($event, $arg, $caller);
 245          }
 246          return true;
 247      } // end func onQuickFormLoad
 248  
 249      // }}}
 250      // {{{ exportValue()
 251  
 252     /**
 253      * Returns the value attribute if the radio is checked, null if it is not
 254      */
 255      function exportValue(&$submitValues, $assoc = false)
 256      {
 257          $value = $this->_findValue($submitValues);
 258          if (null === $value) {
 259              $value = $this->getChecked()? $this->getValue(): null;
 260          } elseif ($value != $this->getValue()) {
 261              $value = null;
 262          }
 263          return $this->_prepareValue($value, $assoc);
 264      }
 265      
 266      // }}}
 267  } //end class HTML_QuickForm_radio
 268  ?>