timer_history.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. header('Content-Type: application/json');
  3. header('Access-Control-Allow-Origin: *');
  4. header('Access-Control-Allow-Methods: GET');
  5. header('Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With');
  6. require_once '../config/database.php';
  7. $database = new Database();
  8. $conn = $database->getConnection();
  9. $task_id = $_GET['task_id'] ?? null;
  10. $date = $_GET['date'] ?? null;
  11. try {
  12. // Simple database query for timer history
  13. $query = "SELECT t.*, u.first_name, u.last_name, COALESCE(ta.title, 'Ei tehtävää') as task_title
  14. FROM timers t
  15. LEFT JOIN users u ON t.user_id = u.id
  16. LEFT JOIN tasks ta ON t.task_id = ta.id
  17. ORDER BY t.created_at DESC";
  18. $stmt = $conn->prepare($query);
  19. $stmt->execute();
  20. $timers = [];
  21. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  22. // Filter by date if provided
  23. if ($date && $row['start_time']) {
  24. $timerDate = date('Y-m-d', strtotime($row['start_time']));
  25. if ($timerDate !== $date) {
  26. continue;
  27. }
  28. }
  29. // Filter by task_id if provided
  30. if ($task_id && $row['task_id'] != $task_id) {
  31. continue;
  32. }
  33. $timers[] = $row;
  34. }
  35. echo json_encode(['success' => true, 'data' => $timers]);
  36. } catch (Exception $e) {
  37. echo json_encode(['success' => false, 'error' => $e->getMessage()]);
  38. }
  39. ?>