ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
User.php
Go to the documentation of this file.
1 <?php
2 
5 
15 class User
16 {
17 
23  public $firstname = '';
29  public $lastname = '';
35  public $fullname = '';
41  public $email = '';
47  public $image = '';
53  public $roles = array();
59  public $groups = array();
65  public $ltiResultSourcedId = null;
71  public $created = null;
77  public $updated = null;
78 
84  private $resourceLink = null;
90  private $resourceLinkId = null;
96  private $id = null;
102  public $ltiUserId = null;
108  private $dataConnector = null;
109 
113  public function __construct()
114  {
115 
116  $this->initialize();
117 
118  }
119 
123  public function initialize()
124  {
125 
126  $this->firstname = '';
127  $this->lastname = '';
128  $this->fullname = '';
129  $this->email = '';
130  $this->image = '';
131  $this->roles = array();
132  $this->groups = array();
133  $this->ltiResultSourcedId = null;
134  $this->created = null;
135  $this->updated = null;
136 
137  }
138 
144  public function initialise()
145  {
146 
147  $this->initialize();
148 
149  }
150 
156  public function save()
157  {
158 
159  if (!empty($this->ltiResultSourcedId) && !is_null($this->resourceLinkId)) {
160  $ok = $this->getDataConnector()->saveUser($this);
161  } else {
162  $ok = true;
163  }
164 
165  return $ok;
166 
167  }
168 
174  public function delete()
175  {
176 
177  $ok = $this->getDataConnector()->deleteUser($this);
178 
179  return $ok;
180 
181  }
182 
188  public function getResourceLink()
189  {
190 
191  if (is_null($this->resourceLink) && !is_null($this->resourceLinkId)) {
192  $this->resourceLink = ResourceLink::fromRecordId($this->resourceLinkId, $this->getDataConnector());
193  }
194 
195  return $this->resourceLink;
196 
197  }
198 
204  public function getRecordId()
205  {
206 
207  return $this->id;
208 
209  }
210 
216  public function setRecordId($id)
217  {
218 
219  $this->id = $id;
220 
221  }
222 
229  {
230 
231  $this->resourceLinkId = $resourceLinkId;
232 
233  }
234 
240  public function getDataConnector()
241  {
242 
243  return $this->dataConnector;
244 
245  }
246 
254  public function getId($idScope = null)
255  {
256 
257  if (empty($idScope)) {
258  if (!is_null($this->resourceLink)) {
259  $idScope = $this->resourceLink->getConsumer()->idScope;
260  } else {
262  }
263  }
264  switch ($idScope) {
267  break;
269  $id = $this->getResourceLink()->getKey();
270  if ($this->resourceLink->ltiContextId) {
271  $id .= ToolProvider::ID_SCOPE_SEPARATOR . $this->resourceLink->ltiContextId;
272  }
274  break;
276  $id = $this->getResourceLink()->getKey();
277  if ($this->resourceLink->ltiResourceLinkId) {
278  $id .= ToolProvider::ID_SCOPE_SEPARATOR . $this->resourceLink->ltiResourceLinkId;
279  }
281  break;
282  default:
284  break;
285  }
286 
287  return $id;
288 
289  }
290 
299  {
300 
301  $names = array(0 => '', 1 => '');
302  if (!empty($fullname)) {
303  $this->fullname = trim($fullname);
304  $names = preg_split("/[\s]+/", $this->fullname, 2);
305  }
306  if (!empty($firstname)) {
307  $this->firstname = trim($firstname);
308  $names[0] = $this->firstname;
309  } else if (!empty($names[0])) {
310  $this->firstname = $names[0];
311  } else {
312  $this->firstname = 'User';
313  }
314  if (!empty($lastname)) {
315  $this->lastname = trim($lastname);
316  $names[1] = $this->lastname;
317  } else if (!empty($names[1])) {
318  $this->lastname = $names[1];
319  } else {
320  $this->lastname = $this->ltiUserId;
321  }
322  if (empty($this->fullname)) {
323  $this->fullname = "{$this->firstname} {$this->lastname}";
324  }
325 
326  }
327 
334  public function setEmail($email, $defaultEmail = null)
335  {
336 
337  if (!empty($email)) {
338  $this->email = $email;
339  } else if (!empty($defaultEmail)) {
340  $this->email = $defaultEmail;
341  if (substr($this->email, 0, 1) === '@') {
342  $this->email = $this->getId() . $this->email;
343  }
344  } else {
345  $this->email = '';
346  }
347 
348  }
349 
355  public function isAdmin()
356  {
357 
358  return $this->hasRole('Administrator') || $this->hasRole('urn:lti:sysrole:ims/lis/SysAdmin') ||
359  $this->hasRole('urn:lti:sysrole:ims/lis/Administrator') || $this->hasRole('urn:lti:instrole:ims/lis/Administrator');
360 
361  }
362 
368  public function isStaff()
369  {
370 
371  return ($this->hasRole('Instructor') || $this->hasRole('ContentDeveloper') || $this->hasRole('TeachingAssistant'));
372 
373  }
374 
380  public function isLearner()
381  {
382 
383  return $this->hasRole('Learner');
384 
385  }
386 
395  public static function fromRecordId($id, $dataConnector)
396  {
397 
398  $user = new User();
399  $user->dataConnector = $dataConnector;
400  $user->load($id);
401 
402  return $user;
403 
404  }
405 
413  public static function fromResourceLink($resourceLink, $ltiUserId)
414  {
415 
416  $user = new User();
417  $user->resourceLink = $resourceLink;
418  if (!is_null($resourceLink)) {
419  $user->resourceLinkId = $resourceLink->getRecordId();
420  $user->dataConnector = $resourceLink->getDataConnector();
421  }
422  $user->ltiUserId = $ltiUserId;
423  if (!empty($ltiUserId)) {
424  $user->load();
425  }
426 
427  return $user;
428 
429  }
430 
431 ###
432 ### PRIVATE METHODS
433 ###
434 
442  private function hasRole($role) {
443 
444  if (substr($role, 0, 4) !== 'urn:')
445  {
446  $role = 'urn:lti:role:ims/lis/' . $role;
447  }
448 
449  return in_array($role, $this->roles);
450 
451  }
452 
460  private function load($id = null)
461  {
462 
463  $this->initialize();
464  $this->id = $id;
465  $dataConnector = $this->getDataConnector();
466  if (!is_null($dataConnector)) {
467  return $dataConnector->loadUser($this);
468  }
469 
470  return false;
471  }
472 
473 }
$email
User&#39;s email address.
Definition: User.php:41
getResourceLink()
Get resource link.
Definition: User.php:188
getDataConnector()
Get the data connector.
Definition: User.php:240
save()
Save the user to the database.
Definition: User.php:156
$created
Date/time the record was created.
Definition: User.php:71
__construct()
Class constructor.
Definition: User.php:113
const ID_SCOPE_RESOURCE
Prefix the ID with the consumer key and resource ID.
$ltiUserId
user ID as supplied in the last connection request.
Definition: User.php:102
$fullname
User&#39;s fullname.
Definition: User.php:35
load($id=null)
Load the user from the database.
Definition: User.php:460
$roles
Roles for user.
Definition: User.php:53
$resourceLinkId
Resource link record ID.
Definition: User.php:90
initialize()
Initialise the user.
Definition: User.php:123
static fromResourceLink($resourceLink, $ltiUserId)
Class constructor from resource link.
Definition: User.php:413
hasRole($role)
Check whether the user has a specified role name.
Definition: User.php:442
$ltiResultSourcedId
User&#39;s result sourcedid.
Definition: User.php:65
$dataConnector
Data connector object or string.
Definition: User.php:108
$groups
Groups for user.
Definition: User.php:59
$firstname
User&#39;s first name.
Definition: User.php:23
setResourceLinkId($resourceLinkId)
Set resource link ID of user.
Definition: User.php:228
setEmail($email, $defaultEmail=null)
Set the user&#39;s email address.
Definition: User.php:334
const ID_SCOPE_CONTEXT
Prefix the ID with the consumer key and context ID.
$updated
Date/time the record was last updated.
Definition: User.php:77
setNames($firstname, $lastname, $fullname)
Set the user&#39;s name.
Definition: User.php:298
$user
Definition: migrateto20.php:57
isAdmin()
Check if the user is an administrator (at any of the system, institution or context levels)...
Definition: User.php:355
$image
User&#39;s image URI.
Definition: User.php:47
$resourceLink
Resource link object.
Definition: User.php:84
isStaff()
Check if the user is staff.
Definition: User.php:368
initialise()
Initialise the user.
Definition: User.php:144
$lastname
User&#39;s last name (surname or family name).
Definition: User.php:29
getRecordId()
Get record ID of user.
Definition: User.php:204
const ID_SCOPE_SEPARATOR
Character used to separate each element of an ID.
getId($idScope=null)
Get the user ID (which may be a compound of the tool consumer and resource link IDs).
Definition: User.php:254
setRecordId($id)
Set record ID of user.
Definition: User.php:216
isLearner()
Check if the user is a learner.
Definition: User.php:380
$id
User record ID value.
Definition: User.php:96
const ID_SCOPE_GLOBAL
Prefix an ID with the consumer key.
static fromRecordId($id, $dataConnector)
Load the user from the database.
Definition: User.php:395
Class to represent a tool consumer user.
Definition: User.php:15
const ID_SCOPE_ID_ONLY
Use ID value only.