<?php
session_start();
require_once '../../config/db_config.php';
// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
echo json_encode(['success' => false, 'message' => 'You must be logged in to view documents']);
exit;
}
$userId = $_SESSION['user_id'];
try {
// Create connection
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Get all documents for the user
$stmt = $conn->prepare("
SELECT id, document_type, file_path, upload_date, status, admin_notes
FROM student_documents
WHERE user_id = ?
ORDER BY upload_date DESC
");
$stmt->execute([$userId]);
$documents = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Get the required document types
$requiredDocuments = [
'id_proof' => 'ID Proof',
'address_proof' => 'Address Proof',
'qualification_certificate' => 'Qualification Certificate',
'passport_photo' => 'Passport Size Photo'
];
// Format documents for response
$formattedDocuments = [];
$uploadedTypes = [];
foreach ($documents as $document) {
// Only include the most recent document of each type that's not rejected
if (!in_array($document['document_type'], $uploadedTypes)) {
$uploadedTypes[] = $document['document_type'];
// Format the document status
$statusBadge = '';
switch ($document['status']) {
case 'pending':
$statusBadge = '<span class="badge bg-warning text-dark">Pending</span>';
break;
case 'verified':
$statusBadge = '<span class="badge bg-success">Verified</span>';
break;
case 'rejected':
$statusBadge = '<span class="badge bg-danger">Rejected</span>';
break;
default:
$statusBadge = '<span class="badge bg-secondary">Unknown</span>';
}
// Format the upload date
$uploadDate = date('M d, Y', strtotime($document['upload_date']));
// Format the document type
$documentType = isset($requiredDocuments[$document['document_type']])
? $requiredDocuments[$document['document_type']]
: ucfirst(str_replace('_', ' ', $document['document_type']));
$formattedDocuments[] = [
'id' => $document['id'],
'document_type' => $document['document_type'],
'document_type_display' => $documentType,
'file_path' => $document['file_path'],
'upload_date' => $uploadDate,
'status' => $document['status'],
'status_badge' => $statusBadge,
'admin_notes' => $document['admin_notes']
];
}
}
// Check which required documents are missing
$missingDocuments = [];
foreach ($requiredDocuments as $type => $displayName) {
if (!in_array($type, $uploadedTypes)) {
$missingDocuments[] = [
'document_type' => $type,
'document_type_display' => $displayName
];
}
}
// Get application status
$stmt = $conn->prepare("
SELECT status
FROM enrollment_applications
WHERE user_id = ?
ORDER BY application_date DESC
LIMIT 1
");
$stmt->execute([$userId]);
$application = $stmt->fetch(PDO::FETCH_ASSOC);
$applicationStatus = $application ? $application['status'] : null;
echo json_encode([
'success' => true,
'documents' => $formattedDocuments,
'missing_documents' => $missingDocuments,
'application_status' => $applicationStatus
]);
} catch (PDOException $e) {
echo json_encode(['success' => false, 'message' => 'Database error: ' . $e->getMessage()]);
} catch (Exception $e) {
echo json_encode(['success' => false, 'message' => 'Error: ' . $e->getMessage()]);
}
?>