User.inc 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. class User {
  3. private $data;
  4. public function populate() {
  5. $session = new Session();
  6. $userId = $session->getValue("logged_in");
  7. if($userId) {
  8. $database = new Database();
  9. $sql = sprintf("SELECT * FROM users WHERE user_id = '%s'", $userId);
  10. $result = $database->query($sql);
  11. $this->data = $result[0];
  12. $sql = sprintf("SELECT * FROM user_topic_acl WHERE user_id = '%s'", $userId);
  13. $result = $database->query($sql);
  14. $this->acl = array();
  15. foreach($result as $userAccess) {
  16. $this->acl[$userAccess['topic_id']] = $userAccess['rank_id'];
  17. }
  18. } else {
  19. //throw new SessionExpiredException();
  20. //shouldn't throw, but something needs to be done if the user isn't logged in
  21. }
  22. }
  23. public function getUserName() {
  24. return $this->data['username'];
  25. }
  26. public function getDisplayName() {
  27. return $this->data['display_name'];
  28. }
  29. public function getId() {
  30. return $this->data['user_id'];
  31. }
  32. public function getEmail() {
  33. return $this->data['email'];
  34. }
  35. public function isTopicModerator($topicId) {
  36. return isset($this->acl[$topicId]) && $this->acl[$topicId] >= 3;
  37. }
  38. public function isTopicOwner($topicId) {
  39. return isset($this->acl[$topicId]) && $this->acl[$topicId] >= 2;
  40. }
  41. public function isLoggedIn() {
  42. $session = new Session();
  43. return $session->getValue("logged_in");
  44. }
  45. public function markPostViewed($postId) {
  46. if($this->isLoggedIn()) {
  47. $sql = sprintf("INSERT INTO user_post_viewed (user_id, post_id) VALUES (%s, %s) ON DUPLICATE KEY UPDATE last_viewed = NOW()",
  48. $this->getId(), $postId);
  49. $database = new Database();
  50. $database->write($sql);
  51. }
  52. }
  53. }