false, 'message' => 'Invalid activity ID']); exit(); } // Check if activity exists $check_sql = "SELECT id, name, date, time_in, time_out FROM activities WHERE id = ?"; $check_stmt = mysqli_prepare($conn, $check_sql); mysqli_stmt_bind_param($check_stmt, "i", $activity_id); mysqli_stmt_execute($check_stmt); $result = mysqli_stmt_get_result($check_stmt); $activity = mysqli_fetch_assoc($result); if (!$activity) { echo json_encode(['success' => false, 'message' => 'Activity not found']); exit(); } // Check if activity is currently ongoing $today = date('Y-m-d'); $current_time = date('H:i:s'); if ($activity['date'] == $today && $current_time >= $activity['time_in'] && $current_time <= $activity['time_out']) { echo json_encode([ 'success' => false, 'message' => 'Cannot delete an activity that is currently ongoing. Please wait until it ends.' ]); exit(); } // Start transaction mysqli_begin_transaction($conn); try { // Get attendance count for logging $attendance_count_sql = "SELECT COUNT(*) as count FROM attendance WHERE activity_id = ?"; $attendance_stmt = mysqli_prepare($conn, $attendance_count_sql); mysqli_stmt_bind_param($attendance_stmt, "i", $activity_id); mysqli_stmt_execute($attendance_stmt); $attendance_result = mysqli_stmt_get_result($attendance_stmt); $attendance_count = mysqli_fetch_assoc($attendance_result)['count']; // First, delete attendance records $delete_attendance_sql = "DELETE FROM attendance WHERE activity_id = ?"; $stmt1 = mysqli_prepare($conn, $delete_attendance_sql); mysqli_stmt_bind_param($stmt1, "i", $activity_id); $attendance_deleted = mysqli_stmt_execute($stmt1); if (!$attendance_deleted) { throw new Exception('Failed to delete attendance records'); } // Then delete the activity $delete_activity_sql = "DELETE FROM activities WHERE id = ?"; $stmt2 = mysqli_prepare($conn, $delete_activity_sql); mysqli_stmt_bind_param($stmt2, "i", $activity_id); $activity_deleted = mysqli_stmt_execute($stmt2); if (!$activity_deleted) { throw new Exception('Failed to delete activity'); } // Commit transaction mysqli_commit($conn); // Log the deletion $log_message = "Activity deleted via AJAX: '{$activity['name']}' (ID: {$activity_id}). "; $log_message .= "Deleted {$attendance_count} attendance records."; log_action($conn, $_SESSION['user_id'], 'delete_activity', $log_message); echo json_encode([ 'success' => true, 'message' => 'Activity deleted successfully!', 'deleted_id' => $activity_id, 'attendance_count' => $attendance_count ]); } catch (Exception $e) { mysqli_rollback($conn); echo json_encode([ 'success' => false, 'message' => 'Error: ' . $e->getMessage() ]); } exit(); } // Generate CSRF token for this page $csrf_token = bin2hex(random_bytes(32)); $_SESSION['csrf_token'] = $csrf_token; // Get all activities with related data $activities = []; $sql = "SELECT a.*, u.full_name as created_by_name, c.code as course_code, c.name as course_name, d.code as department_code, d.name as department_name FROM activities a LEFT JOIN users u ON a.created_by = u.id LEFT JOIN courses c ON a.course_id = c.id LEFT JOIN departments d ON a.department_id = d.id ORDER BY a.date DESC, a.time_in ASC"; $result = query($conn, $sql); while ($row = mysqli_fetch_assoc($result)) { $activities[] = $row; } // Statistics calculation $today = date('Y-m-d'); $upcoming_count = 0; $ongoing_count = 0; $past_count = 0; foreach ($activities as $activity) { $activity_date = $activity['date']; $current_time = date('H:i:s'); $activity_start = $activity['time_in']; $activity_end = $activity['time_out']; if ($activity_date > $today) { $upcoming_count++; } elseif ($activity_date == $today && $current_time >= $activity_start && $current_time <= $activity_end) { $ongoing_count++; } elseif ($activity_date < $today || ($activity_date == $today && $current_time > $activity_end)) { $past_count++; } } include '../includes/header.php'; ?>
Schedule and manage activities, events, and classes.
| Activity | Date & Time | Location | Participants | Status | Actions |
|---|---|---|---|---|---|
|
|
- | All Students'; break; case 'specific_course': echo '' . htmlspecialchars($activity['course_code'] ?? 'Specific Course') . ''; break; case 'specific_department': echo '' . htmlspecialchars($activity['department_name'] ?? 'Specific Department') . ''; break; } ?> | Active Inactive | ||
|
No upcoming activities scheduled |
|||||
| Activity | Time | Location | Duration | Participants | Actions |
|---|---|---|---|---|---|
|
|
-
Ends in
|
' . $duration . ' hours'; ?> | All Students'; break; case 'specific_course': echo '' . htmlspecialchars($activity['course_code'] ?? 'Specific Course') . ''; break; case 'specific_department': echo '' . htmlspecialchars($activity['department_name'] ?? 'Specific Department') . ''; break; } ?> | ||
|
No ongoing activities at the moment |
|||||
| Activity | Date | Time | Location | Attendance | Actions |
|---|---|---|---|---|---|
|
|
- | attended | |||
|
No past activities found |
|||||
| Activity | Date & Time | Location | Participants | Created By | Status | Actions |
|---|---|---|---|---|---|---|
|
No activities found. Schedule your first activity! |
||||||
|
|
- | All Students'; break; case 'specific_course': echo '' . htmlspecialchars($activity['course_code'] ?? 'Specific Course') . ''; break; case 'specific_department': echo '' . htmlspecialchars($activity['department_name'] ?? 'Specific Department') . ''; break; } ?> |
|
Active Inactive | ||
No upcoming activities scheduled
No ongoing activities at the moment
No past activities found
No activities found. Schedule your first activity!