Merge branch 'wip-MDL-52008-master' of git://github.com/abgreeve/moodle
[moodle.git] / user / filters / cohort.php
bloba118f1643d3337573b5a68dbb0f4a1e215245045
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.
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/>.
17 /**
18 * Cohort filter.
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
26 defined('MOODLE_INTERNAL') || die();
28 require_once($CFG->dirroot.'/user/filters/lib.php');
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
35 class user_filter_cohort extends user_filter_type {
36 /**
37 * Constructor
38 * @param boolean $advanced advanced form element flag
40 public function user_filter_cohort($advanced) {
41 parent::user_filter_type('cohort', get_string('idnumber', 'core_cohort'), $advanced);
44 /**
45 * Returns an array of comparison operators
46 * @return array of comparison operators
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'));
57 /**
58 * Adds controls specific to this filter in the form.
59 * @param object $mform a MoodleForm object to setup
61 public function setupForm(&$mform) {
62 $objs = array();
63 $objs['select'] = $mform->createElement('select', $this->_name.'_op', null, $this->getOperators());
64 $objs['text'] = $mform->createElement('text', $this->_name, null);
65 $objs['select']->setLabel(get_string('limiterfor', 'filters', $this->_label));
66 $objs['text']->setLabel(get_string('valuefor', 'filters', $this->_label));
67 $grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false);
68 $mform->setType($this->_name, PARAM_RAW);
69 $mform->disabledIf($this->_name, $this->_name.'_op', 'eq', 5);
70 if ($this->_advanced) {
71 $mform->setAdvanced($this->_name.'_grp');
73 $mform->setDefault($this->_name.'_op', 2);
76 /**
77 * Retrieves data from the form data
78 * @param object $formdata data submited with the form
79 * @return mixed array filter data or false when filter not set
81 public function check_data($formdata) {
82 $field = $this->_name;
83 $operator = $field.'_op';
85 if (array_key_exists($operator, $formdata)) {
86 if ($formdata->$operator != 5 and $formdata->$field == '') {
87 // No data - no change except for empty filter.
88 return false;
90 // If field value is set then use it, else it's null.
91 $fieldvalue = null;
92 if (isset($formdata->$field)) {
93 $fieldvalue = $formdata->$field;
95 return array('operator' => (int)$formdata->$operator, 'value' => $fieldvalue);
98 return false;
102 * Returns the condition to be used with SQL where
103 * @param array $data filter settings
104 * @return array sql string and $params
106 public function get_sql_filter($data) {
107 global $DB;
108 static $counter = 0;
109 $name = 'ex_cohort'.$counter++;
111 $operator = $data['operator'];
112 $value = $data['value'];
114 $params = array();
116 if ($value === '') {
117 return '';
120 $not = '';
121 switch($operator) {
122 case 0: // Contains.
123 $res = $DB->sql_like('idnumber', ":$name", false, false);
124 $params[$name] = "%$value%";
125 break;
126 case 1: // Does not contain.
127 $not = 'NOT';
128 $res = $DB->sql_like('idnumber', ":$name", false, false);
129 $params[$name] = "%$value%";
130 break;
131 case 2: // Equal to.
132 $res = $DB->sql_like('idnumber', ":$name", false, false);
133 $params[$name] = "$value";
134 break;
135 case 3: // Starts with.
136 $res = $DB->sql_like('idnumber', ":$name", false, false);
137 $params[$name] = "$value%";
138 break;
139 case 4: // Ends with.
140 $res = $DB->sql_like('idnumber', ":$name", false, false);
141 $params[$name] = "%$value";
142 break;
143 case 5: // Empty.
144 $not = 'NOT';
145 $res = '(idnumber IS NOT NULL AND idnumber <> :'.$name.')';
146 $params[$name] = '';
147 break;
148 default:
149 return '';
152 $sql = "id $not IN (SELECT userid
153 FROM {cohort_members}
154 JOIN {cohort} ON {cohort_members}.cohortid = {cohort}.id
155 WHERE $res)";
157 return array($sql, $params);
161 * Returns a human friendly description of the filter used as label.
162 * @param array $data filter settings
163 * @return string active filter label
165 public function get_label($data) {
166 $operator = $data['operator'];
167 $value = $data['value'];
168 $operators = $this->getOperators();
170 $a = new stdClass();
171 $a->label = $this->_label;
172 $a->value = '"'.s($value).'"';
173 $a->operator = $operators[$operator];
175 switch ($operator) {
176 case 0: // Contains.
177 case 1: // Doesn't contain.
178 case 2: // Equal to.
179 case 3: // Starts with.
180 case 4: // Ends with.
181 return get_string('textlabel', 'filters', $a);
182 case 5: // Empty.
183 return get_string('textlabelnovalue', 'filters', $a);
186 return '';