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_attendance.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');

$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$limit = isset($_GET['limit']) ? (int)$_GET['limit'] : 10;
$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'];
}

$query = "SELECT ar.*, p.name as program_name, l.name as location_name,
                 ld.name as district_name_from_id, la.name as assembly_name_from_id
          FROM attendance_records ar 
          JOIN programs p ON ar.program_id = p.id 
          LEFT JOIN locations l ON p.location_id = l.id 
          LEFT JOIN locations ld ON ar.district_id = ld.id AND ld.type = 'district'
          LEFT JOIN locations la ON ar.assembly_id = la.id AND la.type = 'assembly'
          WHERE 1=1 $location_filter
          ORDER BY ar.submitted_at DESC 
          LIMIT $limit OFFSET $offset";

try {
    $stmt = $conn->prepare($query);
    $stmt->execute($params);
    $records = $stmt->fetchAll();
    
    // Get total count for pagination info
    $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 the records for frontend
    $formatted_records = [];
    foreach ($records as $record) {
        // Use district/assembly names from IDs if available, otherwise fall back to text fields
        $district_name = $record['district_name_from_id'] ?: $record['district_name'];
        $assembly_name = $record['assembly_name_from_id'] ?: $record['assembly_name'];
        
        $formatted_records[] = [
            'id' => (int)$record['id'],
            'full_name' => $record['full_name'],
            'program_name' => $record['program_name'],
            'location_name' => $record['location_name'] ?? '',
            'district_name' => $district_name ?? '',
            'assembly_name' => $assembly_name ?? '',
            'submitted_at' => $record['submitted_at'],
            'formatted_date' => date('M j, g:i A', strtotime($record['submitted_at']))
        ];
    }
    
    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 occurred'
    ]);
}
?>

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