File "subjects.php"

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

Download   Open   Back

<?php
// Seed sample Subjects for existing seeded courses
header('Content-Type: application/json');
require_once '../../config/config.php';

function findCourseId($pdo, $code){ $stmt=$pdo->prepare('SELECT course_id FROM courses WHERE course_code=?'); $stmt->execute([$code]); return (int)($stmt->fetchColumn() ?: 0); }
function ensureSubject($pdo, $courseId, $code, $name, $sem, $type='Regular', $credits=3, $regYear=2020, $partNo=1){
    $stmt=$pdo->prepare('SELECT subject_id FROM subjects WHERE subject_code=?');
    $stmt->execute([$code]);
    $id=$stmt->fetchColumn();
    if ($id) return (int)$id;
    $is_lab = ($type==='Lab') ? 1 : 0;
    $semType = ($sem % 2 === 1) ? 'Odd' : 'Even';
    $stmt=$pdo->prepare('INSERT INTO subjects (course_id, subject_code, subject_name, regulation_year, type, credits, is_lab, semester, part_no, sem_type) VALUES (?,?,?,?,?,?,?,?,?,?)');
    $stmt->execute([$courseId,$code,$name,$regYear,$type,$credits,$is_lab,$sem,$partNo,$semType]);
    return (int)$pdo->lastInsertId();
}

try{
    $pdo->beginTransaction();
    $map = [
        'BE-ME' => [
            ['ME101','Engineering Mechanics',1,'Regular',4],
            ['ME102','Workshop Practice',1,'Lab',2],
            ['ME201','Thermodynamics',3,'Regular',4],
            ['ME202','Manufacturing Technology',3,'Practical',3],
        ],
        'BE-CSE' => [
            ['CS101','Programming in C',1,'Regular',4],
            ['CS102','Programming Lab',1,'Lab',2],
            ['CS201','Data Structures',3,'Regular',4],
            ['CS202','Database Systems',3,'Regular',4],
        ],
        'ME-MFG' => [
            ['MF501','Advanced Manufacturing Processes',1,'Regular',4],
            ['MF502','CIM Lab',1,'Lab',2],
        ],
        'PHD-ME' => [
            ['PH901','Research Methodology',1,'Regular',3],
            ['PH902','Advanced Seminar',2,'Elective',2],
        ],
    ];

    $created = 0; $byCourse = [];
    foreach($map as $courseCode=>$subjects){
        $courseId = findCourseId($pdo, $courseCode);
        if ($courseId<=0) continue;
        foreach($subjects as $s){
            [$code,$name,$sem,$type,$credits] = $s;
            ensureSubject($pdo,$courseId,$code,$name,$sem,$type,$credits,2020);
            $created++;
        }
        $byCourse[$courseCode] = count($subjects);
    }

    $pdo->commit();
    echo json_encode(['status'=>'success','message'=>'Subjects seeded','created'=>$created,'by_course'=>$byCourse]);
}catch(Throwable $e){ if($pdo->inTransaction()) $pdo->rollBack(); http_response_code(500); echo json_encode(['status'=>'error','message'=>$e->getMessage()]); }
?>


PHP File Manager