-
Notifications
You must be signed in to change notification settings - Fork 1
/
elevatorAPI.php
222 lines (165 loc) · 5.98 KB
/
elevatorAPI.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/**
* Elevator API access class
* But I don't know if the target moodle environment has modern libraries, so this seems like a safe bet.
*/
class elevatorAPI
{
private $userId = null;
private $apiKey = null;
private $baseURL = null;
public $fileTypes = null;
function __construct($elevatorURL, $apiKey, $apiSecret, $userId=null)
{
$this->baseURL = $elevatorURL;
$this->userId = $userId;
$this->apiKey = $apiKey;
$this->apiSecret = $apiSecret;
}
public function getLoginURL($callback) {
$now = time();
return $this->baseURL . "login/loginAndRedirect/" . $this->apiKey . "/" . $now . "/" . sha1($now . $this->apiSecret) . "/?" . $callback;
}
public function getManageURL() {
return $this->baseURL . "login/editUser/" . $this->userId;
}
/**
* isCurrentUserLogged in allows us to check if the user's session is currently cached on elevator.
* This is because elevator is currently reliant entirely on shib sessions for getting things like course enrollment data.
* If this becomes a big issue, we may want to move to using ldap on the elevator side for that, but I'm hoping to avoid that
* dependency for now.
*/
function isCurrentUserLoggedIn($userId) {
$request = "hello";
return $this->execute($request, null, $userId);
}
function getAssetsFromDrawer($drawerId, $pageNumber=0) {
$request = "api_drawers/getContentsOfDrawer/" . $drawerId . "/" . ($pageNumber-1) . "/" . $this->fileTypes;
$assetList = array();
$result = $this->execute($request);
if ($result) {
error_log($result);
$assetList = json_decode($result, true);
}
return $assetList;
}
function getAssetsFromCollection($collectionId, $pageNumber=0) {
$request = "collections/getContentsOfCollection/" . $collectionId . "/" . ($pageNumber-1) . "/" . $this->fileTypes;
$assetList = array();
$result = $this->execute($request);
if ($result) {
$assetList = json_decode($result, true);
}
return $assetList;
}
/**
* In Elevator, a single asset may contain many files. Moodle doesn't have a great way to display that, so instead
* in those cases we display the "parent" asset as a folder and place all the files inside it.
*/
function getAssetChildren($objectId) {
$request = "asset/getAssetChildren/" . $objectId . "/" . $this->fileTypes;
$assetList = array();
$result = $this->execute($request);
if ($result) {
$assetList = json_decode($result, true);
}
return $assetList;
}
function getEmbedContent($objectId, $instance=null, $excerpt=null) {
if ($excerpt) {
$request = "asset/getExcerptLink/" . $excerpt . "/" . $instance;
}
else {
$request = "asset/getEmbedLink/" . $objectId . "/" . $instance;
}
$assetLink = "";
$result = $this->execute($request);
if ($result) {
$assetLink = $result;
}
return $assetLink;
}
function search($searchTerm, $pageNumber = 0) {
$request = "search/performSearch/";
$postArray['searchTerm'] = $searchTerm;
$postArray['pageNumber'] = ($pageNumber - 1); // we keep moodle 1 indexed and fix here
$assetList = array();
$result = $this->execute($request, $postArray);
if ($result) {
$assetList = json_decode($result, true);
}
return $assetList;
}
function assetLookup($assetId) {
$request = "asset/assetLookup/" . $assetId;
$assetInfo = array();
$result = $this->execute($request);
if ($result) {
$assetInfo = json_decode($result, true);
}
return $assetInfo;
}
function assetPreview($assetId) {
$request = "asset/assetPreview/" . $assetId;
$assetInfo = array();
$result = $this->execute($request);
if ($result) {
$assetInfo = json_decode($result, true);
}
return $assetInfo;
}
function fileLookup($assetId) {
$request = "asset/fileLookup/" . $assetId;
$assetInfo = array();
$result = $this->execute($request);
if ($result) {
$assetInfo = json_decode($result, true);
}
return $assetInfo;
}
function getDrawers() {
$request = "api_drawers/listDrawers";
$drawerList = array();
$result = $this->execute($request);
if ($result) {
$drawerList = json_decode($result, true);
}
return $drawerList;
}
function getCollections() {
$request = "collections/listCollections";
$collectionList = array();
$result = $this->execute($request);
if ($result) {
$collectionList = json_decode($result, true);
}
return $collectionList;
}
private function execute($targetURL=null, $postArray=null, $userId=null) {
if (!$userId) {
$userId = $this->userId;
}
if(!$targetURL) {
return false;
}
$now = time();
$header = array("Authorization-User"=>$userId, "Authorization-Key" => $this->apiKey, "Authorization-Timestamp" => $now, "Authorization-Hash" => sha1($now . $this->apiSecret));
$args = array(
'body' => $postArray,
'timeout' => '5',
'redirection' => '5',
'httpversion' => '1.0',
'blocking' => true,
'headers' => $header,
'cookies' => array()
);
$response = $response = wp_remote_post($this->baseURL . $targetURL, $args);
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
echo "Something went wrong: $error_message";
return false;
} else {
return $response["body"];
}
}
}