Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is 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   * Generic checkbox filter.
  19   *
  20   * This will create generic filter with checkbox option and can be used for
  21   * disabling other elements for specific condition.
  22   *
  23   * @package   core_user
  24   * @category  user
  25   * @copyright 2011 Rajesh Taneja
  26   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  require_once($CFG->dirroot.'/user/filters/lib.php');
  32  
  33  /**
  34   * Generic filter based for checkbox and can be used for disabling items
  35   * @copyright 1999 Martin Dougiamas  http://dougiamas.com
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class user_filter_checkbox extends user_filter_type {
  39      /**
  40       * list of all the fields which needs to be disabled, if checkbox is checked
  41       * @var array
  42       */
  43      protected $disableelements = array();
  44  
  45      /**
  46       * name of user table field/fields on which data needs to be compared
  47       * @var mixed
  48       */
  49      protected $field;
  50  
  51      /**
  52       * Constructor, initalize user_filter_type and sets $disableelements array
  53       * with list of elements to be diabled by checkbox.
  54       *
  55       * @param string $name the name of the filter instance
  56       * @param string $label the label of the filter instance
  57       * @param boolean $advanced advanced form element flag
  58       * @param mixed $field user table field/fields name for comparison
  59       * @param array $disableelements name of fields which should be disabled if this checkbox is checked.
  60       */
  61      public function __construct($name, $label, $advanced, $field, $disableelements=null) {
  62          parent::__construct($name, $label, $advanced);
  63          $this->field   = $field;
  64          if (!empty($disableelements)) {
  65              if (!is_array($disableelements)) {
  66                  $this->disableelements = array($disableelements);
  67              } else {
  68                  $this->disableelements = $disableelements;
  69              }
  70          }
  71      }
  72  
  73      /**
  74       * Adds controls specific to this filter in the form.
  75       *
  76       * @param moodleform $mform a MoodleQuickForm object in which element will be added
  77       */
  78      public function setupForm(&$mform) {
  79          $mform->addElement('checkbox', $this->_name, $this->_label, '');
  80  
  81          if ($this->_advanced) {
  82              $mform->setAdvanced($this->_name);
  83          }
  84          // Check if disable if options are set. if yes then set rules.
  85          if (!empty($this->disableelements) && is_array($this->disableelements)) {
  86              foreach ($this->disableelements as $disableelement) {
  87                  $mform->disabledIf($disableelement, $this->_name, 'checked');
  88              }
  89          }
  90      }
  91  
  92      /**
  93       * Retrieves data from the form data
  94       *
  95       * @param object $formdata data submited with the form
  96       * @return mixed array filter data or false when filter not set
  97       */
  98      public function check_data($formdata) {
  99          $field = $this->_name;
 100          // Check if disable if options are set. if yes then don't add this..
 101          if (!empty($this->disableelements) && is_array($this->disableelements)) {
 102              foreach ($this->disableelements as $disableelement) {
 103                  if (property_exists($formdata, $disableelement)) {
 104                      return false;
 105                  }
 106              }
 107          }
 108          if (property_exists($formdata, $field) and $formdata->$field !== '') {
 109              return array('value' => (string)$formdata->$field);
 110          }
 111          return false;
 112      }
 113  
 114      /**
 115       * Returns the condition to be used with SQL where
 116       *
 117       * @param array $data filter settings
 118       * @return array sql string and $params
 119       */
 120      public function get_sql_filter($data) {
 121          $field  = $this->field;
 122          if (is_array($field)) {
 123              $res = " {$field[0]} = {$field[1]} ";
 124          } else {
 125              $res = " {$field} = 0 ";
 126          }
 127          return array($res, array());
 128      }
 129  
 130      /**
 131       * Returns a human friendly description of the filter used as label.
 132       *
 133       * @param array $data filter settings
 134       * @return string active filter label
 135       */
 136      public function get_label($data) {
 137          return $this->_label;
 138      }
 139  }