<?php
session_start();
include_once('../../config/db_config.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();
}
// Validate request
if ($_SERVER['REQUEST_METHOD'] !== 'GET' || !isset($_GET['document_id']) || empty($_GET['document_id'])) {
echo json_encode([
'success' => false,
'message' => 'Invalid request parameters'
]);
exit();
}
$documentId = (int)$_GET['document_id'];
try {
// Get document details 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
WHERE
d.id = ?
";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $documentId);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 0) {
echo json_encode([
'success' => false,
'message' => 'Document not found'
]);
exit();
}
$document = $result->fetch_assoc();
// Format document for response
$documentTypes = [
'id_proof' => 'ID Proof',
'address_proof' => 'Address Proof',
'educational_certificate' => 'Educational Certificate',
'photograph' => 'Passport Size Photo',
'other' => 'Other 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'];
// Fix path if needed
$filePath = $document['document_path'];
if (strpos($filePath, '../') === 0) {
$filePath = substr($filePath, 3);
}
$formattedDocument = [
'id' => $document['id'],
'user_id' => $document['user_id'],
'document_type' => $document['document_type'],
'document_type_display' => $documentType,
'file_path' => $filePath,
'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,
'document' => $formattedDocument
]);
} catch (Exception $e) {
echo json_encode([
'success' => false,
'message' => 'Error: ' . $e->getMessage()
]);
}
?>