Commit | Line | Data |
---|---|---|
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 |
26 | if (!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 | 37 | define('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 | 42 | define('CALENDAR_DEFAULT_UPCOMING_MAXEVENTS', 10); |
08b4a4e1 RW |
43 | |
44 | /** | |
45 | * CALENDAR_DEFAULT_STARTING_WEEKDAY - default value to display the starting weekday | |
46 | */ | |
47 | define('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 | */ | |
55 | define('CALENDAR_DEFAULT_WEEKEND', 65); | |
56 | ||
57 | /** | |
58 | * CALENDAR_URL - path to calendar's folder | |
59 | */ | |
76d9df3f | 60 | define('CALENDAR_URL', $CFG->wwwroot.'/calendar/'); |
08b4a4e1 RW |
61 | |
62 | /** | |
63 | * CALENDAR_TF_24 - Calendar time in 24 hours format | |
64 | */ | |
76d9df3f | 65 | define('CALENDAR_TF_24', '%H:%M'); |
08b4a4e1 RW |
66 | |
67 | /** | |
68 | * CALENDAR_TF_12 - Calendar time in 12 hours format | |
69 | */ | |
76d9df3f | 70 | define('CALENDAR_TF_12', '%I:%M %p'); |
bb4a2e85 | 71 | |
08b4a4e1 RW |
72 | /** |
73 | * CALENDAR_EVENT_GLOBAL - Global calendar event types | |
74 | */ | |
797cedc7 | 75 | define('CALENDAR_EVENT_GLOBAL', 1); |
08b4a4e1 RW |
76 | |
77 | /** | |
78 | * CALENDAR_EVENT_COURSE - Course calendar event types | |
79 | */ | |
797cedc7 | 80 | define('CALENDAR_EVENT_COURSE', 2); |
08b4a4e1 RW |
81 | |
82 | /** | |
83 | * CALENDAR_EVENT_GROUP - group calendar event types | |
84 | */ | |
797cedc7 | 85 | define('CALENDAR_EVENT_GROUP', 4); |
08b4a4e1 RW |
86 | |
87 | /** | |
88 | * CALENDAR_EVENT_USER - user calendar event types | |
89 | */ | |
797cedc7 SH |
90 | define('CALENDAR_EVENT_USER', 8); |
91 | ||
7423f116 | 92 | |
b5a52acd JH |
93 | /** |
94 | * CALENDAR_IMPORT_FROM_FILE - import the calendar from a file | |
95 | */ | |
96 | define('CALENDAR_IMPORT_FROM_FILE', 0); | |
97 | ||
98 | /** | |
99 | * CALENDAR_IMPORT_FROM_URL - import the calendar from a URL | |
100 | */ | |
101 | define('CALENDAR_IMPORT_FROM_URL', 1); | |
102 | ||
103 | /** | |
104 | * CALENDAR_IMPORT_EVENT_UPDATED - imported event was updated | |
105 | */ | |
106 | define('CALENDAR_IMPORT_EVENT_UPDATED', 1); | |
107 | ||
108 | /** | |
109 | * CALENDAR_IMPORT_EVENT_INSERTED - imported event was added by insert | |
110 | */ | |
111 | define('CALENDAR_IMPORT_EVENT_INSERTED', 2); | |
112 | ||
ee74a2a1 AA |
113 | /** |
114 | * CALENDAR_SUBSCRIPTION_UPDATE - Used to represent update action for subscriptions in various forms. | |
115 | */ | |
116 | define('CALENDAR_SUBSCRIPTION_UPDATE', 1); | |
117 | ||
118 | /** | |
119 | * CALENDAR_SUBSCRIPTION_REMOVE - Used to represent remove action for subscriptions in various forms. | |
120 | */ | |
121 | define('CALENDAR_SUBSCRIPTION_REMOVE', 2); | |
122 | ||
ca75ec4f JP |
123 | /** |
124 | * CALENDAR_EVENT_USER_OVERRIDE_PRIORITY - Constant for the user override priority. | |
125 | */ | |
126 | define('CALENDAR_EVENT_USER_OVERRIDE_PRIORITY', 9999999); | |
127 | ||
797cedc7 SH |
128 | /** |
129 | * Return the days of the week | |
130 | * | |
08b4a4e1 | 131 | * @return array array of days |
797cedc7 SH |
132 | */ |
133 | function calendar_get_days() { | |
da304137 MN |
134 | $calendartype = \core_calendar\type_factory::get_calendar_instance(); |
135 | return $calendartype->get_weekdays(); | |
797cedc7 | 136 | } |
f2bffd9e | 137 | |
e73b527c AA |
138 | /** |
139 | * Get the subscription from a given id | |
140 | * | |
141 | * @since Moodle 2.5 | |
142 | * @param int $id id of the subscription | |
143 | * @return stdClass Subscription record from DB | |
144 | * @throws moodle_exception for an invalid id | |
145 | */ | |
146 | function calendar_get_subscription($id) { | |
147 | global $DB; | |
148 | ||
149 | $cache = cache::make('core', 'calendar_subscriptions'); | |
150 | $subscription = $cache->get($id); | |
151 | if (empty($subscription)) { | |
152 | $subscription = $DB->get_record('event_subscriptions', array('id' => $id), '*', MUST_EXIST); | |
153 | // cache the data. | |
154 | $cache->set($id, $subscription); | |
155 | } | |
156 | return $subscription; | |
157 | } | |
158 | ||
76d9df3f SH |
159 | /** |
160 | * Gets the first day of the week | |
161 | * | |
162 | * Used to be define('CALENDAR_STARTING_WEEKDAY', blah); | |
163 | * | |
164 | * @return int | |
165 | */ | |
166 | function calendar_get_starting_weekday() { | |
da304137 MN |
167 | $calendartype = \core_calendar\type_factory::get_calendar_instance(); |
168 | return $calendartype->get_starting_weekday(); | |
76d9df3f | 169 | } |
bd119567 | 170 | |
0f927f1e | 171 | /** |
14236cbd | 172 | * Generates the HTML for a miniature calendar |
0f927f1e | 173 | * |
512edd65 | 174 | * @param array $courses list of course to list events from |
08b4a4e1 RW |
175 | * @param array $groups list of group |
176 | * @param array $users user's info | |
da304137 MN |
177 | * @param int|bool $calmonth calendar month in numeric, default is set to false |
178 | * @param int|bool $calyear calendar month in numeric, default is set to false | |
179 | * @param string|bool $placement the place/page the calendar is set to appear - passed on the the controls function | |
180 | * @param int|bool $courseid id of the course the calendar is displayed on - passed on the the controls function | |
181 | * @param int $time the unixtimestamp representing the date we want to view, this is used instead of $calmonth | |
182 | * and $calyear to support multiple calendars | |
08b4a4e1 | 183 | * @return string $content return html table for mini calendar |
0f927f1e | 184 | */ |
da304137 MN |
185 | function calendar_get_mini($courses, $groups, $users, $calmonth = false, $calyear = false, $placement = false, |
186 | $courseid = false, $time = 0) { | |
17488fd3 | 187 | global $CFG, $OUTPUT, $PAGE; |
da304137 MN |
188 | |
189 | // Get the calendar type we are using. | |
190 | $calendartype = \core_calendar\type_factory::get_calendar_instance(); | |
7423f116 | 191 | |
dd97c328 | 192 | $display = new stdClass; |
da304137 MN |
193 | |
194 | // Assume we are not displaying this month for now. | |
195 | $display->thismonth = false; | |
7423f116 | 196 | |
197 | $content = ''; | |
198 | ||
da304137 MN |
199 | // Do this check for backwards compatibility. The core should be passing a timestamp rather than month and year. |
200 | // If a month and year are passed they will be in Gregorian. | |
201 | if (!empty($calmonth) && !empty($calyear)) { | |
202 | // Ensure it is a valid date, else we will just set it to the current timestamp. | |
203 | if (checkdate($calmonth, 1, $calyear)) { | |
204 | $time = make_timestamp($calyear, $calmonth, 1); | |
36dc3b71 | 205 | } else { |
da304137 MN |
206 | $time = time(); |
207 | } | |
208 | $date = usergetdate($time); | |
209 | if ($calmonth == $date['mon'] && $calyear == $date['year']) { | |
210 | $display->thismonth = true; | |
211 | } | |
212 | // We can overwrite date now with the date used by the calendar type, if it is not Gregorian, otherwise | |
213 | // there is no need as it is already in Gregorian. | |
214 | if ($calendartype->get_name() != 'gregorian') { | |
215 | $date = $calendartype->timestamp_to_date_array($time); | |
216 | } | |
217 | } else if (!empty($time)) { | |
218 | // Get the specified date in the calendar type being used. | |
219 | $date = $calendartype->timestamp_to_date_array($time); | |
220 | $thisdate = $calendartype->timestamp_to_date_array(time()); | |
221 | if ($date['month'] == $thisdate['month'] && $date['year'] == $thisdate['year']) { | |
222 | $display->thismonth = true; | |
223 | // If we are the current month we want to set the date to the current date, not the start of the month. | |
224 | $date = $thisdate; | |
7423f116 | 225 | } |
36dc3b71 | 226 | } else { |
da304137 MN |
227 | // Get the current date in the calendar type being used. |
228 | $time = time(); | |
229 | $date = $calendartype->timestamp_to_date_array($time); | |
7423f116 | 230 | $display->thismonth = true; |
231 | } | |
232 | ||
da304137 | 233 | list($d, $m, $y) = array($date['mday'], $date['mon'], $date['year']); // This is what we want to display. |
7423f116 | 234 | |
1032966c | 235 | // Get Gregorian date for the start of the month. |
da304137 MN |
236 | $gregoriandate = $calendartype->convert_to_gregorian($date['year'], $date['mon'], 1); |
237 | ||
1032966c | 238 | // Store the gregorian date values to be used later. |
da304137 MN |
239 | list($gy, $gm, $gd, $gh, $gmin) = array($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'], |
240 | $gregoriandate['hour'], $gregoriandate['minute']); | |
7423f116 | 241 | |
da304137 MN |
242 | // Get the max number of days in this month for this calendar type. |
243 | $display->maxdays = calendar_days_in_month($m, $y); | |
244 | // Get the starting week day for this month. | |
69244b91 | 245 | $startwday = dayofweek(1, $m, $y); |
da304137 MN |
246 | // Get the days in a week. |
247 | $daynames = calendar_get_days(); | |
248 | // Store the number of days in a week. | |
249 | $numberofdaysinweek = $calendartype->get_num_weekdays(); | |
250 | ||
251 | // Set the min and max weekday. | |
252 | $display->minwday = calendar_get_starting_weekday(); | |
253 | $display->maxwday = $display->minwday + ($numberofdaysinweek - 1); | |
254 | ||
255 | // These are used for DB queries, so we want unixtime, so we need to use Gregorian dates. | |
256 | $display->tstart = make_timestamp($gy, $gm, $gd, $gh, $gmin, 0); | |
ffd95d44 | 257 | $display->tend = $display->tstart + ($display->maxdays * DAYSECS) - 1; |
7423f116 | 258 | |
259 | // Align the starting weekday to fall in our display range | |
260 | // This is simple, not foolproof. | |
da304137 MN |
261 | if ($startwday < $display->minwday) { |
262 | $startwday += $numberofdaysinweek; | |
7423f116 | 263 | } |
264 | ||
265 | // Get the events matching our criteria. Don't forget to offset the timestamps for the user's TZ! | |
da304137 | 266 | $events = calendar_get_events($display->tstart, $display->tend, $users, $groups, $courses); |
7423f116 | 267 | |
b4892fa2 | 268 | // Set event course class for course events |
269 | if (!empty($events)) { | |
13534ef7 | 270 | foreach ($events as $eventid => $event) { |
13534ef7 ML |
271 | if (!empty($event->modulename)) { |
272 | $cm = get_coursemodule_from_instance($event->modulename, $event->instance); | |
8270f0d0 | 273 | if (!\core_availability\info_module::is_user_visible($cm, 0, false)) { |
13534ef7 ML |
274 | unset($events[$eventid]); |
275 | } | |
276 | } | |
b4892fa2 | 277 | } |
278 | } | |
6619eba4 | 279 | |
c635dcda | 280 | // This is either a genius idea or an idiot idea: in order to not complicate things, we use this rule: if, after |
9064751b | 281 | // possibly removing SITEID from $courses, there is only one course left, then clicking on a day in the month |
c635dcda | 282 | // will also set the $SESSION->cal_courses_shown variable to that one course. Otherwise, we 'd need to add extra |
283 | // arguments to this function. | |
0f927f1e | 284 | $hrefparams = array(); |
7bd1677c | 285 | if(!empty($courses)) { |
e749554e | 286 | $courses = array_diff($courses, array(SITEID)); |
7bd1677c | 287 | if(count($courses) == 1) { |
0f927f1e | 288 | $hrefparams['course'] = reset($courses); |
7bd1677c | 289 | } |
c635dcda | 290 | } |
291 | ||
7423f116 | 292 | // We want to have easy access by day, since the display is on a per-day basis. |
7c50db30 | 293 | calendar_events_by_day($events, $m, $y, $eventsbyday, $durationbyday, $typesbyday, $courses); |
7423f116 | 294 | |
da304137 MN |
295 | // Accessibility: added summary and <abbr> elements. |
296 | $summary = get_string('calendarheading', 'calendar', userdate($display->tstart, get_string('strftimemonthyear'))); | |
17488fd3 AN |
297 | // Begin table. |
298 | $content .= '<table class="minicalendar calendartable" summary="' . $summary . '">'; | |
da304137 MN |
299 | if (($placement !== false) && ($courseid !== false)) { |
300 | $content .= '<caption>'. calendar_top_controls($placement, array('id' => $courseid, 'time' => $time)) .'</caption>'; | |
512edd65 | 301 | } |
f136e4c5 | 302 | $content .= '<tr class="weekdays">'; // Header row: day names |
7423f116 | 303 | |
da304137 MN |
304 | // Print out the names of the weekdays. |
305 | for ($i = $display->minwday; $i <= $display->maxwday; ++$i) { | |
306 | $pos = $i % $numberofdaysinweek; | |
307 | $content .= '<th scope="col"><abbr title="'. $daynames[$pos]['fullname'] .'">'. | |
308 | $daynames[$pos]['shortname'] ."</abbr></th>\n"; | |
7423f116 | 309 | } |
310 | ||
f136e4c5 | 311 | $content .= '</tr><tr>'; // End of day names; prepare for day numbers |
7423f116 | 312 | |
313 | // For the table display. $week is the row; $dayweek is the column. | |
7423f116 | 314 | $dayweek = $startwday; |
315 | ||
316 | // Paddding (the first week may have blank days in the beginning) | |
317 | for($i = $display->minwday; $i < $startwday; ++$i) { | |
3bfd8bc8 | 318 | $content .= '<td class="dayblank"> </td>'."\n"; |
7423f116 | 319 | } |
320 | ||
797cedc7 SH |
321 | $weekend = CALENDAR_DEFAULT_WEEKEND; |
322 | if (isset($CFG->calendar_weekend)) { | |
323 | $weekend = intval($CFG->calendar_weekend); | |
324 | } | |
325 | ||
7423f116 | 326 | // Now display all the calendar |
91465b50 | 327 | $daytime = strtotime('-1 day', $display->tstart); |
7423f116 | 328 | for($day = 1; $day <= $display->maxdays; ++$day, ++$dayweek) { |
17488fd3 | 329 | $cellattributes = array(); |
91465b50 | 330 | $daytime = strtotime('+1 day', $daytime); |
7423f116 | 331 | if($dayweek > $display->maxwday) { |
332 | // We need to change week (table row) | |
d56d4e23 | 333 | $content .= '</tr><tr>'; |
7423f116 | 334 | $dayweek = $display->minwday; |
7423f116 | 335 | } |
92668ad2 | 336 | |
da304137 MN |
337 | // Reset vars. |
338 | if ($weekend & (1 << ($dayweek % $numberofdaysinweek))) { | |
7423f116 | 339 | // Weekend. This is true no matter what the exact range is. |
e2aa618b | 340 | $class = 'weekend day'; |
90723839 | 341 | } else { |
7423f116 | 342 | // Normal working day. |
e2aa618b | 343 | $class = 'day'; |
7423f116 | 344 | } |
345 | ||
4f2ff25e RW |
346 | $eventids = array(); |
347 | if (!empty($eventsbyday[$day])) { | |
348 | $eventids = $eventsbyday[$day]; | |
349 | } | |
350 | ||
351 | if (!empty($durationbyday[$day])) { | |
352 | $eventids = array_unique(array_merge($eventids, $durationbyday[$day])); | |
353 | } | |
354 | ||
6349d1e0 JSM |
355 | $finishclass = false; |
356 | ||
4f2ff25e | 357 | if (!empty($eventids)) { |
17488fd3 | 358 | // There is at least one event on this day. |
da304137 | 359 | |
90723839 | 360 | $class .= ' hasevent'; |
0f927f1e | 361 | $hrefparams['view'] = 'day'; |
da304137 | 362 | $dayhref = calendar_get_link_href(new moodle_url(CALENDAR_URL . 'view.php', $hrefparams), 0, 0, 0, $daytime); |
f434283f | 363 | |
7423f116 | 364 | $popupcontent = ''; |
6349d1e0 | 365 | foreach ($eventids as $eventid) { |
41d30a8e | 366 | if (!isset($events[$eventid])) { |
367 | continue; | |
4e17c6f3 | 368 | } |
07c03ff9 | 369 | $event = new calendar_event($events[$eventid]); |
0f927f1e SH |
370 | $popupalt = ''; |
371 | $component = 'moodle'; | |
da304137 | 372 | if (!empty($event->modulename)) { |
0f927f1e | 373 | $popupicon = 'icon'; |
41d30a8e | 374 | $popupalt = $event->modulename; |
0f927f1e | 375 | $component = $event->modulename; |
da304137 | 376 | } else if ($event->courseid == SITEID) { // Site event. |
5326675e | 377 | $popupicon = 'i/siteevent'; |
da304137 | 378 | } else if ($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) { // Course event. |
5326675e | 379 | $popupicon = 'i/courseevent'; |
da304137 | 380 | } else if ($event->groupid) { // Group event. |
5326675e | 381 | $popupicon = 'i/groupevent'; |
da304137 | 382 | } else { // Must be a user event. |
5326675e | 383 | $popupicon = 'i/userevent'; |
4e17c6f3 | 384 | } |
1d5bd3d2 | 385 | |
6349d1e0 JSM |
386 | if ($event->timeduration) { |
387 | $startdate = $calendartype->timestamp_to_date_array($event->timestart); | |
388 | $enddate = $calendartype->timestamp_to_date_array($event->timestart + $event->timeduration - 1); | |
389 | if ($enddate['mon'] == $m && $enddate['year'] == $y && $enddate['mday'] == $day) { | |
390 | $finishclass = true; | |
391 | } | |
392 | } | |
393 | ||
0f927f1e | 394 | $dayhref->set_anchor('event_'.$event->id); |
1d5bd3d2 | 395 | |
0f927f1e | 396 | $popupcontent .= html_writer::start_tag('div'); |
adc3cfc7 | 397 | $popupcontent .= $OUTPUT->pix_icon($popupicon, $popupalt, $component); |
e73b527c AA |
398 | // Show ical source if needed. |
399 | if (!empty($event->subscription) && $CFG->calendar_showicalsource) { | |
07c03ff9 | 400 | $a = new stdClass(); |
03af7e69 | 401 | $a->name = format_string($event->name, true); |
07c03ff9 AA |
402 | $a->source = $event->subscription->name; |
403 | $name = get_string('namewithsource', 'calendar', $a); | |
6349d1e0 JSM |
404 | } else { |
405 | if ($finishclass) { | |
406 | $samedate = $startdate['mon'] == $enddate['mon'] && | |
407 | $startdate['year'] == $enddate['year'] && | |
408 | $startdate['mday'] == $enddate['mday']; | |
409 | ||
410 | if ($samedate) { | |
411 | $name = format_string($event->name, true); | |
412 | } else { | |
413 | $name = format_string($event->name, true) . ' (' . get_string('eventendtime', 'calendar') . ')'; | |
414 | } | |
415 | } else { | |
416 | $name = format_string($event->name, true); | |
417 | } | |
07c03ff9 AA |
418 | } |
419 | $popupcontent .= html_writer::link($dayhref, $name); | |
0f927f1e | 420 | $popupcontent .= html_writer::end_tag('div'); |
7423f116 | 421 | } |
e295df44 | 422 | |
17488fd3 | 423 | if ($display->thismonth && $day == $d) { |
6349d1e0 | 424 | $popupdata = calendar_get_popup(true, $daytime, $popupcontent); |
b5c42e70 | 425 | } else { |
6349d1e0 | 426 | $popupdata = calendar_get_popup(false, $daytime, $popupcontent); |
e295df44 | 427 | } |
17488fd3 | 428 | $cellattributes = array_merge($cellattributes, $popupdata); |
f20c4d02 | 429 | |
7423f116 | 430 | // Class and cell content |
431 | if(isset($typesbyday[$day]['startglobal'])) { | |
90723839 SH |
432 | $class .= ' calendar_event_global'; |
433 | } else if(isset($typesbyday[$day]['startcourse'])) { | |
434 | $class .= ' calendar_event_course'; | |
435 | } else if(isset($typesbyday[$day]['startgroup'])) { | |
436 | $class .= ' calendar_event_group'; | |
437 | } else if(isset($typesbyday[$day]['startuser'])) { | |
438 | $class .= ' calendar_event_user'; | |
7423f116 | 439 | } |
6349d1e0 JSM |
440 | if ($finishclass) { |
441 | $class .= ' duration_finish'; | |
442 | } | |
17488fd3 | 443 | $cell = html_writer::link($dayhref, $day); |
90723839 | 444 | } else { |
05fcc5fd | 445 | $cell = $day; |
7423f116 | 446 | } |
447 | ||
90723839 SH |
448 | $durationclass = false; |
449 | if (isset($typesbyday[$day]['durationglobal'])) { | |
450 | $durationclass = ' duration_global'; | |
451 | } else if(isset($typesbyday[$day]['durationcourse'])) { | |
452 | $durationclass = ' duration_course'; | |
453 | } else if(isset($typesbyday[$day]['durationgroup'])) { | |
454 | $durationclass = ' duration_group'; | |
455 | } else if(isset($typesbyday[$day]['durationuser'])) { | |
456 | $durationclass = ' duration_user'; | |
7423f116 | 457 | } |
90723839 SH |
458 | if ($durationclass) { |
459 | $class .= ' duration '.$durationclass; | |
7423f116 | 460 | } |
461 | ||
b4892fa2 | 462 | // If event has a class set then add it to the table day <td> tag |
463 | // Note: only one colour for minicalendar | |
464 | if(isset($eventsbyday[$day])) { | |
465 | foreach($eventsbyday[$day] as $eventid) { | |
466 | if (!isset($events[$eventid])) { | |
467 | continue; | |
468 | } | |
469 | $event = $events[$eventid]; | |
470 | if (!empty($event->class)) { | |
471 | $class .= ' '.$event->class; | |
472 | } | |
473 | break; | |
474 | } | |
475 | } | |
476 | ||
17488fd3 AN |
477 | if ($display->thismonth && $day == $d) { |
478 | // The current cell is for today - add appropriate classes and additional information for styling. | |
edbe6c1b | 479 | $class .= ' today'; |
b5c42e70 | 480 | $today = get_string('today', 'calendar').' '.userdate(time(), get_string('strftimedayshort')); |
e295df44 | 481 | |
6349d1e0 | 482 | if (!isset($eventsbyday[$day]) && !isset($durationbyday[$day])) { |
450a0a7d | 483 | $class .= ' eventnone'; |
17488fd3 AN |
484 | $popupdata = calendar_get_popup(true, false); |
485 | $cellattributes = array_merge($cellattributes, $popupdata); | |
486 | $cell = html_writer::link('#', $day); | |
b5c42e70 | 487 | } |
17488fd3 | 488 | $cell = get_accesshide($today . ' ') . $cell; |
7423f116 | 489 | } |
490 | ||
491 | // Just display it | |
17488fd3 AN |
492 | $cellattributes['class'] = $class; |
493 | $content .= html_writer::tag('td', $cell, $cellattributes); | |
7423f116 | 494 | } |
495 | ||
496 | // Paddding (the last week may have blank days at the end) | |
497 | for($i = $dayweek; $i <= $display->maxwday; ++$i) { | |
3bfd8bc8 | 498 | $content .= '<td class="dayblank"> </td>'; |
7423f116 | 499 | } |
500 | $content .= '</tr>'; // Last row ends | |
501 | ||
f136e4c5 | 502 | $content .= '</table>'; // Tabular display of days ends |
7423f116 | 503 | |
17488fd3 AN |
504 | static $jsincluded = false; |
505 | if (!$jsincluded) { | |
506 | $PAGE->requires->yui_module('moodle-calendar-info', 'Y.M.core_calendar.info.init'); | |
507 | $jsincluded = true; | |
508 | } | |
7423f116 | 509 | return $content; |
510 | } | |
511 | ||
b5c42e70 | 512 | /** |
08b4a4e1 RW |
513 | * Gets the calendar popup |
514 | * | |
515 | * It called at multiple points in from calendar_get_mini. | |
516 | * Copied and modified from calendar_get_mini. | |
517 | * | |
518 | * @param bool $is_today false except when called on the current day. | |
519 | * @param mixed $event_timestart $events[$eventid]->timestart, OR false if there are no events. | |
7df3ea15 AA |
520 | * @param string $popupcontent content for the popup window/layout. |
521 | * @return string eventid for the calendar_tooltip popup window/layout. | |
b5c42e70 | 522 | */ |
17488fd3 | 523 | function calendar_get_popup($today = false, $timestart, $popupcontent = '') { |
458eb0d1 | 524 | global $PAGE; |
17488fd3 | 525 | |
b5c42e70 | 526 | $popupcaption = ''; |
17488fd3 AN |
527 | if ($today) { |
528 | $popupcaption = get_string('today', 'calendar') . ' '; | |
b5c42e70 | 529 | } |
17488fd3 AN |
530 | |
531 | if (false === $timestart) { | |
b5c42e70 | 532 | $popupcaption .= userdate(time(), get_string('strftimedayshort')); |
533 | $popupcontent = get_string('eventnone', 'calendar'); | |
450a0a7d | 534 | |
b5c42e70 | 535 | } else { |
17488fd3 | 536 | $popupcaption .= get_string('eventsfor', 'calendar', userdate($timestart, get_string('strftimedayshort'))); |
b5c42e70 | 537 | } |
bdbae764 | 538 | |
17488fd3 AN |
539 | return array( |
540 | 'data-core_calendar-title' => $popupcaption, | |
541 | 'data-core_calendar-popupcontent' => $popupcontent, | |
542 | ); | |
b5c42e70 | 543 | } |
544 | ||
08b4a4e1 RW |
545 | /** |
546 | * Gets the calendar upcoming event | |
547 | * | |
548 | * @param array $courses array of courses | |
549 | * @param array|int|bool $groups array of groups, group id or boolean for all/no group events | |
550 | * @param array|int|bool $users array of users, user id or boolean for all/no user events | |
551 | * @param int $daysinfuture number of days in the future we 'll look | |
552 | * @param int $maxevents maximum number of events | |
553 | * @param int $fromtime start time | |
554 | * @return array $output array of upcoming events | |
555 | */ | |
9958a08c | 556 | function calendar_get_upcoming($courses, $groups, $users, $daysinfuture, $maxevents, $fromtime=0) { |
62d11d77 | 557 | global $CFG, $COURSE, $DB; |
7423f116 | 558 | |
dd97c328 | 559 | $display = new stdClass; |
7423f116 | 560 | $display->range = $daysinfuture; // How many days in the future we 'll look |
561 | $display->maxevents = $maxevents; | |
562 | ||
563 | $output = array(); | |
564 | ||
565 | // Prepare "course caching", since it may save us a lot of queries | |
566 | $coursecache = array(); | |
567 | ||
568 | $processed = 0; | |
569 | $now = time(); // We 'll need this later | |
9d567178 | 570 | $usermidnighttoday = usergetmidnight($now); |
7423f116 | 571 | |
9958a08c | 572 | if ($fromtime) { |
573 | $display->tstart = $fromtime; | |
574 | } else { | |
9d567178 | 575 | $display->tstart = $usermidnighttoday; |
9958a08c | 576 | } |
7423f116 | 577 | |
1f473774 | 578 | // This works correctly with respect to the user's DST, but it is accurate |
579 | // only because $fromtime is always the exact midnight of some day! | |
580 | $display->tend = usergetmidnight($display->tstart + DAYSECS * $display->range + 3 * HOURSECS) - 1; | |
7423f116 | 581 | |
582 | // Get the events matching our criteria | |
8263f802 | 583 | $events = calendar_get_events($display->tstart, $display->tend, $users, $groups, $courses); |
7423f116 | 584 | |
c635dcda | 585 | // This is either a genius idea or an idiot idea: in order to not complicate things, we use this rule: if, after |
9064751b | 586 | // possibly removing SITEID from $courses, there is only one course left, then clicking on a day in the month |
c635dcda | 587 | // will also set the $SESSION->cal_courses_shown variable to that one course. Otherwise, we 'd need to add extra |
588 | // arguments to this function. | |
589 | ||
0f927f1e | 590 | $hrefparams = array(); |
7bd1677c | 591 | if(!empty($courses)) { |
e749554e | 592 | $courses = array_diff($courses, array(SITEID)); |
7bd1677c | 593 | if(count($courses) == 1) { |
0f927f1e | 594 | $hrefparams['course'] = reset($courses); |
7bd1677c | 595 | } |
c635dcda | 596 | } |
597 | ||
dd97c328 | 598 | if ($events !== false) { |
599 | ||
f20edd52 | 600 | $modinfo = get_fast_modinfo($COURSE); |
fa22fd5f | 601 | |
7423f116 | 602 | foreach($events as $event) { |
dc6cb74e | 603 | |
dd97c328 | 604 | |
605 | if (!empty($event->modulename)) { | |
606 | if ($event->courseid == $COURSE->id) { | |
607 | if (isset($modinfo->instances[$event->modulename][$event->instance])) { | |
608 | $cm = $modinfo->instances[$event->modulename][$event->instance]; | |
609 | if (!$cm->uservisible) { | |
610 | continue; | |
611 | } | |
612 | } | |
613 | } else { | |
614 | if (!$cm = get_coursemodule_from_instance($event->modulename, $event->instance)) { | |
dc6cb74e | 615 | continue; |
616 | } | |
8270f0d0 | 617 | if (!\core_availability\info_module::is_user_visible($cm, 0, false)) { |
dc6cb74e | 618 | continue; |
619 | } | |
dd97c328 | 620 | } |
dc6cb74e | 621 | } |
9d567178 | 622 | |
dd97c328 | 623 | if ($processed >= $display->maxevents) { |
9d567178 | 624 | break; |
625 | } | |
7423f116 | 626 | |
0f927f1e | 627 | $event->time = calendar_format_event_time($event, $now, $hrefparams); |
3c134875 | 628 | $output[] = $event; |
629 | ++$processed; | |
630 | } | |
631 | } | |
632 | return $output; | |
633 | } | |
7423f116 | 634 | |
f6a5ffe8 HB |
635 | |
636 | /** | |
f37c923d | 637 | * Get a HTML link to a course. |
f6a5ffe8 HB |
638 | * |
639 | * @param int $courseid the course id | |
640 | * @return string a link to the course (as HTML); empty if the course id is invalid | |
641 | */ | |
642 | function calendar_get_courselink($courseid) { | |
643 | ||
644 | if (!$courseid) { | |
645 | return ''; | |
646 | } | |
647 | ||
f37c923d | 648 | calendar_get_course_cached($coursecache, $courseid); |
f6a5ffe8 HB |
649 | $context = context_course::instance($courseid); |
650 | $fullname = format_string($coursecache[$courseid]->fullname, true, array('context' => $context)); | |
651 | $url = new moodle_url('/course/view.php', array('id' => $courseid)); | |
652 | $link = html_writer::link($url, $fullname); | |
653 | ||
654 | return $link; | |
655 | } | |
656 | ||
657 | ||
08b4a4e1 RW |
658 | /** |
659 | * Add calendar event metadata | |
660 | * | |
661 | * @param stdClass $event event info | |
662 | * @return stdClass $event metadata | |
663 | */ | |
9df8ff44 | 664 | function calendar_add_event_metadata($event) { |
6b608f8f | 665 | global $CFG, $OUTPUT; |
fb73f3b3 | 666 | |
e295df44 | 667 | //Support multilang in event->name |
fb73f3b3 | 668 | $event->name = format_string($event->name,true); |
e295df44 | 669 | |
3c134875 | 670 | if(!empty($event->modulename)) { // Activity event |
671 | // The module name is set. I will assume that it has to be displayed, and | |
672 | // also that it is an automatically-generated event. And of course that the | |
673 | // fields for get_coursemodule_from_instance are set correctly. | |
674 | $module = calendar_get_module_cached($coursecache, $event->modulename, $event->instance); | |
7423f116 | 675 | |
3c134875 | 676 | if ($module === false) { |
677 | return; | |
678 | } | |
9958a08c | 679 | |
3c134875 | 680 | $modulename = get_string('modulename', $event->modulename); |
a48bf079 DM |
681 | if (get_string_manager()->string_exists($event->eventtype, $event->modulename)) { |
682 | // will be used as alt text if the event icon | |
683 | $eventtype = get_string($event->eventtype, $event->modulename); | |
684 | } else { | |
685 | $eventtype = ''; | |
686 | } | |
b5d0cafc | 687 | $icon = $OUTPUT->pix_url('icon', $event->modulename) . ''; |
9958a08c | 688 | |
857c5f90 | 689 | $event->icon = '<img src="'.$icon.'" alt="'.$eventtype.'" title="'.$modulename.'" class="icon" />'; |
fb73f3b3 | 690 | $event->referer = '<a href="'.$CFG->wwwroot.'/mod/'.$event->modulename.'/view.php?id='.$module->id.'">'.$event->name.'</a>'; |
f6a5ffe8 | 691 | $event->courselink = calendar_get_courselink($module->course); |
3c134875 | 692 | $event->cmid = $module->id; |
9958a08c | 693 | |
3c134875 | 694 | } else if($event->courseid == SITEID) { // Site event |
857c5f90 | 695 | $event->icon = '<img src="'.$OUTPUT->pix_url('i/siteevent') . '" alt="'.get_string('globalevent', 'calendar').'" class="icon" />'; |
90723839 | 696 | $event->cssclass = 'calendar_event_global'; |
3c134875 | 697 | } else if($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) { // Course event |
857c5f90 | 698 | $event->icon = '<img src="'.$OUTPUT->pix_url('i/courseevent') . '" alt="'.get_string('courseevent', 'calendar').'" class="icon" />'; |
f6a5ffe8 | 699 | $event->courselink = calendar_get_courselink($event->courseid); |
90723839 | 700 | $event->cssclass = 'calendar_event_course'; |
3c134875 | 701 | } else if ($event->groupid) { // Group event |
c9653460 MG |
702 | if ($group = calendar_get_group_cached($event->groupid)) { |
703 | $groupname = format_string($group->name, true, context_course::instance($group->courseid)); | |
704 | } else { | |
705 | $groupname = ''; | |
706 | } | |
707 | $event->icon = html_writer::empty_tag('image', array('src' => $OUTPUT->pix_url('i/groupevent'), | |
708 | 'alt' => get_string('groupevent', 'calendar'), 'title' => $groupname, 'class' => 'icon')); | |
709 | $event->courselink = calendar_get_courselink($event->courseid) . ', ' . $groupname; | |
90723839 | 710 | $event->cssclass = 'calendar_event_group'; |
3c134875 | 711 | } else if($event->userid) { // User event |
857c5f90 | 712 | $event->icon = '<img src="'.$OUTPUT->pix_url('i/userevent') . '" alt="'.get_string('userevent', 'calendar').'" class="icon" />'; |
90723839 | 713 | $event->cssclass = 'calendar_event_user'; |
3c134875 | 714 | } |
9df8ff44 | 715 | return $event; |
e295df44 | 716 | } |
9df8ff44 | 717 | |
8263f802 | 718 | /** |
719 | * Get calendar events | |
08b4a4e1 | 720 | * |
8263f802 | 721 | * @param int $tstart Start time of time range for events |
08b4a4e1 RW |
722 | * @param int $tend End time of time range for events |
723 | * @param array|int|boolean $users array of users, user id or boolean for all/no user events | |
724 | * @param array|int|boolean $groups array of groups, group id or boolean for all/no group events | |
725 | * @param array|int|boolean $courses array of courses, course id or boolean for all/no course events | |
8263f802 | 726 | * @param boolean $withduration whether only events starting within time range selected |
727 | * or events in progress/already started selected as well | |
728 | * @param boolean $ignorehidden whether to select only visible events or all events | |
08b4a4e1 | 729 | * @return array $events of selected events or an empty array if there aren't any (or there was an error) |
8263f802 | 730 | */ |
f52459bb | 731 | function calendar_get_events($tstart, $tend, $users, $groups, $courses, $withduration = true, $ignorehidden = true) { |
62d11d77 | 732 | global $DB; |
733 | ||
a2ed10a5 | 734 | $params = array(); |
02c8a836 DB |
735 | // Quick test. |
736 | if (empty($users) && empty($groups) && empty($courses)) { | |
8263f802 | 737 | return array(); |
7423f116 | 738 | } |
482dbe0c | 739 | |
f52459bb JP |
740 | // Array of filter conditions. To be concatenated by the OR operator. |
741 | $filters = []; | |
742 | ||
743 | // User filter. | |
c3f51a3c | 744 | if ((is_array($users) && !empty($users)) or is_numeric($users)) { |
7423f116 | 745 | // Events from a number of users |
a2ed10a5 | 746 | list($insqlusers, $inparamsusers) = $DB->get_in_or_equal($users, SQL_PARAMS_NAMED); |
f52459bb | 747 | $filters[] = "(e.userid $insqlusers AND e.courseid = 0 AND e.groupid = 0)"; |
a2ed10a5 | 748 | $params = array_merge($params, $inparamsusers); |
f52459bb | 749 | } else if ($users === true) { |
7423f116 | 750 | // Events from ALL users |
f52459bb | 751 | $filters[] = "(e.userid != 0 AND e.courseid = 0 AND e.groupid = 0)"; |
f52f7413 | 752 | } |
f52459bb | 753 | // Boolean false (no users at all): We don't need to do anything. |
482dbe0c | 754 | |
f52459bb | 755 | // Group filter. |
c3f51a3c | 756 | if ((is_array($groups) && !empty($groups)) or is_numeric($groups)) { |
7423f116 | 757 | // Events from a number of groups |
a2ed10a5 | 758 | list($insqlgroups, $inparamsgroups) = $DB->get_in_or_equal($groups, SQL_PARAMS_NAMED); |
f52459bb | 759 | $filters[] = "e.groupid $insqlgroups"; |
a2ed10a5 | 760 | $params = array_merge($params, $inparamsgroups); |
f52459bb | 761 | } else if ($groups === true) { |
7423f116 | 762 | // Events from ALL groups |
f52459bb | 763 | $filters[] = "e.groupid != 0"; |
7423f116 | 764 | } |
f52459bb | 765 | // Boolean false (no groups at all): We don't need to do anything. |
482dbe0c | 766 | |
f52459bb | 767 | // Course filter. |
c3f51a3c | 768 | if ((is_array($courses) && !empty($courses)) or is_numeric($courses)) { |
a2ed10a5 | 769 | list($insqlcourses, $inparamscourses) = $DB->get_in_or_equal($courses, SQL_PARAMS_NAMED); |
f52459bb | 770 | $filters[] = "(e.groupid = 0 AND e.courseid $insqlcourses)"; |
a2ed10a5 | 771 | $params = array_merge($params, $inparamscourses); |
36dc3b71 | 772 | } else if ($courses === true) { |
7423f116 | 773 | // Events from ALL courses |
f52459bb | 774 | $filters[] = "(e.groupid = 0 AND e.courseid != 0)"; |
7423f116 | 775 | } |
8c165fe9 | 776 | |
482dbe0c | 777 | // Security check: if, by now, we have NOTHING in $whereclause, then it means |
778 | // that NO event-selecting clauses were defined. Thus, we won't be returning ANY | |
779 | // events no matter what. Allowing the code to proceed might return a completely | |
780 | // valid query with only time constraints, thus selecting ALL events in that time frame! | |
f52459bb | 781 | if (empty($filters)) { |
8263f802 | 782 | return array(); |
482dbe0c | 783 | } |
784 | ||
f52459bb JP |
785 | // Build our clause for the filters. |
786 | $filterclause = implode(' OR ', $filters); | |
787 | ||
788 | // Array of where conditions for our query. To be concatenated by the AND operator. | |
789 | $whereconditions = ["($filterclause)"]; | |
790 | ||
791 | // Time clause. | |
792 | if ($withduration) { | |
793 | $timeclause = "((e.timestart >= :tstart1 OR e.timestart + e.timeduration > :tstart2) AND e.timestart <= :tend)"; | |
794 | $params['tstart1'] = $tstart; | |
795 | $params['tstart2'] = $tstart; | |
796 | $params['tend'] = $tend; | |
797 | } else { | |
798 | $timeclause = "(e.timestart >= :tstart AND e.timestart <= :tend)"; | |
799 | $params['tstart'] = $tstart; | |
800 | $params['tend'] = $tend; | |
7423f116 | 801 | } |
f52459bb JP |
802 | $whereconditions[] = $timeclause; |
803 | ||
804 | // Show visible only. | |
805 | if ($ignorehidden) { | |
806 | $whereconditions[] = "(e.visible = 1)"; | |
7423f116 | 807 | } |
f52459bb JP |
808 | |
809 | // Build the main query's WHERE clause. | |
810 | $whereclause = implode(' AND ', $whereconditions); | |
811 | ||
812 | // Build SQL subquery and conditions for filtered events based on priorities. | |
813 | $subquerywhere = ''; | |
814 | $subqueryconditions = []; | |
815 | ||
816 | // Get the user's courses. Otherwise, get the default courses being shown by the calendar. | |
817 | $usercourses = calendar_get_default_courses(); | |
818 | ||
819 | // Set calendar filters. | |
820 | list($usercourses, $usergroups, $user) = calendar_set_filters($usercourses, true); | |
821 | $subqueryparams = []; | |
822 | ||
823 | // Flag to indicate whether the query needs to exclude group overrides. | |
824 | $viewgroupsonly = false; | |
825 | ||
826 | if ($user) { | |
827 | // Set filter condition for the user's events. | |
828 | $subqueryconditions[] = "(ev.userid = :user AND ev.courseid = 0 AND ev.groupid = 0)"; | |
829 | $subqueryparams['user'] = $user; | |
830 | ||
831 | foreach ($usercourses as $courseid) { | |
832 | if (has_capability('moodle/site:accessallgroups', context_course::instance($courseid))) { | |
833 | $usergroupmembership = groups_get_all_groups($courseid, $user, 0, 'g.id'); | |
834 | if (count($usergroupmembership) == 0) { | |
835 | $viewgroupsonly = true; | |
836 | break; | |
837 | } | |
838 | } | |
839 | } | |
7423f116 | 840 | } |
f52459bb JP |
841 | |
842 | // Set filter condition for the user's group events. | |
843 | if ($usergroups === true || $viewgroupsonly) { | |
844 | // Fetch group events, but not group overrides. | |
845 | $subqueryconditions[] = "(ev.groupid != 0 AND ev.eventtype = 'group')"; | |
846 | } else if (!empty($usergroups)) { | |
847 | // Fetch group events and group overrides. | |
848 | list($inusergroups, $inusergroupparams) = $DB->get_in_or_equal($usergroups, SQL_PARAMS_NAMED); | |
849 | $subqueryconditions[] = "(ev.groupid $inusergroups)"; | |
850 | $subqueryparams = array_merge($subqueryparams, $inusergroupparams); | |
7423f116 | 851 | } |
f52f7413 | 852 | |
f1131695 JP |
853 | // Get courses to be used for the subquery. |
854 | $subquerycourses = []; | |
855 | if (is_array($courses)) { | |
856 | $subquerycourses = $courses; | |
857 | } else if (is_numeric($courses)) { | |
858 | $subquerycourses[] = $courses; | |
859 | } | |
860 | // Merge with user courses, if necessary. | |
f52459bb | 861 | if (!empty($usercourses)) { |
f1131695 JP |
862 | $subquerycourses = array_merge($subquerycourses, $usercourses); |
863 | // Make sure we remove duplicate values. | |
864 | $subquerycourses = array_unique($subquerycourses); | |
865 | } | |
866 | ||
867 | // Set subquery filter condition for the courses. | |
868 | if (!empty($subquerycourses)) { | |
869 | list($incourses, $incoursesparams) = $DB->get_in_or_equal($subquerycourses, SQL_PARAMS_NAMED); | |
870 | $subqueryconditions[] = "(ev.groupid = 0 AND ev.courseid $incourses)"; | |
871 | $subqueryparams = array_merge($subqueryparams, $incoursesparams); | |
0ad072de | 872 | } |
873 | ||
f52459bb JP |
874 | // Build the WHERE condition for the sub-query. |
875 | if (!empty($subqueryconditions)) { | |
876 | $subquerywhere = 'WHERE ' . implode(" OR ", $subqueryconditions); | |
877 | } | |
878 | ||
879 | // Merge subquery parameters to the parameters of the main query. | |
880 | if (!empty($subqueryparams)) { | |
881 | $params = array_merge($params, $subqueryparams); | |
882 | } | |
883 | ||
884 | // Sub-query that fetches the list of unique events that were filtered based on priority. | |
885 | $subquery = "SELECT ev.modulename, | |
886 | ev.instance, | |
887 | ev.eventtype, | |
888 | MAX(ev.priority) as priority | |
889 | FROM {event} ev | |
890 | $subquerywhere | |
891 | GROUP BY ev.modulename, ev.instance, ev.eventtype"; | |
892 | ||
893 | // Build the main query. | |
7a33494d AG |
894 | $sql = "SELECT e.* |
895 | FROM {event} e | |
f52459bb JP |
896 | INNER JOIN ($subquery) fe |
897 | ON e.modulename = fe.modulename | |
898 | AND e.instance = fe.instance | |
899 | AND e.eventtype = fe.eventtype | |
900 | AND (e.priority = fe.priority OR (e.priority IS NULL AND fe.priority IS NULL)) | |
901 | LEFT JOIN {modules} m | |
902 | ON e.modulename = m.name | |
7a33494d AG |
903 | WHERE (m.visible = 1 OR m.visible IS NULL) AND $whereclause |
904 | ORDER BY e.timestart"; | |
905 | $events = $DB->get_records_sql($sql, $params); | |
906 | ||
8263f802 | 907 | if ($events === false) { |
908 | $events = array(); | |
909 | } | |
910 | return $events; | |
7423f116 | 911 | } |
912 | ||
475896bd AA |
913 | /** Get calendar events by id |
914 | * | |
915 | * @since Moodle 2.5 | |
916 | * @param array $eventids list of event ids | |
917 | * @return array Array of event entries, empty array if nothing found | |
918 | */ | |
919 | ||
920 | function calendar_get_events_by_id($eventids) { | |
921 | global $DB; | |
922 | ||
923 | if (!is_array($eventids) || empty($eventids)) { | |
924 | return array(); | |
925 | } | |
926 | list($wheresql, $params) = $DB->get_in_or_equal($eventids); | |
927 | $wheresql = "id $wheresql"; | |
928 | ||
929 | return $DB->get_records_select('event', $wheresql, $params); | |
930 | } | |
931 | ||
08b4a4e1 RW |
932 | /** |
933 | * Get control options for Calendar | |
934 | * | |
935 | * @param string $type of calendar | |
936 | * @param array $data calendar information | |
937 | * @return string $content return available control for the calender in html | |
938 | */ | |
7423f116 | 939 | function calendar_top_controls($type, $data) { |
1d3972ad | 940 | global $PAGE, $OUTPUT; |
da304137 MN |
941 | |
942 | // Get the calendar type we are using. | |
943 | $calendartype = \core_calendar\type_factory::get_calendar_instance(); | |
944 | ||
7423f116 | 945 | $content = ''; |
5147ad48 | 946 | |
da304137 | 947 | // Ensure course id passed if relevant. |
f21ed0f3 | 948 | $courseid = ''; |
949 | if (!empty($data['id'])) { | |
950 | $courseid = '&course='.$data['id']; | |
951 | } | |
952 | ||
da304137 MN |
953 | // If we are passing a month and year then we need to convert this to a timestamp to |
954 | // support multiple calendars. No where in core should these be passed, this logic | |
955 | // here is for third party plugins that may use this function. | |
956 | if (!empty($data['m']) && !empty($date['y'])) { | |
957 | if (!isset($data['d'])) { | |
958 | $data['d'] = 1; | |
959 | } | |
960 | if (!checkdate($data['m'], $data['d'], $data['y'])) { | |
961 | $time = time(); | |
962 | } else { | |
963 | $time = make_timestamp($data['y'], $data['m'], $data['d']); | |
964 | } | |
965 | } else if (!empty($data['time'])) { | |
966 | $time = $data['time']; | |
967 | } else { | |
5147ad48 | 968 | $time = time(); |
969 | } | |
e295df44 | 970 | |
da304137 MN |
971 | // Get the date for the calendar type. |
972 | $date = $calendartype->timestamp_to_date_array($time); | |
973 | ||
769c73ae | 974 | $urlbase = $PAGE->url; |
7423f116 | 975 | |
da304137 MN |
976 | // We need to get the previous and next months in certain cases. |
977 | if ($type == 'frontpage' || $type == 'course' || $type == 'month') { | |
978 | $prevmonth = calendar_sub_month($date['mon'], $date['year']); | |
1032966c MN |
979 | $prevmonthtime = $calendartype->convert_to_gregorian($prevmonth[1], $prevmonth[0], 1); |
980 | $prevmonthtime = make_timestamp($prevmonthtime['year'], $prevmonthtime['month'], $prevmonthtime['day'], | |
981 | $prevmonthtime['hour'], $prevmonthtime['minute']); | |
da304137 MN |
982 | |
983 | $nextmonth = calendar_add_month($date['mon'], $date['year']); | |
1032966c MN |
984 | $nextmonthtime = $calendartype->convert_to_gregorian($nextmonth[1], $nextmonth[0], 1); |
985 | $nextmonthtime = make_timestamp($nextmonthtime['year'], $nextmonthtime['month'], $nextmonthtime['day'], | |
986 | $nextmonthtime['hour'], $nextmonthtime['minute']); | |
da304137 | 987 | } |
2a06efcc | 988 | |
da304137 | 989 | switch ($type) { |
7423f116 | 990 | case 'frontpage': |
da304137 MN |
991 | $prevlink = calendar_get_link_previous(get_string('monthprev', 'access'), $urlbase, false, false, false, true, $prevmonthtime); |
992 | $nextlink = calendar_get_link_next(get_string('monthnext', 'access'), $urlbase, false, false, false, true, $nextmonthtime); | |
993 | $calendarlink = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', array('view' => 'month')), false, false, false, $time); | |
ae53bd1d | 994 | |
0f927f1e SH |
995 | if (!empty($data['id'])) { |
996 | $calendarlink->param('course', $data['id']); | |
997 | } | |
998 | ||
ed7844b5 FM |
999 | $prevlink = $prevlink; |
1000 | $right = $nextlink; | |
ae53bd1d | 1001 | |
0f927f1e | 1002 | $content .= html_writer::start_tag('div', array('class'=>'calendar-controls')); |
ed7844b5 | 1003 | $content .= $prevlink.'<span class="hide"> | </span>'; |
e71e340d | 1004 | $content .= html_writer::tag('span', html_writer::link($calendarlink, userdate($time, get_string('strftimemonthyear')), array('title'=>get_string('monththis','calendar'))), array('class'=>'current')); |
0f927f1e | 1005 | $content .= '<span class="hide"> | </span>'. $right; |
6694ff84 | 1006 | $content .= "<span class=\"clearer\"><!-- --></span>\n"; |
0f927f1e SH |
1007 | $content .= html_writer::end_tag('div'); |
1008 | ||
1009 | break; | |
7423f116 | 1010 | case 'course': |
da304137 MN |
1011 | $prevlink = calendar_get_link_previous(get_string('monthprev', 'access'), $urlbase, false, false, false, true, $prevmonthtime); |
1012 | $nextlink = calendar_get_link_next(get_string('monthnext', 'access'), $urlbase, false, false, false, true, $nextmonthtime); | |
1013 | $calendarlink = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', array('view' => 'month')), false, false, false, $time); | |
0f927f1e | 1014 | |
0f927f1e SH |
1015 | if (!empty($data['id'])) { |
1016 | $calendarlink->param('course', $data['id']); | |
1017 | } | |
1018 | ||
0f927f1e | 1019 | $content .= html_writer::start_tag('div', array('class'=>'calendar-controls')); |
ed7844b5 | 1020 | $content .= $prevlink.'<span class="hide"> | </span>'; |
e71e340d | 1021 | $content .= html_writer::tag('span', html_writer::link($calendarlink, userdate($time, get_string('strftimemonthyear')), array('title'=>get_string('monththis','calendar'))), array('class'=>'current')); |
ed7844b5 | 1022 | $content .= '<span class="hide"> | </span>'. $nextlink; |
0f927f1e SH |
1023 | $content .= "<span class=\"clearer\"><!-- --></span>"; |
1024 | $content .= html_writer::end_tag('div'); | |
1025 | break; | |
7423f116 | 1026 | case 'upcoming': |
da304137 | 1027 | $calendarlink = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', array('view' => 'upcoming')), false, false, false, $time); |
0f927f1e SH |
1028 | if (!empty($data['id'])) { |
1029 | $calendarlink->param('course', $data['id']); | |
1030 | } | |
1031 | $calendarlink = html_writer::link($calendarlink, userdate($time, get_string('strftimemonthyear'))); | |
1032 | $content .= html_writer::tag('div', $calendarlink, array('class'=>'centered')); | |
1033 | break; | |
7423f116 | 1034 | case 'display': |
da304137 | 1035 | $calendarlink = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', array('view' => 'month')), false, false, false, $time); |
0f927f1e SH |
1036 | if (!empty($data['id'])) { |
1037 | $calendarlink->param('course', $data['id']); | |
1038 | } | |
1039 | $calendarlink = html_writer::link($calendarlink, userdate($time, get_string('strftimemonthyear'))); | |
1040 | $content .= html_writer::tag('h3', $calendarlink); | |
1041 | break; | |
7423f116 | 1042 | case 'month': |
da304137 MN |
1043 | $prevlink = calendar_get_link_previous(userdate($prevmonthtime, get_string('strftimemonthyear')), 'view.php?view=month'.$courseid.'&', false, false, false, false, $prevmonthtime); |
1044 | $nextlink = calendar_get_link_next(userdate($nextmonthtime, get_string('strftimemonthyear')), 'view.php?view=month'.$courseid.'&', false, false, false, false, $nextmonthtime); | |
0f927f1e | 1045 | |
0f927f1e | 1046 | $content .= html_writer::start_tag('div', array('class'=>'calendar-controls')); |
ed7844b5 | 1047 | $content .= $prevlink . '<span class="hide"> | </span>'; |
1d3972ad | 1048 | $content .= $OUTPUT->heading(userdate($time, get_string('strftimemonthyear')), 2, 'current'); |
ed7844b5 | 1049 | $content .= '<span class="hide"> | </span>' . $nextlink; |
0f927f1e SH |
1050 | $content .= '<span class="clearer"><!-- --></span>'; |
1051 | $content .= html_writer::end_tag('div')."\n"; | |
1052 | break; | |
7423f116 | 1053 | case 'day': |
797cedc7 | 1054 | $days = calendar_get_days(); |
da304137 | 1055 | |
a0ef87de MG |
1056 | $prevtimestamp = strtotime('-1 day', $time); |
1057 | $nexttimestamp = strtotime('+1 day', $time); | |
da304137 MN |
1058 | |
1059 | $prevdate = $calendartype->timestamp_to_date_array($prevtimestamp); | |
1060 | $nextdate = $calendartype->timestamp_to_date_array($nexttimestamp); | |
1061 | ||
1062 | $prevname = $days[$prevdate['wday']]['fullname']; | |
1063 | $nextname = $days[$nextdate['wday']]['fullname']; | |
1064 | $prevlink = calendar_get_link_previous($prevname, 'view.php?view=day'.$courseid.'&', false, false, false, false, $prevtimestamp); | |
1065 | $nextlink = calendar_get_link_next($nextname, 'view.php?view=day'.$courseid.'&', false, false, false, false, $nexttimestamp); | |
0f927f1e | 1066 | |
0f927f1e | 1067 | $content .= html_writer::start_tag('div', array('class'=>'calendar-controls')); |
ed7844b5 | 1068 | $content .= $prevlink; |
0f927f1e | 1069 | $content .= '<span class="hide"> | </span><span class="current">'.userdate($time, get_string('strftimedaydate')).'</span>'; |
ed7844b5 | 1070 | $content .= '<span class="hide"> | </span>'. $nextlink; |
0f927f1e SH |
1071 | $content .= "<span class=\"clearer\"><!-- --></span>"; |
1072 | $content .= html_writer::end_tag('div')."\n"; | |
1073 | ||
1074 | break; | |
7423f116 | 1075 | } |
1076 | return $content; | |
1077 | } | |
1078 | ||
ef21ad21 FM |
1079 | /** |
1080 | * Formats a filter control element. | |
1081 | * | |
1082 | * @param moodle_url $url of the filter | |
1083 | * @param int $type constant defining the type filter | |
1084 | * @return string html content of the element | |
1085 | */ | |
1086 | function calendar_filter_controls_element(moodle_url $url, $type) { | |
1087 | global $OUTPUT; | |
1088 | switch ($type) { | |
1089 | case CALENDAR_EVENT_GLOBAL: | |
1090 | $typeforhumans = 'global'; | |
1091 | $class = 'calendar_event_global'; | |
1092 | break; | |
1093 | case CALENDAR_EVENT_COURSE: | |
1094 | $typeforhumans = 'course'; | |
1095 | $class = 'calendar_event_course'; | |
1096 | break; | |
1097 | case CALENDAR_EVENT_GROUP: | |
1098 | $typeforhumans = 'groups'; | |
1099 | $class = 'calendar_event_group'; | |
1100 | break; | |
1101 | case CALENDAR_EVENT_USER: | |
1102 | $typeforhumans = 'user'; | |
1103 | $class = 'calendar_event_user'; | |
1104 | break; | |
1105 | } | |
1106 | if (calendar_show_event_type($type)) { | |
1107 | $icon = $OUTPUT->pix_icon('t/hide', get_string('hide')); | |
1108 | $str = get_string('hide'.$typeforhumans.'events', 'calendar'); | |
1109 | } else { | |
1110 | $icon = $OUTPUT->pix_icon('t/show', get_string('show')); | |
1111 | $str = get_string('show'.$typeforhumans.'events', 'calendar'); | |
1112 | } | |
1113 | $content = html_writer::start_tag('li', array('class' => 'calendar_event')); | |
6cc38fb6 | 1114 | $content .= html_writer::start_tag('a', array('href' => $url, 'rel' => 'nofollow')); |
ef21ad21 FM |
1115 | $content .= html_writer::tag('span', $icon, array('class' => $class)); |
1116 | $content .= html_writer::tag('span', $str, array('class' => 'eventname')); | |
1117 | $content .= html_writer::end_tag('a'); | |
1118 | $content .= html_writer::end_tag('li'); | |
1119 | return $content; | |
1120 | } | |
1121 | ||
08b4a4e1 RW |
1122 | /** |
1123 | * Get the controls filter for calendar. | |
1124 | * | |
1125 | * Filter is used to hide calendar info from the display page | |
1126 | * | |
1127 | * @param moodle_url $returnurl return-url for filter controls | |
1128 | * @return string $content return filter controls in html | |
1129 | */ | |
797cedc7 SH |
1130 | function calendar_filter_controls(moodle_url $returnurl) { |
1131 | global $CFG, $USER, $OUTPUT; | |
7423f116 | 1132 | |
48f508ab | 1133 | $groupevents = true; |
7f4d18fc | 1134 | $id = optional_param( 'id',0,PARAM_INT ); |
abc5f845 | 1135 | $seturl = new moodle_url('/calendar/set.php', array('return' => base64_encode($returnurl->out_as_local_url(false)), 'sesskey'=>sesskey())); |
ef21ad21 | 1136 | $content = html_writer::start_tag('ul'); |
797cedc7 SH |
1137 | |
1138 | $seturl->param('var', 'showglobal'); | |
ef21ad21 | 1139 | $content .= calendar_filter_controls_element($seturl, CALENDAR_EVENT_GLOBAL); |
aa6c1ced | 1140 | |
797cedc7 | 1141 | $seturl->param('var', 'showcourses'); |
ef21ad21 | 1142 | $content .= calendar_filter_controls_element($seturl, CALENDAR_EVENT_COURSE); |
48f508ab | 1143 | |
4f0c2d00 | 1144 | if (isloggedin() && !isguestuser()) { |
797cedc7 | 1145 | if ($groupevents) { |
43c3ffbe | 1146 | // This course MIGHT have group events defined, so show the filter |
797cedc7 | 1147 | $seturl->param('var', 'showgroups'); |
ef21ad21 | 1148 | $content .= calendar_filter_controls_element($seturl, CALENDAR_EVENT_GROUP); |
e3bb6401 | 1149 | } else { |
43c3ffbe | 1150 | // This course CANNOT have group events, so lose the filter |
fdbffa54 | 1151 | } |
797cedc7 | 1152 | $seturl->param('var', 'showuser'); |
ef21ad21 | 1153 | $content .= calendar_filter_controls_element($seturl, CALENDAR_EVENT_USER); |
48f508ab | 1154 | } |
ef21ad21 | 1155 | $content .= html_writer::end_tag('ul'); |
48f508ab | 1156 | |
7423f116 | 1157 | return $content; |
1158 | } | |
1159 | ||
08b4a4e1 RW |
1160 | /** |
1161 | * Return the representation day | |
1162 | * | |
1163 | * @param int $tstamp Timestamp in GMT | |
1164 | * @param int $now current Unix timestamp | |
1165 | * @param bool $usecommonwords | |
1166 | * @return string the formatted date/time | |
1167 | */ | |
7423f116 | 1168 | function calendar_day_representation($tstamp, $now = false, $usecommonwords = true) { |
1169 | ||
0ef7c973 | 1170 | static $shortformat; |
1171 | if(empty($shortformat)) { | |
e70fdac0 | 1172 | $shortformat = get_string('strftimedayshort'); |
0ef7c973 | 1173 | } |
1174 | ||
7423f116 | 1175 | if($now === false) { |
1176 | $now = time(); | |
1177 | } | |
1178 | ||
1179 | // To have it in one place, if a change is needed | |
e70fdac0 | 1180 | $formal = userdate($tstamp, $shortformat); |
7423f116 | 1181 | |
7b38bfa6 | 1182 | $datestamp = usergetdate($tstamp); |
1183 | $datenow = usergetdate($now); | |
7423f116 | 1184 | |
1185 | if($usecommonwords == false) { | |
1186 | // We don't want words, just a date | |
1187 | return $formal; | |
1188 | } | |
7b38bfa6 | 1189 | else if($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday']) { |
7423f116 | 1190 | // Today |
1191 | return get_string('today', 'calendar'); | |
1192 | } | |
7b38bfa6 | 1193 | else if( |
1194 | ($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday'] - 1 ) || | |
1195 | ($datestamp['year'] == $datenow['year'] - 1 && $datestamp['mday'] == 31 && $datestamp['mon'] == 12 && $datenow['yday'] == 1) | |
1196 | ) { | |
7423f116 | 1197 | // Yesterday |
1198 | return get_string('yesterday', 'calendar'); | |
1199 | } | |
7b38bfa6 | 1200 | else if( |
1201 | ($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday'] + 1 ) || | |
1202 | ($datestamp['year'] == $datenow['year'] + 1 && $datenow['mday'] == 31 && $datenow['mon'] == 12 && $datestamp['yday'] == 1) | |
1203 | ) { | |
7423f116 | 1204 | // Tomorrow |
1205 | return get_string('tomorrow', 'calendar'); | |
1206 | } | |
1207 | else { | |
1208 | return $formal; | |
1209 | } | |
1210 | } | |
1211 | ||
08b4a4e1 RW |
1212 | /** |
1213 | * return the formatted representation time | |
1214 | * | |
1215 | * @param int $time the timestamp in UTC, as obtained from the database | |
1216 | * @return string the formatted date/time | |
1217 | */ | |
7423f116 | 1218 | function calendar_time_representation($time) { |
1b0ebe79 | 1219 | static $langtimeformat = NULL; |
1220 | if($langtimeformat === NULL) { | |
1221 | $langtimeformat = get_string('strftimetime'); | |
1222 | } | |
1223 | $timeformat = get_user_preferences('calendar_timeformat'); | |
c7dd2550 | 1224 | if(empty($timeformat)){ |
1225 | $timeformat = get_config(NULL,'calendar_site_timeformat'); | |
1226 | } | |
1b0ebe79 | 1227 | // The ? is needed because the preference might be present, but empty |
1228 | return userdate($time, empty($timeformat) ? $langtimeformat : $timeformat); | |
7423f116 | 1229 | } |
1230 | ||
e295df44 | 1231 | /** |
14236cbd SH |
1232 | * Adds day, month, year arguments to a URL and returns a moodle_url object. |
1233 | * | |
1234 | * @param string|moodle_url $linkbase | |
08b4a4e1 RW |
1235 | * @param int $d The number of the day. |
1236 | * @param int $m The number of the month. | |
1237 | * @param int $y The number of the year. | |
da304137 MN |
1238 | * @param int $time the unixtime, used for multiple calendar support. The values $d, |
1239 | * $m and $y are kept for backwards compatibility. | |
08b4a4e1 | 1240 | * @return moodle_url|null $linkbase |
e295df44 | 1241 | */ |
da304137 | 1242 | function calendar_get_link_href($linkbase, $d, $m, $y, $time = 0) { |
0f927f1e SH |
1243 | if (empty($linkbase)) { |
1244 | return ''; | |
1245 | } | |
1246 | if (!($linkbase instanceof moodle_url)) { | |
e30390a0 | 1247 | $linkbase = new moodle_url($linkbase); |
0f927f1e | 1248 | } |
da304137 MN |
1249 | |
1250 | // If a day, month and year were passed then convert it to a timestamp. If these were passed | |
1251 | // then we can assume the day, month and year are passed as Gregorian, as no where in core | |
1252 | // should we be passing these values rather than the time. | |
1253 | if (!empty($d) && !empty($m) && !empty($y)) { | |
1254 | if (checkdate($m, $d, $y)) { | |
1255 | $time = make_timestamp($y, $m, $d); | |
1256 | } else { | |
1257 | $time = time(); | |
1258 | } | |
1259 | } else if (empty($time)) { | |
1260 | $time = time(); | |
0f927f1e | 1261 | } |
da304137 MN |
1262 | |
1263 | $linkbase->param('time', $time); | |
1264 | ||
0f927f1e | 1265 | return $linkbase; |
7423f116 | 1266 | } |
1267 | ||
a84dea2c | 1268 | /** |
1269 | * Build and return a previous month HTML link, with an arrow. | |
14236cbd | 1270 | * |
a84dea2c | 1271 | * @param string $text The text label. |
14236cbd | 1272 | * @param string|moodle_url $linkbase The URL stub. |
08b4a4e1 RW |
1273 | * @param int $d The number of the date. |
1274 | * @param int $m The number of the month. | |
1275 | * @param int $y year The number of the year. | |
a84dea2c | 1276 | * @param bool $accesshide Default visible, or hide from all except screenreaders. |
da304137 MN |
1277 | * @param int $time the unixtime, used for multiple calendar support. The values $d, |
1278 | * $m and $y are kept for backwards compatibility. | |
a84dea2c | 1279 | * @return string HTML string. |
1280 | */ | |
da304137 MN |
1281 | function calendar_get_link_previous($text, $linkbase, $d, $m, $y, $accesshide = false, $time = 0) { |
1282 | $href = calendar_get_link_href(new moodle_url($linkbase), $d, $m, $y, $time); | |
0f927f1e SH |
1283 | if (empty($href)) { |
1284 | return $text; | |
1285 | } | |
1286 | return link_arrow_left($text, (string)$href, $accesshide, 'previous'); | |
a84dea2c | 1287 | } |
1288 | ||
1289 | /** | |
1290 | * Build and return a next month HTML link, with an arrow. | |
1d5bd3d2 | 1291 | * |
a84dea2c | 1292 | * @param string $text The text label. |
14236cbd | 1293 | * @param string|moodle_url $linkbase The URL stub. |
08b4a4e1 RW |
1294 | * @param int $d the number of the Day |
1295 | * @param int $m The number of the month. | |
1296 | * @param int $y The number of the year. | |
a84dea2c | 1297 | * @param bool $accesshide Default visible, or hide from all except screenreaders. |
da304137 MN |
1298 | * @param int $time the unixtime, used for multiple calendar support. The values $d, |
1299 | * $m and $y are kept for backwards compatibility. | |
a84dea2c | 1300 | * @return string HTML string. |
1301 | */ | |
da304137 MN |
1302 | function calendar_get_link_next($text, $linkbase, $d, $m, $y, $accesshide = false, $time = 0) { |
1303 | $href = calendar_get_link_href(new moodle_url($linkbase), $d, $m, $y, $time); | |
0f927f1e SH |
1304 | if (empty($href)) { |
1305 | return $text; | |
1306 | } | |
1307 | return link_arrow_right($text, (string)$href, $accesshide, 'next'); | |
a84dea2c | 1308 | } |
1309 | ||
08b4a4e1 RW |
1310 | /** |
1311 | * Return the name of the weekday | |
1312 | * | |
1313 | * @param string $englishname | |
1314 | * @return string of the weekeday | |
1315 | */ | |
7423f116 | 1316 | function calendar_wday_name($englishname) { |
1317 | return get_string(strtolower($englishname), 'calendar'); | |
1318 | } | |
1319 | ||
08b4a4e1 RW |
1320 | /** |
1321 | * Return the number of days in month | |
1322 | * | |
1323 | * @param int $month the number of the month. | |
1324 | * @param int $year the number of the year | |
1325 | * @return int | |
1326 | */ | |
7423f116 | 1327 | function calendar_days_in_month($month, $year) { |
da304137 MN |
1328 | $calendartype = \core_calendar\type_factory::get_calendar_instance(); |
1329 | return $calendartype->get_num_days_in_month($year, $month); | |
7423f116 | 1330 | } |
1331 | ||
08b4a4e1 RW |
1332 | /** |
1333 | * Get the upcoming event block | |
1334 | * | |
1335 | * @param array $events list of events | |
1336 | * @param moodle_url|string $linkhref link to event referer | |
f6a5ffe8 | 1337 | * @param boolean $showcourselink whether links to courses should be shown |
08b4a4e1 RW |
1338 | * @return string|null $content html block content |
1339 | */ | |
f6a5ffe8 | 1340 | function calendar_get_block_upcoming($events, $linkhref = NULL, $showcourselink = false) { |
7423f116 | 1341 | $content = ''; |
1342 | $lines = count($events); | |
396b61f0 | 1343 | if (!$lines) { |
1344 | return $content; | |
1345 | } | |
7423f116 | 1346 | |
396b61f0 | 1347 | for ($i = 0; $i < $lines; ++$i) { |
b0ac9180 | 1348 | if (!isset($events[$i]->time)) { // Just for robustness |
1349 | continue; | |
1350 | } | |
9df8ff44 | 1351 | $events[$i] = calendar_add_event_metadata($events[$i]); |
857c5f90 | 1352 | $content .= '<div class="event"><span class="icon c0">'.$events[$i]->icon.'</span>'; |
43c3ffbe | 1353 | if (!empty($events[$i]->referer)) { |
7423f116 | 1354 | // That's an activity event, so let's provide the hyperlink |
396b61f0 | 1355 | $content .= $events[$i]->referer; |
1356 | } else { | |
e749554e | 1357 | if(!empty($linkhref)) { |
da304137 | 1358 | $href = calendar_get_link_href(new moodle_url(CALENDAR_URL . $linkhref), 0, 0, 0, $events[$i]->timestart); |
0f927f1e SH |
1359 | $href->set_anchor('event_'.$events[$i]->id); |
1360 | $content .= html_writer::link($href, $events[$i]->name); | |
e749554e | 1361 | } |
1362 | else { | |
1363 | $content .= $events[$i]->name; | |
1364 | } | |
7423f116 | 1365 | } |
9ecf051d | 1366 | $events[$i]->time = str_replace('»', '<br />»', $events[$i]->time); |
f6a5ffe8 HB |
1367 | if ($showcourselink && !empty($events[$i]->courselink)) { |
1368 | $content .= html_writer::div($events[$i]->courselink, 'course'); | |
1369 | } | |
9ecf051d | 1370 | $content .= '<div class="date">'.$events[$i]->time.'</div></div>'; |
396b61f0 | 1371 | if ($i < $lines - 1) $content .= '<hr />'; |
7423f116 | 1372 | } |
1373 | ||
1374 | return $content; | |
7423f116 | 1375 | } |
1376 | ||
08b4a4e1 RW |
1377 | /** |
1378 | * Get the next following month | |
1379 | * | |
08b4a4e1 RW |
1380 | * @param int $month the number of the month. |
1381 | * @param int $year the number of the year. | |
1382 | * @return array the following month | |
1383 | */ | |
7423f116 | 1384 | function calendar_add_month($month, $year) { |
da304137 MN |
1385 | // Get the calendar type we are using. |
1386 | $calendartype = \core_calendar\type_factory::get_calendar_instance(); | |
1387 | return $calendartype->get_next_month($year, $month); | |
7423f116 | 1388 | } |
1389 | ||
08b4a4e1 | 1390 | /** |
da304137 | 1391 | * Get the previous month. |
08b4a4e1 RW |
1392 | * |
1393 | * @param int $month the number of the month. | |
1394 | * @param int $year the number of the year. | |
1395 | * @return array previous month | |
1396 | */ | |
7423f116 | 1397 | function calendar_sub_month($month, $year) { |
da304137 MN |
1398 | // Get the calendar type we are using. |
1399 | $calendartype = \core_calendar\type_factory::get_calendar_instance(); | |
1400 | return $calendartype->get_prev_month($year, $month); | |
7423f116 | 1401 | } |
1402 | ||
08b4a4e1 RW |
1403 | /** |
1404 | * Get per-day basis events | |
1405 | * | |
1406 | * @param array $events list of events | |
1407 | * @param int $month the number of the month | |
1408 | * @param int $year the number of the year | |
1409 | * @param array $eventsbyday event on specific day | |
1410 | * @param array $durationbyday duration of the event in days | |
1411 | * @param array $typesbyday event type (eg: global, course, user, or group) | |
1412 | * @param array $courses list of courses | |
1413 | * @return void | |
1414 | */ | |
7c50db30 | 1415 | function calendar_events_by_day($events, $month, $year, &$eventsbyday, &$durationbyday, &$typesbyday, &$courses) { |
da304137 MN |
1416 | // Get the calendar type we are using. |
1417 | $calendartype = \core_calendar\type_factory::get_calendar_instance(); | |
1418 | ||
7423f116 | 1419 | $eventsbyday = array(); |
1420 | $typesbyday = array(); | |
1421 | $durationbyday = array(); | |
1422 | ||
1423 | if($events === false) { | |
1424 | return; | |
1425 | } | |
1426 | ||
da304137 MN |
1427 | foreach ($events as $event) { |
1428 | $startdate = $calendartype->timestamp_to_date_array($event->timestart); | |
b4892fa2 | 1429 | // Set end date = start date if no duration |
1430 | if ($event->timeduration) { | |
da304137 | 1431 | $enddate = $calendartype->timestamp_to_date_array($event->timestart + $event->timeduration - 1); |
b4892fa2 | 1432 | } else { |
1433 | $enddate = $startdate; | |
1434 | } | |
7423f116 | 1435 | |
7b38bfa6 | 1436 | // Simple arithmetic: $year * 13 + $month is a distinct integer for each distinct ($year, $month) pair |
ef618501 | 1437 | if(!($startdate['year'] * 13 + $startdate['mon'] <= $year * 13 + $month) && ($enddate['year'] * 13 + $enddate['mon'] >= $year * 13 + $month)) { |
7b38bfa6 | 1438 | // Out of bounds |
1439 | continue; | |
7423f116 | 1440 | } |
1441 | ||
7b38bfa6 | 1442 | $eventdaystart = intval($startdate['mday']); |
7423f116 | 1443 | |
7b38bfa6 | 1444 | if($startdate['mon'] == $month && $startdate['year'] == $year) { |
1445 | // Give the event to its day | |
1446 | $eventsbyday[$eventdaystart][] = $event->id; | |
7423f116 | 1447 | |
7b38bfa6 | 1448 | // Mark the day as having such an event |
9064751b | 1449 | if($event->courseid == SITEID && $event->groupid == 0) { |
7b38bfa6 | 1450 | $typesbyday[$eventdaystart]['startglobal'] = true; |
b4892fa2 | 1451 | // Set event class for global event |
90723839 | 1452 | $events[$event->id]->class = 'calendar_event_global'; |
7b38bfa6 | 1453 | } |
c3d3b6d4 | 1454 | else if($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) { |
7b38bfa6 | 1455 | $typesbyday[$eventdaystart]['startcourse'] = true; |
b4892fa2 | 1456 | // Set event class for course event |
90723839 | 1457 | $events[$event->id]->class = 'calendar_event_course'; |
7b38bfa6 | 1458 | } |
1459 | else if($event->groupid) { | |
1460 | $typesbyday[$eventdaystart]['startgroup'] = true; | |
b4892fa2 | 1461 | // Set event class for group event |
90723839 | 1462 | $events[$event->id]->class = 'calendar_event_group'; |
7b38bfa6 | 1463 | } |
1464 | else if($event->userid) { | |
1465 | $typesbyday[$eventdaystart]['startuser'] = true; | |
b4892fa2 | 1466 | // Set event class for user event |
90723839 | 1467 | $events[$event->id]->class = 'calendar_event_user'; |
7b38bfa6 | 1468 | } |
1469 | } | |
7423f116 | 1470 | |
7b38bfa6 | 1471 | if($event->timeduration == 0) { |
1472 | // Proceed with the next | |
1473 | continue; | |
1474 | } | |
7423f116 | 1475 | |
7b38bfa6 | 1476 | // The event starts on $month $year or before. So... |
ef618501 | 1477 | $lowerbound = $startdate['mon'] == $month && $startdate['year'] == $year ? intval($startdate['mday']) : 0; |
7b38bfa6 | 1478 | |
1479 | // Also, it ends on $month $year or later... | |
1480 | $upperbound = $enddate['mon'] == $month && $enddate['year'] == $year ? intval($enddate['mday']) : calendar_days_in_month($month, $year); | |
1481 | ||
1482 | // Mark all days between $lowerbound and $upperbound (inclusive) as duration | |
1483 | for($i = $lowerbound + 1; $i <= $upperbound; ++$i) { | |
1484 | $durationbyday[$i][] = $event->id; | |
9064751b | 1485 | if($event->courseid == SITEID && $event->groupid == 0) { |
7b38bfa6 | 1486 | $typesbyday[$i]['durationglobal'] = true; |
1487 | } | |
c3d3b6d4 | 1488 | else if($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) { |
7b38bfa6 | 1489 | $typesbyday[$i]['durationcourse'] = true; |
1490 | } | |
1491 | else if($event->groupid) { | |
1492 | $typesbyday[$i]['durationgroup'] = true; | |
1493 | } | |
1494 | else if($event->userid) { | |
1495 | $typesbyday[$i]['durationuser'] = true; | |
7423f116 | 1496 | } |
1497 | } | |
7b38bfa6 | 1498 | |
7423f116 | 1499 | } |
1500 | return; | |
1501 | } | |
1502 | ||
08b4a4e1 RW |
1503 | /** |
1504 | * Get current module cache | |
1505 | * | |
1506 | * @param array $coursecache list of course cache | |
1507 | * @param string $modulename name of the module | |
1508 | * @param int $instance module instance number | |
1509 | * @return stdClass|bool $module information | |
1510 | */ | |
b63c0ee5 | 1511 | function calendar_get_module_cached(&$coursecache, $modulename, $instance) { |
1512 | $module = get_coursemodule_from_instance($modulename, $instance); | |
7423f116 | 1513 | |
1514 | if($module === false) return false; | |
b63c0ee5 | 1515 | if(!calendar_get_course_cached($coursecache, $module->course)) { |
7423f116 | 1516 | return false; |
1517 | } | |
1518 | return $module; | |
1519 | } | |
1520 | ||
08b4a4e1 RW |
1521 | /** |
1522 | * Get current course cache | |
1523 | * | |
1524 | * @param array $coursecache list of course cache | |
1525 | * @param int $courseid id of the course | |
1526 | * @return stdClass $coursecache[$courseid] return the specific course cache | |
1527 | */ | |
7423f116 | 1528 | function calendar_get_course_cached(&$coursecache, $courseid) { |
b571c6b3 | 1529 | if (!isset($coursecache[$courseid])) { |
d20b0aa5 | 1530 | $coursecache[$courseid] = get_course($courseid); |
7423f116 | 1531 | } |
1532 | return $coursecache[$courseid]; | |
1533 | } | |
1534 | ||
c9653460 MG |
1535 | /** |
1536 | * Get group from groupid for calendar display | |
1537 | * | |
1538 | * @param int $groupid | |
1539 | * @return stdClass group object with fields 'id', 'name' and 'courseid' | |
1540 | */ | |
1541 | function calendar_get_group_cached($groupid) { | |
1542 | static $groupscache = array(); | |
1543 | if (!isset($groupscache[$groupid])) { | |
1544 | $groupscache[$groupid] = groups_get_group($groupid, 'id,name,courseid'); | |
1545 | } | |
1546 | return $groupscache[$groupid]; | |
1547 | } | |
1548 | ||
797cedc7 SH |
1549 | /** |
1550 | * Returns the courses to load events for, the | |
1551 | * | |
797cedc7 | 1552 | * @param array $courseeventsfrom An array of courses to load calendar events for |
08b4a4e1 | 1553 | * @param bool $ignorefilters specify the use of filters, false is set as default |
797cedc7 SH |
1554 | * @return array An array of courses, groups, and user to load calendar events for based upon filters |
1555 | */ | |
1556 | function calendar_set_filters(array $courseeventsfrom, $ignorefilters = false) { | |
1557 | global $USER, $CFG, $DB; | |
7423f116 | 1558 | |
797cedc7 SH |
1559 | // For backwards compatability we have to check whether the courses array contains |
1560 | // just id's in which case we need to load course objects. | |
1561 | $coursestoload = array(); | |
1562 | foreach ($courseeventsfrom as $id => $something) { | |
1563 | if (!is_object($something)) { | |
1564 | $coursestoload[] = $id; | |
1565 | unset($courseeventsfrom[$id]); | |
753b799d | 1566 | } |
dd97c328 | 1567 | } |
797cedc7 SH |
1568 | if (!empty($coursestoload)) { |
1569 | // TODO remove this in 2.2 | |
1570 | debugging('calendar_set_filters now preferes an array of course objects with preloaded contexts', DEBUG_DEVELOPER); | |
1571 | $courseeventsfrom = array_merge($courseeventsfrom, $DB->get_records_list('course', 'id', $coursestoload)); | |
89adb174 | 1572 | } |
7423f116 | 1573 | |
797cedc7 SH |
1574 | $courses = array(); |
1575 | $user = false; | |
1576 | $group = false; | |
7423f116 | 1577 | |
7fd5786e HB |
1578 | // capabilities that allow seeing group events from all groups |
1579 | // TODO: rewrite so that moodle/calendar:manageentries is not necessary here | |
1580 | $allgroupscaps = array('moodle/site:accessallgroups', 'moodle/calendar:manageentries'); | |
1581 | ||
797cedc7 | 1582 | $isloggedin = isloggedin(); |
d12e3ff2 | 1583 | |
797cedc7 SH |
1584 | if ($ignorefilters || calendar_show_event_type(CALENDAR_EVENT_COURSE)) { |
1585 | $courses = array_keys($courseeventsfrom); | |
7423f116 | 1586 | } |
797cedc7 SH |
1587 | if ($ignorefilters || calendar_show_event_type(CALENDAR_EVENT_GLOBAL)) { |
1588 | $courses[] = SITEID; | |
7423f116 | 1589 | } |
797cedc7 SH |
1590 | $courses = array_unique($courses); |
1591 | sort($courses); | |
7c50db30 | 1592 | |
797cedc7 | 1593 | if (!empty($courses) && in_array(SITEID, $courses)) { |
7c50db30 | 1594 | // Sort courses for consistent colour highlighting |
1595 | // Effectively ignoring SITEID as setting as last course id | |
1596 | $key = array_search(SITEID, $courses); | |
797cedc7 SH |
1597 | unset($courses[$key]); |
1598 | $courses[] = SITEID; | |
0e6a8f4b | 1599 | } |
7423f116 | 1600 | |
797cedc7 SH |
1601 | if ($ignorefilters || ($isloggedin && calendar_show_event_type(CALENDAR_EVENT_USER))) { |
1602 | $user = $USER->id; | |
7423f116 | 1603 | } |
257e3f4c | 1604 | |
797cedc7 SH |
1605 | if (!empty($courseeventsfrom) && (calendar_show_event_type(CALENDAR_EVENT_GROUP) || $ignorefilters)) { |
1606 | ||
7fd5786e HB |
1607 | if (count($courseeventsfrom)==1) { |
1608 | $course = reset($courseeventsfrom); | |
6ca657a7 | 1609 | if (has_any_capability($allgroupscaps, context_course::instance($course->id))) { |
7fd5786e HB |
1610 | $coursegroups = groups_get_all_groups($course->id, 0, 0, 'g.id'); |
1611 | $group = array_keys($coursegroups); | |
7423f116 | 1612 | } |
7fd5786e HB |
1613 | } |
1614 | if ($group === false) { | |
0601e0ee | 1615 | if (!empty($CFG->calendar_adminseesall) && has_any_capability($allgroupscaps, context_system::instance())) { |
7fd5786e HB |
1616 | $group = true; |
1617 | } else if ($isloggedin) { | |
1618 | $groupids = array(); | |
c46da759 | 1619 | |
7fd5786e HB |
1620 | // We already have the courses to examine in $courses |
1621 | // For each course... | |
1622 | foreach ($courseeventsfrom as $courseid => $course) { | |
1623 | // If the user is an editing teacher in there, | |
1624 | if (!empty($USER->groupmember[$course->id])) { | |
1625 | // We've already cached the users groups for this course so we can just use that | |
1626 | $groupids = array_merge($groupids, $USER->groupmember[$course->id]); | |
1627 | } else if ($course->groupmode != NOGROUPS || !$course->groupmodeforce) { | |
1628 | // If this course has groups, show events from all of those related to the current user | |
1629 | $coursegroups = groups_get_user_groups($course->id, $USER->id); | |
1630 | $groupids = array_merge($groupids, $coursegroups['0']); | |
1631 | } | |
1632 | } | |
1633 | if (!empty($groupids)) { | |
1634 | $group = $groupids; | |
1635 | } | |
6c9584d1 | 1636 | } |
7423f116 | 1637 | } |
7423f116 | 1638 | } |
797cedc7 SH |
1639 | if (empty($courses)) { |
1640 | $courses = false; | |
7423f116 | 1641 | } |
797cedc7 SH |
1642 | |
1643 | return array($courses, $group, $user); | |
7423f116 | 1644 | } |
1645 | ||
08b4a4e1 RW |
1646 | /** |
1647 | * Return the capability for editing calendar event | |
1648 | * | |
1649 | * @param calendar_event $event event object | |
1650 | * @return bool capability to edit event | |
1651 | */ | |
7423f116 | 1652 | function calendar_edit_event_allowed($event) { |
62d11d77 | 1653 | global $USER, $DB; |
7423f116 | 1654 | |
2ac8da76 | 1655 | // Must be logged in |
1656 | if (!isloggedin()) { | |
1657 | return false; | |
1658 | } | |
1659 | ||
89491dbd | 1660 | // can not be using guest account |
2396a414 | 1661 | if (isguestuser()) { |
e295df44 | 1662 | return false; |
89491dbd | 1663 | } |
e295df44 | 1664 | |
e6059873 SH |
1665 | // You cannot edit calendar subscription events presently. |
1666 | if (!empty($event->subscriptionid)) { | |
1667 | return false; | |
1668 | } | |
1669 | ||
6ca657a7 | 1670 | $sitecontext = context_system::instance(); |
89491dbd | 1671 | // if user has manageentries at site level, return true |
28ee98c5 | 1672 | if (has_capability('moodle/calendar:manageentries', $sitecontext)) { |
89491dbd | 1673 | return true; |
f52f7413 | 1674 | } |
e295df44 | 1675 | |
c0a2c361 | 1676 | // if groupid is set, it's definitely a group event |
438e4add | 1677 | if (!empty($event->groupid)) { |
f63d2922 | 1678 | // Allow users to add/edit group events if: |
1679 | // 1) They have manageentries (= entries for whole course) | |
1680 | // 2) They have managegroupentries AND are in the group | |
62d11d77 | 1681 | $group = $DB->get_record('groups', array('id'=>$event->groupid)); |
f63d2922 | 1682 | return $group && ( |
f4700b91 RW |
1683 | has_capability('moodle/calendar:manageentries', $event->context) || |
1684 | (has_capability('moodle/calendar:managegroupentries', $event->context) | |
f63d2922 | 1685 | && groups_is_member($event->groupid))); |
438e4add | 1686 | } else if (!empty($event->courseid)) { |
c0a2c361 | 1687 | // if groupid is not set, but course is set, |
1688 | // it's definiely a course event | |
f4700b91 | 1689 | return has_capability('moodle/calendar:manageentries', $event->context); |
438e4add | 1690 | } else if (!empty($event->userid) && $event->userid == $USER->id) { |
c0a2c361 | 1691 | // if course is not set, but userid id set, it's a user event |
f4700b91 RW |
1692 | return (has_capability('moodle/calendar:manageownentries', $event->context)); |
1693 | } else if (!empty($event->userid)) { | |
1694 | return (has_capability('moodle/calendar:manageentries', $event->context)); | |
e295df44 | 1695 | } |
7423f116 | 1696 | return false; |
1697 | } | |
1698 | ||
797cedc7 SH |
1699 | /** |
1700 | * Returns the default courses to display on the calendar when there isn't a specific | |
1701 | * course to display. | |
1702 | * | |
08b4a4e1 | 1703 | * @return array $courses Array of courses to display |
797cedc7 SH |
1704 | */ |
1705 | function calendar_get_default_courses() { | |
1706 | global $CFG, $DB; | |
7423f116 | 1707 | |
4f0c2d00 | 1708 | if (!isloggedin()) { |
2ef75eee | 1709 | return array(); |
1710 | } | |
1711 | ||
7423f116 | 1712 | $courses = array(); |
d083084a | 1713 | if (!empty($CFG->calendar_adminseesall) && has_capability('moodle/calendar:manageentries', context_system::instance())) { |
2e4c0c91 FM |
1714 | $select = ', ' . context_helper::get_preload_record_columns_sql('ctx'); |
1715 | $join = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)"; | |
d083084a | 1716 | $sql = "SELECT c.* $select |
797cedc7 | 1717 | FROM {course} c |
d083084a ARN |
1718 | $join |
1719 | WHERE EXISTS (SELECT 1 FROM {event} e WHERE e.courseid = c.id) | |
1720 | "; | |
2e4c0c91 | 1721 | $courses = $DB->get_records_sql($sql, array('contextlevel' => CONTEXT_COURSE), 0, 20); |
797cedc7 | 1722 | foreach ($courses as $course) { |
d083084a | 1723 | context_helper::preload_from_record($course); |
86f092d2 | 1724 | } |
797cedc7 | 1725 | return $courses; |
95a89225 | 1726 | } |
e295df44 | 1727 | |
df997f84 | 1728 | $courses = enrol_get_my_courses(); |
37d87d11 | 1729 | |
1730 | return $courses; | |
7423f116 | 1731 | } |
1732 | ||
08b4a4e1 RW |
1733 | /** |
1734 | * Display calendar preference button | |
1735 | * | |
1736 | * @param stdClass $course course object | |
9629790b SB |
1737 | * @deprecated since Moodle 3.2 |
1738 | * @todo MDL-55875 This will be deleted in Moodle 3.6. | |
08b4a4e1 RW |
1739 | * @return string return preference button in html |
1740 | */ | |
797cedc7 SH |
1741 | function calendar_preferences_button(stdClass $course) { |
1742 | global $OUTPUT; | |
7423f116 | 1743 | |
1744 | // Guests have no preferences | |
4f0c2d00 | 1745 | if (!isloggedin() || isguestuser()) { |
7423f116 | 1746 | return ''; |
1747 | } | |
9629790b | 1748 | debugging('This should no longer be used, the calendar preferences are now linked to the user preferences page'); |
7423f116 | 1749 | |
9629790b | 1750 | return $OUTPUT->single_button(new moodle_url('/user/calendar.php'), get_string("preferences", "calendar")); |
7423f116 | 1751 | } |
1752 | ||
08b4a4e1 RW |
1753 | /** |
1754 | * Get event format time | |
1755 | * | |
1756 | * @param calendar_event $event event object | |
1757 | * @param int $now current time in gmt | |
1758 | * @param array $linkparams list of params for event link | |
1759 | * @param bool $usecommonwords the words as formatted date/time. | |
1760 | * @param int $showtime determine the show time GMT timestamp | |
1761 | * @return string $eventtime link/string for event time | |
1762 | */ | |
da304137 MN |
1763 | function calendar_format_event_time($event, $now, $linkparams = null, $usecommonwords = true, $showtime = 0) { |
1764 | $starttime = $event->timestart; | |
1765 | $endtime = $event->timestart + $event->timeduration; | |
8f896582 | 1766 | |
0f927f1e SH |
1767 | if (empty($linkparams) || !is_array($linkparams)) { |
1768 | $linkparams = array(); | |
1769 | } | |
da304137 | 1770 | |
0f927f1e SH |
1771 | $linkparams['view'] = 'day'; |
1772 | ||
8f896582 | 1773 | // OK, now to get a meaningful display... |
da304137 MN |
1774 | // Check if there is a duration for this event. |
1775 | if ($event->timeduration) { | |
1776 | // Get the midnight of the day the event will start. | |
1777 | $usermidnightstart = usergetmidnight($starttime); | |
1778 | // Get the midnight of the day the event will end. | |
1779 | $usermidnightend = usergetmidnight($endtime); | |
1780 | // Check if we will still be on the same day. | |
1781 | if ($usermidnightstart == $usermidnightend) { | |
1782 | // Check if we are running all day. | |
1783 | if ($event->timeduration == DAYSECS) { | |
b4892fa2 | 1784 | $time = get_string('allday', 'calendar'); |
da304137 MN |
1785 | } else { // Specify the time we will be running this from. |
1786 | $datestart = calendar_time_representation($starttime); | |
1787 | $dateend = calendar_time_representation($endtime); | |
1788 | $time = $datestart . ' <strong>»</strong> ' . $dateend; | |
b4892fa2 | 1789 | } |
8f896582 | 1790 | |
da304137 | 1791 | // Set printable representation. |
b4892fa2 | 1792 | if (!$showtime) { |
1793 | $day = calendar_day_representation($event->timestart, $now, $usecommonwords); | |
da304137 MN |
1794 | $url = calendar_get_link_href(new moodle_url(CALENDAR_URL . 'view.php', $linkparams), 0, 0, 0, $endtime); |
1795 | $eventtime = html_writer::link($url, $day) . ', ' . $time; | |
b4892fa2 | 1796 | } else { |
1797 | $eventtime = $time; | |
1798 | } | |
da304137 MN |
1799 | } else { // It must spans two or more days. |
1800 | $daystart = calendar_day_representation($event->timestart, $now, $usecommonwords) . ', '; | |
b4892fa2 | 1801 | if ($showtime == $usermidnightstart) { |
1802 | $daystart = ''; | |
1803 | } | |
8f896582 | 1804 | $timestart = calendar_time_representation($event->timestart); |
da304137 | 1805 | $dayend = calendar_day_representation($event->timestart + $event->timeduration, $now, $usecommonwords) . ', '; |
b4892fa2 | 1806 | if ($showtime == $usermidnightend) { |
1807 | $dayend = ''; | |
1808 | } | |
8f896582 | 1809 | $timeend = calendar_time_representation($event->timestart + $event->timeduration); |
1810 | ||
da304137 | 1811 | // Set printable representation. |
a0ef87de | 1812 | if ($now >= $usermidnightstart && $now < strtotime('+1 day', $usermidnightstart)) { |
da304137 MN |
1813 | $url = calendar_get_link_href(new moodle_url(CALENDAR_URL . 'view.php', $linkparams), 0, 0, 0, $endtime); |
1814 | $eventtime = $timestart . ' <strong>»</strong> ' . html_writer::link($url, $dayend) . $timeend; | |
b4892fa2 | 1815 | } else { |
ef36d240 SB |
1816 | // The event is in the future, print start and end links. |
1817 | $url = calendar_get_link_href(new moodle_url(CALENDAR_URL . 'view.php', $linkparams), 0, 0, 0, $starttime); | |
da304137 | 1818 | $eventtime = html_writer::link($url, $daystart) . $timestart . ' <strong>»</strong> '; |
1d5bd3d2 | 1819 | |
ef36d240 | 1820 | $url = calendar_get_link_href(new moodle_url(CALENDAR_URL . 'view.php', $linkparams), 0, 0, 0, $endtime); |
da304137 | 1821 | $eventtime .= html_writer::link($url, $dayend) . $timeend; |
b4892fa2 | 1822 | } |
8f896582 | 1823 | } |
da304137 | 1824 | } else { // There is no time duration. |
2af268a1 | 1825 | $time = calendar_time_representation($event->timestart); |
da304137 | 1826 | // Set printable representation. |
b4892fa2 | 1827 | if (!$showtime) { |
1828 | $day = calendar_day_representation($event->timestart, $now, $usecommonwords); | |
da304137 MN |
1829 | $url = calendar_get_link_href(new moodle_url(CALENDAR_URL . 'view.php', $linkparams), 0, 0, 0, $starttime); |
1830 | $eventtime = html_writer::link($url, $day) . ', ' . trim($time); | |
b4892fa2 | 1831 | } else { |
1832 | $eventtime = $time; | |
1833 | } | |
1834 | } | |
1835 | ||
da304137 MN |
1836 | // Check if It has expired. |
1837 | if ($event->timestart + $event->timeduration < $now) { | |
1838 | $eventtime = '<span class="dimmed_text">' . str_replace(' href=', ' class="dimmed" href=', $eventtime) . '</span>'; | |
8f896582 | 1839 | } |
09d36284 | 1840 | |
8f896582 | 1841 | return $eventtime; |
1842 | } | |
054193be | 1843 | |
08b4a4e1 RW |
1844 | /** |
1845 | * Display month selector options | |
1846 | * | |
1847 | * @param string $name for the select element | |
1848 | * @param string|array $selected options for select elements | |
1849 | */ | |
86f092d2 | 1850 | function calendar_print_month_selector($name, $selected) { |
86f092d2 | 1851 | $months = array(); |
86f092d2 | 1852 | for ($i=1; $i<=12; $i++) { |
76ab1c33 | 1853 | $months[$i] = userdate(gmmktime(12, 0, 0, $i, 15, 2000), '%B'); |
86f092d2 | 1854 | } |
5c576552 | 1855 | echo html_writer::label(get_string('months'), 'menu'. $name, false, array('class' => 'accesshide')); |
d776d59e | 1856 | echo html_writer::select($months, $name, $selected, false); |
86f092d2 | 1857 | } |
1858 | ||
797cedc7 SH |
1859 | /** |
1860 | * Checks to see if the requested type of event should be shown for the given user. | |
1861 | * | |
1862 | * @param CALENDAR_EVENT_GLOBAL|CALENDAR_EVENT_COURSE|CALENDAR_EVENT_GROUP|CALENDAR_EVENT_USER $type | |
1863 | * The type to check the display for (default is to display all) | |
1864 | * @param stdClass|int|null $user The user to check for - by default the current user | |
1865 | * @return bool True if the tyep should be displayed false otherwise | |
1866 | */ | |
1867 | function calendar_show_event_type($type, $user = null) { | |
1868 | $default = CALENDAR_EVENT_GLOBAL + CALENDAR_EVENT_COURSE + CALENDAR_EVENT_GROUP + CALENDAR_EVENT_USER; | |
1869 | if (get_user_preferences('calendar_persistflt', 0, $user) === 0) { | |
1870 | global $SESSION; | |
1871 | if (!isset($SESSION->calendarshoweventtype)) { | |
1872 | $SESSION->calendarshoweventtype = $default; | |
1873 | } | |
1874 | return $SESSION->calendarshoweventtype & $type; | |
1875 | } else { | |
1876 | return get_user_preferences('calendar_savedflt', $default, $user) & $type; | |
054193be | 1877 | } |
797cedc7 SH |
1878 | } |
1879 | ||
1880 | /** | |
1881 | * Sets the display of the event type given $display. | |
08b4a4e1 | 1882 | * |
797cedc7 SH |
1883 | * If $display = true the event type will be shown. |
1884 | * If $display = false the event type will NOT be shown. | |
1885 | * If $display = null the current value will be toggled and saved. | |
1886 | * | |
08b4a4e1 RW |
1887 | * @param CALENDAR_EVENT_GLOBAL|CALENDAR_EVENT_COURSE|CALENDAR_EVENT_GROUP|CALENDAR_EVENT_USER $type object of CALENDAR_EVENT_XXX |
1888 | * @param bool $display option to display event type | |
1889 | * @param stdClass|int $user moodle user object or id, null means current user | |
797cedc7 SH |
1890 | */ |
1891 | function calendar_set_event_type_display($type, $display = null, $user = null) { | |
1892 | $persist = get_user_preferences('calendar_persistflt', 0, $user); | |
1893 | $default = CALENDAR_EVENT_GLOBAL + CALENDAR_EVENT_COURSE + CALENDAR_EVENT_GROUP + CALENDAR_EVENT_USER; | |
1894 | if ($persist === 0) { | |
1895 | global $SESSION; | |
1896 | if (!isset($SESSION->calendarshoweventtype)) { | |
1897 | $SESSION->calendarshoweventtype = $default; | |
1898 | } | |
1899 | $preference = $SESSION->calendarshoweventtype; | |
1900 | } else { | |
1901 | $preference = get_user_preferences('calendar_savedflt', $default, $user); | |
054193be | 1902 | } |
797cedc7 SH |
1903 | $current = $preference & $type; |
1904 | if ($display === null) { | |
1905 | $display = !$current; | |
054193be | 1906 | } |
797cedc7 SH |
1907 | if ($display && !$current) { |
1908 | $preference += $type; | |
1909 | } else if (!$display && $current) { | |
1910 | $preference -= $type; | |
054193be | 1911 | } |
797cedc7 SH |
1912 | if ($persist === 0) { |
1913 | $SESSION->calendarshoweventtype = $preference; | |
1914 | } else { | |
1915 | if ($preference == $default) { | |
1916 | unset_user_preference('calendar_savedflt', $user); | |
1917 | } else { | |
1918 | set_user_preference('calendar_savedflt', $preference, $user); | |
1919 | } | |
054193be | 1920 | } |
054193be | 1921 | } |
1922 | ||
08b4a4e1 RW |
1923 | /** |
1924 | * Get calendar's allowed types | |
1925 | * | |
1926 | * @param stdClass $allowed list of allowed edit for event type | |
1927 | * @param stdClass|int $course object of a course or course id | |
1928 | */ | |
797cedc7 SH |
1929 | function calendar_get_allowed_types(&$allowed, $course = null) { |
1930 | global $USER, $CFG, $DB; | |
b85b25eb | 1931 | $allowed = new stdClass(); |
0601e0ee | 1932 | $allowed->user = has_capability('moodle/calendar:manageownentries', context_system::instance()); |
86ac8b24 | 1933 | $allowed->groups = false; // This may change just below |
1934 | $allowed->courses = false; // This may change just below | |
6ca657a7 | 1935 | $allowed->site = has_capability('moodle/calendar:manageentries', context_course::instance(SITEID)); |
86ac8b24 | 1936 | |
797cedc7 SH |
1937 | if (!empty($course)) { |
1938 | if (!is_object($course)) { | |
74df2951 | 1939 | $course = $DB->get_record('course', array('id' => $course), '*', MUST_EXIST); |
797cedc7 SH |
1940 | } |
1941 | if ($course->id != SITEID) { | |
6ca657a7 | 1942 | $coursecontext = context_course::instance($course->id); |
3c0ea9b2 | 1943 | $allowed->user = has_capability('moodle/calendar:manageownentries', $coursecontext); |
86ac8b24 | 1944 | |
797cedc7 SH |
1945 | if (has_capability('moodle/calendar:manageentries', $coursecontext)) { |
1946 | $allowed->courses = array($course->id => 1); | |
aa6c1ced | 1947 | |
797cedc7 | 1948 | if ($course->groupmode != NOGROUPS || !$course->groupmodeforce) { |
462e42cb JP |
1949 | if (has_capability('moodle/site:accessallgroups', $coursecontext)) { |
1950 | $allowed->groups = groups_get_all_groups($course->id); | |
1951 | } else { | |
1952 | $allowed->groups = groups_get_all_groups($course->id, $USER->id); | |
1953 | } | |
797cedc7 SH |
1954 | } |
1955 | } else if (has_capability('moodle/calendar:managegroupentries', $coursecontext)) { | |
1956 | if($course->groupmode != NOGROUPS || !$course->groupmodeforce) { | |
462e42cb JP |
1957 | if (has_capability('moodle/site:accessallgroups', $coursecontext)) { |
1958 | $allowed->groups = groups_get_all_groups($course->id); | |
1959 | } else { | |
1960 | $allowed->groups = groups_get_all_groups($course->id, $USER->id); | |
1961 | } | |
797cedc7 | 1962 | } |
f63d2922 | 1963 | } |
86ac8b24 | 1964 | } |
1965 | } | |
1966 | } | |
1967 | ||
1968 | /** | |
08b4a4e1 | 1969 | * See if user can add calendar entries at all |
86ac8b24 | 1970 | * used to print the "New Event" button |
08b4a4e1 RW |
1971 | * |
1972 | * @param stdClass $course object of a course or course id | |
1973 | * @return bool has the capability to add at least one event type | |
86ac8b24 | 1974 | */ |
797cedc7 SH |
1975 | function calendar_user_can_add_event($course) { |
1976 | if (!isloggedin() || isguestuser()) { | |
1977 | return false; | |
1978 | } | |
1979 | calendar_get_allowed_types($allowed, $course); | |
e295df44 | 1980 | return (bool)($allowed->user || $allowed->groups || $allowed->courses || $allowed->site); |
86ac8b24 | 1981 | } |
76d9df3f SH |
1982 | |
1983 | /** | |
1984 | * Check wether the current user is permitted to add events | |
1985 | * | |
08b4a4e1 RW |
1986 | * @param stdClass $event object of event |
1987 | * @return bool has the capability to add event | |
76d9df3f SH |
1988 | */ |
1989 | function calendar_add_event_allowed($event) { | |
1990 | global $USER, $DB; | |
1991 | ||
1992 | // can not be using guest account | |
4f0c2d00 | 1993 | if (!isloggedin() or isguestuser()) { |
76d9df3f SH |
1994 | return false; |
1995 | } | |
1996 | ||
6ca657a7 | 1997 | $sitecontext = context_system::instance(); |
76d9df3f SH |
1998 | // if user has manageentries at site level, always return true |
1999 | if (has_capability('moodle/calendar:manageentries', $sitecontext)) { | |
2000 | return true; | |
2001 | } | |
2002 | ||
2003 | switch ($event->eventtype) { | |
2004 | case 'course': | |
f4700b91 | 2005 | return has_capability('moodle/calendar:manageentries', $event->context); |
76d9df3f SH |
2006 | |
2007 | case 'group': | |
2008 | // Allow users to add/edit group events if: | |
2009 | // 1) They have manageentries (= entries for whole course) | |
2010 | // 2) They have managegroupentries AND are in the group | |
2011 | $group = $DB->get_record('groups', array('id'=>$event->groupid)); | |
2012 | return $group && ( | |
f4700b91 RW |
2013 | has_capability('moodle/calendar:manageentries', $event->context) || |
2014 | (has_capability('moodle/calendar:managegroupentries', $event->context) | |
76d9df3f SH |
2015 | && groups_is_member($event->groupid))); |
2016 | ||
2017 | case 'user': | |
2018 | if ($event->userid == $USER->id) { | |
f4700b91 | 2019 | return (has_capability('moodle/calendar:manageownentries', $event->context)); |
76d9df3f SH |
2020 | } |
2021 | //there is no 'break;' intentionally | |
2022 | ||
2023 | case 'site': | |
f4700b91 | 2024 | return has_capability('moodle/calendar:manageentries', $event->context); |
76d9df3f SH |
2025 | |
2026 | default: | |
f4700b91 | 2027 | return has_capability('moodle/calendar:manageentries', $event->context); |
76d9df3f SH |
2028 | } |
2029 | } | |
2030 | ||
2031 | /** | |
08b4a4e1 | 2032 | * Manage calendar events |
76d9df3f SH |
2033 | * |
2034 | * This class provides the required functionality in order to manage calendar events. | |
2035 | * It was introduced as part of Moodle 2.0 and was created in order to provide a | |
2036 | * better framework for dealing with calendar events in particular regard to file | |
2037 | * handling through the new file API | |
2038 | * | |
08b4a4e1 RW |
2039 | * @package core_calendar |
2040 | * @category calendar | |
2041 | * @copyright 2009 Sam Hemelryk | |
2042 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
2043 | * | |
76d9df3f SH |
2044 | * @property int $id The id within the event table |
2045 | * @property string $name The name of the event | |
2046 | * @property string $description The description of the event | |
2047 | * @property int $format The format of the description FORMAT_? | |
2048 | * @property int $courseid The course the event is associated with (0 if none) | |
2049 | * @property int $groupid The group the event is associated with (0 if none) | |
2050 | * @property int $userid The user the event is associated with (0 if none) | |
2051 | * @property int $repeatid If this is a repeated event this will be set to the | |
2052 | * id of the original | |
2053 | * @property string $modulename If added by a module this will be the module name | |
2054 | * @property int $instance If added by a module this will be the module instance | |
2055 | * @property string $eventtype The event type | |
2056 | * @property int $timestart The start time as a timestamp | |
2057 | * @property int $timeduration The duration of the event in seconds | |
2058 | * @property int $visible 1 if the event is visible | |
2059 | * @property int $uuid ? | |
2060 | * @property int $sequence ? | |
2061 | * @property int $timemodified The time last modified as a timestamp | |
2062 | */ | |
2063 | class calendar_event { | |
2064 | ||
08b4a4e1 | 2065 | /** @var array An object containing the event properties can be accessed via the magic __get/set methods */ |
76d9df3f | 2066 | protected $properties = null; |
08b4a4e1 | 2067 | |
76d9df3f | 2068 | /** |
08b4a4e1 | 2069 | * @var string The converted event discription with file paths resolved. This gets populated when someone requests description for the first time */ |
76d9df3f | 2070 | protected $_description = null; |
08b4a4e1 RW |
2071 | |
2072 | /** @var array The options to use with this description editor */ | |
76d9df3f SH |
2073 | protected $editoroptions = array( |
2074 | 'subdirs'=>false, | |
2075 | 'forcehttps'=>false, | |
2b35ed1a | 2076 | 'maxfiles'=>-1, |
76d9df3f SH |
2077 | 'maxbytes'=>null, |
2078 | 'trusttext'=>false); | |
08b4a4e1 RW |
2079 | |
2080 | /** @var object The context to use with the description editor */ | |
76d9df3f SH |
2081 | protected $editorcontext = null; |
2082 | ||
2083 | /** | |
2084 | * Instantiates a new event and optionally populates its properties with the | |
2085 | * data provided | |
2086 | * | |
2087 | * @param stdClass $data Optional. An object containing the properties to for | |
2088 | * an event | |
2089 | */ | |
2090 | public function __construct($data=null) { | |
c203a277 | 2091 | global $CFG, $USER; |
76d9df3f SH |
2092 | |
2093 | // First convert to object if it is not already (should either be object or assoc array) | |
2094 | if (!is_object($data)) { | |
2095 | $data = (object)$data; | |
2096 | } | |
2097 | ||
2098 | $this->editoroptions['maxbytes'] = $CFG->maxbytes; | |
2099 | ||
2100 | $data->eventrepeats = 0; | |
2101 | ||
2102 | if (empty($data->id)) { | |
2103 | $data->id = null; | |
2104 | } | |
2105 | ||
8a7326ed | 2106 | if (!empty($data->subscriptionid)) { |
e73b527c | 2107 | $data->subscription = calendar_get_subscription($data->subscriptionid); |
8a7326ed AA |
2108 | } |
2109 | ||
c203a277 SH |
2110 | // Default to a user event |
2111 | if (empty($data->eventtype)) { | |
2112 | $data->eventtype = 'user'; | |
2113 | } | |
2114 | ||
2115 | // Default to the current user | |
2116 | if (empty($data->userid)) { | |
2117 | $data->userid = $USER->id; | |
2118 | } | |
2119 | ||
76d9df3f SH |
2120 | if (!empty($data->timeduration) && is_array($data->timeduration)) { |
2121 | $data->timeduration = make_timestamp($data->timeduration['year'], $data->timeduration['month'], $data->timeduration['day'], $data->timeduration['hour'], $data->timeduration['minute']) - $data->timestart; | |
2122 | } | |
2123 | if (!empty($data->description) && is_array($data->description)) { | |
2124 | $data->format = $data->description['format']; | |
2125 | $data->description = $data->description['text']; | |
2126 | } else if (empty($data->description)) { | |
2127 | $data->description = ''; | |
20e5da7d | 2128 | $data->format = editors_get_preferred_format(); |
76d9df3f | 2129 | } |
c203a277 SH |
2130 | // Ensure form is defaulted correctly |
2131 | if (empty($data->format)) { | |
2132 | $data->format = editors_get_preferred_format(); | |
2133 | } | |
76d9df3f | 2134 | |
f4700b91 RW |
2135 | if (empty($data->context)) { |
2136 | $data->context = $this->calculate_context($data); | |
2137 | } | |
76d9df3f SH |
2138 | $this->properties = $data; |
2139 | } | |
2140 | ||
2141 | /** | |
2142 | * Magic property method | |
2143 | * | |
2144 | * Attempts to call a set_$key method if one exists otherwise falls back | |
2145 | * to simply set the property | |
2146 | * | |
08b4a4e1 RW |
2147 | * @param string $key property name |
2148 | * @param mixed $value value of the property | |
76d9df3f SH |
2149 | */ |
2150 | public function __set($key, $value) { | |
2151 | if (method_exists($this, 'set_'.$key)) { | |
2152 | $this->{'set_'.$key}($value); | |
2153 | } | |
2154 | $this->properties->{$key} = $value; | |
2155 | } | |
2156 | ||
2157 | /** | |
2158 | * Magic get method | |
2159 | * | |
2160 | * Attempts to call a get_$key method to return the property and ralls over | |
2161 | * to return the raw property | |
2162 | * | |
08b4a4e1 RW |
2163 | * @param string $key property name |
2164 | * @return mixed property value | |
76d9df3f SH |
2165 | */ |
2166 | public function __get($key) { | |
2167 | if (method_exists($this, 'get_'.$key)) { | |
2168 | return $this->{'get_'.$key}(); | |
2169 | } | |
c203a277 SH |
2170 | if (!isset($this->properties->{$key})) { |
2171 | throw new coding_exception('Undefined property requested'); | |
2172 | } | |
76d9df3f SH |
2173 | return $this->properties->{$key}; |
2174 | } | |
2175 | ||
2176 | /** | |
2177 | * Stupid PHP needs an isset magic method if you use the get magic method and | |
2178 | * still want empty calls to work.... blah ~! | |
2179 | * | |
08b4a4e1 RW |
2180 | * @param string $key $key property name |
2181 | * @return bool|mixed property value, false if property is not exist | |
76d9df3f SH |
2182 | */ |
2183 | public function __isset($key) { | |
2184 | return !empty($this->properties->{$key}); | |
2185 | } | |
2186 | ||
f4700b91 RW |
2187 | /** |
2188 | * Calculate the context value needed for calendar_event. | |
2189 | * Event's type can be determine by the available value store in $data | |
2190 | * It is important to check for the existence of course/courseid to determine | |
2191 | * the course event. | |
2192 | * Default value is set to CONTEXT_USER | |
2193 | * | |
08b4a4e1 RW |
2194 | * @param stdClass $data information about event |
2195 | * @return stdClass The context object. | |
f4700b91 RW |
2196 | */ |
2197 | protected function calculate_context(stdClass $data) { | |
025d82e0 | 2198 | global $USER, $DB; |
f4700b91 | 2199 | |
f4700b91 RW |
2200 | $context = null; |
2201 | if (isset($data->courseid) && $data->courseid > 0) { | |
6ca657a7 | 2202 | $context = context_course::instance($data->courseid); |
f4700b91 | 2203 | } else if (isset($data->course) && $data->course > 0) { |
6ca657a7 | 2204 | $context = context_course::instance($data->course); |
f4700b91 RW |
2205 | } else if (isset($data->groupid) && $data->groupid > 0) { |
2206 | $group = $DB->get_record('groups', array('id'=>$data->groupid)); | |
6ca657a7 | 2207 | $context = context_course::instance($group->courseid); |
f4700b91 | 2208 | } else if (isset($data->userid) && $data->userid > 0 && $data->userid == $USER->id) { |
6ca657a7 | 2209 | $context = context_user::instance($data->userid); |
f4700b91 RW |
2210 | } else if (isset($data->userid) && $data->userid > 0 && $data->userid != $USER->id && |
2211 | isset($data->instance) && $data->instance > 0) { | |
18d63ffb | 2212 | $cm = get_coursemodule_from_instance($data->modulename, $data->instance, 0, false, MUST_EXIST); |
6ca657a7 | 2213 | $context = context_course::instance($cm->course); |
f4700b91 | 2214 | } else { |
e30390a0 | 2215 | $context = context_user::instance($data->userid); |
f4700b91 RW |
2216 | } |
2217 | ||
2218 | return $context; | |
2219 | } | |
2220 | ||
76d9df3f SH |
2221 | /** |
2222 | * Returns an array of editoroptions for this event: Called by __get | |
2223 | * Please use $blah = $event->editoroptions; | |
08b4a4e1 RW |
2224 | * |
2225 | * @return array event editor options | |
76d9df3f SH |
2226 | */ |
2227 | protected function get_editoroptions() { | |
2228 | return $this->editoroptions; | |
2229 | } | |
2230 | ||
2231 | /** | |
2232 | * Returns an event description: Called by __get | |
2233 | * Please use $blah = $event->description; | |
2234 | * | |
08b4a4e1 | 2235 | * @return string event description |
76d9df3f SH |
2236 | */ |
2237 | protected function get_description() { | |
f4700b91 | 2238 | global $CFG; |
99d19c13 PS |
2239 | |
2240 | require_once($CFG->libdir . '/filelib.php'); | |
2241 | ||
76d9df3f SH |
2242 | if ($this->_description === null) { |
2243 | // Check if we have already resolved the context for this event | |
2244 | if ($this->editorcontext === null) { | |
2245 | // Switch on the event type to decide upon the appropriate context | |
2246 | // to use for this event | |
f4700b91 RW |
2247 | $this->editorcontext = $this->properties->context; |
2248 | if ($this->properties->eventtype != 'user' && $this->properties->eventtype != 'course' | |
2249 | && $this->properties->eventtype != 'site' && $this->properties->eventtype != 'group') { | |
2250 | return clean_text($this->properties->description, $this->properties->format); | |
76d9df3f SH |
2251 | } |
2252 | } | |
2253 | ||
2254 | // Work out the item id for the editor, if this is a repeated event then the files will | |
2255 | // be associated with the original | |
2256 | if (!empty($this->properties->repeatid) && $this->properties->repeatid > 0) { | |
2257 | $itemid = $this->properties->repeatid; | |
2258 | } else { | |
2259 | $itemid = $this->properties->id; | |
2260 | } | |
2261 | ||
2262 | // Convert file paths in the description so that things display correctly | |
64f93798 | 2263 | $this->_description = file_rewrite_pluginfile_urls($this->properties->description, 'pluginfile.php', $this->editorcontext->id, 'calendar', 'event_description', $itemid); |
76d9df3f SH |
2264 | // Clean the text so no nasties get through |
2265 | $this->_description = clean_text($this->_description, $this->properties->format); | |
2266 | } | |
2267 | // Finally return the description | |
2268 | return $this->_description; | |
2269 | } | |
2270 | ||
2271 | /** | |
2272 | * Return the number of repeat events there are in this events series | |
aa6c1ced | 2273 | * |
08b4a4e1 | 2274 | * @return int number of event repeated |
76d9df3f SH |
2275 | */ |
2276 | public function count_repeats() { | |
2277 | global $DB; | |
2278 | if (!empty($this->properties->repeatid)) { | |
2279 | $this->properties->eventrepeats = $DB->count_records('event', array('repeatid'=>$this->properties->repeatid)); | |
2280 | // We don't want to count ourselves | |
2281 | $this->properties->eventrepeats--; | |
2282 | } | |
2283 | return $this->properties->eventrepeats; | |
2284 | } | |
2285 | ||
2286 | /** | |
2287 | * Update or create an event within the database | |
2288 | * | |
2289 | * Pass in a object containing the event properties and this function will | |
2290 | * insert it into the database and deal with any associated files | |
2291 | * | |
2149326b AA |
2292 | * @see self::create() |
2293 | * @see self::update() | |
76d9df3f | 2294 | * |
08b4a4e1 RW |
2295 | * @param stdClass $data object of event |
2296 | * @param bool $checkcapability if moodle should check calendar managing capability or not | |
2297 | * @return bool event updated | |
76d9df3f | 2298 | */ |
1d5bd3d2 | 2299 | public function update($data, $checkcapability=true) { |
9cb5d05f | 2300 | global $DB, $USER; |
76d9df3f | 2301 | |
438e4add SH |
2302 | foreach ($data as $key=>$value) { |
2303 | $this->properties->$key = $value; | |
2304 | } | |
2305 | ||
76d9df3f SH |
2306 | $this->properties->timemodified = time(); |
2307 | $usingeditor = (!empty($this->properties->description) && is_array($this->properties->description)); | |
2308 | ||
9cb5d05f AA |
2309 | // Prepare event data. |
2310 | $eventargs = array( | |
2311 | 'context' => $this->properties->context, | |
2312 | 'objectid' => $this->properties->id, | |
2313 | 'other' => array( | |
2314 | 'repeatid' => empty($this->properties->repeatid) ? 0 : $this->properties->repeatid, | |
2315 | 'timestart' => $this->properties->timestart, | |
2316 | 'name' => $this->properties->name | |
2317 | ) | |
2318 | ); | |
2319 | ||
76d9df3f SH |
2320 | if (empty($this->properties->id) || $this->properties->id < 1) { |
2321 | ||
1d5bd3d2 DC |
2322 | if ($checkcapability) { |
2323 | if (!calendar_add_event_allowed($this->properties)) { | |
2324 | print_error('nopermissiontoupdatecalendar'); | |
2325 | } | |
76d9df3f SH |
2326 | } |
2327 | ||
2328 | if ($usingeditor) { | |
2329 | switch ($this->properties->eventtype) { | |
2330 | case 'user': | |
76d9df3f | 2331 | $this->properties->courseid = 0; |
f19d57e5 | 2332 | $this->properties->course = 0; |
76d9df3f SH |
2333 | $this->properties->groupid = 0; |
2334 | $this->properties->userid = $USER->id; | |
2335 | break; | |
2336 | case 'site': | |
76d9df3f | 2337 | $this->properties->courseid = SITEID; |
f19d57e5 | 2338 | $this->properties->course = SITEID; |
76d9df3f SH |
2339 | $this->properties->groupid = 0; |
2340 | $this->properties->userid = $USER->id; | |
2341 | break; | |
2342 | case 'course': | |
76d9df3f SH |
2343 | $this->properties->groupid = 0; |
2344 | $this->properties->userid = $USER->id; | |
2345 | break; | |
2346 | case 'group': | |
76d9df3f SH |
2347 | $this->properties->userid = $USER->id; |
2348 | break; | |
2349 | default: | |
2350 | // Ewww we should NEVER get here, but just incase we do lets | |
2351 | // fail gracefully | |
2352 | $usingeditor = false; | |
2353 | break; | |
2354 | } | |
2355 | ||
f19d57e5 FM |
2356 | // If we are actually using the editor, we recalculate the context because some default values |
2357 | // were set when calculate_context() was called from the constructor. | |
2358 | if ($usingeditor) { | |
2359 | $this->properties->context = $this->calculate_context($this->properties); | |
2360 | $this->editorcontext = $this->properties->context; | |
2361 | } | |
2362 | ||
76d9df3f SH |
2363 | $editor = $this->properties->description; |
2364 | $this->properties->format = $this->properties->description['format']; | |
2365 | $this->properties->description = $this->properties->description['text']; | |
2366 | } | |
2367 | ||
2368 | // Insert the event into the database | |
2369 | $this->properties->id = $DB->insert_record('event', $this->properties); | |
2370 | ||
2371 | if ($usingeditor) { | |
2372 | $this->properties->description = file_save_draft_area_files( | |
2373 | $editor['itemid'], | |
2374 | $this->editorcontext->id, | |
64f93798 PS |
2375 | 'calendar', |
2376 | 'event_description', | |
76d9df3f SH |
2377 | $this->properties->id, |
2378 | $this->editoroptions, | |
2379 | $editor['text'], | |
2380 | $this->editoroptions['forcehttps']); | |
76d9df3f SH |
2381 | $DB->set_field('event', 'description', $this->properties->description, array('id'=>$this->properties->id)); |
2382 | } | |
aa6c1ced | 2383 | |
76d9df3f | 2384 | // Log the event entry. |
9cb5d05f AA |
2385 | $eventargs['objectid'] = $this->properties->id; |
2386 | $eventargs['context'] = $this->properties->context; | |
2387 | $event = \core\event\calendar_event_created::create($eventargs); | |
2388 | $event->trigger(); | |
76d9df3f SH |
2389 | |
2390 | $repeatedids = array(); | |
2391 | ||
2392 | if (!empty($this->properties->repeat)) { | |
2393 | $this->properties->repeatid = $this->properties->id; | |
2394 | $DB->set_field('event', 'repeatid', $this->properties->repeatid, array('id'=>$this->properties->id)); | |
2395 | ||
2396 | $eventcopy = clone($this->properties); | |
2397 | unset($eventcopy->id); | |
2398 | ||
d6e7a63d PS |
2399 | $timestart = new DateTime('@' . $eventcopy->timestart); |
2400 | $timestart->setTimezone(core_date::get_user_timezone_object()); | |
2401 | ||
76d9df3f SH |
2402 | for($i = 1; $i < $eventcopy->repeats; $i++) { |
2403 | ||
d6e7a63d PS |
2404 | $timestart->add(new DateInterval('P7D')); |
2405 | $eventcopy->timestart = $timestart->getTimestamp(); | |
76d9df3f SH |
2406 | |
2407 | // Get the event id for the log record. | |
2408 | $eventcopyid = $DB->insert_record('event', $eventcopy); | |
2409 | ||
2410 | // If the context has been set delete all associated files | |
2411 | if ($usingeditor) { | |
2412 | $fs = get_file_storage(); | |
64f93798 | 2413 | $files = $fs->get_area_files($this->editorcontext->id, 'calendar', 'event_description', $this->properties->id); |
76d9df3f SH |
2414 | foreach ($files as $file) { |
2415 | $fs->create_file_from_storedfile(array('itemid'=>$eventcopyid), $file); | |
2416 | } | |
2417 | } | |
2418 | ||
2419 | $repeatedids[] = $eventcopyid; | |
9cb5d05f AA |
2420 | |
2421 | // Trigger an event. | |
2422 | $eventargs['objectid'] = $eventcopyid; | |
2423 | $eventargs['other']['timestart'] = $eventcopy->timestart; | |
2424 | $event = \core\event\calendar_event_created::create($eventargs); | |
2425 | $event->trigger(); | |
76d9df3f SH |
2426 | } |
2427 | } | |
2428 | ||
2429 | // Hook for tracking added events | |
2430 | self::calendar_event_hook('add_event', array($this->properties, $repeatedids)); | |
2431 | return true; | |
2432 | } else { | |
2433 | ||
1d5bd3d2 DC |
2434 | if ($checkcapability) { |
2435 | if(!calendar_edit_event_allowed($this->properties)) { | |
2436 | print_error('nopermissiontoupdatecalendar'); | |
2437 | } | |
76d9df3f SH |
2438 | } |
2439 | ||
2440 | if ($usingeditor) { | |
2441 | if ($this->editorcontext !== null) { | |
2442 | $this->properties->description = file_save_draft_area_files( | |
2443 | $this->properties->description['itemid'], | |
2444 | $this->editorcontext->id, | |
64f93798 PS |
2445 | 'calendar', |
2446 | 'event_description', | |
76d9df3f SH |
2447 | $this->properties->id, |
2448 | $this->editoroptions, | |
2449 | $this->properties->description['text'], | |
2450 | $this->editoroptions['forcehttps']); | |
2451 | } else { | |
2452 | $this->properties->format = $this->properties->description['format']; | |
2453 | $this->properties->description = $this->properties->description['text']; | |
2454 | } | |
2455 | } | |
2456 | ||
2457 | $event = $DB->get_record('event', array('id'=>$this->properties->id)); | |
2458 | ||
2459 | $updaterepeated = (!empty($this->properties->repeatid) && !empty($this->properties->repeateditall)); | |
2460 | ||
2461 | if ($updaterepeated) { | |
2462 | // Update all | |
2463 | if ($this->properties->timestart != $event->timestart) { | |
2464 | $timestartoffset = $this->properties->timestart - $event->timestart; | |
2465 | $sql = "UPDATE {event} | |
2466 | SET name = ?, | |
2467 | description = ?, | |
2468 | timestart = timestart + ?, | |
2469 | timeduration = ?, | |
2470 | timemodified = ? | |
2471 | WHERE repeatid = ?"; | |
2472 | $params = array($this->properties->name, $this->properties->description, $timestartoffset, $this->properties->timeduration, time(), $event->repeatid); | |
2473 | } else { | |
2474 | $sql = "UPDATE {event} SET name = ?, description = ?, timeduration = ?, timemodified = ? WHERE repeatid = ?"; | |
2475 | $params = array($this->properties->name, $this->properties->description, $this->properties->timeduration, time(), $event->repeatid); | |
2476 | } | |
2477 | $DB->execute($sql, $params); | |
2478 | ||
9cb5d05f AA |
2479 | // Trigger an update event for each of the calendar event. |
2480 | $events = $DB->get_records('event', array('repeatid' => $event->repeatid), '', 'id,timestart'); | |
2481 | foreach ($events as $event) { | |
2482 | $eventargs['objectid'] = $event->id; | |
2483 | $eventargs['other']['timestart'] = $event->timestart; | |
2484 | $event = \core\event\calendar_event_updated::create($eventargs); | |
2485 | $event->trigger(); | |
2486 | } | |
76d9df3f SH |
2487 | } else { |
2488 | $DB->update_record('event', $this->properties); | |
ddaff608 SH |
2489 | $event = calendar_event::load($this->properties->id); |
2490 | $this->properties = $event->properties(); | |
9cb5d05f AA |
2491 | |
2492 | // Trigger an update event. | |
2493 | $event = \core\event\calendar_event_updated::create($eventargs); | |
2494 | $event->trigger(); | |
76d9df3f SH |
2495 | } |
2496 | ||
2497 | // Hook for tracking event updates | |
2498 | self::calendar_event_hook('update_event', array($this->properties, $updaterepeated)); | |
2499 | return true; | |
2500 | } | |
2501 | } | |
2502 | ||
2503 | /** | |
2504 | * Deletes an event and if selected an repeated events in the same series | |
2505 | * | |
2506 | * This function deletes an event, any associated events if $deleterepeated=true, | |
2507 | * and cleans up any files associated with the events. | |
2508 | * | |
2149326b | 2509 | * @see self::delete() |
76d9df3f | 2510 | * |
08b4a4e1 RW |
2511 | * @param bool $deleterepeated delete event repeatedly |
2512 | * @return bool succession of deleting event | |
76d9df3f SH |
2513 | */ |
2514 | public function delete($deleterepeated=false) { | |
f4700b91 | 2515 | global $DB; |
76d9df3f SH |
2516 | |
2517 | // If $this->properties->id is not set then something is wrong | |
2518 | if (empty($this->properties->id)) { | |
2519 | debugging('Attempting to delete an event before it has been loaded', DEBUG_DEVELOPER); | |
2520 | return false; | |
2521 | } | |
9cb5d05f | 2522 | $calevent = $DB->get_record('event', array('id' => $this->properties->id), '*', MUST_EXIST); |
76d9df3f SH |
2523 | // Delete the event |
2524 | $DB->delete_records('event', array('id'=>$this->properties->id)); | |
2525 | ||
9cb5d05f AA |
2526 | // Trigger an event for the delete action. |
2527 | $eventargs = array( | |
2528 | 'context' => $this->properties->context, | |
2529 | 'objectid' => $this->properties->id, | |
2530 | 'other' => array( | |
2531 | 'repeatid' => empty($this->properties->repeatid) ? 0 : $this->properties->repeatid, | |
2532 | 'timestart' => $this->properties->timestart, | |
2533 | 'name' => $this->properties->name | |
2534 | )); | |
2535 | $event = \core\event\calendar_event_deleted::create($eventargs); | |
2536 | $event->add_record_snapshot('event', $calevent); | |
2537 | $event->trigger(); | |
2538 | ||
ff284fac AA |
2539 | // If we are deleting parent of a repeated event series, promote the next event in the series as parent |
2540 | if (($this->properties->id == $this->properties->repeatid) && !$deleterepeated) { | |
2541 | $newparent = $DB->get_field_sql("SELECT id from {event} where repeatid = ? order by id ASC", array($this->properties->id), IGNORE_MULTIPLE); | |
2542 | if (!empty($newparent)) { | |
2543 | $DB->execute("UPDATE {event} SET repeatid = ? WHERE repeatid = ?", array($newparent, $this->properties->id)); | |
5ff114ad AA |
2544 | // Get all records where the repeatid is the same as the event being removed |
2545 | $events = $DB->get_records('event', array('repeatid' => $newparent)); | |
9cb5d05f | 2546 | // For each of the returned events trigger the event_update hook and an update event. |
5ff114ad | 2547 | foreach ($events as $event) { |
9cb5d05f AA |
2548 | // Trigger an event for the update. |
2549 | $eventargs['objectid'] = $event->id; | |
2550 | $eventargs['other']['timestart'] = $event->timestart; | |
2551 | $event = \core\event\calendar_event_updated::create($eventargs); | |
2552 | $event->trigger(); | |
2553 | ||
5ff114ad AA |
2554 | self::calendar_event_hook('update_event', array($event, false)); |
2555 | } | |
ff284fac AA |
2556 | } |
2557 | } | |
2558 | ||
76d9df3f SH |
2559 | // If the editor context hasn't already been set then set it now |
2560 | if ($this->editorcontext === null) { | |
f4700b91 | 2561 | $this->editorcontext = $this->properties->context; |
76d9df3f SH |
2562 | } |
2563 | ||
2564 | // If the context has been set delete all associated files | |
2565 | if ($this->editorcontext !== null) { | |
2566 | $fs = get_file_storage(); | |
64f93798 | 2567 | $files = $fs->get_area_files($this->editorcontext->id, 'calendar', 'event_description', $this->properties->id); |
76d9df3f SH |
2568 | foreach ($files as $file) { |
2569 | $file->delete(); | |
2570 | } | |
2571 | } | |
2572 | ||
2573 | // Fire the event deleted hook | |
2574 | self::calendar_event_hook('delete_event', array($this->properties->id, $deleterepeated)); | |
2575 | ||
2576 | // If we need to delete repeated events then we will fetch them all and delete one by one | |
2577 | if ($deleterepeated && !empty($this->properties->repeatid) && $this->properties->repeatid > 0) { | |
2578 | // Get all records where the repeatid is the same as the event being removed | |
2579 | $events = $DB->get_records('event', array('repeatid'=>$this->properties->repeatid)); | |
2580 | // For each of the returned events populate a calendar_event object and call delete | |
2581 | // make sure the arg passed is false as we are already deleting all repeats | |
2582 | foreach ($events as $event) { | |
2583 | $event = new calendar_event($event); | |
2584 | $event->delete(false); | |
2585 | } | |
2586 | } | |
2587 | ||
2588 | return true; | |
2589 | } | |
2590 | ||
2591 | /** | |
2592 | * Fetch all event properties | |
2593 | * | |
2594 | * This function returns all of the events properties as an object and optionally | |
2595 | * can prepare an editor for the description field at the same time. This is | |
2596 | * designed to work when the properties are going to be used to set the default | |
2597 | * values of a moodle forms form. | |
2598 | * | |
2599 | * @param bool $prepareeditor If set to true a editor is prepared for use with | |
2600 | * the mforms editor element. (for description) | |
2601 | * @return stdClass Object containing event properties | |
2602 | */ | |
2603 | public function properties($prepareeditor=false) { | |
2604 | global $USER, $CFG, $DB; | |
2605 | ||
2606 | // First take a copy of the properties. We don't want to actually change the | |
2607 | // properties or we'd forever be converting back and forwards between an | |
2608 | // editor formatted description and not | |
2609 | $properties = clone($this->properties); | |
2610 | // Clean the description here | |
2611 | $properties->description = clean_text($properties->description, $properties->format); | |
2612 | ||
2613 | // If set to true we need to prepare the properties for use with an editor | |
2614 | // and prepare the file area | |
2615 | if ($prepareeditor) { | |
2616 | ||
2617 | // We may or may not have a property id. If we do then we need to work | |
2618 | // out the context so we can copy the existing files to the draft area | |
2619 | if (!empty($properties->id)) { | |
2620 | ||
2621 | if ($properties->eventtype === 'site') { | |
2622 | // Site context | |
f4700b91 | 2623 | $this->editorcontext = $this->properties->context; |
76d9df3f SH |
2624 | } else if ($properties->eventtype === 'user') { |
2625 | // User context | |
f4700b91 | 2626 | $this->editorcontext = $this->properties->context; |
76d9df3f SH |
2627 | } else if ($properties->eventtype === 'group' || $properties->eventtype === 'course') { |
2628 | // First check the course is valid | |
2629 | $course = $DB->get_record('course', array('id'=>$properties->courseid)); | |
2630 | if (!$course) { | |
2631 | print_error('invalidcourse'); | |
2632 | } | |
2633 | // Course context | |
f4700b91 | 2634 | $this->editorcontext = $this->properties->context; |
76d9df3f SH |
2635 | // We have a course and are within the course context so we had |
2636 | // better use the courses max bytes value | |
2637 | $this->editoroptions['maxbytes'] = $course->maxbytes; | |
2638 | } else { | |
2639 | // If we get here we have a custom event type as used by some | |
2640 | // modules. In this case the event will have been added by | |
2641 | // code and we won't need the editor | |
2642 | $this->editoroptions['maxbytes'] = 0; | |
2643 | $this->editoroptions['maxfiles'] = 0; | |
2644 | } | |
2645 | ||
2646 | if (empty($this->editorcontext) || empty($this->editorcontext->id)) { | |
2647 | $contextid = false; | |
2648 | } else { | |
2649 | // Get the context id that is what we really want | |
2650 | $contextid = $this->editorcontext->id; | |
2651 | } | |
2652 | } else { | |
2653 | ||
2654 | // If we get here then this is a new event in which case we don't need a | |
2655 | // context as there is no existing files to copy to the draft area. | |
2656 | $contextid = null; | |
2657 | } | |
2658 | ||
2659 | // If the contextid === false we don't support files so no preparing | |
2660 | // a draft area | |
2661 | if ($contextid !== false) { | |
2662 | // Just encase it has already been submitted | |
2663 | $draftiddescription = file_get_submitted_draft_itemid('description'); | |
2664 | // Prepare the draft area, this copies existing files to the draft area as well | |
64f93798 | 2665 | $properties->description = file_prepare_draft_area($draftiddescription, $contextid, 'calendar', 'event_description', $properties->id, $this->editoroptions, $properties->description); |
76d9df3f SH |
2666 | } else { |
2667 | $draftiddescription = 0; | |
2668 | } | |
aa6c1ced | 2669 | |
76d9df3f SH |
2670 | // Structure the description field as the editor requires |
2671 | $properties->description = array('text'=>$properties->description, 'format'=>$properties->format, 'itemid'=>$draftiddescription); | |
2672 | } | |
2673 | ||
2674 | // Finally return the properties | |
2675 | return $properties; | |
2676 | } | |
2677 | ||
2678 | /** | |
2679 | * Toggles the visibility of an event | |
2680 | * | |
2681 | * @param null|bool $force If it is left null the events visibility is flipped, | |
2682 | * If it is false the event is made hidden, if it is true it | |
2683 | * is made visible. | |
08b4a4e1 | 2684 | * @return bool if event is successfully updated, toggle will be visible |
76d9df3f SH |
2685 | */ |
2686 | public function toggle_visibility($force=null) { | |
2687 | global $CFG, $DB; | |
2688 | ||
2689 | // Set visible to the default if it is not already set | |
2690 | if (empty($this->properties->visible)) { | |
2691 | $this->properties->visible = 1; | |
2692 | } | |
2693 | ||
2694 | if ($force === true || ($force !== false && $this->properties->visible == 0)) { | |
2695 | // Make this event visible | |
2696 | $this->properties->visible = 1; | |
2697 | // Fire the hook | |
2698 | self::calendar_event_hook('show_event', array($this->properties)); | |
2699 | } else { | |
2700 | // Make this event hidden | |
2701 | $this->properties->visible = 0; | |
2702 | // Fire the hook | |
2703 | self::calendar_event_hook('hide_event', array($this->properties)); | |
2704 | } | |
2705 | ||
2706 | // Update the database to reflect this change | |
2707 | return $DB->set_field('event', 'visible', $this->properties->visible, array('id'=>$this->properties->id)); | |
2708 | } | |
2709 | ||
2710 | /** | |
2711 | * Attempts to call the hook for the specified action should a calendar type | |
2712 | * by set $CFG->calendar, and the appopriate function defined | |
2713 | * | |
76d9df3f SH |
2714 | * @param string $action One of `update_event`, `add_event`, `delete_event`, `show_event`, `hide_event` |
2715 | * @param array $args The args to pass to the hook, usually the event is the first element | |
08b4a4e1 | 2716 | * @return bool attempts to call event hook |
76d9df3f SH |
2717 | */ |
2718 | public static function calendar_event_hook($action, array $args) { | |
2719 | global $CFG; | |
2720 | static $extcalendarinc; | |
2721 | if ($extcalendarinc === null) { | |
d2b46849 BJ |
2722 | if (!empty($CFG->calendar)) { |
2723 | if (is_readable($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php')) { | |
2724 | include_once($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php'); | |
2725 | $extcalendarinc = true; | |
2726 | } else { | |
2727 | debugging("Calendar lib file missing or not readable at /calendar/{$CFG->calendar}/lib.php.", | |
2728 | DEBUG_DEVELOPER); | |
2729 | $extcalendarinc = false; | |
2730 | } | |
76d9df3f SH |
2731 | } else { |
2732 | $extcalendarinc = false; | |
2733 | } | |
2734 | } | |
2735 | if($extcalendarinc === false) { | |
2736 | return false; | |
2737 | } | |
438e4add | 2738 | $hook = $CFG->calendar .'_'.$action; |
76d9df3f SH |
2739 | if (function_exists($hook)) { |
2740 | call_user_func_array($hook, $args); | |
2741 | return true; | |
2742 | } | |
2743 | return false; | |
2744 | } | |
2745 | ||
2746 | /** | |
2747 | * Returns a calendar_event object when provided with an event id | |
2748 | * | |
2749 | * This function makes use of MUST_EXIST, if the event id passed in is invalid | |
2750 | * it will result in an exception being thrown | |
2751 | * | |
08b4a4e1 RW |
2752 | * @param int|object $param event object or event id |
2753 | * @return calendar_event|false status for loading calendar_event | |
76d9df3f | 2754 | */ |
fd699564 | 2755 | public static function load($param) { |
76d9df3f | 2756 | global $DB; |
fd699564 SH |
2757 | if (is_object($param)) { |
2758 | $event = new calendar_event($param); | |
2759 | } else { | |
2760 | $event = $DB->get_record('event', array('id'=>(int)$param), '*', MUST_EXIST); | |
2761 | $event = new calendar_event($event); | |
2762 | } | |
76d9df3f SH |
2763 | return $event; |
2764 | } | |
2765 | ||
2766 | /** | |
2767 | * Creates a new event and returns a calendar_event object | |
2768 | * | |
0f1e1313 AA |
2769 | * @param stdClass|array $properties An object containing event properties |
2770 | * @param bool $checkcapability Check caps or not | |
2771 | * @throws coding_exception | |
2772 | * | |
2773 | * @return calendar_event|bool The event object or false if it failed | |
76d9df3f | 2774 | */ |
0f1e1313 | 2775 | public static function create($properties, $checkcapability = true) { |
76d9df3f SH |
2776 | if (is_array($properties)) { |
2777 | $properties = (object)$properties; | |
2778 | } | |
2779 | if (!is_object($properties)) { | |
2780 | throw new coding_exception('When creating an event properties should be either an object or an assoc array'); | |
2781 | } | |
f4700b91 | 2782 | $event = new calendar_event($properties); |
0f1e1313 | 2783 | if ($event->update($properties, $checkcapability)) { |
76d9df3f SH |
2784 | return $event; |
2785 | } else { | |
2786 | return false; | |
2787 | } | |
2788 | } | |
871988b0 JL |
2789 | |
2790 | /** | |
2791 | * Format the text using the external API. | |
2792 | * This function should we used when text formatting is required in external functions. | |
2793 | * | |
2794 | * @return array an array containing the text formatted and the text format | |
2795 | */ | |
2796 | public function format_external_text() { | |
2797 | ||
2798 | if ($this->editorcontext === null) { | |
2799 | // Switch on the event type to decide upon the appropriate context to use for this event. | |
2800 | $this->editorcontext = $this->properties->context; | |
2801 | ||
2802 | if ($this->properties->eventtype != 'user' && $this->properties->eventtype != 'course' | |
2803 | && $this->properties->eventtype != 'site' && $this->properties->eventtype != 'group') { | |
2804 | // We don't have a context here, do a normal format_text. | |
833be5e4 | 2805 | return external_format_text($this->properties->description, $this->properties->format, $this->editorcontext->id); |
871988b0 JL |
2806 | } |
2807 | } | |
2808 | ||
2809 | // Work out the item id for the editor, if this is a repeated event then the files will be associated with the original. | |
2810 | if (!empty($this->properties->repeatid) && $this->properties->repeatid > 0) { | |
2811 | $itemid = $this->properties->repeatid; | |
2812 | } else { | |
2813 | $itemid = $this->properties->id; | |
2814 | } | |
2815 | ||
2816 | return external_format_text($this->properties->description, $this->properties->format, $this->editorcontext->id, | |
2817 | 'calendar', 'event_description', $itemid); | |
2818 | } | |
76d9df3f | 2819 | } |
36dc3b71 SH |
2820 | |
2821 | /** | |
2822 | * Calendar information class | |
2823 | * | |
2824 | * This class is used simply to organise the information pertaining to a calendar | |
2825 | * and is used primarily to make information easily available. | |
08b4a4e1 RW |
2826 | * |
2827 | * @package core_calendar | |
2828 | * @category calendar | |
2829 | * @copyright 2010 Sam Hemelryk | |
2830 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
36dc3b71 SH |
2831 | */ |
2832 | class calendar_information { | |
08b4a4e1 | 2833 | |
da304137 MN |
2834 | /** |
2835 | * @var int The timestamp | |
2836 | * | |
2837 | * Rather than setting the day, month and year we will set a timestamp which will be able | |
2838 | * to be used by multiple calendars. | |
2839 | */ | |
2840 | public $time; | |
36dc3b71 | 2841 | |
08b4a4e1 | 2842 | /** @var int A course id */ |
36dc3b71 | 2843 | public $courseid = null; |
08b4a4e1 RW |
2844 | |
2845 | /** @var array An array of courses */ | |
36dc3b71 | 2846 | public $courses = array(); |
08b4a4e1 RW |
2847 | |
2848 | /** @var array An array of groups */ | |
36dc3b71 | 2849 | public $groups = array(); |
08b4a4e1 RW |
2850 | |
2851 | /** @var array An array of users */ | |
36dc3b71 SH |
2852 | public $users = array(); |
2853 | ||
2854 | /** | |
2855 | * Creates a new instance | |
2856 | * | |
08b4a4e1 RW |
2857 | * @param int $day the number of the day |
2858 | * @param int $month the number of the month | |
2859 | * @param int $year the number of the year | |
da304137 MN |
2860 | * @param int $time the unixtimestamp representing the date we want to view, this is used instead of $calmonth |
2861 | * and $calyear to support multiple calendars | |
36dc3b71 | 2862 | */ |
da304137 MN |
2863 | public function __construct($day = 0, $month = 0, $year = 0, $time = 0) { |
2864 | // If a day, month and year were passed then convert it to a timestamp. If these were passed | |
2865 | // then we can assume the day, month and year are passed as Gregorian, as no where in core | |
2866 | // should we be passing these values rather than the time. This is done for BC. | |
2867 | if (!empty($day) || !empty($month) || !empty($year)) { | |
2868 | $date = usergetdate(time()); | |
2869 | if (empty($day)) { | |
2870 | $day = $date['mday']; | |
2871 | } | |
2872 | if (empty($month)) { | |
2873 | $month = $date['mon']; | |
2874 | } | |
2875 | if (empty($year)) { | |
2876 | $year = $date['year']; | |
2877 | } | |
2878 | if (checkdate($month, $day, $year)) { | |
2879 | $this->time = make_timestamp($year, $month, $day); | |
2880 | } else { | |
2881 | $this->time = time(); | |
2882 | } | |
2883 | } else if (!empty($time)) { | |
2884 | $this->time = $time; | |
2885 | } else { | |
2886 | $this->time = time(); | |
d8d8bdd9 | 2887 | } |
36dc3b71 SH |
2888 | } |
2889 | ||
797cedc7 | 2890 | /** |
08b4a4e1 | 2891 | * Initialize calendar information |
797cedc7 | 2892 | * |
08b4a4e1 | 2893 | * @param stdClass $course object |
797cedc7 | 2894 | * @param array $coursestoload An array of courses [$course->id => $course] |
08b4a4e1 | 2895 | * @param bool $ignorefilters options to use filter |
797cedc7 SH |
2896 | */ |
2897 | public function prepare_for_view(stdClass $course, array $coursestoload, $ignorefilters = false) { | |
2898 | $this->courseid = $course->id; | |
2899 | $this->course = $course; | |
2900 | list($courses, $group, $user) = calendar_set_filters($coursestoload, $ignorefilters); | |
2901 | $this->courses = $courses; | |
2902 | $this->groups = $group; | |
2903 | $this->users = $user; | |
2904 | } | |
2905 | ||
36dc3b71 SH |
2906 | /** |
2907 | * Ensures the date for the calendar is correct and either sets it to now | |
2908 | * or throws a moodle_exception if not | |
2909 | * | |
08b4a4e1 RW |
2910 | * @param bool $defaultonow use current time |
2911 | * @throws moodle_exception | |
2912 | * @return bool validation of checkdate | |
36dc3b71 SH |
2913 | */ |
2914 | public function checkdate($defaultonow = true) { | |
2915 | if (!checkdate($this->month, $this->day, $this->year)) { | |
2916 | if ($defaultonow) { | |
2917 | $now = usergetdate(time()); | |
2918 | $this->day = intval($now['mday']); | |
2919 | $this->month = intval($now['mon']); | |
2920 | $this->year = intval($now['year']); | |
2921 | return true; | |
2922 | } else { | |
2923 | throw new moodle_exception('invaliddate'); | |
2924 | } | |
2925 | } | |
2926 | return true; | |
2927 | } | |
da304137 | 2928 | |
36dc3b71 SH |
2929 | /** |
2930 | * Gets todays timestamp for the calendar | |
08b4a4e1 RW |
2931 | * |
2932 | * @return int today timestamp | |
36dc3b71 SH |
2933 | */ |
2934 | public function timestamp_today() { | |
da304137 | 2935 | return $this->time; |
36dc3b71 SH |
2936 | } |
2937 | /** | |
2938 | * Gets tomorrows timestamp for the calendar | |
08b4a4e1 RW |
2939 | * |
2940 | * @return int tomorrow timestamp | |
36dc3b71 SH |
2941 | */ |
2942 | public function timestamp_tomorrow() { | |
a0ef87de | 2943 | return strtotime('+1 day', $this->time); |
36dc3b71 SH |
2944 | } |
2945 | /** | |
e30390a0 | 2946 | * Adds the pretend blocks for the calendar |
36dc3b71 SH |
2947 | * |
2948 | * @param core_calendar_renderer $renderer | |
08b4a4e1 RW |
2949 | * @param bool $showfilters display filters, false is set as default |
2950 | * @param string|null $view preference view options (eg: day, month, upcoming) | |
36dc3b71 SH |
2951 | */ |
2952 | public function add_sidecalendar_blocks(core_calendar_renderer $renderer, $showfilters=false, $view=null) { | |
2953 | if ($showfilters) { | |
2954 | $filters = new block_contents(); | |
da304137 | 2955 | $filters->content = $renderer->fake_block_filters($this->courseid, 0, 0, 0, $view, $this->courses); |
36dc3b71 SH |
2956 | $filters->footer = ''; |
2957 | $filters->title = get_string('eventskey', 'calendar'); | |
2958 | $renderer->add_pretend_calendar_block($filters, BLOCK_POS_RIGHT); | |
2959 | } | |
2960 | $block = new block_contents; | |
2961 | $block->content = $renderer->fake_block_threemonths($this); | |
2962 | $block->footer = ''; | |
2963 | $block->title = get_string('monthlyview', 'calendar'); | |
2964 | $renderer->add_pretend_calendar_block($block, BLOCK_POS_RIGHT); | |
2965 | } | |
1d5bd3d2 | 2966 | } |
b5a52acd JH |
2967 | |
2968 | /** | |
e30390a0 SH |
2969 | * Returns option list for the poll interval setting. |
2970 | * | |
2971 | * @return array An array of poll interval options. Interval => description. | |
b5a52acd JH |
2972 | */ |
2973 | function calendar_get_pollinterval_choices() { | |
2974 | return array( | |
e30390a0 | 2975 | '0' => new lang_string('never', 'calendar'), |
0f1e1313 AA |
2976 | HOURSECS => new lang_string('hourly', 'calendar'), |
2977 | DAYSECS => new lang_string('daily', 'calendar'), | |
2978 | WEEKSECS => new lang_string('weekly', 'calendar'), | |
e30390a0 | 2979 | '2628000' => new lang_string('monthly', 'calendar'), |
0f1e1313 | 2980 | YEARSECS => new lang_string('annually', 'calendar') |
e30390a0 | 2981 | ); |
b5a52acd JH |
2982 | } |
2983 | ||
2984 | /** | |
e30390a0 SH |
2985 | * Returns option list of available options for the calendar event type, given the current user and course. |
2986 | * | |
4c349ad7 | 2987 | * @param int $courseid The id of the course |
e30390a0 | 2988 | * @return array An array containing the event types the user can create. |
b5a52acd JH |
2989 | */ |
2990 | function calendar_get_eventtype_choices($courseid) { | |
2991 | $choices = array(); | |
2992 | $allowed = new stdClass; | |
2993 | calendar_get_allowed_types($allowed, $courseid); | |
2994 | ||
2995 | if ($allowed->user) { | |
35ad5fc6 | 2996 | $choices['user'] = get_string('userevents', 'calendar'); |
b5a52acd JH |
2997 | } |
2998 | if ($allowed->site) { | |
6bf410ed | 2999 | $choices['site'] = get_string('siteevents', 'calendar'); |
b5a52acd JH |
3000 | } |
3001 | if (!empty($allowed->courses)) { | |
35ad5fc6 | 3002 | $choices['course'] = get_string('courseevents', 'calendar'); |
b5a52acd JH |
3003 | } |
3004 | if (!empty($allowed->groups) and is_array($allowed->groups)) { | |
3005 | $choices['group'] = get_string('group'); | |
3006 | } | |
3007 | ||
3008 | return array($choices, $allowed->groups); | |
3009 | } | |
3010 | ||
b5a52acd JH |
3011 | /** |
3012 | * Add an iCalendar subscription to the database. | |
e30390a0 SH |
3013 | * |
3014 | * @param stdClass $sub The subscription object (e.g. from the form) | |
3015 | * @return int The insert ID, if any. | |
b5a52acd JH |
3016 | */ |
3017 | function calendar_add_subscription($sub) { | |
35ad5fc6 | 3018 | global $DB, $USER, $SITE; |
e30390a0 | 3019 | |
35ad5fc6 AA |
3020 | if ($sub->eventtype === 'site') { |
3021 | $sub->courseid = $SITE->id; | |
3022 | } else if ($sub->eventtype === 'group' || $sub->eventtype === 'course') { | |
b5a52acd | 3023 | $sub->courseid = $sub->course; |
35ad5fc6 AA |
3024 | } else { |
3025 | // User events. | |
3026 | $sub->courseid = 0; | |
b5a52acd JH |
3027 | } |
3028 | $sub->userid = $USER->id; | |
3029 | ||
e30390a0 | 3030 | // File subscriptions never update. |
b5a52acd JH |
3031 | if (empty($sub->url)) { |
3032 | $sub->pollinterval = 0; | |
3033 | } | |
3034 | ||
3035 | if (!empty($sub->name)) { | |
3036 | if (empty($sub->id)) { | |
3037 | $id = $DB->insert_record('event_subscriptions', $sub); | |
e73b527c | 3038 | // we cannot cache the data here because $sub is not complete. |
756e3c55 SB |
3039 | $sub->id = $id; |
3040 | // Trigger event, calendar subscription added. | |
3041 | $eventparams = array('objectid' => $sub->id, | |
3042 | 'context' => calendar_get_calendar_context($sub), | |
3043 | 'other' => array('eventtype' => $sub->eventtype, 'courseid' => $sub->courseid) | |
3044 | ); | |
3045 | $event = \core\event\calendar_subscription_created::create($eventparams); | |
3046 | $event->trigger(); | |
b5a52acd JH |
3047 | return $id; |
3048 | } else { | |
e73b527c | 3049 | // Why are we doing an update here? |
ab37339d | 3050 | calendar_update_subscription($sub); |
b5a52acd JH |
3051 | return $sub->id; |
3052 | } | |
3053 | } else { | |
3054 | print_error('errorbadsubscription', 'importcalendar'); | |
3055 | } | |
3056 | } | |
3057 | ||
3058 | /** | |
3059 | * Add an iCalendar event to the Moodle calendar. | |
e30390a0 | 3060 | * |
0f1e1313 | 3061 | * @param stdClass $event The RFC-2445 iCalendar event |
e30390a0 SH |
3062 | * @param int $courseid The course ID |
3063 | * @param int $subscriptionid The iCalendar subscription ID | |
accc3ced | 3064 | * @param string $timezone The X-WR-TIMEZONE iCalendar property if provided |
e73b527c | 3065 | * @throws dml_exception A DML specific exception is thrown for invalid subscriptionids. |
0f1e1313 | 3066 | * @return int Code: CALENDAR_IMPORT_EVENT_UPDATED = updated, CALENDAR_IMPORT_EVENT_INSERTED = inserted, 0 = error |
b5a52acd | 3067 | */ |
accc3ced | 3068 | function calendar_add_icalendar_event($event, $courseid, $subscriptionid, $timezone='UTC') { |
0f1e1313 | 3069 | global $DB; |
b5a52acd | 3070 | |
e30390a0 | 3071 | // Probably an unsupported X-MICROSOFT-CDO-BUSYSTATUS event. |
b5a52acd JH |
3072 | if (empty($event->properties['SUMMARY'])) { |
3073 | return 0; | |
3074 | } | |
3075 | ||
3076 | $name = $event->properties['SUMMARY'][0]->value; | |
3077 | $name = str_replace('\n', '<br />', $name); | |
3078 | $name = str_replace('\\', '', $name); | |
e737c2ff | 3079 | $name = preg_replace('/\s+/u', ' ', $name); |
e30390a0 SH |
3080 | |
3081 | $eventrecord = new stdClass; | |
4c349ad7 | 3082 | $eventrecord->name = clean_param($name, PARAM_NOTAGS); |
b5a52acd JH |
3083 | |
3084 | if (empty($event->properties['DESCRIPTION'][0]->value)) { | |
3085 | $description = ''; | |
3086 | } else { | |
3087 | $description = $event->properties['DESCRIPTION'][0]->value; | |
575e27f3 | 3088 | $description = clean_param($description, PARAM_NOTAGS); |
b5a52acd JH |
3089 | $description = str_replace('\n', '<br />', $description); |
3090 | $description = str_replace('\\', '', $description); | |
e737c2ff | 3091 | $description = preg_replace('/\s+/u', ' ', $description); |
b5a52acd | 3092 | } |
575e27f3 | 3093 | $eventrecord->description = $description; |
b5a52acd | 3094 | |
e30390a0 | 3095 | // Probably a repeating event with RRULE etc. TODO: skip for now. |
b5a52acd JH |
3096 | if (empty($event->properties['DTSTART'][0]->value)) { |
3097 | return 0; | |
3098 | } | |
3099 | ||
0f1e1313 | 3100 | $tz = isset($event->properties['DTSTART'][0]->parameters['TZID']) ? $event->properties['DTSTART'][0]->parameters['TZID'] : |
accc3ced | 3101 | $timezone; |
d6e7a63d | 3102 | $tz = core_date::normalise_timezone($tz); |
0f1e1313 | 3103 | $eventrecord->timestart = strtotime($event->properties['DTSTART'][0]->value . ' ' . $tz); |
b5a52acd | 3104 | if (empty($event->properties['DTEND'])) { |
ee19ae69 | 3105 | $eventrecord->timeduration = 0; // no duration if no end time specified |
b5a52acd | 3106 | } else { |
0f1e1313 | 3107 | $endtz = isset($event->properties['DTEND'][0]->parameters['TZID']) ? $event->properties['DTEND'][0]->parameters['TZID'] : |
accc3ced | 3108 | $timezone; |
d6e7a63d | 3109 | $endtz = core_date::normalise_timezone($endtz); |
0f1e1313 | 3110 | $eventrecord->timeduration = strtotime($event->properties['DTEND'][0]->value . ' ' . $endtz) - $eventrecord->timestart; |
b5a52acd | 3111 | } |
accc3ced SB |
3112 | |
3113 | // Check to see if it should be treated as an all day event. | |
3114 | if ($eventrecord->timeduration == DAYSECS) { | |
3115 | // Check to see if the event started at Midnight on the imported calendar. | |
3116 | date_default_timezone_set($timezone); | |
3117 | if (date('H:i:s', $eventrecord->timestart) === "00:00:00") { | |
3118 | // This event should be an all day event. | |
3119 | $eventrecord->timeduration = 0; | |
3120 | } | |
d6e7a63d | 3121 | core_date::set_default_server_timezone(); |
accc3ced SB |
3122 | } |
3123 | ||
b5a52acd JH |
3124 | $eventrecord->uuid = $event->properties['UID'][0]->value; |
3125 | $eventrecord->timemodified = time(); | |
3126 | ||
e30390a0 | 3127 | // Add the iCal subscription details if required. |
e73b527c AA |
3128 | // We should never do anything with an event without a subscription reference. |
3129 | $sub = calendar_get_subscription($subscriptionid); | |
3130 | $eventrecord->subscriptionid = $subscriptionid; | |
3131 | $eventrecord->userid = $sub->userid; | |
3132 | $eventrecord->groupid = $sub->groupid; | |
3133 | $eventrecord->courseid = $sub->courseid; | |
3134 | $eventrecord->eventtype = $sub->eventtype; | |
b5a52acd | 3135 | |
45583580 | 3136 | if ($updaterecord = $DB->get_record('event', array('uuid' => $eventrecord->uuid, 'subscriptionid' => $eventrecord->subscriptionid))) { |
b5a52acd | 3137 | $eventrecord->id = $updaterecord->id; |
0f1e1313 | 3138 | $return = CALENDAR_IMPORT_EVENT_UPDATED; // Update. |
b5a52acd | 3139 | } else { |
0f1e1313 AA |
3140 | $return = CALENDAR_IMPORT_EVENT_INSERTED; // Insert. |
3141 | } | |
3142 | if ($createdevent = calendar_event::create($eventrecord, false)) { | |
3143 | if (!empty($event->properties['RRULE'])) { | |
3144 | // Repeating events. | |
3145 | date_default_timezone_set($tz); // Change time zone to parse all events. | |
3146 | $rrule = new \core_calendar\rrule_manager($event->properties['RRULE'][0]->value); | |
3147 | $rrule->parse_rrule(); | |
3148 | $rrule->create_events($createdevent); | |
234e929e | 3149 | core_date::set_default_server_timezone(); // Change time zone back to what it was. |
0f1e1313 AA |
3150 | } |
3151 | return $return; | |
3152 | } else { | |
3153 | return 0; | |
b5a52acd JH |
3154 | } |
3155 | } | |
3156 | ||
3157 | /** | |
e30390a0 SH |
3158 | * Update a subscription from the form data in one of the rows in the existing subscriptions table. |
3159 | * | |
3160 | * @param int $subscriptionid The ID of the subscription we are acting upon. | |
3161 | * @param int $pollinterval The poll interval to use. | |
3162 | * @param int $action The action to be performed. One of update or remove. | |
e73b527c | 3163 | * @throws dml_exception if invalid subscriptionid is provided |
e30390a0 | 3164 | * @return string A log of the import progress, including errors |
b5a52acd | 3165 | */ |
e30390a0 | 3166 | function calendar_process_subscription_row($subscriptionid, $pollinterval, $action) { |
b5a52acd | 3167 | |
e30390a0 | 3168 | // Fetch the subscription from the database making sure it exists. |
e73b527c | 3169 | $sub = calendar_get_subscription($subscriptionid); |
b5a52acd | 3170 | |
e30390a0 | 3171 | // Update or remove the subscription, based on action. |
b5a52acd | 3172 | switch ($action) { |
ee74a2a1 | 3173 | case CALENDAR_SUBSCRIPTION_UPDATE: |
e30390a0 SH |
3174 | // Skip updating file subscriptions. |
3175 | if (empty($sub->url)) { | |
3176 | break; | |
3177 | } | |
3178 | $sub->pollinterval = $pollinterval; | |
ab37339d | 3179 | calendar_update_subscription($sub); |
e73b527c | 3180 | |
e30390a0 SH |
3181 | // Update the events. |
3182 | return "<p>".get_string('subscriptionupdated', 'calendar', $sub->name)."</p>" . calendar_update_subscription_events($subscriptionid); | |
b5a52acd | 3183 | |
ee74a2a1 | 3184 | case CALENDAR_SUBSCRIPTION_REMOVE: |
f40c37d0 | 3185 | calendar_delete_subscription($subscriptionid); |
e30390a0 SH |
3186 | return get_string('subscriptionremoved', 'calendar', $sub->name); |
3187 | break; | |
b5a52acd | 3188 | |
e30390a0 SH |
3189 | default: |
3190 | break; | |
b5a52acd JH |
3191 | } |
3192 | return ''; | |
3193 | } | |
3194 | ||
f40c37d0 RT |
3195 | /** |
3196 | * Delete subscription and all related events. | |
3197 | * | |
3198 | * @param int|stdClass $subscription subscription or it's id, which needs to be deleted. | |
3199 | */ | |
3200 | function calendar_delete_subscription($subscription) { | |
3201 | global $DB; | |
3202 | ||
756e3c55 SB |
3203 | if (!is_object($subscription)) { |
3204 | $subscription = $DB->get_record('event_subscriptions', array('id' => $subscription), '*', MUST_EXIST); | |
f40c37d0 RT |
3205 | } |
3206 | // Delete subscription and related events. | |
756e3c55 SB |
3207 | $DB->delete_records('event', array('subscriptionid' => $subscription->id)); |
3208 | $DB->delete_records('event_subscriptions', array('id' => $subscription->id)); | |
3209 | cache_helper::invalidate_by_definition('core', 'calendar_subscriptions', array(), array($subscription->id)); | |
3210 | ||
3211 | // Trigger event, calendar subscription deleted. | |
3212 | $eventparams = array('objectid' => $subscription->id, | |
3213 | 'context' => calendar_get_calendar_context($subscription), | |
3214 | 'other' => array('courseid' => $subscription->courseid) | |
3215 | ); | |
3216 | $event = \core\event\calendar_subscription_deleted::create($eventparams); | |
3217 | $event->trigger(); | |
f40c37d0 | 3218 | } |
b5a52acd JH |
3219 | /** |
3220 | * From a URL, fetch the calendar and return an iCalendar object. | |
e30390a0 SH |
3221 | * |
3222 | * @param string $url The iCalendar URL | |
3223 | * @return stdClass The iCalendar object | |
b5a52acd JH |
3224 | */ |
3225 | function calendar_get_icalendar($url) { | |
e30390a0 SH |
3226 | global $CFG; |
3227 | ||
3228 | require_once($CFG->libdir.'/filelib.php'); | |
3229 | ||
3230 | $curl = new curl(); | |
20bdf997 | 3231 | $curl->setopt(array('CURLOPT_FOLLOWLOCATION' => 1, 'CURLOPT_MAXREDIRS' => 5)); |
e30390a0 | 3232 | $calendar = $curl->get($url); |
20bdf997 AA |
3233 | // Http code validation should actually be the job of curl class. |
3234 | if (!$calendar || $curl->info['http_code'] != 200 || !empty($curl->errorno)) { | |
e30390a0 SH |
3235 | throw new moodle_exception('errorinvalidicalurl', 'calendar'); |
3236 | } | |
3237 | ||
b5a52acd JH |
3238 | $ical = new iCalendar(); |
3239 | $ical->unserialize($calendar);< |