Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 400 and 401]

   1  <?php
   2  
   3  namespace Packback\Lti1p3;
   4  
   5  class LtiCourseGroupsService extends LtiAbstractService
   6  {
   7      public const CONTENTTYPE_CONTEXTGROUPCONTAINER = 'application/vnd.ims.lti-gs.v1.contextgroupcontainer+json';
   8  
   9      public function getScope(): array
  10      {
  11          return $this->getServiceData()['scope'];
  12      }
  13  
  14      public function getGroups(): array
  15      {
  16          $request = new ServiceRequest(
  17              ServiceRequest::METHOD_GET,
  18              $this->getServiceData()['context_groups_url'],
  19              ServiceRequest::TYPE_GET_GROUPS
  20          );
  21          $request->setAccept(static::CONTENTTYPE_CONTEXTGROUPCONTAINER);
  22  
  23          return $this->getAll($request, 'groups');
  24      }
  25  
  26      public function getSets(): array
  27      {
  28          // Sets are optional.
  29          if (!isset($this->getServiceData()['context_group_sets_url'])) {
  30              return [];
  31          }
  32  
  33          $request = new ServiceRequest(
  34              ServiceRequest::METHOD_GET,
  35              $this->getServiceData()['context_group_sets_url'],
  36              ServiceRequest::TYPE_GET_SETS
  37          );
  38          $request->setAccept(static::CONTENTTYPE_CONTEXTGROUPCONTAINER);
  39  
  40          return $this->getAll($request, 'sets');
  41      }
  42  
  43      public function getGroupsBySet()
  44      {
  45          $groups = $this->getGroups();
  46          $sets = $this->getSets();
  47  
  48          $groupsBySet = [];
  49          $unsetted = [];
  50  
  51          foreach ($sets as $key => $set) {
  52              $groupsBySet[$set['id']] = $set;
  53              $groupsBySet[$set['id']]['groups'] = [];
  54          }
  55  
  56          foreach ($groups as $key => $group) {
  57              if (isset($group['set_id']) && isset($groupsBySet[$group['set_id']])) {
  58                  $groupsBySet[$group['set_id']]['groups'][$group['id']] = $group;
  59              } else {
  60                  $unsetted[$group['id']] = $group;
  61              }
  62          }
  63  
  64          if (!empty($unsetted)) {
  65              $groupsBySet['none'] = [
  66                  'name' => 'None',
  67                  'id' => 'none',
  68                  'groups' => $unsetted,
  69              ];
  70          }
  71  
  72          return $groupsBySet;
  73      }
  74  }