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.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

   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   * Profile 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   * User filter based on values of custom profile fields.
  30   *
  31   * @copyright 1999 Martin Dougiamas  http://dougiamas.com
  32   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class user_filter_profilefield extends user_filter_type {
  35  
  36      /**
  37       * Constructor
  38       * @param string $name the name of the filter instance
  39       * @param string $label the label of the filter instance
  40       * @param boolean $advanced advanced form element flag
  41       */
  42      public function __construct($name, $label, $advanced) {
  43          parent::__construct($name, $label, $advanced);
  44      }
  45  
  46      /**
  47       * Old syntax of class constructor. Deprecated in PHP7.
  48       *
  49       * @deprecated since Moodle 3.1
  50       */
  51      public function user_filter_profilefield($name, $label, $advanced) {
  52          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  53          self::__construct($name, $label, $advanced);
  54      }
  55  
  56      /**
  57       * Returns an array of comparison operators
  58       * @return array of comparison operators
  59       */
  60      public function get_operators() {
  61          return array(0 => get_string('contains', 'filters'),
  62                       1 => get_string('doesnotcontain', 'filters'),
  63                       2 => get_string('isequalto', 'filters'),
  64                       3 => get_string('startswith', 'filters'),
  65                       4 => get_string('endswith', 'filters'),
  66                       5 => get_string('isempty', 'filters'),
  67                       6 => get_string('isnotdefined', 'filters'),
  68                       7 => get_string('isdefined', 'filters'));
  69      }
  70  
  71      /**
  72       * Returns an array of custom profile fields
  73       * @return array of profile fields
  74       */
  75      public function get_profile_fields() {
  76          global $CFG;
  77          require_once($CFG->dirroot . '/user/profile/lib.php');
  78  
  79          $fieldrecords = profile_get_custom_fields();
  80          foreach ($fieldrecords as $key => $fieldrecord) {
  81              $fieldrecords[$key]->name = format_string($fieldrecords[$key]->name, false, ['context' => context_system::instance()]);
  82          }
  83          $fields = array_combine(array_keys($fieldrecords), array_column($fieldrecords, 'name'));
  84          core_collator::asort($fields);
  85          $res = array(0 => get_string('anyfield', 'filters'));
  86  
  87          return $res + $fields;
  88      }
  89  
  90      /**
  91       * Adds controls specific to this filter in the form.
  92       * @param object $mform a MoodleForm object to setup
  93       */
  94      public function setupForm(&$mform) {
  95          $profilefields = $this->get_profile_fields();
  96          if (empty($profilefields)) {
  97              return;
  98          }
  99          $objs = array();
 100          $objs['field'] = $mform->createElement('select', $this->_name.'_fld', null, $profilefields);
 101          $objs['op'] = $mform->createElement('select', $this->_name.'_op', null, $this->get_operators());
 102          $objs['value'] = $mform->createElement('text', $this->_name, null);
 103          $objs['field']->setLabel(get_string('profilefilterfield', 'filters'));
 104          $objs['op']->setLabel(get_string('profilefilterlimiter', 'filters'));
 105          $objs['value']->setLabel(get_string('valuefor', 'filters', $this->_label));
 106          $grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false);
 107          $mform->setType($this->_name, PARAM_RAW);
 108          if ($this->_advanced) {
 109              $mform->setAdvanced($this->_name.'_grp');
 110          }
 111      }
 112  
 113      /**
 114       * Retrieves data from the form data
 115       * @param object $formdata data submited with the form
 116       * @return mixed array filter data or false when filter not set
 117       */
 118      public function check_data($formdata) {
 119          $profilefields = $this->get_profile_fields();
 120  
 121          if (empty($profilefields)) {
 122              return false;
 123          }
 124  
 125          $field    = $this->_name;
 126          $operator = $field.'_op';
 127          $profile  = $field.'_fld';
 128  
 129          if (property_exists($formdata, $profile)) {
 130              if ($formdata->$operator < 5 and $formdata->$field === '') {
 131                  return false;
 132              }
 133  
 134              return array('value'    => (string)$formdata->$field,
 135                           'operator' => (int)$formdata->$operator,
 136                           'profile'  => (int)$formdata->$profile);
 137          }
 138      }
 139  
 140      /**
 141       * Returns the condition to be used with SQL where
 142       * @param array $data filter settings
 143       * @return array sql string and $params
 144       */
 145      public function get_sql_filter($data) {
 146          global $CFG, $DB;
 147          static $counter = 0;
 148          $name = 'ex_profilefield'.$counter++;
 149  
 150          $profilefields = $this->get_profile_fields();
 151          if (empty($profilefields)) {
 152              return '';
 153          }
 154  
 155          $profile  = $data['profile'];
 156          $operator = $data['operator'];
 157          $value    = $data['value'];
 158  
 159          $params = array();
 160          if (!array_key_exists($profile, $profilefields)) {
 161              return array('', array());
 162          }
 163  
 164          $where = "";
 165          $op = " IN ";
 166  
 167          if ($operator < 5 and $value === '') {
 168              return '';
 169          }
 170  
 171          switch($operator) {
 172              case 0: // Contains.
 173                  $where = $DB->sql_like('data', ":$name", false, false);
 174                  $params[$name] = "%$value%";
 175                  break;
 176              case 1: // Does not contain.
 177                  $where = $DB->sql_like('data', ":$name", false, false, true);
 178                  $params[$name] = "%$value%";
 179                  break;
 180              case 2: // Equal to.
 181                  $where = $DB->sql_like('data', ":$name", false, false);
 182                  $params[$name] = "$value";
 183                  break;
 184              case 3: // Starts with.
 185                  $where = $DB->sql_like('data', ":$name", false, false);
 186                  $params[$name] = "$value%";
 187                  break;
 188              case 4: // Ends with.
 189                  $where = $DB->sql_like('data', ":$name", false, false);
 190                  $params[$name] = "%$value";
 191                  break;
 192              case 5: // Empty.
 193                  $where = "data = :$name";
 194                  $params[$name] = "";
 195                  break;
 196              case 6: // Is not defined.
 197                  $op = " NOT IN ";
 198                  break;
 199              case 7: // Is defined.
 200                  break;
 201          }
 202          if ($profile) {
 203              if ($where !== '') {
 204                  $where = " AND $where";
 205              }
 206              $where = "fieldid=$profile $where";
 207          }
 208          if ($where !== '') {
 209              $where = "WHERE $where";
 210          }
 211          return array("id $op (SELECT userid FROM {user_info_data} $where)", $params);
 212      }
 213  
 214      /**
 215       * Returns a human friendly description of the filter used as label.
 216       * @param array $data filter settings
 217       * @return string active filter label
 218       */
 219      public function get_label($data) {
 220          $operators      = $this->get_operators();
 221          $profilefields = $this->get_profile_fields();
 222  
 223          if (empty($profilefields)) {
 224              return '';
 225          }
 226  
 227          $profile  = $data['profile'];
 228          $operator = $data['operator'];
 229          $value    = $data['value'];
 230  
 231          if (!array_key_exists($profile, $profilefields)) {
 232              return '';
 233          }
 234  
 235          $a = new stdClass();
 236          $a->label    = $this->_label;
 237          $a->value    = $value;
 238          $a->profile  = $profilefields[$profile];
 239          $a->operator = $operators[$operator];
 240  
 241          switch($operator) {
 242              case 0: // Contains.
 243              case 1: // Doesn't contain.
 244              case 2: // Equal to.
 245              case 3: // Starts with.
 246              case 4: // Ends with.
 247                  return get_string('profilelabel', 'filters', $a);
 248              case 5: // Empty.
 249              case 6: // Is not defined.
 250              case 7: // Is defined.
 251                  return get_string('profilelabelnovalue', 'filters', $a);
 252          }
 253          return '';
 254      }
 255  }