45 lines
1.6 KiB
PHP
45 lines
1.6 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../includes/bootstrap.php';
|
|
$staff = require_staff_login();
|
|
header('Content-Type: application/json');
|
|
|
|
$ticketId = (int)($_GET['ticket_id'] ?? 0);
|
|
$action = $_GET['action'] ?? 'ping';
|
|
$pdo = get_db();
|
|
|
|
if (!$ticketId) { echo json_encode([]); exit; }
|
|
|
|
if ($action === 'leave') {
|
|
$pdo->prepare("DELETE FROM ticket_presence WHERE ticket_id=? AND staff_id=?")
|
|
->execute([$ticketId, $staff['id']]);
|
|
echo json_encode(['ok' => true]);
|
|
exit;
|
|
}
|
|
|
|
if ($action === 'ping') {
|
|
// Eigene Präsenz aktualisieren
|
|
$presenceSql = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME) === 'sqlite'
|
|
? 'INSERT INTO ticket_presence (ticket_id, staff_id, last_seen_at) VALUES (?,?,?)
|
|
ON CONFLICT(ticket_id, staff_id) DO UPDATE SET last_seen_at=excluded.last_seen_at'
|
|
: 'INSERT INTO ticket_presence (ticket_id, staff_id, last_seen_at) VALUES (?,?,?)
|
|
ON DUPLICATE KEY UPDATE last_seen_at=VALUES(last_seen_at)';
|
|
$pdo->prepare($presenceSql)->execute([$ticketId, $staff['id'], db_now()]);
|
|
|
|
// Alte Einträge (> 30 Sek.) löschen
|
|
$pdo->prepare('DELETE FROM ticket_presence WHERE last_seen_at < ?')
|
|
->execute([date('Y-m-d H:i:s', time() - 30)]);
|
|
}
|
|
|
|
// Alle aktiven Mitarbeiter auf diesem Ticket zurückgeben
|
|
$activeSince = date('Y-m-d H:i:s', time() - 30);
|
|
$stmt = $pdo->prepare(
|
|
"SELECT s.id, s.name, tp.last_seen_at
|
|
FROM ticket_presence tp
|
|
JOIN staff s ON s.id = tp.staff_id
|
|
WHERE tp.ticket_id = ? AND tp.staff_id != ?
|
|
AND tp.last_seen_at >= ?
|
|
ORDER BY s.name"
|
|
);
|
|
$stmt->execute([$ticketId, $staff['id'], $activeSince]);
|
|
echo json_encode($stmt->fetchAll());
|