MDL-64285 core: Fix environment check test
[moodle.git] / user / filters / date.php
blob8587ab902b94e7f762922199d928ad9779a7b674
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Date filter
20 * @package core_user
21 * @category user
22 * @copyright 1999 Martin Dougiamas http://dougiamas.com
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once($CFG->dirroot.'/user/filters/lib.php');
28 /**
29 * Generic filter based on a date.
30 * @copyright 1999 Martin Dougiamas http://dougiamas.com
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 class user_filter_date extends user_filter_type {
34 /**
35 * the fields available for comparisson
36 * @var string
38 public $_field;
40 /**
41 * Constructor
42 * @param string $name the name of the filter instance
43 * @param string $label the label of the filter instance
44 * @param boolean $advanced advanced form element flag
45 * @param string $field user table filed name
47 public function __construct($name, $label, $advanced, $field) {
48 parent::__construct($name, $label, $advanced);
49 $this->_field = $field;
52 /**
53 * Old syntax of class constructor. Deprecated in PHP7.
55 * @deprecated since Moodle 3.1
57 public function user_filter_date($name, $label, $advanced, $field) {
58 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
59 self::__construct($name, $label, $advanced, $field);
62 /**
63 * Adds controls specific to this filter in the form.
64 * @param object $mform a MoodleForm object to setup
66 public function setupForm(&$mform) {
67 $objs = array();
69 $objs[] = $mform->createElement('static', $this->_name.'_sck', null, get_string('isafter', 'filters'));
70 $objs[] = $mform->createElement('date_selector', $this->_name.'_sdt', null, array('optional' => true));
71 $objs[] = $mform->createElement('static', $this->_name.'_break', null, '<br/>');
72 $objs[] = $mform->createElement('static', $this->_name.'_edk', null, get_string('isbefore', 'filters'));
73 $objs[] = $mform->createElement('date_selector', $this->_name.'_edt', null, array('optional' => true));
75 $grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false);
77 if ($this->_advanced) {
78 $mform->setAdvanced($this->_name.'_grp');
82 /**
83 * Retrieves data from the form data
84 * @param object $formdata data submited with the form
85 * @return mixed array filter data or false when filter not set
87 public function check_data($formdata) {
88 $sdt = $this->_name.'_sdt';
89 $edt = $this->_name.'_edt';
91 if (!$formdata->$sdt and !$formdata->$edt) {
92 return false;
95 $data = array();
96 $data['after'] = $formdata->$sdt;
97 $data['before'] = $formdata->$edt;
99 return $data;
103 * Returns the condition to be used with SQL where
104 * @param array $data filter settings
105 * @return array sql string and $params
107 public function get_sql_filter($data) {
108 $after = (int)$data['after'];
109 $before = (int)$data['before'];
111 $field = $this->_field;
113 if (empty($after) and empty($before)) {
114 return array('', array());
117 $res = " $field >= 0 ";
119 if ($after) {
120 $res .= " AND $field >= $after";
123 if ($before) {
124 $res .= " AND $field <= $before";
126 return array($res, array());
130 * Returns a human friendly description of the filter used as label.
131 * @param array $data filter settings
132 * @return string active filter label
134 public function get_label($data) {
135 $after = $data['after'];
136 $before = $data['before'];
137 $field = $this->_field;
139 $a = new stdClass();
140 $a->label = $this->_label;
141 $a->after = userdate($after);
142 $a->before = userdate($before);
144 if ($after and $before) {
145 return get_string('datelabelisbetween', 'filters', $a);
146 } else if ($after) {
147 return get_string('datelabelisafter', 'filters', $a);
148 } else if ($before) {
149 return get_string('datelabelisbefore', 'filters', $a);
151 return '';