File "academic_structure.php"

Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/seed/academic_structure.php
File size: 3.08 KiB (3152 bytes)
MIME-type: text/x-php
Charset: utf-8

Download   Open   Back

<?php
// Seed sample Academic Structure data
header('Content-Type: application/json');
require_once '../../config/config.php';

function ensureCollege($pdo, $code, $name, $status='Active'){
    $stmt = $pdo->prepare('SELECT college_id FROM colleges WHERE college_code=?');
    $stmt->execute([$code]);
    $id = $stmt->fetchColumn();
    if ($id) return (int)$id;
    $stmt = $pdo->prepare('INSERT INTO colleges (college_code, college_name, status) VALUES (?,?,?)');
    $stmt->execute([$code, $name, $status]);
    return (int)$pdo->lastInsertId();
}

function ensureCategory($pdo, $name, $description=null){
    $stmt = $pdo->prepare('SELECT category_id FROM categories WHERE category_name=?');
    $stmt->execute([$name]);
    $id = $stmt->fetchColumn();
    if ($id) return (int)$id;
    $stmt = $pdo->prepare('INSERT INTO categories (category_name, description) VALUES (?,?)');
    $stmt->execute([$name, $description]);
    return (int)$pdo->lastInsertId();
}

function ensureCourse($pdo, $collegeId, $categoryId, $code, $name, $regYear, $durationYears, $totalSem, $status='Active'){
    $stmt = $pdo->prepare('SELECT course_id FROM courses WHERE course_code=?');
    $stmt->execute([$code]);
    $id = $stmt->fetchColumn();
    if ($id) return (int)$id;
    $stmt = $pdo->prepare('INSERT INTO courses (college_id, category_id, course_name, course_code, regulation_year, duration_years, total_semesters, status) VALUES (?,?,?,?,?,?,?,?)');
    $stmt->execute([$collegeId, $categoryId, $name, $code, $regYear, $durationYears, $totalSem, $status]);
    return (int)$pdo->lastInsertId();
}

try {
    $pdo->beginTransaction();

    // College
    $collegeCode = 'INE01';
    $collegeName = 'Issac Newton Engineering';
    $collegeId = ensureCollege($pdo, $collegeCode, $collegeName);

    // Categories
    $catUG = ensureCategory($pdo, 'UG', 'Undergraduate');
    $catPG = ensureCategory($pdo, 'PG', 'Postgraduate');
    $catR = ensureCategory($pdo, 'Research', 'Doctoral / Research');

    // Courses (examples)
    $courses = [];
    $courses[] = ensureCourse($pdo, $collegeId, $catUG, 'BE-ME', 'BE Mechanical Engineering', 2020, 4, 8, 'Active');
    $courses[] = ensureCourse($pdo, $collegeId, $catUG, 'BE-CSE', 'BE Computer Science and Engineering', 2020, 4, 8, 'Active');
    $courses[] = ensureCourse($pdo, $collegeId, $catPG, 'ME-MFG', 'ME Manufacturing Engineering', 2020, 2, 4, 'Active');
    $courses[] = ensureCourse($pdo, $collegeId, $catR, 'PHD-ME', 'PhD Mechanical Engineering', 2020, 3, 6, 'Active');

    $pdo->commit();

    echo json_encode([
        'status' => 'success',
        'message' => 'Sample Academic Structure data seeded successfully.',
        'college' => [ 'id' => $collegeId, 'code' => $collegeCode, 'name' => $collegeName ],
        'categories' => [ 'UG' => $catUG, 'PG' => $catPG, 'Research' => $catR ],
        'courses_created' => count($courses)
    ]);
} catch (Throwable $e) {
    if ($pdo->inTransaction()) $pdo->rollBack();
    http_response_code(500);
    echo json_encode(['status'=>'error','message'=>$e->getMessage()]);
}
?>


PHP File Manager