@@ -13,17 +13,17 @@ |
||
| 13 | 13 | |
| 14 | 14 | /* prepare the toolbox */ |
| 15 | 15 | if (empty($_SESSION[Toolbox::class])) { |
| 16 | - $_SESSION[Toolbox::class] =& Toolbox::fromConfiguration(CONFIG_FILE); |
|
| 16 | + $_SESSION[Toolbox::class] =& Toolbox::fromConfiguration(CONFIG_FILE); |
|
| 17 | 17 | } |
| 18 | 18 | $toolbox =& $_SESSION[Toolbox::class]; |
| 19 | 19 | $toolbox->smarty_prependTemplateDir(__DIR__ . '/templates', basename(__DIR__)); |
| 20 | 20 | $toolbox->smarty_assign([ |
| 21 | - 'category' => DataUtilities::titleCase(preg_replace('/[\-_]+/', ' ', basename(__DIR__))) |
|
| 21 | + 'category' => DataUtilities::titleCase(preg_replace('/[\-_]+/', ' ', basename(__DIR__))) |
|
| 22 | 22 | ]); |
| 23 | 23 | |
| 24 | 24 | /* set the Tool Consumer's instance URL, if present */ |
| 25 | 25 | if (empty($_SESSION[CANVAS_INSTANCE_URL]) && |
| 26 | - !empty($_SESSION[ToolProvider::class]['canvas']['api_domain']) |
|
| 26 | + !empty($_SESSION[ToolProvider::class]['canvas']['api_domain']) |
|
| 27 | 27 | ) { |
| 28 | - $_SESSION[CANVAS_INSTANCE_URL] = 'https://' . $_SESSION[ToolProvider::class]['canvas']['api_domain']; |
|
| 28 | + $_SESSION[CANVAS_INSTANCE_URL] = 'https://' . $_SESSION[ToolProvider::class]['canvas']['api_domain']; |
|
| 29 | 29 | } |
@@ -13,74 +13,74 @@ |
||
| 13 | 13 | |
| 14 | 14 | /* store any requested actions for future handling */ |
| 15 | 15 | $action = (empty($_REQUEST['action']) ? |
| 16 | - ACTION_UNSPECIFIED : |
|
| 17 | - strtolower($_REQUEST['action']) |
|
| 16 | + ACTION_UNSPECIFIED : |
|
| 17 | + strtolower($_REQUEST['action']) |
|
| 18 | 18 | ); |
| 19 | 19 | |
| 20 | 20 | /* action requests only come from outside the LTI! */ |
| 21 | 21 | if ($action) { |
| 22 | - unset($_SESSION[ToolProvider::class]); |
|
| 22 | + unset($_SESSION[ToolProvider::class]); |
|
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | /* authenticate LTI launch request, if present */ |
| 26 | 26 | if ($toolbox->lti_isLaunching()) { |
| 27 | - $toolbox->resetSession(); |
|
| 28 | - $toolbox->lti_authenticate(); |
|
| 29 | - exit; |
|
| 27 | + $toolbox->resetSession(); |
|
| 28 | + $toolbox->lti_authenticate(); |
|
| 29 | + exit; |
|
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | /* if authenticated LTI launch, get the current user profile */ |
| 33 | 33 | if (!empty($_SESSION[ToolProvider::class]['canvas'])) { |
| 34 | - try { |
|
| 35 | - $profile = $toolbox->api_get('users/' . $_SESSION[ToolProvider::class]['canvas']['user_id'] . '/profile'); |
|
| 36 | - } catch (Exception $e) { |
|
| 37 | - $toolbox->smarty_addMessage( |
|
| 38 | - 'Error', |
|
| 39 | - json_decode($e->getMessage(), true), |
|
| 40 | - NotificationMessage::DANGER |
|
| 41 | - ); |
|
| 42 | - } |
|
| 43 | - $toolbox->smarty_assign([ |
|
| 44 | - 'profile' => $profile |
|
| 45 | - ]); |
|
| 46 | - $toolbox->smarty_display('home.tpl'); |
|
| 47 | - exit; |
|
| 34 | + try { |
|
| 35 | + $profile = $toolbox->api_get('users/' . $_SESSION[ToolProvider::class]['canvas']['user_id'] . '/profile'); |
|
| 36 | + } catch (Exception $e) { |
|
| 37 | + $toolbox->smarty_addMessage( |
|
| 38 | + 'Error', |
|
| 39 | + json_decode($e->getMessage(), true), |
|
| 40 | + NotificationMessage::DANGER |
|
| 41 | + ); |
|
| 42 | + } |
|
| 43 | + $toolbox->smarty_assign([ |
|
| 44 | + 'profile' => $profile |
|
| 45 | + ]); |
|
| 46 | + $toolbox->smarty_display('home.tpl'); |
|
| 47 | + exit; |
|
| 48 | 48 | |
| 49 | 49 | /* if not authenticated, default to showing credentials */ |
| 50 | 50 | } else { |
| 51 | - $action = (empty($action) ? |
|
| 52 | - ACTION_CONFIG : |
|
| 53 | - $action |
|
| 54 | - ); |
|
| 51 | + $action = (empty($action) ? |
|
| 52 | + ACTION_CONFIG : |
|
| 53 | + $action |
|
| 54 | + ); |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | /* process any actions */ |
| 58 | 58 | switch ($action) { |
| 59 | - /* reset cached install data from config file */ |
|
| 60 | - case ACTION_INSTALL: |
|
| 61 | - $_SESSION['toolbox'] = Toolbox::fromConfiguration(CONFIG_FILE, true); |
|
| 62 | - $toolbox =& $_SESSION['toolbox']; |
|
| 59 | + /* reset cached install data from config file */ |
|
| 60 | + case ACTION_INSTALL: |
|
| 61 | + $_SESSION['toolbox'] = Toolbox::fromConfiguration(CONFIG_FILE, true); |
|
| 62 | + $toolbox =& $_SESSION['toolbox']; |
|
| 63 | 63 | |
| 64 | - /* test to see if we can connect to the API */ |
|
| 65 | - try { |
|
| 66 | - $toolbox->getAPI(); |
|
| 67 | - } catch (ConfigurationException $e) { |
|
| 68 | - /* if there isn't an API token in config.xml, are there OAuth credentials? */ |
|
| 69 | - if ($e->getCode() === ConfigurationException::CANVAS_API_INCORRECT) { |
|
| 70 | - $toolbox->interactiveGetAccessToken(); |
|
| 71 | - exit; |
|
| 72 | - } else { /* no (understandable) API credentials available -- doh! */ |
|
| 73 | - throw $e; |
|
| 74 | - } |
|
| 75 | - } |
|
| 64 | + /* test to see if we can connect to the API */ |
|
| 65 | + try { |
|
| 66 | + $toolbox->getAPI(); |
|
| 67 | + } catch (ConfigurationException $e) { |
|
| 68 | + /* if there isn't an API token in config.xml, are there OAuth credentials? */ |
|
| 69 | + if ($e->getCode() === ConfigurationException::CANVAS_API_INCORRECT) { |
|
| 70 | + $toolbox->interactiveGetAccessToken(); |
|
| 71 | + exit; |
|
| 72 | + } else { /* no (understandable) API credentials available -- doh! */ |
|
| 73 | + throw $e; |
|
| 74 | + } |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /* finish by opening consumers control panel */ |
|
| 78 | - header('Location: consumers.php'); |
|
| 79 | - exit; |
|
| 77 | + /* finish by opening consumers control panel */ |
|
| 78 | + header('Location: consumers.php'); |
|
| 79 | + exit; |
|
| 80 | 80 | |
| 81 | - /* show LTI configuration XML file */ |
|
| 82 | - case ACTION_CONFIG: |
|
| 83 | - header('Content-type: application/xml'); |
|
| 84 | - echo $toolbox->saveConfigurationXML(); |
|
| 85 | - exit; |
|
| 81 | + /* show LTI configuration XML file */ |
|
| 82 | + case ACTION_CONFIG: |
|
| 83 | + header('Content-type: application/xml'); |
|
| 84 | + echo $toolbox->saveConfigurationXML(); |
|
| 85 | + exit; |
|
| 86 | 86 | } |
@@ -3,27 +3,27 @@ |
||
| 3 | 3 | require_once 'common.inc.php'; |
| 4 | 4 | |
| 5 | 5 | if (isset($_REQUEST['course_url'])) { |
| 6 | - $courseId = preg_replace('|.*/courses/(\d+)/?.*|', '$1', parse_url($_REQUEST['course_url'], PHP_URL_PATH)); |
|
| 7 | - $course = $api->get("/courses/$courseId"); |
|
| 8 | - if ($course) { |
|
| 9 | - $webcalFeed = str_replace('https://', 'webcal://', $course['calendar']['ics']); |
|
| 10 | - $smarty->assign([ |
|
| 11 | - 'content' => '<h3>Course Calendar ICS Feed</h3> |
|
| 6 | + $courseId = preg_replace('|.*/courses/(\d+)/?.*|', '$1', parse_url($_REQUEST['course_url'], PHP_URL_PATH)); |
|
| 7 | + $course = $api->get("/courses/$courseId"); |
|
| 8 | + if ($course) { |
|
| 9 | + $webcalFeed = str_replace('https://', 'webcal://', $course['calendar']['ics']); |
|
| 10 | + $smarty->assign([ |
|
| 11 | + 'content' => '<h3>Course Calendar ICS Feed</h3> |
|
| 12 | 12 | <p>You can subscribe to the calendar for <a href="https://' . |
| 13 | - $_SESSION[CANVAS_INSTANCE_URL] . '/courses/' . $courseId . |
|
| 14 | - '">' . $course['name'] . '</a> at <a href="' . |
|
| 15 | - $webcalFeed . '">' . $webcalFeed . |
|
| 16 | - '</a> in any calendar application that supports external ICS feeds.</p>' |
|
| 17 | - ]); |
|
| 18 | - } else { |
|
| 19 | - $messages[] = array( |
|
| 20 | - 'class' => 'error', |
|
| 21 | - 'title' => 'Canvas API Error', |
|
| 22 | - 'content' => 'The course you requested could not be accessed.<pre>' . print_r($json, false) . '</pre>' |
|
| 23 | - ); |
|
| 24 | - } |
|
| 13 | + $_SESSION[CANVAS_INSTANCE_URL] . '/courses/' . $courseId . |
|
| 14 | + '">' . $course['name'] . '</a> at <a href="' . |
|
| 15 | + $webcalFeed . '">' . $webcalFeed . |
|
| 16 | + '</a> in any calendar application that supports external ICS feeds.</p>' |
|
| 17 | + ]); |
|
| 18 | + } else { |
|
| 19 | + $messages[] = array( |
|
| 20 | + 'class' => 'error', |
|
| 21 | + 'title' => 'Canvas API Error', |
|
| 22 | + 'content' => 'The course you requested could not be accessed.<pre>' . print_r($json, false) . '</pre>' |
|
| 23 | + ); |
|
| 24 | + } |
|
| 25 | 25 | } else { |
| 26 | - $smarty->assign('content', ' |
|
| 26 | + $smarty->assign('content', ' |
|
| 27 | 27 | <form method="post" action="' . $_SERVER['PHP_SELF'] . '"> |
| 28 | 28 | <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> |
| 29 | 29 | <input id="course_url" name="course_url" type="text" /> |
@@ -10,8 +10,8 @@ discard block |
||
| 10 | 10 | $smarty->addTemplateDir(__DIR__ . '/templates'); |
| 11 | 11 | |
| 12 | 12 | if (empty($_REQUEST['url'])) { |
| 13 | - $smarty->display('visualize-form.tpl'); |
|
| 14 | - exit; |
|
| 13 | + $smarty->display('visualize-form.tpl'); |
|
| 14 | + exit; |
|
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | $config = new ConfigXML(__DIR__ . '/secrets.xml'); |
@@ -22,14 +22,14 @@ discard block |
||
| 22 | 22 | |
| 23 | 23 | $ics = $cache->getCache($_REQUEST['url']); |
| 24 | 24 | if (empty($ics)) { |
| 25 | - $ics = new vcalendar( |
|
| 26 | - array( |
|
| 27 | - 'unique_id' => basename(__FILE__, '.php'), |
|
| 28 | - 'url' => $_REQUEST['url'] |
|
| 29 | - ) |
|
| 30 | - ); |
|
| 31 | - $ics->parse(); |
|
| 32 | - $cache->setCache($_REQUEST['url'], $ics); |
|
| 25 | + $ics = new vcalendar( |
|
| 26 | + array( |
|
| 27 | + 'unique_id' => basename(__FILE__, '.php'), |
|
| 28 | + 'url' => $_REQUEST['url'] |
|
| 29 | + ) |
|
| 30 | + ); |
|
| 31 | + $ics->parse(); |
|
| 32 | + $cache->setCache($_REQUEST['url'], $ics); |
|
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | $smarty->assign('ics', $ics); |
@@ -1,12 +1,12 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (php_sapi_name() == 'cli') { |
| 4 | - // TODO there is a more streamlined way of doing this that escapes me just this second |
|
| 5 | - $_REQUEST['cal'] = $argv[1]; |
|
| 6 | - $_REQUEST['canvas_url'] = $argv[2]; |
|
| 7 | - $_REQUEST['schedule'] = $argv[3]; |
|
| 4 | + // TODO there is a more streamlined way of doing this that escapes me just this second |
|
| 5 | + $_REQUEST['cal'] = $argv[1]; |
|
| 6 | + $_REQUEST['canvas_url'] = $argv[2]; |
|
| 7 | + $_REQUEST['schedule'] = $argv[3]; |
|
| 8 | 8 | |
| 9 | - define('IGNORE_LTI', true); |
|
| 9 | + define('IGNORE_LTI', true); |
|
| 10 | 10 | } |
| 11 | 11 | |
| 12 | 12 | require_once 'common.inc.php'; |
@@ -19,8 +19,8 @@ discard block |
||
| 19 | 19 | **/ |
| 20 | 20 | function urlExists($url) |
| 21 | 21 | { |
| 22 | - $handle = fopen($url, 'r'); |
|
| 23 | - return $handle !== false; |
|
| 22 | + $handle = fopen($url, 'r'); |
|
| 23 | + return $handle !== false; |
|
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | /** |
@@ -28,41 +28,41 @@ discard block |
||
| 28 | 28 | **/ |
| 29 | 29 | function getCanvasContext($canvasUrl) |
| 30 | 30 | { |
| 31 | - global $metadata; |
|
| 32 | - |
|
| 33 | - // TODO: accept calendar2?contexts links too (they would be an intuitively obvious link to use, after all) |
|
| 34 | - // FIXME: users aren't working |
|
| 35 | - // TODO: it would probably be better to look up users by email address than URL |
|
| 36 | - /* get the context (user, course or group) for the canvas URL */ |
|
| 37 | - $canvasContext = array(); |
|
| 38 | - if (preg_match('%(https?://)?(' . parse_url($metadata['CANVAS_INSTANCE_URL'], PHP_URL_HOST) . '/((about/(\d+))|(courses/(\d+)(/groups/(\d+))?)|(accounts/\d+/groups/(\d+))))%', $_REQUEST['canvas_url'], $matches)) { |
|
| 39 | - $canvasContext['canonical_url'] = "https://{$matches[2]}"; // https://stmarksschool.instructure.com/courses/953 |
|
| 40 | - |
|
| 41 | - // course or account groups |
|
| 42 | - if (isset($matches[9]) || isset($matches[11])) { |
|
| 43 | - $canvasContext['context'] = 'group'; // used to for context_code in events |
|
| 44 | - $canvasContext['id'] = ($matches[9] > $matches[11] ? $matches[9] : $matches[11]); |
|
| 45 | - $canvasContext['verification_url'] = "groups/{$canvasContext['id']}"; // used once to look up the object to be sure it really exists |
|
| 46 | - |
|
| 47 | - // courses |
|
| 48 | - } elseif (isset($matches[7])) { |
|
| 49 | - $canvasContext['context'] = 'course'; |
|
| 50 | - $canvasContext['id'] = $matches[7]; |
|
| 51 | - $canvasContext['verification_url'] = "courses/{$canvasContext['id']}"; |
|
| 52 | - |
|
| 53 | - // users |
|
| 54 | - } elseif (isset($matches[5])) { |
|
| 55 | - $canvasContext['context'] = 'user'; |
|
| 56 | - $canvasContext['id'] = $matches[5]; |
|
| 57 | - $canvasContext['verification_url'] = "users/{$canvasContext['id']}/profile"; |
|
| 58 | - |
|
| 59 | - // we're somewhere where we don't know where we are |
|
| 60 | - } else { |
|
| 61 | - return false; |
|
| 62 | - } |
|
| 63 | - return $canvasContext; |
|
| 64 | - } |
|
| 65 | - return false; |
|
| 31 | + global $metadata; |
|
| 32 | + |
|
| 33 | + // TODO: accept calendar2?contexts links too (they would be an intuitively obvious link to use, after all) |
|
| 34 | + // FIXME: users aren't working |
|
| 35 | + // TODO: it would probably be better to look up users by email address than URL |
|
| 36 | + /* get the context (user, course or group) for the canvas URL */ |
|
| 37 | + $canvasContext = array(); |
|
| 38 | + if (preg_match('%(https?://)?(' . parse_url($metadata['CANVAS_INSTANCE_URL'], PHP_URL_HOST) . '/((about/(\d+))|(courses/(\d+)(/groups/(\d+))?)|(accounts/\d+/groups/(\d+))))%', $_REQUEST['canvas_url'], $matches)) { |
|
| 39 | + $canvasContext['canonical_url'] = "https://{$matches[2]}"; // https://stmarksschool.instructure.com/courses/953 |
|
| 40 | + |
|
| 41 | + // course or account groups |
|
| 42 | + if (isset($matches[9]) || isset($matches[11])) { |
|
| 43 | + $canvasContext['context'] = 'group'; // used to for context_code in events |
|
| 44 | + $canvasContext['id'] = ($matches[9] > $matches[11] ? $matches[9] : $matches[11]); |
|
| 45 | + $canvasContext['verification_url'] = "groups/{$canvasContext['id']}"; // used once to look up the object to be sure it really exists |
|
| 46 | + |
|
| 47 | + // courses |
|
| 48 | + } elseif (isset($matches[7])) { |
|
| 49 | + $canvasContext['context'] = 'course'; |
|
| 50 | + $canvasContext['id'] = $matches[7]; |
|
| 51 | + $canvasContext['verification_url'] = "courses/{$canvasContext['id']}"; |
|
| 52 | + |
|
| 53 | + // users |
|
| 54 | + } elseif (isset($matches[5])) { |
|
| 55 | + $canvasContext['context'] = 'user'; |
|
| 56 | + $canvasContext['id'] = $matches[5]; |
|
| 57 | + $canvasContext['verification_url'] = "users/{$canvasContext['id']}/profile"; |
|
| 58 | + |
|
| 59 | + // we're somewhere where we don't know where we are |
|
| 60 | + } else { |
|
| 61 | + return false; |
|
| 62 | + } |
|
| 63 | + return $canvasContext; |
|
| 64 | + } |
|
| 65 | + return false; |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | /** |
@@ -73,22 +73,22 @@ discard block |
||
| 73 | 73 | function filterEvent($event, $calendarCache) |
| 74 | 74 | { |
| 75 | 75 | |
| 76 | - return ( |
|
| 77 | - // include this event if filtering is off... |
|
| 78 | - $calendarCache['enable_regexp_filter'] == false || |
|
| 79 | - ( |
|
| 80 | - ( |
|
| 81 | - ( // if filtering is on, and there's an include pattern test that pattern... |
|
| 82 | - !empty($calendarCache['include_regexp']) && |
|
| 83 | - preg_match("%{$calendarCache['include_regexp']}%", $event->getProperty('SUMMARY')) |
|
| 84 | - ) |
|
| 85 | - ) && |
|
| 86 | - !( // if there is an exclude pattern, make sure that this event is NOT excluded |
|
| 87 | - !empty($calendarCache['exclude_regexp']) && |
|
| 88 | - preg_match("%{$calendarCache['exclude_regexp']}%", $event->getProperty('SUMMARY')) |
|
| 89 | - ) |
|
| 90 | - ) |
|
| 91 | - ); |
|
| 76 | + return ( |
|
| 77 | + // include this event if filtering is off... |
|
| 78 | + $calendarCache['enable_regexp_filter'] == false || |
|
| 79 | + ( |
|
| 80 | + ( |
|
| 81 | + ( // if filtering is on, and there's an include pattern test that pattern... |
|
| 82 | + !empty($calendarCache['include_regexp']) && |
|
| 83 | + preg_match("%{$calendarCache['include_regexp']}%", $event->getProperty('SUMMARY')) |
|
| 84 | + ) |
|
| 85 | + ) && |
|
| 86 | + !( // if there is an exclude pattern, make sure that this event is NOT excluded |
|
| 87 | + !empty($calendarCache['exclude_regexp']) && |
|
| 88 | + preg_match("%{$calendarCache['exclude_regexp']}%", $event->getProperty('SUMMARY')) |
|
| 89 | + ) |
|
| 90 | + ) |
|
| 91 | + ); |
|
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | // TODO: it would be nice to be able to cleanly remove a synched calendar |
@@ -99,54 +99,54 @@ discard block |
||
| 99 | 99 | /* do we have the vital information (an ICS feed and a URL to a canvas |
| 100 | 100 | object)? */ |
| 101 | 101 | if (isset($_REQUEST['cal']) && isset($_REQUEST['canvas_url'])) { |
| 102 | - if ($canvasContext = getCanvasContext($_REQUEST['canvas_url'])) { |
|
| 103 | - /* check ICS feed to be sure it exists */ |
|
| 104 | - if (urlExists($_REQUEST['cal'])) { |
|
| 105 | - /* look up the canvas object -- mostly to make sure that it exists! */ |
|
| 106 | - if ($canvasObject = $api->get($canvasContext['verification_url'])) { |
|
| 107 | - /* calculate the unique pairing ID of this ICS feed and canvas object */ |
|
| 108 | - $pairingHash = $toolbox->getPairingHash($_REQUEST['cal'], $canvasContext['canonical_url']); |
|
| 109 | - $log = Log::singleton('file', __DIR__ . "/logs/$pairingHash.log"); |
|
| 110 | - postMessage('Sync started', getSyncTimestamp(), NotificationMessage::INFO); |
|
| 111 | - |
|
| 112 | - /* tell users that it's started and to cool their jets */ |
|
| 113 | - if (php_sapi_name() != 'cli') { |
|
| 114 | - $smarty->assign([ |
|
| 115 | - 'content' => '<h3>Calendar Import Started</h3> |
|
| 102 | + if ($canvasContext = getCanvasContext($_REQUEST['canvas_url'])) { |
|
| 103 | + /* check ICS feed to be sure it exists */ |
|
| 104 | + if (urlExists($_REQUEST['cal'])) { |
|
| 105 | + /* look up the canvas object -- mostly to make sure that it exists! */ |
|
| 106 | + if ($canvasObject = $api->get($canvasContext['verification_url'])) { |
|
| 107 | + /* calculate the unique pairing ID of this ICS feed and canvas object */ |
|
| 108 | + $pairingHash = $toolbox->getPairingHash($_REQUEST['cal'], $canvasContext['canonical_url']); |
|
| 109 | + $log = Log::singleton('file', __DIR__ . "/logs/$pairingHash.log"); |
|
| 110 | + postMessage('Sync started', getSyncTimestamp(), NotificationMessage::INFO); |
|
| 111 | + |
|
| 112 | + /* tell users that it's started and to cool their jets */ |
|
| 113 | + if (php_sapi_name() != 'cli') { |
|
| 114 | + $smarty->assign([ |
|
| 115 | + 'content' => '<h3>Calendar Import Started</h3> |
|
| 116 | 116 | <p>The calendar import that you requested has begun. You may leave this page at anytime. You can see the progress of the import by visiting <a target="_blank" href="' . $_SESSION[CANVAS_INSTANCE_URL] . "/calendar?include_contexts={$canvasContext['context']}_{$canvasObject['id']}\">this calendar</a> in Canvas.</p>" |
| 117 | - ]); |
|
| 118 | - $smarty->display('page.tpl'); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /* parse the ICS feed */ |
|
| 122 | - $ics = new vcalendar( |
|
| 123 | - array( |
|
| 124 | - 'unique_id' => $metadata['APP_ID'], |
|
| 125 | - 'url' => $_REQUEST['cal'] |
|
| 126 | - ) |
|
| 127 | - ); |
|
| 128 | - $ics->parse(); |
|
| 129 | - |
|
| 130 | - /* log this pairing in the database cache, if it doesn't already exist */ |
|
| 131 | - $calendarCacheResponse = $sql->query(" |
|
| 117 | + ]); |
|
| 118 | + $smarty->display('page.tpl'); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /* parse the ICS feed */ |
|
| 122 | + $ics = new vcalendar( |
|
| 123 | + array( |
|
| 124 | + 'unique_id' => $metadata['APP_ID'], |
|
| 125 | + 'url' => $_REQUEST['cal'] |
|
| 126 | + ) |
|
| 127 | + ); |
|
| 128 | + $ics->parse(); |
|
| 129 | + |
|
| 130 | + /* log this pairing in the database cache, if it doesn't already exist */ |
|
| 131 | + $calendarCacheResponse = $sql->query(" |
|
| 132 | 132 | SELECT * |
| 133 | 133 | FROM `calendars` |
| 134 | 134 | WHERE |
| 135 | 135 | `id` = '$pairingHash' |
| 136 | 136 | "); |
| 137 | - $calendarCache = $calendarCacheResponse->fetch_assoc(); |
|
| 137 | + $calendarCache = $calendarCacheResponse->fetch_assoc(); |
|
| 138 | 138 | |
| 139 | - /* if the calendar is already cached, just update the sync timestamp */ |
|
| 140 | - if ($calendarCache) { |
|
| 141 | - $sql->query(" |
|
| 139 | + /* if the calendar is already cached, just update the sync timestamp */ |
|
| 140 | + if ($calendarCache) { |
|
| 141 | + $sql->query(" |
|
| 142 | 142 | UPDATE `calendars` |
| 143 | 143 | SET |
| 144 | 144 | `synced` = '" . getSyncTimestamp() . "' |
| 145 | 145 | WHERE |
| 146 | 146 | `id` = '$pairingHash' |
| 147 | 147 | "); |
| 148 | - } else { |
|
| 149 | - $sql->query(" |
|
| 148 | + } else { |
|
| 149 | + $sql->query(" |
|
| 150 | 150 | INSERT INTO `calendars` |
| 151 | 151 | ( |
| 152 | 152 | `id`, |
@@ -169,44 +169,44 @@ discard block |
||
| 169 | 169 | " . ($_REQUEST['enable_regexp_filter'] == VALUE_ENABLE_REGEXP_FILTER ? "'" . $sql->real_escape_string($_REQUEST['exclude_regexp']) . "'" : 'NULL') . " |
| 170 | 170 | ) |
| 171 | 171 | "); |
| 172 | - } |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | - /* refresh calendar information from cache database */ |
|
| 175 | - $calendarCacheResponse = $sql->query(" |
|
| 174 | + /* refresh calendar information from cache database */ |
|
| 175 | + $calendarCacheResponse = $sql->query(" |
|
| 176 | 176 | SELECT * |
| 177 | 177 | FROM `calendars` |
| 178 | 178 | WHERE |
| 179 | 179 | `id` = '$pairingHash' |
| 180 | 180 | "); |
| 181 | - $calendarCache = $calendarCacheResponse->fetch_assoc(); |
|
| 181 | + $calendarCache = $calendarCacheResponse->fetch_assoc(); |
|
| 182 | 182 | |
| 183 | - /* walk through $master_array and update the Canvas calendar to match the |
|
| 183 | + /* walk through $master_array and update the Canvas calendar to match the |
|
| 184 | 184 | ICS feed, caching changes in the database */ |
| 185 | - // TODO: would it be worth the performance improvement to just process things from today's date forward? (i.e. ignore old items, even if they've changed...) |
|
| 186 | - // TODO:0 the best window for syncing would be the term of the course in question, right? issue:12 |
|
| 187 | - // TODO:0 Arbitrarily selecting events in for a year on either side of today's date, probably a better system? issue:12 |
|
| 188 | - foreach ($ics->selectComponents( |
|
| 189 | - date('Y')-1, // startYear |
|
| 190 | - date('m'), // startMonth |
|
| 191 | - date('d'), // startDay |
|
| 192 | - date('Y')+1, // endYEar |
|
| 193 | - date('m'), // endMonth |
|
| 194 | - date('d'), // endDay |
|
| 195 | - 'vevent', // cType |
|
| 196 | - false, // flat |
|
| 197 | - true, // any |
|
| 198 | - true // split |
|
| 199 | - ) as $year) { |
|
| 200 | - foreach ($year as $month => $days) { |
|
| 201 | - foreach ($days as $day => $events) { |
|
| 202 | - foreach ($events as $i => $event) { |
|
| 203 | - /* does this event already exist in Canvas? */ |
|
| 204 | - $eventHash = getEventHash($event); |
|
| 205 | - |
|
| 206 | - /* if the event should be included... */ |
|
| 207 | - if (filterEvent($event, $calendarCache)) { |
|
| 208 | - /* have we cached this event already? */ |
|
| 209 | - $eventCacheResponse = $sql->query(" |
|
| 185 | + // TODO: would it be worth the performance improvement to just process things from today's date forward? (i.e. ignore old items, even if they've changed...) |
|
| 186 | + // TODO:0 the best window for syncing would be the term of the course in question, right? issue:12 |
|
| 187 | + // TODO:0 Arbitrarily selecting events in for a year on either side of today's date, probably a better system? issue:12 |
|
| 188 | + foreach ($ics->selectComponents( |
|
| 189 | + date('Y')-1, // startYear |
|
| 190 | + date('m'), // startMonth |
|
| 191 | + date('d'), // startDay |
|
| 192 | + date('Y')+1, // endYEar |
|
| 193 | + date('m'), // endMonth |
|
| 194 | + date('d'), // endDay |
|
| 195 | + 'vevent', // cType |
|
| 196 | + false, // flat |
|
| 197 | + true, // any |
|
| 198 | + true // split |
|
| 199 | + ) as $year) { |
|
| 200 | + foreach ($year as $month => $days) { |
|
| 201 | + foreach ($days as $day => $events) { |
|
| 202 | + foreach ($events as $i => $event) { |
|
| 203 | + /* does this event already exist in Canvas? */ |
|
| 204 | + $eventHash = getEventHash($event); |
|
| 205 | + |
|
| 206 | + /* if the event should be included... */ |
|
| 207 | + if (filterEvent($event, $calendarCache)) { |
|
| 208 | + /* have we cached this event already? */ |
|
| 209 | + $eventCacheResponse = $sql->query(" |
|
| 210 | 210 | SELECT * |
| 211 | 211 | FROM `events` |
| 212 | 212 | WHERE |
@@ -215,11 +215,11 @@ discard block |
||
| 215 | 215 | "); |
| 216 | 216 | |
| 217 | 217 | |
| 218 | - /* if we already have the event cached in its current form, just update |
|
| 218 | + /* if we already have the event cached in its current form, just update |
|
| 219 | 219 | the timestamp */ |
| 220 | - $eventCache = $eventCacheResponse->fetch_assoc(); |
|
| 221 | - if ($eventCache) { |
|
| 222 | - $sql->query(" |
|
| 220 | + $eventCache = $eventCacheResponse->fetch_assoc(); |
|
| 221 | + if ($eventCache) { |
|
| 222 | + $sql->query(" |
|
| 223 | 223 | UPDATE `events` |
| 224 | 224 | SET |
| 225 | 225 | `synced` = '" . getSyncTimestamp() . "' |
@@ -227,31 +227,31 @@ discard block |
||
| 227 | 227 | `id` = '{$eventCache['id']}' |
| 228 | 228 | "); |
| 229 | 229 | |
| 230 | - /* otherwise, add this new event and cache it */ |
|
| 231 | - } else { |
|
| 232 | - /* multi-day event instance start times need to be changed to _this_ date */ |
|
| 233 | - $start = new DateTime(iCalUtilityFunctions::_date2strdate($event->getProperty('DTSTART'))); |
|
| 234 | - $end = new DateTime(iCalUtilityFunctions::_date2strdate($event->getProperty('DTEND'))); |
|
| 235 | - if ($event->getProperty('X-RECURRENCE')) { |
|
| 236 | - $start = new DateTime($event->getProperty('X-CURRENT-DTSTART')[1]); |
|
| 237 | - $end = new DateTime($event->getProperty('X-CURRENT-DTEND')[1]); |
|
| 238 | - } |
|
| 239 | - $start->setTimeZone(new DateTimeZone(LOCAL_TIMEZONE)); |
|
| 240 | - $end->setTimeZone(new DateTimeZone(LOCAL_TIMEZONE)); |
|
| 241 | - |
|
| 242 | - $calendarEvent = $api->post( |
|
| 243 | - "/calendar_events", |
|
| 244 | - [ |
|
| 245 | - 'calendar_event[context_code]' => "{$canvasContext['context']}_{$canvasObject['id']}", |
|
| 246 | - 'calendar_event[title]' => preg_replace('%^([^\]]+)(\s*\[[^\]]+\]\s*)+$%', '\\1', strip_tags($event->getProperty('SUMMARY'))), |
|
| 247 | - 'calendar_event[description]' => \Michelf\Markdown::defaultTransform(str_replace('\n', "\n\n", $event->getProperty('DESCRIPTION', 1))), |
|
| 248 | - 'calendar_event[start_at]' => $start->format(CANVAS_TIMESTAMP_FORMAT), |
|
| 249 | - 'calendar_event[end_at]' => $end->format(CANVAS_TIMESTAMP_FORMAT), |
|
| 250 | - 'calendar_event[location_name]' => $event->getProperty('LOCATION') |
|
| 251 | - ] |
|
| 252 | - ); |
|
| 253 | - |
|
| 254 | - $sql->query(" |
|
| 230 | + /* otherwise, add this new event and cache it */ |
|
| 231 | + } else { |
|
| 232 | + /* multi-day event instance start times need to be changed to _this_ date */ |
|
| 233 | + $start = new DateTime(iCalUtilityFunctions::_date2strdate($event->getProperty('DTSTART'))); |
|
| 234 | + $end = new DateTime(iCalUtilityFunctions::_date2strdate($event->getProperty('DTEND'))); |
|
| 235 | + if ($event->getProperty('X-RECURRENCE')) { |
|
| 236 | + $start = new DateTime($event->getProperty('X-CURRENT-DTSTART')[1]); |
|
| 237 | + $end = new DateTime($event->getProperty('X-CURRENT-DTEND')[1]); |
|
| 238 | + } |
|
| 239 | + $start->setTimeZone(new DateTimeZone(LOCAL_TIMEZONE)); |
|
| 240 | + $end->setTimeZone(new DateTimeZone(LOCAL_TIMEZONE)); |
|
| 241 | + |
|
| 242 | + $calendarEvent = $api->post( |
|
| 243 | + "/calendar_events", |
|
| 244 | + [ |
|
| 245 | + 'calendar_event[context_code]' => "{$canvasContext['context']}_{$canvasObject['id']}", |
|
| 246 | + 'calendar_event[title]' => preg_replace('%^([^\]]+)(\s*\[[^\]]+\]\s*)+$%', '\\1', strip_tags($event->getProperty('SUMMARY'))), |
|
| 247 | + 'calendar_event[description]' => \Michelf\Markdown::defaultTransform(str_replace('\n', "\n\n", $event->getProperty('DESCRIPTION', 1))), |
|
| 248 | + 'calendar_event[start_at]' => $start->format(CANVAS_TIMESTAMP_FORMAT), |
|
| 249 | + 'calendar_event[end_at]' => $end->format(CANVAS_TIMESTAMP_FORMAT), |
|
| 250 | + 'calendar_event[location_name]' => $event->getProperty('LOCATION') |
|
| 251 | + ] |
|
| 252 | + ); |
|
| 253 | + |
|
| 254 | + $sql->query(" |
|
| 255 | 255 | INSERT INTO `events` |
| 256 | 256 | ( |
| 257 | 257 | `calendar`, |
@@ -266,105 +266,105 @@ discard block |
||
| 266 | 266 | '" . getSyncTimestamp() . "' |
| 267 | 267 | ) |
| 268 | 268 | "); |
| 269 | - } |
|
| 270 | - } |
|
| 271 | - } |
|
| 272 | - } |
|
| 273 | - } |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - /* clean out previously synced events that are no longer correct */ |
|
| 277 | - $deletedEventsResponse = $sql->query(" |
|
| 269 | + } |
|
| 270 | + } |
|
| 271 | + } |
|
| 272 | + } |
|
| 273 | + } |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + /* clean out previously synced events that are no longer correct */ |
|
| 277 | + $deletedEventsResponse = $sql->query(" |
|
| 278 | 278 | SELECT * FROM `events` |
| 279 | 279 | WHERE |
| 280 | 280 | `calendar` = '{$calendarCache['id']}' AND |
| 281 | 281 | `synced` != '" . getSyncTimestamp() . "' |
| 282 | 282 | "); |
| 283 | - while ($deletedEventCache = $deletedEventsResponse->fetch_assoc()) { |
|
| 284 | - try { |
|
| 285 | - $deletedEvent = $api->delete( |
|
| 286 | - "/calendar_events/{$deletedEventCache['calendar_event[id]']}", |
|
| 287 | - [ |
|
| 288 | - 'cancel_reason' => getSyncTimestamp(), |
|
| 289 | - 'as_user_id' => ($canvasContext['context'] == 'user' ? $canvasObject['id'] : '') // TODO: this feels skeevy -- like the empty string will break |
|
| 290 | - ] |
|
| 291 | - ); |
|
| 292 | - } catch (Pest_Unauthorized $e) { |
|
| 293 | - /* if the event has been deleted in Canvas, we'll get an error when |
|
| 283 | + while ($deletedEventCache = $deletedEventsResponse->fetch_assoc()) { |
|
| 284 | + try { |
|
| 285 | + $deletedEvent = $api->delete( |
|
| 286 | + "/calendar_events/{$deletedEventCache['calendar_event[id]']}", |
|
| 287 | + [ |
|
| 288 | + 'cancel_reason' => getSyncTimestamp(), |
|
| 289 | + 'as_user_id' => ($canvasContext['context'] == 'user' ? $canvasObject['id'] : '') // TODO: this feels skeevy -- like the empty string will break |
|
| 290 | + ] |
|
| 291 | + ); |
|
| 292 | + } catch (Pest_Unauthorized $e) { |
|
| 293 | + /* if the event has been deleted in Canvas, we'll get an error when |
|
| 294 | 294 | we try to delete it a second time. We still need to delete it from |
| 295 | 295 | our cache database, however */ |
| 296 | - postMessage('Cache out-of-sync', "calendar_event[{$deletedEventCache['calendar_event[id]']}] no longer exists and will be purged from cache.", NotificationMessage::INFO); |
|
| 297 | - } catch (Pest_ClientError $e) { |
|
| 298 | - postMessage( |
|
| 299 | - 'API Client Error', |
|
| 300 | - '<pre>' . print_r(array( |
|
| 301 | - 'Status' => $PEST->lastStatus(), |
|
| 302 | - 'Error' => $PEST->lastBody(), |
|
| 303 | - 'Verb' => $verb, |
|
| 304 | - 'URL' => $url, |
|
| 305 | - 'Data' => $data |
|
| 306 | - ), false) . '</pre>', |
|
| 307 | - NotificationMessage::ERROR |
|
| 308 | - ); |
|
| 309 | - if (php_sapi_name() != 'cli') { |
|
| 310 | - $smarty->display('page.tpl') |
|
| 311 | - }; |
|
| 312 | - exit; |
|
| 313 | - } |
|
| 314 | - $sql->query(" |
|
| 296 | + postMessage('Cache out-of-sync', "calendar_event[{$deletedEventCache['calendar_event[id]']}] no longer exists and will be purged from cache.", NotificationMessage::INFO); |
|
| 297 | + } catch (Pest_ClientError $e) { |
|
| 298 | + postMessage( |
|
| 299 | + 'API Client Error', |
|
| 300 | + '<pre>' . print_r(array( |
|
| 301 | + 'Status' => $PEST->lastStatus(), |
|
| 302 | + 'Error' => $PEST->lastBody(), |
|
| 303 | + 'Verb' => $verb, |
|
| 304 | + 'URL' => $url, |
|
| 305 | + 'Data' => $data |
|
| 306 | + ), false) . '</pre>', |
|
| 307 | + NotificationMessage::ERROR |
|
| 308 | + ); |
|
| 309 | + if (php_sapi_name() != 'cli') { |
|
| 310 | + $smarty->display('page.tpl') |
|
| 311 | + }; |
|
| 312 | + exit; |
|
| 313 | + } |
|
| 314 | + $sql->query(" |
|
| 315 | 315 | DELETE FROM `events` |
| 316 | 316 | WHERE |
| 317 | 317 | `id` = '{$deletedEventCache['id']}' |
| 318 | 318 | "); |
| 319 | - } |
|
| 319 | + } |
|
| 320 | 320 | |
| 321 | - /* if this was a scheduled import (i.e. a sync), update that schedule */ |
|
| 322 | - if (isset($_REQUEST['schedule'])) { |
|
| 323 | - $sql->query(" |
|
| 321 | + /* if this was a scheduled import (i.e. a sync), update that schedule */ |
|
| 322 | + if (isset($_REQUEST['schedule'])) { |
|
| 323 | + $sql->query(" |
|
| 324 | 324 | UPDATE `schedules` |
| 325 | 325 | SET |
| 326 | 326 | `synced` = '" . getSyncTimestamp() . "' |
| 327 | 327 | WHERE |
| 328 | 328 | `id` = '{$_REQUEST['schedule']}' |
| 329 | 329 | "); |
| 330 | - } |
|
| 331 | - /* are we setting up a regular synchronization? */ |
|
| 332 | - if (isset($_REQUEST['sync']) && $_REQUEST['sync'] != SCHEDULE_ONCE) { |
|
| 330 | + } |
|
| 331 | + /* are we setting up a regular synchronization? */ |
|
| 332 | + if (isset($_REQUEST['sync']) && $_REQUEST['sync'] != SCHEDULE_ONCE) { |
|
| 333 | 333 | |
| 334 | - // FIXME:0 CRON SYNC SETUP GOES HERE issue:15 issue:13 |
|
| 334 | + // FIXME:0 CRON SYNC SETUP GOES HERE issue:15 issue:13 |
|
| 335 | 335 | |
| 336 | - /* add to the cache database schedule, replacing any schedules for this |
|
| 336 | + /* add to the cache database schedule, replacing any schedules for this |
|
| 337 | 337 | calendar that are already there */ |
| 338 | - $schedulesResponse = $sql->query(" |
|
| 338 | + $schedulesResponse = $sql->query(" |
|
| 339 | 339 | SELECT * |
| 340 | 340 | FROM `schedules` |
| 341 | 341 | WHERE |
| 342 | 342 | `calendar` = '{$calendarCache['id']}' |
| 343 | 343 | "); |
| 344 | 344 | |
| 345 | - if ($schedule = $schedulesResponse->fetch_assoc()) { |
|
| 345 | + if ($schedule = $schedulesResponse->fetch_assoc()) { |
|
| 346 | 346 | |
| 347 | - /* only need to worry if the cached schedule is different from the |
|
| 347 | + /* only need to worry if the cached schedule is different from the |
|
| 348 | 348 | new one we just set */ |
| 349 | - if ($shellArguments[INDEX_SCHEDULE] != $schedule['schedule']) { |
|
| 350 | - /* was this the last schedule to require this trigger? */ |
|
| 351 | - $schedulesResponse = $sql->query(" |
|
| 349 | + if ($shellArguments[INDEX_SCHEDULE] != $schedule['schedule']) { |
|
| 350 | + /* was this the last schedule to require this trigger? */ |
|
| 351 | + $schedulesResponse = $sql->query(" |
|
| 352 | 352 | SELECT * |
| 353 | 353 | FROM `schedules` |
| 354 | 354 | WHERE |
| 355 | 355 | `calendar` != '{$calendarCache['id']}' AND |
| 356 | 356 | `schedule` == '{$schedule['schedule']}' |
| 357 | 357 | "); |
| 358 | - /* we're the last one, delete it from crontab */ |
|
| 359 | - if ($schedulesResponse->num_rows == 0) { |
|
| 360 | - $crontabs = preg_replace("%^.*{$schedule['schedule']}.*" . PHP_EOL . '%', '', shell_exec('crontab -l')); |
|
| 361 | - $filename = md5(getSyncTimestamp()) . '.txt'; |
|
| 362 | - file_put_contents("/tmp/$filename", $crontabs); |
|
| 363 | - shell_exec("crontab /tmp/$filename"); |
|
| 364 | - postMessage('Unused schedule', "removed schedule '{$schedule['schedule']}' from crontab", NotificationMessage::INFO); |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - $sql->query(" |
|
| 358 | + /* we're the last one, delete it from crontab */ |
|
| 359 | + if ($schedulesResponse->num_rows == 0) { |
|
| 360 | + $crontabs = preg_replace("%^.*{$schedule['schedule']}.*" . PHP_EOL . '%', '', shell_exec('crontab -l')); |
|
| 361 | + $filename = md5(getSyncTimestamp()) . '.txt'; |
|
| 362 | + file_put_contents("/tmp/$filename", $crontabs); |
|
| 363 | + shell_exec("crontab /tmp/$filename"); |
|
| 364 | + postMessage('Unused schedule', "removed schedule '{$schedule['schedule']}' from crontab", NotificationMessage::INFO); |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + $sql->query(" |
|
| 368 | 368 | UPDATE `schedules` |
| 369 | 369 | SET |
| 370 | 370 | `schedule` = '" . $shellArguments[INDEX_SCHEDULE] . "', |
@@ -372,9 +372,9 @@ discard block |
||
| 372 | 372 | WHERE |
| 373 | 373 | `calendar` = '{$calendarCache['id']}' |
| 374 | 374 | "); |
| 375 | - } |
|
| 376 | - } else { |
|
| 377 | - $sql->query(" |
|
| 375 | + } |
|
| 376 | + } else { |
|
| 377 | + $sql->query(" |
|
| 378 | 378 | INSERT INTO `schedules` |
| 379 | 379 | ( |
| 380 | 380 | `calendar`, |
@@ -387,46 +387,46 @@ discard block |
||
| 387 | 387 | '" . getSyncTimestamp() . "' |
| 388 | 388 | ) |
| 389 | 389 | "); |
| 390 | - } |
|
| 391 | - } |
|
| 390 | + } |
|
| 391 | + } |
|
| 392 | 392 | |
| 393 | - /* if we're ovewriting data (for example, if this is a recurring sync, we |
|
| 393 | + /* if we're ovewriting data (for example, if this is a recurring sync, we |
|
| 394 | 394 | need to remove the events that were _not_ synced this in this round */ |
| 395 | - if (isset($_REQUEST['overwrite']) && $_REQUEST['overwrite'] == VALUE_OVERWRITE_CANVAS_CALENDAR) { |
|
| 396 | - // TODO: actually deal with this |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - // TODO: deal with messaging based on context |
|
| 400 | - |
|
| 401 | - postMessage('Finished sync', getSyncTimestamp(), NotificationMessage::INFO); |
|
| 402 | - exit; |
|
| 403 | - } else { |
|
| 404 | - postMessage( |
|
| 405 | - 'Canvas Object Not Found', |
|
| 406 | - 'The object whose URL you submitted could not be found.<pre>' . print_r(array( |
|
| 407 | - 'Canvas URL' => $_REQUEST['canvas_url'], |
|
| 408 | - 'Canvas Context' => $canvasContext, |
|
| 409 | - 'Canvas Object' => $canvasObject |
|
| 410 | - ), false) . '</pre>', |
|
| 411 | - NotificationMessage::ERROR |
|
| 412 | - ); |
|
| 413 | - } |
|
| 414 | - } else { |
|
| 415 | - postMessage( |
|
| 416 | - 'ICS feed Not Found', |
|
| 417 | - 'The calendar whose URL you submitted could not be found.<pre>' . $_REQUEST['cal'] . '</pre>', |
|
| 418 | - NotificationMessage::ERROR |
|
| 419 | - ); |
|
| 420 | - } |
|
| 421 | - } else { |
|
| 422 | - postMessage( |
|
| 423 | - 'Invalid Canvas URL', |
|
| 424 | - 'The Canvas URL you submitted could not be parsed.<pre>' . $_REQUEST['canvas_url'] . '</pre>', |
|
| 425 | - NotificationMessage::ERROR |
|
| 426 | - ); |
|
| 427 | - if (php_sapi_name() != 'cli') $smarty->display('page.tpl'); |
|
| 428 | - exit; |
|
| 429 | - } |
|
| 395 | + if (isset($_REQUEST['overwrite']) && $_REQUEST['overwrite'] == VALUE_OVERWRITE_CANVAS_CALENDAR) { |
|
| 396 | + // TODO: actually deal with this |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + // TODO: deal with messaging based on context |
|
| 400 | + |
|
| 401 | + postMessage('Finished sync', getSyncTimestamp(), NotificationMessage::INFO); |
|
| 402 | + exit; |
|
| 403 | + } else { |
|
| 404 | + postMessage( |
|
| 405 | + 'Canvas Object Not Found', |
|
| 406 | + 'The object whose URL you submitted could not be found.<pre>' . print_r(array( |
|
| 407 | + 'Canvas URL' => $_REQUEST['canvas_url'], |
|
| 408 | + 'Canvas Context' => $canvasContext, |
|
| 409 | + 'Canvas Object' => $canvasObject |
|
| 410 | + ), false) . '</pre>', |
|
| 411 | + NotificationMessage::ERROR |
|
| 412 | + ); |
|
| 413 | + } |
|
| 414 | + } else { |
|
| 415 | + postMessage( |
|
| 416 | + 'ICS feed Not Found', |
|
| 417 | + 'The calendar whose URL you submitted could not be found.<pre>' . $_REQUEST['cal'] . '</pre>', |
|
| 418 | + NotificationMessage::ERROR |
|
| 419 | + ); |
|
| 420 | + } |
|
| 421 | + } else { |
|
| 422 | + postMessage( |
|
| 423 | + 'Invalid Canvas URL', |
|
| 424 | + 'The Canvas URL you submitted could not be parsed.<pre>' . $_REQUEST['canvas_url'] . '</pre>', |
|
| 425 | + NotificationMessage::ERROR |
|
| 426 | + ); |
|
| 427 | + if (php_sapi_name() != 'cli') $smarty->display('page.tpl'); |
|
| 428 | + exit; |
|
| 429 | + } |
|
| 430 | 430 | } |
| 431 | 431 | |
| 432 | 432 | ?> |
@@ -3,36 +3,36 @@ |
||
| 3 | 3 | require_once 'common.inc.php'; |
| 4 | 4 | |
| 5 | 5 | if (isset($_REQUEST['course_url'])) { |
| 6 | - $eventsApi = new CanvasPest($_SESSION['apiUrl'], $_SESSION['apiToken']); |
|
| 6 | + $eventsApi = new CanvasPest($_SESSION['apiUrl'], $_SESSION['apiToken']); |
|
| 7 | 7 | |
| 8 | - // TODO work nicely with the cache (purge uncached events, or only cached events, etc.) |
|
| 8 | + // TODO work nicely with the cache (purge uncached events, or only cached events, etc.) |
|
| 9 | 9 | |
| 10 | - $events = $eventsApi->get( |
|
| 11 | - 'calendar_events', |
|
| 12 | - [ |
|
| 13 | - 'type' => 'event', |
|
| 14 | - 'all_events' => true, |
|
| 15 | - 'context_codes[]' => preg_replace('|.*/courses/(\d+)/?.*|', "course_$1", $_REQUEST['course_url']) |
|
| 16 | - ] |
|
| 17 | - ); |
|
| 18 | - do { |
|
| 19 | - foreach ($events as $event) { |
|
| 20 | - $api->delete( |
|
| 21 | - "calendar_events/{$event['id']}", |
|
| 22 | - [ |
|
| 23 | - 'cancel_reason' => $metadata['APP_NAME'] . " course_url={$_REQUEST['course_url']}" |
|
| 24 | - ] |
|
| 25 | - ); |
|
| 26 | - } |
|
| 27 | - } while ($events = $eventsApi->nextPage()); |
|
| 10 | + $events = $eventsApi->get( |
|
| 11 | + 'calendar_events', |
|
| 12 | + [ |
|
| 13 | + 'type' => 'event', |
|
| 14 | + 'all_events' => true, |
|
| 15 | + 'context_codes[]' => preg_replace('|.*/courses/(\d+)/?.*|', "course_$1", $_REQUEST['course_url']) |
|
| 16 | + ] |
|
| 17 | + ); |
|
| 18 | + do { |
|
| 19 | + foreach ($events as $event) { |
|
| 20 | + $api->delete( |
|
| 21 | + "calendar_events/{$event['id']}", |
|
| 22 | + [ |
|
| 23 | + 'cancel_reason' => $metadata['APP_NAME'] . " course_url={$_REQUEST['course_url']}" |
|
| 24 | + ] |
|
| 25 | + ); |
|
| 26 | + } |
|
| 27 | + } while ($events = $eventsApi->nextPage()); |
|
| 28 | 28 | |
| 29 | - $smarty->assign([ |
|
| 30 | - 'content' => 'Calendar purged.' |
|
| 31 | - ]); |
|
| 29 | + $smarty->assign([ |
|
| 30 | + 'content' => 'Calendar purged.' |
|
| 31 | + ]); |
|
| 32 | 32 | } else { |
| 33 | - $smarty->assign([ |
|
| 34 | - 'content' => '<form action="' . $_SERVER['PHP_SELF'] . '" method="post"><label for="course_url">Course URL <input id="course_url" name="course_url" type="text" /><input type="submit" value="Purge All Calendar Events" /></form>' |
|
| 35 | - ]); |
|
| 33 | + $smarty->assign([ |
|
| 34 | + 'content' => '<form action="' . $_SERVER['PHP_SELF'] . '" method="post"><label for="course_url">Course URL <input id="course_url" name="course_url" type="text" /><input type="submit" value="Purge All Calendar Events" /></form>' |
|
| 35 | + ]); |
|
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | $smarty->display(); |
@@ -15,13 +15,13 @@ |
||
| 15 | 15 | "); |
| 16 | 16 | |
| 17 | 17 | while ($schedule = $schedulesResponse->fetch_assoc()) { |
| 18 | - $calendarResponse = $sql->query(" |
|
| 18 | + $calendarResponse = $sql->query(" |
|
| 19 | 19 | SELECT * |
| 20 | 20 | FROM `calendars` |
| 21 | 21 | WHERE |
| 22 | 22 | `id` = '{$schedule['calendar']}' |
| 23 | 23 | "); |
| 24 | - if ($calendar = $calendarResponse->fetch_assoc()) { |
|
| 25 | - echo shell_exec('php ' . __DIR__ . '/import.php ' . $calendar['ics_url'] . ' ' . $calendar['canvas_url'] . ' ' . $schedule['id']); |
|
| 26 | - } |
|
| 24 | + if ($calendar = $calendarResponse->fetch_assoc()) { |
|
| 25 | + echo shell_exec('php ' . __DIR__ . '/import.php ' . $calendar['ics_url'] . ' ' . $calendar['canvas_url'] . ' ' . $schedule['id']); |
|
| 26 | + } |
|
| 27 | 27 | } |
@@ -7,24 +7,24 @@ |
||
| 7 | 7 | |
| 8 | 8 | class Toolbox extends \smtech\StMarksReflexiveCanvasLTI\Toolbox |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * Write messages to the log file if a script is being run |
|
| 12 | - * non-interactively from the command line |
|
| 13 | - * |
|
| 14 | - * @param string $subject |
|
| 15 | - * @param string $body |
|
| 16 | - * @param string $flag (Optional, default `NotificationMessage::INFO`) |
|
| 17 | - * @param Log $log (Optional, defaults to app log) |
|
| 18 | - */ |
|
| 19 | - public function smarty_addMessage($subject, $body, $flag = NotificationMessage::INFO, Log $log = null) |
|
| 20 | - { |
|
| 21 | - if (empty($log)) { |
|
| 22 | - $log = $this->getLog(); |
|
| 23 | - } |
|
| 24 | - if (php_sapi_name() == 'cli') { |
|
| 25 | - $log->log("[$flag] $subject: $body"); |
|
| 26 | - } else { |
|
| 27 | - parent::smarty_addMessage($subject, $body, $flag); |
|
| 28 | - } |
|
| 29 | - } |
|
| 10 | + /** |
|
| 11 | + * Write messages to the log file if a script is being run |
|
| 12 | + * non-interactively from the command line |
|
| 13 | + * |
|
| 14 | + * @param string $subject |
|
| 15 | + * @param string $body |
|
| 16 | + * @param string $flag (Optional, default `NotificationMessage::INFO`) |
|
| 17 | + * @param Log $log (Optional, defaults to app log) |
|
| 18 | + */ |
|
| 19 | + public function smarty_addMessage($subject, $body, $flag = NotificationMessage::INFO, Log $log = null) |
|
| 20 | + { |
|
| 21 | + if (empty($log)) { |
|
| 22 | + $log = $this->getLog(); |
|
| 23 | + } |
|
| 24 | + if (php_sapi_name() == 'cli') { |
|
| 25 | + $log->log("[$flag] $subject: $body"); |
|
| 26 | + } else { |
|
| 27 | + parent::smarty_addMessage($subject, $body, $flag); |
|
| 28 | + } |
|
| 29 | + } |
|
| 30 | 30 | } |
@@ -6,7 +6,7 @@ |
||
| 6 | 6 | |
| 7 | 7 | class CanvasContext extends Enum |
| 8 | 8 | { |
| 9 | - const COURSE = 'course'; |
|
| 10 | - const USER = 'user'; |
|
| 11 | - const GROUP = 'group'; |
|
| 9 | + const COURSE = 'course'; |
|
| 10 | + const USER = 'user'; |
|
| 11 | + const GROUP = 'group'; |
|
| 12 | 12 | } |