8934ed736846ddc8e294ee03519d0df8ffc9d9c2
[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         static $i=0;
68         $i++;
69         $name = "df_picture_$i";
70         return array(" ({$tablealias}.fieldid = {$this->field->id} AND {$tablealias}.content = '$value') ", array($name=>"%$value%"));
71     }
73     function parse_search_field() {
75         $day   = optional_param('f_'.$this->field->id.'_d', 0, PARAM_INT);
76         $month = optional_param('f_'.$this->field->id.'_m', 0, PARAM_INT);
77         $year  = optional_param('f_'.$this->field->id.'_y', 0, PARAM_INT);
78         if (!empty($day) && !empty($month) && !empty($year)) {
79             return make_timestamp($year, $month, $day, 12, 0, 0, 0, false);
80         }
81         else {
82             return 0;
83         }
85     }
87     function update_content($recordid, $value, $name='') {
88         global $DB;
90         $names = explode('_',$name);
91         $name = $names[2];          // day month or year
93         $this->$name = $value;
95         if ($this->day and $this->month and $this->year) {  // All of them have been collected now
97             $content = new stdClass();
98             $content->fieldid = $this->field->id;
99             $content->recordid = $recordid;
100             $content->content = make_timestamp($this->year, $this->month, $this->day, 12, 0, 0, 0, false);
102             if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
103                 $content->id = $oldcontent->id;
104                 return $DB->update_record('data_content', $content);
105             } else {
106                 return $DB->insert_record('data_content', $content);
107             }
108         }
109     }
111     function display_browse_field($recordid, $template) {
112         global $CFG, $DB;
114         if ($content = $DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
115             return userdate($content, get_string('strftimedate'), 0);
116         }
117     }
119     function get_sort_sql($fieldname) {
120         global $DB;
121         return $DB->sql_cast_char2int($fieldname, true);
122     }