File "create.php"

Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/assessment_settings/create.php
File size: 2.55 KiB (2614 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 {
    $pattern_id = (int)($_POST['pattern_id'] ?? 0);
    $section_name = trim($_POST['section_name'] ?? '');
    $num_questions = (int)($_POST['num_questions'] ?? 0);
    $marks_per_question = (int)($_POST['marks_per_question'] ?? 0);
    $section_total = (int)($_POST['section_total'] ?? ($num_questions * $marks_per_question));
    $question_start_num = (int)($_POST['question_start_num'] ?? 0);
    $question_end_num = (int)($_POST['question_end_num'] ?? 0);

    $errors = [];
    if ($pattern_id <= 0) $errors[] = 'Pattern is required';
    if ($section_name === '') $errors[] = 'Section name is required';
    if ($num_questions <= 0) $errors[] = 'Number of questions must be > 0';
    if ($marks_per_question <= 0) $errors[] = 'Marks per question must be > 0';
    if ($section_total <= 0) $errors[] = 'Section total must be > 0';
    if ($question_start_num <= 0) $errors[] = 'Question start number must be > 0';
    if ($question_end_num < $question_start_num) $errors[] = 'Question end number must be >= start';
    if ($errors) { echo json_encode(['status'=>'error','message'=>implode(', ', $errors)]); exit; }

    // Ensure pattern exists
    $chk = $pdo->prepare('SELECT pattern_id FROM question_patterns WHERE pattern_id = ?');
    $chk->execute([$pattern_id]);
    if (!$chk->fetch()) { echo json_encode(['status'=>'error','message'=>'Invalid pattern']); exit; }

    $sql = 'INSERT INTO pattern_sections (pattern_id, section_name, num_questions, marks_per_question, section_total, question_start_num, question_end_num)
            VALUES (?,?,?,?,?,?,?)';
    $pdo->prepare($sql)->execute([$pattern_id, $section_name, $num_questions, $marks_per_question, $section_total, $question_start_num, $question_end_num]);
    $section_id = (int)$pdo->lastInsertId();

    write_audit_log($pdo, [
        'module' => 'Question Papers',
        'action' => 'create',
        'entity_table' => 'pattern_sections',
        'entity_id' => $section_id,
        'after_data' => compact('section_id','pattern_id','section_name','num_questions','marks_per_question','section_total','question_start_num','question_end_num'),
        'message' => 'Pattern section created'
    ]);

    echo json_encode(['status'=>'success','message'=>'Pattern section created','data'=>['section_id'=>$section_id]]);
} catch (Throwable $e) {
    http_response_code(500);
    echo json_encode(['status'=>'error','message'=>$e->getMessage()]);
}
?>


PHP File Manager