MDL-10873 If both site default and user pref are empty for studentsperpage, we assume...
[moodle-pu.git] / user / filters / radios.php
blobfcad77e58ee4a9aada007324edc8c94f17fc874e
1 <?php //$Id$
3 require_once($CFG->dirroot . '/user/filters/lib.php');
5 /**
6 * Generic filter with radio buttons for integer fields.
7 */
8 class user_filter_radios extends user_filter_type {
9 /**
10 * options for the radio buttons
12 var $_options;
13 /**
14 * id of the option which disables the filter
16 var $_offoption;
17 /**
18 * Constructor
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;
29 if($offoption) {
30 $this->_offoption = @min(array_keys($options)) - 1;
31 } else {
32 $this->_offoption = null;
36 /**
37 * Adds controls specific to this filter in the form.
38 * @param object $mform a MoodleForm object to setup
40 function setupForm(&$mform) {
41 $objs = array();
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'));
53 /**
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;
62 /**
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) {
68 return null;
70 return $this->_field . '=' . $this->_value;
73 /**
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];