<?php
session_start();
include_once('../../config/database.php');
// Check if admin is logged in
if (!isset($_SESSION['user_id']) || ($_SESSION['role'] !== 'admin' && $_SESSION['role'] !== 'director')) {
echo json_encode([
'success' => false,
'message' => 'Unauthorized access'
]);
exit();
}
try {
// Get all documents with student information
$query = "
SELECT
d.id,
d.user_id,
d.document_type,
d.document_path,
d.upload_date,
d.status,
d.admin_notes,
u.first_name,
u.last_name,
u.email
FROM
student_documents d
JOIN
users u ON d.user_id = u.id
ORDER BY
d.upload_date DESC
";
$result = $conn->query($query);
if (!$result) {
throw new Exception("Database query error: " . $conn->error);
}
$documents = [];
while ($row = $result->fetch_assoc()) {
$documents[] = $row;
}
// Get all students who have uploaded documents
$students_query = "
SELECT DISTINCT
u.id,
CONCAT(u.first_name, ' ', u.last_name) AS name
FROM
users u
JOIN
student_documents d ON u.id = d.user_id
ORDER BY
u.first_name, u.last_name
";
$students_result = $conn->query($students_query);
if (!$students_result) {
throw new Exception("Failed to retrieve students: " . $conn->error);
}
$students = [];
while ($row = $students_result->fetch_assoc()) {
$students[] = $row;
}
// Format documents for response
$formattedDocuments = [];
$documentTypes = [
'id_proof' => 'ID Proof',
'address_proof' => 'Address Proof',
'educational_certificate' => 'Educational Certificate',
'photograph' => 'Passport Size Photo',
'other' => 'Other Document'
];
foreach ($documents as $document) {
// 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($documentTypes[$document['document_type']])
? $documentTypes[$document['document_type']]
: ucfirst(str_replace('_', ' ', $document['document_type']));
// Format student name
$studentName = $document['first_name'] . ' ' . $document['last_name'];
$formattedDocuments[] = [
'id' => $document['id'],
'user_id' => $document['user_id'],
'document_type' => $document['document_type'],
'document_type_display' => $documentType,
'document_path' => $document['document_path'],
'upload_date' => $uploadDate,
'status' => $document['status'],
'status_badge' => $statusBadge,
'admin_notes' => $document['admin_notes'],
'student_name' => $studentName,
'student_email' => $document['email']
];
}
echo json_encode([
'success' => true,
'data' => $formattedDocuments,
'students' => $students
]);
} catch (Exception $e) {
echo json_encode([
'success' => false,
'message' => 'Error: ' . $e->getMessage()
]);
}
?>