41 lines
1.9 KiB
PHP
41 lines
1.9 KiB
PHP
<?php
|
|
require_once 'db.php';
|
|
require_once 'functions.php';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$episode_id = (int)($_POST['episode_id'] ?? 0);
|
|
$duration = (int)($_POST['duration'] ?? 0);
|
|
$session_id = $_POST['session_id'] ?? null;
|
|
|
|
if ($episode_id > 0) {
|
|
try {
|
|
// Check if this is a new session to log as "Listener Started"
|
|
$checkStmt = $pdo->prepare("SELECT COUNT(*) FROM plays WHERE session_id = ?");
|
|
$checkStmt->execute([$session_id]);
|
|
$isNewSession = ($checkStmt->fetchColumn() == 0);
|
|
|
|
$sql = "INSERT INTO plays (episode_id, duration, session_id) VALUES (?, ?, ?)
|
|
ON DUPLICATE KEY UPDATE duration = duration + VALUES(duration)";
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute([$episode_id, $duration, $session_id]);
|
|
|
|
if ($isNewSession && $session_id) {
|
|
$titleStmt = $pdo->prepare("SELECT title FROM episodes WHERE id = ?");
|
|
$titleStmt->execute([$episode_id]);
|
|
$title = $titleStmt->fetchColumn();
|
|
logActivity(null, 'LISTENER_STARTED', "Episode: $title (Session: $session_id)");
|
|
}
|
|
|
|
file_put_contents('debug.log', date('[Y-m-d H:i:s] ') . "SUCCESS: Logged play for episode $episode_id ($duration s, session: $session_id)" . PHP_EOL, FILE_APPEND);
|
|
echo json_encode(['status' => 'success']);
|
|
} catch (PDOException $e) {
|
|
file_put_contents('debug.log', date('[Y-m-d H:i:s] ') . "DB ERROR: " . $e->getMessage() . PHP_EOL, FILE_APPEND);
|
|
echo json_encode(['status' => 'error', 'message' => 'Database error']);
|
|
}
|
|
} else {
|
|
file_put_contents('debug.log', date('[Y-m-d H:i:s] ') . "INVALID DATA: ID=$episode_id, DUR=$duration" . PHP_EOL, FILE_APPEND);
|
|
echo json_encode(['status' => 'error', 'message' => 'Invalid episode ID']);
|
|
}
|
|
}
|
|
?>
|