2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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');
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
{
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);
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'));
58 * Adds controls specific to this filter in the form.
59 * @param object $mform a MoodleForm object to setup
61 public function setupForm(&$mform) {
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);
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.
90 // If field value is set then use it, else it's null.
92 if (isset($formdata->$field)) {
93 $fieldvalue = $formdata->$field;
95 return array('operator' => (int)$formdata->$operator, 'value' => $fieldvalue);
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) {
109 $name = 'ex_cohort'.$counter++
;
111 $operator = $data['operator'];
112 $value = $data['value'];
123 $res = $DB->sql_like('idnumber', ":$name", false, false);
124 $params[$name] = "%$value%";
126 case 1: // Does not contain.
128 $res = $DB->sql_like('idnumber', ":$name", false, false);
129 $params[$name] = "%$value%";
132 $res = $DB->sql_like('idnumber', ":$name", false, false);
133 $params[$name] = "$value";
135 case 3: // Starts with.
136 $res = $DB->sql_like('idnumber', ":$name", false, false);
137 $params[$name] = "$value%";
139 case 4: // Ends with.
140 $res = $DB->sql_like('idnumber', ":$name", false, false);
141 $params[$name] = "%$value";
145 $res = '(idnumber IS NOT NULL AND idnumber <> :'.$name.')';
152 $sql = "id $not IN (SELECT userid
153 FROM {cohort_members}
154 JOIN {cohort} ON {cohort_members}.cohortid = {cohort}.id
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();
171 $a->label
= $this->_label
;
172 $a->value
= '"'.s($value).'"';
173 $a->operator
= $operators[$operator];
177 case 1: // Doesn't contain.
179 case 3: // Starts with.
180 case 4: // Ends with.
181 return get_string('textlabel', 'filters', $a);
183 return get_string('textlabelnovalue', 'filters', $a);