MDL-72626 lib: Upgrade RTLCSS to 1.0.1
[moodle.git] / user / filters / cohort.php
blob1ea0fe1da12a7995df7ffea7a73c79ac62607ae2
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 __construct($advanced) {
41 parent::__construct('cohort', get_string('idnumber', 'core_cohort'), $advanced);
44 /**
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);
54 /**
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'));
67 /**
68 * Adds controls specific to this filter in the form.
69 * @param object $mform a MoodleForm object to setup
71 public function setupForm(&$mform) {
72 $objs = array();
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);
86 /**
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 (property_exists($formdata, $operator)) {
96 if ($formdata->$operator != 5 and $formdata->$field == '') {
97 // No data - no change except for empty filter.
98 return false;
100 // If field value is set then use it, else it's null.
101 $fieldvalue = null;
102 if (isset($formdata->$field)) {
103 $fieldvalue = $formdata->$field;
105 return array('operator' => (int)$formdata->$operator, 'value' => $fieldvalue);
108 return false;
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) {
117 global $DB;
118 static $counter = 0;
119 $name = 'ex_cohort'.$counter++;
121 $operator = $data['operator'];
122 $value = $data['value'];
124 $params = array();
126 if ($value === '') {
127 return '';
130 $not = '';
131 switch($operator) {
132 case 0: // Contains.
133 $res = $DB->sql_like('idnumber', ":$name", false, false);
134 $params[$name] = "%$value%";
135 break;
136 case 1: // Does not contain.
137 $not = 'NOT';
138 $res = $DB->sql_like('idnumber', ":$name", false, false);
139 $params[$name] = "%$value%";
140 break;
141 case 2: // Equal to.
142 $res = $DB->sql_like('idnumber', ":$name", false, false);
143 $params[$name] = "$value";
144 break;
145 case 3: // Starts with.
146 $res = $DB->sql_like('idnumber', ":$name", false, false);
147 $params[$name] = "$value%";
148 break;
149 case 4: // Ends with.
150 $res = $DB->sql_like('idnumber', ":$name", false, false);
151 $params[$name] = "%$value";
152 break;
153 case 5: // Empty.
154 $not = 'NOT';
155 $res = '(idnumber IS NOT NULL AND idnumber <> :'.$name.')';
156 $params[$name] = '';
157 break;
158 default:
159 return '';
162 $sql = "id $not IN (SELECT userid
163 FROM {cohort_members}
164 JOIN {cohort} ON {cohort_members}.cohortid = {cohort}.id
165 WHERE $res)";
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();
180 $a = new stdClass();
181 $a->label = $this->_label;
182 $a->value = '"'.s($value).'"';
183 $a->operator = $operators[$operator];
185 switch ($operator) {
186 case 0: // Contains.
187 case 1: // Doesn't contain.
188 case 2: // Equal to.
189 case 3: // Starts with.
190 case 4: // Ends with.
191 return get_string('textlabel', 'filters', $a);
192 case 5: // Empty.
193 return get_string('textlabelnovalue', 'filters', $a);
196 return '';