Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   1  <?php
   2  
   3  namespace IMSGlobal\LTI\ToolProvider\Service;
   4  
   5  use IMSGlobal\LTI\ToolProvider;
   6  
   7  /**
   8   * Class to implement the Membership service
   9   *
  10   * @author  Stephen P Vickers <svickers@imsglobal.org>
  11   * @copyright  IMS Global Learning Consortium Inc
  12   * @date  2016
  13   * @version 3.0.0
  14   * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  15   */
  16  #[\AllowDynamicProperties]
  17  class Membership extends Service
  18  {
  19  
  20  /**
  21   * The object to which the settings apply (ResourceLink, Context or ToolConsumer).
  22   *
  23   * @var object  $source
  24   */
  25      private $source;
  26  
  27  /**
  28   * Class constructor.
  29   *
  30   * @param object       $source     The object to which the memberships apply (ResourceLink or Context)
  31   * @param string       $endpoint   Service endpoint
  32   */
  33      public function __construct($source, $endpoint)
  34      {
  35  
  36          $consumer = $source->getConsumer();
  37          parent::__construct($consumer, $endpoint, 'application/vnd.ims.lis.v2.membershipcontainer+json');
  38          $this->source = $source;
  39  
  40      }
  41  
  42  /**
  43   * Get the memberships.
  44   *
  45   * @param string    $role   Role for which memberships are to be requested (optional, default is all roles)
  46   * @param int       $limit  Limit on the number of memberships to be returned (optional, default is all)
  47   *
  48   * @return mixed The array of User objects if successful, otherwise false
  49   */
  50      public function get($role = null, $limit = 0) {
  51  
  52          $isLink = is_a($this->source, 'IMSGlobal\LTI\ToolProvider\ResourceLink');
  53          $parameters = array();
  54          if (!empty($role)) {
  55              $parameters['role'] = $role;
  56          }
  57          if ($limit > 0) {
  58              $parameters['limit'] = strval($limit);
  59          }
  60          if ($isLink) {
  61              $parameters['rlid'] = $this->source->getId();
  62          }
  63          $http = $this->send('GET', $parameters);
  64          if (!$http->ok) {
  65              $users = false;
  66          } else {
  67              $users = array();
  68              if ($isLink) {
  69                  $oldUsers = $this->source->getUserResultSourcedIDs(true, ToolProvider\ToolProvider::ID_SCOPE_RESOURCE);
  70              }
  71              foreach ($http->responseJson->pageOf->membershipSubject->membership as $membership) {
  72                  $member = $membership->member;
  73                  if ($isLink) {
  74                      $user = ToolProvider\User::fromResourceLink($this->source, $member->userId);
  75                  } else {
  76                      $user = new ToolProvider\User();
  77                      $user->ltiUserId = $member->userId;
  78                  }
  79  
  80  // Set the user name
  81                  $firstname = (isset($member->givenName)) ? $member->givenName : '';
  82                  $lastname = (isset($member->familyName)) ? $member->familyName : '';
  83                  $fullname = (isset($member->name)) ? $member->name : '';
  84                  $user->setNames($firstname, $lastname, $fullname);
  85  
  86  // Set the user email
  87                  $email = (isset($member->email)) ? $member->email : '';
  88                  $user->setEmail($email, $this->source->getConsumer()->defaultEmail);
  89  
  90  // Set the user roles
  91                  if (isset($membership->role)) {
  92                      $user->roles = ToolProvider\ToolProvider::parseRoles($membership->role);
  93                  }
  94  
  95  // If a result sourcedid is provided save the user
  96                  if ($isLink) {
  97                      if (isset($member->message)) {
  98                          foreach ($member->message as $message) {
  99                              if (isset($message->message_type) && ($message->message_type === 'basic-lti-launch-request')) {
 100                                  if (isset($message->lis_result_sourcedid)) {
 101                                      $user->ltiResultSourcedId = $message->lis_result_sourcedid;
 102                                      $user->save();
 103                                  }
 104                                  break;                                
 105                              }
 106                          }
 107                      }
 108                  }
 109                  $users[] = $user;
 110  
 111  // Remove old user (if it exists)
 112                  if ($isLink) {
 113                      unset($oldUsers[$user->getId(ToolProvider\ToolProvider::ID_SCOPE_RESOURCE)]);
 114                  }
 115              }
 116  
 117  // Delete any old users which were not in the latest list from the tool consumer
 118              if ($isLink) {
 119                  foreach ($oldUsers as $id => $user) {
 120                      $user->delete();
 121                  }
 122              }
 123          }
 124  
 125          return $users;
 126  
 127      }
 128  
 129  }