Timer.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. class Timer {
  3. private $conn;
  4. private $table_name = 'timers';
  5. public $id;
  6. public $task_id;
  7. public $user_id;
  8. public $start_time;
  9. public $end_time;
  10. public $duration;
  11. public $description;
  12. public $created_at;
  13. public $updated_at;
  14. public function __construct($db) {
  15. $this->conn = $db;
  16. }
  17. public function create() {
  18. $query = "INSERT INTO " . $this->table_name . "
  19. SET task_id=:task_id, user_id=:user_id, start_time=:start_time,
  20. end_time=:end_time, duration=:duration, description=:description,
  21. created_at=:created_at, updated_at=:updated_at";
  22. $stmt = $this->conn->prepare($query);
  23. // Handle null values properly
  24. $this->task_id = $this->task_id ? htmlspecialchars(strip_tags($this->task_id)) : null;
  25. $this->user_id = htmlspecialchars(strip_tags($this->user_id));
  26. $this->start_time = htmlspecialchars(strip_tags($this->start_time));
  27. $this->end_time = $this->end_time ? htmlspecialchars(strip_tags($this->end_time)) : null;
  28. $this->duration = $this->duration ? htmlspecialchars(strip_tags($this->duration)) : null;
  29. $this->description = $this->description ? htmlspecialchars(strip_tags($this->description)) : null;
  30. $this->created_at = gmdate('Y-m-d H:i:s');
  31. $this->updated_at = gmdate('Y-m-d H:i:s');
  32. $stmt->bindParam(":task_id", $this->task_id);
  33. $stmt->bindParam(":user_id", $this->user_id);
  34. $stmt->bindParam(":start_time", $this->start_time);
  35. $stmt->bindParam(":end_time", $this->end_time);
  36. $stmt->bindParam(":duration", $this->duration);
  37. $stmt->bindParam(":description", $this->description);
  38. $stmt->bindParam(":created_at", $this->created_at);
  39. $stmt->bindParam(":updated_at", $this->updated_at);
  40. if($stmt->execute()) {
  41. return true;
  42. }
  43. return false;
  44. }
  45. public function read() {
  46. $query = "SELECT t.*, u.first_name, u.last_name
  47. FROM " . $this->table_name . " t
  48. LEFT JOIN users u ON t.user_id = u.id
  49. LEFT JOIN tasks ta ON t.task_id = ta.id
  50. ORDER BY t.created_at DESC";
  51. $stmt = $this->conn->prepare($query);
  52. $stmt->execute();
  53. return $stmt;
  54. }
  55. public function readActive() {
  56. $query = "SELECT t.*, u.first_name, u.last_name, ta.title as task_title
  57. FROM " . $this->table_name . " t
  58. LEFT JOIN users u ON t.user_id = u.id
  59. LEFT JOIN tasks ta ON t.task_id = ta.id
  60. WHERE t.end_time IS NULL
  61. ORDER BY t.created_at DESC";
  62. $stmt = $this->conn->prepare($query);
  63. $stmt->execute();
  64. return $stmt;
  65. }
  66. public function readByTask($task_id) {
  67. $query = "SELECT t.*, u.first_name, u.last_name
  68. FROM " . $this->table_name . " t
  69. LEFT JOIN users u ON t.user_id = u.id
  70. LEFT JOIN tasks ta ON t.task_id = ta.id
  71. WHERE t.task_id = ?
  72. ORDER BY t.created_at DESC";
  73. $stmt = $this->conn->prepare($query);
  74. $stmt->bindParam(1, $task_id);
  75. $stmt->execute();
  76. return $stmt;
  77. }
  78. public function readOne() {
  79. $query = "SELECT t.*, u.first_name, u.last_name, ta.title as task_title
  80. FROM " . $this->table_name . " t
  81. LEFT JOIN users u ON t.user_id = u.id
  82. LEFT JOIN tasks ta ON t.task_id = ta.id
  83. WHERE t.id = ? LIMIT 0,1";
  84. $stmt = $this->conn->prepare($query);
  85. $stmt->bindParam(1, $this->id);
  86. $stmt->execute();
  87. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  88. $this->task_id = $row['task_id'];
  89. $this->user_id = $row['user_id'];
  90. $this->start_time = $row['start_time'];
  91. $this->end_time = $row['end_time'];
  92. $this->duration = $row['duration'];
  93. $this->description = $row['description'];
  94. $this->created_at = $row['created_at'];
  95. $this->updated_at = $row['updated_at'];
  96. }
  97. public function update() {
  98. $query = "UPDATE " . $this->table_name . "
  99. SET task_id=:task_id, user_id=:user_id, start_time=:start_time,
  100. end_time=:end_time, duration=:duration, description=:description,
  101. updated_at=:updated_at
  102. WHERE id=:id";
  103. $stmt = $this->conn->prepare($query);
  104. $this->task_id = htmlspecialchars(strip_tags($this->task_id));
  105. $this->user_id = htmlspecialchars(strip_tags($this->user_id));
  106. $this->start_time = htmlspecialchars(strip_tags($this->start_time));
  107. $this->end_time = htmlspecialchars(strip_tags($this->end_time));
  108. $this->duration = htmlspecialchars(strip_tags($this->duration));
  109. $this->description = htmlspecialchars(strip_tags($this->description));
  110. $this->updated_at = gmdate('Y-m-d H:i:s');
  111. $stmt->bindParam(":task_id", $this->task_id);
  112. $stmt->bindParam(":user_id", $this->user_id);
  113. $stmt->bindParam(":start_time", $this->start_time);
  114. $stmt->bindParam(":end_time", $this->end_time);
  115. $stmt->bindParam(":duration", $this->duration);
  116. $stmt->bindParam(":description", $this->description);
  117. $stmt->bindParam(":updated_at", $this->updated_at);
  118. $stmt->bindParam(":id", $this->id);
  119. if($stmt->execute()) {
  120. return true;
  121. }
  122. return false;
  123. }
  124. public function delete() {
  125. $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
  126. $stmt = $this->conn->prepare($query);
  127. $stmt->bindParam(1, $this->id);
  128. if($stmt->execute()) {
  129. return true;
  130. }
  131. return false;
  132. }
  133. public function stop($id) {
  134. $this->end_time = gmdate('Y-m-d H:i:s');
  135. $this->duration = $this->calculateDuration($this->start_time, $this->end_time);
  136. $query = "UPDATE " . $this->table_name . "
  137. SET end_time=:end_time, duration=:duration, updated_at=:updated_at
  138. WHERE id=:id";
  139. $stmt = $this->conn->prepare($query);
  140. $stmt->bindParam(":end_time", $this->end_time);
  141. $stmt->bindParam(":duration", $this->duration);
  142. $stmt->bindParam(":updated_at", gmdate('Y-m-d H:i:s'));
  143. $stmt->bindParam(":id", $id);
  144. if($stmt->execute()) {
  145. return true;
  146. }
  147. return false;
  148. }
  149. private function calculateDuration($start_time, $end_time) {
  150. $start = new DateTime($start_time);
  151. $end = new DateTime($end_time);
  152. $interval = $start->diff($end);
  153. $hours = $interval->format('%H');
  154. $minutes = $interval->format('%I');
  155. $seconds = $interval->format('%s');
  156. return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
  157. }
  158. }
  159. ?>