File "update.php"
Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/grades/update.php
File size: 1.74 KiB (1781 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 {
$id=(int)($_POST['id']??0);
$grading_system_id = (int)($_POST['grading_system_id'] ?? 0);
$grade_name = trim($_POST['grade_name'] ?? '');
$min_marks = (int)($_POST['min_marks'] ?? 0);
$max_marks = (int)($_POST['max_marks'] ?? 0);
$grade_point = (float)($_POST['grade_point'] ?? 0);
$is_pass = isset($_POST['is_pass']) ? (int)($_POST['is_pass'] ? 1 : 0) : 1;
$errors=[]; if($id<=0)$errors[]='Invalid ID.'; if($grading_system_id<=0)$errors[]='Grading System required.'; if($grade_name==='')$errors[]='Grade Name required.'; if ($min_marks<0 || $max_marks<0 || $min_marks>$max_marks) $errors[]='Enter valid min/max marks.'; if($errors){ echo json_encode(['status'=>'error','message'=>$errors]); exit; }
$before=null; try{ $b=$pdo->prepare('SELECT * FROM grades WHERE grade_id=?'); $b->execute([$id]); $before=$b->fetch(PDO::FETCH_ASSOC);}catch(Throwable $e){}
$stmt=$pdo->prepare('UPDATE grades SET grading_system_id=?, grade_name=?, min_marks=?, max_marks=?, grade_point=?, is_pass=? WHERE grade_id=?');
$stmt->execute([$grading_system_id,$grade_name,$min_marks,$max_marks,$grade_point,$is_pass,$id]);
write_audit_log($pdo, ['module'=>'Grading System','action'=>'update','entity_table'=>'grades','entity_id'=>$id,'before_data'=>$before,'after_data'=>compact('grading_system_id','grade_name','min_marks','max_marks','grade_point','is_pass'),'message'=>'Grade updated']);
echo json_encode(['status'=>'success','message'=>'Grade updated successfully.']);
} catch (Throwable $e){ http_response_code(500); echo json_encode(['status'=>'error','message'=>$e->getMessage()]); }
?>