Files
edu/bbs/api/save_user_learning_goal.php
T

126 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'] ?? $_SESSION['user_id'] ?? '');
$sysCompCode = (string)($_SESSION['sys_comp_code'] ?? $_SESSION['company'] ?? '');
if ($memberId === '') {
http_response_code(401);
echo json_encode(['success' => false, 'message' => 'not_logged_in'], JSON_UNESCAPED_UNICODE);
exit;
}
try {
$pdo = db_conn();
if ($sysCompCode === '') {
$stmtComp = $pdo->prepare('SELECT sys_comp_code FROM edu_users WHERE member_id = ? ORDER BY sys_comp_code LIMIT 1');
$stmtComp->execute([$memberId]);
$sysCompCode = (string)($stmtComp->fetchColumn() ?: '');
}
if ($sysCompCode === '') {
http_response_code(400);
echo json_encode(['success' => false, 'message' => 'sys_comp_code_missing'], JSON_UNESCAPED_UNICODE);
exit;
}
$input = json_decode((string)file_get_contents('php://input'), true) ?: [];
$goalCode = trim((string)($input['goal_code'] ?? ''));
$quarter = trim((string)($input['quarter'] ?? ''));
if ($goalCode === '') {
http_response_code(400);
echo json_encode(['success' => false, 'message' => 'goal_code_required'], JSON_UNESCAPED_UNICODE);
exit;
}
if ($quarter !== '') {
$stmtGoal = $pdo->prepare('SELECT goal_code, quarter FROM edu_learning_goals WHERE is_active = \'1\' AND goal_code = ? AND quarter = ? LIMIT 1');
$stmtGoal->execute([$goalCode, $quarter]);
} else {
$stmtGoal = $pdo->prepare('SELECT goal_code, quarter FROM edu_learning_goals WHERE is_active = \'1\' AND goal_code = ? LIMIT 1');
$stmtGoal->execute([$goalCode]);
}
$goalRow = $stmtGoal->fetch(PDO::FETCH_ASSOC);
if (!$goalRow) {
http_response_code(404);
echo json_encode(['success' => false, 'message' => 'goal_not_found'], JSON_UNESCAPED_UNICODE);
exit;
}
$goalCode = (string)$goalRow['goal_code'];
$quarter = trim((string)($goalRow['quarter'] ?? $quarter));
$pdo->beginTransaction();
if ($quarter !== '') {
$stmtDeactivate = $pdo->prepare(
'UPDATE edu_user_learning_goals
SET is_active = \'0\', updated_by = ?, updated_at = NOW()
WHERE member_id = ?
AND sys_comp_code = ?
AND quarter = ?
AND goal_code <> ?
AND is_active = \'1\'
AND (completed_date IS NULL OR completed_date = \'0000-00-00\')'
);
$stmtDeactivate->execute([$memberId, $memberId, $sysCompCode, $quarter, $goalCode]);
}
$stmtExists = $pdo->prepare(
'SELECT 1
FROM edu_user_learning_goals
WHERE member_id = ?
AND sys_comp_code = ?
AND goal_code = ?
LIMIT 1'
);
$stmtExists->execute([$memberId, $sysCompCode, $goalCode]);
$exists = (bool)$stmtExists->fetchColumn();
if ($exists) {
$stmtUp = $pdo->prepare(
'UPDATE edu_user_learning_goals
SET quarter = ?, is_active = \'1\', updated_by = ?, updated_at = NOW()
WHERE member_id = ?
AND sys_comp_code = ?
AND goal_code = ?'
);
$stmtUp->execute([$quarter, $memberId, $memberId, $sysCompCode, $goalCode]);
} else {
$stmtIn = $pdo->prepare(
'INSERT INTO edu_user_learning_goals
(member_id, sys_comp_code, goal_code, quarter, is_active, created_by, created_at, updated_by, updated_at)
VALUES (?, ?, ?, ?, \'1\', ?, NOW(), ?, NOW())'
);
$stmtIn->execute([$memberId, $sysCompCode, $goalCode, $quarter, $memberId, $memberId]);
}
$pdo->commit();
echo json_encode([
'success' => true,
'member_id' => $memberId,
'sys_comp_code' => $sysCompCode,
'goal_code' => $goalCode,
'quarter' => $quarter,
], JSON_UNESCAPED_UNICODE);
} catch (Throwable $e) {
if (isset($pdo) && $pdo instanceof PDO && $pdo->inTransaction()) {
$pdo->rollBack();
}
error_log('[save_user_learning_goal] ' . $e->getMessage());
http_response_code(500);
echo json_encode(['success' => false, 'message' => 'server_error'], JSON_UNESCAPED_UNICODE);
}