[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/user/filters/ -> profilefield.php (source)

   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 user_filter_profilefield($name, $label, $advanced) {
  43          parent::user_filter_type($name, $label, $advanced);
  44      }
  45  
  46      /**
  47       * Returns an array of comparison operators
  48       * @return array of comparison operators
  49       */
  50      public function get_operators() {
  51          return array(0 => get_string('contains', 'filters'),
  52                       1 => get_string('doesnotcontain', 'filters'),
  53                       2 => get_string('isequalto', 'filters'),
  54                       3 => get_string('startswith', 'filters'),
  55                       4 => get_string('endswith', 'filters'),
  56                       5 => get_string('isempty', 'filters'),
  57                       6 => get_string('isnotdefined', 'filters'),
  58                       7 => get_string('isdefined', 'filters'));
  59      }
  60  
  61      /**
  62       * Returns an array of custom profile fields
  63       * @return array of profile fields
  64       */
  65      public function get_profile_fields() {
  66          global $DB;
  67          if (!$fields = $DB->get_records('user_info_field', null, 'shortname', 'id,shortname')) {
  68              return null;
  69          }
  70          $res = array(0 => get_string('anyfield', 'filters'));
  71          foreach ($fields as $k => $v) {
  72              $res[$k] = $v->shortname;
  73          }
  74          return $res;
  75      }
  76  
  77      /**
  78       * Adds controls specific to this filter in the form.
  79       * @param object $mform a MoodleForm object to setup
  80       */
  81      public function setupForm(&$mform) {
  82          $profilefields = $this->get_profile_fields();
  83          if (empty($profilefields)) {
  84              return;
  85          }
  86          $objs = array();
  87          $objs[] = $mform->createElement('select', $this->_name.'_fld', null, $profilefields);
  88          $objs[] = $mform->createElement('select', $this->_name.'_op', null, $this->get_operators());
  89          $objs[] = $mform->createElement('text', $this->_name, null);
  90          $grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false);
  91          $mform->setType($this->_name, PARAM_RAW);
  92          if ($this->_advanced) {
  93              $mform->setAdvanced($this->_name.'_grp');
  94          }
  95      }
  96  
  97      /**
  98       * Retrieves data from the form data
  99       * @param object $formdata data submited with the form
 100       * @return mixed array filter data or false when filter not set
 101       */
 102      public function check_data($formdata) {
 103          $profilefields = $this->get_profile_fields();
 104  
 105          if (empty($profilefields)) {
 106              return false;
 107          }
 108  
 109          $field    = $this->_name;
 110          $operator = $field.'_op';
 111          $profile  = $field.'_fld';
 112  
 113          if (array_key_exists($profile, $formdata)) {
 114              if ($formdata->$operator < 5 and $formdata->$field === '') {
 115                  return false;
 116              }
 117  
 118              return array('value'    => (string)$formdata->$field,
 119                           'operator' => (int)$formdata->$operator,
 120                           'profile'  => (int)$formdata->$profile);
 121          }
 122      }
 123  
 124      /**
 125       * Returns the condition to be used with SQL where
 126       * @param array $data filter settings
 127       * @return array sql string and $params
 128       */
 129      public function get_sql_filter($data) {
 130          global $CFG, $DB;
 131          static $counter = 0;
 132          $name = 'ex_profilefield'.$counter++;
 133  
 134          $profilefields = $this->get_profile_fields();
 135          if (empty($profilefields)) {
 136              return '';
 137          }
 138  
 139          $profile  = $data['profile'];
 140          $operator = $data['operator'];
 141          $value    = $data['value'];
 142  
 143          $params = array();
 144          if (!array_key_exists($profile, $profilefields)) {
 145              return array('', array());
 146          }
 147  
 148          $where = "";
 149          $op = " IN ";
 150  
 151          if ($operator < 5 and $value === '') {
 152              return '';
 153          }
 154  
 155          switch($operator) {
 156              case 0: // Contains.
 157                  $where = $DB->sql_like('data', ":$name", false, false);
 158                  $params[$name] = "%$value%";
 159                  break;
 160              case 1: // Does not contain.
 161                  $where = $DB->sql_like('data', ":$name", false, false, true);
 162                  $params[$name] = "%$value%";
 163                  break;
 164              case 2: // Equal to.
 165                  $where = $DB->sql_like('data', ":$name", false, false);
 166                  $params[$name] = "$value";
 167                  break;
 168              case 3: // Starts with.
 169                  $where = $DB->sql_like('data', ":$name", false, false);
 170                  $params[$name] = "$value%";
 171                  break;
 172              case 4: // Ends with.
 173                  $where = $DB->sql_like('data', ":$name", false, false);
 174                  $params[$name] = "%$value";
 175                  break;
 176              case 5: // Empty.
 177                  $where = "data = :$name";
 178                  $params[$name] = "";
 179                  break;
 180              case 6: // Is not defined.
 181                  $op = " NOT IN ";
 182                  break;
 183              case 7: // Is defined.
 184                  break;
 185          }
 186          if ($profile) {
 187              if ($where !== '') {
 188                  $where = " AND $where";
 189              }
 190              $where = "fieldid=$profile $where";
 191          }
 192          if ($where !== '') {
 193              $where = "WHERE $where";
 194          }
 195          return array("id $op (SELECT userid FROM {user_info_data} $where)", $params);
 196      }
 197  
 198      /**
 199       * Returns a human friendly description of the filter used as label.
 200       * @param array $data filter settings
 201       * @return string active filter label
 202       */
 203      public function get_label($data) {
 204          $operators      = $this->get_operators();
 205          $profilefields = $this->get_profile_fields();
 206  
 207          if (empty($profilefields)) {
 208              return '';
 209          }
 210  
 211          $profile  = $data['profile'];
 212          $operator = $data['operator'];
 213          $value    = $data['value'];
 214  
 215          if (!array_key_exists($profile, $profilefields)) {
 216              return '';
 217          }
 218  
 219          $a = new stdClass();
 220          $a->label    = $this->_label;
 221          $a->value    = $value;
 222          $a->profile  = $profilefields[$profile];
 223          $a->operator = $operators[$operator];
 224  
 225          switch($operator) {
 226              case 0: // Contains.
 227              case 1: // Doesn't contain.
 228              case 2: // Equal to.
 229              case 3: // Starts with.
 230              case 4: // Ends with.
 231                  return get_string('profilelabel', 'filters', $a);
 232              case 5: // Empty.
 233              case 6: // Is not defined.
 234              case 7: // Is defined.
 235                  return get_string('profilelabelnovalue', 'filters', $a);
 236          }
 237          return '';
 238      }
 239  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1