/lib/ltiprovider/src/ToolProvider/User.php

https://github.com/markn86/moodle · PHP · 473 lines · 203 code · 73 blank · 197 comment · 34 complexity · 3ce485b7e916c135d97447dee3046647 MD5 · raw file

  1. <?php
  2. namespace IMSGlobal\LTI\ToolProvider;
  3. use IMSGlobal\LTI\ToolProvider\DataConnector\DataConnector;
  4. /**
  5. * Class to represent a tool consumer user
  6. *
  7. * @author Stephen P Vickers <svickers@imsglobal.org>
  8. * @copyright IMS Global Learning Consortium Inc
  9. * @date 2016
  10. * @version 3.0.2
  11. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  12. */
  13. class User
  14. {
  15. /**
  16. * User's first name.
  17. *
  18. * @var string $firstname
  19. */
  20. public $firstname = '';
  21. /**
  22. * User's last name (surname or family name).
  23. *
  24. * @var string $lastname
  25. */
  26. public $lastname = '';
  27. /**
  28. * User's fullname.
  29. *
  30. * @var string $fullname
  31. */
  32. public $fullname = '';
  33. /**
  34. * User's email address.
  35. *
  36. * @var string $email
  37. */
  38. public $email = '';
  39. /**
  40. * User's image URI.
  41. *
  42. * @var string $image
  43. */
  44. public $image = '';
  45. /**
  46. * Roles for user.
  47. *
  48. * @var array $roles
  49. */
  50. public $roles = array();
  51. /**
  52. * Groups for user.
  53. *
  54. * @var array $groups
  55. */
  56. public $groups = array();
  57. /**
  58. * User's result sourcedid.
  59. *
  60. * @var string $ltiResultSourcedId
  61. */
  62. public $ltiResultSourcedId = null;
  63. /**
  64. * Date/time the record was created.
  65. *
  66. * @var object $created
  67. */
  68. public $created = null;
  69. /**
  70. * Date/time the record was last updated.
  71. *
  72. * @var object $updated
  73. */
  74. public $updated = null;
  75. /**
  76. * Resource link object.
  77. *
  78. * @var ResourceLink $resourceLink
  79. */
  80. private $resourceLink = null;
  81. /**
  82. * Resource link record ID.
  83. *
  84. * @var int $resourceLinkId
  85. */
  86. private $resourceLinkId = null;
  87. /**
  88. * User record ID value.
  89. *
  90. * @var string $id
  91. */
  92. private $id = null;
  93. /**
  94. * user ID as supplied in the last connection request.
  95. *
  96. * @var string $ltiUserId
  97. */
  98. public $ltiUserId = null;
  99. /**
  100. * Data connector object or string.
  101. *
  102. * @var mixed $dataConnector
  103. */
  104. private $dataConnector = null;
  105. /**
  106. * Class constructor.
  107. */
  108. public function __construct()
  109. {
  110. $this->initialize();
  111. }
  112. /**
  113. * Initialise the user.
  114. */
  115. public function initialize()
  116. {
  117. $this->firstname = '';
  118. $this->lastname = '';
  119. $this->fullname = '';
  120. $this->email = '';
  121. $this->image = '';
  122. $this->roles = array();
  123. $this->groups = array();
  124. $this->ltiResultSourcedId = null;
  125. $this->created = null;
  126. $this->updated = null;
  127. }
  128. /**
  129. * Initialise the user.
  130. *
  131. * Pseudonym for initialize().
  132. */
  133. public function initialise()
  134. {
  135. $this->initialize();
  136. }
  137. /**
  138. * Save the user to the database.
  139. *
  140. * @return boolean True if the user object was successfully saved
  141. */
  142. public function save()
  143. {
  144. if (!empty($this->ltiResultSourcedId) && !is_null($this->resourceLinkId)) {
  145. $ok = $this->getDataConnector()->saveUser($this);
  146. } else {
  147. $ok = true;
  148. }
  149. return $ok;
  150. }
  151. /**
  152. * Delete the user from the database.
  153. *
  154. * @return boolean True if the user object was successfully deleted
  155. */
  156. public function delete()
  157. {
  158. $ok = $this->getDataConnector()->deleteUser($this);
  159. return $ok;
  160. }
  161. /**
  162. * Get resource link.
  163. *
  164. * @return ResourceLink Resource link object
  165. */
  166. public function getResourceLink()
  167. {
  168. if (is_null($this->resourceLink) && !is_null($this->resourceLinkId)) {
  169. $this->resourceLink = ResourceLink::fromRecordId($this->resourceLinkId, $this->getDataConnector());
  170. }
  171. return $this->resourceLink;
  172. }
  173. /**
  174. * Get record ID of user.
  175. *
  176. * @return int Record ID of user
  177. */
  178. public function getRecordId()
  179. {
  180. return $this->id;
  181. }
  182. /**
  183. * Set record ID of user.
  184. *
  185. * @param int $id Record ID of user
  186. */
  187. public function setRecordId($id)
  188. {
  189. $this->id = $id;
  190. }
  191. /**
  192. * Set resource link ID of user.
  193. *
  194. * @param int $resourceLinkId Resource link ID of user
  195. */
  196. public function setResourceLinkId($resourceLinkId)
  197. {
  198. $this->resourceLinkId = $resourceLinkId;
  199. }
  200. /**
  201. * Get the data connector.
  202. *
  203. * @return mixed Data connector object or string
  204. */
  205. public function getDataConnector()
  206. {
  207. return $this->dataConnector;
  208. }
  209. /**
  210. * Get the user ID (which may be a compound of the tool consumer and resource link IDs).
  211. *
  212. * @param int $idScope Scope to use for user ID (optional, default is null for consumer default setting)
  213. *
  214. * @return string User ID value
  215. */
  216. public function getId($idScope = null)
  217. {
  218. if (empty($idScope)) {
  219. if (!is_null($this->resourceLink)) {
  220. $idScope = $this->resourceLink->getConsumer()->idScope;
  221. } else {
  222. $idScope = ToolProvider::ID_SCOPE_ID_ONLY;
  223. }
  224. }
  225. switch ($idScope) {
  226. case ToolProvider::ID_SCOPE_GLOBAL:
  227. $id = $this->getResourceLink()->getKey() . ToolProvider::ID_SCOPE_SEPARATOR . $this->ltiUserId;
  228. break;
  229. case ToolProvider::ID_SCOPE_CONTEXT:
  230. $id = $this->getResourceLink()->getKey();
  231. if ($this->resourceLink->ltiContextId) {
  232. $id .= ToolProvider::ID_SCOPE_SEPARATOR . $this->resourceLink->ltiContextId;
  233. }
  234. $id .= ToolProvider::ID_SCOPE_SEPARATOR . $this->ltiUserId;
  235. break;
  236. case ToolProvider::ID_SCOPE_RESOURCE:
  237. $id = $this->getResourceLink()->getKey();
  238. if ($this->resourceLink->ltiResourceLinkId) {
  239. $id .= ToolProvider::ID_SCOPE_SEPARATOR . $this->resourceLink->ltiResourceLinkId;
  240. }
  241. $id .= ToolProvider::ID_SCOPE_SEPARATOR . $this->ltiUserId;
  242. break;
  243. default:
  244. $id = $this->ltiUserId;
  245. break;
  246. }
  247. return $id;
  248. }
  249. /**
  250. * Set the user's name.
  251. *
  252. * @param string $firstname User's first name.
  253. * @param string $lastname User's last name.
  254. * @param string $fullname User's full name.
  255. */
  256. public function setNames($firstname, $lastname, $fullname)
  257. {
  258. $names = array(0 => '', 1 => '');
  259. if (!empty($fullname)) {
  260. $this->fullname = trim($fullname);
  261. $names = preg_split("/[\s]+/", $this->fullname, 2);
  262. }
  263. if (!empty($firstname)) {
  264. $this->firstname = trim($firstname);
  265. $names[0] = $this->firstname;
  266. } else if (!empty($names[0])) {
  267. $this->firstname = $names[0];
  268. } else {
  269. $this->firstname = 'User';
  270. }
  271. if (!empty($lastname)) {
  272. $this->lastname = trim($lastname);
  273. $names[1] = $this->lastname;
  274. } else if (!empty($names[1])) {
  275. $this->lastname = $names[1];
  276. } else {
  277. $this->lastname = $this->ltiUserId;
  278. }
  279. if (empty($this->fullname)) {
  280. $this->fullname = "{$this->firstname} {$this->lastname}";
  281. }
  282. }
  283. /**
  284. * Set the user's email address.
  285. *
  286. * @param string $email Email address value
  287. * @param string $defaultEmail Value to use if no email is provided (optional, default is none)
  288. */
  289. public function setEmail($email, $defaultEmail = null)
  290. {
  291. if (!empty($email)) {
  292. $this->email = $email;
  293. } else if (!empty($defaultEmail)) {
  294. $this->email = $defaultEmail;
  295. if (substr($this->email, 0, 1) === '@') {
  296. $this->email = $this->getId() . $this->email;
  297. }
  298. } else {
  299. $this->email = '';
  300. }
  301. }
  302. /**
  303. * Check if the user is an administrator (at any of the system, institution or context levels).
  304. *
  305. * @return boolean True if the user has a role of administrator
  306. */
  307. public function isAdmin()
  308. {
  309. return $this->hasRole('Administrator') || $this->hasRole('urn:lti:sysrole:ims/lis/SysAdmin') ||
  310. $this->hasRole('urn:lti:sysrole:ims/lis/Administrator') || $this->hasRole('urn:lti:instrole:ims/lis/Administrator');
  311. }
  312. /**
  313. * Check if the user is staff.
  314. *
  315. * @return boolean True if the user has a role of instructor, contentdeveloper or teachingassistant
  316. */
  317. public function isStaff()
  318. {
  319. return ($this->hasRole('Instructor') || $this->hasRole('ContentDeveloper') || $this->hasRole('TeachingAssistant'));
  320. }
  321. /**
  322. * Check if the user is a learner.
  323. *
  324. * @return boolean True if the user has a role of learner
  325. */
  326. public function isLearner()
  327. {
  328. return $this->hasRole('Learner');
  329. }
  330. /**
  331. * Load the user from the database.
  332. *
  333. * @param int $id Record ID of user
  334. * @param DataConnector $dataConnector Database connection object
  335. *
  336. * @return User User object
  337. */
  338. public static function fromRecordId($id, $dataConnector)
  339. {
  340. $user = new User();
  341. $user->dataConnector = $dataConnector;
  342. $user->load($id);
  343. return $user;
  344. }
  345. /**
  346. * Class constructor from resource link.
  347. *
  348. * @param ResourceLink $resourceLink Resource_Link object
  349. * @param string $ltiUserId User ID value
  350. * @return User
  351. */
  352. public static function fromResourceLink($resourceLink, $ltiUserId)
  353. {
  354. $user = new User();
  355. $user->resourceLink = $resourceLink;
  356. if (!is_null($resourceLink)) {
  357. $user->resourceLinkId = $resourceLink->getRecordId();
  358. $user->dataConnector = $resourceLink->getDataConnector();
  359. }
  360. $user->ltiUserId = $ltiUserId;
  361. if (!empty($ltiUserId)) {
  362. $user->load();
  363. }
  364. return $user;
  365. }
  366. ###
  367. ### PRIVATE METHODS
  368. ###
  369. /**
  370. * Check whether the user has a specified role name.
  371. *
  372. * @param string $role Name of role
  373. *
  374. * @return boolean True if the user has the specified role
  375. */
  376. private function hasRole($role) {
  377. if (substr($role, 0, 4) !== 'urn:')
  378. {
  379. $role = 'urn:lti:role:ims/lis/' . $role;
  380. }
  381. return in_array($role, $this->roles);
  382. }
  383. /**
  384. * Load the user from the database.
  385. *
  386. * @param int $id Record ID of user (optional, default is null)
  387. *
  388. * @return boolean True if the user object was successfully loaded
  389. */
  390. private function load($id = null)
  391. {
  392. $this->initialize();
  393. $this->id = $id;
  394. $dataConnector = $this->getDataConnector();
  395. if (!is_null($dataConnector)) {
  396. return $dataConnector->loadUser($this);
  397. }
  398. return false;
  399. }
  400. }