LTI Integration Library 4.10.3
PHP class library for building LTI integrations
 
Loading...
Searching...
No Matches
Groups.php
1<?php
2
3namespace ceLTIc\LTI\Service;
4
7
15class Groups extends Service
16{
17
21 const MEDIA_TYPE_COURSE_GROUP_SETS = 'application/vnd.ims.lti-gs.v1.contextgroupsetcontainer+json';
22
26 const MEDIA_TYPE_COURSE_GROUPS = 'application/vnd.ims.lti-gs.v1.contextgroupcontainer+json';
27
33 public static $SCOPE = 'https://purl.imsglobal.org/spec/lti-gs/scope/contextgroup.readonly';
34
40 public static $defaultLimit = null;
41
47 private $context = null;
48
54 private $groupsEndpoint = null;
55
61 private $groupSetsEndpoint = null;
62
70 private $limit;
71
79 private $pagingMode;
80
90 public function __construct($context, $groupsEndpoint, $groupSetsEndpoint = null, $limit = null, $pagingMode = false)
91 {
92 $platform = $context->getPlatform();
93 parent::__construct($platform, $groupsEndpoint);
94 $this->scope = self::$SCOPE;
95 $this->mediaType = self::MEDIA_TYPE_COURSE_GROUPS;
96 $this->context = $context;
97 $this->groupsEndpoint = $groupsEndpoint;
98 $this->groupSetsEndpoint = $groupSetsEndpoint;
99 $this->limit = $limit;
100 $this->pagingMode = $pagingMode;
101 }
102
112 public function get($allowNonSets = false, $user = null, $limit = null)
113 {
114 $ok = $this->getGroupSets($limit);
115 if ($ok) {
116 $ok = $this->getGroups($allowNonSets, $user, $limit);
117 }
118 if (!$ok) {
119 $this->context->groupSets = null;
120 $this->context->groups = null;
121 }
122
123 return $ok;
124 }
125
133 public function getGroupSets($limit = null)
134 {
135 $this->endpoint = $this->groupSetsEndpoint;
136 $ok = !empty($this->endpoint);
137 if ($ok) {
138 $this->mediaType = self::MEDIA_TYPE_COURSE_GROUP_SETS;
139 $parameters = array();
140 if (is_null($limit)) {
141 $limit = $this->limit;
142 }
143 if (is_null($limit)) {
144 $limit = self::$defaultLimit;
145 }
146 if (!empty($limit)) {
147 $parameters['limit'] = strval($limit);
148 }
149 $this->context->groupSets = array();
150 $groupSets = array();
152 do {
153 $http = $this->send('GET', $parameters);
154 $ok = !empty($http) && $http->ok;
155 $url = '';
156 if ($ok) {
157 if (isset($http->responseJson->sets)) {
158 foreach ($http->responseJson->sets as $set) {
159 $groupSets[$set->id] = array('title' => $set->name, 'groups' => array(),
160 'num_members' => 0, 'num_staff' => 0, 'num_learners' => 0);
161 }
162 }
163 if (!$this->pagingMode && $http->hasRelativeLink('next')) {
164 $url = $http->getRelativeLink('next');
165 $this->endpoint = $url;
166 $parameters = array();
167 }
168 }
169 } while ($url);
170 $this->endpoint = $endpoint;
171 if ($ok) {
172 $this->context->groupSets = $groupSets;
173 }
174 }
175
176 return $ok;
177 }
178
188 public function getGroups($allowNonSets = false, $user = null, $limit = null)
189 {
190 $this->endpoint = $this->groupsEndpoint;
191 $ok = !empty($this->endpoint);
192 if ($ok) {
193 $this->mediaType = self::MEDIA_TYPE_COURSE_GROUPS;
194 $parameters = array();
195 $ltiUserId = null;
196 if (!empty($user) && !empty($user->ltiUserId)) {
197 $ltiUserId = $user->ltiUserId;
198 }
199 if (!empty($ltiUserId)) {
200 $parameters['user_id'] = $ltiUserId;
201 }
202 if (is_null($limit)) {
203 $limit = $this->limit;
204 }
205 if (is_null($limit)) {
206 $limit = self::$defaultLimit;
207 }
208 if (!empty($limit)) {
209 $parameters['limit'] = strval($limit);
210 }
211 if (is_null($this->context->groupSets)) {
212 $groupSets = array();
213 } else {
214 $groupSets = $this->context->groupSets;
215 }
216 $groups = array();
218 do {
219 $http = $this->send('GET', $parameters);
220 $ok = !empty($http) && $http->ok;
221 $url = '';
222 if ($ok) {
223 if (isset($http->responseJson->groups)) {
224 foreach ($http->responseJson->groups as $agroup) {
225 if (!$allowNonSets && empty($agroup->set_id)) {
226 continue;
227 }
228 $group = array('title' => $agroup->name);
229 if (!empty($agroup->set_id)) {
230 if (!array_key_exists($agroup->set_id, $groupSets)) {
231 $groupSets[$agroup->set_id] = array('title' => "Set {$agroup->set_id}", 'groups' => array(),
232 'num_members' => 0, 'num_staff' => 0, 'num_learners' => 0);
233 }
234 $groupSets[$agroup->set_id]['groups'][] = $agroup->id;
235 $group['set'] = $agroup->set_id;
236 }
237 if (!empty($agroup->tag)) {
238 $group['tag'] = $agroup->tag;
239 }
240 $groups[$agroup->id] = $group;
241 }
242 }
243 if (!$this->pagingMode && $http->hasRelativeLink('next')) {
244 $url = $http->getRelativeLink('next');
245 $this->endpoint = $url;
246 $parameters = array();
247 }
248 }
249 } while ($url);
250 $this->endpoint = $endpoint;
251 if ($ok) {
252 $this->context->groupSets = $groupSets;
253 if (empty($ltiUserId)) {
254 $this->context->groups = $groups;
255 } else {
256 $user->groups = $groups;
257 }
258 }
259 }
260
261 return $ok;
262 }
263
264}
Class to represent a platform context.
Definition Context.php:18
Class to implement the Course Groups service.
Definition Groups.php:16
static $SCOPE
Access scope.
Definition Groups.php:33
getGroupSets($limit=null)
Get the course group sets.
Definition Groups.php:133
__construct($context, $groupsEndpoint, $groupSetsEndpoint=null, $limit=null, $pagingMode=false)
Class constructor.
Definition Groups.php:90
const MEDIA_TYPE_COURSE_GROUPS
Media type for course groups service.
Definition Groups.php:26
getGroups($allowNonSets=false, $user=null, $limit=null)
Get the course groups.
Definition Groups.php:188
const MEDIA_TYPE_COURSE_GROUP_SETS
Media type for course group sets service.
Definition Groups.php:21
static $defaultLimit
Default limit on size of container to be returned from requests.
Definition Groups.php:40
Class to implement a service.
Definition Service.php:19
send($method, $parameters=array(), $body=null)
Send a service request.
Definition Service.php:119
$endpoint
Service endpoint.
Definition Service.php:33
Class to represent a platform user.
Definition User.php:13