LTI Integration Library  3.1.0
PHP class library for building LTI integrations
User.php
Go to the documentation of this file.
1 <?php
2 
3 namespace ceLTIc\LTI;
4 
13 class User
14 {
15 
21  public $firstname = '';
22 
28  public $lastname = '';
29 
35  public $fullname = '';
36 
42  public static $allowEmptyName = false;
43 
49  public $email = '';
50 
56  public $image = '';
57 
63  public $roles = array();
64 
70  public $groups = array();
71 
77  public $ltiUserId = null;
78 
82  public function __construct()
83  {
84  $this->initialize();
85  }
86 
90  public function initialize()
91  {
92  $this->firstname = '';
93  $this->lastname = '';
94  $this->fullname = '';
95  $this->email = '';
96  $this->image = '';
97  $this->roles = array();
98  $this->groups = array();
99  }
100 
106  public function initialise()
107  {
108  $this->initialize();
109  }
110 
119  {
120  $names = array(0 => '', 1 => '');
121  if (!empty($fullname)) {
122  $this->fullname = trim($fullname);
123  $names = preg_split("/[\s]+/", $this->fullname, 2);
124  }
125  if (!empty($firstname)) {
126  $this->firstname = trim($firstname);
127  $names[0] = $this->firstname;
128  } elseif (!empty($names[0])) {
129  $this->firstname = $names[0];
130  } elseif (!static::$allowEmptyName) {
131  $this->firstname = 'User';
132  } else {
133  $this->firstname = '';
134  }
135  if (!empty($lastname)) {
136  $this->lastname = trim($lastname);
137  $names[1] = $this->lastname;
138  } elseif (!empty($names[1])) {
139  $this->lastname = $names[1];
140  } elseif (!static::$allowEmptyName) {
141  $this->lastname = $this->ltiUserId;
142  } else {
143  $this->lastname = '';
144  }
145  if (empty($this->fullname) && (!empty($this->firstname) || !empty($this->lastname))) {
146  $this->fullname = trim("{$this->firstname} {$this->lastname}");
147  }
148  }
149 
156  public function setEmail($email, $defaultEmail = null)
157  {
158  if (!empty($email)) {
159  $this->email = $email;
160  } elseif (!empty($defaultEmail)) {
161  $this->email = $defaultEmail;
162  if (substr($this->email, 0, 1) === '@') {
163  $this->email = $this->getId() . $this->email;
164  }
165  } else {
166  $this->email = '';
167  }
168  }
169 
175  public function isAdmin()
176  {
177  return $this->hasRole('Administrator') || $this->hasRole('urn:lti:sysrole:ims/lis/SysAdmin') ||
178  $this->hasRole('urn:lti:sysrole:ims/lis/Administrator') || $this->hasRole('urn:lti:instrole:ims/lis/Administrator');
179  }
180 
186  public function isStaff()
187  {
188  return ($this->hasRole('Instructor') || $this->hasRole('ContentDeveloper') || $this->hasRole('TeachingAssistant'));
189  }
190 
196  public function isLearner()
197  {
198  return $this->hasRole('Learner');
199  }
200 
201 ###
202 ### PRIVATE METHODS
203 ###
204 
212  private function hasRole($role)
213  {
214  $role2 = null;
215  if ((substr($role, 0, 4) !== 'urn:') && (substr($role, 0, 7) !== 'http://') && (substr($role, 0, 8) !== 'https://')) {
216  $role2 = 'http://purl.imsglobal.org/vocab/lis/v2/membership#' . $role;
217  $role = 'urn:lti:role:ims/lis/' . $role;
218  }
219  $ok = in_array($role, $this->roles);
220  if (!$ok && !empty($role2)) {
221  $ok = in_array($role2, $this->roles);
222  }
223 
224  return $ok;
225  }
226 
227 }
isStaff()
Check if the user is staff.
Definition: User.php:186
isLearner()
Check if the user is a learner.
Definition: User.php:196
$firstname
UserResult's first name.
Definition: User.php:21
$ltiUserId
user ID as supplied in the last connection request.
Definition: User.php:77
initialize()
Initialise the user.
Definition: User.php:90
$lastname
UserResult's last name (surname or family name).
Definition: User.php:28
$fullname
UserResult's fullname.
Definition: User.php:35
__construct()
Class constructor.
Definition: User.php:82
$groups
Groups for user.
Definition: User.php:70
$email
UserResult's email address.
Definition: User.php:49
setNames($firstname, $lastname, $fullname)
Set the user's name.
Definition: User.php:118
initialise()
Initialise the user.
Definition: User.php:106
$roles
Roles for user.
Definition: User.php:63
setEmail($email, $defaultEmail=null)
Set the user's email address.
Definition: User.php:156
$image
UserResult's image URI.
Definition: User.php:56
Class to represent a tool consumer user.
Definition: User.php:13
static $allowEmptyName
Allow user name field to be empty?
Definition: User.php:42
isAdmin()
Check if the user is an administrator (at any of the system, institution or context levels).
Definition: User.php:175