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.

Differences Between: [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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   * Text field 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 for text fields.
  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_text extends user_filter_type {
  34      /** @var string */
  35      public $_field;
  36  
  37      /**
  38       * Constructor
  39       * @param string $name the name of the filter instance
  40       * @param string $label the label of the filter instance
  41       * @param boolean $advanced advanced form element flag
  42       * @param string $field user table filed name
  43       */
  44      public function __construct($name, $label, $advanced, $field) {
  45          parent::__construct($name, $label, $advanced);
  46          $this->_field = $field;
  47      }
  48  
  49      /**
  50       * Old syntax of class constructor. Deprecated in PHP7.
  51       *
  52       * @deprecated since Moodle 3.1
  53       */
  54      public function user_filter_text($name, $label, $advanced, $field) {
  55          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  56          self::__construct($name, $label, $advanced, $field);
  57      }
  58  
  59      /**
  60       * Returns an array of comparison operators
  61       * @return array of comparison operators
  62       */
  63      public function getOperators() {
  64          return array(0 => get_string('contains', 'filters'),
  65                       1 => get_string('doesnotcontain', 'filters'),
  66                       2 => get_string('isequalto', 'filters'),
  67                       3 => get_string('startswith', 'filters'),
  68                       4 => get_string('endswith', 'filters'),
  69                       5 => get_string('isempty', 'filters'));
  70      }
  71  
  72      /**
  73       * Adds controls specific to this filter in the form.
  74       * @param object $mform a MoodleForm object to setup
  75       */
  76      public function setupForm(&$mform) {
  77          $objs = array();
  78          $objs['select'] = $mform->createElement('select', $this->_name.'_op', null, $this->getOperators());
  79          $objs['text'] = $mform->createElement('text', $this->_name, null);
  80          $objs['select']->setLabel(get_string('limiterfor', 'filters', $this->_label));
  81          $objs['text']->setLabel(get_string('valuefor', 'filters', $this->_label));
  82          $grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false);
  83          $mform->setType($this->_name, PARAM_RAW);
  84          $mform->disabledIf($this->_name, $this->_name.'_op', 'eq', 5);
  85          if ($this->_advanced) {
  86              $mform->setAdvanced($this->_name.'_grp');
  87          }
  88      }
  89  
  90      /**
  91       * Retrieves data from the form data
  92       * @param object $formdata data submited with the form
  93       * @return mixed array filter data or false when filter not set
  94       */
  95      public function check_data($formdata) {
  96          $field    = $this->_name;
  97          $operator = $field.'_op';
  98  
  99          if (property_exists($formdata, $operator)) {
 100              if ($formdata->$operator != 5 and $formdata->$field == '') {
 101                  // No data - no change except for empty filter.
 102                  return false;
 103              }
 104              // If field value is set then use it, else it's null.
 105              $fieldvalue = null;
 106              if (isset($formdata->$field)) {
 107                  $fieldvalue = $formdata->$field;
 108              }
 109              return array('operator' => (int)$formdata->$operator, 'value' => $fieldvalue);
 110          }
 111  
 112          return false;
 113      }
 114  
 115      /**
 116       * Returns the condition to be used with SQL where
 117       * @param array $data filter settings
 118       * @return array sql string and $params
 119       */
 120      public function get_sql_filter($data) {
 121          global $DB;
 122          static $counter = 0;
 123          $name = 'ex_text'.$counter++;
 124  
 125          $operator = $data['operator'];
 126          $value    = $data['value'];
 127          $field    = $this->_field;
 128  
 129          $params = array();
 130  
 131          if ($operator != 5 and $value === '') {
 132              return '';
 133          }
 134  
 135          switch($operator) {
 136              case 0: // Contains.
 137                  $res = $DB->sql_like($field, ":$name", false, false);
 138                  $params[$name] = "%$value%";
 139                  break;
 140              case 1: // Does not contain.
 141                  $res = $DB->sql_like($field, ":$name", false, false, true);
 142                  $params[$name] = "%$value%";
 143                  break;
 144              case 2: // Equal to.
 145                  $res = $DB->sql_like($field, ":$name", false, false);
 146                  $params[$name] = "$value";
 147                  break;
 148              case 3: // Starts with.
 149                  $res = $DB->sql_like($field, ":$name", false, false);
 150                  $params[$name] = "$value%";
 151                  break;
 152              case 4: // Ends with.
 153                  $res = $DB->sql_like($field, ":$name", false, false);
 154                  $params[$name] = "%$value";
 155                  break;
 156              case 5: // Empty.
 157                  $res = "$field = :$name";
 158                  $params[$name] = '';
 159                  break;
 160              default:
 161                  return '';
 162          }
 163          return array($res, $params);
 164      }
 165  
 166      /**
 167       * Returns a human friendly description of the filter used as label.
 168       * @param array $data filter settings
 169       * @return string active filter label
 170       */
 171      public function get_label($data) {
 172          $operator  = $data['operator'];
 173          $value     = $data['value'];
 174          $operators = $this->getOperators();
 175  
 176          $a = new stdClass();
 177          $a->label    = $this->_label;
 178          $a->value    = '"'.s($value).'"';
 179          $a->operator = $operators[$operator];
 180  
 181          switch ($operator) {
 182              case 0: // Contains.
 183              case 1: // Doesn't contain.
 184              case 2: // Equal to.
 185              case 3: // Starts with.
 186              case 4: // Ends with.
 187                  return get_string('textlabel', 'filters', $a);
 188              case 5: // Empty.
 189                  return get_string('textlabelnovalue', 'filters', $a);
 190          }
 191  
 192          return '';
 193      }
 194  }