mirrored from git://git.moodle.org/moodle.git
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
structure.php
430 lines (389 loc) · 13.8 KB
/
structure.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace calendartype_gregorian;
use core_calendar\type_base;
/**
* Handles calendar functions for the gregorian calendar.
*
* @package calendartype_gregorian
* @copyright 2008 onwards Foodle Group {@link http://foodle.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class structure extends type_base {
/**
* Returns the name of the calendar.
*
* This is the non-translated name, usually just
* the name of the folder.
*
* @return string the calendar name
*/
public function get_name() {
return 'gregorian';
}
/**
* Returns a list of all the possible days for all months.
*
* This is used to generate the select box for the days
* in the date selector elements. Some months contain more days
* than others so this function should return all possible days as
* we can not predict what month will be chosen (the user
* may have JS turned off and we need to support this situation in
* Moodle).
*
* @return array the days
*/
public function get_days() {
$days = array();
for ($i = 1; $i <= 31; $i++) {
$days[$i] = $i;
}
return $days;
}
/**
* Returns a list of all the names of the months.
*
* @return array the month names
*/
public function get_months() {
$months = array();
for ($i = 1; $i <= 12; $i++) {
$months[$i] = userdate(gmmktime(12, 0, 0, $i, 15, 2000), '%B');
}
return $months;
}
/**
* Returns the minimum year for the calendar.
*
* @return int The minimum year
*/
public function get_min_year() {
return 1900;
}
/**
* Returns the maximum year for the calendar
*
* @return int The maximum year
*/
public function get_max_year() {
return 2050;
}
/**
* Returns an array of years.
*
* @param int $minyear
* @param int $maxyear
* @return array the years
*/
public function get_years($minyear = null, $maxyear = null) {
if (is_null($minyear)) {
$minyear = $this->get_min_year();
}
if (is_null($maxyear)) {
$maxyear = $this->get_max_year();
}
$years = array();
for ($i = $minyear; $i <= $maxyear; $i++) {
$years[$i] = $i;
}
return $years;
}
/**
* Returns a multidimensional array with information for day, month, year
* and the order they are displayed when selecting a date.
* The order in the array will be the order displayed when selecting a date.
* Override this function to change the date selector order.
*
* @param int $minyear The year to start with
* @param int $maxyear The year to finish with
* @return array Full date information
*/
public function get_date_order($minyear = null, $maxyear = null) {
$dateinfo = array();
$dateinfo['day'] = $this->get_days();
$dateinfo['month'] = $this->get_months();
$dateinfo['year'] = $this->get_years($minyear, $maxyear);
return $dateinfo;
}
/**
* Returns the number of days in a week.
*
* @return int the number of days
*/
public function get_num_weekdays() {
return 7;
}
/**
* Returns an indexed list of all the names of the weekdays.
*
* The list starts with the index 0. Each index, representing a
* day, must be an array that contains the indexes 'shortname'
* and 'fullname'.
*
* @return array array of days
*/
public function get_weekdays() {
return array(
0 => array(
'shortname' => get_string('sun', 'calendar'),
'fullname' => get_string('sunday', 'calendar')
),
1 => array(
'shortname' => get_string('mon', 'calendar'),
'fullname' => get_string('monday', 'calendar')
),
2 => array(
'shortname' => get_string('tue', 'calendar'),
'fullname' => get_string('tuesday', 'calendar')
),
3 => array(
'shortname' => get_string('wed', 'calendar'),
'fullname' => get_string('wednesday', 'calendar')
),
4 => array(
'shortname' => get_string('thu', 'calendar'),
'fullname' => get_string('thursday', 'calendar')
),
5 => array(
'shortname' => get_string('fri', 'calendar'),
'fullname' => get_string('friday', 'calendar')
),
6 => array(
'shortname' => get_string('sat', 'calendar'),
'fullname' => get_string('saturday', 'calendar')
),
);
}
/**
* Returns the index of the starting week day.
*
* This may vary, for example some may consider Monday as the start of the week,
* where as others may consider Sunday the start.
*
* @return int
*/
public function get_starting_weekday() {
global $CFG;
if (isset($CFG->calendar_startwday)) {
$firstday = $CFG->calendar_startwday;
} else {
$firstday = get_string('firstdayofweek', 'langconfig');
}
if (!is_numeric($firstday)) {
$startingweekday = CALENDAR_DEFAULT_STARTING_WEEKDAY;
} else {
$startingweekday = intval($firstday) % 7;
}
return get_user_preferences('calendar_startwday', $startingweekday);
}
/**
* Returns the index of the weekday for a specific calendar date.
*
* @param int $year
* @param int $month
* @param int $day
* @return int
*/
public function get_weekday($year, $month, $day) {
return intval(date('w', mktime(12, 0, 0, $month, $day, $year)));
}
/**
* Returns the number of days in a given month.
*
* @param int $year
* @param int $month
* @return int the number of days
*/
public function get_num_days_in_month($year, $month) {
return intval(date('t', mktime(0, 0, 0, $month, 1, $year)));
}
/**
* Get the previous month.
*
* If the current month is January, it will get the last month of the previous year.
*
* @param int $year
* @param int $month
* @return array previous month and year
*/
public function get_prev_month($year, $month) {
if ($month == 1) {
return array(12, $year - 1);
} else {
return array($month - 1, $year);
}
}
/**
* Get the next month.
*
* If the current month is December, it will get the first month of the following year.
*
* @param int $year
* @param int $month
* @return array the following month and year
*/
public function get_next_month($year, $month) {
if ($month == 12) {
return array(1, $year + 1);
} else {
return array($month + 1, $year);
}
}
/**
* Returns a formatted string that represents a date in user time.
*
* Returns a formatted string that represents a date in user time
* <b>WARNING: note that the format is for strftime(), not date().</b>
* Because of a bug in most Windows time libraries, we can't use
* the nicer %e, so we have to use %d which has leading zeroes.
* A lot of the fuss in the function is just getting rid of these leading
* zeroes as efficiently as possible.
*
* If parameter fixday = true (default), then take off leading
* zero from %d, else maintain it.
*
* @param int $time the timestamp in UTC, as obtained from the database
* @param string $format strftime format
* @param int|float|string $timezone the timezone to use
* {@link http://docs.moodle.org/dev/Time_API#Timezone}
* @param bool $fixday if true then the leading zero from %d is removed,
* if false then the leading zero is maintained
* @param bool $fixhour if true then the leading zero from %I is removed,
* if false then the leading zero is maintained
* @return string the formatted date/time
*/
public function timestamp_to_date_string($time, $format, $timezone, $fixday, $fixhour) {
global $CFG;
if (empty($format)) {
$format = get_string('strftimedaydatetime', 'langconfig');
}
if (!empty($CFG->nofixday)) { // Config.php can force %d not to be fixed.
$fixday = false;
} else if ($fixday) {
$formatnoday = str_replace('%d', 'DD', $format);
$fixday = ($formatnoday != $format);
$format = $formatnoday;
}
// Note: This logic about fixing 12-hour time to remove unnecessary leading
// zero is required because on Windows, PHP strftime function does not
// support the correct 'hour without leading zero' parameter (%l).
if (!empty($CFG->nofixhour)) {
// Config.php can force %I not to be fixed.
$fixhour = false;
} else if ($fixhour) {
$formatnohour = str_replace('%I', 'HH', $format);
$fixhour = ($formatnohour != $format);
$format = $formatnohour;
}
// Add daylight saving offset for string timezones only, as we can't get dst for
// float values. if timezone is 99 (user default timezone), then try update dst.
if ((99 == $timezone) || !is_numeric($timezone)) {
$time += dst_offset_on($time, $timezone);
}
$timezone = get_user_timezone_offset($timezone);
// If we are running under Windows convert to windows encoding and then back to UTF-8
// (because it's impossible to specify UTF-8 to fetch locale info in Win32).
if (abs($timezone) > 13) { // Server time.
$datestring = date_format_string($time, $format, $timezone);
if ($fixday) {
$daystring = ltrim(str_replace(array(' 0', ' '), '', strftime(' %d', $time)));
$datestring = str_replace('DD', $daystring, $datestring);
}
if ($fixhour) {
$hourstring = ltrim(str_replace(array(' 0', ' '), '', strftime(' %I', $time)));
$datestring = str_replace('HH', $hourstring, $datestring);
}
} else {
$time += (int)($timezone * 3600);
$datestring = date_format_string($time, $format, $timezone);
if ($fixday) {
$daystring = ltrim(str_replace(array(' 0', ' '), '', gmstrftime(' %d', $time)));
$datestring = str_replace('DD', $daystring, $datestring);
}
if ($fixhour) {
$hourstring = ltrim(str_replace(array(' 0', ' '), '', gmstrftime(' %I', $time)));
$datestring = str_replace('HH', $hourstring, $datestring);
}
}
return $datestring;
}
/**
* Given a $time timestamp in GMT (seconds since epoch), returns an array that
* represents the date in user time.
*
* @param int $time Timestamp in GMT
* @param float|int|string $timezone offset's time with timezone, if float and not 99, then no
* dst offset is applied {@link http://docs.moodle.org/dev/Time_API#Timezone}
* @return array an array that represents the date in user time
*/
public function timestamp_to_date_array($time, $timezone = 99) {
return usergetdate($time, $timezone);
}
/**
* Provided with a day, month, year, hour and minute in a specific
* calendar type convert it into the equivalent Gregorian date.
*
* In this function we don't need to do anything except pass the data
* back as an array. This is because the date received is Gregorian.
*
* @param int $year
* @param int $month
* @param int $day
* @param int $hour
* @param int $minute
* @return array the converted date
*/
public function convert_from_gregorian($year, $month, $day, $hour = 0, $minute = 0) {
$date = array();
$date['year'] = $year;
$date['month'] = $month;
$date['day'] = $day;
$date['hour'] = $hour;
$date['minute'] = $minute;
return $date;
}
/**
* Provided with a day, month, year, hour and minute in a specific
* calendar type convert it into the equivalent Gregorian date.
*
* In this function we don't need to do anything except pass the data
* back as an array. This is because the date received is Gregorian.
*
* @param int $year
* @param int $month
* @param int $day
* @param int $hour
* @param int $minute
* @return array the converted date
*/
public function convert_to_gregorian($year, $month, $day, $hour = 0, $minute = 0) {
$date = array();
$date['year'] = $year;
$date['month'] = $month;
$date['day'] = $day;
$date['hour'] = $hour;
$date['minute'] = $minute;
return $date;
}
/**
* This return locale for windows os.
*
* @return string locale
*/
public function locale_win_charset() {
return get_string('localewincharset', 'langconfig');
}
}