File "save_submission.php"
Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/qp_setter/save_submission.php
File size: 4.13 KiB (4225 bytes)
MIME-type: text/x-php
Charset: utf-8
<?php
header('Content-Type: application/json');
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
require_once __DIR__ . '/../../config/config.php';
if (session_status() !== PHP_SESSION_ACTIVE) { session_start(); }
// Ensure PDO throws exceptions so we can report useful errors
try { if (isset($pdo)) { $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } } catch (Throwable $e) { /* ignore */ }
function qps_log($msg) {
try {
$line = '['.date('Y-m-d H:i:s').'] '.$msg."\n";
@file_put_contents(__DIR__ . '/../../logs/qps_api.log', $line, FILE_APPEND);
} catch (Throwable $e) { /* ignore */ }
}
try {
$staff_id = $_SESSION['qps_staff_id'] ?? 0;
$assignment_id = $_SESSION['qps_assignment_id'] ?? 0;
if (!$staff_id || !$assignment_id) { throw new Exception('Unauthorized'); }
$raw = file_get_contents('php://input');
$data = json_decode($raw, true);
if (!is_array($data)) { throw new Exception('Invalid JSON payload'); }
$status = ($data['status'] ?? 'Draft') === 'Submitted' ? 'Submitted' : 'Draft';
$template_json = $data['template_json'] ?? '';
$notes = $data['notes'] ?? '';
$form_data = $data['form'] ?? '';
// Basic validation
if (!is_string($template_json)) { $template_json = json_encode($template_json); }
if (!is_string($notes)) { $notes = (string)$notes; }
if (!is_string($form_data)) { $form_data = json_encode($form_data); }
// Check if form_data column exists
$columnExists = false;
try {
$checkColumn = $pdo->query("SHOW COLUMNS FROM qp_setter_submissions LIKE 'form_data'");
$columnExists = $checkColumn->rowCount() > 0;
} catch (Exception $e) {
$columnExists = false;
}
// Upsert latest submission (wrap in transaction for safety)
$pdo->beginTransaction();
$sel = $pdo->prepare('SELECT id FROM qp_setter_submissions WHERE assignment_id = ? AND staff_id = ? ORDER BY id DESC LIMIT 1');
$sel->execute([$assignment_id, $staff_id]);
$existingId = (int)($sel->fetchColumn() ?: 0);
if ($existingId) {
if ($columnExists) {
$sql = 'UPDATE qp_setter_submissions
SET template_json = ?, notes = ?, form_data = ?, status = ?, submitted_at = CASE WHEN ? = "Submitted" THEN NOW() ELSE submitted_at END
WHERE id = ?';
$stmt = $pdo->prepare($sql);
$stmt->execute([$template_json, $notes, $form_data, $status, $status, $existingId]);
} else {
$sql = 'UPDATE qp_setter_submissions
SET template_json = ?, notes = ?, status = ?, submitted_at = CASE WHEN ? = "Submitted" THEN NOW() ELSE submitted_at END
WHERE id = ?';
$stmt = $pdo->prepare($sql);
$stmt->execute([$template_json, $notes, $status, $status, $existingId]);
}
} else {
if ($columnExists) {
$sql = 'INSERT INTO qp_setter_submissions (assignment_id, staff_id, template_json, notes, form_data, status, submitted_at)
VALUES (?,?,?,?,?,?, CASE WHEN ? = "Submitted" THEN NOW() ELSE NULL END)';
$stmt = $pdo->prepare($sql);
$stmt->execute([$assignment_id, $staff_id, $template_json, $notes, $form_data, $status, $status]);
} else {
$sql = 'INSERT INTO qp_setter_submissions (assignment_id, staff_id, template_json, notes, status, submitted_at)
VALUES (?,?,?,?,?, CASE WHEN ? = "Submitted" THEN NOW() ELSE NULL END)';
$stmt = $pdo->prepare($sql);
$stmt->execute([$assignment_id, $staff_id, $template_json, $notes, $status, $status]);
}
}
$pdo->commit();
echo json_encode(['status'=>'success','message'=> $status === 'Submitted' ? 'Submitted successfully' : 'Draft saved']);
} catch (Throwable $e) {
http_response_code(400);
try { if ($pdo && $pdo->inTransaction()) { $pdo->rollBack(); } } catch (Throwable $e2) {}
qps_log('save_submission error: '.$e->getMessage().' | staff='.$_SESSION['qps_staff_id'].' assignment='.$_SESSION['qps_assignment_id']);
echo json_encode(['status'=>'error','message'=>'Save failed: '.$e->getMessage()]);
}
?>