Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/portal/modules/event-reports/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/portal/modules/event-reports/index.php

<?php
require_once '../../config/config.php';
checkLogin();

if (!isSuperuser() && !in_array(getAccessLevel(), ['area', 'district', 'assembly'])) {
    redirect('../../dashboard.php');
}

$pageTitle = "Event Reports - " . APP_NAME;
$db = Database::getInstance()->getConnection();

require_once '../../classes/EventManager.php';
$eventManager = new EventManager();

// Get filter parameters
$selectedEventId = $_GET['event_id'] ?? null;
$dateFrom = $_GET['date_from'] ?? date('Y-m-01');
$dateTo = $_GET['date_to'] ?? date('Y-m-t');

// Get events for dropdown
$events = $eventManager->getEvents(['active' => true]);

// Get statistics
$stats = [
    'total_events' => 0,
    'total_registrations' => 0,
    'total_attendance' => 0,
    'attendance_rate' => 0
];

$sql = "SELECT 
    COUNT(DISTINCT e.id) as total_events,
    COUNT(DISTINCT er.id) as total_registrations,
    COUNT(DISTINCT ea.id) as total_attendance
FROM events e
LEFT JOIN event_registrations er ON e.id = er.event_id
LEFT JOIN event_attendance ea ON e.id = ea.event_id
WHERE e.is_active = 1";

$params = [];

if ($selectedEventId) {
    $sql .= " AND e.id = :event_id";
    $params['event_id'] = $selectedEventId;
}

if ($dateFrom && $dateTo) {
    $sql .= " AND e.start_date BETWEEN :date_from AND :date_to";
    $params['date_from'] = $dateFrom . ' 00:00:00';
    $params['date_to'] = $dateTo . ' 23:59:59';
}

$stmt = $db->prepare($sql);
$stmt->execute($params);
$queryResult = $stmt->fetch();

// Merge with default stats to ensure all keys exist
$stats = array_merge($stats, $queryResult ?: []);

// Calculate attendance rate
if ($stats['total_registrations'] > 0) {
    $stats['attendance_rate'] = round(($stats['total_attendance'] / $stats['total_registrations']) * 100, 1);
} else {
    $stats['attendance_rate'] = 0;
}

// Get detailed event data
$eventReports = [];
$sql = "SELECT 
    e.id, e.name, e.start_date, e.end_date, e.event_type,
    COUNT(DISTINCT er.id) as registrations,
    COUNT(DISTINCT ea.id) as attendance,
    COUNT(DISTINCT CASE WHEN er.registration_type = 'member' THEN er.id END) as member_registrations,
    COUNT(DISTINCT CASE WHEN er.registration_type = 'guest' THEN er.id END) as guest_registrations
FROM events e
LEFT JOIN event_registrations er ON e.id = er.event_id
LEFT JOIN event_attendance ea ON e.id = ea.event_id
WHERE e.is_active = 1";

if ($selectedEventId) {
    $sql .= " AND e.id = :event_id";
}

if ($dateFrom && $dateTo) {
    $sql .= " AND e.start_date BETWEEN :date_from AND :date_to";
}

$sql .= " GROUP BY e.id ORDER BY e.start_date DESC";

$stmt = $db->prepare($sql);
$stmt->execute($params);
$eventReports = $stmt->fetchAll();

include '../../includes/header.php';
include '../../includes/sidebar.php';
?>

