Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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   * Value select filter.
  19   *
  20   * @package   core_user
  21   * @category  user
  22   * @copyright 1999 Martin Dougiamas  http://dougiamas.com
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  require_once($CFG->dirroot.'/user/filters/lib.php');
  27  
  28  /**
  29   * Generic filter based on a list of values.
  30   * @copyright 1999 Martin Dougiamas  http://dougiamas.com
  31   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class user_filter_select extends user_filter_type {
  34      /**
  35       * options for the list values
  36       * @var array
  37       */
  38      public $_options;
  39  
  40      /** @var string */
  41      public $_field;
  42  
  43      /** @var mixed|null */
  44      public $_default;
  45  
  46      /**
  47       * Constructor
  48       * @param string $name the name of the filter instance
  49       * @param string $label the label of the filter instance
  50       * @param boolean $advanced advanced form element flag
  51       * @param string $field user table filed name
  52       * @param array $options select options
  53       * @param mixed $default option
  54       */
  55      public function __construct($name, $label, $advanced, $field, $options, $default=null) {
  56          parent::__construct($name, $label, $advanced);
  57          $this->_field   = $field;
  58          $this->_options = $options;
  59          $this->_default = $default;
  60      }
  61  
  62      /**
  63       * Old syntax of class constructor. Deprecated in PHP7.
  64       *
  65       * @deprecated since Moodle 3.1
  66       */
  67      public function user_filter_select($name, $label, $advanced, $field, $options, $default=null) {
  68          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  69          self::__construct($name, $label, $advanced, $field, $options, $default=null);
  70      }
  71  
  72      /**
  73       * Returns an array of comparison operators
  74       * @return array of comparison operators
  75       */
  76      public function get_operators() {
  77          return array(0 => get_string('isanyvalue', 'filters'),
  78                       1 => get_string('isequalto', 'filters'),
  79                       2 => get_string('isnotequalto', 'filters'));
  80      }
  81  
  82      /**
  83       * Adds controls specific to this filter in the form.
  84       * @param moodleform $mform a MoodleForm object to setup
  85       */
  86      public function setupForm(&$mform) {
  87          $objs = array();
  88          $objs['limiter'] = $mform->createElement('select', $this->_name.'_op', null, $this->get_operators());
  89          $objs['limiter']->setLabel(get_string('limiterfor', 'filters', $this->_label));
  90          $objs['country'] = $mform->createElement('select', $this->_name, null, $this->_options);
  91          $objs['country']->setLabel(get_string('valuefor', 'filters', $this->_label));
  92          $grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false);
  93          $mform->disabledIf($this->_name, $this->_name.'_op', 'eq', 0);
  94          if (!is_null($this->_default)) {
  95              $mform->setDefault($this->_name, $this->_default);
  96          }
  97          if ($this->_advanced) {
  98              $mform->setAdvanced($this->_name.'_grp');
  99          }
 100      }
 101  
 102      /**
 103       * Retrieves data from the form data
 104       * @param stdClass $formdata data submited with the form
 105       * @return mixed array filter data or false when filter not set
 106       */
 107      public function check_data($formdata) {
 108          $field    = $this->_name;
 109          $operator = $field.'_op';
 110  
 111          if (property_exists($formdata, $field) and !empty($formdata->$operator)) {
 112              return array('operator' => (int)$formdata->$operator,
 113                           'value'    => (string)$formdata->$field);
 114          }
 115  
 116          return false;
 117      }
 118  
 119      /**
 120       * Returns the condition to be used with SQL where
 121       * @param array $data filter settings
 122       * @return array sql string and $params
 123       */
 124      public function get_sql_filter($data) {
 125          static $counter = 0;
 126          $name = 'ex_select'.$counter++;
 127  
 128          $operator = $data['operator'];
 129          $value    = $data['value'];
 130          $field    = $this->_field;
 131  
 132          $params = array();
 133  
 134          switch($operator) {
 135              case 1: // Equal to.
 136                  $res = "=:$name";
 137                  $params[$name] = $value;
 138                  break;
 139              case 2: // Not equal to.
 140                  $res = "<>:$name";
 141                  $params[$name] = $value;
 142                   break;
 143              default:
 144                  return array('', array());
 145          }
 146          return array($field.$res, $params);
 147      }
 148  
 149      /**
 150       * Returns a human friendly description of the filter used as label.
 151       * @param array $data filter settings
 152       * @return string active filter label
 153       */
 154      public function get_label($data) {
 155          $operators = $this->get_operators();
 156          $operator  = $data['operator'];
 157          $value     = $data['value'];
 158  
 159          if (empty($operator)) {
 160              return '';
 161          }
 162  
 163          $a = new stdClass();
 164          $a->label    = $this->_label;
 165          $a->value    = '"'.s($this->_options[$value]).'"';
 166          $a->operator = $operators[$operator];
 167  
 168          return get_string('selectlabel', 'filters', $a);
 169      }
 170  }
 171