| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- <?php
- class Timer {
- private $conn;
- private $table_name = 'timers';
-
- public $id;
- public $task_id;
- public $user_id;
- public $start_time;
- public $end_time;
- public $duration;
- public $description;
- public $created_at;
- public $updated_at;
-
- public function __construct($db) {
- $this->conn = $db;
- }
-
- public function create() {
- $query = "INSERT INTO " . $this->table_name . "
- SET task_id=:task_id, user_id=:user_id, start_time=:start_time,
- end_time=:end_time, duration=:duration, description=:description,
- created_at=:created_at, updated_at=:updated_at";
-
- $stmt = $this->conn->prepare($query);
-
- // Handle null values properly
- $this->task_id = $this->task_id ? htmlspecialchars(strip_tags($this->task_id)) : null;
- $this->user_id = htmlspecialchars(strip_tags($this->user_id));
- $this->start_time = htmlspecialchars(strip_tags($this->start_time));
- $this->end_time = $this->end_time ? htmlspecialchars(strip_tags($this->end_time)) : null;
- $this->duration = $this->duration ? htmlspecialchars(strip_tags($this->duration)) : null;
- $this->description = $this->description ? htmlspecialchars(strip_tags($this->description)) : null;
- $this->created_at = gmdate('Y-m-d H:i:s');
- $this->updated_at = gmdate('Y-m-d H:i:s');
-
- $stmt->bindParam(":task_id", $this->task_id);
- $stmt->bindParam(":user_id", $this->user_id);
- $stmt->bindParam(":start_time", $this->start_time);
- $stmt->bindParam(":end_time", $this->end_time);
- $stmt->bindParam(":duration", $this->duration);
- $stmt->bindParam(":description", $this->description);
- $stmt->bindParam(":created_at", $this->created_at);
- $stmt->bindParam(":updated_at", $this->updated_at);
-
- if($stmt->execute()) {
- return true;
- }
-
- return false;
- }
-
- public function read() {
- $query = "SELECT t.*, u.first_name, u.last_name
- FROM " . $this->table_name . " t
- LEFT JOIN users u ON t.user_id = u.id
- LEFT JOIN tasks ta ON t.task_id = ta.id
- ORDER BY t.created_at DESC";
-
- $stmt = $this->conn->prepare($query);
- $stmt->execute();
-
- return $stmt;
- }
-
- public function readActive() {
- $query = "SELECT t.*, u.first_name, u.last_name, ta.title as task_title
- FROM " . $this->table_name . " t
- LEFT JOIN users u ON t.user_id = u.id
- LEFT JOIN tasks ta ON t.task_id = ta.id
- WHERE t.end_time IS NULL
- ORDER BY t.created_at DESC";
-
- $stmt = $this->conn->prepare($query);
- $stmt->execute();
-
- return $stmt;
- }
-
- public function readByTask($task_id) {
- $query = "SELECT t.*, u.first_name, u.last_name
- FROM " . $this->table_name . " t
- LEFT JOIN users u ON t.user_id = u.id
- LEFT JOIN tasks ta ON t.task_id = ta.id
- WHERE t.task_id = ?
- ORDER BY t.created_at DESC";
-
- $stmt = $this->conn->prepare($query);
- $stmt->bindParam(1, $task_id);
- $stmt->execute();
-
- return $stmt;
- }
-
- public function readOne() {
- $query = "SELECT t.*, u.first_name, u.last_name, ta.title as task_title
- FROM " . $this->table_name . " t
- LEFT JOIN users u ON t.user_id = u.id
- LEFT JOIN tasks ta ON t.task_id = ta.id
- WHERE t.id = ? LIMIT 0,1";
-
- $stmt = $this->conn->prepare($query);
- $stmt->bindParam(1, $this->id);
- $stmt->execute();
-
- $row = $stmt->fetch(PDO::FETCH_ASSOC);
-
- $this->task_id = $row['task_id'];
- $this->user_id = $row['user_id'];
- $this->start_time = $row['start_time'];
- $this->end_time = $row['end_time'];
- $this->duration = $row['duration'];
- $this->description = $row['description'];
- $this->created_at = $row['created_at'];
- $this->updated_at = $row['updated_at'];
- }
-
- public function update() {
- $query = "UPDATE " . $this->table_name . "
- SET task_id=:task_id, user_id=:user_id, start_time=:start_time,
- end_time=:end_time, duration=:duration, description=:description,
- updated_at=:updated_at
- WHERE id=:id";
-
- $stmt = $this->conn->prepare($query);
-
- $this->task_id = htmlspecialchars(strip_tags($this->task_id));
- $this->user_id = htmlspecialchars(strip_tags($this->user_id));
- $this->start_time = htmlspecialchars(strip_tags($this->start_time));
- $this->end_time = htmlspecialchars(strip_tags($this->end_time));
- $this->duration = htmlspecialchars(strip_tags($this->duration));
- $this->description = htmlspecialchars(strip_tags($this->description));
- $this->updated_at = gmdate('Y-m-d H:i:s');
-
- $stmt->bindParam(":task_id", $this->task_id);
- $stmt->bindParam(":user_id", $this->user_id);
- $stmt->bindParam(":start_time", $this->start_time);
- $stmt->bindParam(":end_time", $this->end_time);
- $stmt->bindParam(":duration", $this->duration);
- $stmt->bindParam(":description", $this->description);
- $stmt->bindParam(":updated_at", $this->updated_at);
- $stmt->bindParam(":id", $this->id);
-
- if($stmt->execute()) {
- return true;
- }
-
- return false;
- }
-
- public function delete() {
- $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
-
- $stmt = $this->conn->prepare($query);
- $stmt->bindParam(1, $this->id);
-
- if($stmt->execute()) {
- return true;
- }
-
- return false;
- }
-
- public function stop($id) {
- $this->end_time = gmdate('Y-m-d H:i:s');
- $this->duration = $this->calculateDuration($this->start_time, $this->end_time);
-
- $query = "UPDATE " . $this->table_name . "
- SET end_time=:end_time, duration=:duration, updated_at=:updated_at
- WHERE id=:id";
-
- $stmt = $this->conn->prepare($query);
- $stmt->bindParam(":end_time", $this->end_time);
- $stmt->bindParam(":duration", $this->duration);
- $stmt->bindParam(":updated_at", gmdate('Y-m-d H:i:s'));
- $stmt->bindParam(":id", $id);
-
- if($stmt->execute()) {
- return true;
- }
-
- return false;
- }
-
- private function calculateDuration($start_time, $end_time) {
- $start = new DateTime($start_time);
- $end = new DateTime($end_time);
- $interval = $start->diff($end);
-
- $hours = $interval->format('%H');
- $minutes = $interval->format('%I');
- $seconds = $interval->format('%s');
-
- return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
- }
- }
- ?>
|