Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/attendance/admin/api/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/attendance/admin/api/get_reports_data.php

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

// Check if user is logged in
if (!isLoggedIn()) {
    http_response_code(401);
    echo json_encode(['success' => false, 'message' => 'Unauthorized']);
    exit;
}

// Set content type
header('Content-Type: application/json');

$type = $_GET['type'] ?? 'attendance';
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$limit = isset($_GET['limit']) ? (int)$_GET['limit'] : 20;
$offset = ($page - 1) * $limit;

$db = new Database();
$conn = $db->getConnection();

// Build query with location restriction for admin users
$location_filter = '';
$params = [];

if (hasRole('admin') && isset($_SESSION['location_id']) && $_SESSION['location_id']) {
    $location_filter = " AND p.location_id = ?";
    $params[] = $_SESSION['location_id'];
}

try {
    if ($type === 'attendance') {
        // Get detailed attendance records
        $query = "SELECT ar.*, p.name as program_name, l.name as location_name 
                  FROM attendance_records ar 
                  JOIN programs p ON ar.program_id = p.id 
                  LEFT JOIN locations l ON p.location_id = l.id 
                  WHERE 1=1 $location_filter
                  ORDER BY ar.submitted_at DESC 
                  LIMIT $limit OFFSET $offset";
        
        $stmt = $conn->prepare($query);
        $stmt->execute($params);
        $records = $stmt->fetchAll();
        
        // Get total count
        $count_query = "SELECT COUNT(*) as total 
                        FROM attendance_records ar 
                        JOIN programs p ON ar.program_id = p.id 
                        WHERE 1=1 $location_filter";
        $count_stmt = $conn->prepare($count_query);
        $count_stmt->execute($params);
        $total = $count_stmt->fetch()['total'];
        
        // Format records
        $formatted_records = [];
        foreach ($records as $record) {
            $formatted_records[] = [
                'id' => (int)$record['id'],
                'full_name' => $record['full_name'],
                'email' => $record['email'],
                'telephone' => $record['telephone'],
                'district_name' => $record['district_name'],
                'assembly_name' => $record['assembly_name'],
                'program_name' => $record['program_name'],
                'location_name' => $record['location_name'] ?? '',
                'submitted_at' => $record['submitted_at'],
                'formatted_date' => date('M j, Y g:i A', strtotime($record['submitted_at']))
            ];
        }
        
    } elseif ($type === 'programs') {
        // Get program statistics
        $query = "SELECT p.name, p.description, p.start_date, p.end_date, 
                         l.name as location_name, COUNT(ar.id) as attendance_count
                  FROM programs p 
                  LEFT JOIN locations l ON p.location_id = l.id 
                  LEFT JOIN attendance_records ar ON p.id = ar.program_id
                  WHERE p.is_active = 1 $location_filter
                  GROUP BY p.id
                  ORDER BY attendance_count DESC
                  LIMIT $limit OFFSET $offset";
        
        $stmt = $conn->prepare($query);
        $stmt->execute($params);
        $records = $stmt->fetchAll();
        
        // Get total count
        $count_query = "SELECT COUNT(*) as total 
                        FROM programs p 
                        WHERE p.is_active = 1 $location_filter";
        $count_stmt = $conn->prepare($count_query);
        $count_stmt->execute($params);
        $total = $count_stmt->fetch()['total'];
        
        // Format records
        $formatted_records = [];
        foreach ($records as $record) {
            $formatted_records[] = [
                'name' => $record['name'],
                'description' => $record['description'],
                'location_name' => $record['location_name'] ?? 'All Locations',
                'attendance_count' => (int)$record['attendance_count'],
                'start_date' => $record['start_date'],
                'end_date' => $record['end_date'],
                'formatted_start' => $record['start_date'] ? date('M j, Y', strtotime($record['start_date'])) : '',
                'formatted_end' => $record['end_date'] ? date('M j, Y', strtotime($record['end_date'])) : ''
            ];
        }
        
    } else {
        throw new Exception('Invalid report type');
    }
    
    echo json_encode([
        'success' => true,
        'records' => $formatted_records,
        'pagination' => [
            'current_page' => $page,
            'total_records' => (int)$total,
            'has_more' => ($offset + $limit) < $total,
            'per_page' => $limit
        ]
    ]);
    
} catch (Exception $e) {
    http_response_code(500);
    echo json_encode([
        'success' => false,
        'message' => 'Database error: ' . $e->getMessage()
    ]);
}
?>

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