File "halls_reset.php"
Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/seed/halls_reset.php
File size: 2.9 KiB (2965 bytes)
MIME-type: text/x-php
Charset: utf-8
<?php
require_once '../../config/config.php';
header('Content-Type: application/json');
try {
$pdo->beginTransaction();
// Clear dependent data in safe order
$pdo->exec('DELETE FROM student_seating');
$pdo->exec('DELETE FROM hall_allocations');
$pdo->exec('DELETE FROM hall_settings');
$pdo->exec('DELETE FROM halls');
// Ensure a college exists
$collegeId = (int)($pdo->query("SELECT college_id FROM colleges ORDER BY college_id LIMIT 1")->fetchColumn() ?: 0);
if ($collegeId <= 0) {
$pdo->rollBack();
echo json_encode(['status'=>'error','message'=>'No college found. Seed academic_structure first.']);
exit;
}
// Seed 10 halls
$defs = [
['Main Hall A', 'Block A', 120],
['Main Hall B', 'Block A', 100],
['Main Hall C', 'Block A', 90],
['Annex Hall D', 'Block B', 80],
['Annex Hall E', 'Block B', 70],
['Annex Hall F', 'Block B', 60],
['Lab Seminar G', 'Block C', 50],
['Auditorium H', 'Block D', 200],
['Lecture Hall I', 'Block E', 75],
['Lecture Hall J', 'Block E', 65]
];
$insHall = $pdo->prepare('INSERT INTO halls (college_id, hall_name, block_name, capacity) VALUES (?,?,?,?)');
$hallIds = [];
foreach ($defs as $d) { $insHall->execute([$collegeId,$d[0],$d[1],$d[2]]); $hallIds[] = (int)$pdo->lastInsertId(); }
// Seed settings for each hall
$insSet = $pdo->prepare('INSERT INTO hall_settings (hall_id, rows_count, columns_count, seating_method, bench_capacity, max_students, row_labeling, internal_invigilators_required, external_invigilators_required, aisle_every_n_cols, aisle_every_n_rows) VALUES (?,?,?,?,?,?,?,?,?,?,?)');
$createdSettings = 0;
foreach ($hallIds as $hid) {
// derive layout based on capacity
$cap = (int)($pdo->prepare('SELECT capacity FROM halls WHERE hall_id=?')->execute([$hid]) || 0);
$capStmt = $pdo->prepare('SELECT capacity FROM halls WHERE hall_id=?');
$capStmt->execute([$hid]);
$capacity = (int)($capStmt->fetchColumn() ?: 0);
$rows = max(6, min(14, (int)floor(($capacity ?: 60)/10)));
$cols = max(6, min(14, (int)ceil(($capacity ?: 60)/$rows/2)));
$bench = 2;
$maxS = ($rows*$cols*$bench);
$aisleC = ($cols >= 8) ? 4 : 0; // vertical aisle after every 4 benches if wide
$aisleR = 0;
$insSet->execute([$hid,$rows,$cols,'Parallel',$bench,$maxS,'Numeric',1,0,$aisleC,$aisleR]);
$createdSettings++;
}
$pdo->commit();
echo json_encode(['status'=>'success','message'=>'Halls and settings reset & seeded','created_halls'=>count($hallIds),'created_settings'=>$createdSettings]);
} catch (Throwable $e) {
if ($pdo->inTransaction()) $pdo->rollBack();
http_response_code(500);
echo json_encode(['status'=>'error','message'=>$e->getMessage()]);
}
?>