user.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace Model;
  3. class User extends \Model {
  4. protected
  5. $_table_name = "user",
  6. $_groupUsers = null;
  7. /**
  8. * Load currently logged in user, if any
  9. * @return mixed
  10. */
  11. public function loadCurrent() {
  12. $f3 = \Base::instance();
  13. // Load current session
  14. $session = new \Model\Session;
  15. $session->loadCurrent();
  16. // Load user
  17. if($session->user_id) {
  18. $this->load(array("id = ? AND deleted_date IS NULL", $session->user_id));
  19. if($this->id) {
  20. $f3->set("user", $this->cast());
  21. $f3->set("user_obj", $this);
  22. // Change default language if user has selected one
  23. if($this->exists("language") && $this->language) {
  24. $f3->set("LANGUAGE", $this->language);
  25. }
  26. }
  27. }
  28. return $this;
  29. }
  30. /**
  31. * Get path to user's avatar or gravatar
  32. * @param integer $size
  33. * @return string|bool
  34. */
  35. public function avatar($size = 80) {
  36. if(!$this->id) {
  37. return false;
  38. }
  39. if($this->get("avatar_filename") && is_file("uploads/avatars/" . $this->get("avatar_filename"))) {
  40. return "/avatar/$size-" . $this->id . ".png";
  41. }
  42. return \Helper\View::instance()->gravatar($this->get("email"), $size);
  43. }
  44. /**
  45. * Load all active users
  46. * @return array
  47. */
  48. public function getAll() {
  49. return $this->find("deleted_date IS NULL AND role != 'group'", array("order" => "name ASC"));
  50. }
  51. /**
  52. * Load all active groups
  53. * @return array
  54. */
  55. public function getAllGroups() {
  56. return $this->find("deleted_date IS NULL AND role = 'group'", array("order" => "name ASC"));
  57. }
  58. /**
  59. * Get all users within a group
  60. * @return array|NULL
  61. */
  62. public function getGroupUsers() {
  63. if($this->role == "group") {
  64. if($this->_groupUsers !== null) {
  65. return $this->_groupUsers;
  66. }
  67. $ug = new User\Group;
  68. $users = $ug->find(array("group_id = ?", $this->id));
  69. $user_ids = array();
  70. foreach($users as $user) {
  71. $user_ids[] = $user->user_id;
  72. }
  73. return $this->_groupUsers = $user_ids ? $this->find("id IN (" . implode(",", $user_ids) . ") AND deleted_date IS NULL") : array();
  74. } else {
  75. return null;
  76. }
  77. }
  78. /**
  79. * Send an email alert with issues due on the given date
  80. * @param string $date
  81. * @return bool
  82. */
  83. public function sendDueAlert($date = '') {
  84. if(!$this->id) {
  85. return false;
  86. }
  87. if(!$date) {
  88. $date = date("Y-m-d", \Helper\View::instance()->utc2local());
  89. }
  90. $issue = new \Model\Issue;
  91. $issues = $issue->find(array("due_date = ? AND owner_id = ? AND closed_date IS NULL AND deleted_date IS NULL", $date, $this->id), array("order" => "priority DESC"));
  92. if($issues) {
  93. $notif = new \Helper\Notification;
  94. return $notif->user_due_issues($this, $issues);
  95. } else {
  96. return false;
  97. }
  98. }
  99. /**
  100. * Get user statistics
  101. * @param int $time The lower limit on timestamps for stats collection
  102. * @return array
  103. */
  104. public function stats($time = 0) {
  105. \Helper\View::instance()->utc2local();
  106. $offset = \Base::instance()->get("site.timeoffset");
  107. if(!$time) {
  108. $time = strtotime("-2 weeks", time() + $offset);
  109. }
  110. $result = array();
  111. $result["spent"] = $this->db->exec(
  112. "SELECT DATE(DATE_ADD(u.created_date, INTERVAL :offset SECOND)) AS `date`, SUM(f.new_value - f.old_value) AS `val`
  113. FROM issue_update u
  114. JOIN issue_update_field f ON u.id = f.issue_update_id AND f.field = 'hours_spent'
  115. WHERE u.user_id = :user AND u.created_date > :date
  116. GROUP BY DATE(DATE_ADD(u.created_date, INTERVAL :offset2 SECOND))",
  117. array(":user" => $this->id, ":offset" => $offset, ":offset2" => $offset, ":date" => date("Y-m-d H:i:s", $time))
  118. );
  119. $result["closed"] = $this->db->exec(
  120. "SELECT DATE(DATE_ADD(i.closed_date, INTERVAL :offset SECOND)) AS `date`, COUNT(*) AS `val`
  121. FROM issue i
  122. WHERE i.owner_id = :user AND i.closed_date > :date
  123. GROUP BY DATE(DATE_ADD(i.closed_date, INTERVAL :offset2 SECOND))",
  124. array(":user" => $this->id, ":offset" => $offset, ":offset2" => $offset, ":date" => date("Y-m-d H:i:s", $time))
  125. );
  126. $result["created"] = $this->db->exec(
  127. "SELECT DATE(DATE_ADD(i.created_date, INTERVAL :offset SECOND)) AS `date`, COUNT(*) AS `val`
  128. FROM issue i
  129. WHERE i.author_id = :user AND i.created_date > :date
  130. GROUP BY DATE(DATE_ADD(i.created_date, INTERVAL :offset2 SECOND))",
  131. array(":user" => $this->id, ":offset" => $offset, ":offset2" => $offset, ":date" => date("Y-m-d H:i:s", $time))
  132. );
  133. $dates = $this->_createDateRangeArray(date("Y-m-d", $time), date("Y-m-d", time() + $offset));
  134. $return = array(
  135. "labels" => array(),
  136. "spent" => array(),
  137. "closed" => array(),
  138. "created" => array()
  139. );
  140. foreach($result["spent"] as $r) {
  141. $return["spent"][$r["date"]] = floatval($r["val"]);
  142. }
  143. foreach($result["closed"] as $r) {
  144. $return["closed"][$r["date"]] = intval($r["val"]);
  145. }
  146. foreach($result["created"] as $r) {
  147. $return["created"][$r["date"]] = intval($r["val"]);
  148. }
  149. foreach($dates as $date) {
  150. $return["labels"][$date] = date("D j", strtotime($date));
  151. if(!isset($return["spent"][$date])) {
  152. $return["spent"][$date] = 0;
  153. }
  154. if(!isset($return["closed"][$date])) {
  155. $return["closed"][$date] = 0;
  156. }
  157. if(!isset($return["created"][$date])) {
  158. $return["created"][$date] = 0;
  159. }
  160. }
  161. foreach($return as &$r) {
  162. ksort($r);
  163. }
  164. return $return;
  165. }
  166. }