File "bulk_create.php"
Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/exam_attendance/bulk_create.php
File size: 4.4 KiB (4509 bytes)
MIME-type: text/x-php
Charset: utf-8
<?php
header('Content-Type: application/json');
require_once '../../config/config.php';
try {
$input = json_decode(file_get_contents('php://input'), true);
if (!$input) {
echo json_encode([
'status' => 'error',
'message' => 'Invalid JSON input'
]);
exit;
}
$attendance_data = $input['attendance_data'] ?? [];
if (empty($attendance_data)) {
echo json_encode([
'status' => 'error',
'message' => 'Attendance data is required'
]);
exit;
}
$results = [
'updated_count' => 0,
'created_count' => 0,
'skipped_count' => 0,
'errors' => []
];
// Start transaction
$pdo->beginTransaction();
try {
// Process each attendance record
foreach ($attendance_data as $attendance) {
$registration_id = $attendance['registration_id'] ?? null;
$timetable_id = $attendance['timetable_id'] ?? null;
$status = $attendance['attendance_status'] ?? 'Present';
if (!$registration_id) {
$results['errors'][] = 'Missing registration_id in attendance data';
continue;
}
if (!in_array($status, ['Present', 'Absent'])) {
$results['errors'][] = "Invalid attendance status for registration_id: $registration_id";
continue;
}
// Check if attendance already exists
$existing = $pdo->prepare("SELECT attendance_id FROM tbl_exam_attendance WHERE registration_id = ?");
$existing->execute([$registration_id]);
$existingRecord = $existing->fetch(PDO::FETCH_ASSOC);
if ($existingRecord) {
// Update existing record
$update = $pdo->prepare("UPDATE tbl_exam_attendance
SET attendance_status = ?, marked_at = CURRENT_TIMESTAMP
WHERE registration_id = ?");
$update->execute([$status, $registration_id]);
$results['updated_count']++;
} else {
// Create new record - handle filtered data case
if ($timetable_id === 'filtered') {
// For filtered data, we need to get the actual timetable_id
$getTimetable = $pdo->prepare("
SELECT et.timetable_id
FROM exam_timetable et
JOIN exam_subjects es ON et.exam_subject_id = es.exam_subject_id
JOIN student_registrations sr ON es.exam_subject_id = sr.exam_subject_id
WHERE sr.registration_id = ?
LIMIT 1
");
$getTimetable->execute([$registration_id]);
$timetable = $getTimetable->fetch(PDO::FETCH_ASSOC);
$timetable_id = $timetable ? $timetable['timetable_id'] : null;
}
if ($timetable_id) {
$insert = $pdo->prepare("INSERT INTO tbl_exam_attendance
(registration_id, timetable_id, attendance_status)
VALUES (?, ?, ?)");
$insert->execute([$registration_id, $timetable_id, $status]);
$results['created_count']++;
} else {
$results['errors'][] = "Could not find timetable for registration_id: $registration_id";
}
}
}
// Commit transaction
$pdo->commit();
$total_processed = $results['updated_count'] + $results['created_count'];
echo json_encode([
'status' => 'success',
'message' => "Successfully processed $total_processed attendance records",
'updated_count' => $total_processed,
'data' => $results
]);
} catch (Exception $e) {
// Rollback transaction on error
$pdo->rollBack();
throw $e;
}
} catch (Exception $e) {
echo json_encode([
'status' => 'error',
'message' => 'Error: ' . $e->getMessage()
]);
}
?>