From c4829d57e2c83e95e4fc7d0d145387e7cd2105b2 Mon Sep 17 00:00:00 2001 From: Stephen Bourget Date: Thu, 25 Sep 2014 09:42:10 -0400 Subject: [PATCH] MDL-46084 Calendar: Fix importing of full day events from Google --- calendar/lib.php | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) mode change 100644 => 100755 calendar/lib.php diff --git a/calendar/lib.php b/calendar/lib.php old mode 100644 new mode 100755 index 8bb2a63ffb4..676b26dc83f --- a/calendar/lib.php +++ b/calendar/lib.php @@ -2888,10 +2888,11 @@ function calendar_add_subscription($sub) { * @param stdClass $event The RFC-2445 iCalendar event * @param int $courseid The course ID * @param int $subscriptionid The iCalendar subscription ID + * @param string $timezone The X-WR-TIMEZONE iCalendar property if provided * @throws dml_exception A DML specific exception is thrown for invalid subscriptionids. * @return int Code: CALENDAR_IMPORT_EVENT_UPDATED = updated, CALENDAR_IMPORT_EVENT_INSERTED = inserted, 0 = error */ -function calendar_add_icalendar_event($event, $courseid, $subscriptionid) { +function calendar_add_icalendar_event($event, $courseid, $subscriptionid, $timezone='UTC') { global $DB; // Probably an unsupported X-MICROSOFT-CDO-BUSYSTATUS event. @@ -2924,15 +2925,27 @@ function calendar_add_icalendar_event($event, $courseid, $subscriptionid) { $defaulttz = date_default_timezone_get(); $tz = isset($event->properties['DTSTART'][0]->parameters['TZID']) ? $event->properties['DTSTART'][0]->parameters['TZID'] : - 'UTC'; + $timezone; $eventrecord->timestart = strtotime($event->properties['DTSTART'][0]->value . ' ' . $tz); if (empty($event->properties['DTEND'])) { $eventrecord->timeduration = 3600; // one hour if no end time specified } else { $endtz = isset($event->properties['DTEND'][0]->parameters['TZID']) ? $event->properties['DTEND'][0]->parameters['TZID'] : - 'UTC'; + $timezone; $eventrecord->timeduration = strtotime($event->properties['DTEND'][0]->value . ' ' . $endtz) - $eventrecord->timestart; } + + // Check to see if it should be treated as an all day event. + if ($eventrecord->timeduration == DAYSECS) { + // Check to see if the event started at Midnight on the imported calendar. + date_default_timezone_set($timezone); + if (date('H:i:s', $eventrecord->timestart) === "00:00:00") { + // This event should be an all day event. + $eventrecord->timeduration = 0; + } + date_default_timezone_set($defaulttz); + } + $eventrecord->uuid = $event->properties['UID'][0]->value; $eventrecord->timemodified = time(); @@ -3066,8 +3079,14 @@ function calendar_import_icalendar_events($ical, $courseid, $subscriptionid = nu $sql = "UPDATE {event} SET timemodified = :time WHERE subscriptionid = :id"; $DB->execute($sql, array('time' => 0, 'id' => $subscriptionid)); } + // Grab the timezone from the iCalendar file to be used later. + if (isset($ical->properties['X-WR-TIMEZONE'][0]->value)) { + $timezone = $ical->properties['X-WR-TIMEZONE'][0]->value; + } else { + $timezone = 'UTC'; + } foreach ($ical->components['VEVENT'] as $event) { - $res = calendar_add_icalendar_event($event, $courseid, $subscriptionid); + $res = calendar_add_icalendar_event($event, $courseid, $subscriptionid, $timezone); switch ($res) { case CALENDAR_IMPORT_EVENT_UPDATED: $updatecount++;