3 require_once($CFG->dirroot
. '/user/filters/lib.php');
6 * Generic filter with radio buttons for integer fields.
8 class user_filter_radios
extends user_filter_type
{
10 * options for the radio buttons
14 * id of the option which disables the filter
19 * @param string $name the name of the filter instance
20 * @param string $label the label of the filter instance
21 * @param string $field the field used for filtering data
22 * @param array $options associative array used to generate the radio buttons
23 * @param boolean $offoption true if a "don't care" option should be generated
24 * @param int $value the value used for filtering data
26 function user_filter_radios($name, $label, $field, $options, $offoption=true, $value=null) {
27 parent
::user_filter_type($name, $label, $field, $value);
28 $this->_options
= $options;
30 $this->_offoption
= @min
(array_keys($options)) - 1;
32 $this->_offoption
= null;
37 * Adds controls specific to this filter in the form.
38 * @param object $mform a MoodleForm object to setup
40 function setupForm(&$mform) {
42 if(!is_null($this->_offoption
)) {
43 $objs[] =& $mform->createElement('radio', $this->_name
, null, get_string('anyvalue', 'filters'), $this->_offoption
);
45 foreach($this->_options
as $k=>$v) {
46 $objs[] =& $mform->createElement('radio', $this->_name
, null, $v, $k);
48 $grp =& $mform->addElement('group', $this->_name
. '_grp', $this->_label
, $objs, '', false);
49 $mform->setDefault($this->_name
, -1);
50 $grp->setHelpButton(array('radios','','filters'));
54 * Retrieves data from the form data
55 * @param object $formdata data submited with the form
57 function checkData($formdata) {
58 $field = $this->_name
;
59 $this->_value
= (int)@$formdata->$field;
63 * Returns the condition to be used with SQL where
64 * @return string the filtering condition or null if the filter is disabled
66 function getSQLFilter() {
67 if($this->_value
=== $this->_offoption
) {
70 return $this->_field
. '=' . $this->_value
;
74 * Returns a human friendly description of the filter.
75 * @return string filter description
77 function getDescription() {
78 return $this->_label
. ' is ' . $this->_options
[$this->_value
];