adding current groupid to grade_export class - soon to be used in plugins
[moodle-pu.git] / user / filters / date.php
blob78f6c345778627b9c6781bba3ff3c22cfa95d93b
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 end Unix timestamp (0 if disabled)
12 var $_value2;
13 /**
14 * the fields available for comparisson
16 var $_fields;
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 int $start the start Unix timestamp (0 if disabled)
23 * @param int $end the end Unix timestamp (0 if disabled)
25 function user_filter_date($name, $label, $field, $fields=null, $start=0, $end=0) {
26 parent::user_filter_type($name, $label, $field, $start);
27 $this->_value2 = $end;
28 $this->_fields = $fields;
31 /**
32 * Adds controls specific to this filter in the form.
33 * @param object $mform a MoodleForm object to setup
35 function setupForm(&$mform) {
36 $objs = array();
37 if(is_array($this->_fields)) {
38 $objs[] =& $mform->createElement('select', $this->_name . '_fld', null, $this->_fields);
40 $objs[] =& $mform->createElement('checkbox', $this->_name . '_sck', null, get_string('isafter', 'filters'));
41 $objs[] =& $mform->createElement('date_selector', $this->_name . '_sdt', null);
42 $objs[] =& $mform->createElement('checkbox', $this->_name . '_eck', null, get_string('isbefore', 'filters'));
43 $objs[] =& $mform->createElement('date_selector', $this->_name . '_edt', null);
44 $grp =& $mform->addElement('group', $this->_name . '_grp', $this->_label, $objs, '', false);
45 $grp->setHelpButton(array('date','','filters'));
46 $mform->setDefault($this->_name . '_sck', !empty($this->_value));
47 $mform->setDefault($this->_name . '_eck', !empty($this->_value2));
48 $mform->setDefault($this->_name . '_sdt', $this->_value);
49 $mform->setDefault($this->_name . '_edt', $this->_value2);
50 if(is_array($this->_fields)) {
51 $mform->setDefault($this->_name . '_fld', $this->_field);
53 $mform->disabledIf($this->_name . '_sdt', $this->_name . '_sck');
54 $mform->disabledIf($this->_name . '_edt', $this->_name . '_eck');
57 /**
58 * Retrieves data from the form data
59 * @param object $formdata data submited with the form
61 function checkData($formdata) {
62 $fld = $this->_name . '_fld';
63 $sdt = $this->_name . '_sdt';
64 $edt = $this->_name . '_edt';
65 $sck = $this->_name . '_sck';
66 $eck = $this->_name . '_eck';
67 if(@$formdata->$fld) {
68 $this->_field = @$formdata->$fld;
70 $this->_value = @$formdata->$sck ? (int)@$formdata->$sdt : 0;
71 $this->_value2 = @$formdata->$eck ? (int)@$formdata->$edt : 0;
74 /**
75 * Returns the condition to be used with SQL where
76 * @return string the filtering condition or null if the filter is disabled
78 function getSQLFilter() {
79 if(empty($this->_value) && empty($this->_value2)) {
80 return null;
82 $res = $this->_field . '>0' ;
83 if($this->_value) {
84 $res .= ' AND ' . $this->_field . '>=' . $this->_value;
86 if($this->_value2) {
87 $res .= ' AND ' . $this->_field . '<=' . $this->_value2;
89 return $res;
92 /**
93 * Returns a human friendly description of the filter.
94 * @return string filter description
96 function getDescription() {
97 if(is_array($this->_fields)) {
98 $res = $this->_fields[$this->_field] . ' ';
99 } else {
100 $res = $this->_label . ' ';
102 if($this->_value && $this->_value2) {
103 $res .= get_string('isbetween', 'filters', array(userdate($this->_value), userdate($this->_value2)));
104 } else if($this->_value) {
105 $res .= get_string('isafter', 'filters', userdate($this->_value));
106 } else {
107 $res .= get_string('isbefore', 'filters', userdate($this->_value2));
109 return $res;