Files
edu/bbs/api/get_recommend_videos_goal.php
T

140 lines
3.8 KiB
PHP

<?php
//마이클래스 영상모달 관련영상 리스트 API - 목표코드(goal_code) 기반
declare(strict_types=1);
header('Content-Type: application/json; charset=utf-8');
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
require_once __DIR__ . '/../db_conn.php';
$memberId = (string)($_SESSION['member_id'] ?? '');
$sysCompCode = (string)($_SESSION['sys_comp_code'] ?? '');
if ($memberId === '') {
http_response_code(401);
echo json_encode(['success' => false, 'message' => 'not_logged_in'], JSON_UNESCAPED_UNICODE);
exit;
}
$contentId = trim((string)($_GET['content_id'] ?? ''));
if ($contentId === '') {
http_response_code(400);
echo json_encode(['success' => false, 'message' => 'content_id_required'], JSON_UNESCAPED_UNICODE);
exit;
}
try {
$pdo = db_conn();
$stmtCurrent = $pdo->prepare(
"SELECT content_id, goal_code
FROM edu_contents
WHERE content_id = ?
AND is_active = 1
LIMIT 1"
);
$stmtCurrent->execute([$contentId]);
$current = $stmtCurrent->fetch(PDO::FETCH_ASSOC) ?: null;
if (!$current) {
echo json_encode([
'success' => true,
'mode' => 'goal_code',
'goal_code' => null,
'keywords' => [],
'videos' => [],
], JSON_UNESCAPED_UNICODE);
exit;
}
$goalCode = trim((string)($current['goal_code'] ?? ''));
if ($goalCode === '') {
echo json_encode([
'success' => true,
'mode' => 'goal_code',
'goal_code' => '',
'keywords' => [],
'videos' => [],
], JSON_UNESCAPED_UNICODE);
exit;
}
$stmtVideos = $pdo->prepare(
"SELECT
c.content_id,
c.title,
c.content_url,
c.thumbnail_url,
c.category_code,
c.category_group,
c.description,
COALESCE(ec.code_name, c.category_code) AS category_name,
COALESCE(ec_grp.code_name, c.category_group) AS category_group_name,
c.goal_code
FROM edu_contents c
LEFT JOIN edu_codes ec
ON TRIM(UPPER(ec.base_code)) = TRIM(UPPER(c.category_code))
LEFT JOIN edu_codes ec_grp
ON TRIM(UPPER(ec_grp.base_code)) = TRIM(UPPER(c.category_group))
WHERE c.is_active = 1
AND c.goal_code = ?
AND c.content_id <> ?
ORDER BY
COALESCE(NULLIF(c.sort_order, 0), 9999) ASC,
c.updated_at DESC,
c.content_id DESC
LIMIT 6"
);
$stmtVideos->execute([$goalCode, $contentId]);
$rows = $stmtVideos->fetchAll(PDO::FETCH_ASSOC) ?: [];
$videos = [];
foreach ($rows as $row) {
$raw = trim((string)($row['content_url'] ?? ''));
$videoId = '';
if (preg_match('/(?:v=|youtu\.be\/)([A-Za-z0-9_-]{11})/', $raw, $m)) {
$videoId = $m[1];
} elseif (preg_match('/^[A-Za-z0-9_-]{11}$/', $raw)) {
$videoId = $raw;
}
$thumbFromDb = trim((string)($row['thumbnail_url'] ?? ''));
$thumbnail = $thumbFromDb !== ''
? $thumbFromDb
: ($videoId !== '' ? "https://img.youtube.com/vi/{$videoId}/sddefault.jpg" : '');
$catCode = (string)($row['category_code'] ?? '');
$subcate = trim((string)($row['category_group_name'] ?? $row['category_group'] ?? ''));
$catName = trim((string)($row['category_name'] ?? $catCode));
$videos[] = [
'content_id' => $row['content_id'] ?? '',
'title' => (string)($row['title'] ?? ''),
'content_url' => $videoId !== '' ? "https://www.youtube.com/watch?v={$videoId}" : $raw,
'thumbnail' => $thumbnail,
'category_code' => $catCode,
'category' => $catName,
'category_name' => $catName,
'subcate' => $subcate,
'description' => (string)($row['description'] ?? ''),
'goal_code' => (string)($row['goal_code'] ?? ''),
];
}
echo json_encode([
'success' => true,
'mode' => 'goal_code',
'goal_code' => $goalCode,
'keywords' => [],
'videos' => $videos,
], JSON_UNESCAPED_UNICODE);
} catch (Throwable $e) {
error_log('[get_recommend_videos] ' . $e->getMessage());
http_response_code(500);
echo json_encode(['success' => false, 'message' => 'server_error'], JSON_UNESCAPED_UNICODE);
}