1 |
efrain |
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 |
///////////////////////////////////////////////////////////////////////////
|
|
|
24 |
|
|
|
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.
|
|
|
28 |
|
|
|
29 |
class data_field_date extends data_field_base {
|
|
|
30 |
|
|
|
31 |
var $type = 'date';
|
|
|
32 |
|
|
|
33 |
var $day = 0;
|
|
|
34 |
var $month = 0;
|
|
|
35 |
var $year = 0;
|
|
|
36 |
|
|
|
37 |
public function supports_preview(): bool {
|
|
|
38 |
return true;
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
public function get_data_content_preview(int $recordid): stdClass {
|
|
|
42 |
return (object)[
|
|
|
43 |
'id' => 0,
|
|
|
44 |
'fieldid' => $this->field->id,
|
|
|
45 |
'recordid' => $recordid,
|
|
|
46 |
'content' => (string) time(),
|
|
|
47 |
'content1' => null,
|
|
|
48 |
'content2' => null,
|
|
|
49 |
'content3' => null,
|
|
|
50 |
'content4' => null,
|
|
|
51 |
];
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
function display_add_field($recordid = 0, $formdata = null) {
|
|
|
55 |
global $DB, $OUTPUT;
|
|
|
56 |
|
|
|
57 |
if ($formdata) {
|
|
|
58 |
$fieldname = 'field_' . $this->field->id . '_day';
|
|
|
59 |
$day = $formdata->$fieldname;
|
|
|
60 |
$fieldname = 'field_' . $this->field->id . '_month';
|
|
|
61 |
$month = $formdata->$fieldname;
|
|
|
62 |
$fieldname = 'field_' . $this->field->id . '_year';
|
|
|
63 |
$year = $formdata->$fieldname;
|
|
|
64 |
|
|
|
65 |
$calendartype = \core_calendar\type_factory::get_calendar_instance();
|
|
|
66 |
$gregoriandate = $calendartype->convert_to_gregorian($year, $month, $day);
|
|
|
67 |
$content = make_timestamp(
|
|
|
68 |
$gregoriandate['year'],
|
|
|
69 |
$gregoriandate['month'],
|
|
|
70 |
$gregoriandate['day'],
|
|
|
71 |
$gregoriandate['hour'],
|
|
|
72 |
$gregoriandate['minute'],
|
|
|
73 |
0,
|
|
|
74 |
0,
|
|
|
75 |
false);
|
|
|
76 |
} else if ($recordid) {
|
|
|
77 |
$content = (int)$DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid));
|
|
|
78 |
} else {
|
|
|
79 |
$content = time();
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
$str = '<div title="'.s($this->field->description).'" class="mod-data-input d-flex flex-wrap align-items-center">';
|
|
|
83 |
|
|
|
84 |
$dayselector = html_writer::select_time(
|
|
|
85 |
type: 'days',
|
|
|
86 |
name: "field_{$this->field->id}_day",
|
|
|
87 |
currenttime: $content,
|
|
|
88 |
timezone: 0,
|
|
|
89 |
);
|
|
|
90 |
$monthselector = html_writer::select_time(
|
|
|
91 |
type: 'months',
|
|
|
92 |
name: "field_{$this->field->id}_month",
|
|
|
93 |
currenttime: $content,
|
|
|
94 |
timezone: 0,
|
|
|
95 |
);
|
|
|
96 |
$yearselector = html_writer::select_time(
|
|
|
97 |
type: 'years',
|
|
|
98 |
name: "field_{$this->field->id}_year",
|
|
|
99 |
currenttime: $content,
|
|
|
100 |
timezone: 0,
|
|
|
101 |
);
|
|
|
102 |
|
|
|
103 |
$str .= $dayselector . $monthselector . $yearselector;
|
|
|
104 |
$str .= '</div>';
|
|
|
105 |
|
|
|
106 |
return $str;
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
// Enable the following three functions once core API issues have been addressed.
|
|
|
110 |
|
|
|
111 |
/**
|
|
|
112 |
* Display the search field in advanced search page
|
|
|
113 |
* @param mixed $value
|
|
|
114 |
* @return string
|
|
|
115 |
* @throws coding_exception
|
|
|
116 |
*/
|
|
|
117 |
public function display_search_field($value = null) {
|
|
|
118 |
$currenttime = time();
|
|
|
119 |
$selectors = html_writer::select_time('days', 'f_' . $this->field->id . '_d', $value['timestamp'] ?? $currenttime)
|
|
|
120 |
. html_writer::select_time('months', 'f_' . $this->field->id . '_m', $value['timestamp'] ?? $currenttime)
|
|
|
121 |
. html_writer::select_time('years', 'f_' . $this->field->id . '_y', $value['timestamp'] ?? $currenttime);
|
|
|
122 |
$datecheck = html_writer::checkbox('f_' . $this->field->id . '_z', 1, $value['usedate'] ?? 0);
|
|
|
123 |
$str = '<div class="d-flex flex-wrap">' . $selectors . ' ' . $datecheck . ' ' . get_string('usedate', 'data') . '</div>';
|
|
|
124 |
|
|
|
125 |
return $str;
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
function generate_sql($tablealias, $value) {
|
|
|
129 |
global $DB;
|
|
|
130 |
|
|
|
131 |
static $i=0;
|
|
|
132 |
$i++;
|
|
|
133 |
$name = "df_date_$i";
|
|
|
134 |
$varcharcontent = $DB->sql_compare_text("{$tablealias}.content");
|
|
|
135 |
return array(" ({$tablealias}.fieldid = {$this->field->id} AND $varcharcontent = :$name) ", array($name => $value['timestamp']));
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
public function parse_search_field($defaults = null) {
|
|
|
139 |
$paramday = 'f_'.$this->field->id.'_d';
|
|
|
140 |
$parammonth = 'f_'.$this->field->id.'_m';
|
|
|
141 |
$paramyear = 'f_'.$this->field->id.'_y';
|
|
|
142 |
$paramusedate = 'f_'.$this->field->id.'_z';
|
|
|
143 |
if (empty($defaults[$paramday])) { // One empty means the other ones are empty too.
|
|
|
144 |
$defaults = array($paramday => 0, $parammonth => 0, $paramyear => 0, $paramusedate => 0);
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
$day = optional_param($paramday, $defaults[$paramday], PARAM_INT);
|
|
|
148 |
$month = optional_param($parammonth, $defaults[$parammonth], PARAM_INT);
|
|
|
149 |
$year = optional_param($paramyear, $defaults[$paramyear], PARAM_INT);
|
|
|
150 |
$usedate = optional_param($paramusedate, $defaults[$paramusedate], PARAM_INT);
|
|
|
151 |
|
|
|
152 |
$data = array();
|
|
|
153 |
if (!empty($day) && !empty($month) && !empty($year) && $usedate == 1) {
|
|
|
154 |
$calendartype = \core_calendar\type_factory::get_calendar_instance();
|
|
|
155 |
$gregoriandate = $calendartype->convert_to_gregorian($year, $month, $day);
|
|
|
156 |
|
|
|
157 |
$data['timestamp'] = make_timestamp(
|
|
|
158 |
$gregoriandate['year'],
|
|
|
159 |
$gregoriandate['month'],
|
|
|
160 |
$gregoriandate['day'],
|
|
|
161 |
$gregoriandate['hour'],
|
|
|
162 |
$gregoriandate['minute'],
|
|
|
163 |
0,
|
|
|
164 |
0,
|
|
|
165 |
false);
|
|
|
166 |
$data['usedate'] = 1;
|
|
|
167 |
return $data;
|
|
|
168 |
} else {
|
|
|
169 |
return 0;
|
|
|
170 |
}
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
function update_content($recordid, $value, $name='') {
|
|
|
174 |
global $DB;
|
|
|
175 |
|
|
|
176 |
$names = explode('_',$name);
|
|
|
177 |
$name = $names[2]; // day month or year
|
|
|
178 |
|
|
|
179 |
$this->$name = $value;
|
|
|
180 |
|
|
|
181 |
if ($this->day and $this->month and $this->year) { // All of them have been collected now
|
|
|
182 |
|
|
|
183 |
$content = new stdClass();
|
|
|
184 |
$content->fieldid = $this->field->id;
|
|
|
185 |
$content->recordid = $recordid;
|
|
|
186 |
|
|
|
187 |
$calendartype = \core_calendar\type_factory::get_calendar_instance();
|
|
|
188 |
$gregoriandate = $calendartype->convert_to_gregorian($this->year, $this->month, $this->day);
|
|
|
189 |
$content->content = make_timestamp(
|
|
|
190 |
$gregoriandate['year'],
|
|
|
191 |
$gregoriandate['month'],
|
|
|
192 |
$gregoriandate['day'],
|
|
|
193 |
$gregoriandate['hour'],
|
|
|
194 |
$gregoriandate['minute'],
|
|
|
195 |
0,
|
|
|
196 |
0,
|
|
|
197 |
false);
|
|
|
198 |
|
|
|
199 |
if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
|
|
|
200 |
$content->id = $oldcontent->id;
|
|
|
201 |
return $DB->update_record('data_content', $content);
|
|
|
202 |
} else {
|
|
|
203 |
return $DB->insert_record('data_content', $content);
|
|
|
204 |
}
|
|
|
205 |
}
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
function display_browse_field($recordid, $template) {
|
|
|
209 |
$content = $this->get_data_content($recordid);
|
|
|
210 |
if (!$content || empty($content->content)) {
|
|
|
211 |
return '';
|
|
|
212 |
}
|
|
|
213 |
return userdate($content->content, get_string('strftimedate'), 0);
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
function get_sort_sql($fieldname) {
|
|
|
217 |
global $DB;
|
|
|
218 |
return $DB->sql_cast_char2real($fieldname, true);
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
/**
|
|
|
222 |
* Return the plugin configs for external functions.
|
|
|
223 |
*
|
|
|
224 |
* @return array the list of config parameters
|
|
|
225 |
* @since Moodle 3.3
|
|
|
226 |
*/
|
|
|
227 |
public function get_config_for_external() {
|
|
|
228 |
// Return all the config parameters.
|
|
|
229 |
$configs = [];
|
|
|
230 |
for ($i = 1; $i <= 10; $i++) {
|
|
|
231 |
$configs["param$i"] = $this->field->{"param$i"};
|
|
|
232 |
}
|
|
|
233 |
return $configs;
|
|
|
234 |
}
|
|
|
235 |
}
|