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 __construct($advanced) {
41 parent
::__construct('cohort', get_string('idnumber', 'core_cohort'), $advanced);
45 * Old syntax of class constructor. Deprecated in PHP7.
47 * @deprecated since Moodle 3.1
49 public function user_filter_cohort($advanced) {
50 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER
);
51 self
::__construct($advanced);
55 * Returns an array of comparison operators
56 * @return array of comparison operators
58 public function getOperators() {
59 return array(0 => get_string('contains', 'filters'),
60 1 => get_string('doesnotcontain', 'filters'),
61 2 => get_string('isequalto', 'filters'),
62 3 => get_string('startswith', 'filters'),
63 4 => get_string('endswith', 'filters'),
64 5 => get_string('isempty', 'filters'));
68 * Adds controls specific to this filter in the form.
69 * @param object $mform a MoodleForm object to setup
71 public function setupForm(&$mform) {
73 $objs['select'] = $mform->createElement('select', $this->_name
.'_op', null, $this->getOperators());
74 $objs['text'] = $mform->createElement('text', $this->_name
, null);
75 $objs['select']->setLabel(get_string('limiterfor', 'filters', $this->_label
));
76 $objs['text']->setLabel(get_string('valuefor', 'filters', $this->_label
));
77 $grp =& $mform->addElement('group', $this->_name
.'_grp', $this->_label
, $objs, '', false);
78 $mform->setType($this->_name
, PARAM_RAW
);
79 $mform->disabledIf($this->_name
, $this->_name
.'_op', 'eq', 5);
80 if ($this->_advanced
) {
81 $mform->setAdvanced($this->_name
.'_grp');
83 $mform->setDefault($this->_name
.'_op', 2);
87 * Retrieves data from the form data
88 * @param object $formdata data submited with the form
89 * @return mixed array filter data or false when filter not set
91 public function check_data($formdata) {
92 $field = $this->_name
;
93 $operator = $field.'_op';
95 if (array_key_exists($operator, $formdata)) {
96 if ($formdata->$operator != 5 and $formdata->$field == '') {
97 // No data - no change except for empty filter.
100 // If field value is set then use it, else it's null.
102 if (isset($formdata->$field)) {
103 $fieldvalue = $formdata->$field;
105 return array('operator' => (int)$formdata->$operator, 'value' => $fieldvalue);
112 * Returns the condition to be used with SQL where
113 * @param array $data filter settings
114 * @return array sql string and $params
116 public function get_sql_filter($data) {
119 $name = 'ex_cohort'.$counter++
;
121 $operator = $data['operator'];
122 $value = $data['value'];
133 $res = $DB->sql_like('idnumber', ":$name", false, false);
134 $params[$name] = "%$value%";
136 case 1: // Does not contain.
138 $res = $DB->sql_like('idnumber', ":$name", false, false);
139 $params[$name] = "%$value%";
142 $res = $DB->sql_like('idnumber', ":$name", false, false);
143 $params[$name] = "$value";
145 case 3: // Starts with.
146 $res = $DB->sql_like('idnumber', ":$name", false, false);
147 $params[$name] = "$value%";
149 case 4: // Ends with.
150 $res = $DB->sql_like('idnumber', ":$name", false, false);
151 $params[$name] = "%$value";
155 $res = '(idnumber IS NOT NULL AND idnumber <> :'.$name.')';
162 $sql = "id $not IN (SELECT userid
163 FROM {cohort_members}
164 JOIN {cohort} ON {cohort_members}.cohortid = {cohort}.id
167 return array($sql, $params);
171 * Returns a human friendly description of the filter used as label.
172 * @param array $data filter settings
173 * @return string active filter label
175 public function get_label($data) {
176 $operator = $data['operator'];
177 $value = $data['value'];
178 $operators = $this->getOperators();
181 $a->label
= $this->_label
;
182 $a->value
= '"'.s($value).'"';
183 $a->operator
= $operators[$operator];
187 case 1: // Doesn't contain.
189 case 3: // Starts with.
190 case 4: // Ends with.
191 return get_string('textlabel', 'filters', $a);
193 return get_string('textlabelnovalue', 'filters', $a);