MDL-67814 core_h5p: added renderer and editor classes
[moodle.git] / calendar / lib.php
blobf378c102b0a0c96006328d3a792d5ef5c9f7bdf6
1 <?php
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.
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/>.
17 /**
18 * Calendar extension
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
26 if (!defined('MOODLE_INTERNAL')) {
27 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
30 /**
31 * These are read by the administration component to provide default values
34 /**
35 * CALENDAR_DEFAULT_UPCOMING_LOOKAHEAD - default value of upcoming event preference
37 define('CALENDAR_DEFAULT_UPCOMING_LOOKAHEAD', 21);
39 /**
40 * CALENDAR_DEFAULT_UPCOMING_MAXEVENTS - default value to display the maximum number of upcoming event
42 define('CALENDAR_DEFAULT_UPCOMING_MAXEVENTS', 10);
44 /**
45 * CALENDAR_DEFAULT_STARTING_WEEKDAY - default value to display the starting weekday
47 define('CALENDAR_DEFAULT_STARTING_WEEKDAY', 1);
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
52 /**
53 * CALENDAR_DEFAULT_WEEKEND - default value for weekend (Saturday & Sunday)
55 define('CALENDAR_DEFAULT_WEEKEND', 65);
57 /**
58 * CALENDAR_URL - path to calendar's folder
60 define('CALENDAR_URL', $CFG->wwwroot.'/calendar/');
62 /**
63 * CALENDAR_TF_24 - Calendar time in 24 hours format
65 define('CALENDAR_TF_24', '%H:%M');
67 /**
68 * CALENDAR_TF_12 - Calendar time in 12 hours format
70 define('CALENDAR_TF_12', '%I:%M %p');
72 /**
73 * CALENDAR_EVENT_GLOBAL - Site calendar event types
74 * @deprecated since 3.8
76 define('CALENDAR_EVENT_GLOBAL', 1);
78 /**
79 * CALENDAR_EVENT_SITE - Site calendar event types
81 define('CALENDAR_EVENT_SITE', 1);
83 /**
84 * CALENDAR_EVENT_COURSE - Course calendar event types
86 define('CALENDAR_EVENT_COURSE', 2);
88 /**
89 * CALENDAR_EVENT_GROUP - group calendar event types
91 define('CALENDAR_EVENT_GROUP', 4);
93 /**
94 * CALENDAR_EVENT_USER - user calendar event types
96 define('CALENDAR_EVENT_USER', 8);
98 /**
99 * CALENDAR_EVENT_COURSECAT - Course category calendar event types
101 define('CALENDAR_EVENT_COURSECAT', 16);
104 * CALENDAR_IMPORT_FROM_FILE - import the calendar from a file
106 define('CALENDAR_IMPORT_FROM_FILE', 0);
109 * CALENDAR_IMPORT_FROM_URL - import the calendar from a URL
111 define('CALENDAR_IMPORT_FROM_URL', 1);
114 * CALENDAR_IMPORT_EVENT_UPDATED_SKIPPED - imported event was skipped
116 define('CALENDAR_IMPORT_EVENT_SKIPPED', -1);
119 * CALENDAR_IMPORT_EVENT_UPDATED - imported event was updated
121 define('CALENDAR_IMPORT_EVENT_UPDATED', 1);
124 * CALENDAR_IMPORT_EVENT_INSERTED - imported event was added by insert
126 define('CALENDAR_IMPORT_EVENT_INSERTED', 2);
129 * CALENDAR_SUBSCRIPTION_UPDATE - Used to represent update action for subscriptions in various forms.
131 define('CALENDAR_SUBSCRIPTION_UPDATE', 1);
134 * CALENDAR_SUBSCRIPTION_REMOVE - Used to represent remove action for subscriptions in various forms.
136 define('CALENDAR_SUBSCRIPTION_REMOVE', 2);
139 * CALENDAR_EVENT_USER_OVERRIDE_PRIORITY - Constant for the user override priority.
141 define('CALENDAR_EVENT_USER_OVERRIDE_PRIORITY', 0);
144 * CALENDAR_EVENT_TYPE_STANDARD - Standard events.
146 define('CALENDAR_EVENT_TYPE_STANDARD', 0);
149 * CALENDAR_EVENT_TYPE_ACTION - Action events.
151 define('CALENDAR_EVENT_TYPE_ACTION', 1);
154 * Manage calendar events.
156 * This class provides the required functionality in order to manage calendar events.
157 * It was introduced as part of Moodle 2.0 and was created in order to provide a
158 * better framework for dealing with calendar events in particular regard to file
159 * handling through the new file API.
161 * @package core_calendar
162 * @category calendar
163 * @copyright 2009 Sam Hemelryk
164 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
166 * @property int $id The id within the event table
167 * @property string $name The name of the event
168 * @property string $description The description of the event
169 * @property int $format The format of the description FORMAT_?
170 * @property int $courseid The course the event is associated with (0 if none)
171 * @property int $groupid The group the event is associated with (0 if none)
172 * @property int $userid The user the event is associated with (0 if none)
173 * @property int $repeatid If this is a repeated event this will be set to the
174 * id of the original
175 * @property string $modulename If added by a module this will be the module name
176 * @property int $instance If added by a module this will be the module instance
177 * @property string $eventtype The event type
178 * @property int $timestart The start time as a timestamp
179 * @property int $timeduration The duration of the event in seconds
180 * @property int $visible 1 if the event is visible
181 * @property int $uuid ?
182 * @property int $sequence ?
183 * @property int $timemodified The time last modified as a timestamp
185 class calendar_event {
187 /** @var array An object containing the event properties can be accessed via the magic __get/set methods */
188 protected $properties = null;
190 /** @var string The converted event discription with file paths resolved.
191 * This gets populated when someone requests description for the first time */
192 protected $_description = null;
194 /** @var array The options to use with this description editor */
195 protected $editoroptions = array(
196 'subdirs' => false,
197 'forcehttps' => false,
198 'maxfiles' => -1,
199 'maxbytes' => null,
200 'trusttext' => false);
202 /** @var object The context to use with the description editor */
203 protected $editorcontext = null;
206 * Instantiates a new event and optionally populates its properties with the data provided.
208 * @param \stdClass $data Optional. An object containing the properties to for
209 * an event
211 public function __construct($data = null) {
212 global $CFG, $USER;
214 // First convert to object if it is not already (should either be object or assoc array).
215 if (!is_object($data)) {
216 $data = (object) $data;
219 $this->editoroptions['maxbytes'] = $CFG->maxbytes;
221 $data->eventrepeats = 0;
223 if (empty($data->id)) {
224 $data->id = null;
227 if (!empty($data->subscriptionid)) {
228 $data->subscription = calendar_get_subscription($data->subscriptionid);
231 // Default to a user event.
232 if (empty($data->eventtype)) {
233 $data->eventtype = 'user';
236 // Default to the current user.
237 if (empty($data->userid)) {
238 $data->userid = $USER->id;
241 if (!empty($data->timeduration) && is_array($data->timeduration)) {
242 $data->timeduration = make_timestamp(
243 $data->timeduration['year'], $data->timeduration['month'], $data->timeduration['day'],
244 $data->timeduration['hour'], $data->timeduration['minute']) - $data->timestart;
247 if (!empty($data->description) && is_array($data->description)) {
248 $data->format = $data->description['format'];
249 $data->description = $data->description['text'];
250 } else if (empty($data->description)) {
251 $data->description = '';
252 $data->format = editors_get_preferred_format();
255 // Ensure form is defaulted correctly.
256 if (empty($data->format)) {
257 $data->format = editors_get_preferred_format();
260 $this->properties = $data;
264 * Magic set method.
266 * Attempts to call a set_$key method if one exists otherwise falls back
267 * to simply set the property.
269 * @param string $key property name
270 * @param mixed $value value of the property
272 public function __set($key, $value) {
273 if (method_exists($this, 'set_'.$key)) {
274 $this->{'set_'.$key}($value);
276 $this->properties->{$key} = $value;
280 * Magic get method.
282 * Attempts to call a get_$key method to return the property and ralls over
283 * to return the raw property.
285 * @param string $key property name
286 * @return mixed property value
287 * @throws \coding_exception
289 public function __get($key) {
290 if (method_exists($this, 'get_'.$key)) {
291 return $this->{'get_'.$key}();
293 if (!property_exists($this->properties, $key)) {
294 throw new \coding_exception('Undefined property requested');
296 return $this->properties->{$key};
300 * Magic isset method.
302 * PHP needs an isset magic method if you use the get magic method and
303 * still want empty calls to work.
305 * @param string $key $key property name
306 * @return bool|mixed property value, false if property is not exist
308 public function __isset($key) {
309 return !empty($this->properties->{$key});
313 * Calculate the context value needed for an event.
315 * Event's type can be determine by the available value store in $data
316 * It is important to check for the existence of course/courseid to determine
317 * the course event.
318 * Default value is set to CONTEXT_USER
320 * @return \stdClass The context object.
322 protected function calculate_context() {
323 global $USER, $DB;
325 $context = null;
326 if (isset($this->properties->categoryid) && $this->properties->categoryid > 0) {
327 $context = \context_coursecat::instance($this->properties->categoryid);
328 } else if (isset($this->properties->courseid) && $this->properties->courseid > 0) {
329 $context = \context_course::instance($this->properties->courseid);
330 } else if (isset($this->properties->course) && $this->properties->course > 0) {
331 $context = \context_course::instance($this->properties->course);
332 } else if (isset($this->properties->groupid) && $this->properties->groupid > 0) {
333 $group = $DB->get_record('groups', array('id' => $this->properties->groupid));
334 $context = \context_course::instance($group->courseid);
335 } else if (isset($this->properties->userid) && $this->properties->userid > 0
336 && $this->properties->userid == $USER->id) {
337 $context = \context_user::instance($this->properties->userid);
338 } else if (isset($this->properties->userid) && $this->properties->userid > 0
339 && $this->properties->userid != $USER->id &&
340 isset($this->properties->instance) && $this->properties->instance > 0) {
341 $cm = get_coursemodule_from_instance($this->properties->modulename, $this->properties->instance, 0,
342 false, MUST_EXIST);
343 $context = \context_course::instance($cm->course);
344 } else {
345 $context = \context_user::instance($this->properties->userid);
348 return $context;
352 * Returns the context for this event. The context is calculated
353 * the first time is is requested and then stored in a member
354 * variable to be returned each subsequent time.
356 * This is a magical getter function that will be called when
357 * ever the context property is accessed, e.g. $event->context.
359 * @return context
361 protected function get_context() {
362 if (!isset($this->properties->context)) {
363 $this->properties->context = $this->calculate_context();
366 return $this->properties->context;
370 * Returns an array of editoroptions for this event.
372 * @return array event editor options
374 protected function get_editoroptions() {
375 return $this->editoroptions;
379 * Returns an event description: Called by __get
380 * Please use $blah = $event->description;
382 * @return string event description
384 protected function get_description() {
385 global $CFG;
387 require_once($CFG->libdir . '/filelib.php');
389 if ($this->_description === null) {
390 // Check if we have already resolved the context for this event.
391 if ($this->editorcontext === null) {
392 // Switch on the event type to decide upon the appropriate context to use for this event.
393 $this->editorcontext = $this->get_context();
394 if (!calendar_is_valid_eventtype($this->properties->eventtype)) {
395 return clean_text($this->properties->description, $this->properties->format);
399 // Work out the item id for the editor, if this is a repeated event
400 // then the files will be associated with the original.
401 if (!empty($this->properties->repeatid) && $this->properties->repeatid > 0) {
402 $itemid = $this->properties->repeatid;
403 } else {
404 $itemid = $this->properties->id;
407 // Convert file paths in the description so that things display correctly.
408 $this->_description = file_rewrite_pluginfile_urls($this->properties->description, 'pluginfile.php',
409 $this->editorcontext->id, 'calendar', 'event_description', $itemid);
410 // Clean the text so no nasties get through.
411 $this->_description = clean_text($this->_description, $this->properties->format);
414 // Finally return the description.
415 return $this->_description;
419 * Return the number of repeat events there are in this events series.
421 * @return int number of event repeated
423 public function count_repeats() {
424 global $DB;
425 if (!empty($this->properties->repeatid)) {
426 $this->properties->eventrepeats = $DB->count_records('event',
427 array('repeatid' => $this->properties->repeatid));
428 // We don't want to count ourselves.
429 $this->properties->eventrepeats--;
431 return $this->properties->eventrepeats;
435 * Update or create an event within the database
437 * Pass in a object containing the event properties and this function will
438 * insert it into the database and deal with any associated files
440 * Capability checking should be performed if the user is directly manipulating the event
441 * and no other capability has been tested. However if the event is not being manipulated
442 * directly by the user and another capability has been checked for them to do this then
443 * capabilites should not be checked.
445 * For example if a user is editing an event in the calendar the check should be true,
446 * but if you are updating an event in an activities settings are changed then the calendar
447 * capabilites should not be checked.
449 * @see self::create()
450 * @see self::update()
452 * @param \stdClass $data object of event
453 * @param bool $checkcapability If Moodle should check the user can manage the calendar events for this call or not.
454 * @return bool event updated
456 public function update($data, $checkcapability=true) {
457 global $DB, $USER;
459 foreach ($data as $key => $value) {
460 $this->properties->$key = $value;
463 $this->properties->timemodified = time();
464 $usingeditor = (!empty($this->properties->description) && is_array($this->properties->description));
466 // Prepare event data.
467 $eventargs = array(
468 'context' => $this->get_context(),
469 'objectid' => $this->properties->id,
470 'other' => array(
471 'repeatid' => empty($this->properties->repeatid) ? 0 : $this->properties->repeatid,
472 'timestart' => $this->properties->timestart,
473 'name' => $this->properties->name
477 if (empty($this->properties->id) || $this->properties->id < 1) {
478 if ($checkcapability) {
479 if (!calendar_add_event_allowed($this->properties)) {
480 print_error('nopermissiontoupdatecalendar');
484 if ($usingeditor) {
485 switch ($this->properties->eventtype) {
486 case 'user':
487 $this->properties->courseid = 0;
488 $this->properties->course = 0;
489 $this->properties->groupid = 0;
490 $this->properties->userid = $USER->id;
491 break;
492 case 'site':
493 $this->properties->courseid = SITEID;
494 $this->properties->course = SITEID;
495 $this->properties->groupid = 0;
496 $this->properties->userid = $USER->id;
497 break;
498 case 'course':
499 $this->properties->groupid = 0;
500 $this->properties->userid = $USER->id;
501 break;
502 case 'category':
503 $this->properties->groupid = 0;
504 $this->properties->category = 0;
505 $this->properties->userid = $USER->id;
506 break;
507 case 'group':
508 $this->properties->userid = $USER->id;
509 break;
510 default:
511 // We should NEVER get here, but just incase we do lets fail gracefully.
512 $usingeditor = false;
513 break;
516 // If we are actually using the editor, we recalculate the context because some default values
517 // were set when calculate_context() was called from the constructor.
518 if ($usingeditor) {
519 $this->properties->context = $this->calculate_context();
520 $this->editorcontext = $this->get_context();
523 $editor = $this->properties->description;
524 $this->properties->format = $this->properties->description['format'];
525 $this->properties->description = $this->properties->description['text'];
528 // Insert the event into the database.
529 $this->properties->id = $DB->insert_record('event', $this->properties);
531 if ($usingeditor) {
532 $this->properties->description = file_save_draft_area_files(
533 $editor['itemid'],
534 $this->editorcontext->id,
535 'calendar',
536 'event_description',
537 $this->properties->id,
538 $this->editoroptions,
539 $editor['text'],
540 $this->editoroptions['forcehttps']);
541 $DB->set_field('event', 'description', $this->properties->description,
542 array('id' => $this->properties->id));
545 // Log the event entry.
546 $eventargs['objectid'] = $this->properties->id;
547 $eventargs['context'] = $this->get_context();
548 $event = \core\event\calendar_event_created::create($eventargs);
549 $event->trigger();
551 $repeatedids = array();
553 if (!empty($this->properties->repeat)) {
554 $this->properties->repeatid = $this->properties->id;
555 $DB->set_field('event', 'repeatid', $this->properties->repeatid, array('id' => $this->properties->id));
557 $eventcopy = clone($this->properties);
558 unset($eventcopy->id);
560 $timestart = new \DateTime('@' . $eventcopy->timestart);
561 $timestart->setTimezone(\core_date::get_user_timezone_object());
563 for ($i = 1; $i < $eventcopy->repeats; $i++) {
565 $timestart->add(new \DateInterval('P7D'));
566 $eventcopy->timestart = $timestart->getTimestamp();
568 // Get the event id for the log record.
569 $eventcopyid = $DB->insert_record('event', $eventcopy);
571 // If the context has been set delete all associated files.
572 if ($usingeditor) {
573 $fs = get_file_storage();
574 $files = $fs->get_area_files($this->editorcontext->id, 'calendar', 'event_description',
575 $this->properties->id);
576 foreach ($files as $file) {
577 $fs->create_file_from_storedfile(array('itemid' => $eventcopyid), $file);
581 $repeatedids[] = $eventcopyid;
583 // Trigger an event.
584 $eventargs['objectid'] = $eventcopyid;
585 $eventargs['other']['timestart'] = $eventcopy->timestart;
586 $event = \core\event\calendar_event_created::create($eventargs);
587 $event->trigger();
591 return true;
592 } else {
594 if ($checkcapability) {
595 if (!calendar_edit_event_allowed($this->properties)) {
596 print_error('nopermissiontoupdatecalendar');
600 if ($usingeditor) {
601 if ($this->editorcontext !== null) {
602 $this->properties->description = file_save_draft_area_files(
603 $this->properties->description['itemid'],
604 $this->editorcontext->id,
605 'calendar',
606 'event_description',
607 $this->properties->id,
608 $this->editoroptions,
609 $this->properties->description['text'],
610 $this->editoroptions['forcehttps']);
611 } else {
612 $this->properties->format = $this->properties->description['format'];
613 $this->properties->description = $this->properties->description['text'];
617 $event = $DB->get_record('event', array('id' => $this->properties->id));
619 $updaterepeated = (!empty($this->properties->repeatid) && !empty($this->properties->repeateditall));
621 if ($updaterepeated) {
623 $sqlset = 'name = ?,
624 description = ?,
625 timeduration = ?,
626 timemodified = ?,
627 groupid = ?,
628 courseid = ?';
630 // Note: Group and course id may not be set. If not, keep their current values.
631 $params = [
632 $this->properties->name,
633 $this->properties->description,
634 $this->properties->timeduration,
635 time(),
636 isset($this->properties->groupid) ? $this->properties->groupid : $event->groupid,
637 isset($this->properties->courseid) ? $this->properties->courseid : $event->courseid,
640 // Note: Only update start date, if it was changed by the user.
641 if ($this->properties->timestart != $event->timestart) {
642 $timestartoffset = $this->properties->timestart - $event->timestart;
643 $sqlset .= ', timestart = timestart + ?';
644 $params[] = $timestartoffset;
647 // Note: Only update location, if it was changed by the user.
648 $updatelocation = (!empty($this->properties->location) && $this->properties->location !== $event->location);
649 if ($updatelocation) {
650 $sqlset .= ', location = ?';
651 $params[] = $this->properties->location;
654 // Update all.
655 $sql = "UPDATE {event}
656 SET $sqlset
657 WHERE repeatid = ?";
659 $params[] = $event->repeatid;
660 $DB->execute($sql, $params);
662 // Trigger an update event for each of the calendar event.
663 $events = $DB->get_records('event', array('repeatid' => $event->repeatid), '', '*');
664 foreach ($events as $calendarevent) {
665 $eventargs['objectid'] = $calendarevent->id;
666 $eventargs['other']['timestart'] = $calendarevent->timestart;
667 $event = \core\event\calendar_event_updated::create($eventargs);
668 $event->add_record_snapshot('event', $calendarevent);
669 $event->trigger();
671 } else {
672 $DB->update_record('event', $this->properties);
673 $event = self::load($this->properties->id);
674 $this->properties = $event->properties();
676 // Trigger an update event.
677 $event = \core\event\calendar_event_updated::create($eventargs);
678 $event->add_record_snapshot('event', $this->properties);
679 $event->trigger();
682 return true;
687 * Deletes an event and if selected an repeated events in the same series
689 * This function deletes an event, any associated events if $deleterepeated=true,
690 * and cleans up any files associated with the events.
692 * @see self::delete()
694 * @param bool $deleterepeated delete event repeatedly
695 * @return bool succession of deleting event
697 public function delete($deleterepeated = false) {
698 global $DB;
700 // If $this->properties->id is not set then something is wrong.
701 if (empty($this->properties->id)) {
702 debugging('Attempting to delete an event before it has been loaded', DEBUG_DEVELOPER);
703 return false;
705 $calevent = $DB->get_record('event', array('id' => $this->properties->id), '*', MUST_EXIST);
706 // Delete the event.
707 $DB->delete_records('event', array('id' => $this->properties->id));
709 // Trigger an event for the delete action.
710 $eventargs = array(
711 'context' => $this->get_context(),
712 'objectid' => $this->properties->id,
713 'other' => array(
714 'repeatid' => empty($this->properties->repeatid) ? 0 : $this->properties->repeatid,
715 'timestart' => $this->properties->timestart,
716 'name' => $this->properties->name
718 $event = \core\event\calendar_event_deleted::create($eventargs);
719 $event->add_record_snapshot('event', $calevent);
720 $event->trigger();
722 // If we are deleting parent of a repeated event series, promote the next event in the series as parent.
723 if (($this->properties->id == $this->properties->repeatid) && !$deleterepeated) {
724 $newparent = $DB->get_field_sql("SELECT id from {event} where repeatid = ? order by id ASC",
725 array($this->properties->id), IGNORE_MULTIPLE);
726 if (!empty($newparent)) {
727 $DB->execute("UPDATE {event} SET repeatid = ? WHERE repeatid = ?",
728 array($newparent, $this->properties->id));
729 // Get all records where the repeatid is the same as the event being removed.
730 $events = $DB->get_records('event', array('repeatid' => $newparent));
731 // For each of the returned events trigger an update event.
732 foreach ($events as $calendarevent) {
733 // Trigger an event for the update.
734 $eventargs['objectid'] = $calendarevent->id;
735 $eventargs['other']['timestart'] = $calendarevent->timestart;
736 $event = \core\event\calendar_event_updated::create($eventargs);
737 $event->add_record_snapshot('event', $calendarevent);
738 $event->trigger();
743 // If the editor context hasn't already been set then set it now.
744 if ($this->editorcontext === null) {
745 $this->editorcontext = $this->get_context();
748 // If the context has been set delete all associated files.
749 if ($this->editorcontext !== null) {
750 $fs = get_file_storage();
751 $files = $fs->get_area_files($this->editorcontext->id, 'calendar', 'event_description', $this->properties->id);
752 foreach ($files as $file) {
753 $file->delete();
757 // If we need to delete repeated events then we will fetch them all and delete one by one.
758 if ($deleterepeated && !empty($this->properties->repeatid) && $this->properties->repeatid > 0) {
759 // Get all records where the repeatid is the same as the event being removed.
760 $events = $DB->get_records('event', array('repeatid' => $this->properties->repeatid));
761 // For each of the returned events populate an event object and call delete.
762 // make sure the arg passed is false as we are already deleting all repeats.
763 foreach ($events as $event) {
764 $event = new calendar_event($event);
765 $event->delete(false);
769 return true;
773 * Fetch all event properties.
775 * This function returns all of the events properties as an object and optionally
776 * can prepare an editor for the description field at the same time. This is
777 * designed to work when the properties are going to be used to set the default
778 * values of a moodle forms form.
780 * @param bool $prepareeditor If set to true a editor is prepared for use with
781 * the mforms editor element. (for description)
782 * @return \stdClass Object containing event properties
784 public function properties($prepareeditor = false) {
785 global $DB;
787 // First take a copy of the properties. We don't want to actually change the
788 // properties or we'd forever be converting back and forwards between an
789 // editor formatted description and not.
790 $properties = clone($this->properties);
791 // Clean the description here.
792 $properties->description = clean_text($properties->description, $properties->format);
794 // If set to true we need to prepare the properties for use with an editor
795 // and prepare the file area.
796 if ($prepareeditor) {
798 // We may or may not have a property id. If we do then we need to work
799 // out the context so we can copy the existing files to the draft area.
800 if (!empty($properties->id)) {
802 if ($properties->eventtype === 'site') {
803 // Site context.
804 $this->editorcontext = $this->get_context();
805 } else if ($properties->eventtype === 'user') {
806 // User context.
807 $this->editorcontext = $this->get_context();
808 } else if ($properties->eventtype === 'group' || $properties->eventtype === 'course') {
809 // First check the course is valid.
810 $course = $DB->get_record('course', array('id' => $properties->courseid));
811 if (!$course) {
812 print_error('invalidcourse');
814 // Course context.
815 $this->editorcontext = $this->get_context();
816 // We have a course and are within the course context so we had
817 // better use the courses max bytes value.
818 $this->editoroptions['maxbytes'] = $course->maxbytes;
819 } else if ($properties->eventtype === 'category') {
820 // First check the course is valid.
821 \core_course_category::get($properties->categoryid, MUST_EXIST, true);
822 // Course context.
823 $this->editorcontext = $this->get_context();
824 } else {
825 // If we get here we have a custom event type as used by some
826 // modules. In this case the event will have been added by
827 // code and we won't need the editor.
828 $this->editoroptions['maxbytes'] = 0;
829 $this->editoroptions['maxfiles'] = 0;
832 if (empty($this->editorcontext) || empty($this->editorcontext->id)) {
833 $contextid = false;
834 } else {
835 // Get the context id that is what we really want.
836 $contextid = $this->editorcontext->id;
838 } else {
840 // If we get here then this is a new event in which case we don't need a
841 // context as there is no existing files to copy to the draft area.
842 $contextid = null;
845 // If the contextid === false we don't support files so no preparing
846 // a draft area.
847 if ($contextid !== false) {
848 // Just encase it has already been submitted.
849 $draftiddescription = file_get_submitted_draft_itemid('description');
850 // Prepare the draft area, this copies existing files to the draft area as well.
851 $properties->description = file_prepare_draft_area($draftiddescription, $contextid, 'calendar',
852 'event_description', $properties->id, $this->editoroptions, $properties->description);
853 } else {
854 $draftiddescription = 0;
857 // Structure the description field as the editor requires.
858 $properties->description = array('text' => $properties->description, 'format' => $properties->format,
859 'itemid' => $draftiddescription);
862 // Finally return the properties.
863 return $properties;
867 * Toggles the visibility of an event
869 * @param null|bool $force If it is left null the events visibility is flipped,
870 * If it is false the event is made hidden, if it is true it
871 * is made visible.
872 * @return bool if event is successfully updated, toggle will be visible
874 public function toggle_visibility($force = null) {
875 global $DB;
877 // Set visible to the default if it is not already set.
878 if (empty($this->properties->visible)) {
879 $this->properties->visible = 1;
882 if ($force === true || ($force !== false && $this->properties->visible == 0)) {
883 // Make this event visible.
884 $this->properties->visible = 1;
885 } else {
886 // Make this event hidden.
887 $this->properties->visible = 0;
890 // Update the database to reflect this change.
891 $success = $DB->set_field('event', 'visible', $this->properties->visible, array('id' => $this->properties->id));
892 $calendarevent = $DB->get_record('event', array('id' => $this->properties->id), '*', MUST_EXIST);
894 // Prepare event data.
895 $eventargs = array(
896 'context' => $this->get_context(),
897 'objectid' => $this->properties->id,
898 'other' => array(
899 'repeatid' => empty($this->properties->repeatid) ? 0 : $this->properties->repeatid,
900 'timestart' => $this->properties->timestart,
901 'name' => $this->properties->name
904 $event = \core\event\calendar_event_updated::create($eventargs);
905 $event->add_record_snapshot('event', $calendarevent);
906 $event->trigger();
908 return $success;
912 * Returns an event object when provided with an event id.
914 * This function makes use of MUST_EXIST, if the event id passed in is invalid
915 * it will result in an exception being thrown.
917 * @param int|object $param event object or event id
918 * @return calendar_event
920 public static function load($param) {
921 global $DB;
922 if (is_object($param)) {
923 $event = new calendar_event($param);
924 } else {
925 $event = $DB->get_record('event', array('id' => (int)$param), '*', MUST_EXIST);
926 $event = new calendar_event($event);
928 return $event;
932 * Creates a new event and returns an event object.
934 * Capability checking should be performed if the user is directly creating the event
935 * and no other capability has been tested. However if the event is not being created
936 * directly by the user and another capability has been checked for them to do this then
937 * capabilites should not be checked.
939 * For example if a user is creating an event in the calendar the check should be true,
940 * but if you are creating an event in an activity when it is created then the calendar
941 * capabilites should not be checked.
943 * @param \stdClass|array $properties An object containing event properties
944 * @param bool $checkcapability If Moodle should check the user can manage the calendar events for this call or not.
945 * @throws \coding_exception
947 * @return calendar_event|bool The event object or false if it failed
949 public static function create($properties, $checkcapability = true) {
950 if (is_array($properties)) {
951 $properties = (object)$properties;
953 if (!is_object($properties)) {
954 throw new \coding_exception('When creating an event properties should be either an object or an assoc array');
956 $event = new calendar_event($properties);
957 if ($event->update($properties, $checkcapability)) {
958 return $event;
959 } else {
960 return false;
965 * Format the text using the external API.
967 * This function should we used when text formatting is required in external functions.
969 * @return array an array containing the text formatted and the text format
971 public function format_external_text() {
973 if ($this->editorcontext === null) {
974 // Switch on the event type to decide upon the appropriate context to use for this event.
975 $this->editorcontext = $this->get_context();
977 if (!calendar_is_valid_eventtype($this->properties->eventtype)) {
978 // We don't have a context here, do a normal format_text.
979 return external_format_text($this->properties->description, $this->properties->format, $this->editorcontext->id);
983 // Work out the item id for the editor, if this is a repeated event then the files will be associated with the original.
984 if (!empty($this->properties->repeatid) && $this->properties->repeatid > 0) {
985 $itemid = $this->properties->repeatid;
986 } else {
987 $itemid = $this->properties->id;
990 return external_format_text($this->properties->description, $this->properties->format, $this->editorcontext->id,
991 'calendar', 'event_description', $itemid);
996 * Calendar information class
998 * This class is used simply to organise the information pertaining to a calendar
999 * and is used primarily to make information easily available.
1001 * @package core_calendar
1002 * @category calendar
1003 * @copyright 2010 Sam Hemelryk
1004 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1006 class calendar_information {
1009 * @var int The timestamp
1011 * Rather than setting the day, month and year we will set a timestamp which will be able
1012 * to be used by multiple calendars.
1014 public $time;
1016 /** @var int A course id */
1017 public $courseid = null;
1019 /** @var array An array of categories */
1020 public $categories = array();
1022 /** @var int The current category */
1023 public $categoryid = null;
1025 /** @var array An array of courses */
1026 public $courses = array();
1028 /** @var array An array of groups */
1029 public $groups = array();
1031 /** @var array An array of users */
1032 public $users = array();
1034 /** @var context The anticipated context that the calendar is viewed in */
1035 public $context = null;
1038 * Creates a new instance
1040 * @param int $day the number of the day
1041 * @param int $month the number of the month
1042 * @param int $year the number of the year
1043 * @param int $time the unixtimestamp representing the date we want to view, this is used instead of $calmonth
1044 * and $calyear to support multiple calendars
1046 public function __construct($day = 0, $month = 0, $year = 0, $time = 0) {
1047 // If a day, month and year were passed then convert it to a timestamp. If these were passed
1048 // then we can assume the day, month and year are passed as Gregorian, as no where in core
1049 // should we be passing these values rather than the time. This is done for BC.
1050 if (!empty($day) || !empty($month) || !empty($year)) {
1051 $date = usergetdate(time());
1052 if (empty($day)) {
1053 $day = $date['mday'];
1055 if (empty($month)) {
1056 $month = $date['mon'];
1058 if (empty($year)) {
1059 $year = $date['year'];
1061 if (checkdate($month, $day, $year)) {
1062 $time = make_timestamp($year, $month, $day);
1063 } else {
1064 $time = time();
1068 $this->set_time($time);
1072 * Creates and set up a instance.
1074 * @param int $time the unixtimestamp representing the date we want to view.
1075 * @param int $courseid The ID of the course the user wishes to view.
1076 * @param int $categoryid The ID of the category the user wishes to view
1077 * If a courseid is specified, this value is ignored.
1078 * @return calendar_information
1080 public static function create($time, int $courseid, int $categoryid = null) : calendar_information {
1081 $calendar = new static(0, 0, 0, $time);
1082 if ($courseid != SITEID && !empty($courseid)) {
1083 // Course ID must be valid and existing.
1084 $course = get_course($courseid);
1085 $calendar->context = context_course::instance($course->id);
1087 if (!$course->visible && !is_role_switched($course->id)) {
1088 require_capability('moodle/course:viewhiddencourses', $calendar->context);
1091 $courses = [$course->id => $course];
1092 $category = (\core_course_category::get($course->category, MUST_EXIST, true))->get_db_record();
1093 } else if (!empty($categoryid)) {
1094 $course = get_site();
1095 $courses = calendar_get_default_courses(null, 'id, category, groupmode, groupmodeforce');
1097 // Filter available courses to those within this category or it's children.
1098 $ids = [$categoryid];
1099 $category = \core_course_category::get($categoryid);
1100 $ids = array_merge($ids, array_keys($category->get_children()));
1101 $courses = array_filter($courses, function($course) use ($ids) {
1102 return array_search($course->category, $ids) !== false;
1104 $category = $category->get_db_record();
1106 $calendar->context = context_coursecat::instance($categoryid);
1107 } else {
1108 $course = get_site();
1109 $courses = calendar_get_default_courses(null, 'id, category, groupmode, groupmodeforce');
1110 $category = null;
1112 $calendar->context = context_system::instance();
1115 $calendar->set_sources($course, $courses, $category);
1117 return $calendar;
1121 * Set the time period of this instance.
1123 * @param int $time the unixtimestamp representing the date we want to view.
1124 * @return $this
1126 public function set_time($time = null) {
1127 if (empty($time)) {
1128 $this->time = time();
1129 } else {
1130 $this->time = $time;
1133 return $this;
1137 * Initialize calendar information
1139 * @deprecated 3.4
1140 * @param stdClass $course object
1141 * @param array $coursestoload An array of courses [$course->id => $course]
1142 * @param bool $ignorefilters options to use filter
1144 public function prepare_for_view(stdClass $course, array $coursestoload, $ignorefilters = false) {
1145 debugging('The prepare_for_view() function has been deprecated. Please update your code to use set_sources()',
1146 DEBUG_DEVELOPER);
1147 $this->set_sources($course, $coursestoload);
1151 * Set the sources for events within the calendar.
1153 * If no category is provided, then the category path for the current
1154 * course will be used.
1156 * @param stdClass $course The current course being viewed.
1157 * @param stdClass[] $courses The list of all courses currently accessible.
1158 * @param stdClass $category The current category to show.
1160 public function set_sources(stdClass $course, array $courses, stdClass $category = null) {
1161 global $USER;
1163 // A cousre must always be specified.
1164 $this->course = $course;
1165 $this->courseid = $course->id;
1167 list($courseids, $group, $user) = calendar_set_filters($courses);
1168 $this->courses = $courseids;
1169 $this->groups = $group;
1170 $this->users = $user;
1172 // Do not show category events by default.
1173 $this->categoryid = null;
1174 $this->categories = null;
1176 // Determine the correct category information to show.
1177 // When called with a course, the category of that course is usually included too.
1178 // When a category was specifically requested, it should be requested with the site id.
1179 if (SITEID !== $this->courseid) {
1180 // A specific course was requested.
1181 // Fetch the category that this course is in, along with all parents.
1182 // Do not include child categories of this category, as the user many not have enrolments in those siblings or children.
1183 $category = \core_course_category::get($course->category, MUST_EXIST, true);
1184 $this->categoryid = $category->id;
1186 $this->categories = $category->get_parents();
1187 $this->categories[] = $category->id;
1188 } else if (null !== $category && $category->id > 0) {
1189 // A specific category was requested.
1190 // Fetch all parents of this category, along with all children too.
1191 $category = \core_course_category::get($category->id);
1192 $this->categoryid = $category->id;
1194 // Build the category list.
1195 // This includes the current category.
1196 $this->categories = $category->get_parents();
1197 $this->categories[] = $category->id;
1198 $this->categories = array_merge($this->categories, $category->get_all_children_ids());
1199 } else if (SITEID === $this->courseid) {
1200 // The site was requested.
1201 // Fetch all categories where this user has any enrolment, and all categories that this user can manage.
1203 // Grab the list of categories that this user has courses in.
1204 $coursecategories = array_flip(array_map(function($course) {
1205 return $course->category;
1206 }, $courses));
1208 $calcatcache = cache::make('core', 'calendar_categories');
1209 $this->categories = $calcatcache->get('site');
1210 if ($this->categories === false) {
1211 // Use the category id as the key in the following array. That way we do not have to remove duplicates.
1212 $categories = [];
1213 foreach (\core_course_category::get_all() as $category) {
1214 if (isset($coursecategories[$category->id]) ||
1215 has_capability('moodle/category:manage', $category->get_context(), $USER, false)) {
1216 // If the user has access to a course in this category or can manage the category,
1217 // then they can see all parent categories too.
1218 $categories[$category->id] = true;
1219 foreach ($category->get_parents() as $catid) {
1220 $categories[$catid] = true;
1224 $this->categories = array_keys($categories);
1225 $calcatcache->set('site', $this->categories);
1231 * Ensures the date for the calendar is correct and either sets it to now
1232 * or throws a moodle_exception if not
1234 * @param bool $defaultonow use current time
1235 * @throws moodle_exception
1236 * @return bool validation of checkdate
1238 public function checkdate($defaultonow = true) {
1239 if (!checkdate($this->month, $this->day, $this->year)) {
1240 if ($defaultonow) {
1241 $now = usergetdate(time());
1242 $this->day = intval($now['mday']);
1243 $this->month = intval($now['mon']);
1244 $this->year = intval($now['year']);
1245 return true;
1246 } else {
1247 throw new moodle_exception('invaliddate');
1250 return true;
1254 * Gets todays timestamp for the calendar
1256 * @return int today timestamp
1258 public function timestamp_today() {
1259 return $this->time;
1262 * Gets tomorrows timestamp for the calendar
1264 * @return int tomorrow timestamp
1266 public function timestamp_tomorrow() {
1267 return strtotime('+1 day', $this->time);
1270 * Adds the pretend blocks for the calendar
1272 * @param core_calendar_renderer $renderer
1273 * @param bool $showfilters display filters, false is set as default
1274 * @param string|null $view preference view options (eg: day, month, upcoming)
1276 public function add_sidecalendar_blocks(core_calendar_renderer $renderer, $showfilters=false, $view=null) {
1277 if ($showfilters) {
1278 $filters = new block_contents();
1279 $filters->content = $renderer->event_filter();
1280 $filters->footer = '';
1281 $filters->title = get_string('eventskey', 'calendar');
1282 $renderer->add_pretend_calendar_block($filters, BLOCK_POS_RIGHT);
1284 $block = new block_contents;
1285 $block->content = $renderer->fake_block_threemonths($this);
1286 $block->footer = '';
1287 $block->title = get_string('monthlyview', 'calendar');
1288 $renderer->add_pretend_calendar_block($block, BLOCK_POS_RIGHT);
1293 * Get calendar events.
1295 * @param int $tstart Start time of time range for events
1296 * @param int $tend End time of time range for events
1297 * @param array|int|boolean $users array of users, user id or boolean for all/no user events
1298 * @param array|int|boolean $groups array of groups, group id or boolean for all/no group events
1299 * @param array|int|boolean $courses array of courses, course id or boolean for all/no course events
1300 * @param boolean $withduration whether only events starting within time range selected
1301 * or events in progress/already started selected as well
1302 * @param boolean $ignorehidden whether to select only visible events or all events
1303 * @param array|int|boolean $categories array of categories, category id or boolean for all/no course events
1304 * @return array $events of selected events or an empty array if there aren't any (or there was an error)
1306 function calendar_get_events($tstart, $tend, $users, $groups, $courses,
1307 $withduration = true, $ignorehidden = true, $categories = []) {
1308 global $DB;
1310 $whereclause = '';
1311 $params = array();
1312 // Quick test.
1313 if (empty($users) && empty($groups) && empty($courses) && empty($categories)) {
1314 return array();
1317 if ((is_array($users) && !empty($users)) or is_numeric($users)) {
1318 // Events from a number of users
1319 if(!empty($whereclause)) $whereclause .= ' OR';
1320 list($insqlusers, $inparamsusers) = $DB->get_in_or_equal($users, SQL_PARAMS_NAMED);
1321 $whereclause .= " (e.userid $insqlusers AND e.courseid = 0 AND e.groupid = 0 AND e.categoryid = 0)";
1322 $params = array_merge($params, $inparamsusers);
1323 } else if($users === true) {
1324 // Events from ALL users
1325 if(!empty($whereclause)) $whereclause .= ' OR';
1326 $whereclause .= ' (e.userid != 0 AND e.courseid = 0 AND e.groupid = 0 AND e.categoryid = 0)';
1327 } else if($users === false) {
1328 // No user at all, do nothing
1331 if ((is_array($groups) && !empty($groups)) or is_numeric($groups)) {
1332 // Events from a number of groups
1333 if(!empty($whereclause)) $whereclause .= ' OR';
1334 list($insqlgroups, $inparamsgroups) = $DB->get_in_or_equal($groups, SQL_PARAMS_NAMED);
1335 $whereclause .= " e.groupid $insqlgroups ";
1336 $params = array_merge($params, $inparamsgroups);
1337 } else if($groups === true) {
1338 // Events from ALL groups
1339 if(!empty($whereclause)) $whereclause .= ' OR ';
1340 $whereclause .= ' e.groupid != 0';
1342 // boolean false (no groups at all): we don't need to do anything
1344 if ((is_array($courses) && !empty($courses)) or is_numeric($courses)) {
1345 if(!empty($whereclause)) $whereclause .= ' OR';
1346 list($insqlcourses, $inparamscourses) = $DB->get_in_or_equal($courses, SQL_PARAMS_NAMED);
1347 $whereclause .= " (e.groupid = 0 AND e.courseid $insqlcourses)";
1348 $params = array_merge($params, $inparamscourses);
1349 } else if ($courses === true) {
1350 // Events from ALL courses
1351 if(!empty($whereclause)) $whereclause .= ' OR';
1352 $whereclause .= ' (e.groupid = 0 AND e.courseid != 0)';
1355 if ((is_array($categories) && !empty($categories)) || is_numeric($categories)) {
1356 if (!empty($whereclause)) {
1357 $whereclause .= ' OR';
1359 list($insqlcategories, $inparamscategories) = $DB->get_in_or_equal($categories, SQL_PARAMS_NAMED);
1360 $whereclause .= " (e.groupid = 0 AND e.courseid = 0 AND e.categoryid $insqlcategories)";
1361 $params = array_merge($params, $inparamscategories);
1362 } else if ($categories === true) {
1363 // Events from ALL categories.
1364 if (!empty($whereclause)) {
1365 $whereclause .= ' OR';
1367 $whereclause .= ' (e.groupid = 0 AND e.courseid = 0 AND e.categoryid != 0)';
1370 // Security check: if, by now, we have NOTHING in $whereclause, then it means
1371 // that NO event-selecting clauses were defined. Thus, we won't be returning ANY
1372 // events no matter what. Allowing the code to proceed might return a completely
1373 // valid query with only time constraints, thus selecting ALL events in that time frame!
1374 if(empty($whereclause)) {
1375 return array();
1378 if($withduration) {
1379 $timeclause = '(e.timestart >= '.$tstart.' OR e.timestart + e.timeduration > '.$tstart.') AND e.timestart <= '.$tend;
1381 else {
1382 $timeclause = 'e.timestart >= '.$tstart.' AND e.timestart <= '.$tend;
1384 if(!empty($whereclause)) {
1385 // We have additional constraints
1386 $whereclause = $timeclause.' AND ('.$whereclause.')';
1388 else {
1389 // Just basic time filtering
1390 $whereclause = $timeclause;
1393 if ($ignorehidden) {
1394 $whereclause .= ' AND e.visible = 1';
1397 $sql = "SELECT e.*
1398 FROM {event} e
1399 LEFT JOIN {modules} m ON e.modulename = m.name
1400 -- Non visible modules will have a value of 0.
1401 WHERE (m.visible = 1 OR m.visible IS NULL) AND $whereclause
1402 ORDER BY e.timestart";
1403 $events = $DB->get_records_sql($sql, $params);
1405 if ($events === false) {
1406 $events = array();
1408 return $events;
1412 * Return the days of the week.
1414 * @return array array of days
1416 function calendar_get_days() {
1417 $calendartype = \core_calendar\type_factory::get_calendar_instance();
1418 return $calendartype->get_weekdays();
1422 * Get the subscription from a given id.
1424 * @since Moodle 2.5
1425 * @param int $id id of the subscription
1426 * @return stdClass Subscription record from DB
1427 * @throws moodle_exception for an invalid id
1429 function calendar_get_subscription($id) {
1430 global $DB;
1432 $cache = \cache::make('core', 'calendar_subscriptions');
1433 $subscription = $cache->get($id);
1434 if (empty($subscription)) {
1435 $subscription = $DB->get_record('event_subscriptions', array('id' => $id), '*', MUST_EXIST);
1436 $cache->set($id, $subscription);
1439 return $subscription;
1443 * Gets the first day of the week.
1445 * Used to be define('CALENDAR_STARTING_WEEKDAY', blah);
1447 * @return int
1449 function calendar_get_starting_weekday() {
1450 $calendartype = \core_calendar\type_factory::get_calendar_instance();
1451 return $calendartype->get_starting_weekday();
1455 * Get a HTML link to a course.
1457 * @param int|stdClass $course the course id or course object
1458 * @return string a link to the course (as HTML); empty if the course id is invalid
1460 function calendar_get_courselink($course) {
1461 if (!$course) {
1462 return '';
1465 if (!is_object($course)) {
1466 $course = calendar_get_course_cached($coursecache, $course);
1468 $context = \context_course::instance($course->id);
1469 $fullname = format_string($course->fullname, true, array('context' => $context));
1470 $url = new \moodle_url('/course/view.php', array('id' => $course->id));
1471 $link = \html_writer::link($url, $fullname);
1473 return $link;
1477 * Get current module cache.
1479 * Only use this method if you do not know courseid. Otherwise use:
1480 * get_fast_modinfo($courseid)->instances[$modulename][$instance]
1482 * @param array $modulecache in memory module cache
1483 * @param string $modulename name of the module
1484 * @param int $instance module instance number
1485 * @return stdClass|bool $module information
1487 function calendar_get_module_cached(&$modulecache, $modulename, $instance) {
1488 if (!isset($modulecache[$modulename . '_' . $instance])) {
1489 $modulecache[$modulename . '_' . $instance] = get_coursemodule_from_instance($modulename, $instance);
1492 return $modulecache[$modulename . '_' . $instance];
1496 * Get current course cache.
1498 * @param array $coursecache list of course cache
1499 * @param int $courseid id of the course
1500 * @return stdClass $coursecache[$courseid] return the specific course cache
1502 function calendar_get_course_cached(&$coursecache, $courseid) {
1503 if (!isset($coursecache[$courseid])) {
1504 $coursecache[$courseid] = get_course($courseid);
1506 return $coursecache[$courseid];
1510 * Get group from groupid for calendar display
1512 * @param int $groupid
1513 * @return stdClass group object with fields 'id', 'name' and 'courseid'
1515 function calendar_get_group_cached($groupid) {
1516 static $groupscache = array();
1517 if (!isset($groupscache[$groupid])) {
1518 $groupscache[$groupid] = groups_get_group($groupid, 'id,name,courseid');
1520 return $groupscache[$groupid];
1524 * Add calendar event metadata
1526 * @param stdClass $event event info
1527 * @return stdClass $event metadata
1529 function calendar_add_event_metadata($event) {
1530 global $CFG, $OUTPUT;
1532 // Support multilang in event->name.
1533 $event->name = format_string($event->name, true);
1535 if (!empty($event->modulename)) { // Activity event.
1536 // The module name is set. I will assume that it has to be displayed, and
1537 // also that it is an automatically-generated event. And of course that the
1538 // instace id and modulename are set correctly.
1539 $instances = get_fast_modinfo($event->courseid)->get_instances_of($event->modulename);
1540 if (!array_key_exists($event->instance, $instances)) {
1541 return;
1543 $module = $instances[$event->instance];
1545 $modulename = $module->get_module_type_name(false);
1546 if (get_string_manager()->string_exists($event->eventtype, $event->modulename)) {
1547 // Will be used as alt text if the event icon.
1548 $eventtype = get_string($event->eventtype, $event->modulename);
1549 } else {
1550 $eventtype = '';
1553 $event->icon = '<img src="' . s($module->get_icon_url()) . '" alt="' . s($eventtype) .
1554 '" title="' . s($modulename) . '" class="icon" />';
1555 $event->referer = html_writer::link($module->url, $event->name);
1556 $event->courselink = calendar_get_courselink($module->get_course());
1557 $event->cmid = $module->id;
1558 } else if ($event->courseid == SITEID) { // Site event.
1559 $event->icon = '<img src="' . $OUTPUT->image_url('i/siteevent') . '" alt="' .
1560 get_string('siteevent', 'calendar') . '" class="icon" />';
1561 $event->cssclass = 'calendar_event_site';
1562 } else if ($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) { // Course event.
1563 $event->icon = '<img src="' . $OUTPUT->image_url('i/courseevent') . '" alt="' .
1564 get_string('courseevent', 'calendar') . '" class="icon" />';
1565 $event->courselink = calendar_get_courselink($event->courseid);
1566 $event->cssclass = 'calendar_event_course';
1567 } else if ($event->groupid) { // Group event.
1568 if ($group = calendar_get_group_cached($event->groupid)) {
1569 $groupname = format_string($group->name, true, \context_course::instance($group->courseid));
1570 } else {
1571 $groupname = '';
1573 $event->icon = \html_writer::empty_tag('image', array('src' => $OUTPUT->image_url('i/groupevent'),
1574 'alt' => get_string('groupevent', 'calendar'), 'title' => $groupname, 'class' => 'icon'));
1575 $event->courselink = calendar_get_courselink($event->courseid) . ', ' . $groupname;
1576 $event->cssclass = 'calendar_event_group';
1577 } else if ($event->userid) { // User event.
1578 $event->icon = '<img src="' . $OUTPUT->image_url('i/userevent') . '" alt="' .
1579 get_string('userevent', 'calendar') . '" class="icon" />';
1580 $event->cssclass = 'calendar_event_user';
1583 return $event;
1587 * Get calendar events by id.
1589 * @since Moodle 2.5
1590 * @param array $eventids list of event ids
1591 * @return array Array of event entries, empty array if nothing found
1593 function calendar_get_events_by_id($eventids) {
1594 global $DB;
1596 if (!is_array($eventids) || empty($eventids)) {
1597 return array();
1600 list($wheresql, $params) = $DB->get_in_or_equal($eventids);
1601 $wheresql = "id $wheresql";
1603 return $DB->get_records_select('event', $wheresql, $params);
1607 * Get control options for calendar.
1609 * @param string $type of calendar
1610 * @param array $data calendar information
1611 * @return string $content return available control for the calender in html
1613 function calendar_top_controls($type, $data) {
1614 global $PAGE, $OUTPUT;
1616 // Get the calendar type we are using.
1617 $calendartype = \core_calendar\type_factory::get_calendar_instance();
1619 $content = '';
1621 // Ensure course id passed if relevant.
1622 $courseid = '';
1623 if (!empty($data['id'])) {
1624 $courseid = '&amp;course=' . $data['id'];
1627 // If we are passing a month and year then we need to convert this to a timestamp to
1628 // support multiple calendars. No where in core should these be passed, this logic
1629 // here is for third party plugins that may use this function.
1630 if (!empty($data['m']) && !empty($date['y'])) {
1631 if (!isset($data['d'])) {
1632 $data['d'] = 1;
1634 if (!checkdate($data['m'], $data['d'], $data['y'])) {
1635 $time = time();
1636 } else {
1637 $time = make_timestamp($data['y'], $data['m'], $data['d']);
1639 } else if (!empty($data['time'])) {
1640 $time = $data['time'];
1641 } else {
1642 $time = time();
1645 // Get the date for the calendar type.
1646 $date = $calendartype->timestamp_to_date_array($time);
1648 $urlbase = $PAGE->url;
1650 // We need to get the previous and next months in certain cases.
1651 if ($type == 'frontpage' || $type == 'course' || $type == 'month') {
1652 $prevmonth = calendar_sub_month($date['mon'], $date['year']);
1653 $prevmonthtime = $calendartype->convert_to_gregorian($prevmonth[1], $prevmonth[0], 1);
1654 $prevmonthtime = make_timestamp($prevmonthtime['year'], $prevmonthtime['month'], $prevmonthtime['day'],
1655 $prevmonthtime['hour'], $prevmonthtime['minute']);
1657 $nextmonth = calendar_add_month($date['mon'], $date['year']);
1658 $nextmonthtime = $calendartype->convert_to_gregorian($nextmonth[1], $nextmonth[0], 1);
1659 $nextmonthtime = make_timestamp($nextmonthtime['year'], $nextmonthtime['month'], $nextmonthtime['day'],
1660 $nextmonthtime['hour'], $nextmonthtime['minute']);
1663 switch ($type) {
1664 case 'frontpage':
1665 $prevlink = calendar_get_link_previous(get_string('monthprev', 'calendar'), $urlbase, false, false, false,
1666 true, $prevmonthtime);
1667 $nextlink = calendar_get_link_next(get_string('monthnext', 'calendar'), $urlbase, false, false, false, true,
1668 $nextmonthtime);
1669 $calendarlink = calendar_get_link_href(new \moodle_url(CALENDAR_URL . 'view.php', array('view' => 'month')),
1670 false, false, false, $time);
1672 if (!empty($data['id'])) {
1673 $calendarlink->param('course', $data['id']);
1676 $right = $nextlink;
1678 $content .= \html_writer::start_tag('div', array('class' => 'calendar-controls'));
1679 $content .= $prevlink . '<span class="hide"> | </span>';
1680 $content .= \html_writer::tag('span', \html_writer::link($calendarlink,
1681 userdate($time, get_string('strftimemonthyear')), array('title' => get_string('monththis', 'calendar'))
1682 ), array('class' => 'current'));
1683 $content .= '<span class="hide"> | </span>' . $right;
1684 $content .= "<span class=\"clearer\"><!-- --></span>\n";
1685 $content .= \html_writer::end_tag('div');
1687 break;
1688 case 'course':
1689 $prevlink = calendar_get_link_previous(get_string('monthprev', 'calendar'), $urlbase, false, false, false,
1690 true, $prevmonthtime);
1691 $nextlink = calendar_get_link_next(get_string('monthnext', 'calendar'), $urlbase, false, false, false,
1692 true, $nextmonthtime);
1693 $calendarlink = calendar_get_link_href(new \moodle_url(CALENDAR_URL . 'view.php', array('view' => 'month')),
1694 false, false, false, $time);
1696 if (!empty($data['id'])) {
1697 $calendarlink->param('course', $data['id']);
1700 $content .= \html_writer::start_tag('div', array('class' => 'calendar-controls'));
1701 $content .= $prevlink . '<span class="hide"> | </span>';
1702 $content .= \html_writer::tag('span', \html_writer::link($calendarlink,
1703 userdate($time, get_string('strftimemonthyear')), array('title' => get_string('monththis', 'calendar'))
1704 ), array('class' => 'current'));
1705 $content .= '<span class="hide"> | </span>' . $nextlink;
1706 $content .= "<span class=\"clearer\"><!-- --></span>";
1707 $content .= \html_writer::end_tag('div');
1708 break;
1709 case 'upcoming':
1710 $calendarlink = calendar_get_link_href(new \moodle_url(CALENDAR_URL . 'view.php', array('view' => 'upcoming')),
1711 false, false, false, $time);
1712 if (!empty($data['id'])) {
1713 $calendarlink->param('course', $data['id']);
1715 $calendarlink = \html_writer::link($calendarlink, userdate($time, get_string('strftimemonthyear')));
1716 $content .= \html_writer::tag('div', $calendarlink, array('class' => 'centered'));
1717 break;
1718 case 'display':
1719 $calendarlink = calendar_get_link_href(new \moodle_url(CALENDAR_URL . 'view.php', array('view' => 'month')),
1720 false, false, false, $time);
1721 if (!empty($data['id'])) {
1722 $calendarlink->param('course', $data['id']);
1724 $calendarlink = \html_writer::link($calendarlink, userdate($time, get_string('strftimemonthyear')));
1725 $content .= \html_writer::tag('h3', $calendarlink);
1726 break;
1727 case 'month':
1728 $prevlink = calendar_get_link_previous(userdate($prevmonthtime, get_string('strftimemonthyear')),
1729 'view.php?view=month' . $courseid . '&amp;', false, false, false, false, $prevmonthtime);
1730 $nextlink = calendar_get_link_next(userdate($nextmonthtime, get_string('strftimemonthyear')),
1731 'view.php?view=month' . $courseid . '&amp;', false, false, false, false, $nextmonthtime);
1733 $content .= \html_writer::start_tag('div', array('class' => 'calendar-controls'));
1734 $content .= $prevlink . '<span class="hide"> | </span>';
1735 $content .= $OUTPUT->heading(userdate($time, get_string('strftimemonthyear')), 2, 'current');
1736 $content .= '<span class="hide"> | </span>' . $nextlink;
1737 $content .= '<span class="clearer"><!-- --></span>';
1738 $content .= \html_writer::end_tag('div')."\n";
1739 break;
1740 case 'day':
1741 $days = calendar_get_days();
1743 $prevtimestamp = strtotime('-1 day', $time);
1744 $nexttimestamp = strtotime('+1 day', $time);
1746 $prevdate = $calendartype->timestamp_to_date_array($prevtimestamp);
1747 $nextdate = $calendartype->timestamp_to_date_array($nexttimestamp);
1749 $prevname = $days[$prevdate['wday']]['fullname'];
1750 $nextname = $days[$nextdate['wday']]['fullname'];
1751 $prevlink = calendar_get_link_previous($prevname, 'view.php?view=day' . $courseid . '&amp;', false, false,
1752 false, false, $prevtimestamp);
1753 $nextlink = calendar_get_link_next($nextname, 'view.php?view=day' . $courseid . '&amp;', false, false, false,
1754 false, $nexttimestamp);
1756 $content .= \html_writer::start_tag('div', array('class' => 'calendar-controls'));
1757 $content .= $prevlink;
1758 $content .= '<span class="hide"> | </span><span class="current">' .userdate($time,
1759 get_string('strftimedaydate')) . '</span>';
1760 $content .= '<span class="hide"> | </span>' . $nextlink;
1761 $content .= "<span class=\"clearer\"><!-- --></span>";
1762 $content .= \html_writer::end_tag('div') . "\n";
1764 break;
1767 return $content;
1771 * Return the representation day.
1773 * @param int $tstamp Timestamp in GMT
1774 * @param int|bool $now current Unix timestamp
1775 * @param bool $usecommonwords
1776 * @return string the formatted date/time
1778 function calendar_day_representation($tstamp, $now = false, $usecommonwords = true) {
1779 static $shortformat;
1781 if (empty($shortformat)) {
1782 $shortformat = get_string('strftimedayshort');
1785 if ($now === false) {
1786 $now = time();
1789 // To have it in one place, if a change is needed.
1790 $formal = userdate($tstamp, $shortformat);
1792 $datestamp = usergetdate($tstamp);
1793 $datenow = usergetdate($now);
1795 if ($usecommonwords == false) {
1796 // We don't want words, just a date.
1797 return $formal;
1798 } else if ($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday']) {
1799 return get_string('today', 'calendar');
1800 } else if (($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday'] - 1 ) ||
1801 ($datestamp['year'] == $datenow['year'] - 1 && $datestamp['mday'] == 31 && $datestamp['mon'] == 12
1802 && $datenow['yday'] == 1)) {
1803 return get_string('yesterday', 'calendar');
1804 } else if (($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday'] + 1 ) ||
1805 ($datestamp['year'] == $datenow['year'] + 1 && $datenow['mday'] == 31 && $datenow['mon'] == 12
1806 && $datestamp['yday'] == 1)) {
1807 return get_string('tomorrow', 'calendar');
1808 } else {
1809 return $formal;
1814 * return the formatted representation time.
1817 * @param int $time the timestamp in UTC, as obtained from the database
1818 * @return string the formatted date/time
1820 function calendar_time_representation($time) {
1821 static $langtimeformat = null;
1823 if ($langtimeformat === null) {
1824 $langtimeformat = get_string('strftimetime');
1827 $timeformat = get_user_preferences('calendar_timeformat');
1828 if (empty($timeformat)) {
1829 $timeformat = get_config(null, 'calendar_site_timeformat');
1832 // Allow language customization of selected time format.
1833 if ($timeformat === CALENDAR_TF_12) {
1834 $timeformat = get_string('strftimetime12', 'langconfig');
1835 } else if ($timeformat === CALENDAR_TF_24) {
1836 $timeformat = get_string('strftimetime24', 'langconfig');
1839 return userdate($time, empty($timeformat) ? $langtimeformat : $timeformat);
1843 * Adds day, month, year arguments to a URL and returns a moodle_url object.
1845 * @param string|moodle_url $linkbase
1846 * @param int $d The number of the day.
1847 * @param int $m The number of the month.
1848 * @param int $y The number of the year.
1849 * @param int $time the unixtime, used for multiple calendar support. The values $d,
1850 * $m and $y are kept for backwards compatibility.
1851 * @return moodle_url|null $linkbase
1853 function calendar_get_link_href($linkbase, $d, $m, $y, $time = 0) {
1854 if (empty($linkbase)) {
1855 return null;
1858 if (!($linkbase instanceof \moodle_url)) {
1859 $linkbase = new \moodle_url($linkbase);
1862 $linkbase->param('time', calendar_get_timestamp($d, $m, $y, $time));
1864 return $linkbase;
1868 * Build and return a previous month HTML link, with an arrow.
1870 * @param string $text The text label.
1871 * @param string|moodle_url $linkbase The URL stub.
1872 * @param int $d The number of the date.
1873 * @param int $m The number of the month.
1874 * @param int $y year The number of the year.
1875 * @param bool $accesshide Default visible, or hide from all except screenreaders.
1876 * @param int $time the unixtime, used for multiple calendar support. The values $d,
1877 * $m and $y are kept for backwards compatibility.
1878 * @return string HTML string.
1880 function calendar_get_link_previous($text, $linkbase, $d, $m, $y, $accesshide = false, $time = 0) {
1881 $href = calendar_get_link_href(new \moodle_url($linkbase), $d, $m, $y, $time);
1883 if (empty($href)) {
1884 return $text;
1887 $attrs = [
1888 'data-time' => calendar_get_timestamp($d, $m, $y, $time),
1889 'data-drop-zone' => 'nav-link',
1892 return link_arrow_left($text, $href->out(false), $accesshide, 'previous', $attrs);
1896 * Build and return a next month HTML link, with an arrow.
1898 * @param string $text The text label.
1899 * @param string|moodle_url $linkbase The URL stub.
1900 * @param int $d the number of the Day
1901 * @param int $m The number of the month.
1902 * @param int $y The number of the year.
1903 * @param bool $accesshide Default visible, or hide from all except screenreaders.
1904 * @param int $time the unixtime, used for multiple calendar support. The values $d,
1905 * $m and $y are kept for backwards compatibility.
1906 * @return string HTML string.
1908 function calendar_get_link_next($text, $linkbase, $d, $m, $y, $accesshide = false, $time = 0) {
1909 $href = calendar_get_link_href(new \moodle_url($linkbase), $d, $m, $y, $time);
1911 if (empty($href)) {
1912 return $text;
1915 $attrs = [
1916 'data-time' => calendar_get_timestamp($d, $m, $y, $time),
1917 'data-drop-zone' => 'nav-link',
1920 return link_arrow_right($text, $href->out(false), $accesshide, 'next', $attrs);
1924 * Return the number of days in month.
1926 * @param int $month the number of the month.
1927 * @param int $year the number of the year
1928 * @return int
1930 function calendar_days_in_month($month, $year) {
1931 $calendartype = \core_calendar\type_factory::get_calendar_instance();
1932 return $calendartype->get_num_days_in_month($year, $month);
1936 * Get the next following month.
1938 * @param int $month the number of the month.
1939 * @param int $year the number of the year.
1940 * @return array the following month
1942 function calendar_add_month($month, $year) {
1943 $calendartype = \core_calendar\type_factory::get_calendar_instance();
1944 return $calendartype->get_next_month($year, $month);
1948 * Get the previous month.
1950 * @param int $month the number of the month.
1951 * @param int $year the number of the year.
1952 * @return array previous month
1954 function calendar_sub_month($month, $year) {
1955 $calendartype = \core_calendar\type_factory::get_calendar_instance();
1956 return $calendartype->get_prev_month($year, $month);
1960 * Get per-day basis events
1962 * @param array $events list of events
1963 * @param int $month the number of the month
1964 * @param int $year the number of the year
1965 * @param array $eventsbyday event on specific day
1966 * @param array $durationbyday duration of the event in days
1967 * @param array $typesbyday event type (eg: site, course, user, or group)
1968 * @param array $courses list of courses
1969 * @return void
1971 function calendar_events_by_day($events, $month, $year, &$eventsbyday, &$durationbyday, &$typesbyday, &$courses) {
1972 $calendartype = \core_calendar\type_factory::get_calendar_instance();
1974 $eventsbyday = array();
1975 $typesbyday = array();
1976 $durationbyday = array();
1978 if ($events === false) {
1979 return;
1982 foreach ($events as $event) {
1983 $startdate = $calendartype->timestamp_to_date_array($event->timestart);
1984 if ($event->timeduration) {
1985 $enddate = $calendartype->timestamp_to_date_array($event->timestart + $event->timeduration - 1);
1986 } else {
1987 $enddate = $startdate;
1990 // Simple arithmetic: $year * 13 + $month is a distinct integer for each distinct ($year, $month) pair.
1991 if (!($startdate['year'] * 13 + $startdate['mon'] <= $year * 13 + $month) &&
1992 ($enddate['year'] * 13 + $enddate['mon'] >= $year * 13 + $month)) {
1993 continue;
1996 $eventdaystart = intval($startdate['mday']);
1998 if ($startdate['mon'] == $month && $startdate['year'] == $year) {
1999 // Give the event to its day.
2000 $eventsbyday[$eventdaystart][] = $event->id;
2002 // Mark the day as having such an event.
2003 if ($event->courseid == SITEID && $event->groupid == 0) {
2004 $typesbyday[$eventdaystart]['startsite'] = true;
2005 // Set event class for site event.
2006 $events[$event->id]->class = 'calendar_event_site';
2007 } else if ($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) {
2008 $typesbyday[$eventdaystart]['startcourse'] = true;
2009 // Set event class for course event.
2010 $events[$event->id]->class = 'calendar_event_course';
2011 } else if ($event->groupid) {
2012 $typesbyday[$eventdaystart]['startgroup'] = true;
2013 // Set event class for group event.
2014 $events[$event->id]->class = 'calendar_event_group';
2015 } else if ($event->userid) {
2016 $typesbyday[$eventdaystart]['startuser'] = true;
2017 // Set event class for user event.
2018 $events[$event->id]->class = 'calendar_event_user';
2022 if ($event->timeduration == 0) {
2023 // Proceed with the next.
2024 continue;
2027 // The event starts on $month $year or before.
2028 if ($startdate['mon'] == $month && $startdate['year'] == $year) {
2029 $lowerbound = intval($startdate['mday']);
2030 } else {
2031 $lowerbound = 0;
2034 // Also, it ends on $month $year or later.
2035 if ($enddate['mon'] == $month && $enddate['year'] == $year) {
2036 $upperbound = intval($enddate['mday']);
2037 } else {
2038 $upperbound = calendar_days_in_month($month, $year);
2041 // Mark all days between $lowerbound and $upperbound (inclusive) as duration.
2042 for ($i = $lowerbound + 1; $i <= $upperbound; ++$i) {
2043 $durationbyday[$i][] = $event->id;
2044 if ($event->courseid == SITEID && $event->groupid == 0) {
2045 $typesbyday[$i]['durationsite'] = true;
2046 } else if ($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) {
2047 $typesbyday[$i]['durationcourse'] = true;
2048 } else if ($event->groupid) {
2049 $typesbyday[$i]['durationgroup'] = true;
2050 } else if ($event->userid) {
2051 $typesbyday[$i]['durationuser'] = true;
2056 return;
2060 * Returns the courses to load events for.
2062 * @param array $courseeventsfrom An array of courses to load calendar events for
2063 * @param bool $ignorefilters specify the use of filters, false is set as default
2064 * @param stdClass $user The user object. This defaults to the global $USER object.
2065 * @return array An array of courses, groups, and user to load calendar events for based upon filters
2067 function calendar_set_filters(array $courseeventsfrom, $ignorefilters = false, stdClass $user = null) {
2068 global $CFG, $USER;
2070 if (is_null($user)) {
2071 $user = $USER;
2074 $courses = array();
2075 $userid = false;
2076 $group = false;
2078 // Get the capabilities that allow seeing group events from all groups.
2079 $allgroupscaps = array('moodle/site:accessallgroups', 'moodle/calendar:manageentries');
2081 $isvaliduser = !empty($user->id);
2083 if ($ignorefilters || calendar_show_event_type(CALENDAR_EVENT_COURSE, $user)) {
2084 $courses = array_keys($courseeventsfrom);
2086 if ($ignorefilters || calendar_show_event_type(CALENDAR_EVENT_SITE, $user)) {
2087 $courses[] = SITEID;
2089 $courses = array_unique($courses);
2090 sort($courses);
2092 if (!empty($courses) && in_array(SITEID, $courses)) {
2093 // Sort courses for consistent colour highlighting.
2094 // Effectively ignoring SITEID as setting as last course id.
2095 $key = array_search(SITEID, $courses);
2096 unset($courses[$key]);
2097 $courses[] = SITEID;
2100 if ($ignorefilters || ($isvaliduser && calendar_show_event_type(CALENDAR_EVENT_USER, $user))) {
2101 $userid = $user->id;
2104 if (!empty($courseeventsfrom) && (calendar_show_event_type(CALENDAR_EVENT_GROUP, $user) || $ignorefilters)) {
2106 if (count($courseeventsfrom) == 1) {
2107 $course = reset($courseeventsfrom);
2108 if (has_any_capability($allgroupscaps, \context_course::instance($course->id))) {
2109 $coursegroups = groups_get_all_groups($course->id, 0, 0, 'g.id');
2110 $group = array_keys($coursegroups);
2113 if ($group === false) {
2114 if (!empty($CFG->calendar_adminseesall) && has_any_capability($allgroupscaps, \context_system::instance())) {
2115 $group = true;
2116 } else if ($isvaliduser) {
2117 $groupids = array();
2118 foreach ($courseeventsfrom as $courseid => $course) {
2119 // If the user is an editing teacher in there.
2120 if (!empty($user->groupmember[$course->id])) {
2121 // We've already cached the users groups for this course so we can just use that.
2122 $groupids = array_merge($groupids, $user->groupmember[$course->id]);
2123 } else if ($course->groupmode != NOGROUPS || !$course->groupmodeforce) {
2124 // If this course has groups, show events from all of those related to the current user.
2125 $coursegroups = groups_get_user_groups($course->id, $user->id);
2126 $groupids = array_merge($groupids, $coursegroups['0']);
2129 if (!empty($groupids)) {
2130 $group = $groupids;
2135 if (empty($courses)) {
2136 $courses = false;
2139 return array($courses, $group, $userid);
2143 * Return the capability for viewing a calendar event.
2145 * @param calendar_event $event event object
2146 * @return boolean
2148 function calendar_view_event_allowed(calendar_event $event) {
2149 global $USER;
2151 // Anyone can see site events.
2152 if ($event->courseid && $event->courseid == SITEID) {
2153 return true;
2156 // If a user can manage events at the site level they can see any event.
2157 $sitecontext = \context_system::instance();
2158 // If user has manageentries at site level, return true.
2159 if (has_capability('moodle/calendar:manageentries', $sitecontext)) {
2160 return true;
2163 if (!empty($event->groupid)) {
2164 // If it is a group event we need to be able to manage events in the course, or be in the group.
2165 if (has_capability('moodle/calendar:manageentries', $event->context) ||
2166 has_capability('moodle/calendar:managegroupentries', $event->context)) {
2167 return true;
2170 $mycourses = enrol_get_my_courses('id');
2171 return isset($mycourses[$event->courseid]) && groups_is_member($event->groupid);
2172 } else if ($event->modulename) {
2173 // If this is a module event we need to be able to see the module.
2174 $coursemodules = [];
2175 $courseid = 0;
2176 // Override events do not have the courseid set.
2177 if ($event->courseid) {
2178 $courseid = $event->courseid;
2179 $coursemodules = get_fast_modinfo($event->courseid)->instances;
2180 } else {
2181 $cmraw = get_coursemodule_from_instance($event->modulename, $event->instance, 0, false, MUST_EXIST);
2182 $courseid = $cmraw->course;
2183 $coursemodules = get_fast_modinfo($cmraw->course)->instances;
2185 $hasmodule = isset($coursemodules[$event->modulename]);
2186 $hasinstance = isset($coursemodules[$event->modulename][$event->instance]);
2188 // If modinfo doesn't know about the module, return false to be safe.
2189 if (!$hasmodule || !$hasinstance) {
2190 return false;
2193 // Must be able to see the course and the module - MDL-59304.
2194 $cm = $coursemodules[$event->modulename][$event->instance];
2195 if (!$cm->uservisible) {
2196 return false;
2198 $mycourses = enrol_get_my_courses('id');
2199 return isset($mycourses[$courseid]);
2200 } else if ($event->categoryid) {
2201 // If this is a category we need to be able to see the category.
2202 $cat = \core_course_category::get($event->categoryid, IGNORE_MISSING);
2203 if (!$cat) {
2204 return false;
2206 return true;
2207 } else if (!empty($event->courseid)) {
2208 // If it is a course event we need to be able to manage events in the course, or be in the course.
2209 if (has_capability('moodle/calendar:manageentries', $event->context)) {
2210 return true;
2213 return can_access_course(get_course($event->courseid));
2214 } else if ($event->userid) {
2215 if ($event->userid != $USER->id) {
2216 // No-one can ever see another users events.
2217 return false;
2219 return true;
2220 } else {
2221 throw new moodle_exception('unknown event type');
2224 return false;
2228 * Return the capability for editing calendar event.
2230 * @param calendar_event $event event object
2231 * @param bool $manualedit is the event being edited manually by the user
2232 * @return bool capability to edit event
2234 function calendar_edit_event_allowed($event, $manualedit = false) {
2235 global $USER, $DB;
2237 // Must be logged in.
2238 if (!isloggedin()) {
2239 return false;
2242 // Can not be using guest account.
2243 if (isguestuser()) {
2244 return false;
2247 if ($manualedit && !empty($event->modulename)) {
2248 $hascallback = component_callback_exists(
2249 'mod_' . $event->modulename,
2250 'core_calendar_event_timestart_updated'
2253 if (!$hascallback) {
2254 // If the activity hasn't implemented the correct callback
2255 // to handle changes to it's events then don't allow any
2256 // manual changes to them.
2257 return false;
2260 $coursemodules = get_fast_modinfo($event->courseid)->instances;
2261 $hasmodule = isset($coursemodules[$event->modulename]);
2262 $hasinstance = isset($coursemodules[$event->modulename][$event->instance]);
2264 // If modinfo doesn't know about the module, return false to be safe.
2265 if (!$hasmodule || !$hasinstance) {
2266 return false;
2269 $coursemodule = $coursemodules[$event->modulename][$event->instance];
2270 $context = context_module::instance($coursemodule->id);
2271 // This is the capability that allows a user to modify the activity
2272 // settings. Since the activity generated this event we need to check
2273 // that the current user has the same capability before allowing them
2274 // to update the event because the changes to the event will be
2275 // reflected within the activity.
2276 return has_capability('moodle/course:manageactivities', $context);
2279 // You cannot edit URL based calendar subscription events presently.
2280 if (!empty($event->subscriptionid)) {
2281 if (!empty($event->subscription->url)) {
2282 // This event can be updated externally, so it cannot be edited.
2283 return false;
2287 $sitecontext = \context_system::instance();
2289 // If user has manageentries at site level, return true.
2290 if (has_capability('moodle/calendar:manageentries', $sitecontext)) {
2291 return true;
2294 // If groupid is set, it's definitely a group event.
2295 if (!empty($event->groupid)) {
2296 // Allow users to add/edit group events if -
2297 // 1) They have manageentries for the course OR
2298 // 2) They have managegroupentries AND are in the group.
2299 $group = $DB->get_record('groups', array('id' => $event->groupid));
2300 return $group && (
2301 has_capability('moodle/calendar:manageentries', $event->context) ||
2302 (has_capability('moodle/calendar:managegroupentries', $event->context)
2303 && groups_is_member($event->groupid)));
2304 } else if (!empty($event->courseid)) {
2305 // If groupid is not set, but course is set, it's definitely a course event.
2306 return has_capability('moodle/calendar:manageentries', $event->context);
2307 } else if (!empty($event->categoryid)) {
2308 // If groupid is not set, but category is set, it's definitely a category event.
2309 return has_capability('moodle/calendar:manageentries', $event->context);
2310 } else if (!empty($event->userid) && $event->userid == $USER->id) {
2311 // If course is not set, but userid id set, it's a user event.
2312 return (has_capability('moodle/calendar:manageownentries', $event->context));
2313 } else if (!empty($event->userid)) {
2314 return (has_capability('moodle/calendar:manageentries', $event->context));
2317 return false;
2321 * Return the capability for deleting a calendar event.
2323 * @param calendar_event $event The event object
2324 * @return bool Whether the user has permission to delete the event or not.
2326 function calendar_delete_event_allowed($event) {
2327 // Only allow delete if you have capabilities and it is not an module event.
2328 return (calendar_edit_event_allowed($event) && empty($event->modulename));
2332 * Returns the default courses to display on the calendar when there isn't a specific
2333 * course to display.
2335 * @param int $courseid (optional) If passed, an additional course can be returned for admins (the current course).
2336 * @param string $fields Comma separated list of course fields to return.
2337 * @param bool $canmanage If true, this will return the list of courses the user can create events in, rather
2338 * than the list of courses they see events from (an admin can always add events in a course
2339 * calendar, even if they are not enrolled in the course).
2340 * @param int $userid (optional) The user which this function returns the default courses for.
2341 * By default the current user.
2342 * @return array $courses Array of courses to display
2344 function calendar_get_default_courses($courseid = null, $fields = '*', $canmanage = false, int $userid = null) {
2345 global $CFG, $USER;
2347 if (!$userid) {
2348 if (!isloggedin()) {
2349 return array();
2351 $userid = $USER->id;
2354 if ((!empty($CFG->calendar_adminseesall) || $canmanage) &&
2355 has_capability('moodle/calendar:manageentries', context_system::instance(), $userid)) {
2357 // Add a c. prefix to every field as expected by get_courses function.
2358 $fieldlist = explode(',', $fields);
2360 $prefixedfields = array_map(function($value) {
2361 return 'c.' . trim(strtolower($value));
2362 }, $fieldlist);
2363 if (!in_array('c.visible', $prefixedfields) && !in_array('c.*', $prefixedfields)) {
2364 $prefixedfields[] = 'c.visible';
2366 $courses = get_courses('all', 'c.shortname', implode(',', $prefixedfields));
2367 } else {
2368 $courses = enrol_get_users_courses($userid, true, $fields);
2371 if ($courseid && $courseid != SITEID) {
2372 if (empty($courses[$courseid]) && has_capability('moodle/calendar:manageentries', context_system::instance(), $userid)) {
2373 // Allow a site admin to see calendars from courses he is not enrolled in.
2374 // This will come from $COURSE.
2375 $courses[$courseid] = get_course($courseid);
2379 return $courses;
2383 * Get event format time.
2385 * @param calendar_event $event event object
2386 * @param int $now current time in gmt
2387 * @param array $linkparams list of params for event link
2388 * @param bool $usecommonwords the words as formatted date/time.
2389 * @param int $showtime determine the show time GMT timestamp
2390 * @return string $eventtime link/string for event time
2392 function calendar_format_event_time($event, $now, $linkparams = null, $usecommonwords = true, $showtime = 0) {
2393 $starttime = $event->timestart;
2394 $endtime = $event->timestart + $event->timeduration;
2396 if (empty($linkparams) || !is_array($linkparams)) {
2397 $linkparams = array();
2400 $linkparams['view'] = 'day';
2402 // OK, now to get a meaningful display.
2403 // Check if there is a duration for this event.
2404 if ($event->timeduration) {
2405 // Get the midnight of the day the event will start.
2406 $usermidnightstart = usergetmidnight($starttime);
2407 // Get the midnight of the day the event will end.
2408 $usermidnightend = usergetmidnight($endtime);
2409 // Check if we will still be on the same day.
2410 if ($usermidnightstart == $usermidnightend) {
2411 // Check if we are running all day.
2412 if ($event->timeduration == DAYSECS) {
2413 $time = get_string('allday', 'calendar');
2414 } else { // Specify the time we will be running this from.
2415 $datestart = calendar_time_representation($starttime);
2416 $dateend = calendar_time_representation($endtime);
2417 $time = $datestart . ' <strong>&raquo;</strong> ' . $dateend;
2420 // Set printable representation.
2421 if (!$showtime) {
2422 $day = calendar_day_representation($event->timestart, $now, $usecommonwords);
2423 $url = calendar_get_link_href(new \moodle_url(CALENDAR_URL . 'view.php', $linkparams), 0, 0, 0, $endtime);
2424 $eventtime = \html_writer::link($url, $day) . ', ' . $time;
2425 } else {
2426 $eventtime = $time;
2428 } else { // It must spans two or more days.
2429 $daystart = calendar_day_representation($event->timestart, $now, $usecommonwords) . ', ';
2430 if ($showtime == $usermidnightstart) {
2431 $daystart = '';
2433 $timestart = calendar_time_representation($event->timestart);
2434 $dayend = calendar_day_representation($event->timestart + $event->timeduration, $now, $usecommonwords) . ', ';
2435 if ($showtime == $usermidnightend) {
2436 $dayend = '';
2438 $timeend = calendar_time_representation($event->timestart + $event->timeduration);
2440 // Set printable representation.
2441 if ($now >= $usermidnightstart && $now < strtotime('+1 day', $usermidnightstart)) {
2442 $url = calendar_get_link_href(new \moodle_url(CALENDAR_URL . 'view.php', $linkparams), 0, 0, 0, $endtime);
2443 $eventtime = $timestart . ' <strong>&raquo;</strong> ' . \html_writer::link($url, $dayend) . $timeend;
2444 } else {
2445 // The event is in the future, print start and end links.
2446 $url = calendar_get_link_href(new \moodle_url(CALENDAR_URL . 'view.php', $linkparams), 0, 0, 0, $starttime);
2447 $eventtime = \html_writer::link($url, $daystart) . $timestart . ' <strong>&raquo;</strong> ';
2449 $url = calendar_get_link_href(new \moodle_url(CALENDAR_URL . 'view.php', $linkparams), 0, 0, 0, $endtime);
2450 $eventtime .= \html_writer::link($url, $dayend) . $timeend;
2453 } else { // There is no time duration.
2454 $time = calendar_time_representation($event->timestart);
2455 // Set printable representation.
2456 if (!$showtime) {
2457 $day = calendar_day_representation($event->timestart, $now, $usecommonwords);
2458 $url = calendar_get_link_href(new \moodle_url(CALENDAR_URL . 'view.php', $linkparams), 0, 0, 0, $starttime);
2459 $eventtime = \html_writer::link($url, $day) . ', ' . trim($time);
2460 } else {
2461 $eventtime = $time;
2465 // Check if It has expired.
2466 if ($event->timestart + $event->timeduration < $now) {
2467 $eventtime = '<span class="dimmed_text">' . str_replace(' href=', ' class="dimmed" href=', $eventtime) . '</span>';
2470 return $eventtime;
2474 * Checks to see if the requested type of event should be shown for the given user.
2476 * @param int $type The type to check the display for (default is to display all)
2477 * @param stdClass|int|null $user The user to check for - by default the current user
2478 * @return bool True if the tyep should be displayed false otherwise
2480 function calendar_show_event_type($type, $user = null) {
2481 $default = CALENDAR_EVENT_SITE + CALENDAR_EVENT_COURSE + CALENDAR_EVENT_GROUP + CALENDAR_EVENT_USER;
2483 if (get_user_preferences('calendar_persistflt', 0, $user) === 0) {
2484 global $SESSION;
2485 if (!isset($SESSION->calendarshoweventtype)) {
2486 $SESSION->calendarshoweventtype = $default;
2488 return $SESSION->calendarshoweventtype & $type;
2489 } else {
2490 return get_user_preferences('calendar_savedflt', $default, $user) & $type;
2495 * Sets the display of the event type given $display.
2497 * If $display = true the event type will be shown.
2498 * If $display = false the event type will NOT be shown.
2499 * If $display = null the current value will be toggled and saved.
2501 * @param int $type object of CALENDAR_EVENT_XXX
2502 * @param bool $display option to display event type
2503 * @param stdClass|int $user moodle user object or id, null means current user
2505 function calendar_set_event_type_display($type, $display = null, $user = null) {
2506 $persist = get_user_preferences('calendar_persistflt', 0, $user);
2507 $default = CALENDAR_EVENT_SITE + CALENDAR_EVENT_COURSE + CALENDAR_EVENT_GROUP
2508 + CALENDAR_EVENT_USER + CALENDAR_EVENT_COURSECAT;
2509 if ($persist === 0) {
2510 global $SESSION;
2511 if (!isset($SESSION->calendarshoweventtype)) {
2512 $SESSION->calendarshoweventtype = $default;
2514 $preference = $SESSION->calendarshoweventtype;
2515 } else {
2516 $preference = get_user_preferences('calendar_savedflt', $default, $user);
2518 $current = $preference & $type;
2519 if ($display === null) {
2520 $display = !$current;
2522 if ($display && !$current) {
2523 $preference += $type;
2524 } else if (!$display && $current) {
2525 $preference -= $type;
2527 if ($persist === 0) {
2528 $SESSION->calendarshoweventtype = $preference;
2529 } else {
2530 if ($preference == $default) {
2531 unset_user_preference('calendar_savedflt', $user);
2532 } else {
2533 set_user_preference('calendar_savedflt', $preference, $user);
2539 * Get calendar's allowed types.
2541 * @param stdClass $allowed list of allowed edit for event type
2542 * @param stdClass|int $course object of a course or course id
2543 * @param array $groups array of groups for the given course
2544 * @param stdClass|int $category object of a category
2546 function calendar_get_allowed_types(&$allowed, $course = null, $groups = null, $category = null) {
2547 global $USER, $DB;
2549 $allowed = new \stdClass();
2550 $allowed->user = has_capability('moodle/calendar:manageownentries', \context_system::instance());
2551 $allowed->groups = false;
2552 $allowed->courses = false;
2553 $allowed->categories = false;
2554 $allowed->site = has_capability('moodle/calendar:manageentries', \context_course::instance(SITEID));
2555 $getgroupsfunc = function($course, $context, $user) use ($groups) {
2556 if ($course->groupmode != NOGROUPS || !$course->groupmodeforce) {
2557 if (has_capability('moodle/site:accessallgroups', $context)) {
2558 return is_null($groups) ? groups_get_all_groups($course->id) : $groups;
2559 } else {
2560 if (is_null($groups)) {
2561 return groups_get_all_groups($course->id, $user->id);
2562 } else {
2563 return array_filter($groups, function($group) use ($user) {
2564 return isset($group->members[$user->id]);
2570 return false;
2573 if (!empty($course)) {
2574 if (!is_object($course)) {
2575 $course = $DB->get_record('course', array('id' => $course), 'id, groupmode, groupmodeforce', MUST_EXIST);
2577 if ($course->id != SITEID) {
2578 $coursecontext = \context_course::instance($course->id);
2579 $allowed->user = has_capability('moodle/calendar:manageownentries', $coursecontext);
2581 if (has_capability('moodle/calendar:manageentries', $coursecontext)) {
2582 $allowed->courses = array($course->id => 1);
2583 $allowed->groups = $getgroupsfunc($course, $coursecontext, $USER);
2584 } else if (has_capability('moodle/calendar:managegroupentries', $coursecontext)) {
2585 $allowed->groups = $getgroupsfunc($course, $coursecontext, $USER);
2590 if (!empty($category)) {
2591 $catcontext = \context_coursecat::instance($category->id);
2592 if (has_capability('moodle/category:manage', $catcontext)) {
2593 $allowed->categories = [$category->id => 1];
2599 * See if user can add calendar entries at all used to print the "New Event" button.
2601 * @param stdClass $course object of a course or course id
2602 * @return bool has the capability to add at least one event type
2604 function calendar_user_can_add_event($course) {
2605 if (!isloggedin() || isguestuser()) {
2606 return false;
2609 calendar_get_allowed_types($allowed, $course);
2611 return (bool)($allowed->user || $allowed->groups || $allowed->courses || $allowed->categories || $allowed->site);
2615 * Check wether the current user is permitted to add events.
2617 * @param stdClass $event object of event
2618 * @return bool has the capability to add event
2620 function calendar_add_event_allowed($event) {
2621 global $USER, $DB;
2623 // Can not be using guest account.
2624 if (!isloggedin() or isguestuser()) {
2625 return false;
2628 $sitecontext = \context_system::instance();
2630 // If user has manageentries at site level, always return true.
2631 if (has_capability('moodle/calendar:manageentries', $sitecontext)) {
2632 return true;
2635 switch ($event->eventtype) {
2636 case 'category':
2637 return has_capability('moodle/category:manage', $event->context);
2638 case 'course':
2639 return has_capability('moodle/calendar:manageentries', $event->context);
2640 case 'group':
2641 // Allow users to add/edit group events if -
2642 // 1) They have manageentries (= entries for whole course).
2643 // 2) They have managegroupentries AND are in the group.
2644 $group = $DB->get_record('groups', array('id' => $event->groupid));
2645 return $group && (
2646 has_capability('moodle/calendar:manageentries', $event->context) ||
2647 (has_capability('moodle/calendar:managegroupentries', $event->context)
2648 && groups_is_member($event->groupid)));
2649 case 'user':
2650 if ($event->userid == $USER->id) {
2651 return (has_capability('moodle/calendar:manageownentries', $event->context));
2653 // There is intentionally no 'break'.
2654 case 'site':
2655 return has_capability('moodle/calendar:manageentries', $event->context);
2656 default:
2657 return has_capability('moodle/calendar:manageentries', $event->context);
2662 * Returns option list for the poll interval setting.
2664 * @return array An array of poll interval options. Interval => description.
2666 function calendar_get_pollinterval_choices() {
2667 return array(
2668 '0' => new \lang_string('never', 'calendar'),
2669 HOURSECS => new \lang_string('hourly', 'calendar'),
2670 DAYSECS => new \lang_string('daily', 'calendar'),
2671 WEEKSECS => new \lang_string('weekly', 'calendar'),
2672 '2628000' => new \lang_string('monthly', 'calendar'),
2673 YEARSECS => new \lang_string('annually', 'calendar')
2678 * Returns option list of available options for the calendar event type, given the current user and course.
2680 * @param int $courseid The id of the course
2681 * @return array An array containing the event types the user can create.
2683 function calendar_get_eventtype_choices($courseid) {
2684 $choices = array();
2685 $allowed = new \stdClass;
2686 calendar_get_allowed_types($allowed, $courseid);
2688 if ($allowed->user) {
2689 $choices['user'] = get_string('userevents', 'calendar');
2691 if ($allowed->site) {
2692 $choices['site'] = get_string('siteevents', 'calendar');
2694 if (!empty($allowed->courses)) {
2695 $choices['course'] = get_string('courseevents', 'calendar');
2697 if (!empty($allowed->categories)) {
2698 $choices['category'] = get_string('categoryevents', 'calendar');
2700 if (!empty($allowed->groups) and is_array($allowed->groups)) {
2701 $choices['group'] = get_string('group');
2704 return array($choices, $allowed->groups);
2708 * Add an iCalendar subscription to the database.
2710 * @param stdClass $sub The subscription object (e.g. from the form)
2711 * @return int The insert ID, if any.
2713 function calendar_add_subscription($sub) {
2714 global $DB, $USER, $SITE;
2716 // Undo the form definition work around to allow us to have two different
2717 // course selectors present depending on which event type the user selects.
2718 if (!empty($sub->groupcourseid)) {
2719 $sub->courseid = $sub->groupcourseid;
2720 unset($sub->groupcourseid);
2723 // Default course id if none is set.
2724 if (empty($sub->courseid)) {
2725 if ($sub->eventtype === 'site') {
2726 $sub->courseid = SITEID;
2727 } else {
2728 $sub->courseid = 0;
2732 if ($sub->eventtype === 'site') {
2733 $sub->courseid = $SITE->id;
2734 } else if ($sub->eventtype === 'group' || $sub->eventtype === 'course') {
2735 $sub->courseid = $sub->courseid;
2736 } else if ($sub->eventtype === 'category') {
2737 $sub->categoryid = $sub->categoryid;
2738 } else {
2739 // User events.
2740 $sub->courseid = 0;
2742 $sub->userid = $USER->id;
2744 // File subscriptions never update.
2745 if (empty($sub->url)) {
2746 $sub->pollinterval = 0;
2749 if (!empty($sub->name)) {
2750 if (empty($sub->id)) {
2751 $id = $DB->insert_record('event_subscriptions', $sub);
2752 // We cannot cache the data here because $sub is not complete.
2753 $sub->id = $id;
2754 // Trigger event, calendar subscription added.
2755 $eventparams = array('objectid' => $sub->id,
2756 'context' => calendar_get_calendar_context($sub),
2757 'other' => array(
2758 'eventtype' => $sub->eventtype,
2761 switch ($sub->eventtype) {
2762 case 'category':
2763 $eventparams['other']['categoryid'] = $sub->categoryid;
2764 break;
2765 case 'course':
2766 $eventparams['other']['courseid'] = $sub->courseid;
2767 break;
2768 case 'group':
2769 $eventparams['other']['courseid'] = $sub->courseid;
2770 $eventparams['other']['groupid'] = $sub->groupid;
2771 break;
2772 default:
2773 $eventparams['other']['courseid'] = $sub->courseid;
2776 $event = \core\event\calendar_subscription_created::create($eventparams);
2777 $event->trigger();
2778 return $id;
2779 } else {
2780 // Why are we doing an update here?
2781 calendar_update_subscription($sub);
2782 return $sub->id;
2784 } else {
2785 print_error('errorbadsubscription', 'importcalendar');
2790 * Add an iCalendar event to the Moodle calendar.
2792 * @param stdClass $event The RFC-2445 iCalendar event
2793 * @param int $unused Deprecated
2794 * @param int $subscriptionid The iCalendar subscription ID
2795 * @param string $timezone The X-WR-TIMEZONE iCalendar property if provided
2796 * @throws dml_exception A DML specific exception is thrown for invalid subscriptionids.
2797 * @return int Code: CALENDAR_IMPORT_EVENT_UPDATED = updated, CALENDAR_IMPORT_EVENT_INSERTED = inserted, 0 = error
2799 function calendar_add_icalendar_event($event, $unused = null, $subscriptionid, $timezone='UTC') {
2800 global $DB;
2802 // Probably an unsupported X-MICROSOFT-CDO-BUSYSTATUS event.
2803 if (empty($event->properties['SUMMARY'])) {
2804 return 0;
2807 $name = $event->properties['SUMMARY'][0]->value;
2808 $name = str_replace('\n', '<br />', $name);
2809 $name = str_replace('\\', '', $name);
2810 $name = preg_replace('/\s+/u', ' ', $name);
2812 $eventrecord = new \stdClass;
2813 $eventrecord->name = clean_param($name, PARAM_NOTAGS);
2815 if (empty($event->properties['DESCRIPTION'][0]->value)) {
2816 $description = '';
2817 } else {
2818 $description = $event->properties['DESCRIPTION'][0]->value;
2819 $description = clean_param($description, PARAM_NOTAGS);
2820 $description = str_replace('\n', '<br />', $description);
2821 $description = str_replace('\\', '', $description);
2822 $description = preg_replace('/\s+/u', ' ', $description);
2824 $eventrecord->description = $description;
2826 // Probably a repeating event with RRULE etc. TODO: skip for now.
2827 if (empty($event->properties['DTSTART'][0]->value)) {
2828 return 0;
2831 if (isset($event->properties['DTSTART'][0]->parameters['TZID'])) {
2832 $tz = $event->properties['DTSTART'][0]->parameters['TZID'];
2833 } else {
2834 $tz = $timezone;
2836 $tz = \core_date::normalise_timezone($tz);
2837 $eventrecord->timestart = strtotime($event->properties['DTSTART'][0]->value . ' ' . $tz);
2838 if (empty($event->properties['DTEND'])) {
2839 $eventrecord->timeduration = 0; // No duration if no end time specified.
2840 } else {
2841 if (isset($event->properties['DTEND'][0]->parameters['TZID'])) {
2842 $endtz = $event->properties['DTEND'][0]->parameters['TZID'];
2843 } else {
2844 $endtz = $timezone;
2846 $endtz = \core_date::normalise_timezone($endtz);
2847 $eventrecord->timeduration = strtotime($event->properties['DTEND'][0]->value . ' ' . $endtz) - $eventrecord->timestart;
2850 // Check to see if it should be treated as an all day event.
2851 if ($eventrecord->timeduration == DAYSECS) {
2852 // Check to see if the event started at Midnight on the imported calendar.
2853 date_default_timezone_set($timezone);
2854 if (date('H:i:s', $eventrecord->timestart) === "00:00:00") {
2855 // This event should be an all day event. This is not correct, we don't do anything differently for all day events.
2856 // See MDL-56227.
2857 $eventrecord->timeduration = 0;
2859 \core_date::set_default_server_timezone();
2862 $eventrecord->location = empty($event->properties['LOCATION'][0]->value) ? '' :
2863 trim(str_replace('\\', '', $event->properties['LOCATION'][0]->value));
2864 $eventrecord->uuid = $event->properties['UID'][0]->value;
2865 $eventrecord->timemodified = time();
2867 // Add the iCal subscription details if required.
2868 // We should never do anything with an event without a subscription reference.
2869 $sub = calendar_get_subscription($subscriptionid);
2870 $eventrecord->subscriptionid = $subscriptionid;
2871 $eventrecord->userid = $sub->userid;
2872 $eventrecord->groupid = $sub->groupid;
2873 $eventrecord->courseid = $sub->courseid;
2874 $eventrecord->categoryid = $sub->categoryid;
2875 $eventrecord->eventtype = $sub->eventtype;
2877 if ($updaterecord = $DB->get_record('event', array('uuid' => $eventrecord->uuid,
2878 'subscriptionid' => $eventrecord->subscriptionid))) {
2880 // Compare iCal event data against the moodle event to see if something has changed.
2881 $result = array_diff((array) $eventrecord, (array) $updaterecord);
2883 // Unset timemodified field because it's always going to be different.
2884 unset($result['timemodified']);
2886 if (count($result)) {
2887 $eventrecord->id = $updaterecord->id;
2888 $return = CALENDAR_IMPORT_EVENT_UPDATED; // Update.
2889 } else {
2890 return CALENDAR_IMPORT_EVENT_SKIPPED;
2892 } else {
2893 $return = CALENDAR_IMPORT_EVENT_INSERTED; // Insert.
2896 if ($createdevent = \calendar_event::create($eventrecord, false)) {
2897 if (!empty($event->properties['RRULE'])) {
2898 // Repeating events.
2899 date_default_timezone_set($tz); // Change time zone to parse all events.
2900 $rrule = new \core_calendar\rrule_manager($event->properties['RRULE'][0]->value);
2901 $rrule->parse_rrule();
2902 $rrule->create_events($createdevent);
2903 \core_date::set_default_server_timezone(); // Change time zone back to what it was.
2905 return $return;
2906 } else {
2907 return 0;
2912 * Update a subscription from the form data in one of the rows in the existing subscriptions table.
2914 * @param int $subscriptionid The ID of the subscription we are acting upon.
2915 * @param int $pollinterval The poll interval to use.
2916 * @param int $action The action to be performed. One of update or remove.
2917 * @throws dml_exception if invalid subscriptionid is provided
2918 * @return string A log of the import progress, including errors
2920 function calendar_process_subscription_row($subscriptionid, $pollinterval, $action) {
2921 // Fetch the subscription from the database making sure it exists.
2922 $sub = calendar_get_subscription($subscriptionid);
2924 // Update or remove the subscription, based on action.
2925 switch ($action) {
2926 case CALENDAR_SUBSCRIPTION_UPDATE:
2927 // Skip updating file subscriptions.
2928 if (empty($sub->url)) {
2929 break;
2931 $sub->pollinterval = $pollinterval;
2932 calendar_update_subscription($sub);
2934 // Update the events.
2935 return "<p>" . get_string('subscriptionupdated', 'calendar', $sub->name) . "</p>" .
2936 calendar_update_subscription_events($subscriptionid);
2937 case CALENDAR_SUBSCRIPTION_REMOVE:
2938 calendar_delete_subscription($subscriptionid);
2939 return get_string('subscriptionremoved', 'calendar', $sub->name);
2940 break;
2941 default:
2942 break;
2944 return '';
2948 * Delete subscription and all related events.
2950 * @param int|stdClass $subscription subscription or it's id, which needs to be deleted.
2952 function calendar_delete_subscription($subscription) {
2953 global $DB;
2955 if (!is_object($subscription)) {
2956 $subscription = $DB->get_record('event_subscriptions', array('id' => $subscription), '*', MUST_EXIST);
2959 // Delete subscription and related events.
2960 $DB->delete_records('event', array('subscriptionid' => $subscription->id));
2961 $DB->delete_records('event_subscriptions', array('id' => $subscription->id));
2962 \cache_helper::invalidate_by_definition('core', 'calendar_subscriptions', array(), array($subscription->id));
2964 // Trigger event, calendar subscription deleted.
2965 $eventparams = array('objectid' => $subscription->id,
2966 'context' => calendar_get_calendar_context($subscription),
2967 'other' => array(
2968 'eventtype' => $subscription->eventtype,
2971 switch ($subscription->eventtype) {
2972 case 'category':
2973 $eventparams['other']['categoryid'] = $subscription->categoryid;
2974 break;
2975 case 'course':
2976 $eventparams['other']['courseid'] = $subscription->courseid;
2977 break;
2978 case 'group':
2979 $eventparams['other']['courseid'] = $subscription->courseid;
2980 $eventparams['other']['groupid'] = $subscription->groupid;
2981 break;
2982 default:
2983 $eventparams['other']['courseid'] = $subscription->courseid;
2985 $event = \core\event\calendar_subscription_deleted::create($eventparams);
2986 $event->trigger();
2990 * From a URL, fetch the calendar and return an iCalendar object.
2992 * @param string $url The iCalendar URL
2993 * @return iCalendar The iCalendar object
2995 function calendar_get_icalendar($url) {
2996 global $CFG;
2998 require_once($CFG->libdir . '/filelib.php');
3000 $curl = new \curl();
3001 $curl->setopt(array('CURLOPT_FOLLOWLOCATION' => 1, 'CURLOPT_MAXREDIRS' => 5));
3002 $calendar = $curl->get($url);
3004 // Http code validation should actually be the job of curl class.
3005 if (!$calendar || $curl->info['http_code'] != 200 || !empty($curl->errorno)) {
3006 throw new \moodle_exception('errorinvalidicalurl', 'calendar');
3009 $ical = new \iCalendar();
3010 $ical->unserialize($calendar);
3012 return $ical;
3016 * Import events from an iCalendar object into a course calendar.
3018 * @param iCalendar $ical The iCalendar object.
3019 * @param int $unused Deprecated
3020 * @param int $subscriptionid The subscription ID.
3021 * @return string A log of the import progress, including errors.
3023 function calendar_import_icalendar_events($ical, $unused = null, $subscriptionid = null) {
3024 global $DB;
3026 $return = '';
3027 $eventcount = 0;
3028 $updatecount = 0;
3029 $skippedcount = 0;
3031 // Large calendars take a while...
3032 if (!CLI_SCRIPT) {
3033 \core_php_time_limit::raise(300);
3036 // Grab the timezone from the iCalendar file to be used later.
3037 if (isset($ical->properties['X-WR-TIMEZONE'][0]->value)) {
3038 $timezone = $ical->properties['X-WR-TIMEZONE'][0]->value;
3039 } else {
3040 $timezone = 'UTC';
3043 $icaluuids = [];
3044 foreach ($ical->components['VEVENT'] as $event) {
3045 $icaluuids[] = $event->properties['UID'][0]->value;
3046 $res = calendar_add_icalendar_event($event, null, $subscriptionid, $timezone);
3047 switch ($res) {
3048 case CALENDAR_IMPORT_EVENT_UPDATED:
3049 $updatecount++;
3050 break;
3051 case CALENDAR_IMPORT_EVENT_INSERTED:
3052 $eventcount++;
3053 break;
3054 case CALENDAR_IMPORT_EVENT_SKIPPED:
3055 $skippedcount++;
3056 break;
3057 case 0:
3058 $return .= '<p>' . get_string('erroraddingevent', 'calendar') . ': ';
3059 if (empty($event->properties['SUMMARY'])) {
3060 $return .= '(' . get_string('notitle', 'calendar') . ')';
3061 } else {
3062 $return .= $event->properties['SUMMARY'][0]->value;
3064 $return .= "</p>\n";
3065 break;
3069 $existing = $DB->get_field('event_subscriptions', 'lastupdated', ['id' => $subscriptionid]);
3070 if (!empty($existing)) {
3071 $eventsuuids = $DB->get_records_menu('event', ['subscriptionid' => $subscriptionid], '', 'id, uuid');
3073 $icaleventscount = count($icaluuids);
3074 $tobedeleted = [];
3075 if (count($eventsuuids) > $icaleventscount) {
3076 foreach ($eventsuuids as $eventid => $eventuuid) {
3077 if (!in_array($eventuuid, $icaluuids)) {
3078 $tobedeleted[] = $eventid;
3081 if (!empty($tobedeleted)) {
3082 $DB->delete_records_list('event', 'id', $tobedeleted);
3083 $return .= "<p> " . get_string('eventsdeleted', 'calendar') . ": " . count($tobedeleted) . "</p> ";
3088 $return .= "<p>" . get_string('eventsimported', 'calendar', $eventcount) . "</p> ";
3089 $return .= "<p>" . get_string('eventsskipped', 'calendar', $skippedcount) . "</p> ";
3090 $return .= "<p>" . get_string('eventsupdated', 'calendar', $updatecount) . "</p>";
3091 return $return;
3095 * Fetch a calendar subscription and update the events in the calendar.
3097 * @param int $subscriptionid The course ID for the calendar.
3098 * @return string A log of the import progress, including errors.
3100 function calendar_update_subscription_events($subscriptionid) {
3101 $sub = calendar_get_subscription($subscriptionid);
3103 // Don't update a file subscription.
3104 if (empty($sub->url)) {
3105 return 'File subscription not updated.';
3108 $ical = calendar_get_icalendar($sub->url);
3109 $return = calendar_import_icalendar_events($ical, null, $subscriptionid);
3110 $sub->lastupdated = time();
3112 calendar_update_subscription($sub);
3114 return $return;
3118 * Update a calendar subscription. Also updates the associated cache.
3120 * @param stdClass|array $subscription Subscription record.
3121 * @throws coding_exception If something goes wrong
3122 * @since Moodle 2.5
3124 function calendar_update_subscription($subscription) {
3125 global $DB;
3127 if (is_array($subscription)) {
3128 $subscription = (object)$subscription;
3130 if (empty($subscription->id) || !$DB->record_exists('event_subscriptions', array('id' => $subscription->id))) {
3131 throw new \coding_exception('Cannot update a subscription without a valid id');
3134 $DB->update_record('event_subscriptions', $subscription);
3136 // Update cache.
3137 $cache = \cache::make('core', 'calendar_subscriptions');
3138 $cache->set($subscription->id, $subscription);
3140 // Trigger event, calendar subscription updated.
3141 $eventparams = array('userid' => $subscription->userid,
3142 'objectid' => $subscription->id,
3143 'context' => calendar_get_calendar_context($subscription),
3144 'other' => array(
3145 'eventtype' => $subscription->eventtype,
3148 switch ($subscription->eventtype) {
3149 case 'category':
3150 $eventparams['other']['categoryid'] = $subscription->categoryid;
3151 break;
3152 case 'course':
3153 $eventparams['other']['courseid'] = $subscription->courseid;
3154 break;
3155 case 'group':
3156 $eventparams['other']['courseid'] = $subscription->courseid;
3157 $eventparams['other']['groupid'] = $subscription->groupid;
3158 break;
3159 default:
3160 $eventparams['other']['courseid'] = $subscription->courseid;
3162 $event = \core\event\calendar_subscription_updated::create($eventparams);
3163 $event->trigger();
3167 * Checks to see if the user can edit a given subscription feed.
3169 * @param mixed $subscriptionorid Subscription object or id
3170 * @return bool true if current user can edit the subscription else false
3172 function calendar_can_edit_subscription($subscriptionorid) {
3173 if (is_array($subscriptionorid)) {
3174 $subscription = (object)$subscriptionorid;
3175 } else if (is_object($subscriptionorid)) {
3176 $subscription = $subscriptionorid;
3177 } else {
3178 $subscription = calendar_get_subscription($subscriptionorid);
3181 $allowed = new \stdClass;
3182 $courseid = $subscription->courseid;
3183 $categoryid = $subscription->categoryid;
3184 $groupid = $subscription->groupid;
3185 $category = null;
3187 if (!empty($categoryid)) {
3188 $category = \core_course_category::get($categoryid);
3190 calendar_get_allowed_types($allowed, $courseid, null, $category);
3191 switch ($subscription->eventtype) {
3192 case 'user':
3193 return $allowed->user;
3194 case 'course':
3195 if (isset($allowed->courses[$courseid])) {
3196 return $allowed->courses[$courseid];
3197 } else {
3198 return false;
3200 case 'category':
3201 if (isset($allowed->categories[$categoryid])) {
3202 return $allowed->categories[$categoryid];
3203 } else {
3204 return false;
3206 case 'site':
3207 return $allowed->site;
3208 case 'group':
3209 if (isset($allowed->groups[$groupid])) {
3210 return $allowed->groups[$groupid];
3211 } else {
3212 return false;
3214 default:
3215 return false;
3220 * Helper function to determine the context of a calendar subscription.
3221 * Subscriptions can be created in two contexts COURSE, or USER.
3223 * @param stdClass $subscription
3224 * @return context instance
3226 function calendar_get_calendar_context($subscription) {
3227 // Determine context based on calendar type.
3228 if ($subscription->eventtype === 'site') {
3229 $context = \context_course::instance(SITEID);
3230 } else if ($subscription->eventtype === 'group' || $subscription->eventtype === 'course') {
3231 $context = \context_course::instance($subscription->courseid);
3232 } else {
3233 $context = \context_user::instance($subscription->userid);
3235 return $context;
3239 * Implements callback user_preferences, whitelists preferences that users are allowed to update directly
3241 * Used in {@see core_user::fill_preferences_cache()}, see also {@see useredit_update_user_preference()}
3243 * @return array
3245 function core_calendar_user_preferences() {
3246 $preferences = [];
3247 $preferences['calendar_timeformat'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED, 'default' => '0',
3248 'choices' => array('0', CALENDAR_TF_12, CALENDAR_TF_24)
3250 $preferences['calendar_startwday'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 0,
3251 'choices' => array(0, 1, 2, 3, 4, 5, 6));
3252 $preferences['calendar_maxevents'] = array('type' => PARAM_INT, 'choices' => range(1, 20));
3253 $preferences['calendar_lookahead'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 365,
3254 'choices' => array(365, 270, 180, 150, 120, 90, 60, 30, 21, 14, 7, 6, 5, 4, 3, 2, 1));
3255 $preferences['calendar_persistflt'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 0,
3256 'choices' => array(0, 1));
3257 return $preferences;
3261 * Get legacy calendar events
3263 * @param int $tstart Start time of time range for events
3264 * @param int $tend End time of time range for events
3265 * @param array|int|boolean $users array of users, user id or boolean for all/no user events
3266 * @param array|int|boolean $groups array of groups, group id or boolean for all/no group events
3267 * @param array|int|boolean $courses array of courses, course id or boolean for all/no course events
3268 * @param boolean $withduration whether only events starting within time range selected
3269 * or events in progress/already started selected as well
3270 * @param boolean $ignorehidden whether to select only visible events or all events
3271 * @param array $categories array of category ids and/or objects.
3272 * @param int $limitnum Number of events to fetch or zero to fetch all.
3274 * @return array $events of selected events or an empty array if there aren't any (or there was an error)
3276 function calendar_get_legacy_events($tstart, $tend, $users, $groups, $courses,
3277 $withduration = true, $ignorehidden = true, $categories = [], $limitnum = 0) {
3278 // Normalise the users, groups and courses parameters so that they are compliant with \core_calendar\local\api::get_events().
3279 // Existing functions that were using the old calendar_get_events() were passing a mixture of array, int, boolean for these
3280 // parameters, but with the new API method, only null and arrays are accepted.
3281 list($userparam, $groupparam, $courseparam, $categoryparam) = array_map(function($param) {
3282 // If parameter is true, return null.
3283 if ($param === true) {
3284 return null;
3287 // If parameter is false, return an empty array.
3288 if ($param === false) {
3289 return [];
3292 // If the parameter is a scalar value, enclose it in an array.
3293 if (!is_array($param)) {
3294 return [$param];
3297 // No normalisation required.
3298 return $param;
3299 }, [$users, $groups, $courses, $categories]);
3301 // If a single user is provided, we can use that for capability checks.
3302 // Otherwise current logged in user is used - See MDL-58768.
3303 if (is_array($userparam) && count($userparam) == 1) {
3304 \core_calendar\local\event\container::set_requesting_user($userparam[0]);
3306 $mapper = \core_calendar\local\event\container::get_event_mapper();
3307 $events = \core_calendar\local\api::get_events(
3308 $tstart,
3309 $tend,
3310 null,
3311 null,
3312 null,
3313 null,
3314 $limitnum,
3315 null,
3316 $userparam,
3317 $groupparam,
3318 $courseparam,
3319 $categoryparam,
3320 $withduration,
3321 $ignorehidden
3324 return array_reduce($events, function($carry, $event) use ($mapper) {
3325 return $carry + [$event->get_id() => $mapper->from_event_to_stdclass($event)];
3326 }, []);
3331 * Get the calendar view output.
3333 * @param \calendar_information $calendar The calendar being represented
3334 * @param string $view The type of calendar to have displayed
3335 * @param bool $includenavigation Whether to include navigation
3336 * @param bool $skipevents Whether to load the events or not
3337 * @param int $lookahead Overwrites site and users's lookahead setting.
3338 * @return array[array, string]
3340 function calendar_get_view(\calendar_information $calendar, $view, $includenavigation = true, bool $skipevents = false,
3341 ?int $lookahead = null) {
3342 global $PAGE, $CFG;
3344 $renderer = $PAGE->get_renderer('core_calendar');
3345 $type = \core_calendar\type_factory::get_calendar_instance();
3347 // Calculate the bounds of the month.
3348 $calendardate = $type->timestamp_to_date_array($calendar->time);
3350 $date = new \DateTime('now', core_date::get_user_timezone_object(99));
3351 $eventlimit = 0;
3353 if ($view === 'day') {
3354 $tstart = $type->convert_to_timestamp($calendardate['year'], $calendardate['mon'], $calendardate['mday']);
3355 $date->setTimestamp($tstart);
3356 $date->modify('+1 day');
3357 } else if ($view === 'upcoming' || $view === 'upcoming_mini') {
3358 // Number of days in the future that will be used to fetch events.
3359 if (!$lookahead) {
3360 if (isset($CFG->calendar_lookahead)) {
3361 $defaultlookahead = intval($CFG->calendar_lookahead);
3362 } else {
3363 $defaultlookahead = CALENDAR_DEFAULT_UPCOMING_LOOKAHEAD;
3365 $lookahead = get_user_preferences('calendar_lookahead', $defaultlookahead);
3368 // Maximum number of events to be displayed on upcoming view.
3369 $defaultmaxevents = CALENDAR_DEFAULT_UPCOMING_MAXEVENTS;
3370 if (isset($CFG->calendar_maxevents)) {
3371 $defaultmaxevents = intval($CFG->calendar_maxevents);
3373 $eventlimit = get_user_preferences('calendar_maxevents', $defaultmaxevents);
3375 $tstart = $type->convert_to_timestamp($calendardate['year'], $calendardate['mon'], $calendardate['mday'],
3376 $calendardate['hours']);
3377 $date->setTimestamp($tstart);
3378 $date->modify('+' . $lookahead . ' days');
3379 } else {
3380 $tstart = $type->convert_to_timestamp($calendardate['year'], $calendardate['mon'], 1);
3381 $monthdays = $type->get_num_days_in_month($calendardate['year'], $calendardate['mon']);
3382 $date->setTimestamp($tstart);
3383 $date->modify('+' . $monthdays . ' days');
3385 if ($view === 'mini' || $view === 'minithree') {
3386 $template = 'core_calendar/calendar_mini';
3387 } else {
3388 $template = 'core_calendar/calendar_month';
3392 // We need to extract 1 second to ensure that we don't get into the next day.
3393 $date->modify('-1 second');
3394 $tend = $date->getTimestamp();
3396 list($userparam, $groupparam, $courseparam, $categoryparam) = array_map(function($param) {
3397 // If parameter is true, return null.
3398 if ($param === true) {
3399 return null;
3402 // If parameter is false, return an empty array.
3403 if ($param === false) {
3404 return [];
3407 // If the parameter is a scalar value, enclose it in an array.
3408 if (!is_array($param)) {
3409 return [$param];
3412 // No normalisation required.
3413 return $param;
3414 }, [$calendar->users, $calendar->groups, $calendar->courses, $calendar->categories]);
3416 if ($skipevents) {
3417 $events = [];
3418 } else {
3419 $events = \core_calendar\local\api::get_events(
3420 $tstart,
3421 $tend,
3422 null,
3423 null,
3424 null,
3425 null,
3426 $eventlimit,
3427 null,
3428 $userparam,
3429 $groupparam,
3430 $courseparam,
3431 $categoryparam,
3432 true,
3433 true,
3434 function ($event) {
3435 if ($proxy = $event->get_course_module()) {
3436 $cminfo = $proxy->get_proxied_instance();
3437 return $cminfo->uservisible;
3440 if ($proxy = $event->get_category()) {
3441 $category = $proxy->get_proxied_instance();
3443 return $category->is_uservisible();
3446 return true;
3451 $related = [
3452 'events' => $events,
3453 'cache' => new \core_calendar\external\events_related_objects_cache($events),
3454 'type' => $type,
3457 $data = [];
3458 if ($view == "month" || $view == "mini" || $view == "minithree") {
3459 $month = new \core_calendar\external\month_exporter($calendar, $type, $related);
3460 $month->set_includenavigation($includenavigation);
3461 $month->set_initialeventsloaded(!$skipevents);
3462 $month->set_showcoursefilter($view == "month");
3463 $data = $month->export($renderer);
3464 $data->viewingmonth = true;
3465 } else if ($view == "day") {
3466 $day = new \core_calendar\external\calendar_day_exporter($calendar, $related);
3467 $data = $day->export($renderer);
3468 $data->viewingday = true;
3469 $template = 'core_calendar/calendar_day';
3470 } else if ($view == "upcoming" || $view == "upcoming_mini") {
3471 $upcoming = new \core_calendar\external\calendar_upcoming_exporter($calendar, $related);
3472 $data = $upcoming->export($renderer);
3474 if ($view == "upcoming") {
3475 $template = 'core_calendar/calendar_upcoming';
3476 $data->viewingupcoming = true;
3477 } else if ($view == "upcoming_mini") {
3478 $template = 'core_calendar/calendar_upcoming_mini';
3482 return [$data, $template];
3486 * Request and render event form fragment.
3488 * @param array $args The fragment arguments.
3489 * @return string The rendered mform fragment.
3491 function calendar_output_fragment_event_form($args) {
3492 global $CFG, $OUTPUT, $USER;
3493 require_once($CFG->libdir . '/grouplib.php');
3494 $html = '';
3495 $data = [];
3496 $eventid = isset($args['eventid']) ? clean_param($args['eventid'], PARAM_INT) : null;
3497 $starttime = isset($args['starttime']) ? clean_param($args['starttime'], PARAM_INT) : null;
3498 $courseid = (isset($args['courseid']) && $args['courseid'] != SITEID) ? clean_param($args['courseid'], PARAM_INT) : null;
3499 $categoryid = isset($args['categoryid']) ? clean_param($args['categoryid'], PARAM_INT) : null;
3500 $event = null;
3501 $hasformdata = isset($args['formdata']) && !empty($args['formdata']);
3502 $context = \context_user::instance($USER->id);
3503 $editoroptions = \core_calendar\local\event\forms\create::build_editor_options($context);
3504 $formoptions = ['editoroptions' => $editoroptions, 'courseid' => $courseid];
3505 $draftitemid = 0;
3507 if ($hasformdata) {
3508 parse_str(clean_param($args['formdata'], PARAM_TEXT), $data);
3509 if (isset($data['description']['itemid'])) {
3510 $draftitemid = $data['description']['itemid'];
3514 if ($starttime) {
3515 $formoptions['starttime'] = $starttime;
3517 // Let's check first which event types user can add.
3518 $eventtypes = calendar_get_allowed_event_types($courseid);
3519 $formoptions['eventtypes'] = $eventtypes;
3521 if (is_null($eventid)) {
3522 if (!empty($courseid)) {
3523 $groupcoursedata = groups_get_course_data($courseid);
3524 $formoptions['groups'] = [];
3525 foreach ($groupcoursedata->groups as $groupid => $groupdata) {
3526 $formoptions['groups'][$groupid] = $groupdata->name;
3530 $mform = new \core_calendar\local\event\forms\create(
3531 null,
3532 $formoptions,
3533 'post',
3535 null,
3536 true,
3537 $data
3540 // If the user is on course context and is allowed to add course events set the event type default to course.
3541 if (!empty($courseid) && !empty($eventtypes['course'])) {
3542 $data['eventtype'] = 'course';
3543 $data['courseid'] = $courseid;
3544 $data['groupcourseid'] = $courseid;
3545 } else if (!empty($categoryid) && !empty($eventtypes['category'])) {
3546 $data['eventtype'] = 'category';
3547 $data['categoryid'] = $categoryid;
3548 } else if (!empty($groupcoursedata) && !empty($eventtypes['group'])) {
3549 $data['groupcourseid'] = $courseid;
3550 $data['groups'] = $groupcoursedata->groups;
3552 $mform->set_data($data);
3553 } else {
3554 $event = calendar_event::load($eventid);
3556 if (!calendar_edit_event_allowed($event)) {
3557 print_error('nopermissiontoupdatecalendar');
3560 $mapper = new \core_calendar\local\event\mappers\create_update_form_mapper();
3561 $eventdata = $mapper->from_legacy_event_to_data($event);
3562 $data = array_merge((array) $eventdata, $data);
3563 $event->count_repeats();
3564 $formoptions['event'] = $event;
3566 if (!empty($event->courseid)) {
3567 $groupcoursedata = groups_get_course_data($event->courseid);
3568 $formoptions['groups'] = [];
3569 foreach ($groupcoursedata->groups as $groupid => $groupdata) {
3570 $formoptions['groups'][$groupid] = $groupdata->name;
3574 $data['description']['text'] = file_prepare_draft_area(
3575 $draftitemid,
3576 $event->context->id,
3577 'calendar',
3578 'event_description',
3579 $event->id,
3580 null,
3581 $data['description']['text']
3583 $data['description']['itemid'] = $draftitemid;
3585 $mform = new \core_calendar\local\event\forms\update(
3586 null,
3587 $formoptions,
3588 'post',
3590 null,
3591 true,
3592 $data
3594 $mform->set_data($data);
3596 // Check to see if this event is part of a subscription or import.
3597 // If so display a warning on edit.
3598 if (isset($event->subscriptionid) && ($event->subscriptionid != null)) {
3599 $renderable = new \core\output\notification(
3600 get_string('eventsubscriptioneditwarning', 'calendar'),
3601 \core\output\notification::NOTIFY_INFO
3604 $html .= $OUTPUT->render($renderable);
3608 if ($hasformdata) {
3609 $mform->is_validated();
3612 $html .= $mform->render();
3613 return $html;
3617 * Calculate the timestamp from the supplied Gregorian Year, Month, and Day.
3619 * @param int $d The day
3620 * @param int $m The month
3621 * @param int $y The year
3622 * @param int $time The timestamp to use instead of a separate y/m/d.
3623 * @return int The timestamp
3625 function calendar_get_timestamp($d, $m, $y, $time = 0) {
3626 // If a day, month and year were passed then convert it to a timestamp. If these were passed
3627 // then we can assume the day, month and year are passed as Gregorian, as no where in core
3628 // should we be passing these values rather than the time.
3629 if (!empty($d) && !empty($m) && !empty($y)) {
3630 if (checkdate($m, $d, $y)) {
3631 $time = make_timestamp($y, $m, $d);
3632 } else {
3633 $time = time();
3635 } else if (empty($time)) {
3636 $time = time();
3639 return $time;
3643 * Get the calendar footer options.
3645 * @param calendar_information $calendar The calendar information object.
3646 * @return array The data for template and template name.
3648 function calendar_get_footer_options($calendar) {
3649 global $CFG, $USER, $DB, $PAGE;
3651 // Generate hash for iCal link.
3652 $rawhash = $USER->id . $DB->get_field('user', 'password', ['id' => $USER->id]) . $CFG->calendar_exportsalt;
3653 $authtoken = sha1($rawhash);
3655 $renderer = $PAGE->get_renderer('core_calendar');
3656 $footer = new \core_calendar\external\footer_options_exporter($calendar, $USER->id, $authtoken);
3657 $data = $footer->export($renderer);
3658 $template = 'core_calendar/footer_options';
3660 return [$data, $template];
3664 * Get the list of potential calendar filter types as a type => name
3665 * combination.
3667 * @return array
3669 function calendar_get_filter_types() {
3670 $types = [
3671 'site',
3672 'category',
3673 'course',
3674 'group',
3675 'user',
3678 return array_map(function($type) {
3679 return [
3680 'eventtype' => $type,
3681 'name' => get_string("eventtype{$type}", "calendar"),
3682 'icon' => true,
3683 'key' => 'i/' . $type . 'event',
3684 'component' => 'core'
3686 }, $types);
3690 * Check whether the specified event type is valid.
3692 * @param string $type
3693 * @return bool
3695 function calendar_is_valid_eventtype($type) {
3696 $validtypes = [
3697 'user',
3698 'group',
3699 'course',
3700 'category',
3701 'site',
3703 return in_array($type, $validtypes);
3707 * Get event types the user can create event based on categories, courses and groups
3708 * the logged in user belongs to.
3710 * @param int|null $courseid The course id.
3711 * @return array The array of allowed types.
3713 function calendar_get_allowed_event_types(int $courseid = null) {
3714 global $DB, $CFG, $USER;
3716 $types = [
3717 'user' => false,
3718 'site' => false,
3719 'course' => false,
3720 'group' => false,
3721 'category' => false
3724 if (!empty($courseid) && $courseid != SITEID) {
3725 $context = \context_course::instance($courseid);
3726 $types['user'] = has_capability('moodle/calendar:manageownentries', $context);
3727 calendar_internal_update_course_and_group_permission($courseid, $context, $types);
3730 if (has_capability('moodle/calendar:manageentries', \context_course::instance(SITEID))) {
3731 $types['site'] = true;
3734 if (has_capability('moodle/calendar:manageownentries', \context_system::instance())) {
3735 $types['user'] = true;
3737 if (core_course_category::has_manage_capability_on_any()) {
3738 $types['category'] = true;
3741 // We still don't know if the user can create group and course events, so iterate over the courses to find out
3742 // if the user has capabilities in one of the courses.
3743 if ($types['course'] == false || $types['group'] == false) {
3744 if ($CFG->calendar_adminseesall && has_capability('moodle/calendar:manageentries', context_system::instance())) {
3745 $sql = "SELECT c.id, " . context_helper::get_preload_record_columns_sql('ctx') . "
3746 FROM {course} c
3747 JOIN {context} ctx ON ctx.contextlevel = ? AND ctx.instanceid = c.id
3748 WHERE c.id IN (
3749 SELECT DISTINCT courseid FROM {groups}
3751 $courseswithgroups = $DB->get_recordset_sql($sql, [CONTEXT_COURSE]);
3752 foreach ($courseswithgroups as $course) {
3753 context_helper::preload_from_record($course);
3754 $context = context_course::instance($course->id);
3756 if (has_capability('moodle/calendar:manageentries', $context)) {
3757 if (has_any_capability(['moodle/site:accessallgroups', 'moodle/calendar:managegroupentries'], $context)) {
3758 // The user can manage group entries or access any group.
3759 $types['group'] = true;
3760 $types['course'] = true;
3761 break;
3765 $courseswithgroups->close();
3767 if (false === $types['course']) {
3768 // Course is still not confirmed. There may have been no courses with a group in them.
3769 $ctxfields = context_helper::get_preload_record_columns_sql('ctx');
3770 $sql = "SELECT
3771 c.id, c.visible, {$ctxfields}
3772 FROM {course} c
3773 JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
3774 $params = [
3775 'contextlevel' => CONTEXT_COURSE,
3777 $courses = $DB->get_recordset_sql($sql, $params);
3778 foreach ($courses as $course) {
3779 context_helper::preload_from_record($course);
3780 $context = context_course::instance($course->id);
3781 if (has_capability('moodle/calendar:manageentries', $context)) {
3782 $types['course'] = true;
3783 break;
3786 $courses->close();
3789 } else {
3790 $courses = calendar_get_default_courses(null, 'id');
3791 if (empty($courses)) {
3792 return $types;
3795 $courseids = array_map(function($c) {
3796 return $c->id;
3797 }, $courses);
3799 // Check whether the user has access to create events within courses which have groups.
3800 list($insql, $params) = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED);
3801 $sql = "SELECT c.id, " . context_helper::get_preload_record_columns_sql('ctx') . "
3802 FROM {course} c
3803 JOIN {context} ctx ON ctx.contextlevel = :contextlevel AND ctx.instanceid = c.id
3804 WHERE c.id $insql
3805 AND c.id IN (SELECT DISTINCT courseid FROM {groups})";
3806 $params['contextlevel'] = CONTEXT_COURSE;
3807 $courseswithgroups = $DB->get_recordset_sql($sql, $params);
3808 foreach ($courseswithgroups as $coursewithgroup) {
3809 context_helper::preload_from_record($coursewithgroup);
3810 $context = context_course::instance($coursewithgroup->id);
3812 calendar_internal_update_course_and_group_permission($coursewithgroup->id, $context, $types);
3814 // Okay, course and group event types are allowed, no need to keep the loop iteration.
3815 if ($types['course'] == true && $types['group'] == true) {
3816 break;
3819 $courseswithgroups->close();
3821 if (false === $types['course']) {
3822 list($insql, $params) = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED);
3823 $contextsql = "SELECT c.id, " . context_helper::get_preload_record_columns_sql('ctx') . "
3824 FROM {course} c
3825 JOIN {context} ctx ON ctx.contextlevel = :contextlevel AND ctx.instanceid = c.id
3826 WHERE c.id $insql";
3827 $params['contextlevel'] = CONTEXT_COURSE;
3828 $contextrecords = $DB->get_recordset_sql($contextsql, $params);
3829 foreach ($contextrecords as $course) {
3830 context_helper::preload_from_record($course);
3831 $coursecontext = context_course::instance($course->id);
3832 if (has_capability('moodle/calendar:manageentries', $coursecontext)
3833 && ($courseid == $course->id || empty($courseid))) {
3834 $types['course'] = true;
3835 break;
3838 $contextrecords->close();
3844 return $types;
3848 * Given a course id, and context, updates the permission types array to add the 'course' or 'group'
3849 * permission if it is relevant for that course.
3851 * For efficiency, if they already have 'course' or 'group' then it skips checks.
3853 * Do not call this function directly, it is only for use by calendar_get_allowed_event_types().
3855 * @param int $courseid Course id
3856 * @param context $context Context for that course
3857 * @param array $types Current permissions
3859 function calendar_internal_update_course_and_group_permission(int $courseid, context $context, array &$types) {
3860 if (!$types['course']) {
3861 // If they have manageentries permission on the course, then they can update this course.
3862 if (has_capability('moodle/calendar:manageentries', $context)) {
3863 $types['course'] = true;
3866 // To update group events they must have EITHER manageentries OR managegroupentries.
3867 if (!$types['group'] && (has_capability('moodle/calendar:manageentries', $context) ||
3868 has_capability('moodle/calendar:managegroupentries', $context))) {
3869 // And they also need for a group to exist on the course.
3870 $groups = groups_get_all_groups($courseid);
3871 if (!empty($groups)) {
3872 // And either accessallgroups, or belong to one of the groups.
3873 if (has_capability('moodle/site:accessallgroups', $context)) {
3874 $types['group'] = true;
3875 } else {
3876 foreach ($groups as $group) {
3877 if (groups_is_member($group->id)) {
3878 $types['group'] = true;
3879 break;