File "create.php"

Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/question_paper_orders/create.php
File size: 3.03 KiB (3100 bytes)
MIME-type: text/x-php
Charset: utf-8

Download   Open   Back

<?php
require_once '../../config/config.php';
require_once '../helper/audit_log.php';
header('Content-Type: application/json');

try {
    $exam_subject_id = (int)($_POST['exam_subject_id'] ?? 0);
    $setter_id = (int)($_POST['setter_id'] ?? 0);
    $status = trim($_POST['status'] ?? 'Order Sent');
    $time_duration = isset($_POST['time_duration']) ? (int)$_POST['time_duration'] : null;
    $approver_id = isset($_POST['approver_id']) && $_POST['approver_id'] !== '' ? (int)$_POST['approver_id'] : null;
    $approved_at = isset($_POST['approved_at']) && $_POST['approved_at'] !== '' ? $_POST['approved_at'] : null;
    $final_pattern_id = isset($_POST['final_pattern_id']) && $_POST['final_pattern_id'] !== '' ? (int)$_POST['final_pattern_id'] : null;

    $allowed = ['Order Sent','Accepted','Draft Submitted','Approved','Rejected'];
    $errors = [];
    if ($exam_subject_id <= 0) $errors[] = 'Exam subject is required';
    if ($setter_id <= 0) $errors[] = 'Setter is required';
    if (!in_array($status, $allowed)) $errors[] = 'Invalid status';
    if ($errors) { echo json_encode(['status'=>'error','message'=>implode(', ', $errors)]); exit; }

    $sql = 'INSERT INTO question_paper_orders (exam_subject_id, setter_id, status, time_duration, approver_id, approved_at, final_pattern_id)
            VALUES (:exam_subject_id, :setter_id, :status, :time_duration, :approver_id, :approved_at, :final_pattern_id)';
    $stmt = $pdo->prepare($sql);
    $stmt->bindParam(':exam_subject_id', $exam_subject_id, PDO::PARAM_INT);
    $stmt->bindParam(':setter_id', $setter_id, PDO::PARAM_INT);
    $stmt->bindParam(':status', $status, PDO::PARAM_STR);
    $stmt->bindValue(':time_duration', $time_duration, $time_duration === null ? PDO::PARAM_NULL : PDO::PARAM_INT);
    $stmt->bindValue(':approver_id', $approver_id, $approver_id === null ? PDO::PARAM_NULL : PDO::PARAM_INT);
    $stmt->bindValue(':approved_at', $approved_at, $approved_at === null ? PDO::PARAM_NULL : PDO::PARAM_STR);
    $stmt->bindValue(':final_pattern_id', $final_pattern_id, $final_pattern_id === null ? PDO::PARAM_NULL : PDO::PARAM_INT);
    $stmt->execute();
    $order_id = (int)$pdo->lastInsertId();

    write_audit_log($pdo, [
        'module' => 'Question Papers',
        'action' => 'create',
        'entity_table' => 'question_paper_orders',
        'entity_id' => $order_id,
        'after_data' => compact('order_id','exam_subject_id','setter_id','status','time_duration','approver_id','approved_at','final_pattern_id'),
        'message' => 'Question paper order created'
    ]);

    echo json_encode(['status'=>'success','message'=>'Order created','data'=>['order_id'=>$order_id]]);
} catch (PDOException $e) {
    $msg = $e->getCode()==='23000' ? 'Order already exists for this exam subject' : 'Database error: '.$e->getMessage();
    http_response_code(400);
    echo json_encode(['status'=>'error','message'=>$msg]);
} catch (Throwable $e) {
    http_response_code(500);
    echo json_encode(['status'=>'error','message'=>$e->getMessage()]);
}
?>


PHP File Manager