Path : /home/vishqocm/pcib.in/admin/ajax/
File Upload :
Current File : /home/vishqocm/pcib.in/admin/ajax/get_document_history.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' => 'Enrollment ID is required'
    ]);
    exit;
}

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

try {
    // First get the user_id from the enrollment
    $query = "SELECT user_id, course_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 student details
    $user_query = "SELECT first_name, last_name, email FROM users WHERE id = ?";
    $stmt = $conn->prepare($user_query);
    $stmt->bind_param("i", $user_id);
    $stmt->execute();
    $user_result = $stmt->get_result();
    $user = $user_result->fetch_assoc();
    
    // Get all documents for this user
    $docs_query = "SELECT d.id, d.document_type, d.file_path, d.status, d.admin_notes, 
                  d.upload_date, d.verified_date, 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
    $document_types = [
        'national_id' => 'National ID',
        'passport' => 'Passport',
        'birth_certificate' => 'Birth Certificate',
        'diploma' => 'Diploma',
        'transcript' => 'Academic Transcript',
        'cv' => 'Resume/CV',
        'recommendation' => 'Recommendation Letter',
        'profile_photo' => 'Profile Photo',
        'medical_certificate' => 'Medical Certificate',
        'other' => 'Other Document'
    ];
    
    // Define status badges
    $status_badges = [
        'pending' => '<span class="badge bg-warning text-dark">Pending</span>',
        'verified' => '<span class="badge bg-success">Verified</span>',
        'rejected' => '<span class="badge bg-danger">Rejected</span>'
    ];
    
    // Print student information
    echo '<div class="card mb-3">
            <div class="card-header bg-primary text-white">
                <h5 class="mb-0">Student Documents</h5>
            </div>
            <div class="card-body">
                <p><strong>Student:</strong> ' . htmlspecialchars($user['first_name'] . ' ' . $user['last_name']) . '</p>
                <p><strong>Email:</strong> ' . htmlspecialchars($user['email']) . '</p>
            </div>
        </div>';
    
    if ($docs_result->num_rows > 0) {
        echo '<div class="table-responsive">
                <table class="table table-striped table-bordered">
                    <thead class="table-dark">
                        <tr>
                            <th>Document Type</th>
                            <th>Status</th>
                            <th>Upload Date</th>
                            <th>Verified Date</th>
                            <th>Verified By</th>
                            <th>Admin Notes</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>';
        
        while ($doc = $docs_result->fetch_assoc()) {
            $doc_type = isset($document_types[$doc['document_type']]) 
                      ? $document_types[$doc['document_type']] 
                      : ucfirst(str_replace('_', ' ', $doc['document_type']));
            
            $status = isset($status_badges[$doc['status']]) 
                    ? $status_badges[$doc['status']] 
                    : '<span class="badge bg-secondary">' . ucfirst($doc['status']) . '</span>';
            
            $verified_by = !empty($doc['admin_first_name']) 
                         ? htmlspecialchars($doc['admin_first_name'] . ' ' . $doc['admin_last_name']) 
                         : 'N/A';
            
            echo '<tr>
                    <td>' . htmlspecialchars($doc_type) . '</td>
                    <td>' . $status . '</td>
                    <td>' . date('M d, Y H:i', strtotime($doc['upload_date'])) . '</td>
                    <td>' . (!empty($doc['verified_date']) ? date('M d, Y H:i', strtotime($doc['verified_date'])) : 'N/A') . '</td>
                    <td>' . $verified_by . '</td>
                    <td>' . (!empty($doc['admin_notes']) ? htmlspecialchars($doc['admin_notes']) : 'No notes') . '</td>
                    <td>
                        <a href="../../' . htmlspecialchars($doc['file_path']) . '" target="_blank" class="btn btn-sm btn-primary">
                            <i class="fas fa-eye"></i> View
                        </a>';
            
            if ($doc['status'] === 'pending') {
                echo ' <button type="button" class="btn btn-sm btn-success verify-doc" data-document-id="' . $doc['id'] . '" data-status="verified">
                            <i class="fas fa-check"></i> Verify
                        </button>
                        <button type="button" class="btn btn-sm btn-danger verify-doc" data-document-id="' . $doc['id'] . '" data-status="rejected">
                            <i class="fas fa-times"></i> Reject
                        </button>';
            }
            
            echo '</td>
                </tr>';
        }
        
        echo '</tbody>
            </table>
        </div>';
        
        // Add JavaScript for document verification
        echo '<script>
            $(document).ready(function() {
                $(".verify-doc").click(function() {
                    var documentId = $(this).data("document-id");
                    var status = $(this).data("status");
                    var adminNotes = "";
                    
                    if (status === "rejected") {
                        adminNotes = prompt("Please provide a reason for rejection:");
                        if (adminNotes === null) return; // User canceled
                    }
                    
                    $.ajax({
                        url: "admin/ajax/verify_document.php",
                        type: "POST",
                        data: {
                            document_id: documentId,
                            status: status,
                            admin_notes: adminNotes
                        },
                        beforeSend: function() {
                            $("#documentLoader").show();
                        },
                        success: function(response) {
                            try {
                                var data = JSON.parse(response);
                                if (data.success) {
                                    showAlert("success", "Document " + (status === "verified" ? "verified" : "rejected") + " successfully");
                                    // Refresh document list
                                    loadDocumentHistory(' . $enrollment_id . ');
                                } else {
                                    showAlert("danger", "Error: " + data.message);
                                }
                            } catch (e) {
                                showAlert("danger", "Error processing response");
                            }
                        },
                        error: function() {
                            showAlert("danger", "Server error while processing request");
                        },
                        complete: function() {
                            $("#documentLoader").hide();
                        }
                    });
                });
            });
        </script>';
    } else {
        echo '<div class="alert alert-info">No documents found for this student.</div>';
    }
    
} catch (Exception $e) {
    echo '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
}
?>