LTI Integration Library  3.1.0
PHP class library for building LTI integrations
CanvasApi.php
Go to the documentation of this file.
1 <?php
2 
4 
6 use ceLTIc\LTI\HttpMessage;
7 
17 trait CanvasApi
18 {
19 
23  private static $DEFAULT_PER_PAGE = 50;
24 
28  private $domain = null;
29 
33  private $token = null;
34 
38  private $courseId = null;
39 
43  private $sourceObject = null;
44 
52  private function get($withGroups)
53  {
54  $consumer = $this->sourceObject->getConsumer();
55  $this->domain = $consumer->getSetting('canvas.domain');
56  $this->token = $consumer->getSetting('canvas.token');
57  $this->courseId = $this->sourceObject->getSetting('custom_canvas_course_id');
58  $perPage = $consumer->getSetting('canvas.per_page', strval(self::$DEFAULT_PER_PAGE));
59  if (!is_numeric($perPage)) {
60  $perPage = self::$DEFAULT_PER_PAGE;
61  }
62  $prefix = $consumer->getSetting('canvas.group_set_prefix');
63  if ($this->domain && $this->token && $this->courseId) {
64  if ($withGroups) {
65  $this->setGroupSets($perPage, $prefix);
66  }
67  $users = $this->getUsers($perPage, $withGroups);
68  if ($withGroups) {
69  $this->setGroups($perPage, $users);
70  }
71  } else {
72  $users = false;
73  }
74 
75  return $users;
76  }
77 
84  private function setGroupSets($perPage, $prefix)
85  {
86  $this->sourceObject->groupSets = array();
87  $url = "https://{$this->domain}/api/v1/courses/{$this->courseId}/group_categories";
88  if ($perPage > 0) {
89  $url .= "?per_page={$perPage}";
90  }
91  do {
92  $http = new HttpMessage($url, 'GET', null, "Authorization: Bearer {$this->token}");
93  $http->send();
94  if ($http->ok) {
95  $allCategories = json_decode($http->response);
96  $http->ok = !is_null($allCategories) && is_array($allCategories);
97  }
98  $url = '';
99  if ($http->ok) {
100  foreach ($allCategories as $category) {
101  if (empty($prefix) || (strpos($category->name, $prefix) === 0)) {
102  $this->sourceObject->groupSets[strval($category->id)] = array('title' => $category->name, 'groups' => array(),
103  'num_members' => 0, 'num_staff' => 0, 'num_learners' => 0);
104  }
105  }
106  if (preg_match('/<([^>]+)>; *rel=\"next\"/', $http->responseHeaders, $matches)) {
107  $url = $matches[1];
108  }
109  }
110  } while ($url);
111  }
112 
120  private function getRoles($perPage)
121  {
122  $roles = array();
123 
124  $url = "https://{$this->domain}/api/v1/courses/{$this->courseId}/enrollments?state[]=invited&state[]=active&state[]=completed";
125  if ($perPage > 0) {
126  $url .= "&per_page={$perPage}";
127  }
128  do {
129  $http = new HttpMessage($url, 'GET', null, "Authorization: Bearer {$this->token}");
130  $http->send();
131  if ($http->ok) {
132  $enrolments = json_decode($http->response);
133  $http->ok = !is_null($enrolments) && is_array($enrolments);
134  }
135  $url = '';
136  if ($http->ok) {
137  foreach ($enrolments as $enrolment) {
138  $roles[strval($enrolment->user_id)] = $enrolment->type;
139  }
140  if (preg_match('/<([^>]+)>; *rel=\"next\"/', $http->responseHeaders, $matches)) {
141  $url = $matches[1];
142  }
143  } else {
144  $roles = false;
145  }
146  } while ($url);
147 
148  return $roles;
149  }
150 
159  private function getUsers($perPage, $withGroups)
160  {
161  $users = array();
162 
163  $url = "https://{$this->domain}/api/v1/courses/{$this->courseId}/users?state[]=invited&state[]=active&state[]=completed";
164  if ($perPage > 0) {
165  $url .= "&per_page={$perPage}";
166  }
167  if ($withGroups) {
168  $url .= '&include[]=group_ids';
169  }
170  $roles = $this->getRoles($perPage);
171  do {
172  $http = new HttpMessage($url, 'GET', null, "Authorization: Bearer {$this->token}");
173  $http->send();
174  if ($http->ok) {
175  $enrolments = json_decode($http->response);
176  $http->ok = !is_null($enrolments) && is_array($enrolments);
177  }
178  $url = '';
179  if ($http->ok) {
180  foreach ($enrolments as $enrolment) {
181  $userId = strval($enrolment->id);
182  if (array_key_exists($userId, $users)) {
183  $user = $users[$userId];
184  } else {
185  if (is_a($this->sourceObject, 'ceLTIc\LTI\ResourceLink')) {
186  $user = UserResult::fromResourceLink($this->sourceObject, $userId);
187  } else {
188  $user = new UserResult();
189  $user->ltiUserId = $userId;
190  }
191  }
192  $user->setNames('', '', $enrolment->name);
193  $user->setEmail($enrolment->email, $this->sourceObject->getConsumer()->defaultEmail);
194  if (!empty($enrolment->group_ids)) {
195  foreach ($enrolment->group_ids as $groupId) {
196  $user->groups[] = strval($groupId);
197  }
198  }
199  if (array_key_exists($userId, $roles)) {
200  switch ($roles[$userId]) {
201  case 'StudentEnrollment':
202  $user->roles[] = 'urn:lti:role:ims/lis/Learner';
203  break;
204  case 'TeacherEnrollment':
205  $user->roles[] = 'urn:lti:role:ims/lis/Instructor';
206  break;
207  case 'TaEnrollment':
208  $user->roles[] = 'urn:lti:role:ims/lis/TeachingAssistant';
209  break;
210  case 'DesignerEnrollment':
211  $user->roles[] = 'urn:lti:role:ims/lis/ContentDeveloper';
212  break;
213  case 'ObserverEnrollment':
214  $user->roles[] = 'urn:lti:instrole:ims/lis/Observer';
215  $user->roles[] = 'urn:lti:role:ims/lis/Mentor';
216  break;
217  }
218  }
219  $users[$userId] = $user;
220  }
221  if (preg_match('/<([^>]+)>; *rel=\"next\"/', $http->responseHeaders, $matches)) {
222  $url = $matches[1];
223  }
224  } else {
225  $users = false;
226  }
227  } while ($url);
228 
229  return $users;
230  }
231 
238  private function setGroups($perPage, $users)
239  {
240  $this->sourceObject->groups = array();
241  $url = "https://{$this->domain}/api/v1/courses/{$this->courseId}/groups";
242  if ($perPage > 0) {
243  $url .= "?per_page={$perPage}";
244  }
245  do {
246  $http = new HttpMessage($url, 'GET', null, "Authorization: Bearer {$this->token}");
247  $http->send();
248  if ($http->ok) {
249  $allGroups = json_decode($http->response);
250  $http->ok = !is_null($allGroups) && is_array($allGroups);
251  }
252  $url = '';
253  if ($http->ok) {
254  foreach ($allGroups as $group) {
255  $setId = strval($group->group_category_id);
256  if (array_key_exists($setId, $this->sourceObject->groupSets)) {
257  $groupId = strval($group->id);
258  $this->sourceObject->groups[$groupId] = array('title' => $group->name, 'set' => $setId);
259  foreach ($users as $user) {
260  if (in_array($groupId, $user->groups)) {
261  $this->sourceObject->groupSets[$setId]['num_members'] ++;
262  if ($user->isStaff()) {
263  $this->sourceObject->groupSets[$setId]['num_staff'] ++;
264  }
265  if ($user->isLearner()) {
266  $this->sourceObject->groupSets[$setId]['num_learners'] ++;
267  }
268  if (!in_array($groupId, $this->sourceObject->groupSets[$setId]['groups'])) {
269  $this->sourceObject->groupSets[$setId]['groups'][] = $groupId;
270  }
271  }
272  }
273  }
274  }
275  if (preg_match('/<([^>]+)>; *rel=\"next\"/', $http->responseHeaders, $matches)) {
276  $url = $matches[1];
277  }
278  }
279  } while ($url);
280  }
281 
282 }
static fromResourceLink($resourceLink, $ltiUserId)
Class constructor from resource link.
Definition: UserResult.php:255
Class to represent a tool consumer user.
Definition: UserResult.php:15
Class to handle Canvas web service requests.
Definition: CanvasApi.php:17