File "save_ci_settings.php"

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

Download   Open   Back

<?php
require_once __DIR__ . '/../../config/config.php';
require_once __DIR__ . '/../../config/db.php';

header('Content-Type: application/json');

try {
    $input = json_decode(file_get_contents('php://input'), true);
    
    if (!$input) {
        echo json_encode(['status' => 'error', 'message' => 'Invalid JSON data']);
        exit;
    }
    
    $course_id = intval($input['course_id']);
    $ci1_max_marks = intval($input['ci1_max_marks']);
    $ci2_max_marks = intval($input['ci2_max_marks']);
    $ci3_max_marks = intval($input['ci3_max_marks']);
    $calculation_method = $input['calculation_method'] ?? 'best2_avg';
    $exam_type = $input['exam_type'] ?? 'Internal';
    $setting_id = isset($input['setting_id']) ? intval($input['setting_id']) : null;
    
    // Validate inputs
    if ($course_id <= 0) {
        echo json_encode(['status' => 'error', 'message' => 'Valid course is required']);
        exit;
    }
    
    if ($ci1_max_marks <= 0 || $ci2_max_marks <= 0 || $ci3_max_marks <= 0) {
        echo json_encode(['status' => 'error', 'message' => 'All CI max marks must be greater than 0']);
        exit;
    }
    
    if (!in_array($calculation_method, ['best2_avg', 'avg3', 'best1'])) {
        echo json_encode(['status' => 'error', 'message' => 'Invalid calculation method']);
        exit;
    }
    
    if (!in_array($exam_type, ['Internal', 'Semester'])) {
        echo json_encode(['status' => 'error', 'message' => 'Invalid exam type']);
        exit;
    }
    
    if ($setting_id) {
        // Update existing setting
        $stmt = $pdo->prepare("
            UPDATE ci_assessment_settings 
            SET ci1_max_marks = ?, ci2_max_marks = ?, ci3_max_marks = ?, calculation_method = ?, exam_type = ?
            WHERE setting_id = ?
        ");
        $result = $stmt->execute([$ci1_max_marks, $ci2_max_marks, $ci3_max_marks, $calculation_method, $exam_type, $setting_id]);
        
        if ($result) {
            echo json_encode([
                'status' => 'success',
                'message' => 'CI assessment settings updated successfully'
            ]);
        } else {
            echo json_encode([
                'status' => 'error',
                'message' => 'Failed to update CI assessment settings'
            ]);
        }
    } else {
        // Insert new setting
        $stmt = $pdo->prepare("
            INSERT INTO ci_assessment_settings (course_id, ci1_max_marks, ci2_max_marks, ci3_max_marks, calculation_method, exam_type)
            VALUES (?, ?, ?, ?, ?, ?)
            ON DUPLICATE KEY UPDATE
                ci1_max_marks = VALUES(ci1_max_marks),
                ci2_max_marks = VALUES(ci2_max_marks),
                ci3_max_marks = VALUES(ci3_max_marks),
                calculation_method = VALUES(calculation_method),
                updated_at = CURRENT_TIMESTAMP
        ");
        $result = $stmt->execute([$course_id, $ci1_max_marks, $ci2_max_marks, $ci3_max_marks, $calculation_method, $exam_type]);
        
        if ($result) {
            echo json_encode([
                'status' => 'success',
                'message' => 'CI assessment settings created successfully'
            ]);
        } else {
            echo json_encode([
                'status' => 'error',
                'message' => 'Failed to create CI assessment settings'
            ]);
        }
    }
    
} catch (Exception $e) {
    echo json_encode([
        'status' => 'error',
        'message' => 'Database error: ' . $e->getMessage()
    ]);
}
?>

PHP File Manager