<?php require_once __DIR__ . '/.ignore.calendar-ics-authentication.inc.php'; require_once __DIR__ . '/config.inc.php'; require_once APP_PATH . '/include/page-generator.inc.php'; require_once APP_PATH . '/include/canvas-api.inc.php'; if (isset($_REQUEST['course_url'])) { $courseId = preg_replace('|.*/courses/(\\d+)/?.*|', '$1', parse_url($_REQUEST['course_url'], PHP_URL_PATH)); $course = callCanvasApi('get', "/courses/{$courseId}"); if ($course) { $webcalFeed = str_replace('https://', 'webcal://', $course['calendar']['ics']); displayPage(' <h3>Course Calendar ICS Feed</h3> <p>You can subscribe to the calendar for <a href="https://' . parse_url(CANVAS_API_URL, PHP_URL_HOST) . '/courses/' . $courseId . '">' . $course['name'] . '</a> at <a href="' . $webcalFeed . '">' . $webcalFeed . '</a> in any calendar application that supports external ICS feeds.</p>'); exit; } else { displayError($json, false, 'Canvas API Error', 'The course you requested could not be accessed.'); exit; } } else { displayPage(' <form method="post" action="' . $_SERVER['PHP_SELF'] . '"> <label for="course_url">Course URL <span class="comment">The URL to the course whose calendar you would like to export as an ICS feed</span></label> <input id="course_url" name="course_url" type="text" /> <input type="submit" value="Generate ICS Feed" /> </form> '); }
$end->setDate(substr($date, 0, 4), substr($date, 4, 2), substr($date, 6, 2)); $calendarEvent = callCanvasApi(CANVAS_API_POST, "/calendar_events", array('calendar_event[context_code]' => "{$canvasContext['context']}_{$canvasObject['id']}", 'calendar_event[title]' => $event['event_text'], 'calendar_event[description]' => $event['description'], 'calendar_event[start_at]' => $start->format(CANVAS_TIMESTAMP_FORMAT), 'calendar_event[end_at]' => $end->format(CANVAS_TIMESTAMP_FORMAT), 'calendar_event[location_name]' => $event['location'])); $icalEventJson = json_encode($event); $calendarEventJson = json_encode($calendarEvent); mysqlQuery("\n\t\t\t\t\t\t\t\t\t\t\tINSERT INTO `events`\n\t\t\t\t\t\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\t\t\t\t\t\t`calendar`,\n\t\t\t\t\t\t\t\t\t\t\t\t\t`calendar_event[id]`,\n\t\t\t\t\t\t\t\t\t\t\t\t\t`event_hash`,\n\t\t\t\t\t\t\t\t\t\t\t\t\t`synced`\n\t\t\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t\t\t\tVALUES (\n\t\t\t\t\t\t\t\t\t\t\t\t\t'{$calendarCache['id']}',\n\t\t\t\t\t\t\t\t\t\t\t\t\t'{$calendarEvent['id']}',\n\t\t\t\t\t\t\t\t\t\t\t\t\t'{$eventHash}',\n\t\t\t\t\t\t\t\t\t\t\t\t\t'" . getSyncTimestamp() . "'\n\t\t\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t\t"); } } } } } } /* clean out previously synced events that are no longer correct */ $deletedEventsResponse = mysqlQuery("\n\t\t\t\t\tSELECT * FROM `events`\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t`calendar` = '{$calendarCache['id']}' AND\n\t\t\t\t\t\t\t`synced` != '" . getSyncTimestamp() . "'\n\t\t\t\t"); while ($deletedEventCache = $deletedEventsResponse->fetch_assoc()) { try { $deletedEvent = callCanvasApi(CANVAS_API_DELETE, "/calendar_events/{$deletedEventCache['calendar_event[id]']}", array('cancel_reason' => getSyncTimestamp(), 'as_user_id' => $canvasContext['context'] == 'user' ? $canvasObject['id'] : ''), CANVAS_API_EXCEPTION_CLIENT); } catch (Pest_Unauthorized $e) { /* if the event has been deleted in Canvas, we'll get an error when we try to delete it a second time. We still need to delete it from our cache database, however */ debugFlag("Cache out-of-sync: calendar_event[{$deletedEventCache['calendar_event[id]']}] no longer exists and will be purged from cache."); } catch (Pest_ClientError $e) { displayError(array('Status' => $PEST->lastStatus(), 'Error' => $PEST->lastBody(), 'Verb' => $verb, 'URL' => $url, 'Data' => $data), true, 'API Client Error'); exit; } mysqlQuery("\n\t\t\t\t\t\tDELETE FROM `events`\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t`id` = '{$deletedEventCache['id']}'\n\t\t\t\t\t"); } /* if this was a scheduled import (i.e. a sync), update that schedule */ if (isset($_REQUEST['schedule'])) { mysqlQuery("\n\t\t\t\t\t\tUPDATE `schedules`\n\t\t\t\t\t\t\tSET\n\t\t\t\t\t\t\t\t`synced` = '" . getSyncTimestamp() . "'\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t`id` = '{$_REQUEST['schedule']}'\n\t\t\t\t\t"); }