|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00028 defined('MOODLE_INTERNAL') || die(); 00029 00030 require_once($CFG->dirroot.'/user/filters/lib.php'); 00031 00035 class user_filter_checkbox extends user_filter_type { 00040 protected $disableelements = array(); 00041 00046 protected $field; 00047 00058 public function __construct($name, $label, $advanced, $field, $disableelements=null) { 00059 parent::__construct($name, $label, $advanced); 00060 $this->field = $field; 00061 if (!empty($disableelements)) { 00062 if (!is_array($disableelements)) { 00063 $this->disableelements = array($disableelements); 00064 } else { 00065 $this->disableelements = $disableelements; 00066 } 00067 } 00068 } 00069 00075 public function setupForm(MoodleQuickForm &$mform) { 00076 $objs = array(); 00077 00078 $objs[] = $mform->createElement('checkbox', $this->_name, null, ''); 00079 $grp = $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false); 00080 00081 if ($this->_advanced) { 00082 $mform->setAdvanced($this->_name.'_grp'); 00083 } 00084 //Check if disable if options are set. if yes then set rules 00085 if (!empty($this->disableelements) && is_array($this->disableelements)) { 00086 foreach ($this->disableelements as $disableelement) { 00087 $mform->disabledIf($disableelement, $this->_name, 'checked'); 00088 } 00089 } 00090 } 00091 00098 public function check_data($formdata) { 00099 $field = $this->_name; 00100 //Check if disable if options are set. if yes then don't add this. 00101 if (!empty($this->disableelements) && is_array($this->disableelements)) { 00102 foreach ($this->disableelements as $disableelement) { 00103 if (array_key_exists($disableelement, $formdata)) { 00104 return false; 00105 } 00106 } 00107 } 00108 if (array_key_exists($field, $formdata) and $formdata->$field !== '') { 00109 return array('value' => (string)$formdata->$field); 00110 } 00111 return false; 00112 } 00113 00120 public function get_sql_filter($data) { 00121 $field = $this->field; 00122 if (is_array($field)) { 00123 $res = " {$field[0]} = {$field[1]} "; 00124 } else { 00125 $res = " {$field} = 0 "; 00126 } 00127 return array($res, array()); 00128 } 00129 00136 public function get_label($data) { 00137 return $this->_label; 00138 } 00139 }