File "save_course_settings.php"

Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/internal_marks/save_course_settings.php
File size: 2.38 KiB (2434 bytes)
MIME-type: text/x-php
Charset: utf-8

Download   Open   Back

<?php
require_once __DIR__ . '/../../config/config.php';
header('Content-Type: application/json');
ob_start();
try {
    $raw = file_get_contents('php://input');
    $p = json_decode($raw, true);
    $course_id = (int)($p['course_id'] ?? 0);
    $max_marks = isset($p['max_marks']) ? (int)$p['max_marks'] : null;
    $pass_marks = isset($p['pass_marks']) ? (int)$p['pass_marks'] : null;
    $min_marks = isset($p['min_marks']) ? (int)$p['min_marks'] : 0;
    $exam_type = isset($p['exam_type']) ? $p['exam_type'] : 'Internal';
    $setting_id = isset($p['setting_id']) ? (int)$p['setting_id'] : 0;
    
    if(!$course_id || $max_marks===null || $pass_marks===null){ 
        echo json_encode(['status'=>'error','message'=>'Invalid data']); 
        exit; 
    }
    
    if($pass_marks > $max_marks){ 
        echo json_encode(['status'=>'error','message'=>'Pass must be <= Max']); 
        exit; 
    }
    
    if($min_marks > $pass_marks){ 
        echo json_encode(['status'=>'error','message'=>'Min must be <= Pass']); 
        exit; 
    }
    
    global $pdo;
    
    if ($setting_id > 0) {
        // Update existing setting
        $stmt = $pdo->prepare("
            UPDATE course_assessment_settings 
            SET course_id = ?, exam_type = ?, max_marks = ?, pass_marks = ?, min_marks = ?, updated_at = CURRENT_TIMESTAMP
            WHERE setting_id = ?
        ");
        $stmt->execute([$course_id, $exam_type, $max_marks, $pass_marks, $min_marks, $setting_id]);
        $message = 'Course settings updated successfully';
    } else {
        // Insert new setting or update if exists
        $stmt = $pdo->prepare("
            INSERT INTO course_assessment_settings (course_id, exam_type, max_marks, pass_marks, min_marks) 
            VALUES (?, ?, ?, ?, ?)
            ON DUPLICATE KEY UPDATE 
            max_marks = VALUES(max_marks),
            pass_marks = VALUES(pass_marks),
            min_marks = VALUES(min_marks),
            updated_at = CURRENT_TIMESTAMP
        ");
        $stmt->execute([$course_id, $exam_type, $max_marks, $pass_marks, $min_marks]);
        $message = 'Course settings saved successfully';
    }
    
    echo json_encode(['status'=>'success','message'=>$message]);
} catch (Throwable $e) {
    error_log('save_course_settings error: '.$e->getMessage());
    http_response_code(500);
    echo json_encode(['status'=>'error','message'=>$e->getMessage()]);
}
ob_end_flush();
?>

PHP File Manager