Path : /home/vishqocm/pcib.in/admin/ajax/
File Upload :
Current File : //home/vishqocm/pcib.in/admin/ajax/get_enrollment_documents.php

<?php
// Start session
session_start();

// Check if user has admin privileges
if (!isset($_SESSION['role']) || ($_SESSION['role'] !== 'admin' && $_SESSION['role'] !== 'director')) {
    echo json_encode(['success' => false, 'message' => 'Unauthorized access']);
    exit;
}

// Include database configuration
require_once '../../admin/database/db_config.php';

// Check if enrollment ID is provided
if (!isset($_GET['enrollment_id']) || empty($_GET['enrollment_id'])) {
    echo json_encode(['success' => false, 'message' => 'Invalid enrollment ID']);
    exit;
}

$enrollment_id = intval($_GET['enrollment_id']);

try {
    // First get the user_id from the enrollment
    $query = "SELECT user_id FROM enrollments WHERE id = ?";
    $stmt = $conn->prepare($query);
    $stmt->bind_param("i", $enrollment_id);
    $stmt->execute();
    $result = $stmt->get_result();
    
    if ($result->num_rows === 0) {
        throw new Exception('Enrollment not found');
    }
    
    $enrollment = $result->fetch_assoc();
    $user_id = $enrollment['user_id'];
    
    // Get all documents for this user
    $docs_query = "SELECT d.*, 
                  a.first_name as admin_first_name, a.last_name as admin_last_name
                  FROM student_documents d
                  LEFT JOIN users a ON d.verified_by = a.id
                  WHERE d.user_id = ?
                  ORDER BY d.upload_date DESC";
    $stmt = $conn->prepare($docs_query);
    $stmt->bind_param("i", $user_id);
    $stmt->execute();
    $docs_result = $stmt->get_result();
    
    // Define document types
    $doc_types = [
        'id_proof' => 'ID Proof',
        'educational_certificate' => 'Educational Certificate',
        'photograph' => 'Photograph',
        'address_proof' => 'Address Proof',
        'other' => 'Other Document'
    ];
    
    // Define status badges
    $status_badges = [
        'pending' => 'warning',
        'verified' => 'success',
        'rejected' => 'danger'
    ];
    
    $documents = [];
    
    while ($doc = $docs_result->fetch_assoc()) {
        // Format the document data
        $doc['document_type_label'] = isset($doc_types[$doc['document_type']]) ? 
                                      $doc_types[$doc['document_type']] : 
                                      ucfirst(str_replace('_', ' ', $doc['document_type']));
        
        $doc['status_badge'] = isset($status_badges[$doc['status']]) ? 
                              $status_badges[$doc['status']] : 
                              'secondary';
        
        $doc['upload_date_formatted'] = date('M d, Y g:i A', strtotime($doc['upload_date']));
        
        $doc['verified_by_name'] = '';
        if ($doc['status'] !== 'pending' && !empty($doc['admin_first_name'])) {
            $doc['verified_by_name'] = $doc['admin_first_name'] . ' ' . $doc['admin_last_name'];
        }
        
        $documents[] = $doc;
    }
    
    // Get statistics
    $stats_query = "SELECT 
                    COUNT(*) as total_documents,
                    SUM(CASE WHEN status = 'verified' THEN 1 ELSE 0 END) as verified_documents,
                    SUM(CASE WHEN status = 'rejected' THEN 1 ELSE 0 END) as rejected_documents,
                    SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) as pending_documents
                    FROM student_documents
                    WHERE user_id = ?";
    $stmt = $conn->prepare($stats_query);
    $stmt->bind_param("i", $user_id);
    $stmt->execute();
    $stats = $stmt->get_result()->fetch_assoc();
    
    echo json_encode([
        'success' => true,
        'documents' => $documents,
        'stats' => $stats,
        'document_types' => $doc_types
    ]);
    
} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'message' => $e->getMessage()
    ]);
}
?>