ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Membership.php
Go to the documentation of this file.
1 <?php
2 
4 
6 
16 class Membership extends Service
17 {
18 
24  private $source;
25 
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 
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 }
if($orgName !==null) if($spconfig->hasValue('contacts')) $email
Definition: metadata.php:193
static parseRoles($roles)
Get an array of fully qualified user roles.
const ID_SCOPE_RESOURCE
Prefix the ID with the consumer key and resource ID.
Class to represent an LTI Tool Provider.
__construct($source, $endpoint)
Class constructor.
Definition: Membership.php:32
Class to implement the Membership service.
Definition: Membership.php:16
send($method, $parameters=array(), $body=null)
Send a service request.
Definition: Service.php:71
if(!array_key_exists('StateId', $_REQUEST)) $id
static fromResourceLink($resourceLink, $ltiUserId)
Class constructor from resource link.
Definition: User.php:413
catch(Exception $e) $message
$http
Definition: raiseError.php:7
Create styles array
The data for the language used.
$users
Definition: authpage.php:44
Class to implement a service.
Definition: Service.php:17
$consumer
Tool Consumer for this service request.
Definition: Service.php:38
$source
The object to which the settings apply (ResourceLink, Context or ToolConsumer).
Definition: Membership.php:24
Class to represent a tool consumer user.
Definition: User.php:15