File "delete.php"
Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/grading_systems/delete.php
File size: 1.91 KiB (1951 bytes)
MIME-type: text/x-php
Charset: utf-8
<?php
require_once '../../config/config.php';
require_once '../helper/audit_log.php';
header('Content-Type: application/json');
try {
$raw=file_get_contents('php://input'); $input=json_decode($raw,true);
$id=0; if(is_array($input)&&isset($input['id'])){ $id=(int)$input['id']; } elseif(isset($_POST['id'])){ $id=(int)$_POST['id']; }
if($id<=0){ echo json_encode(['status'=>'error','message'=>'Invalid ID.']); exit; }
$before=null; try{ $b=$pdo->prepare('SELECT * FROM grading_systems WHERE grading_system_id=?'); $b->execute([$id]); $before=$b->fetch(PDO::FETCH_ASSOC);}catch(Throwable $e){}
if(!$before){ http_response_code(404); echo json_encode(['status'=>'error','message'=>'Grading system not found.']); exit; }
// Pre-check: grades referencing this grading system
try{ $cnt=$pdo->prepare('SELECT COUNT(*) FROM grades WHERE grading_system_id=?'); $cnt->execute([$id]); if((int)$cnt->fetchColumn()>0){ http_response_code(409); echo json_encode(['status'=>'error','message'=>'Cannot delete: grades exist under this grading system. Remove them first.']); exit; } }catch(Throwable $e){}
try{ $stmt=$pdo->prepare('DELETE FROM grading_systems WHERE grading_system_id=?'); $stmt->execute([$id]); }
catch(PDOException $e){ $info=$e->errorInfo??null; if(is_array($info)&&isset($info[1])&&(int)$info[1]===1451){ http_response_code(409); echo json_encode(['status'=>'error','message'=>'Cannot delete: grading system is referenced by other records.']); exit; } throw $e; }
write_audit_log($pdo, ['module'=>'Grading System','action'=>'delete','entity_table'=>'grading_systems','entity_id'=>$id,'before_data'=>$before,'message'=>'Grading system deleted']);
echo json_encode(['status'=>'success','message'=>'Grading system deleted successfully.']);
} catch (Throwable $e){ http_response_code(500); echo json_encode(['status'=>'error','message'=>'Request failed. Please try again.']); }
?>