File "delete.php"

Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/mark_entry/delete.php
File size: 1.97 KiB (2022 bytes)
MIME-type: text/x-php
Charset: utf-8

Download   Open   Back

<?php
require_once '../../config/config.php';
header('Content-Type: application/json');

try {
    $input = json_decode(file_get_contents('php://input'), true);
    
    if (!$input) {
        throw new Exception('Invalid JSON input');
    }

    if (!isset($input['session_id']) || empty($input['session_id'])) {
        throw new Exception('Session ID is required');
    }

    $session_id = $input['session_id'];

    // Start transaction
    $pdo->beginTransaction();

    // Check if session has any marks entered
    $checkSql = "SELECT COUNT(*) as count FROM student_marks_entry WHERE session_id = ?";
    $checkStmt = $pdo->prepare($checkSql);
    $checkStmt->execute([$session_id]);
    $count = $checkStmt->fetch(PDO::FETCH_ASSOC)['count'];

    if ($count > 0) {
        throw new Exception('Cannot delete session with existing marks. Please delete marks first.');
    }

    // Delete assignments
    $deleteAssignmentsSql = "DELETE FROM mark_entry_assignments WHERE session_id = ?";
    $deleteAssignmentsStmt = $pdo->prepare($deleteAssignmentsSql);
    $deleteAssignmentsStmt->execute([$session_id]);

    // Delete progress records
    $deleteProgressSql = "DELETE FROM mark_entry_progress WHERE session_id = ?";
    $deleteProgressStmt = $pdo->prepare($deleteProgressSql);
    $deleteProgressStmt->execute([$session_id]);

    // Delete session
    $deleteSessionSql = "DELETE FROM mark_entry_sessions WHERE session_id = ?";
    $deleteSessionStmt = $pdo->prepare($deleteSessionSql);
    $deleteSessionStmt->execute([$session_id]);

    // Commit transaction
    $pdo->commit();

    echo json_encode([
        'status' => 'success',
        'message' => 'Mark entry session deleted successfully'
    ]);

} catch (Exception $e) {
    // Rollback transaction on error
    if ($pdo->inTransaction()) {
        $pdo->rollback();
    }
    
    echo json_encode([
        'status' => 'error',
        'message' => $e->getMessage()
    ]);
}
?>

PHP File Manager