3ae6c1ae2e
Scans nested course folders for mp4s, tracks per-user resume position and per-folder progress. SPA frontend with hash routing over a JSON API; range-request streaming via stream.php.
65 lines
2.3 KiB
PHP
65 lines
2.3 KiB
PHP
<?php
|
|
// GET api/progress.php?video_id=N -> saved progress for this user/video
|
|
// POST api/progress.php -> save progress
|
|
// JSON body: {video_id, position, duration}
|
|
// (also accepts form-encoded fields; used by navigator.sendBeacon)
|
|
require_once __DIR__ . '/../includes/auth.php';
|
|
require_once __DIR__ . '/../includes/functions.php';
|
|
|
|
header('Content-Type: application/json');
|
|
$userId = requireLoginApi();
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
|
$videoId = (int)($_GET['video_id'] ?? 0);
|
|
$stmt = db()->prepare(
|
|
'SELECT position_seconds, duration_seconds, completed
|
|
FROM video_progress WHERE user_id = ? AND video_id = ?'
|
|
);
|
|
$stmt->execute([$userId, $videoId]);
|
|
echo json_encode($stmt->fetch() ?: ['position_seconds' => 0, 'duration_seconds' => null, 'completed' => 0]);
|
|
exit;
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
http_response_code(405);
|
|
echo json_encode(['error' => 'Method not allowed']);
|
|
exit;
|
|
}
|
|
|
|
$raw = file_get_contents('php://input');
|
|
$data = json_decode($raw, true);
|
|
if (!is_array($data)) {
|
|
parse_str($raw, $data); // sendBeacon posts form-encoded
|
|
}
|
|
|
|
$videoId = (int)($data['video_id'] ?? 0);
|
|
$position = max(0, (float)($data['position'] ?? 0));
|
|
$duration = (float)($data['duration'] ?? 0);
|
|
|
|
if ($videoId <= 0 || !getVideo($videoId)) {
|
|
http_response_code(400);
|
|
echo json_encode(['error' => 'Unknown video']);
|
|
exit;
|
|
}
|
|
|
|
$completed = ($duration > 0 && $position / $duration >= COMPLETE_THRESHOLD) ? 1 : 0;
|
|
|
|
$db = db();
|
|
$stmt = $db->prepare(
|
|
'INSERT INTO video_progress (user_id, video_id, position_seconds, duration_seconds, completed)
|
|
VALUES (?, ?, ?, ?, ?)
|
|
ON DUPLICATE KEY UPDATE
|
|
position_seconds = VALUES(position_seconds),
|
|
duration_seconds = VALUES(duration_seconds),
|
|
completed = GREATEST(completed, VALUES(completed))' // once complete, stays complete
|
|
);
|
|
$stmt->execute([$userId, $videoId, $position, $duration > 0 ? $duration : null, $completed]);
|
|
|
|
// Cache the duration on the video row so listings can show it without ffprobe.
|
|
if ($duration > 0) {
|
|
$db->prepare('UPDATE videos SET duration_seconds = ? WHERE id = ? AND duration_seconds IS NULL')
|
|
->execute([$duration, $videoId]);
|
|
}
|
|
|
|
echo json_encode(['ok' => true, 'completed' => (bool)$completed]);
|