ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
Groups.php
Go to the documentation of this file.
1 <?php
2 
20 
22 
30 class Groups extends Service
31 {
35  public const MEDIA_TYPE_COURSE_GROUP_SETS = 'application/vnd.ims.lti-gs.v1.contextgroupsetcontainer+json';
36 
40  public const MEDIA_TYPE_COURSE_GROUPS = 'application/vnd.ims.lti-gs.v1.contextgroupcontainer+json';
41 
45  public static string $SCOPE = 'https://purl.imsglobal.org/spec/lti-gs/scope/contextgroup.readonly';
46 
50  public static ?int $defaultLimit = null; //UK added ?int
51 
57  private ?Context $context = null;
58 
64  private ?string $groupsEndpoint = null;
65 
71  private ?string $groupSetsEndpoint = null;
72 
80  private ?int $limit;
81 
89  private bool $pagingMode;
90 
99  public function __construct(Context $context, string $groupsEndpoint, string $groupSetsEndpoint = null, int $limit = null, bool $pagingMode = false)
100  {
101  $platform = $context->getPlatform();
102  parent::__construct($platform, $groupsEndpoint);
103  $this->scope = self::$SCOPE;
104  $this->mediaType = self::MEDIA_TYPE_COURSE_GROUPS;
105  $this->context = $context;
106  $this->groupsEndpoint = $groupsEndpoint;
107  $this->groupSetsEndpoint = $groupSetsEndpoint;
108  $this->limit = $limit;
109  $this->pagingMode = $pagingMode;
110  }
111 
119  public function get(bool $allowNonSets = false, \ILIAS\LTI\ToolProvider\User $user = null, int $limit = null): bool
120  {
121  $ok = $this->getGroupSets($limit);
122  if ($ok) {
123  $ok = $this->getGroups($allowNonSets, $user, $limit);
124  }
125  if (!$ok) {
126  $this->context->groupSets = null;
127  $this->context->groups = null;
128  }
129 
130  return $ok;
131  }
132 
138  public function getGroupSets(int $limit = null): bool
139  {
140  $this->endpoint = $this->groupSetsEndpoint;
141  $ok = !empty($this->endpoint);
142  if ($ok) {
143  $this->mediaType = self::MEDIA_TYPE_COURSE_GROUP_SETS;
144  $parameters = array();
145  if (is_null($limit)) {
146  $limit = $this->limit;
147  }
148  if (is_null($limit)) {
149  $limit = self::$defaultLimit;
150  }
151  if (!empty($limit)) {
152  $parameters['limit'] = strval($limit);
153  }
154  $this->context->groupSets = array();
155  $groupSets = array();
157  do {
158  $http = $this->send('GET', $parameters);
159  $ok = !empty($http) && $http->ok;
160  $url = '';
161  if ($ok) {
162  if (isset($http->responseJson->sets)) {
163  foreach ($http->responseJson->sets as $set) {
164  $groupSets[$set->id] = array('title' => $set->name, 'groups' => array(),
165  'num_members' => 0, 'num_staff' => 0, 'num_learners' => 0);
166  }
167  }
168  if (!$this->pagingMode && $http->hasRelativeLink('next')) {
169  $url = $http->getRelativeLink('next');
170  $this->endpoint = $url;
171  $parameters = array();
172  }
173  }
174  } while ($url);
175  $this->endpoint = $endpoint;
176  if ($ok) {
177  $this->context->groupSets = $groupSets;
178  }
179  }
180 
181  return $ok;
182  }
183 
191  public function getGroups(bool $allowNonSets = false, \ILIAS\LTI\ToolProvider\User $user = null, int $limit = null): bool
192  {
193  $this->endpoint = $this->groupsEndpoint;
194  $ok = !empty($this->endpoint);
195  if ($ok) {
196  $this->mediaType = self::MEDIA_TYPE_COURSE_GROUPS;
197  $parameters = array();
198  $ltiUserId = null;
199  if (!empty($user) && !empty($user->ltiUserId)) {
200  $ltiUserId = $user->ltiUserId;
201  }
202  if (!empty($ltiUserId)) {
203  $parameters['user_id'] = $ltiUserId;
204  }
205  if (is_null($limit)) {
206  $limit = $this->limit;
207  }
208  if (is_null($limit)) {
209  $limit = self::$defaultLimit;
210  }
211  if (!empty($limit)) {
212  $parameters['limit'] = strval($limit);
213  }
214  if (is_null($this->context->groupSets)) {
215  $groupSets = array();
216  } else {
217  $groupSets = $this->context->groupSets;
218  }
219  $groups = array();
221  do {
222  $http = $this->send('GET', $parameters);
223  $ok = !empty($http) && $http->ok;
224  $url = '';
225  if ($ok) {
226  if (isset($http->responseJson->groups)) {
227  foreach ($http->responseJson->groups as $agroup) {
228  if (!$allowNonSets && empty($agroup->set_id)) {
229  continue;
230  }
231  $group = array('title' => $agroup->name);
232  if (!empty($agroup->set_id)) {
233  if (!array_key_exists($agroup->set_id, $groupSets)) {
234  $groupSets[$agroup->set_id] = array('title' => "Set {$agroup->set_id}", 'groups' => array(),
235  'num_members' => 0, 'num_staff' => 0, 'num_learners' => 0);
236  }
237  $groupSets[$agroup->set_id]['groups'][] = $agroup->id;
238  $group['set'] = $agroup->set_id;
239  }
240  if (!empty($agroup->tag)) {
241  $group['tag'] = $agroup->tag;
242  }
243  $groups[$agroup->id] = $group;
244  }
245  }
246  if (!$this->pagingMode && $http->hasRelativeLink('next')) {
247  $url = $http->getRelativeLink('next');
248  $this->endpoint = $url;
249  $parameters = array();
250  }
251  }
252  } while ($url);
253  $this->endpoint = $endpoint;
254  if ($ok) {
255  $this->context->groupSets = $groupSets;
256  if (empty($ltiUserId)) {
257  $this->context->groups = $groups;
258  } else {
259  $user->groups = $groups;
260  }
261  }
262  }
263 
264  return $ok;
265  }
266 }
string $groupSetsEndpoint
The endpoint for course groupset requests.
Definition: Groups.php:71
Class to implement a service.
Definition: Service.php:33
int $limit
Limit on size of container to be returned from requests.
Definition: Groups.php:80
Class to represent a platform user.
Definition: User.php:30
Class ChatMainBarProvider .
string $endpoint
Service endpoint.
Definition: Service.php:47
send(string $method, array $parameters=array(), string $body=null)
Send a service request.
Definition: Service.php:130
__construct(Context $context, string $groupsEndpoint, string $groupSetsEndpoint=null, int $limit=null, bool $pagingMode=false)
Class constructor.
Definition: Groups.php:99
HTTPMessage $http
HttpMessage object for last service request.
Definition: Service.php:75
getGroups(bool $allowNonSets=false, \ILIAS\LTI\ToolProvider\User $user=null, int $limit=null)
Get the course groups.
Definition: Groups.php:191
static int $defaultLimit
Default limit on size of container to be returned from requests.
Definition: Groups.php:50
Class to implement the Course Groups service.
Definition: Groups.php:30
string $groupsEndpoint
The endpoint for course group requests.
Definition: Groups.php:64
bool $pagingMode
Whether requests should be made one page at a time when a limit is set.
Definition: Groups.php:89
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(Container $dic, ilPlugin $plugin)
const MEDIA_TYPE_COURSE_GROUP_SETS
Media type for course group sets service.
Definition: Groups.php:35
static string $SCOPE
Access scope.
Definition: Groups.php:45
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$url
Platform $platform
Platform for this service request.
Definition: Service.php:68
Class to represent a platform context.
Definition: Context.php:33
const MEDIA_TYPE_COURSE_GROUPS
Media type for course groups service.
Definition: Groups.php:40
getGroupSets(int $limit=null)
Get the course group sets.
Definition: Groups.php:138
getPlatform()
Get tool consumer.
Definition: Context.php:239
Context $context
The context to which the course groups apply.
Definition: Groups.php:57