Merge branch 'wip-mdl-48624-m27' of https://github.com/rajeshtaneja/moodle into MOODL...
[moodle.git] / user / filters / select.php
blob8fbf3dbdb15319a07d4581e177bcec193e6b3c63
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 * Value select filter.
20 * @package core_user
21 * @category user
22 * @copyright 1999 Martin Dougiamas http://dougiamas.com
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once($CFG->dirroot.'/user/filters/lib.php');
28 /**
29 * Generic filter based on a list of values.
30 * @copyright 1999 Martin Dougiamas http://dougiamas.com
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 class user_filter_select extends user_filter_type {
34 /**
35 * options for the list values
36 * @var array
38 public $_options;
40 /** @var string */
41 public $_field;
43 /** @var mixed|null */
44 public $_default;
46 /**
47 * Constructor
48 * @param string $name the name of the filter instance
49 * @param string $label the label of the filter instance
50 * @param boolean $advanced advanced form element flag
51 * @param string $field user table filed name
52 * @param array $options select options
53 * @param mixed $default option
55 public function user_filter_select($name, $label, $advanced, $field, $options, $default=null) {
56 parent::user_filter_type($name, $label, $advanced);
57 $this->_field = $field;
58 $this->_options = $options;
59 $this->_default = $default;
62 /**
63 * Returns an array of comparison operators
64 * @return array of comparison operators
66 public function get_operators() {
67 return array(0 => get_string('isanyvalue', 'filters'),
68 1 => get_string('isequalto', 'filters'),
69 2 => get_string('isnotequalto', 'filters'));
72 /**
73 * Adds controls specific to this filter in the form.
74 * @param moodleform $mform a MoodleForm object to setup
76 public function setupForm(&$mform) {
77 $objs = array();
78 $objs[] = $mform->createElement('select', $this->_name.'_op', null, $this->get_operators());
79 $objs[] = $mform->createElement('select', $this->_name, null, $this->_options);
80 $grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false);
81 $mform->disabledIf($this->_name, $this->_name.'_op', 'eq', 0);
82 if (!is_null($this->_default)) {
83 $mform->setDefault($this->_name, $this->_default);
85 if ($this->_advanced) {
86 $mform->setAdvanced($this->_name.'_grp');
90 /**
91 * Retrieves data from the form data
92 * @param stdClass $formdata data submited with the form
93 * @return mixed array filter data or false when filter not set
95 public function check_data($formdata) {
96 $field = $this->_name;
97 $operator = $field.'_op';
99 if (array_key_exists($field, $formdata) and !empty($formdata->$operator)) {
100 return array('operator' => (int)$formdata->$operator,
101 'value' => (string)$formdata->$field);
104 return false;
108 * Returns the condition to be used with SQL where
109 * @param array $data filter settings
110 * @return array sql string and $params
112 public function get_sql_filter($data) {
113 static $counter = 0;
114 $name = 'ex_select'.$counter++;
116 $operator = $data['operator'];
117 $value = $data['value'];
118 $field = $this->_field;
120 $params = array();
122 switch($operator) {
123 case 1: // Equal to.
124 $res = "=:$name";
125 $params[$name] = $value;
126 break;
127 case 2: // Not equal to.
128 $res = "<>:$name";
129 $params[$name] = $value;
130 break;
131 default:
132 return array('', array());
134 return array($field.$res, $params);
138 * Returns a human friendly description of the filter used as label.
139 * @param array $data filter settings
140 * @return string active filter label
142 public function get_label($data) {
143 $operators = $this->get_operators();
144 $operator = $data['operator'];
145 $value = $data['value'];
147 if (empty($operator)) {
148 return '';
151 $a = new stdClass();
152 $a->label = $this->_label;
153 $a->value = '"'.s($this->_options[$value]).'"';
154 $a->operator = $operators[$operator];
156 return get_string('selectlabel', 'filters', $a);