User.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * User entity file.
  4. *
  5. * Creates the entity and makes it available.
  6. *
  7. * @category Entity
  8. */
  9. /**
  10. * Require dependent entities if not present.
  11. */
  12. require_once(PATH::ENTITY . "Entity.php");
  13. require_once(PATH::ENTITY . "Player.php");
  14. /**
  15. * A User.
  16. *
  17. * Represents an object from the table 'user'.
  18. *
  19. * @category Entity
  20. */
  21. class User extends Entity{
  22. /**
  23. * @var int User ID.
  24. */
  25. private $id;
  26. /**
  27. * @var string User name.
  28. */
  29. private $name;
  30. /**
  31. * @var string User email.
  32. */
  33. private $mail;
  34. /**
  35. * @var string User API key.
  36. */
  37. private $api_key;
  38. /**
  39. * @var boolean Indicates if the user is an administrator.
  40. */
  41. private $admin = false;
  42. private $flag_accounts = false;
  43. /**
  44. * @var \Player[] All of the players accounts.
  45. */
  46. private $account = [];
  47. /**
  48. * Constructor.
  49. *
  50. * Searches the database and retrieves the information about the
  51. * user, populating it and it's items.
  52. *
  53. * @param int $id User identifier.
  54. */
  55. public function __construct($id){
  56. $statement = get_context()->get_db()->prepare("
  57. SELECT
  58. id,
  59. name,
  60. mail,
  61. api_key,
  62. admin
  63. FROM user
  64. WHERE id = :id;
  65. ");
  66. $statement->bindValue(':id', $id, SQLITE3_TEXT);
  67. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  68. if ($r){
  69. $this->id = $r["id"];
  70. $this->name = $r["name"];
  71. $this->mail = $r["mail"];
  72. $this->api_key = $r["api_key"];
  73. $this->admin = $r["admin"];
  74. }
  75. }
  76. private function load_accounts(){
  77. $statement = get_context()->get_db()->prepare("
  78. SELECT id
  79. FROM player
  80. WHERE user = :user
  81. ORDER BY last_access DESC
  82. ");
  83. $statement->bindValue(':user', $this->id, SQLITE3_TEXT);
  84. $q = $statement->execute();
  85. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  86. array_push($this->account, new Player($r["id"]));
  87. }
  88. $this->flag_accounts = true;
  89. }
  90. /**
  91. * Retrieves the user identifier.
  92. *
  93. * @return number User ID.
  94. */
  95. public function get_id(){
  96. return $this->id;
  97. }
  98. /**
  99. * Retrieves the user name.
  100. *
  101. * @return string User name.
  102. */
  103. public function get_name(){
  104. return $this->name;
  105. }
  106. /**
  107. * Retrieves the user email address.
  108. *
  109. * @return string User mail.
  110. */
  111. public function get_mail(){
  112. return $this->mail;
  113. }
  114. /**
  115. * Retrieves the user's API key.
  116. *
  117. * @return string User's API key.
  118. */
  119. public function get_api_key(){
  120. return $this->api_key;
  121. }
  122. /**
  123. * Checks if the user is a site administrator.
  124. *
  125. * @return boolean True if the user is administrator, false otherwise.
  126. */
  127. public function is_admin(){
  128. return $this->admin;
  129. }
  130. /**
  131. * Retrieves the user's accounts, sorted by the last account date in descending order.
  132. *
  133. * @return Player[] The user's accounts.
  134. */
  135. public function get_accounts(){
  136. if (! $this->flag_accounts){
  137. $this->load_accounts();
  138. }
  139. return $this->account;
  140. }
  141. }
  142. ?>