File "generate_schedule.php"

Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/exam_subjects/generate_schedule.php
File size: 4.58 KiB (4687 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 {
    $examId = isset($_POST['exam_id']) ? (int)$_POST['exam_id'] : 0;
    $startDate = trim($_POST['start_date'] ?? '');
    $shuffle = isset($_POST['shuffle']) ? (int)$_POST['shuffle'] === 1 : false;
    $onlyWithRegistrations = isset($_POST['with_registrations']) ? (int)$_POST['with_registrations'] === 1 : true;

    if ($examId <= 0) { echo json_encode(['status'=>'error','message'=>'Exam is required']); exit; }
    if ($startDate === '') { echo json_encode(['status'=>'error','message'=>'Start date is required']); exit; }

    // Fetch subjects in the exam (optionally filter to those with registrations)
    $sql = "SELECT es.exam_subject_id, s.subject_code, s.subject_name
            FROM exam_subjects es
            JOIN subjects s ON s.subject_id = es.subject_id
            WHERE es.exam_id = ?";
    if ($onlyWithRegistrations) {
        $sql .= " AND EXISTS (SELECT 1 FROM student_registrations r WHERE r.exam_subject_id = es.exam_subject_id)";
    }
    $sql .= " ORDER BY s.subject_code";
    $stmt=$pdo->prepare($sql);
    $stmt->execute([$examId]);
    $list=$stmt->fetchAll(PDO::FETCH_ASSOC);
    if (!$list) { echo json_encode(['status'=>'error','message'=>'No exam subjects found for this exam']); exit; }
    if ($shuffle) { shuffle($list); }

    $currentDate = new DateTime($startDate);
    $updated = 0; $timetableCreated = 0; $timetableUpdated = 0; $rows = [];
    $fn = ['session'=>'FN','start'=>'10:00:00','end'=>'13:00:00'];
    $an = ['session'=>'AN','start'=>'14:00:00','end'=>'17:00:00'];

    foreach ($list as $item) {
        $esid = (int)$item['exam_subject_id'];
        $dateStr = $currentDate->format('Y-m-d');

        // Update exam_subjects date (session we keep as FN by default)
        $pdo->prepare('UPDATE exam_subjects SET exam_date = ?, session = ? WHERE exam_subject_id = ?')
            ->execute([$dateStr, $fn['session'], $esid]);
        $updated++;

        // Determine if arrears exist for this subject
        $cStmt = $pdo->prepare('SELECT SUM(CASE WHEN is_arrear=0 THEN 1 ELSE 0 END) AS curr, SUM(CASE WHEN is_arrear=1 THEN 1 ELSE 0 END) AS arr FROM student_registrations WHERE exam_subject_id=?');
        $cStmt->execute([$esid]);
        $c=$cStmt->fetch(PDO::FETCH_ASSOC) ?: ['curr'=>0,'arr'=>0];
        $hasCurrent = (int)$c['curr'] > 0; $hasArrear = (int)$c['arr'] > 0;

        // Upsert timetable FN (always if has current, else still create FN)
        $sel=$pdo->prepare('SELECT timetable_id FROM exam_timetable WHERE exam_subject_id=? AND session=?');
        $sel->execute([$esid,$fn['session']]);
        $tid=(int)($sel->fetchColumn()?:0);
        if ($tid>0){
            $pdo->prepare('UPDATE exam_timetable SET exam_date=?, start_time=?, end_time=? WHERE timetable_id=?')
                ->execute([$dateStr,$fn['start'],$fn['end'],$tid]);
            $timetableUpdated++;
        } else {
            $pdo->prepare('INSERT INTO exam_timetable (exam_subject_id, exam_date, session, start_time, end_time) VALUES (?,?,?,?,?)')
                ->execute([$esid,$dateStr,$fn['session'],$fn['start'],$fn['end']]);
            $timetableCreated++;
        }

        // Upsert timetable AN if there are arrears
        if ($hasArrear) {
            $sel=$pdo->prepare('SELECT timetable_id FROM exam_timetable WHERE exam_subject_id=? AND session=?');
            $sel->execute([$esid,$an['session']]);
            $tid=(int)($sel->fetchColumn()?:0);
            if ($tid>0){
                $pdo->prepare('UPDATE exam_timetable SET exam_date=?, start_time=?, end_time=? WHERE timetable_id=?')
                    ->execute([$dateStr,$an['start'],$an['end'],$tid]);
                $timetableUpdated++;
            } else {
                $pdo->prepare('INSERT INTO exam_timetable (exam_subject_id, exam_date, session, start_time, end_time) VALUES (?,?,?,?,?)')
                    ->execute([$esid,$dateStr,$an['session'],$an['start'],$an['end']]);
                $timetableCreated++;
            }
        }

        $rows[] = ['exam_subject_id'=>$esid,'exam_date'=>$dateStr,'session'=>$fn['session']];
        $currentDate->modify('+1 day');
    }

    echo json_encode(['status'=>'success','message'=>'Schedule generated','updated_exam_subjects'=>$updated,'timetable_created'=>$timetableCreated,'timetable_updated'=>$timetableUpdated,'data'=>$rows]);
} catch (Throwable $e) {
    http_response_code(500);
    echo json_encode(['status'=>'error','message'=>$e->getMessage()]);
}
?>


PHP File Manager