"MDL-8642, setting of auto scrolling"
[moodle-linuxchix.git] / user / filters / date.php
blob17e72f772ad9672300cc17cdea592f5c080d9daf
1 <?php //$Id$
3 require_once($CFG->dirroot.'/user/filters/lib.php');
5 /**
6 * Generic filter based on a date.
7 */
8 class user_filter_date extends user_filter_type {
9 /**
10 * the fields available for comparisson
12 var $_field;
14 /**
15 * Constructor
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;
26 /**
27 * Adds controls specific to this filter in the form.
28 * @param object $mform a MoodleForm object to setup
30 function setupForm(&$mform) {
31 $objs = array();
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');
52 /**
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)) {
64 return false;
67 $data = array();
68 if (array_key_exists($sck, $formdata)) {
69 $data['after'] = $formdata->$sdt;
70 } else {
71 $data['after'] = 0;
73 if (array_key_exists($eck, $formdata)) {
74 $data['before'] = $formdata->$edt;
75 } else {
76 $data['before'] = 0;
78 return $data;
81 /**
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)) {
92 return '';
95 $res = "$field > 0" ;
97 if ($after) {
98 $res .= " AND $field >= $after";
100 if ($before) {
101 $res .= " AND $field <= $before";
103 return $res;
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;
116 $a = new object();
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);
124 } else if ($after) {
125 return get_string('datelabelisafter', 'filters', $a);
127 } else if ($before) {
128 return get_string('datelabelisbefore', 'filters', $a);
130 return '';