<main class="flex-1 md:ml-64 mt-16">
<div class="container mx-auto px-4 py-8">
    <div class="max-w-7xl mx-auto">
        <div class="mb-6">
            <h1 class="text-3xl font-bold text-gray-800">
                <i class="fas fa-chart-bar mr-2 text-blue-500"></i>Event Reports
            </h1>
            <p class="text-gray-600 mt-2">Analytics and insights for event management</p>
        </div>
        
        <!-- Filters -->
        <div class="bg-white rounded-xl shadow-lg p-6 mb-8">
            <h3 class="text-lg font-semibold mb-4">Report Filters</h3>
            <form method="GET" class="grid grid-cols-1 md:grid-cols-4 gap-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">Event</label>
                    <select name="event_id" class="w-full px-4 py-2 border border-gray-300 rounded-lg">
                        <option value="">All Events</option>
                        <?php foreach ($events as $event): ?>
                            <option value="<?php echo $event['id']; ?>" <?php echo $selectedEventId == $event['id'] ? 'selected' : ''; ?>>
                                <?php echo htmlspecialchars($event['name']); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                
                <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="w-full px-4 py-2 border border-gray-300 rounded-lg">
                </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="w-full px-4 py-2 border border-gray-300 rounded-lg">
                </div>
                
                <div class="flex items-end">
                    <button type="submit" class="w-full bg-blue-500 text-white px-4 py-2 rounded-lg hover:bg-blue-600">
                        <i class="fas fa-search mr-2"></i>Generate Report
                    </button>
                </div>
            </form>
        </div>
        
        <!-- Statistics Cards -->
        <div class="grid grid-cols-1 md:grid-cols-4 gap-6 mb-8">
            <div class="bg-white rounded-xl shadow-lg p-6">
                <div class="flex items-center">
                    <div class="p-3 rounded-full bg-blue-100 text-blue-600">
                        <i class="fas fa-calendar text-xl"></i>
                    </div>
                    <div class="ml-4">
                        <p class="text-sm font-medium text-gray-600">Total Events</p>
                        <p class="text-2xl font-bold text-gray-900"><?php echo $stats['total_events']; ?></p>
                    </div>
                </div>
            </div>
            
            <div class="bg-white rounded-xl shadow-lg p-6">
                <div class="flex items-center">
                    <div class="p-3 rounded-full bg-green-100 text-green-600">
                        <i class="fas fa-user-plus text-xl"></i>
                    </div>
                    <div class="ml-4">
                        <p class="text-sm font-medium text-gray-600">Registrations</p>
                        <p class="text-2xl font-bold text-gray-900"><?php echo $stats['total_registrations']; ?></p>
                    </div>
                </div>
            </div>
            
            <div class="bg-white rounded-xl shadow-lg p-6">
                <div class="flex items-center">
                    <div class="p-3 rounded-full bg-purple-100 text-purple-600">
                        <i class="fas fa-users text-xl"></i>
                    </div>
                    <div class="ml-4">
                        <p class="text-sm font-medium text-gray-600">Attendance</p>
                        <p class="text-2xl font-bold text-gray-900"><?php echo $stats['total_attendance']; ?></p>
                    </div>
                </div>
            </div>
            
            <div class="bg-white rounded-xl shadow-lg p-6">
                <div class="flex items-center">
                    <div class="p-3 rounded-full bg-yellow-100 text-yellow-600">
                        <i class="fas fa-percentage text-xl"></i>
                    </div>
                    <div class="ml-4">
                        <p class="text-sm font-medium text-gray-600">Attendance Rate</p>
                        <p class="text-2xl font-bold text-gray-900"><?php echo $stats['attendance_rate']; ?>%</p>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Event Details Table -->
        <div class="bg-white rounded-xl shadow-lg p-6">
            <div class="flex justify-between items-center mb-6">
                <h3 class="text-lg font-semibold">Event Details</h3>
                <div class="flex space-x-2">
                    <button onclick="exportReport('csv')" class="bg-green-500 text-white px-4 py-2 rounded-lg hover:bg-green-600">
                        <i class="fas fa-file-csv mr-2"></i>Export CSV
                    </button>
                    <button onclick="exportReport('pdf')" class="bg-red-500 text-white px-4 py-2 rounded-lg hover:bg-red-600">
                        <i class="fas fa-file-pdf mr-2"></i>Export PDF
                    </button>
                </div>
            </div>
            
            <div class="overflow-x-auto">
                <table class="min-w-full border border-gray-200 rounded-lg">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Event</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Date</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Type</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Registrations</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Attendance</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Rate</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Members</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Guests</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        <?php if (empty($eventReports)): ?>
                            <tr>
                                <td colspan="8" class="px-6 py-8 text-center text-gray-500">
                                    <i class="fas fa-chart-bar text-3xl mb-2"></i>
                                    <p>No events found for the selected criteria</p>
                                </td>
                            </tr>
                        <?php else: ?>
                            <?php foreach ($eventReports as $report): ?>
                                <tr>
                                    <td class="px-6 py-4">
                                        <div class="text-sm font-medium text-gray-900">
                                            <?php echo htmlspecialchars($report['name']); ?>
                                        </div>
                                    </td>
                                    <td class="px-6 py-4 text-sm text-gray-900">
                                        <?php echo date('M j, Y', strtotime($report['start_date'])); ?>
                                    </td>
                                    <td class="px-6 py-4">
                                        <span class="px-2 py-1 text-xs font-medium rounded-full bg-blue-100 text-blue-800">
                                            <?php echo ucfirst($report['event_type']); ?>
                                        </span>
                                    </td>
                                    <td class="px-6 py-4 text-sm text-gray-900">
                                        <?php echo $report['registrations']; ?>
                                    </td>
                                    <td class="px-6 py-4 text-sm text-gray-900">
                                        <?php echo $report['attendance']; ?>
                                    </td>
                                    <td class="px-6 py-4 text-sm text-gray-900">
                                        <?php 
                                        $rate = $report['registrations'] > 0 ? round(($report['attendance'] / $report['registrations']) * 100, 1) : 0;
                                        $rateColor = $rate >= 80 ? 'text-green-600' : ($rate >= 60 ? 'text-yellow-600' : 'text-red-600');
                                        ?>
                                        <span class="<?php echo $rateColor; ?> font-medium"><?php echo $rate; ?>%</span>
                                    </td>
                                    <td class="px-6 py-4 text-sm text-gray-900">
                                        <?php echo $report['member_registrations']; ?>
                                    </td>
                                    <td class="px-6 py-4 text-sm text-gray-900">
                                        <?php echo $report['guest_registrations']; ?>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
</main>

<script>
function exportReport(format) {
    const params = new URLSearchParams(window.location.search);
    params.set('export', format);
    window.open('export.php?' + params.toString(), '_blank');
}
</script>

<?php include '../../includes/footer.php'; ?>

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