<?php
$pageTitle = "View Certificate";
include_once('includes/header.php');
// Check if user is logged in
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'student') {
header("Location: ../login.php");
exit();
}
// Get student ID
$userId = $_SESSION['user_id'];
// Get certificate ID from URL
$cert_id = isset($_GET['cert_id']) ? intval($_GET['cert_id']) : 0;
if ($cert_id <= 0) {
$_SESSION['error_message'] = "Invalid certificate ID";
header("Location: certificates.php");
exit();
}
// Get certificate details
$cert_query = "
SELECT uc.*, c.title as course_title, c.description as course_description, c.duration,
CONCAT(u.first_name, ' ', u.last_name) as student_name, u.email, u.enrollment_number,
e.completion_date, se.percentage as exam_score,
es.title as exam_title, es.passing_percentage
FROM unified_certificates uc
JOIN courses c ON uc.course_id = c.id
JOIN users u ON uc.user_id = u.id
LEFT JOIN enrollments e ON uc.enrollment_id = e.id
LEFT JOIN student_exams se ON uc.student_exam_id = se.id
LEFT JOIN exam_schedules es ON se.exam_id = es.id
WHERE uc.id = ? AND uc.user_id = ?
AND (uc.payment_status = 'completed' OR uc.payment_status = 'paid')
";
$stmt = $conn->prepare($cert_query);
$stmt->bind_param("ii", $cert_id, $userId);
$stmt->execute();
$cert_result = $stmt->get_result();
if ($cert_result->num_rows === 0) {
$_SESSION['error_message'] = "Certificate not found or payment not completed";
header("Location: certificates.php");
exit();
}
$certificate = $cert_result->fetch_assoc();
// Get institute information
$institute_query = "SELECT setting_key, setting_value FROM site_settings WHERE setting_key IN ('institute_name', 'institute_logo', 'institute_address', 'director_name', 'director_designation', 'contact_email', 'contact_phone')";
$institute_result = $conn->query($institute_query);
$institute = [];
if ($institute_result && $institute_result->num_rows > 0) {
while ($row = $institute_result->fetch_assoc()) {
$institute[$row['setting_key']] = $row['setting_value'];
}
}
// Set default values if not found in settings
$institute_name = $institute['institute_name'] ?? 'Popular Computer Institute';
$institute_logo = $institute['institute_logo'] ?? '../assets/img/logo.png';
$institute_address = $institute['institute_address'] ?? 'Bhimpura No.1, Ballia, UP';
$institute_email = $institute['contact_email'] ?? '';
$institute_phone = $institute['contact_phone'] ?? '';
$director_name = $institute['director_name'] ?? 'Institute Director';
$director_designation = $institute['director_designation'] ?? 'Director';
// Get grade based on exam score if available
$grade = '';
$letter_grade = '';
if (!empty($certificate['exam_score'])) {
$score = floatval($certificate['exam_score']);
if ($score >= 90) {
$grade = 'Excellent';
$letter_grade = 'A+';
} elseif ($score >= 80) {
$grade = 'Very Good';
$letter_grade = 'A';
} elseif ($score >= 70) {
$grade = 'Good';
$letter_grade = 'B';
} elseif ($score >= 60) {
$grade = 'Satisfactory';
$letter_grade = 'C';
} else {
$grade = 'Pass';
$letter_grade = 'D';
}
}
// Format date for display
$issue_date = !empty($certificate['issue_date']) ? date('d F Y', strtotime($certificate['issue_date'])) : date('d F Y');
$completion_date = !empty($certificate['completion_date']) ? date('d F Y', strtotime($certificate['completion_date'])) : '';
// Certificate serial number with leading zeros
$serial_number = str_pad($certificate['id'], 6, '0', STR_PAD_LEFT);
// Generate QR code URL for certificate verification
$verification_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]/verify-certificate.php?cert=" . urlencode($certificate['certificate_number']);
$qr_code_url = "https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=" . urlencode($verification_url) . "&choe=UTF-8";
?>
<!-- Include certificate CSS -->
<link rel="stylesheet" href="assets/css/certificate.css">
<div class="container-fluid py-4">
<div class="text-center mb-4">
<a href="certificates.php" class="btn btn-outline-primary">
<i class="fas fa-arrow-left me-2"></i> Back to Certificates
</a>
<button id="printCertificate" class="btn btn-success ms-2">
<i class="fas fa-print me-2"></i> Print Certificate
</button>
<a href="<?php echo $verification_url; ?>" class="btn btn-outline-secondary ms-2" target="_blank">
<i class="fas fa-link me-2"></i> Verification Link
</a>
<a href="#" class="btn btn-info ms-2" id="downloadPDF">
<i class="fas fa-file-pdf me-2"></i> Download PDF
</a>
</div>
<!-- Certificate Container -->
<div id="certificateContainer">
<!-- First Page (Cover) -->
<div class="certificate-page page-1">
<div class="certificate-border">
<div class="certificate-content">
<!-- Institute Header -->
<div class="text-center header-section">
<img src="<?php echo '../' . $institute_logo; ?>" alt="Institute Logo" class="institute-logo">
<h1 class="institute-name"><?php echo htmlspecialchars($institute_name); ?></h1>
<p class="institute-address"><?php echo htmlspecialchars($institute_address); ?></p>
</div>
<!-- Certificate Title -->
<div class="text-center title-section">
<div class="ornament-line"></div>
<h2 class="certificate-title">Certificate of Completion</h2>
<div class="ornament-line"></div>
</div>
<!-- Student Information -->
<div class="text-center student-section">
<p class="certificate-text">This is to certify that</p>
<h3 class="student-name"><?php echo htmlspecialchars($certificate['student_name']); ?></h3>
<p class="enrollment-number">Enrollment Number: <?php echo htmlspecialchars($certificate['enrollment_number'] ?? 'N/A'); ?></p>
<p class="certificate-text">has successfully completed the course</p>
<h3 class="course-title"><?php echo htmlspecialchars($certificate['course_title']); ?></h3>
<?php if (!empty($certificate['duration'])): ?>
<p class="certificate-text">
with a duration of <strong><?php echo htmlspecialchars($certificate['duration']); ?></strong>
</p>
<?php endif; ?>
<?php if (!empty($completion_date)): ?>
<p class="certificate-text">
Completed on <strong><?php echo $completion_date; ?></strong>
</p>
<?php endif; ?>
</div>
<!-- Signature Section -->
<div class="signature-section">
<div class="signature director-signature">
<img src="../assets/img/signature.png" alt="Director Signature" class="signature-img">
<div class="signature-line"></div>
<p class="signature-name"><?php echo htmlspecialchars($director_name); ?></p>
<p class="signature-title"><?php echo htmlspecialchars($director_designation); ?></p>
</div>
<!-- Certificate Seal -->
<div class="certificate-seal"></div>
</div>
<!-- Certificate Footer -->
<div class="certificate-footer">
<div class="certificate-verification">
<p>Certificate Number: <strong><?php echo htmlspecialchars($certificate['certificate_number']); ?></strong></p>
<p>Verification Code: <strong><?php echo htmlspecialchars($certificate['verification_code']); ?></strong></p>
<p>Issue Date: <strong><?php echo $issue_date; ?></strong></p>
</div>
<div class="qr-code">
<img src="<?php echo $qr_code_url; ?>" alt="Verification QR Code">
<p class="text-center">Scan to verify</p>
</div>
</div>
</div>
</div>
</div>
<!-- Second Page (Details) -->
<div class="certificate-page page-2">
<div class="certificate-border">
<div class="certificate-content">
<!-- Institute Header (Smaller) -->
<div class="text-center header-section-small">
<img src="<?php echo '../' . $institute_logo; ?>" alt="Institute Logo" class="institute-logo-small">
<h2 class="institute-name-small"><?php echo htmlspecialchars($institute_name); ?></h2>
</div>
<h3 class="page-title">Certificate Supplement</h3>
<!-- Certificate ID Section -->
<div class="certificate-id-section">
<div class="certificate-number-box">
<h4>Certificate ID: <?php echo htmlspecialchars($certificate['certificate_number']); ?></h4>
<p>This document is an official supplement to the Certificate of Completion issued to:</p>
<h4 class="student-name-small"><?php echo htmlspecialchars($certificate['student_name']); ?></h4>
</div>
</div>
<!-- Course Details -->
<div class="course-details-section">
<h4>Course Details</h4>
<table class="details-table">
<tr>
<th>Course Title:</th>
<td><?php echo htmlspecialchars($certificate['course_title']); ?></td>
</tr>
<tr>
<th>Duration:</th>
<td><?php echo htmlspecialchars($certificate['duration'] ?? 'N/A'); ?></td>
</tr>
<tr>
<th>Completion Date:</th>
<td><?php echo $completion_date ?: 'N/A'; ?></td>
</tr>
<tr>
<th>Course Description:</th>
<td><?php echo htmlspecialchars($certificate['course_description'] ?? 'N/A'); ?></td>
</tr>
</table>
</div>
<!-- Examination Results -->
<?php if (!empty($certificate['exam_score'])): ?>
<div class="examination-section">
<h4>Examination Results</h4>
<table class="details-table">
<tr>
<th>Exam Title:</th>
<td><?php echo htmlspecialchars($certificate['exam_title'] ?? 'Final Assessment'); ?></td>
</tr>
<tr>
<th>Score:</th>
<td><?php echo number_format($certificate['exam_score'], 1); ?>%</td>
</tr>
<tr>
<th>Grade:</th>
<td><strong><?php echo $letter_grade; ?></strong> (<?php echo $grade; ?>)</td>
</tr>
<tr>
<th>Passing Score:</th>
<td><?php echo number_format($certificate['passing_percentage'] ?? 60, 1); ?>%</td>
</tr>
</table>
</div>
<?php endif; ?>
<!-- Verification Box -->
<div class="verification-box">
<div class="verification-info">
<h4>Certificate Verification</h4>
<p>This certificate can be verified online at our website using the following information:</p>
<p><strong>Certificate Number:</strong> <?php echo htmlspecialchars($certificate['certificate_number']); ?></p>
<p><strong>Verification Code:</strong> <?php echo htmlspecialchars($certificate['verification_code']); ?></p>
<p><strong>URL:</strong> <a href="<?php echo $verification_url; ?>" target="_blank"><?php echo $verification_url; ?></a></p>
</div>
<div class="verification-qr">
<img src="<?php echo $qr_code_url; ?>" alt="Verification QR Code">
<p>Scan to verify</p>
</div>
</div>
<!-- Footer Note -->
<div class="footer-note">
<p>This certificate supplement forms an integral part of the Certificate of Completion and is not valid without it.</p>
<p>Issued on <?php echo $issue_date; ?> by <?php echo htmlspecialchars($institute_name); ?></p>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
// Handle print button
document.getElementById('printCertificate').addEventListener('click', function() {
window.print();
});
// Handle download PDF button (requires jsPDF library)
document.getElementById('downloadPDF').addEventListener('click', function() {
alert('Please use the print function and save as PDF');
window.print();
});
});
</script>
<?php include_once 'includes/footer.php'; ?>