Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are 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   * Advanced checkbox type form element
  20   *
  21   * Contains HTML class for an advcheckbox type form 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/advcheckbox.php');
  29  require_once ('templatable_form_element.php');
  30  
  31  /**
  32   * HTML class for an advcheckbox type element
  33   *
  34   * Overloaded {@link HTML_QuickForm_advcheckbox} with default behavior modified for Moodle.
  35   * This will return '0' if not checked and '1' if checked.
  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_advcheckbox extends HTML_QuickForm_advcheckbox implements templatable {
  43  
  44      use templatable_form_element {
  45          export_for_template as export_for_template_base;
  46      }
  47  
  48      /** @var string html for help button, if empty then no help will icon will be dispalyed. */
  49      var $_helpbutton='';
  50  
  51      /** @var string Group to which this checkbox belongs (for select all/select none button) */
  52      var $_group;
  53  
  54      /**
  55       * constructor
  56       *
  57       * @param string $elementName (optional) name of the checkbox
  58       * @param string $elementLabel (optional) checkbox label
  59       * @param string $text (optional) Text to put after the checkbox
  60       * @param mixed $attributes (optional) Either a typical HTML attribute string
  61       *              or an associative array
  62       * @param mixed $values (optional) Values to pass if checked or not checked
  63       */
  64      public function __construct($elementName=null, $elementLabel=null, $text=null, $attributes=null, $values=null)
  65      {
  66          if ($values === null){
  67              $values = array(0, 1);
  68          }
  69  
  70          if (!empty($attributes['group'])) {
  71  
  72              $this->_group = 'checkboxgroup' . $attributes['group'];
  73              unset($attributes['group']);
  74              if (is_null($attributes)) {
  75                  $attributes = array();
  76                  $attributes['class'] .= " $this->_group";
  77              } elseif (is_array($attributes)) {
  78                  if (isset($attributes['class'])) {
  79                      $attributes['class'] .= " $this->_group";
  80                  } else {
  81                      $attributes['class'] = $this->_group;
  82                  }
  83              } elseif ($strpos = stripos($attributes, 'class="')) {
  84                  $attributes = str_ireplace('class="', 'class="' . $this->_group . ' ', $attributes);
  85              } else {
  86                  $attributes .= ' class="' . $this->_group . '"';
  87              }
  88          }
  89  
  90          parent::__construct($elementName, $elementLabel, $text, $attributes, $values);
  91          $this->_type = 'advcheckbox';
  92      }
  93  
  94      /**
  95       * Old syntax of class constructor. Deprecated in PHP7.
  96       *
  97       * @deprecated since Moodle 3.1
  98       */
  99      public function MoodleQuickForm_advcheckbox($elementName=null, $elementLabel=null, $text=null, $attributes=null, $values=null) {
 100          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
 101          self::__construct($elementName, $elementLabel, $text, $attributes, $values);
 102      }
 103  
 104      /**
 105       * get html for help button
 106       *
 107       * @return string html for help button
 108       */
 109      function getHelpButton(){
 110          return $this->_helpbutton;
 111      }
 112  
 113      /**
 114       * Returns HTML for advchecbox form element.
 115       *
 116       * @return string
 117       */
 118      function toHtml()
 119      {
 120          return '<span>' . parent::toHtml() . '</span>';
 121      }
 122  
 123      /**
 124       * Returns the disabled field. Accessibility: the return "[ ]" from parent
 125       * class is not acceptable for screenreader users, and we DO want a label.
 126       *
 127       * @return string
 128       */
 129      function getFrozenHtml()
 130      {
 131          //$this->_generateId();
 132          $output = '<input type="checkbox" disabled="disabled" id="'.$this->getAttribute('id').'" ';
 133          if ($this->getChecked()) {
 134              $output .= 'checked="checked" />'.$this->_getPersistantData();
 135          } else {
 136              $output .= '/>';
 137          }
 138          return $output;
 139      }
 140  
 141      public function export_for_template(renderer_base $output) {
 142          $context = $this->export_for_template_base($output);
 143  
 144          $context['selectedvalue'] = $this->_values[1];
 145          $context['deselectedvalue'] = $this->_values[0];
 146          $context['frozenvalue'] = $this->getValue();
 147          return $context;
 148      }
 149  
 150  }