76 lines
2.7 KiB
PHP
76 lines
2.7 KiB
PHP
<?php
|
|
/**
|
|
* Certificate Verification API
|
|
* GET ?code=TPU-2569-0016 — Verify a certificate by its unique code
|
|
*/
|
|
|
|
require_once __DIR__ . '/../config/database.php';
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
$code = trim($_GET['code'] ?? '');
|
|
|
|
if (empty($code)) {
|
|
jsonResponse(['success' => false, 'error' => 'กรุณาระบุรหัสตรวจสอบเกียรติบัตร'], 400);
|
|
}
|
|
|
|
$db = getDB();
|
|
|
|
// ค้นหารหัสเกียรติบัตรในฐานข้อมูล
|
|
$stmt = $db->prepare("
|
|
SELECT sr.id, sr.respondent_info, sr.submitted_at, s.title as survey_title
|
|
FROM survey_responses sr
|
|
JOIN surveys s ON sr.survey_id = s.id
|
|
WHERE sr.cert_code = ?
|
|
");
|
|
$stmt->execute([$code]);
|
|
$record = $stmt->fetch();
|
|
|
|
if (!$record) {
|
|
jsonResponse(['success' => false, 'error' => 'ไม่พบรหัสเกียรติบัตรนี้ในระบบ หรือรหัสอาจไม่ถูกต้อง'], 404);
|
|
}
|
|
|
|
// ถอดรหัสชื่อผู้เข้าร่วม
|
|
$respondentInfo = json_decode($record['respondent_info'], true) ?: [];
|
|
$name = $respondentInfo['name'] ?? '';
|
|
|
|
// ถ้ารายละเอียดของชื่อว่าง (เช่น กรณีข้อมูลเก่าที่ใช้ Fallback)
|
|
// ให้พยายามดึงข้อมูลจากตารางคำตอบจริงที่มีคำถามเกี่ยวกับชื่อ
|
|
if (empty($name)) {
|
|
$stmtQ = $db->prepare("
|
|
SELECT q.id
|
|
FROM survey_questions q
|
|
JOIN survey_sections s ON q.section_id = s.id
|
|
WHERE s.survey_id = (SELECT survey_id FROM survey_responses WHERE id = ?)
|
|
AND (q.question_text LIKE '%ชื่อ%' OR q.question_text LIKE '%นามสกุล%')
|
|
LIMIT 1
|
|
");
|
|
$stmtQ->execute([$record['id']]);
|
|
$qRow = $stmtQ->fetch();
|
|
if ($qRow) {
|
|
$nameQuestionId = $qRow['id'];
|
|
$stmtA = $db->prepare("
|
|
SELECT answer_value FROM response_answers
|
|
WHERE response_id = ? AND question_id = ?
|
|
");
|
|
$stmtA->execute([$record['id'], $nameQuestionId]);
|
|
$aRow = $stmtA->fetch();
|
|
if ($aRow) {
|
|
$name = trim($aRow['answer_value']);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (empty($name)) {
|
|
$name = 'ผู้เข้าร่วมโครงการ';
|
|
}
|
|
|
|
// ส่งรายละเอียดกลับไปหาผู้ขอตรวจสอบ
|
|
jsonResponse([
|
|
'success' => true,
|
|
'code' => $code,
|
|
'recipient_name' => $name,
|
|
'course_title' => $record['survey_title'],
|
|
'issue_date' => $record['submitted_at']
|
|
]);
|