<?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,
CONCAT(i.first_name, ' ', i.last_name) as instructor_name,
i.designation as instructor_designation,
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 users i ON c.instructor_id = i.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', 'site_name', 'site_logo', 'site_address', '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'] ?? $institute['site_name'] ?? 'Popular Computer Institute';
$institute_logo = $institute['institute_logo'] ?? $institute['site_logo'] ?? '../assets/img/logo.png';
$institute_address = $institute['institute_address'] ?? $institute['site_address'] ?? 'Bhimpura No.1, Ballia, UP';
$institute_email = $institute['contact_email'] ?? '';
$institute_phone = $institute['contact_phone'] ?? '';
// 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";
?>
<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>
<div class="card mb-4 shadow">
<div class="card-body p-4">
<div id="certificateContainer" class="position-relative mx-auto" style="max-width: 1050px; background-color: #fff; box-shadow: 0 0 25px rgba(0,0,0,0.1);">
<!-- Certificate Border -->
<div style="border: 2px solid #d4af37; padding: 15px;">
<div style="border: 1px solid #d4af37; padding: 25px; background-image: url('https://i.imgur.com/tZBttT6.png'); background-size: cover; background-repeat: no-repeat;">
<!-- Top Ornamental Divider -->
<div class="text-center mb-4">
<img src="https://i.imgur.com/gDGlQhO.png" style="max-width: 80%; height: auto;">
</div>
<!-- Certificate Header -->
<div class="text-center mb-3">
<div class="d-flex justify-content-center align-items-center">
<div class="mx-2">
<img src="<?php echo '../' . $institute_logo; ?>" alt="<?php echo htmlspecialchars($institute_name); ?>" class="img-fluid" style="max-height: 100px;">
</div>
<div class="mx-2">
<h1 class="mb-0 certificate-title" style="font-family: 'Cormorant Garamond', serif; color: #14397d; font-size: 36px; text-transform: uppercase; letter-spacing: 2px;"><?php echo htmlspecialchars($institute_name); ?></h1>
<p class="mb-0" style="font-family: 'Cormorant Garamond', serif; font-size: 18px; color: #444; font-style: italic;"><?php echo htmlspecialchars($institute_address); ?></p>
</div>
</div>
</div>
<!-- Certificate Title -->
<div class="text-center my-4">
<h2 style="font-family: 'Playfair Display', serif; color: #8B0000; font-size: 42px; text-transform: uppercase; letter-spacing: 4px;">Certificate of Completion</h2>
<div style="width: 200px; height: 2px; background-color: #d4af37; margin: 15px auto;"></div>
</div>
<!-- Certificate Content -->
<div class="text-center my-5">
<p style="font-family: 'Cormorant Garamond', serif; font-size: 22px; color: #444; font-style: italic;">This is to certify that</p>
<h3 style="font-family: 'Playfair Display', serif; color: #14397d; font-size: 42px; font-weight: bold; margin: 20px 0; text-transform: capitalize;"><?php echo htmlspecialchars($certificate['student_name']); ?></h3>
<p style="font-family: 'Cormorant Garamond', serif; font-size: 22px; color: #444; font-style: italic;">has successfully completed the course</p>
<h3 style="font-family: 'Playfair Display', serif; color: #14397d; font-size: 36px; font-weight: bold; margin: 20px 0; padding: 0 40px;"><?php echo htmlspecialchars($certificate['course_title']); ?></h3>
<?php if (!empty($certificate['duration'])): ?>
<p style="font-family: 'Cormorant Garamond', serif; font-size: 22px; color: #444;">
with a duration of <strong><?php echo htmlspecialchars($certificate['duration']); ?></strong>
</p>
<?php endif; ?>
<?php if (!empty($completion_date)): ?>
<p style="font-family: 'Cormorant Garamond', serif; font-size: 22px; color: #444;">
Completed on <strong><?php echo $completion_date; ?></strong>
</p>
<?php endif; ?>
</div>
<!-- Exam Details -->
<?php if (!empty($certificate['exam_score'])): ?>
<div class="text-center my-4" style="background-color: rgba(255,255,255,0.7); border-radius: 10px; width: 70%; margin: 0 auto; padding: 15px; border: 1px solid #d4af37;">
<h4 style="font-family: 'Playfair Display', serif; color: #14397d; font-size: 24px; margin-bottom: 10px;">Examination Results</h4>
<table style="width: 100%; font-family: 'Cormorant Garamond', serif; font-size: 18px; color: #444; border-collapse: collapse;">
<tr>
<td style="text-align: right; padding: 5px 10px; font-weight: bold; width: 40%;">Exam:</td>
<td style="text-align: left; padding: 5px 10px;"><?php echo htmlspecialchars($certificate['exam_title']); ?></td>
</tr>
<tr>
<td style="text-align: right; padding: 5px 10px; font-weight: bold;">Score:</td>
<td style="text-align: left; padding: 5px 10px;"><?php echo number_format($certificate['exam_score'], 1); ?>%</td>
</tr>
<tr>
<td style="text-align: right; padding: 5px 10px; font-weight: bold;">Grade:</td>
<td style="text-align: left; padding: 5px 10px;"><strong style="font-size: 22px; color: #8B0000;"><?php echo $letter_grade; ?></strong> (<?php echo $grade; ?>)</td>
</tr>
</table>
</div>
<?php endif; ?>
<!-- Signatures Section -->
<div class="row mt-5">
<div class="col-5 text-center">
<img src="https://i.imgur.com/XzI6xGH.png" style="max-width: 150px; height: auto; margin-bottom: 10px;">
<div style="border-top: 2px solid #14397d; width: 80%; margin: 0 auto; padding-top: 10px;">
<?php if (!empty($certificate['instructor_name'])): ?>
<p style="margin-bottom: 0; font-family: 'Playfair Display', serif; font-size: 18px; font-weight: bold; color: #14397d;"><?php echo htmlspecialchars($certificate['instructor_name']); ?></p>
<p style="margin-bottom: 0; font-family: 'Cormorant Garamond', serif; font-size: 16px; color: #444;"><?php echo !empty($certificate['instructor_designation']) ? htmlspecialchars($certificate['instructor_designation']) : 'Course Instructor'; ?></p>
<?php else: ?>
<p style="margin-bottom: 0; font-family: 'Playfair Display', serif; font-size: 18px; font-weight: bold; color: #14397d;">Course Instructor</p>
<?php endif; ?>
</div>
</div>
<div class="col-2 text-center">
<!-- Certificate Seal -->
<div style="position: relative; margin-top: -20px;">
<img src="https://i.imgur.com/uVNU2kj.png" style="max-width: 120px; height: auto;">
</div>
</div>
<div class="col-5 text-center">
<img src="https://i.imgur.com/XzI6xGH.png" style="max-width: 150px; height: auto; margin-bottom: 10px;">
<div style="border-top: 2px solid #14397d; width: 80%; margin: 0 auto; padding-top: 10px;">
<p style="margin-bottom: 0; font-family: 'Playfair Display', serif; font-size: 18px; font-weight: bold; color: #14397d;">Director</p>
<p style="margin-bottom: 0; font-family: 'Cormorant Garamond', serif; font-size: 16px; color: #444;"><?php echo htmlspecialchars($institute_name); ?></p>
</div>
</div>
</div>
<!-- Certificate Footer -->
<div class="row mt-5 align-items-center">
<div class="col-8">
<div style="border: 1px solid #d4af37; padding: 15px; background-color: rgba(255,255,255,0.7); border-radius: 10px;">
<div class="row">
<div class="col-6">
<p style="margin-bottom: 5px; font-family: 'Cormorant Garamond', serif; font-size: 14px; color: #444;">
<strong>Certificate Number:</strong><br>
<?php echo htmlspecialchars($certificate['certificate_number']); ?>
</p>
<p style="margin-bottom: 5px; font-family: 'Cormorant Garamond', serif; font-size: 14px; color: #444;">
<strong>Verification Code:</strong><br>
<?php echo htmlspecialchars($certificate['verification_code']); ?>
</p>
</div>
<div class="col-6">
<p style="margin-bottom: 5px; font-family: 'Cormorant Garamond', serif; font-size: 14px; color: #444;">
<strong>Issue Date:</strong><br>
<?php echo $issue_date; ?>
</p>
<p style="margin-bottom: 0; font-family: 'Cormorant Garamond', serif; font-size: 14px; color: #444;">
<strong>Serial Number:</strong><br>
<?php echo $serial_number; ?>
</p>
</div>
</div>
</div>
<p class="mt-2" style="font-family: 'Cormorant Garamond', serif; font-size: 12px; color: #666; font-style: italic;">
This certificate is valid only when verified through the official website.<br>
Verify at: <?php echo $verification_url; ?>
</p>
</div>
<div class="col-4 text-center">
<!-- QR Code -->
<img src="<?php echo $qr_code_url; ?>" alt="Verification QR Code" style="max-width: 120px;">
<p style="font-family: 'Cormorant Garamond', serif; font-size: 12px; color: #666; margin-top: 5px;">
Scan to verify
</p>
</div>
</div>
<!-- Bottom Ornamental Divider -->
<div class="text-center mt-2">
<img src="https://i.imgur.com/gDGlQhO.png" style="max-width: 80%; height: auto; transform: rotate(180deg);">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<style>
@import url('https://fonts.googleapis.com/css2?family=Playfair+Display:wght@400;700&family=Cormorant+Garamond:wght@400;500;600;700&display=swap');
#certificateContainer {
position: relative;
}
/* Print styles */
@media print {
body * {
visibility: hidden;
}
#certificateContainer, #certificateContainer * {
visibility: visible;
}
#certificateContainer {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
.container-fluid, .card, .card-body {
padding: 0 !important;
margin: 0 !important;
}
header, footer, .btn, .alert, nav, .sidebar, #mainHeader {
display: none !important;
}
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.9.3/html2pdf.bundle.min.js"></script>
<script>
document.getElementById('printCertificate').addEventListener('click', function() {
window.print();
});
document.getElementById('downloadPDF').addEventListener('click', function(e) {
e.preventDefault();
const certificate = document.getElementById('certificateContainer');
const options = {
margin: 0,
filename: 'certificate-<?php echo htmlspecialchars($certificate['certificate_number']); ?>.pdf',
image: { type: 'jpeg', quality: 1 },
html2canvas: { scale: 2, useCORS: true },
jsPDF: { unit: 'mm', format: 'a4', orientation: 'landscape' }
};
html2pdf().set(options).from(certificate).save();
});
</script>
<?php include_once('includes/footer.php'); ?>