|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 require_once($CFG->dirroot.'/user/filters/lib.php'); 00004 00008 class user_filter_select extends user_filter_type { 00012 var $_options; 00013 00014 var $_field; 00015 00016 var $_default; 00017 00027 function user_filter_select($name, $label, $advanced, $field, $options, $default=null) { 00028 parent::user_filter_type($name, $label, $advanced); 00029 $this->_field = $field; 00030 $this->_options = $options; 00031 $this->_default = $default; 00032 } 00033 00038 function get_operators() { 00039 return array(0 => get_string('isanyvalue','filters'), 00040 1 => get_string('isequalto','filters'), 00041 2 => get_string('isnotequalto','filters')); 00042 } 00043 00048 function setupForm(&$mform) { 00049 $objs = array(); 00050 $objs[] =& $mform->createElement('select', $this->_name.'_op', null, $this->get_operators()); 00051 $objs[] =& $mform->createElement('select', $this->_name, null, $this->_options); 00052 $grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false); 00053 $mform->disabledIf($this->_name, $this->_name.'_op', 'eq', 0); 00054 if (!is_null($this->_default)) { 00055 $mform->setDefault($this->_name, $this->_default); 00056 } 00057 if ($this->_advanced) { 00058 $mform->setAdvanced($this->_name.'_grp'); 00059 } 00060 } 00061 00067 function check_data($formdata) { 00068 $field = $this->_name; 00069 $operator = $field.'_op'; 00070 00071 if (array_key_exists($field, $formdata) and !empty($formdata->$operator)) { 00072 return array('operator' => (int)$formdata->$operator, 00073 'value' => (string)$formdata->$field); 00074 } 00075 00076 return false; 00077 } 00078 00084 function get_sql_filter($data) { 00085 static $counter = 0; 00086 $name = 'ex_select'.$counter++; 00087 00088 $operator = $data['operator']; 00089 $value = $data['value']; 00090 $field = $this->_field; 00091 00092 $params = array(); 00093 00094 switch($operator) { 00095 case 1: // equal to 00096 $res = "=:$name"; 00097 $params[$name] = $value; 00098 break; 00099 case 2: // not equal to 00100 $res = "<>:$name"; 00101 $params[$name] = $value; 00102 break; 00103 default: 00104 return array('', array()); 00105 } 00106 return array($field.$res, $params); 00107 } 00108 00114 function get_label($data) { 00115 $operators = $this->get_operators(); 00116 $operator = $data['operator']; 00117 $value = $data['value']; 00118 00119 if (empty($operator)) { 00120 return ''; 00121 } 00122 00123 $a = new stdClass(); 00124 $a->label = $this->_label; 00125 $a->value = '"'.s($this->_options[$value]).'"'; 00126 $a->operator = $operators[$operator]; 00127 00128 return get_string('selectlabel', 'filters', $a); 00129 } 00130 } 00131