File "preview_sync.php"
Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/exam_subjects/preview_sync.php
File size: 4.43 KiB (4532 bytes)
MIME-type: text/x-php
Charset: utf-8
<?php
require_once '../../config/config.php';
header('Content-Type: application/json');
try {
$examIds = $_POST['exam_ids'] ?? [];
$courseIds = $_POST['course_ids'] ?? [];
if (empty($examIds) || empty($courseIds)) {
echo json_encode(['success' => false, 'message' => 'Exam IDs and Course IDs are required']);
exit;
}
// Convert arrays to comma-separated strings for SQL IN clause
$examIdsStr = implode(',', array_map('intval', $examIds));
$courseIdsStr = implode(',', array_map('intval', $courseIds));
// Get exam details
$examsSql = "SELECT e.exam_id, e.exam_name, e.academic_year, e.semester
FROM exams e
WHERE e.exam_id IN ($examIdsStr) AND e.status = 'Active'";
$examsStmt = $pdo->prepare($examsSql);
$examsStmt->execute();
$exams = $examsStmt->fetchAll(PDO::FETCH_ASSOC);
if (empty($exams)) {
echo json_encode(['success' => false, 'message' => 'No active exams found']);
exit;
}
$previewData = [];
foreach ($exams as $exam) {
// Get subjects for the selected courses and this exam's semester
$subjectsSql = "SELECT s.subject_id, s.subject_name, s.subject_code, c.course_id, c.course_name
FROM subjects s
LEFT JOIN courses c ON c.course_id = s.course_id
WHERE s.course_id IN ($courseIdsStr) AND s.semester = ?
ORDER BY c.course_name, s.subject_name";
$subjectsStmt = $pdo->prepare($subjectsSql);
$subjectsStmt->execute([$exam['semester']]);
$subjects = $subjectsStmt->fetchAll(PDO::FETCH_ASSOC);
if (empty($subjects)) {
continue; // Skip if no subjects found
}
// Check existing exam subjects for this exam
$existingSql = "SELECT es.subject_id, s.subject_name, s.subject_code
FROM exam_subjects es
LEFT JOIN subjects s ON s.subject_id = es.subject_id
WHERE es.exam_id = ?";
$existingStmt = $pdo->prepare($existingSql);
$existingStmt->execute([$exam['exam_id']]);
$existingSubjects = $existingStmt->fetchAll(PDO::FETCH_ASSOC);
$existingSubjectIds = array_column($existingSubjects, 'subject_id');
// Group subjects by course
$subjectsByCourse = [];
foreach ($subjects as $subject) {
$courseId = $subject['course_id'];
if (!isset($subjectsByCourse[$courseId])) {
$subjectsByCourse[$courseId] = [
'course_id' => $courseId,
'course_name' => $subject['course_name'],
'subjects' => []
];
}
$subjectsByCourse[$courseId]['subjects'][] = $subject;
}
// Process each course
foreach ($subjectsByCourse as $courseData) {
$subjectsToSync = [];
$subjectsExisting = [];
foreach ($courseData['subjects'] as $subject) {
if (in_array($subject['subject_id'], $existingSubjectIds)) {
$subjectsExisting[] = $subject['subject_name'];
} else {
$subjectsToSync[] = $subject['subject_name'];
}
}
$previewData[] = [
'exam_id' => $exam['exam_id'],
'exam_name' => $exam['exam_name'],
'course_id' => $courseData['course_id'],
'course_name' => $courseData['course_name'],
'semester' => $exam['semester'],
'subjects_to_sync' => count($subjectsToSync),
'subjects_existing' => count($subjectsExisting),
'subjects_to_sync_list' => $subjectsToSync,
'subjects_existing_list' => $subjectsExisting
];
}
}
echo json_encode([
'success' => true,
'data' => $previewData,
'summary' => [
'total_exams' => count($previewData),
'total_to_sync' => array_sum(array_column($previewData, 'subjects_to_sync')),
'total_existing' => array_sum(array_column($previewData, 'subjects_existing'))
]
]);
} catch (Exception $e) {
http_response_code(500);
echo json_encode(['success' => false, 'message' => 'Database error: ' . $e->getMessage()]);
}
?>