1
|
|
|
<?php |
|
|
|
|
2
|
|
|
require_once 'common.inc.php'; |
3
|
|
|
|
4
|
|
|
use smtech\CanvasICSSync\Toolbox; |
5
|
|
|
use smtech\ReflexiveCanvasLTI\LTI\ToolProvider; |
6
|
|
|
use smtech\ReflexiveCanvasLTI\Exception\ConfigurationException; |
7
|
|
|
|
8
|
|
|
define('ACTION_CONFIG', 'config'); |
9
|
|
|
define('ACTION_INSTALL', 'install'); |
10
|
|
|
define('ACTION_CONSUMERS', 'consumers'); |
11
|
|
|
define('ACTION_UNSPECIFIED', false); |
12
|
|
|
|
13
|
|
|
/* store any requested actions for future handling */ |
14
|
|
|
$action = (empty($_REQUEST['action']) ? |
15
|
|
|
ACTION_UNSPECIFIED : |
16
|
|
|
strtolower($_REQUEST['action']) |
17
|
|
|
); |
18
|
|
|
|
19
|
|
|
/* action requests only come from outside the LTI! */ |
20
|
|
|
if ($action) { |
21
|
|
|
unset($_SESSION[ToolProvider::class]); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/* authenticate LTI launch request, if present */ |
25
|
|
|
if ($toolbox->lti_isLaunching()) { |
26
|
|
|
$toolbox->resetSession(); |
27
|
|
|
$toolbox->lti_authenticate(); |
28
|
|
|
exit; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/* if authenticated LTI launch, get the current user profile */ |
32
|
|
|
if (!empty($_SESSION[ToolProvider::class]['canvas'])) { |
33
|
|
|
try { |
34
|
|
|
$profile = $toolbox->api_get('users/' . $_SESSION[ToolProvider::class]['canvas']['user_id'] . '/profile'); |
35
|
|
|
} catch (Exception $e) { |
36
|
|
|
$toolbox->smarty_addMessage( |
37
|
|
|
'Error', |
38
|
|
|
json_decode($e->getMessage(), true), |
39
|
|
|
NotificationMessage::DANGER |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
$toolbox->smarty_assign([ |
43
|
|
|
'profile' => $profile, |
44
|
|
|
]); |
45
|
|
|
header('Location: import.php'); |
46
|
|
|
exit; |
47
|
|
|
|
48
|
|
|
/* if not authenticated, default to showing credentials */ |
49
|
|
|
} else { |
50
|
|
|
$action = (empty($action) ? |
51
|
|
|
ACTION_CONFIG : |
52
|
|
|
$action |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/* process any actions */ |
57
|
|
|
switch ($action) { |
58
|
|
|
/* reset cached install data from config file */ |
59
|
|
|
case ACTION_INSTALL: |
60
|
|
|
$_SESSION['toolbox'] = Toolbox::fromConfiguration(CONFIG_FILE, true); |
61
|
|
|
$toolbox =& $_SESSION['toolbox']; |
62
|
|
|
|
63
|
|
|
/* test to see if we can connect to the API */ |
64
|
|
|
try { |
65
|
|
|
$toolbox->getAPI(); |
66
|
|
|
} catch (ConfigurationException $e) { |
67
|
|
|
/* if there isn't an API token in config.xml, are there OAuth credentials? */ |
68
|
|
|
if ($e->getCode() === ConfigurationException::CANVAS_API_INCORRECT) { |
69
|
|
|
$toolbox->interactiveGetAccessToken(); |
|
|
|
|
70
|
|
|
exit; |
71
|
|
|
} else { /* no (understandable) API credentials available -- doh! */ |
72
|
|
|
throw $e; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/* load the the database schema */ |
77
|
|
|
$toolbox->loadSchema(); |
|
|
|
|
78
|
|
|
|
79
|
|
|
/* finish by opening consumers control panel */ |
80
|
|
|
header('Location: consumers.php'); |
81
|
|
|
exit; |
82
|
|
|
|
83
|
|
|
/* show LTI configuration XML file */ |
84
|
|
|
case ACTION_CONFIG: |
85
|
|
|
header('Content-type: application/xml'); |
86
|
|
|
echo $toolbox->saveConfigurationXML(); |
87
|
|
|
exit; |
88
|
|
|
} |
89
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.