[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/user/filters/ -> cohort.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   * Cohort filter.
  19   *
  20   * @package   core_user
  21   * @category  user
  22   * @copyright 2011 Petr Skoda
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once($CFG->dirroot.'/user/filters/lib.php');
  29  
  30  /**
  31   * Generic filter for cohort membership.
  32   * @copyright 1999 Martin Dougiamas  http://dougiamas.com
  33   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class user_filter_cohort extends user_filter_type {
  36      /**
  37       * Constructor
  38       * @param boolean $advanced advanced form element flag
  39       */
  40      public function user_filter_cohort($advanced) {
  41          parent::user_filter_type('cohort', get_string('idnumber', 'core_cohort'), $advanced);
  42      }
  43  
  44      /**
  45       * Returns an array of comparison operators
  46       * @return array of comparison operators
  47       */
  48      public function getOperators() {
  49          return array(0 => get_string('contains', 'filters'),
  50                       1 => get_string('doesnotcontain', 'filters'),
  51                       2 => get_string('isequalto', 'filters'),
  52                       3 => get_string('startswith', 'filters'),
  53                       4 => get_string('endswith', 'filters'),
  54                       5 => get_string('isempty', 'filters'));
  55      }
  56  
  57      /**
  58       * Adds controls specific to this filter in the form.
  59       * @param object $mform a MoodleForm object to setup
  60       */
  61      public function setupForm(&$mform) {
  62          $objs = array();
  63          $objs[] = $mform->createElement('select', $this->_name.'_op', null, $this->getOperators());
  64          $objs[] = $mform->createElement('text', $this->_name, null);
  65          $grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false);
  66          $mform->setType($this->_name, PARAM_RAW);
  67          $mform->disabledIf($this->_name, $this->_name.'_op', 'eq', 5);
  68          if ($this->_advanced) {
  69              $mform->setAdvanced($this->_name.'_grp');
  70          }
  71          $mform->setDefault($this->_name.'_op', 2);
  72      }
  73  
  74      /**
  75       * Retrieves data from the form data
  76       * @param object $formdata data submited with the form
  77       * @return mixed array filter data or false when filter not set
  78       */
  79      public function check_data($formdata) {
  80          $field    = $this->_name;
  81          $operator = $field.'_op';
  82  
  83          if (array_key_exists($operator, $formdata)) {
  84              if ($formdata->$operator != 5 and $formdata->$field == '') {
  85                  // No data - no change except for empty filter.
  86                  return false;
  87              }
  88              // If field value is set then use it, else it's null.
  89              $fieldvalue = null;
  90              if (isset($formdata->$field)) {
  91                  $fieldvalue = $formdata->$field;
  92              }
  93              return array('operator' => (int)$formdata->$operator, 'value' => $fieldvalue);
  94          }
  95  
  96          return false;
  97      }
  98  
  99      /**
 100       * Returns the condition to be used with SQL where
 101       * @param array $data filter settings
 102       * @return array sql string and $params
 103       */
 104      public function get_sql_filter($data) {
 105          global $DB;
 106          static $counter = 0;
 107          $name = 'ex_cohort'.$counter++;
 108  
 109          $operator = $data['operator'];
 110          $value    = $data['value'];
 111  
 112          $params = array();
 113  
 114          if ($value === '') {
 115              return '';
 116          }
 117  
 118          $not = '';
 119          switch($operator) {
 120              case 0: // Contains.
 121                  $res = $DB->sql_like('idnumber', ":$name", false, false);
 122                  $params[$name] = "%$value%";
 123                  break;
 124              case 1: // Does not contain.
 125                  $not = 'NOT';
 126                  $res = $DB->sql_like('idnumber', ":$name", false, false);
 127                  $params[$name] = "%$value%";
 128                  break;
 129              case 2: // Equal to.
 130                  $res = $DB->sql_like('idnumber', ":$name", false, false);
 131                  $params[$name] = "$value";
 132                  break;
 133              case 3: // Starts with.
 134                  $res = $DB->sql_like('idnumber', ":$name", false, false);
 135                  $params[$name] = "$value%";
 136                  break;
 137              case 4: // Ends with.
 138                  $res = $DB->sql_like('idnumber', ":$name", false, false);
 139                  $params[$name] = "%$value";
 140                  break;
 141              case 5: // Empty.
 142                  $not = 'NOT';
 143                  $res = '(idnumber IS NOT NULL AND idnumber <> :'.$name.')';
 144                  $params[$name] = '';
 145                  break;
 146              default:
 147                  return '';
 148          }
 149  
 150          $sql = "id $not IN (SELECT userid
 151                           FROM {cohort_members}
 152                           JOIN {cohort} ON {cohort_members}.cohortid = {cohort}.id
 153                          WHERE $res)";
 154  
 155          return array($sql, $params);
 156      }
 157  
 158      /**
 159       * Returns a human friendly description of the filter used as label.
 160       * @param array $data filter settings
 161       * @return string active filter label
 162       */
 163      public function get_label($data) {
 164          $operator  = $data['operator'];
 165          $value     = $data['value'];
 166          $operators = $this->getOperators();
 167  
 168          $a = new stdClass();
 169          $a->label    = $this->_label;
 170          $a->value    = '"'.s($value).'"';
 171          $a->operator = $operators[$operator];
 172  
 173          switch ($operator) {
 174              case 0: // Contains.
 175              case 1: // Doesn't contain.
 176              case 2: // Equal to.
 177              case 3: // Starts with.
 178              case 4: // Ends with.
 179                  return get_string('textlabel', 'filters', $a);
 180              case 5: // Empty.
 181                  return get_string('textlabelnovalue', 'filters', $a);
 182          }
 183  
 184          return '';
 185      }
 186  }


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