* @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.
$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();
$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++;