3 require_once($CFG->dirroot
.'/user/filters/lib.php');
6 * Generic filter based on a date.
8 class user_filter_date
extends user_filter_type
{
10 * the fields available for comparisson
16 * @param string $name the name of the filter instance
17 * @param string $label the label of the filter instance
18 * @param boolean $advanced advanced form element flag
19 * @param string $field user table filed name
21 function user_filter_date($name, $label, $advanced, $field) {
22 parent
::user_filter_type($name, $label, $advanced);
23 $this->_field
= $field;
27 * Adds controls specific to this filter in the form.
28 * @param object $mform a MoodleForm object to setup
30 function setupForm(&$mform) {
33 $objs[] =& $mform->createElement('checkbox', $this->_name
.'_sck', null, get_string('isafter', 'filters'));
34 $objs[] =& $mform->createElement('date_selector', $this->_name
.'_sdt', null);
35 $objs[] =& $mform->createElement('checkbox', $this->_name
.'_eck', null, get_string('isbefore', 'filters'));
36 $objs[] =& $mform->createElement('date_selector', $this->_name
.'_edt', null);
37 $grp =& $mform->addElement('group', $this->_name
.'_grp', $this->_label
, $objs, '', false);
38 $grp->setHelpButton(array('date',$this->_label
,'filters'));
40 if ($this->_advanced
) {
41 $mform->setAdvanced($this->_name
.'_grp');
44 $mform->disabledIf($this->_name
.'_sdt[day]', $this->_name
.'_sck', 'notchecked');
45 $mform->disabledIf($this->_name
.'_sdt[month]', $this->_name
.'_sck', 'notchecked');
46 $mform->disabledIf($this->_name
.'_sdt[year]', $this->_name
.'_sck', 'notchecked');
47 $mform->disabledIf($this->_name
.'_edt[day]', $this->_name
.'_eck', 'notchecked');
48 $mform->disabledIf($this->_name
.'_edt[month]', $this->_name
.'_eck', 'notchecked');
49 $mform->disabledIf($this->_name
.'_edt[year]', $this->_name
.'_eck', 'notchecked');
53 * Retrieves data from the form data
54 * @param object $formdata data submited with the form
55 * @return mixed array filter data or false when filter not set
57 function check_data($formdata) {
58 $sck = $this->_name
.'_sck';
59 $sdt = $this->_name
.'_sdt';
60 $eck = $this->_name
.'_eck';
61 $edt = $this->_name
.'_edt';
63 if (!array_key_exists($sck, $formdata) and !array_key_exists($eck, $formdata)) {
68 if (array_key_exists($sck, $formdata)) {
69 $data['after'] = $formdata->$sdt;
73 if (array_key_exists($eck, $formdata)) {
74 $data['before'] = $formdata->$edt;
82 * Returns the condition to be used with SQL where
83 * @param array $data filter settings
84 * @return string the filtering condition or null if the filter is disabled
86 function get_sql_filter($data) {
87 $after = $data['after'];
88 $before = $data['before'];
89 $field = $this->_field
;
91 if (empty($after) and empty($before)) {
98 $res .= " AND $field >= $after";
101 $res .= " AND $field <= $before";
107 * Returns a human friendly description of the filter used as label.
108 * @param array $data filter settings
109 * @return string active filter label
111 function get_label($data) {
112 $after = $data['after'];
113 $before = $data['before'];
114 $field = $this->_field
;
117 $a->label
= $this->_label
;
118 $a->after
= userdate($after);
119 $a->before
= userdate($before);
121 if ($after and $before) {
122 return get_string('datelabelisbetween', 'filters', $a);
125 return get_string('datelabelisafter', 'filters', $a);
127 } else if ($before) {
128 return get_string('datelabelisbefore', 'filters', $a);