File "announcements_save.php"
Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/role/announcements_save.php
File size: 2.7 KiB (2769 bytes)
MIME-type: text/x-php
Charset: utf-8
<?php
require_once __DIR__ . '/../../config/config.php';
header('Content-Type: application/json');
try {
$course_id = (int)($_POST['course_id'] ?? 0);
$academic_year = trim($_POST['academic_year'] ?? '');
$exam_type_id = (int)($_POST['exam_type_id'] ?? 0);
$start_at = trim($_POST['start_at'] ?? '');
$end_at = trim($_POST['end_at'] ?? '');
$is_active = isset($_POST['is_active']) ? (int)$_POST['is_active'] : 1;
if($course_id<=0 || !$academic_year || $exam_type_id<=0 || !$start_at || !$end_at){
echo json_encode(['status'=>'error','message'=>'Missing fields']);
exit;
}
global $pdo;
// Check if table exists, if not create it
$stmt = $pdo->query("SHOW TABLES LIKE 'result_announcements'");
if ($stmt->rowCount() == 0) {
$createTableSQL = "CREATE TABLE IF NOT EXISTS `result_announcements` (
`announcement_id` int(11) NOT NULL AUTO_INCREMENT,
`course_id` int(11) NOT NULL,
`academic_year` varchar(50) NOT NULL,
`exam_type_id` int(11) NOT NULL,
`start_at` datetime NOT NULL,
`end_at` datetime NOT NULL,
`is_active` tinyint(1) NOT NULL DEFAULT 1,
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`announcement_id`),
KEY `idx_course_year_type` (`course_id`,`academic_year`,`exam_type_id`,`is_active`),
CONSTRAINT `fk_ra_course` FOREIGN KEY (`course_id`) REFERENCES `courses` (`course_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
$pdo->exec($createTableSQL);
}
// Check for duplicates
$stmt = $pdo->prepare("SELECT COUNT(*) as count FROM result_announcements
WHERE course_id = ? AND academic_year = ? AND exam_type_id = ?");
$stmt->execute([$course_id, $academic_year, $exam_type_id]);
$duplicate = $stmt->fetch(PDO::FETCH_ASSOC);
if ($duplicate['count'] > 0) {
echo json_encode(['status' => 'error', 'message' => 'Announcement already exists for this course, academic year, and exam type']);
exit;
}
$stmt = $pdo->prepare("INSERT INTO result_announcements (course_id, academic_year, exam_type_id, start_at, end_at, is_active)
VALUES (?,?,?,?,?,?)");
$stmt->execute([$course_id, $academic_year, $exam_type_id, $start_at, $end_at, $is_active]);
echo json_encode(['status' => 'success', 'message' => 'Announcement created successfully']);
} catch (Exception $e){
http_response_code(500);
echo json_encode(['status'=>'error','message'=>$e->getMessage()]);
}
?>