MDL-21437 data - number and date fields search under mssql and oracle
[moodle.git] / mod / data / field / date / field.class.php
1 <?php
2 ///////////////////////////////////////////////////////////////////////////
3 //                                                                       //
4 // NOTICE OF COPYRIGHT                                                   //
5 //                                                                       //
6 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
7 //          http://moodle.org                                            //
8 //                                                                       //
9 // Copyright (C) 1999-onwards Moodle Pty Ltd  http://moodle.com          //
10 //                                                                       //
11 // This program is free software; you can redistribute it and/or modify  //
12 // it under the terms of the GNU General Public License as published by  //
13 // the Free Software Foundation; either version 2 of the License, or     //
14 // (at your option) any later version.                                   //
15 //                                                                       //
16 // This program is distributed in the hope that it will be useful,       //
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of        //
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
19 // GNU General Public License for more details:                          //
20 //                                                                       //
21 //          http://www.gnu.org/copyleft/gpl.html                         //
22 //                                                                       //
23 ///////////////////////////////////////////////////////////////////////////
25 //2/19/07:  Advanced search of the date field is currently disabled because it does not track
26 // pre 1970 dates and does not handle blank entrys.  Advanced search functionality for this field
27 // type can be enabled once these issues are addressed in the core API.
29 class data_field_date extends data_field_base {
31     var $type = 'date';
33     var $day   = 0;
34     var $month = 0;
35     var $year  = 0;
37     function display_add_field($recordid=0) {
38         global $DB, $OUTPUT;
40         if ($recordid) {
41             $content = (int)$DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid));
42         } else {
43             $content = time();
44         }
46         $str = '<div title="'.s($this->field->description).'">';
47         $dayselector = html_writer::select_time('days', 'field_'.$this->field->id.'_day', $content);
48         $monthselector = html_writer::select_time('months', 'field_'.$this->field->id.'_month', $content);
49         $yearselector = html_writer::select_time('years', 'field_'.$this->field->id.'_year', $content);
50         $str .= $dayselector . $monthselector . $yearselector;
51         $str .= '</div>';
53         return $str;
54     }
56     //Enable the following three functions once core API issues have been addressed.
57     function display_search_field($value=0) {
58         $selectors = html_writer::select_time('days', 'f_'.$this->field->id.'_d', $value)
59            . html_writer::select_time('months', 'f_'.$this->field->id.'_m', $value)
60            . html_writer::select_time('years', 'f_'.$this->field->id.'_y', $value);
61        return $selectors;
63         //return print_date_selector('f_'.$this->field->id.'_d', 'f_'.$this->field->id.'_m', 'f_'.$this->field->id.'_y', $value, true);
64     }
66     function generate_sql($tablealias, $value) {
67         global $DB;
69         static $i=0;
70         $i++;
71         $name = "df_date_$i";
72         $varcharcontent = $DB->sql_compare_text("{$tablealias}.content");
73         return array(" ({$tablealias}.fieldid = {$this->field->id} AND $varcharcontent = :$name) ", array($name=>$value));
74     }
76     function parse_search_field() {
78         $day   = optional_param('f_'.$this->field->id.'_d', 0, PARAM_INT);
79         $month = optional_param('f_'.$this->field->id.'_m', 0, PARAM_INT);
80         $year  = optional_param('f_'.$this->field->id.'_y', 0, PARAM_INT);
81         if (!empty($day) && !empty($month) && !empty($year)) {
82             return make_timestamp($year, $month, $day, 12, 0, 0, 0, false);
83         }
84         else {
85             return 0;
86         }
88     }
90     function update_content($recordid, $value, $name='') {
91         global $DB;
93         $names = explode('_',$name);
94         $name = $names[2];          // day month or year
96         $this->$name = $value;
98         if ($this->day and $this->month and $this->year) {  // All of them have been collected now
100             $content = new stdClass();
101             $content->fieldid = $this->field->id;
102             $content->recordid = $recordid;
103             $content->content = make_timestamp($this->year, $this->month, $this->day, 12, 0, 0, 0, false);
105             if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
106                 $content->id = $oldcontent->id;
107                 return $DB->update_record('data_content', $content);
108             } else {
109                 return $DB->insert_record('data_content', $content);
110             }
111         }
112     }
114     function display_browse_field($recordid, $template) {
115         global $CFG, $DB;
117         if ($content = $DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
118             return userdate($content, get_string('strftimedate'), 0);
119         }
120     }
122     function get_sort_sql($fieldname) {
123         global $DB;
124         return $DB->sql_cast_char2int($fieldname, true);
125     }