forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
305 lines (269 loc) · 9.7 KB
/
lib.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
<?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/>.
/**
* Calendar extension
*
* @package core_calendar
* @copyright 2004 Greek School Network (http://www.sch.gr), Jon Papaioannou,
* Avgoustos Tsinakos
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
}
/**
* These are read by the administration component to provide default values
*/
/**
* CALENDAR_DEFAULT_UPCOMING_LOOKAHEAD - default value of upcoming event preference
*/
define('CALENDAR_DEFAULT_UPCOMING_LOOKAHEAD', 21);
/**
* CALENDAR_DEFAULT_UPCOMING_MAXEVENTS - default value to display the maximum number of upcoming event
*/
define('CALENDAR_DEFAULT_UPCOMING_MAXEVENTS', 10);
/**
* CALENDAR_DEFAULT_STARTING_WEEKDAY - default value to display the starting weekday
*/
define('CALENDAR_DEFAULT_STARTING_WEEKDAY', 1);
// This is a packed bitfield: day X is "weekend" if $field & (1 << X) is true
// Default value = 65 = 64 + 1 = 2^6 + 2^0 = Saturday & Sunday
/**
* CALENDAR_DEFAULT_WEEKEND - default value for weekend (Saturday & Sunday)
*/
define('CALENDAR_DEFAULT_WEEKEND', 65);
/**
* CALENDAR_URL - path to calendar's folder
*/
define('CALENDAR_URL', $CFG->wwwroot.'/calendar/');
/**
* CALENDAR_TF_24 - Calendar time in 24 hours format
*/
define('CALENDAR_TF_24', '%H:%M');
/**
* CALENDAR_TF_12 - Calendar time in 12 hours format
*/
define('CALENDAR_TF_12', '%I:%M %p');
/**
* CALENDAR_EVENT_GLOBAL - Global calendar event types
*/
define('CALENDAR_EVENT_GLOBAL', 1);
/**
* CALENDAR_EVENT_COURSE - Course calendar event types
*/
define('CALENDAR_EVENT_COURSE', 2);
/**
* CALENDAR_EVENT_GROUP - group calendar event types
*/
define('CALENDAR_EVENT_GROUP', 4);
/**
* CALENDAR_EVENT_USER - user calendar event types
*/
define('CALENDAR_EVENT_USER', 8);
/**
* CALENDAR_IMPORT_FROM_FILE - import the calendar from a file
*/
define('CALENDAR_IMPORT_FROM_FILE', 0);
/**
* CALENDAR_IMPORT_FROM_URL - import the calendar from a URL
*/
define('CALENDAR_IMPORT_FROM_URL', 1);
/**
* CALENDAR_IMPORT_EVENT_UPDATED - imported event was updated
*/
define('CALENDAR_IMPORT_EVENT_UPDATED', 1);
/**
* CALENDAR_IMPORT_EVENT_INSERTED - imported event was added by insert
*/
define('CALENDAR_IMPORT_EVENT_INSERTED', 2);
/**
* CALENDAR_SUBSCRIPTION_UPDATE - Used to represent update action for subscriptions in various forms.
*/
define('CALENDAR_SUBSCRIPTION_UPDATE', 1);
/**
* CALENDAR_SUBSCRIPTION_REMOVE - Used to represent remove action for subscriptions in various forms.
*/
define('CALENDAR_SUBSCRIPTION_REMOVE', 2);
/**
* CALENDAR_EVENT_USER_OVERRIDE_PRIORITY - Constant for the user override priority.
*/
define('CALENDAR_EVENT_USER_OVERRIDE_PRIORITY', 9999999);
/**
* CALENDAR_EVENT_TYPE_STANDARD - Standard events.
*/
define('CALENDAR_EVENT_TYPE_STANDARD', 0);
/**
* CALENDAR_EVENT_TYPE_ACTION - Action events.
*/
define('CALENDAR_EVENT_TYPE_ACTION', 1);
/**
* Calendar information class
*
* This class is used simply to organise the information pertaining to a calendar
* and is used primarily to make information easily available.
*
* @package core_calendar
* @category calendar
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class calendar_information {
/**
* @var int The timestamp
*
* Rather than setting the day, month and year we will set a timestamp which will be able
* to be used by multiple calendars.
*/
public $time;
/** @var int A course id */
public $courseid = null;
/** @var array An array of courses */
public $courses = array();
/** @var array An array of groups */
public $groups = array();
/** @var array An array of users */
public $users = array();
/**
* Creates a new instance
*
* @param int $day the number of the day
* @param int $month the number of the month
* @param int $year the number of the year
* @param int $time the unixtimestamp representing the date we want to view, this is used instead of $calmonth
* and $calyear to support multiple calendars
*/
public function __construct($day = 0, $month = 0, $year = 0, $time = 0) {
// If a day, month and year were passed then convert it to a timestamp. If these were passed
// then we can assume the day, month and year are passed as Gregorian, as no where in core
// should we be passing these values rather than the time. This is done for BC.
if (!empty($day) || !empty($month) || !empty($year)) {
$date = usergetdate(time());
if (empty($day)) {
$day = $date['mday'];
}
if (empty($month)) {
$month = $date['mon'];
}
if (empty($year)) {
$year = $date['year'];
}
if (checkdate($month, $day, $year)) {
$this->time = make_timestamp($year, $month, $day);
} else {
$this->time = time();
}
} else if (!empty($time)) {
$this->time = $time;
} else {
$this->time = time();
}
}
/**
* Initialize calendar information
*
* @param stdClass $course object
* @param array $coursestoload An array of courses [$course->id => $course]
* @param bool $ignorefilters options to use filter
*/
public function prepare_for_view(stdClass $course, array $coursestoload, $ignorefilters = false) {
$this->courseid = $course->id;
$this->course = $course;
list($courses, $group, $user) = \core_calendar\api::set_filters($coursestoload, $ignorefilters);
$this->courses = $courses;
$this->groups = $group;
$this->users = $user;
}
/**
* Ensures the date for the calendar is correct and either sets it to now
* or throws a moodle_exception if not
*
* @param bool $defaultonow use current time
* @throws moodle_exception
* @return bool validation of checkdate
*/
public function checkdate($defaultonow = true) {
if (!checkdate($this->month, $this->day, $this->year)) {
if ($defaultonow) {
$now = usergetdate(time());
$this->day = intval($now['mday']);
$this->month = intval($now['mon']);
$this->year = intval($now['year']);
return true;
} else {
throw new moodle_exception('invaliddate');
}
}
return true;
}
/**
* Gets todays timestamp for the calendar
*
* @return int today timestamp
*/
public function timestamp_today() {
return $this->time;
}
/**
* Gets tomorrows timestamp for the calendar
*
* @return int tomorrow timestamp
*/
public function timestamp_tomorrow() {
return strtotime('+1 day', $this->time);
}
/**
* Adds the pretend blocks for the calendar
*
* @param core_calendar_renderer $renderer
* @param bool $showfilters display filters, false is set as default
* @param string|null $view preference view options (eg: day, month, upcoming)
*/
public function add_sidecalendar_blocks(core_calendar_renderer $renderer, $showfilters=false, $view=null) {
if ($showfilters) {
$filters = new block_contents();
$filters->content = $renderer->fake_block_filters($this->courseid, 0, 0, 0, $view, $this->courses);
$filters->footer = '';
$filters->title = get_string('eventskey', 'calendar');
$renderer->add_pretend_calendar_block($filters, BLOCK_POS_RIGHT);
}
$block = new block_contents;
$block->content = $renderer->fake_block_threemonths($this);
$block->footer = '';
$block->title = get_string('monthlyview', 'calendar');
$renderer->add_pretend_calendar_block($block, BLOCK_POS_RIGHT);
}
}
/**
* Implements callback user_preferences, whitelists preferences that users are allowed to update directly
*
* Used in {@see core_user::fill_preferences_cache()}, see also {@see useredit_update_user_preference()}
*
* @return array
*/
function core_calendar_user_preferences() {
$preferences = [];
$preferences['calendar_timeformat'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED, 'default' => '0',
'choices' => array('0', CALENDAR_TF_12, CALENDAR_TF_24)
);
$preferences['calendar_startwday'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 0,
'choices' => array(0, 1, 2, 3, 4, 5, 6));
$preferences['calendar_maxevents'] = array('type' => PARAM_INT, 'choices' => range(1, 20));
$preferences['calendar_lookahead'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 365,
'choices' => array(365, 270, 180, 150, 120, 90, 60, 30, 21, 14, 7, 6, 5, 4, 3, 2, 1));
$preferences['calendar_persistflt'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 0,
'choices' => array(0, 1));
return $preferences;
}