MDL-57449 block_myoverview: add behat tests for UI
[moodle.git] / calendar / lib.php
CommitLineData
93c91ee4 1<?php
08b4a4e1
RW
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
7423f116 16
08b4a4e1
RW
17/**
18 * Calendar extension
19 *
20 * @package core_calendar
21 * @copyright 2004 Greek School Network (http://www.sch.gr), Jon Papaioannou,
22 * Avgoustos Tsinakos
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
e30390a0
SH
26if (!defined('MOODLE_INTERNAL')) {
27 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
28}
b5a52acd 29
08b4a4e1
RW
30/**
31 * These are read by the administration component to provide default values
32 */
33
34/**
35 * CALENDAR_DEFAULT_UPCOMING_LOOKAHEAD - default value of upcoming event preference
36 */
bb4a2e85 37define('CALENDAR_DEFAULT_UPCOMING_LOOKAHEAD', 21);
08b4a4e1
RW
38
39/**
40 * CALENDAR_DEFAULT_UPCOMING_MAXEVENTS - default value to display the maximum number of upcoming event
41 */
bb4a2e85 42define('CALENDAR_DEFAULT_UPCOMING_MAXEVENTS', 10);
08b4a4e1
RW
43
44/**
45 * CALENDAR_DEFAULT_STARTING_WEEKDAY - default value to display the starting weekday
46 */
47define('CALENDAR_DEFAULT_STARTING_WEEKDAY', 1);
48
bb4a2e85 49// This is a packed bitfield: day X is "weekend" if $field & (1 << X) is true
50// Default value = 65 = 64 + 1 = 2^6 + 2^0 = Saturday & Sunday
08b4a4e1
RW
51
52/**
53 * CALENDAR_DEFAULT_WEEKEND - default value for weekend (Saturday & Sunday)
54 */
55define('CALENDAR_DEFAULT_WEEKEND', 65);
56
57/**
58 * CALENDAR_URL - path to calendar's folder
59 */
76d9df3f 60define('CALENDAR_URL', $CFG->wwwroot.'/calendar/');
08b4a4e1
RW
61
62/**
63 * CALENDAR_TF_24 - Calendar time in 24 hours format
64 */
76d9df3f 65define('CALENDAR_TF_24', '%H:%M');
08b4a4e1
RW
66
67/**
68 * CALENDAR_TF_12 - Calendar time in 12 hours format
69 */
76d9df3f 70define('CALENDAR_TF_12', '%I:%M %p');
bb4a2e85 71
08b4a4e1
RW
72/**
73 * CALENDAR_EVENT_GLOBAL - Global calendar event types
74 */
797cedc7 75define('CALENDAR_EVENT_GLOBAL', 1);
08b4a4e1
RW
76
77/**
78 * CALENDAR_EVENT_COURSE - Course calendar event types
79 */
797cedc7 80define('CALENDAR_EVENT_COURSE', 2);
08b4a4e1
RW
81
82/**
83 * CALENDAR_EVENT_GROUP - group calendar event types
84 */
797cedc7 85define('CALENDAR_EVENT_GROUP', 4);
08b4a4e1
RW
86
87/**
88 * CALENDAR_EVENT_USER - user calendar event types
89 */
797cedc7
SH
90define('CALENDAR_EVENT_USER', 8);
91
7423f116 92
b5a52acd
JH
93/**
94 * CALENDAR_IMPORT_FROM_FILE - import the calendar from a file
95 */
96define('CALENDAR_IMPORT_FROM_FILE', 0);
97
98/**
99 * CALENDAR_IMPORT_FROM_URL - import the calendar from a URL
100 */
101define('CALENDAR_IMPORT_FROM_URL', 1);
102
103/**
104 * CALENDAR_IMPORT_EVENT_UPDATED - imported event was updated
105 */
106define('CALENDAR_IMPORT_EVENT_UPDATED', 1);
107
108/**
109 * CALENDAR_IMPORT_EVENT_INSERTED - imported event was added by insert
110 */
111define('CALENDAR_IMPORT_EVENT_INSERTED', 2);
112
ee74a2a1
AA
113/**
114 * CALENDAR_SUBSCRIPTION_UPDATE - Used to represent update action for subscriptions in various forms.
115 */
116define('CALENDAR_SUBSCRIPTION_UPDATE', 1);
117
118/**
119 * CALENDAR_SUBSCRIPTION_REMOVE - Used to represent remove action for subscriptions in various forms.
120 */
121define('CALENDAR_SUBSCRIPTION_REMOVE', 2);
122
ca75ec4f
JP
123/**
124 * CALENDAR_EVENT_USER_OVERRIDE_PRIORITY - Constant for the user override priority.
125 */
126define('CALENDAR_EVENT_USER_OVERRIDE_PRIORITY', 9999999);
127
5ca71c2d
CB
128/**
129 * CALENDAR_EVENT_TYPE_STANDARD - Standard events.
130 */
131define('CALENDAR_EVENT_TYPE_STANDARD', 0);
132
133/**
134 * CALENDAR_EVENT_TYPE_ACTION - Action events.
135 */
136define('CALENDAR_EVENT_TYPE_ACTION', 1);
137
36dc3b71
SH
138/**
139 * Calendar information class
140 *
141 * This class is used simply to organise the information pertaining to a calendar
142 * and is used primarily to make information easily available.
08b4a4e1
RW
143 *
144 * @package core_calendar
145 * @category calendar
146 * @copyright 2010 Sam Hemelryk
147 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36dc3b71
SH
148 */
149class calendar_information {
08b4a4e1 150
da304137
MN
151 /**
152 * @var int The timestamp
153 *
154 * Rather than setting the day, month and year we will set a timestamp which will be able
155 * to be used by multiple calendars.
156 */
157 public $time;
36dc3b71 158
08b4a4e1 159 /** @var int A course id */
36dc3b71 160 public $courseid = null;
08b4a4e1
RW
161
162 /** @var array An array of courses */
36dc3b71 163 public $courses = array();
08b4a4e1
RW
164
165 /** @var array An array of groups */
36dc3b71 166 public $groups = array();
08b4a4e1
RW
167
168 /** @var array An array of users */
36dc3b71
SH
169 public $users = array();
170
171 /**
172 * Creates a new instance
173 *
08b4a4e1
RW
174 * @param int $day the number of the day
175 * @param int $month the number of the month
176 * @param int $year the number of the year
da304137
MN
177 * @param int $time the unixtimestamp representing the date we want to view, this is used instead of $calmonth
178 * and $calyear to support multiple calendars
36dc3b71 179 */
da304137
MN
180 public function __construct($day = 0, $month = 0, $year = 0, $time = 0) {
181 // If a day, month and year were passed then convert it to a timestamp. If these were passed
182 // then we can assume the day, month and year are passed as Gregorian, as no where in core
183 // should we be passing these values rather than the time. This is done for BC.
184 if (!empty($day) || !empty($month) || !empty($year)) {
185 $date = usergetdate(time());
186 if (empty($day)) {
187 $day = $date['mday'];
188 }
189 if (empty($month)) {
190 $month = $date['mon'];
191 }
192 if (empty($year)) {
193 $year = $date['year'];
194 }
195 if (checkdate($month, $day, $year)) {
196 $this->time = make_timestamp($year, $month, $day);
197 } else {
198 $this->time = time();
199 }
200 } else if (!empty($time)) {
201 $this->time = $time;
202 } else {
203 $this->time = time();
d8d8bdd9 204 }
36dc3b71
SH
205 }
206
797cedc7 207 /**
08b4a4e1 208 * Initialize calendar information
797cedc7 209 *
08b4a4e1 210 * @param stdClass $course object
797cedc7 211 * @param array $coursestoload An array of courses [$course->id => $course]
08b4a4e1 212 * @param bool $ignorefilters options to use filter
797cedc7
SH
213 */
214 public function prepare_for_view(stdClass $course, array $coursestoload, $ignorefilters = false) {
215 $this->courseid = $course->id;
216 $this->course = $course;
12cbce0a 217 list($courses, $group, $user) = \core_calendar\api::set_filters($coursestoload, $ignorefilters);
797cedc7
SH
218 $this->courses = $courses;
219 $this->groups = $group;
220 $this->users = $user;
221 }
222
36dc3b71
SH
223 /**
224 * Ensures the date for the calendar is correct and either sets it to now
225 * or throws a moodle_exception if not
226 *
08b4a4e1
RW
227 * @param bool $defaultonow use current time
228 * @throws moodle_exception
229 * @return bool validation of checkdate
36dc3b71
SH
230 */
231 public function checkdate($defaultonow = true) {
232 if (!checkdate($this->month, $this->day, $this->year)) {
233 if ($defaultonow) {
234 $now = usergetdate(time());
235 $this->day = intval($now['mday']);
236 $this->month = intval($now['mon']);
237 $this->year = intval($now['year']);
238 return true;
239 } else {
240 throw new moodle_exception('invaliddate');
241 }
242 }
243 return true;
244 }
da304137 245
36dc3b71
SH
246 /**
247 * Gets todays timestamp for the calendar
08b4a4e1
RW
248 *
249 * @return int today timestamp
36dc3b71
SH
250 */
251 public function timestamp_today() {
da304137 252 return $this->time;
36dc3b71
SH
253 }
254 /**
255 * Gets tomorrows timestamp for the calendar
08b4a4e1
RW
256 *
257 * @return int tomorrow timestamp
36dc3b71
SH
258 */
259 public function timestamp_tomorrow() {
a0ef87de 260 return strtotime('+1 day', $this->time);
36dc3b71
SH
261 }
262 /**
e30390a0 263 * Adds the pretend blocks for the calendar
36dc3b71
SH
264 *
265 * @param core_calendar_renderer $renderer
08b4a4e1
RW
266 * @param bool $showfilters display filters, false is set as default
267 * @param string|null $view preference view options (eg: day, month, upcoming)
36dc3b71
SH
268 */
269 public function add_sidecalendar_blocks(core_calendar_renderer $renderer, $showfilters=false, $view=null) {
270 if ($showfilters) {
271 $filters = new block_contents();
da304137 272 $filters->content = $renderer->fake_block_filters($this->courseid, 0, 0, 0, $view, $this->courses);
36dc3b71
SH
273 $filters->footer = '';
274 $filters->title = get_string('eventskey', 'calendar');
275 $renderer->add_pretend_calendar_block($filters, BLOCK_POS_RIGHT);
276 }
277 $block = new block_contents;
278 $block->content = $renderer->fake_block_threemonths($this);
279 $block->footer = '';
280 $block->title = get_string('monthlyview', 'calendar');
281 $renderer->add_pretend_calendar_block($block, BLOCK_POS_RIGHT);
282 }
1d5bd3d2 283}
b5a52acd 284
6e65554e
MG
285/**
286 * Implements callback user_preferences, whitelists preferences that users are allowed to update directly
287 *
288 * Used in {@see core_user::fill_preferences_cache()}, see also {@see useredit_update_user_preference()}
289 *
290 * @return array
291 */
292function core_calendar_user_preferences() {
293 $preferences = [];
294 $preferences['calendar_timeformat'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED, 'default' => '0',
295 'choices' => array('0', CALENDAR_TF_12, CALENDAR_TF_24)
296 );
297 $preferences['calendar_startwday'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 0,
298 'choices' => array(0, 1, 2, 3, 4, 5, 6));
299 $preferences['calendar_maxevents'] = array('type' => PARAM_INT, 'choices' => range(1, 20));
300 $preferences['calendar_lookahead'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 365,
301 'choices' => array(365, 270, 180, 150, 120, 90, 60, 30, 21, 14, 7, 6, 5, 4, 3, 2, 1));
302 $preferences['calendar_persistflt'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 0,
303 'choices' => array(0, 1));
304 return $preferences;
663640f5 305}