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   * Simple 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_simpleselect extends user_filter_type {
  34      /**
  35       * options for the list values
  36       * @var array
  37       */
  38      public $_options;
  39  
  40      /**
  41       * @var string
  42       */
  43      public $_field;
  44  
  45      /**
  46       * Constructor
  47       * @param string $name the name of the filter instance
  48       * @param string $label the label of the filter instance
  49       * @param boolean $advanced advanced form element flag
  50       * @param string $field user table filed name
  51       * @param array $options select options
  52       */
  53      public function __construct($name, $label, $advanced, $field, $options) {
  54          parent::__construct($name, $label, $advanced);
  55          $this->_field   = $field;
  56          $this->_options = $options;
  57      }
  58  
  59      /**
  60       * Old syntax of class constructor. Deprecated in PHP7.
  61       *
  62       * @deprecated since Moodle 3.1
  63       */
  64      public function user_filter_simpleselect($name, $label, $advanced, $field, $options) {
  65          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  66          self::__construct($name, $label, $advanced, $field, $options);
  67      }
  68  
  69      /**
  70       * Adds controls specific to this filter in the form.
  71       * @param moodleform $mform a MoodleForm object to setup
  72       */
  73      public function setupForm(&$mform) {
  74          $choices = array('' => get_string('anyvalue', 'filters')) + $this->_options;
  75          $mform->addElement('select', $this->_name, $this->_label, $choices);
  76          if ($this->_advanced) {
  77              $mform->setAdvanced($this->_name);
  78          }
  79      }
  80  
  81      /**
  82       * Retrieves data from the form data
  83       * @param object $formdata data submited with the form
  84       * @return mixed array filter data or false when filter not set
  85       */
  86      public function check_data($formdata) {
  87          $field = $this->_name;
  88  
  89          if (property_exists($formdata, $field) and $formdata->$field !== '') {
  90              return array('value' => (string)$formdata->$field);
  91          }
  92  
  93          return false;
  94      }
  95  
  96      /**
  97       * Returns the condition to be used with SQL where
  98       * @param array $data filter settings
  99       * @return array sql string and $params
 100       */
 101      public function get_sql_filter($data) {
 102          static $counter = 0;
 103          $name = 'ex_simpleselect'.$counter++;
 104  
 105          $value = $data['value'];
 106          $params = array();
 107          $field = $this->_field;
 108          if ($value == '') {
 109              return '';
 110          }
 111          return array("$field=:$name", array($name => $value));
 112      }
 113  
 114      /**
 115       * Returns a human friendly description of the filter used as label.
 116       * @param array $data filter settings
 117       * @return string active filter label
 118       */
 119      public function get_label($data) {
 120          $value = $data['value'];
 121  
 122          $a = new stdClass();
 123          $a->label    = $this->_label;
 124          $a->value    = '"'.s($this->_options[$value]).'"';
 125          $a->operator = get_string('isequalto', 'filters');
 126  
 127          return get_string('selectlabel', 'filters', $a);
 128      }
 129  }
 130