Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/portal/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/portal/ministry-attendance-admin.php

<?php
require_once 'config/config.php';

$pageTitle = "Ministry Attendance Admin - " . APP_NAME;

$db = Database::getInstance()->getConnection();
$stmt = $db->query("SELECT * FROM general_settings ORDER BY id DESC LIMIT 1");
$settings = $stmt->fetch();

$settings = array_merge([
    'site_title' => 'Church Membership System',
    'theme_primary_color' => '#1E40AF',
    'theme_secondary_color' => '#F97316',
], $settings ?: []);

$ministryId = isset($_GET['ministry_id']) ? intval($_GET['ministry_id']) : 0;
$dateFrom = $_GET['date_from'] ?? date('Y-m-01');
$dateTo = $_GET['date_to'] ?? date('Y-m-d');

if (!$ministryId) {
    header('Location: ministry-attendance.php');
    exit;
}

// Get ministry details
$ministryStmt = $db->prepare("
    SELECT m.*, mc.category_name, mc.icon, mc.color, a.assembly_name, d.district_name, ar.area_name
    FROM ministries m
    LEFT JOIN ministry_categories mc ON m.category_id = mc.id
    LEFT JOIN assemblies a ON m.assembly_id = a.id
    LEFT JOIN districts d ON m.district_id = d.id
    LEFT JOIN areas ar ON m.area_id = ar.id
    WHERE m.id = :id
");
$ministryStmt->execute(['id' => $ministryId]);
$ministry = $ministryStmt->fetch();

if (!$ministry) {
    header('Location: ministry-attendance.php');
    exit;
}

// Get attendance records for date range
$attendanceStmt = $db->prepare("
    SELECT ma.*, m.first_name, m.last_name, m.membershipcard_id, m.phone as member_phone
    FROM ministry_attendance ma
    LEFT JOIN members m ON ma.member_id = m.id
    WHERE ma.ministry_id = :ministry_id 
    AND ma.attendance_date BETWEEN :date_from AND :date_to
    ORDER BY ma.attendance_date DESC, ma.check_in_time DESC
");
$attendanceStmt->execute([
    'ministry_id' => $ministryId,
    'date_from' => $dateFrom,
    'date_to' => $dateTo
]);
$attendanceRecords = $attendanceStmt->fetchAll();

// Get ministry members
$membersStmt = $db->prepare("
    SELECT mm.*, m.id as member_id, m.first_name, m.last_name, m.membershipcard_id
    FROM ministry_members mm
    JOIN members m ON mm.member_id = m.id
    WHERE mm.ministry_id = :ministry_id AND mm.status = 'active'
    ORDER BY m.first_name, m.last_name
");
$membersStmt->execute(['ministry_id' => $ministryId]);
$ministryMembers = $membersStmt->fetchAll();

// Calculate statistics
$totalMembers = count($ministryMembers);
$uniqueDates = array_unique(array_column($attendanceRecords, 'attendance_date'));
$totalMeetings = count($uniqueDates);

$memberAttendance = array_filter($attendanceRecords, fn($a) => $a['attendance_type'] === 'member');
$guestAttendance = array_filter($attendanceRecords, fn($a) => $a['attendance_type'] !== 'member');

// Calculate attendance by member
$memberAttendanceCounts = [];
foreach ($memberAttendance as $att) {
    $memberId = $att['member_id'];
    if (!isset($memberAttendanceCounts[$memberId])) {
        $memberAttendanceCounts[$memberId] = [
            'name' => $att['first_name'] . ' ' . $att['last_name'],
            'count' => 0
        ];
    }
    $memberAttendanceCounts[$memberId]['count']++;
}

// Handle CSV Export
if (isset($_GET['export']) && $_GET['export'] === 'csv') {
    header('Content-Type: text/csv');
    header('Content-Disposition: attachment; filename="ministry_attendance_' . $ministry['ministry_name'] . '_' . date('Y-m-d') . '.csv"');
    
    $output = fopen('php://output', 'w');
    fputcsv($output, ['Ministry Attendance Report']);
    fputcsv($output, ['Ministry:', $ministry['ministry_name']]);
    fputcsv($output, ['Date Range:', $dateFrom . ' to ' . $dateTo]);
    fputcsv($output, ['Generated:', date('Y-m-d H:i:s')]);
    fputcsv($output, []);
    fputcsv($output, ['Date', 'Name', 'Type', 'Phone', 'Check-in Time', 'Status']);
    
    foreach ($attendanceRecords as $record) {
        $name = $record['member_id'] ? $record['first_name'] . ' ' . $record['last_name'] : $record['guest_name'];
        $phone = $record['member_id'] ? $record['member_phone'] : $record['guest_phone'];
        fputcsv($output, [
            $record['attendance_date'],
            $name,
            ucfirst($record['attendance_type']),
            $phone,
            date('g:i A', strtotime($record['check_in_time'])),
            ucfirst($record['status'])
        ]);
    }
    
    fclose($output);
    exit;
}

// Handle HTML Report Generation
if (isset($_GET['report']) && $_GET['report'] === 'html') {
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <title>Ministry Attendance Report - <?php echo htmlspecialchars($ministry['ministry_name']); ?></title>
        <style>
            body { font-family: Arial, sans-serif; margin: 20px; }
            .header { text-align: center; margin-bottom: 30px; }
            .header h1 { color: #1E40AF; margin-bottom: 5px; }
            .stats { display: flex; justify-content: space-around; margin: 20px 0; }
            .stat-box { text-align: center; padding: 15px; background: #f3f4f6; border-radius: 8px; }
            .stat-box h3 { margin: 0; font-size: 24px; color: #1E40AF; }
            .stat-box p { margin: 5px 0 0; color: #666; }
            table { width: 100%; border-collapse: collapse; margin-top: 20px; }
            th, td { border: 1px solid #ddd; padding: 10px; text-align: left; }
            th { background: #1E40AF; color: white; }
            tr:nth-child(even) { background: #f9fafb; }
            .member { color: #9333EA; }
            .guest { color: #F97316; }
            .footer { margin-top: 30px; text-align: center; color: #666; font-size: 12px; }
            @media print { .no-print { display: none; } }
        </style>
    </head>
    <body>
        <div class="header">
            <h1><?php echo htmlspecialchars($ministry['ministry_name']); ?></h1>
            <p>Attendance Report: <?php echo date('M j, Y', strtotime($dateFrom)); ?> - <?php echo date('M j, Y', strtotime($dateTo)); ?></p>
            <p><?php echo htmlspecialchars($ministry['assembly_name'] ?? ''); ?></p>
        </div>
        
        <div class="stats">
            <div class="stat-box">
                <h3><?php echo $totalMembers; ?></h3>
                <p>Total Members</p>
            </div>
            <div class="stat-box">
                <h3><?php echo $totalMeetings; ?></h3>
                <p>Meetings Held</p>
            </div>
            <div class="stat-box">
                <h3><?php echo count($memberAttendance); ?></h3>
                <p>Member Check-ins</p>
            </div>
            <div class="stat-box">
                <h3><?php echo count($guestAttendance); ?></h3>
                <p>Guest/Visitor Check-ins</p>
            </div>
        </div>
        
        <h2>Attendance Records</h2>
        <table>
            <thead>
                <tr>
                    <th>#</th>
                    <th>Date</th>
                    <th>Name</th>
                    <th>Type</th>
                    <th>Phone</th>
                    <th>Time</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($attendanceRecords as $i => $record): ?>
                <tr>
                    <td><?php echo $i + 1; ?></td>
                    <td><?php echo date('M j, Y', strtotime($record['attendance_date'])); ?></td>
                    <td><?php echo $record['member_id'] ? htmlspecialchars($record['first_name'] . ' ' . $record['last_name']) : htmlspecialchars($record['guest_name']); ?></td>
                    <td class="<?php echo $record['attendance_type'] === 'member' ? 'member' : 'guest'; ?>">
                        <?php echo ucfirst($record['attendance_type']); ?>
                    </td>
                    <td><?php echo $record['member_id'] ? htmlspecialchars($record['member_phone'] ?? '') : htmlspecialchars($record['guest_phone'] ?? ''); ?></td>
                    <td><?php echo date('g:i A', strtotime($record['check_in_time'])); ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        
        <h2>Member Attendance Summary</h2>
        <table>
            <thead>
                <tr>
                    <th>#</th>
                    <th>Member Name</th>
                    <th>Times Present</th>
                    <th>Attendance Rate</th>
                </tr>
            </thead>
            <tbody>
                <?php 
                $i = 0;
                foreach ($ministryMembers as $member): 
                    $count = $memberAttendanceCounts[$member['member_id']]['count'] ?? 0;
                    $rate = $totalMeetings > 0 ? round(($count / $totalMeetings) * 100) : 0;
                ?>
                <tr>
                    <td><?php echo ++$i; ?></td>
                    <td><?php echo htmlspecialchars($member['first_name'] . ' ' . $member['last_name']); ?></td>
                    <td><?php echo $count; ?> / <?php echo $totalMeetings; ?></td>
                    <td><?php echo $rate; ?>%</td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        
        <div class="footer">
            <p>Generated on <?php echo date('F j, Y \a\t g:i A'); ?></p>
            <p><?php echo htmlspecialchars($settings['site_title']); ?></p>
        </div>
        
        <div class="no-print" style="text-align: center; margin-top: 20px;">
            <button onclick="window.print()" style="padding: 10px 20px; background: #1E40AF; color: white; border: none; border-radius: 5px; cursor: pointer;">
                Print Report
            </button>
            <button onclick="window.close()" style="padding: 10px 20px; background: #666; color: white; border: none; border-radius: 5px; cursor: pointer; margin-left: 10px;">
                Close
            </button>
        </div>
    </body>
    </html>
    <?php
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $pageTitle; ?></title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        .hero-gradient { background: linear-gradient(135deg, #3B82F6 0%, #1E40AF 100%); }
        .btn-gradient { background: linear-gradient(135deg, #3B82F6 0%, #1E40AF 100%); }
    </style>
</head>
<body class="bg-gray-50 min-h-screen">
    <!-- Header -->
    <header class="bg-white shadow-lg sticky top-0 z-50">
        <div class="container mx-auto px-4">
            <div class="flex items-center justify-between h-16">
                <div class="flex items-center space-x-3">
                    <div class="w-12 h-12 rounded-xl flex items-center justify-center hero-gradient">
                        <i class="fas fa-church text-white text-xl"></i>
                    </div>
                    <div>
                        <h1 class="text-xl font-bold text-blue-800"><?php echo htmlspecialchars($settings['site_title']); ?></h1>
                        <p class="text-xs text-gray-500">Ministry Attendance Admin</p>
                    </div>
                </div>
                
                <nav class="flex items-center space-x-4">
                    <a href="ministry-attendance.php" class="text-gray-700 hover:text-blue-600 font-medium transition">
                        <i class="fas fa-arrow-left mr-1"></i>Back
                    </a>
                </nav>
            </div>
        </div>
    </header>

    <!-- Ministry Header -->
    <section class="py-6" style="background-color: <?php echo $ministry['color'] ?? '#3B82F6'; ?>;">
        <div class="container mx-auto px-4">
            <div class="flex items-center justify-between text-white">
                <div class="flex items-center space-x-4">
                    <div class="w-16 h-16 rounded-xl bg-white/20 flex items-center justify-center">
                        <i class="fas fa-<?php echo $ministry['icon'] ?? 'users'; ?> text-2xl"></i>
                    </div>
                    <div>
                        <h1 class="text-2xl font-bold"><?php echo htmlspecialchars($ministry['ministry_name']); ?></h1>
                        <p class="text-white/80"><?php echo htmlspecialchars($ministry['assembly_name'] ?? ''); ?></p>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-4 py-8">
        <!-- Date Filter -->
        <div class="bg-white rounded-xl shadow-lg p-6 mb-6">
            <!-- Quick Filter Buttons -->
            <div class="flex flex-wrap gap-2 mb-4">
                <span class="text-sm font-medium text-gray-700 mr-2 self-center">Quick Filter:</span>
                <a href="?ministry_id=<?php echo $ministryId; ?>&date_from=<?php echo date('Y-m-d'); ?>&date_to=<?php echo date('Y-m-d'); ?>" 
                   class="px-3 py-1 text-sm rounded-full <?php echo ($dateFrom === date('Y-m-d') && $dateTo === date('Y-m-d')) ? 'bg-blue-600 text-white' : 'bg-gray-200 text-gray-700 hover:bg-gray-300'; ?>">
                    Today
                </a>
                <a href="?ministry_id=<?php echo $ministryId; ?>&date_from=<?php echo date('Y-m-d', strtotime('monday this week')); ?>&date_to=<?php echo date('Y-m-d'); ?>" 
                   class="px-3 py-1 text-sm rounded-full <?php echo ($dateFrom === date('Y-m-d', strtotime('monday this week'))) ? 'bg-blue-600 text-white' : 'bg-gray-200 text-gray-700 hover:bg-gray-300'; ?>">
                    This Week
                </a>
                <a href="?ministry_id=<?php echo $ministryId; ?>&date_from=<?php echo date('Y-m-01'); ?>&date_to=<?php echo date('Y-m-d'); ?>" 
                   class="px-3 py-1 text-sm rounded-full <?php echo ($dateFrom === date('Y-m-01')) ? 'bg-blue-600 text-white' : 'bg-gray-200 text-gray-700 hover:bg-gray-300'; ?>">
                    This Month
                </a>
                <a href="?ministry_id=<?php echo $ministryId; ?>&date_from=<?php echo date('Y-01-01', strtotime(date('Y') . '-' . (date('n') <= 6 ? '01' : '07') . '-01')); ?>&date_to=<?php echo date('Y-m-d'); ?>" 
                   class="px-3 py-1 text-sm rounded-full bg-gray-200 text-gray-700 hover:bg-gray-300">
                    Half Year
                </a>
                <a href="?ministry_id=<?php echo $ministryId; ?>&date_from=<?php echo date('Y-01-01'); ?>&date_to=<?php echo date('Y-m-d'); ?>" 
                   class="px-3 py-1 text-sm rounded-full <?php echo ($dateFrom === date('Y-01-01')) ? 'bg-blue-600 text-white' : 'bg-gray-200 text-gray-700 hover:bg-gray-300'; ?>">
                    Full Year
                </a>
            </div>
            
            <form method="GET" class="flex flex-wrap items-end gap-4">
                <input type="hidden" name="ministry_id" value="<?php echo $ministryId; ?>">
                
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">From Date</label>
                    <input type="date" name="date_from" value="<?php echo $dateFrom; ?>"
                           class="px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                </div>
                
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">To Date</label>
                    <input type="date" name="date_to" value="<?php echo $dateTo; ?>"
                           class="px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                </div>
                
                <button type="submit" class="px-6 py-2 btn-gradient text-white rounded-lg font-semibold">
                    <i class="fas fa-filter mr-2"></i>Filter
                </button>
                
                <a href="?ministry_id=<?php echo $ministryId; ?>&date_from=<?php echo $dateFrom; ?>&date_to=<?php echo $dateTo; ?>&export=csv" 
                   class="px-6 py-2 bg-green-600 hover:bg-green-700 text-white rounded-lg font-semibold">
                    <i class="fas fa-file-csv mr-2"></i>Export CSV
                </a>
                
                <a href="?ministry_id=<?php echo $ministryId; ?>&date_from=<?php echo $dateFrom; ?>&date_to=<?php echo $dateTo; ?>&report=html" 
                   target="_blank"
                   class="px-6 py-2 bg-purple-600 hover:bg-purple-700 text-white rounded-lg font-semibold">
                    <i class="fas fa-file-alt mr-2"></i>Generate Report
                </a>
            </form>
        </div>
        
        <!-- Statistics -->
        <div class="grid grid-cols-2 md:grid-cols-4 gap-4 mb-6">
            <div class="bg-white rounded-xl shadow p-6 text-center">
                <p class="text-3xl font-bold text-blue-600"><?php echo $totalMembers; ?></p>
                <p class="text-gray-500">Total Members</p>
            </div>
            <div class="bg-white rounded-xl shadow p-6 text-center">
                <p class="text-3xl font-bold text-purple-600"><?php echo $totalMeetings; ?></p>
                <p class="text-gray-500">Meetings Held</p>
            </div>
            <div class="bg-white rounded-xl shadow p-6 text-center">
                <p class="text-3xl font-bold text-green-600"><?php echo count($memberAttendance); ?></p>
                <p class="text-gray-500">Member Check-ins</p>
            </div>
            <div class="bg-white rounded-xl shadow p-6 text-center">
                <p class="text-3xl font-bold text-orange-600"><?php echo count($guestAttendance); ?></p>
                <p class="text-gray-500">Guest Check-ins</p>
            </div>
        </div>
        
        <!-- Attendance Records -->
        <div class="bg-white rounded-xl shadow-lg p-6 mb-6">
            <h3 class="text-lg font-bold text-gray-800 mb-4">
                <i class="fas fa-list mr-2 text-blue-500"></i>Attendance Records (<?php echo count($attendanceRecords); ?>)
            </h3>
            
            <?php if (count($attendanceRecords) > 0): ?>
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-4 py-3 text-left text-sm font-semibold text-gray-600">#</th>
                            <th class="px-4 py-3 text-left text-sm font-semibold text-gray-600">Date</th>
                            <th class="px-4 py-3 text-left text-sm font-semibold text-gray-600">Name</th>
                            <th class="px-4 py-3 text-left text-sm font-semibold text-gray-600">Type</th>
                            <th class="px-4 py-3 text-left text-sm font-semibold text-gray-600">Phone</th>
                            <th class="px-4 py-3 text-left text-sm font-semibold text-gray-600">Time</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-100">
                        <?php foreach ($attendanceRecords as $i => $record): ?>
                        <tr class="hover:bg-gray-50">
                            <td class="px-4 py-3 text-sm"><?php echo $i + 1; ?></td>
                            <td class="px-4 py-3 text-sm"><?php echo date('M j, Y', strtotime($record['attendance_date'])); ?></td>
                            <td class="px-4 py-3 font-medium">
                                <?php echo $record['member_id'] ? htmlspecialchars($record['first_name'] . ' ' . $record['last_name']) : htmlspecialchars($record['guest_name']); ?>
                            </td>
                            <td class="px-4 py-3">
                                <span class="px-2 py-1 rounded-full text-xs font-medium 
                                    <?php echo $record['attendance_type'] === 'member' ? 'bg-purple-100 text-purple-800' : 'bg-orange-100 text-orange-800'; ?>">
                                    <?php echo ucfirst($record['attendance_type']); ?>
                                </span>
                            </td>
                            <td class="px-4 py-3 text-sm text-gray-500">
                                <?php echo $record['member_id'] ? htmlspecialchars($record['member_phone'] ?? '-') : htmlspecialchars($record['guest_phone'] ?? '-'); ?>
                            </td>
                            <td class="px-4 py-3 text-sm text-gray-500">
                                <?php echo date('g:i A', strtotime($record['check_in_time'])); ?>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <?php else: ?>
            <div class="text-center py-8 text-gray-500">
                <i class="fas fa-clipboard-list text-4xl mb-3"></i>
                <p>No attendance records found for this date range.</p>
            </div>
            <?php endif; ?>
        </div>
        
        <!-- Member Attendance Summary -->
        <div class="bg-white rounded-xl shadow-lg p-6">
            <h3 class="text-lg font-bold text-gray-800 mb-4">
                <i class="fas fa-chart-bar mr-2 text-purple-500"></i>Member Attendance Summary
            </h3>
            
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-4 py-3 text-left text-sm font-semibold text-gray-600">#</th>
                            <th class="px-4 py-3 text-left text-sm font-semibold text-gray-600">Member</th>
                            <th class="px-4 py-3 text-left text-sm font-semibold text-gray-600">Times Present</th>
                            <th class="px-4 py-3 text-left text-sm font-semibold text-gray-600">Attendance Rate</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-100">
                        <?php 
                        $i = 0;
                        foreach ($ministryMembers as $member): 
                            $count = $memberAttendanceCounts[$member['member_id']]['count'] ?? 0;
                            $rate = $totalMeetings > 0 ? round(($count / $totalMeetings) * 100) : 0;
                        ?>
                        <tr class="hover:bg-gray-50">
                            <td class="px-4 py-3 text-sm"><?php echo ++$i; ?></td>
                            <td class="px-4 py-3 font-medium"><?php echo htmlspecialchars($member['first_name'] . ' ' . $member['last_name']); ?></td>
                            <td class="px-4 py-3 text-sm"><?php echo $count; ?> / <?php echo $totalMeetings; ?></td>
                            <td class="px-4 py-3">
                                <div class="flex items-center">
                                    <div class="w-24 bg-gray-200 rounded-full h-2 mr-2">
                                        <div class="h-2 rounded-full <?php echo $rate >= 75 ? 'bg-green-500' : ($rate >= 50 ? 'bg-yellow-500' : 'bg-red-500'); ?>" 
                                             style="width: <?php echo $rate; ?>%"></div>
                                    </div>
                                    <span class="text-sm font-medium"><?php echo $rate; ?>%</span>
                                </div>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-800 text-white py-6 mt-auto">
        <div class="container mx-auto px-4 text-center">
            <p class="text-gray-400 text-sm">
                © <?php echo date('Y'); ?> <?php echo htmlspecialchars($settings['site_title']); ?>
            </p>
        </div>
    </footer>
</body>
</html>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists