Initial commit: 교육 프로젝트 배포
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
require __DIR__ . '/../../bbs/db_conn.php';
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
// PDO 연결 생성
|
||||
$pdo = db_conn();
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
echo json_encode(['success' => false, 'message' => '잘못된 요청입니다.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$code = isset($_POST['code']) ? trim($_POST['code']) : '';
|
||||
$endDate = isset($_POST['end_date']) ? trim($_POST['end_date']) : '';
|
||||
$action = isset($_POST['action']) ? trim($_POST['action']) : 'count'; // 'count' 또는 'send'
|
||||
|
||||
if ($code === '' || !in_array($code, ['100', '200', '900'])) {
|
||||
echo json_encode(['success' => false, 'message' => '잘못된 코드입니다.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
//$typeCode = 'AL100' . str_pad($code, 3, '0', STR_PAD_LEFT) . '0';
|
||||
$typeCode = 'AL100' . $code;
|
||||
try {
|
||||
// 대상 사용자 조회
|
||||
if ($code === '100') {
|
||||
// 미수료자: 법정교육 콘텐츠를 수강해야 하지만 학습 이력이 없는 사용자
|
||||
$userStmt = $pdo->prepare("
|
||||
SELECT u.member_id, u.name, u.sys_comp_code
|
||||
FROM edu_contents a
|
||||
CROSS JOIN edu_users u
|
||||
WHERE a.category_code = 'CA10003' -- 법정교육 카테고리
|
||||
AND a.base_year = YEAR(NOW()) -- 기준년도
|
||||
AND a.is_active = '1' -- 콘텐츠 활성 여부
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM edu_learning_histories b
|
||||
WHERE b.content_id = a.content_id -- 콘텐츠ID
|
||||
AND b.member_id = u.member_id -- 사용자ID
|
||||
AND b.sys_comp_code = u.sys_comp_code -- 사영여부
|
||||
AND b.completed_at is not null -- 학습완료일시
|
||||
)
|
||||
GROUP BY u.member_id, u.name, u.sys_comp_code
|
||||
");
|
||||
} else {
|
||||
// 법정교육 안내 및 전체 공지: 활성 사용자
|
||||
$userStmt = $pdo->prepare("
|
||||
SELECT member_id, name, sys_comp_code
|
||||
FROM edu_users
|
||||
WHERE (end_date IS NULL OR end_date > CURDATE())
|
||||
");
|
||||
}
|
||||
$userStmt->execute();
|
||||
$users = $userStmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
$userCount = count($users);
|
||||
|
||||
// 대상자 수만 반환하는 경우
|
||||
if ($action === 'count') {
|
||||
echo json_encode(['success' => true, 'count' => $userCount]);
|
||||
exit;
|
||||
}
|
||||
|
||||
// 실제 알람 발송
|
||||
$pdo->beginTransaction();
|
||||
|
||||
// 법정교육 기간 정보 조회 (메시지 치환용)
|
||||
$periodStmt = $pdo->prepare("SELECT start_date, end_date FROM edu_contents WHERE category_code = 'CA10003' AND base_year = YEAR(NOW()) LIMIT 1");
|
||||
$periodStmt->execute();
|
||||
$period = $periodStmt->fetch(PDO::FETCH_ASSOC);
|
||||
$startDate = $period['start_date'] ?? '';
|
||||
$periodEndDate = $period['end_date'] ?? '';
|
||||
|
||||
// 기본 메시지 조회
|
||||
$msgStmt = $pdo->prepare("SELECT desc01 FROM edu_codes WHERE group_code = 'AL100' AND code = :code");
|
||||
$msgStmt->execute([':code' => $code]);
|
||||
$baseMessage = $msgStmt->fetchColumn();
|
||||
|
||||
foreach ($users as $user) {
|
||||
// seq 최대값 +1
|
||||
$seqStmt = $pdo->prepare("SELECT COALESCE(MAX(seq), 0) + 1 FROM edu_notifications WHERE member_id = :mid");
|
||||
$seqStmt->execute([':mid' => $user['member_id']]);
|
||||
$seq = $seqStmt->fetchColumn();
|
||||
|
||||
// 메시지 치환
|
||||
$message = str_replace(
|
||||
['{학습자명}', '{시작일}', '{종료일}'],
|
||||
[$user['name'], $startDate, $periodEndDate],
|
||||
$baseMessage
|
||||
);
|
||||
|
||||
// 알람 저장
|
||||
$insStmt = $pdo->prepare("INSERT INTO edu_notifications (member_id, corp_code, seq, type_code, message, sent_at, end_date) VALUES (:mid, :corp, :seq, :type, :msg, NOW(), :end)");
|
||||
$insStmt->execute([
|
||||
':mid' => $user['member_id'],
|
||||
':corp' => $user['sys_comp_code'],
|
||||
':seq' => $seq,
|
||||
':type' => $typeCode,
|
||||
':msg' => $message,
|
||||
':end' => $endDate
|
||||
]);
|
||||
}
|
||||
|
||||
$pdo->commit();
|
||||
echo json_encode(['success' => true, 'count' => $userCount]);
|
||||
} catch (PDOException $e) {
|
||||
$pdo->rollBack();
|
||||
echo json_encode(['success' => false, 'message' => 'DB 에러: ' . $e->getMessage()]);
|
||||
}
|
||||
exit;
|
||||
Reference in New Issue
Block a user