forked from schoology/schoology_php_sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.php
400 lines (389 loc) · 12.2 KB
/
example.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<?php
require_once(dirname(__FILE__).'/SchoologyApi.class.php');
Class Request
{
function __construct()
{
$this->_token_key = '3031d64a01bc262ee9504ed29c6e1eee0610be9c8';
$this->_token_secret = 'd9826b46c3f46d4aa3e83373b570ba59';
$this->_two_legged = true;
$this->schoology = null;
$this->uid = null;
}
public function requestAuthHeader()
{
//xxxxx
}
public function requestSecretKeys ()
{
if (isset($_SESSION) && isset($_SESSION['user_id']))
{
$uid = $_SESSION['user_id'];
}
else
{
$uid = 0;
}
// Use hard-coded access tokens
$this->_token_key = '3031d64a01bc262ee9504ed29c6e1eee0610be9c8';
$this->_token_secret = 'd9826b46c3f46d4aa3e83373b570ba59';
return true;
}
public function requestAccessTokens()
{
if (!empty($this->_token_key) && !empty($this->_token_secret))
{
try
{
$this->apiResult('users/me');
}
catch (Exception $err)
{
if ($err->getCode() == 401)
{
unset($this->_token_key, $this->_token_secret);
echo "<p style='color: red;'>Your access token key/secret did not work.";
return false;
}
}
//$this->schoology = new SchoologyApi(SCHOOLOGY_CONSUMER_KEY, SCHOOLOGY_CONSUMER_SECRET, '', '','', TRUE);
}
}
public function assignAdditionalSchoolsBulk($schools, $users)
{
if (empty($schools))
{
throw new Exception('You must input a comma-delimited string of school id\'s.');
}
$usersArray = explode(',', $users);
$userChildObjs = array();
foreach ($usersArray as $user)
{
$tmpUser = new stdClass();
$tmpUser->id = $user;
$tmpUser->additional_buildings = $schools;
array_push($userChildObjs, $tmpUser);
unset($tmpUser);
}
$usersObj = new stdClass();
$usersObj->users = new stdClass();
$usersObj->users->user = $userChildObjs;
$url = 'users';
/* print_r(json_encode((array) $usersObj, JSON_PRETTY_PRINT));
return false; */
$this->schoology = new SchoologyApi($this->_token_key, $this->_token_secret, '', '','', TRUE);
$response = $this->schoology->api($url, 'PUT', (array) $usersObj);
if ($response)
{
if (isset($response->result) && isset($response->http_code) && (int) $response->http_code > 199 && (int) $response->http_code < 300)
{
if (isset($response->result->user))
{
return true;
}
}
else
{
echo "<br/>Error with assigning additional buildings $schools to users: $users. Response: <br/><pre><code>";
print_r(json_encode($response, JSON_PRETTY_PRINT));
echo "</code></pre>";
throw new Exception("Error. Failed to assign additional buildings.");
}
}
else
{
throw new Exception("Failed to bulk update schools. schools: $schools. users: $users");
}
return false;
}
public function assignPrimarySchoolBulk($school=null, $users)
{
if (!is_numeric($school))
{
throw new Exception("You can only enter one primary school id. It must be numeric.");
return false;
}
$usersArray = explode(',', $users);
$userChildObjs = array();
foreach ($usersArray as $user)
{
$tmpUser = new stdClass();
$tmpUser->id = $user;
$tmpUser->building_id = $school;
array_push($userChildObjs, $tmpUser);
unset($tmpUser);
}
$usersObj = new stdClass();
$usersObj->users = new stdClass();
$usersObj->users->user = $userChildObjs;
$url = 'users';
/* print_r(json_encode((array) $usersObj, JSON_PRETTY_PRINT));
return false; */
$this->schoology = new SchoologyApi($this->_token_key, $this->_token_secret, '', '','', TRUE);
$response = $this->schoology->api($url, 'PUT', (array) $usersObj);
if ($response)
{
if (isset($response->result) && isset($response->http_code) && (int) $response->http_code > 199 && (int) $response->http_code < 300)
{
if (isset($response->result->user))
{
return true;
}
}
else
{
echo "<br/>Error with assigning additional buildings $school to users: $users. Response: <br/><pre><code>";
print_r(json_encode($response, JSON_PRETTY_PRINT));
echo "</code></pre>";
throw new Exception("Error. Failed to assign additional buildings.");
}
}
else
{
throw new Exception("Failed to bulk update schools. schools: $school. users: $users");
}
return false;
}
public function genericRequest ($url, $json=false)
{
$this->schoology = new SchoologyApi($this->_token_key, $this->_token_secret, '', '','', TRUE);
$response = $this->schoology->api($url);
if ($response)
{
if ($response && isset($response->result))
{
if ($json && isset($response->raw_result))
return $response->raw_result;
else
return $response->result;
}
else
{
return $response;
}
}
else
{
throw new Exception("Failed to retrieve a valid Schoology internal user id.");
}
}
public function getUserIdFromUniqueId ($uniqueId)
{
$this->schoology = new SchoologyApi($this->_token_key, $this->_token_secret, '', '','', TRUE);
$response = $this->schoology->api("users/ext/$uniqueId");
if ($response && isset($response->redirect_url) && !empty($response->redirect_url))
{
$idArray = array();
$this->uid = preg_match('~[\d]+$~', $response->redirect_url, $idArray);
/* echo "<pre><code>idArray matches:<br/>";
print_r($idArray);
return true; */
if (isset($idArray[0]) && is_numeric($idArray[0]))
{
$this->uid = $idArray[0];
// throw new Exception("<br/>Fonud user id: ".$this->uid);
return $this->uid;
}
else
{
throw new Exception("Failed to retrieve a valid Schoology internal user id.");
}
}
else
{
throw new Exception("Failed to retrieve a valid Schoology internal user id.");
}
return false;
}
}
$email = isset($_REQUEST['email']) ? filter_var($_REQUEST['email'], FILTER_VALIDATE_EMAIL) : null;
if (isset($_REQUEST['singleuser']) && $_REQUEST['singleuser'] === 'true' && $email)
{
$readonly = $email ? 'readonly' : '';
echo "<h4>User update page</h4><br/><br/><form method='POST' action='' id='begin'>
User email: <input type='text' id='email' name='email' value='$email' placeholder='$email' ".$readonly." />
<input type='hidden' id='singleuser' name='singleuser' value='true' /><br/>
<input type='submit' value='Null' /><br/>
</form>";
if ($email)
{
echo "<form id='resetForm' action='' method='POST'>
<input type='submit' id='reset' value='Reset' />
</form>";
}
echo "<pre><code>";
print_r($_REQUEST);
echo "</code><pre>";
$api = new Request();
$email = $_REQUEST['email'];
try
{
$uidRequest = $api->getUserIdFromUniqueId($email);
} catch (Exception $e)
{
echo "<br/>That user was not found.";
exit();
}
if ($uidRequest)
{
echo "User id: $uidRequest<br/>";
/* echo "<pre><code>";
print_r(json_encode($uidRequest,JSON_PRETTY_PRINT));
echo "</code></pre>"; */
}
else
{
echo "User api request failed.";
exit();
}
/* Make school update before populatidng the form fields. */
if (isset($_REQUEST['updateready']) && $_REQUEST['updateready'] === 'true' &&
isset($_REQUEST['primary_school_string']) &&
isset($_REQUEST['additional_schools_string'])
)
{
$priSchool = $_REQUEST['primary_school_string'];
$addSchools = $_REQUEST['additional_schools_string'];
if(!preg_match('~[,[:blank:][:digit:]]+~', $priSchool) || !preg_match('~[,[:blank:][:digit:]]+~', $addSchools))
{
echo "<br/>The school codes entered must be comma-delimited numbers only.";
exit();
}
$user = (string) $api->uid;
$priSchoolUpdate = $api->assignPrimarySchoolBulk($priSchool, $user);
$additionalSchoolUpdates = $api->assignAdditionalSchoolsBulk($addSchools, $user);
}
$userReq = "users/$uidRequest";
$userInfo = $api->genericRequest($userReq);
/* echo "<br/>User info for $email:<br/>";
echo '<br/>Response:<br/><pre><code>';
print_r($userInfo);
echo '</code></pre><br/>'; */
$userPrimaryBldg = null;
$userAdditionalBldgs = null;
if (isset($userInfo->building_id) && isset($userInfo->additional_buildings))
{
$userPrimaryBldg = $userInfo->building_id;
$userAdditionalBldgs = $userInfo->additional_buildings;
}
echo "<form id='schoologyUpdateForm' action='' method='POST'>
<input type='hidden' id='useremail' name='email' value='$email' />
Primary School: <input id='priBuilding' type='text' style='width: 200px;' name='primary_school_string' placeholder=''>
Additional Schools: <input id='addBuildings' type='text' style='width: 200px;' name='additional_schools_string' placeholder=''><br/>
<input type='hidden' id='singleuser' name='singleuser' value='true' />
<input type='hidden' id='updateusers' name='updateready' value='true' />
<input type='submit' value='Update' />
</form>
<script type='text/javascript'>
var priB = document.getElementById('priBuilding');
var addB = document.getElementById('addBuildings');
priB.value = '$userPrimaryBldg';
addB.value = '$userAdditionalBldgs';
</script>";
if (isset($_REQUEST['updateready']) && $_REQUEST['updateready'] === 'true' &&
isset($_REQUEST['primary_school_string']) &&
isset($_REQUEST['additional_schools_string'])
)
{
/* $priSchool = $_REQUEST['primary_school_string'];
$addSchools = $_REQUEST['additional_schools_string'];
if(!preg_match('~[,[:blank:][:digit:]]+~', $priSchool) || !preg_match('~[,[:blank:][:digit:]]+~', $addSchools))
{
echo "<br/>The school codes entered must be comma-delimited numbers only.";
exit();
}
$user = (string) $api->uid;
$priSchoolUpdate = $api->assignPrimarySchoolBulk($priSchool, $user);
$additionalSchoolUpdates = $api->assignAdditionalSchoolsBulk($addSchools, $user); */
if ($priSchoolUpdate)
{
echo "<br/>Assigning primary school $priSchool to user: $user. Response: <pre><code>";
print_r(json_encode($priSchoolUpdate, JSON_PRETTY_PRINT));
echo "</code></pre>";
}
if ($additionalSchoolUpdates)
{
echo "<br/>Assigning additional schools $addSchools to user: $user. Response: <pre><code>";
print_r(json_encode($additionalSchoolUpdates, JSON_PRETTY_PRINT));
echo "</code></pre>";
}
}
echo "<br/><br/><br/>User info for $email:<br/>";
$userReq = "users/$uidRequest";
$userData = $api->genericRequest($userReq);
echo '<br/>Response:<br/><pre><code>';
print_r($userData);
echo '</code></pre><br/>';
exit();
}
else
{
echo '<h4>User update page</h4><br/><br/><form method="POST" action="" id="schoologyUpdateForm">
User email: <input type="text" id="email" name="email" placeholder="[email protected]" />
<input type="hidden" id="singleuser" name="singleuser" value="true" />
<input type="submit" value="Continue" />
</form>';
echo "<pre><code>";
print_r($_REQUEST);
exit();
}
exit('Done');
//***************
echo "<br/>REquesting ....";
$email = '[email protected]';
//$req = 'users/109529517/sections';
$myInfo = new Request();
if (!$myInfo->getUserIdFromUniqueId($email))
{
exit('Failed to retrieve a valid user id for this email: '.$email);
}
if (!$myInfo->uid || !is_numeric($myInfo->uid))
{
exit("No valid user id");
}
if (isset($myInfo->uid) && $myInfo->uid)
{
$uid = $myInfo->uid;
}
else
{
exit("No valid user id");
}
$userReq = "users/$uid";
echo "<br/>User info for $email:<br/>";
$userInfo = $myInfo->genericRequest($userReq);
echo '<br/>Response:<br/><pre><code>';
print_r($userInfo);
echo '</code></pre><br/>';
$req = "users/".$myInfo->uid.'/sections';
echo "<br/>Request: $req, Fetching user data:<br/>";
$sections = $myInfo->genericRequest($req);
echo '<br/>Response:<br/><pre><code>';
//print_r($sections);
echo '</code></pre><br/>';
$sch = "schools/15433069/buildings";
echo "<br/>Fetching Schools info '$sch':<br/>";
$schools = $myInfo->genericRequest($sch);
echo '<br/>All schools for 15433069:<br/><pre><code>';
/*print_r($schools);
exit(); */
foreach ($schools->building as $key => $building)
{
$bldg_code = $building->building_code ? $building->building_code : 'none';
echo "<br/>".$building->title.", Internal id: ".$building->id.', Building code: '.$bldg_code;
}
echo "<br/><br/>Users Obj:<br/>";
/* Bulk update schools */
//$schools = '102709161,109029373,116302935';
$schools = '102493601';
$users = '10101,20202,30303';
$users = (string) $myInfo->uid;
$schoolUpdates = $myInfo->assignAdditionalSchoolsBulk($schools, $users);
if ($schoolUpdates)
{
echo "<br/>Assigning additional buildings $schools to users: $users. Response: <br/><pre><code>";
print_r(json_encode($schoolUpdates, JSON_PRETTY_PRINT));
echo "</code></pre>";
}
echo "Finished....";