Files
edu/bbs/api/get_video_time.php

82 lines
2.3 KiB
PHP

<?php
/**
* 영상 시청 시간 조회 API
* POST /bbs/api/get_video_time.php
*
* Parameters:
* - content_id (필수): 영상 content ID (string)
*
* Returns:
* - watch_tm : edu_learning_histories.watch_tm (이어보기 기준 시간, 초)
* - content_tm : edu_learning_histories.content_tm (영상 전체 길이, 초)
*/
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
header('Content-Type: application/json; charset=utf-8');
$member_id = trim((string)($_SESSION['member_id'] ?? ''));
$sys_comp_code = trim((string)($_SESSION['sys_comp_code'] ?? ''));
if ($member_id === '') {
http_response_code(401);
echo json_encode(['success' => false, 'error' => 'Unauthorized']);
exit;
}
if ($sys_comp_code === '') {
http_response_code(401);
echo json_encode(['success' => false, 'error' => 'sys_comp_code_missing']);
exit;
}
// content_id: JSON body 또는 POST 폼 데이터 모두 허용
$rawBody = file_get_contents('php://input');
$jsonBody = (is_string($rawBody) && $rawBody !== '') ? json_decode($rawBody, true) : null;
$content_id = trim((string)(
(is_array($jsonBody) ? ($jsonBody['content_id'] ?? '') : '') ?:
($_POST['content_id'] ?? '') ?:
($_GET['content_id'] ?? '')
));
if ($content_id === '') {
http_response_code(400);
echo json_encode(['success' => false, 'error' => 'Invalid content_id']);
exit;
}
try {
require_once __DIR__ . '/../db_conn.php';
$pdo = db_conn();
// edu_learning_histories 에서 watch_tm / content_tm 조회
$stmt = $pdo->prepare(
"SELECT watch_tm, content_tm
FROM edu_learning_histories
WHERE member_id = ?
AND sys_comp_code = ?
AND content_id = ?
LIMIT 1"
);
$stmt->execute([$member_id, $sys_comp_code, $content_id]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$watch_tm = (int)($row['watch_tm'] ?? 0);
$content_tm = (int)($row['content_tm'] ?? 0);
echo json_encode([
'success' => true,
'content_id' => $content_id,
'watch_tm' => $watch_tm,
'content_tm' => $content_tm,
], JSON_UNESCAPED_UNICODE);
} catch (Throwable $e) {
error_log('[get_video_time.php] Error: ' . $e->getMessage());
http_response_code(500);
echo json_encode(['success' => false, 'error' => 'server_error']);
}
?>