6 use ceLTIc\LTI\HttpMessage;
23 private static $DEFAULT_PER_PAGE = 50;
28 private $domain =
null;
33 private $token =
null;
38 private $courseId =
null;
43 private $sourceObject =
null;
52 private function get($withGroups)
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;
62 $prefix = $consumer->getSetting(
'canvas.group_set_prefix');
63 if ($this->domain && $this->token && $this->courseId) {
65 $this->setGroupSets($perPage, $prefix);
67 $users = $this->getUsers($perPage, $withGroups);
69 $this->setGroups($perPage, $users);
84 private function setGroupSets($perPage, $prefix)
86 $this->sourceObject->groupSets = array();
87 $url =
"https://{$this->domain}/api/v1/courses/{$this->courseId}/group_categories";
89 $url .=
"?per_page={$perPage}";
92 $http =
new HttpMessage($url,
'GET',
null,
"Authorization: Bearer {$this->token}");
95 $allCategories = json_decode($http->response);
96 $http->ok = !is_null($allCategories) && is_array($allCategories);
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);
106 if (preg_match(
'/<([^>]+)>; *rel=\"next\"/', $http->responseHeaders, $matches)) {
120 private function getRoles($perPage)
124 $url =
"https://{$this->domain}/api/v1/courses/{$this->courseId}/enrollments?state[]=invited&state[]=active&state[]=completed";
126 $url .=
"&per_page={$perPage}";
129 $http =
new HttpMessage($url,
'GET',
null,
"Authorization: Bearer {$this->token}");
132 $enrolments = json_decode($http->response);
133 $http->ok = !is_null($enrolments) && is_array($enrolments);
137 foreach ($enrolments as $enrolment) {
138 $roles[strval($enrolment->user_id)] = $enrolment->type;
140 if (preg_match(
'/<([^>]+)>; *rel=\"next\"/', $http->responseHeaders, $matches)) {
159 private function getUsers($perPage, $withGroups)
163 $url =
"https://{$this->domain}/api/v1/courses/{$this->courseId}/users?state[]=invited&state[]=active&state[]=completed";
165 $url .=
"&per_page={$perPage}";
168 $url .=
'&include[]=group_ids';
170 $roles = $this->getRoles($perPage);
172 $http =
new HttpMessage($url,
'GET',
null,
"Authorization: Bearer {$this->token}");
175 $enrolments = json_decode($http->response);
176 $http->ok = !is_null($enrolments) && is_array($enrolments);
180 foreach ($enrolments as $enrolment) {
181 $userId = strval($enrolment->id);
182 if (array_key_exists($userId, $users)) {
183 $user = $users[$userId];
185 if (is_a($this->sourceObject,
'ceLTIc\LTI\ResourceLink')) {
189 $user->ltiUserId = $userId;
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);
199 if (array_key_exists($userId, $roles)) {
200 switch ($roles[$userId]) {
201 case 'StudentEnrollment':
202 $user->roles[] =
'urn:lti:role:ims/lis/Learner';
204 case 'TeacherEnrollment':
205 $user->roles[] =
'urn:lti:role:ims/lis/Instructor';
208 $user->roles[] =
'urn:lti:role:ims/lis/TeachingAssistant';
210 case 'DesignerEnrollment':
211 $user->roles[] =
'urn:lti:role:ims/lis/ContentDeveloper';
213 case 'ObserverEnrollment':
214 $user->roles[] =
'urn:lti:instrole:ims/lis/Observer';
215 $user->roles[] =
'urn:lti:role:ims/lis/Mentor';
219 $users[$userId] = $user;
221 if (preg_match(
'/<([^>]+)>; *rel=\"next\"/', $http->responseHeaders, $matches)) {
238 private function setGroups($perPage, $users)
240 $this->sourceObject->groups = array();
241 $url =
"https://{$this->domain}/api/v1/courses/{$this->courseId}/groups";
243 $url .=
"?per_page={$perPage}";
246 $http =
new HttpMessage($url,
'GET',
null,
"Authorization: Bearer {$this->token}");
249 $allGroups = json_decode($http->response);
250 $http->ok = !is_null($allGroups) && is_array($allGroups);
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'] ++;
265 if ($user->isLearner()) {
266 $this->sourceObject->groupSets[$setId][
'num_learners'] ++;
268 if (!in_array($groupId, $this->sourceObject->groupSets[$setId][
'groups'])) {
269 $this->sourceObject->groupSets[$setId][
'groups'][] = $groupId;
275 if (preg_match(
'/<([^>]+)>; *rel=\"next\"/', $http->responseHeaders, $matches)) {
static fromResourceLink($resourceLink, $ltiUserId)
Class constructor from resource link.
Class to represent a tool consumer user.
Class to handle Canvas web service requests.