File "get_by_exam.php"
Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/exam_attendance/get_by_exam.php
File size: 5.26 KiB (5391 bytes)
MIME-type: text/x-php
Charset: utf-8
<?php
header('Content-Type: application/json');
require_once '../../config/config.php';
try {
$exam_id = $_GET['exam_id'] ?? null;
$exam_date = $_GET['exam_date'] ?? null;
if (!$exam_id) {
echo json_encode([
'status' => 'error',
'message' => 'Exam ID is required'
]);
exit;
}
// Get exam details
$examQuery = $pdo->prepare("SELECT e.exam_id, e.exam_name, e.academic_year, e.semester, e.status
FROM exams e WHERE e.exam_id = ?");
$examQuery->execute([$exam_id]);
$exam = $examQuery->fetch(PDO::FETCH_ASSOC);
if (!$exam) {
echo json_encode([
'status' => 'error',
'message' => 'Exam not found'
]);
exit;
}
// Get all timetable entries for this exam (with optional date filter)
$timetableQuery = "SELECT et.timetable_id, et.exam_date, et.session, et.start_time, et.end_time,
s.subject_name, s.subject_code, es.max_marks, es.pass_marks
FROM exam_timetable et
JOIN exam_subjects es ON et.exam_subject_id = es.exam_subject_id
JOIN subjects s ON es.subject_id = s.subject_id
WHERE es.exam_id = ?";
$params = [$exam_id];
// Add date filter if provided
if ($exam_date) {
$timetableQuery .= " AND et.exam_date = ?";
$params[] = $exam_date;
}
$timetableQuery .= " ORDER BY et.exam_date, et.session";
$timetableStmt = $pdo->prepare($timetableQuery);
$timetableStmt->execute($params);
$timetables = $timetableStmt->fetchAll(PDO::FETCH_ASSOC);
$result = [
'exam' => $exam,
'timetables' => []
];
foreach ($timetables as $timetable) {
// Get students registered for this timetable
$studentsQuery = $pdo->prepare("SELECT sr.registration_id, s.student_id, s.first_name, s.last_name,
s.university_rollno, c.course_name, c.course_code,
ea.attendance_status, ea.marked_at, ea.remarks, ea.is_late, ea.late_minutes,
st.first_name as marked_by_name, st.last_name as marked_by_last_name
FROM student_registrations sr
JOIN students s ON sr.student_id = s.student_id
JOIN courses c ON s.course_id = c.course_id
JOIN exam_subjects es ON sr.exam_subject_id = es.exam_subject_id
JOIN exam_timetable et ON es.exam_subject_id = et.exam_subject_id
LEFT JOIN tbl_exam_attendance ea ON sr.registration_id = ea.registration_id
LEFT JOIN staff st ON ea.marked_by = st.staff_id
WHERE et.timetable_id = ?
ORDER BY s.university_rollno");
$studentsQuery->execute([$timetable['timetable_id']]);
$students = $studentsQuery->fetchAll(PDO::FETCH_ASSOC);
// Calculate statistics for this timetable
$totalStudents = count($students);
$presentCount = count(array_filter($students, function($s) { return $s['attendance_status'] === 'Present'; }));
$absentCount = count(array_filter($students, function($s) { return $s['attendance_status'] === 'Absent'; }));
$unmarkedCount = count(array_filter($students, function($s) { return $s['attendance_status'] === null; }));
$lateCount = count(array_filter($students, function($s) { return $s['is_late'] == 1; }));
$timetable['students'] = $students;
$timetable['statistics'] = [
'total_students' => $totalStudents,
'present_count' => $presentCount,
'absent_count' => $absentCount,
'unmarked_count' => $unmarkedCount,
'late_count' => $lateCount,
'attendance_percentage' => $totalStudents > 0 ? round(($presentCount / $totalStudents) * 100, 2) : 0
];
$result['timetables'][] = $timetable;
}
// Overall statistics
$totalStudents = array_sum(array_column($result['timetables'], 'statistics'));
$totalPresent = array_sum(array_column(array_column($result['timetables'], 'statistics'), 'present_count'));
$totalAbsent = array_sum(array_column(array_column($result['timetables'], 'statistics'), 'absent_count'));
$totalUnmarked = array_sum(array_column(array_column($result['timetables'], 'statistics'), 'unmarked_count'));
$result['overall_statistics'] = [
'total_students' => $totalStudents,
'total_present' => $totalPresent,
'total_absent' => $totalAbsent,
'total_unmarked' => $totalUnmarked,
'overall_attendance_percentage' => $totalStudents > 0 ? round(($totalPresent / $totalStudents) * 100, 2) : 0
];
echo json_encode([
'status' => 'success',
'message' => 'Exam attendance data retrieved successfully',
'data' => $result
]);
} catch (Exception $e) {
echo json_encode([
'status' => 'error',
'message' => 'Error: ' . $e->getMessage()
]);
}
?>