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 401 and 402] [Versions 401 and 403]

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