MDL-77326 core_grades: Refactor grade report functions.
[moodle.git] / user / filters / checkbox.php
blobf2b8f9421835b72b4eb11c300c70a8a33bb7feef
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 * Generic checkbox filter.
20 * This will create generic filter with checkbox option and can be used for
21 * disabling other elements for specific condition.
23 * @package core_user
24 * @category user
25 * @copyright 2011 Rajesh Taneja
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 defined('MOODLE_INTERNAL') || die();
31 require_once($CFG->dirroot.'/user/filters/lib.php');
33 /**
34 * Generic filter based for checkbox and can be used for disabling items
35 * @copyright 1999 Martin Dougiamas http://dougiamas.com
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class user_filter_checkbox extends user_filter_type {
39 /**
40 * list of all the fields which needs to be disabled, if checkbox is checked
41 * @var array
43 protected $disableelements = array();
45 /**
46 * name of user table field/fields on which data needs to be compared
47 * @var mixed
49 protected $field;
51 /**
52 * Constructor, initalize user_filter_type and sets $disableelements array
53 * with list of elements to be diabled by checkbox.
55 * @param string $name the name of the filter instance
56 * @param string $label the label of the filter instance
57 * @param boolean $advanced advanced form element flag
58 * @param mixed $field user table field/fields name for comparison
59 * @param array $disableelements name of fields which should be disabled if this checkbox is checked.
61 public function __construct($name, $label, $advanced, $field, $disableelements=null) {
62 parent::__construct($name, $label, $advanced);
63 $this->field = $field;
64 if (!empty($disableelements)) {
65 if (!is_array($disableelements)) {
66 $this->disableelements = array($disableelements);
67 } else {
68 $this->disableelements = $disableelements;
73 /**
74 * Adds controls specific to this filter in the form.
76 * @param moodleform $mform a MoodleQuickForm object in which element will be added
78 public function setupForm(&$mform) {
79 $mform->addElement('checkbox', $this->_name, $this->_label, '');
81 if ($this->_advanced) {
82 $mform->setAdvanced($this->_name);
84 // Check if disable if options are set. if yes then set rules.
85 if (!empty($this->disableelements) && is_array($this->disableelements)) {
86 foreach ($this->disableelements as $disableelement) {
87 $mform->disabledIf($disableelement, $this->_name, 'checked');
92 /**
93 * Retrieves data from the form data
95 * @param object $formdata data submited with the form
96 * @return mixed array filter data or false when filter not set
98 public function check_data($formdata) {
99 $field = $this->_name;
100 // Check if disable if options are set. if yes then don't add this..
101 if (!empty($this->disableelements) && is_array($this->disableelements)) {
102 foreach ($this->disableelements as $disableelement) {
103 if (property_exists($formdata, $disableelement)) {
104 return false;
108 if (property_exists($formdata, $field) and $formdata->$field !== '') {
109 return array('value' => (string)$formdata->$field);
111 return false;
115 * Returns the condition to be used with SQL where
117 * @param array $data filter settings
118 * @return array sql string and $params
120 public function get_sql_filter($data) {
121 $field = $this->field;
122 if (is_array($field)) {
123 $res = " {$field[0]} = {$field[1]} ";
124 } else {
125 $res = " {$field} = 0 ";
127 return array($res, array());
131 * Returns a human friendly description of the filter used as label.
133 * @param array $data filter settings
134 * @return string active filter label
136 public function get_label($data) {
137 return $this->_label;