125 lines
4.3 KiB
PHP
125 lines
4.3 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
require_once dirname(__DIR__) . '/db_conn.php';
|
|
|
|
if (session_status() !== PHP_SESSION_ACTIVE) {
|
|
session_start();
|
|
}
|
|
|
|
$memberId = (string)($_SESSION['member_id'] ?? '');
|
|
|
|
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();
|
|
|
|
$stmtCategory = $pdo->prepare('SELECT category_code FROM edu_contents WHERE content_id = ? LIMIT 1');
|
|
$stmtCategory->execute([$contentId]);
|
|
$categoryCode = strtoupper((string)($stmtCategory->fetchColumn() ?: ''));
|
|
|
|
// ── 마이클래스(CA10001): edu_learning_histories.comment 에서 본인 소감 조회 ──
|
|
if ($categoryCode === 'CA10001') {
|
|
$sqlLh = 'SELECT lh.member_id, COALESCE(u.name, lh.member_id) AS member_name,
|
|
lh.comment, lh.last_viewed_at
|
|
FROM edu_learning_histories lh
|
|
LEFT JOIN edu_users u ON u.member_id = lh.member_id AND u.sys_comp_code = lh.sys_comp_code
|
|
WHERE lh.content_id = ? AND lh.member_id = ?';
|
|
$paramsLh = [$contentId, $memberId];
|
|
|
|
$sysCompCode = (string)($_SESSION['sys_comp_code'] ?? '');
|
|
if ($sysCompCode !== '') {
|
|
$sqlLh .= ' AND lh.sys_comp_code = ?';
|
|
$paramsLh[] = $sysCompCode;
|
|
}
|
|
|
|
$sqlLh .= ' ORDER BY lh.last_viewed_at DESC LIMIT 1';
|
|
$stmtLh = $pdo->prepare($sqlLh);
|
|
$stmtLh->execute($paramsLh);
|
|
$row = $stmtLh->fetch(PDO::FETCH_ASSOC);
|
|
|
|
$data = [];
|
|
if ($row && $row['comment'] !== null && trim((string)$row['comment']) !== '') {
|
|
$authorId = (string)($row['member_id'] ?? '');
|
|
$profileFilePath = __DIR__ . '/../../img/profile/' . $authorId . '_' . $sysCompCode . '.png';
|
|
$profileImageUrl = file_exists($profileFilePath)
|
|
? '/img/profile/' . $authorId . '_' . $sysCompCode . '.png'
|
|
: '/img/ico/ico_user.svg';
|
|
$data[] = [
|
|
'id' => $contentId,
|
|
'member_id' => $authorId,
|
|
'member_name' => (string)($row['member_name'] ?? $authorId),
|
|
'comment' => (string)$row['comment'],
|
|
'created_at' => (string)($row['last_viewed_at'] ?? ''),
|
|
'updated_at' => (string)($row['last_viewed_at'] ?? ''),
|
|
'is_author' => true,
|
|
'profile_image' => $profileImageUrl,
|
|
];
|
|
}
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'category' => $categoryCode,
|
|
'data' => $data,
|
|
], JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
// ── 기타 카테고리: 기존 edu_comments 테이블 조회 ──
|
|
$sysCompCode = (string)($_SESSION['sys_comp_code'] ?? '');
|
|
$params = [$contentId];
|
|
$sql = 'SELECT c.id, c.member_id, c.sys_comp_code, COALESCE(u.name, c.member_id) AS member_name, c.comment, c.created_at, c.updated_at
|
|
FROM edu_comments c
|
|
LEFT JOIN edu_users u ON u.member_id = c.member_id AND u.sys_comp_code = c.sys_comp_code
|
|
WHERE c.content_id = ?';
|
|
|
|
$sql .= ' ORDER BY c.id DESC';
|
|
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute($params);
|
|
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC) ?: [];
|
|
|
|
$data = array_map(static function (array $row) use ($memberId, $sysCompCode): array {
|
|
$authorId = (string)($row['member_id'] ?? '');
|
|
$rowSysCompCode = (string)($row['sys_comp_code'] ?? '');
|
|
$profileFilePath = __DIR__ . '/../../img/profile/' . $authorId . '_' . $rowSysCompCode . '.png';
|
|
$profileImageUrl = file_exists($profileFilePath)
|
|
? '/img/profile/' . $authorId . '_' . $rowSysCompCode . '.png'
|
|
: '/img/ico/ico_user.svg';
|
|
return [
|
|
'id' => (int)($row['id'] ?? 0),
|
|
'member_id' => $authorId,
|
|
'member_name' => (string)($row['member_name'] ?? $authorId),
|
|
'comment' => (string)($row['comment'] ?? ''),
|
|
'created_at' => (string)($row['created_at'] ?? ''),
|
|
'updated_at' => (string)($row['updated_at'] ?? ''),
|
|
'is_author' => ($authorId === $memberId && $rowSysCompCode === $sysCompCode),
|
|
'profile_image' => $profileImageUrl,
|
|
];
|
|
}, $rows);
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'category' => $categoryCode,
|
|
'data' => $data,
|
|
], JSON_UNESCAPED_UNICODE);
|
|
} catch (Throwable $e) {
|
|
error_log('[get_comments] ' . $e->getMessage());
|
|
http_response_code(500);
|
|
echo json_encode(['success' => false, 'message' => 'server_error'], JSON_UNESCAPED_UNICODE);
|
|
}
|
|
|