|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* A Tool Provider that can handle LTI requests |
|
5
|
|
|
* |
|
6
|
|
|
* @author Seth Battis <[email protected]> |
|
7
|
|
|
**/ |
|
8
|
|
|
class CanvasAPIviaLTI extends LTI_Tool_Provider { |
|
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Handle launch requests, which start the application running |
|
12
|
|
|
**/ |
|
13
|
|
|
public function onLaunch() { |
|
|
|
|
|
|
14
|
|
|
global $metadata; // FIXME:0 grown-ups don't program like this issue:17 |
|
|
|
|
|
|
15
|
|
|
global $sql; // FIXME:0 grown-ups don't program like this issue:17 |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/* is this user in a role that can use this app? */ |
|
18
|
|
|
if ($this->user->isAdmin()) { |
|
19
|
|
|
|
|
20
|
|
|
/* set up any needed session variables */ |
|
21
|
|
|
$_SESSION['consumer_key'] = $this->consumer->getKey(); |
|
22
|
|
|
$_SESSION['resource_id'] = $this->resource_link->getId(); |
|
23
|
|
|
$_SESSION['user_consumer_key'] = $this->user->getResourceLink()->getConsumer()->getKey(); |
|
24
|
|
|
$_SESSION['user_id'] = $this->user->getId(); |
|
25
|
|
|
$_SESSION['isStudent'] = $this->user->isLearner(); |
|
26
|
|
|
$_SESSION['isContentItem'] = FALSE; |
|
27
|
|
|
|
|
28
|
|
|
/* do we have an admin API access token? */ |
|
29
|
|
|
$haveToken = true; |
|
30
|
|
|
if (empty($metadata['CANVAS_API_TOKEN'])) { |
|
31
|
|
|
|
|
32
|
|
|
/* ...if not, do we have a user API access token for this user? */ |
|
33
|
|
|
$userToken = new UserAPIToken($_SESSION['user_consumer_key'], $_SESSION['user_id'], $sql); |
|
34
|
|
|
if (empty($userToken->getToken())) { |
|
35
|
|
|
|
|
36
|
|
|
/* ...if this user has no token, let's start by getting one */ |
|
37
|
|
|
$haveToken = false; |
|
38
|
|
|
$this->redirectURL = "{$metadata['APP_URL']}/lti/token_request.php?oauth=request"; |
|
39
|
|
|
} else { |
|
40
|
|
|
|
|
41
|
|
|
/* ...but if the user does have a token, rock on! */ |
|
42
|
|
|
$_SESSION['isUserToken'] = true; |
|
43
|
|
|
$_SESSION['apiToken'] = $userToken->getToken(); |
|
44
|
|
|
//$_SESSION['apiUrl'] = $userToken->getAPIUrl(); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
} else { |
|
47
|
|
|
|
|
48
|
|
|
/* ...if we have an admin API token, rock on! */ |
|
49
|
|
|
$_SESSION['isUserToken'] = false; |
|
50
|
|
|
$_SESSION['apiToken'] = $metadata['CANVAS_API_TOKEN']; |
|
51
|
|
|
//$_SESSION['apiUrl'] = $metadata['CANVAS_API_URL']; |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
$_SESSION['apiUrl'] = 'https://' . $this->user->getResourceLink()->settings['custom_canvas_api_domain'] . '/api/v1'; |
|
54
|
|
|
|
|
55
|
|
|
/* pass control off to the app */ |
|
56
|
|
|
if ($haveToken) { |
|
57
|
|
|
$this->redirectURL = "{$metadata['APP_URL']}/app.php?lti-request=launch"; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/* ...otherwise set an appropriate error message and fail */ |
|
61
|
|
|
} else { |
|
62
|
|
|
$this->reason = 'Invalid role'; |
|
63
|
|
|
$this->isOK = false; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Handle errors created while processing the LTI request |
|
69
|
|
|
**/ |
|
70
|
|
|
public function onError() { |
|
71
|
|
|
global $metadata; // FIXME:0 grown-ups don't program like this issue:17 |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
$this->redirectURL = "{$metadata['APP_URL']}/app.php?lti-request=error&reason={$this->reason}"; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Handle dashboard requests (coming in LTI v2.0, I guess) |
|
78
|
|
|
**/ |
|
79
|
|
|
public function onDashboard() { |
|
80
|
|
|
global $metadata; // FIXME:0 grown-ups don't program like this issue:17 |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
$this->redirectURL = "{$metadata['APP_URL']}/app.php?lti-request=dashboard"; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Handle configure requests (coming in LTI v2.0, I guess) |
|
87
|
|
|
**/ |
|
88
|
|
|
public function onConfigure() { |
|
89
|
|
|
global $metadata; // FIXME:0 grown-ups don't program like this issue:17 |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
$this->redirectURL = "{$metadata['APP_URL']}/app.php?lti-request=configure"; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Handle content-item requests (that is we're a tool provider that adds a button in the content editor) |
|
96
|
|
|
**/ |
|
97
|
|
|
public function onContentItem() { |
|
98
|
|
|
global $metadata; // FIXME:0 grown-ups don't program like this issue:17 |
|
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
$this->redirectURL = "{$metadata['APP_URL']}/app.php?lti-request=content-item"; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Exceptions thrown by CanvasAPIviaLTI |
|
106
|
|
|
* |
|
107
|
|
|
* @author Seth Battis <[email protected]> |
|
108
|
|
|
**/ |
|
109
|
|
|
class CanvasAPIviaLTI_Exception extends Exception { |
|
|
|
|
|
|
110
|
|
|
const MISSING_SECRETS_FILE = 1; |
|
111
|
|
|
const INVALID_SECRETS_FILE = 2; |
|
112
|
|
|
const MYSQL_CONNECTION = 3; |
|
113
|
|
|
const LAUNCH_REQUEST = 4; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
?> |
|
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.