forked from schoology/schoology_php_sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schools.php
271 lines (263 loc) · 7.56 KB
/
schools.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
<?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;
}
}
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/>';
/* All Schools */
$schreq = 'schools'; // 'schools/{id}'
echo "<br/>Fetching All Schools:<br/>";
$schoolsResult = $myInfo->genericRequest($schreq);
echo '<br/>All Schools:<br/><pre><code>';
print_r($schoolsResult);
echo "</code></pre>";
/* All buildings */
$campuses = "schools/15433069/buildings";
echo "<br/>Fetching All Buldings/Campuses '$campuses':<br/>";
$campusesResult = $myInfo->genericRequest($campuses);
echo '<br/>All buildings for 15433069:<br/><pre><code>';
print_r($campusesResult);
echo '</code></pre><br/>';
foreach ($campusesResult->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....";