Path : /home/vishqocm/pcib.in/
File Upload :
Current File : /home/vishqocm//pcib.in/verify-certificate.php

<?php
// Include database connection
require_once 'config/database.php';

// Initialize variables
$certificate_number = '';
$verification_code = '';
$verification_message = '';
$verification_status = '';
$certificate_data = null;

// Get certificate number from URL if present
if (isset($_GET['cert'])) {
    $certificate_number = trim($_GET['cert']);
}

// Process verification form
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $certificate_number = trim($_POST['certificate_number'] ?? '');
    $verification_code = trim($_POST['verification_code'] ?? '');
    
    if (empty($certificate_number)) {
        $verification_message = "Please enter a certificate number.";
        $verification_status = "danger";
    } elseif (empty($verification_code)) {
        $verification_message = "Please enter a verification code.";
        $verification_status = "danger";
    } else {
        // Check for unified certificate first
        $query = "
            SELECT uc.*, 
                  u.first_name, u.last_name, u.email,
                  c.title as course_title, c.duration, c.image as course_image,
                  e.completion_date,
                  se.percentage as exam_score, se.status as exam_status,
                  es.title as exam_title, es.passing_percentage
            FROM unified_certificates uc
            JOIN users u ON uc.user_id = u.id
            JOIN courses c ON uc.course_id = c.id
            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.certificate_number = ? AND uc.verification_code = ? AND (uc.payment_status = 'paid' OR uc.payment_status = 'completed')
        ";
        
        $stmt = $conn->prepare($query);
        $stmt->bind_param("ss", $certificate_number, $verification_code);
        $stmt->execute();
        $result = $stmt->get_result();
        
        if ($result->num_rows === 1) {
            $certificate_data = $result->fetch_assoc();
            $certificate_data['certificate_type'] = 'unified';
            $verification_message = "Certificate verified successfully!";
            $verification_status = "success";
        } else {
            // If not found, check older separate certificates
            // Check exam certificate
            $query = "
                SELECT ec.*, 
                      se.user_id, se.percentage as exam_score, se.status as exam_status,
                      u.first_name, u.last_name, u.email,
                      c.title as course_title, c.image as course_image,
                      es.title as exam_title, es.passing_percentage
                FROM exam_certificates ec
                JOIN student_exams se ON ec.student_exam_id = se.id
                JOIN users u ON se.user_id = u.id
                JOIN exam_schedules es ON se.exam_id = es.id
                JOIN courses c ON es.course_id = c.id
                WHERE ec.certificate_number = ? AND ec.verification_code = ?
            ";
            
            $stmt = $conn->prepare($query);
            $stmt->bind_param("ss", $certificate_number, $verification_code);
            $stmt->execute();
            $result = $stmt->get_result();
            
            if ($result->num_rows === 1) {
                $certificate_data = $result->fetch_assoc();
                $certificate_data['certificate_type'] = 'exam';
                $verification_message = "Exam certificate verified successfully!";
                $verification_status = "success";
            } else {
                // Check course certificate
                $query = "
                    SELECT e.id, e.user_id, e.course_id, e.completion_date, e.certificate_number, 
                           e.certificate_path, e.certificate_issue_date,
                           u.first_name, u.last_name, u.email,
                           c.title as course_title, c.duration, c.image as course_image
                    FROM enrollments e
                    JOIN users u ON e.user_id = u.id
                    JOIN courses c ON e.course_id = c.id
                    WHERE e.certificate_number = ? AND u.email = ?
                ";
                
                $stmt = $conn->prepare($query);
                $stmt->bind_param("ss", $certificate_number, $verification_code);
                $stmt->execute();
                $result = $stmt->get_result();
                
                if ($result->num_rows === 1) {
                    $certificate_data = $result->fetch_assoc();
                    $certificate_data['certificate_type'] = 'course';
                    $verification_message = "Course completion certificate verified successfully!";
                    $verification_status = "success";
                } else {
                    $verification_message = "Certificate not found. Please check the certificate number and verification code.";
                    $verification_status = "danger";
                }
            }
        }
    }
}

// Function to generate a PDF certificate
function generateCertificatePDF($certificate_data) {
    // Include TCPDF library
    require_once('vendor/tcpdf/tcpdf.php');
    
    // Create new PDF document
    $pdf = new TCPDF('L', 'mm', 'A4', true, 'UTF-8', false);
    
    // Set document information
    $pdf->SetCreator('Popular Computer Institute');
    $pdf->SetAuthor('Popular Computer Institute');
    $pdf->SetTitle('Certificate of Achievement');
    $pdf->SetSubject('Course Completion and Exam Certificate');
    
    // Remove default header/footer
    $pdf->setPrintHeader(false);
    $pdf->setPrintFooter(false);
    
    // Set margins
    $pdf->SetMargins(10, 10, 10);
    
    // Add a page
    $pdf->AddPage();
    
    // Set background image
    $pdf->Image('assets/img/certificate-bg.jpg', 0, 0, 297, 210, '', '', '', false, 300, '', false, false, 0);
    
    // Set certificate content - Course completion page
    $html = '
    <style>
        .certificate-title {
            color: #003366;
            font-size: 30pt;
            text-align: center;
            margin-bottom: 15mm;
            font-weight: bold;
        }
        .certificate-subtitle {
            color: #666666;
            font-size: 16pt;
            text-align: center;
            margin-bottom: 10mm;
        }
        .student-name {
            color: #000066;
            font-size: 24pt;
            text-align: center;
            margin: 15mm 0;
            font-weight: bold;
        }
        .certificate-text {
            color: #333333;
            font-size: 12pt;
            text-align: center;
            margin-bottom: 5mm;
        }
        .certificate-footer {
            color: #666666;
            font-size: 10pt;
            text-align: center;
            margin-top: 20mm;
        }
        .signature-area {
            margin-top: 15mm;
            text-align: center;
        }
        .signature-line {
            width: 70mm;
            border-bottom: 1px solid #000000;
            margin: 0 auto;
        }
        .signature-name {
            font-size: 12pt;
            margin-top: 2mm;
        }
        .certificate-details {
            margin-top: 15mm;
            text-align: center;
            font-size: 9pt;
            color: #666666;
        }
    </style>
    
    <div class="certificate-title">Certificate of Completion</div>
    <div class="certificate-subtitle">This is to certify that</div>
    
    <div class="student-name">' . htmlspecialchars($certificate_data['first_name'] . ' ' . $certificate_data['last_name']) . '</div>
    
    <div class="certificate-text">has successfully completed the course</div>
    <div class="certificate-subtitle">' . htmlspecialchars($certificate_data['course_title']) . '</div>
    
    <div class="certificate-text">on ' . date('F d, Y', strtotime($certificate_data['completion_date'] ?? date('Y-m-d'))) . '</div>
    
    <div class="signature-area">
        <div class="signature-line"></div>
        <div class="signature-name">Director<br>Popular Computer Institute</div>
    </div>
    
    <div class="certificate-details">
        Certificate Number: ' . htmlspecialchars($certificate_data['certificate_number']) . '<br>
        Verification Code: ' . htmlspecialchars($certificate_data['verification_code']) . '<br>
        Issue Date: ' . date('F d, Y', strtotime($certificate_data['issue_date'] ?? $certificate_data['certificate_issue_date'] ?? date('Y-m-d'))) . '
    </div>';
    
    // Write HTML to PDF
    $pdf->writeHTML($html, true, false, true, false, '');
    
    // Check if this is a unified certificate with exam data
    if ($certificate_data['certificate_type'] == 'unified' && !empty($certificate_data['exam_status']) && $certificate_data['exam_status'] == 'passed') {
        // Add a new page for the exam scorecard
        $pdf->AddPage();
        
        // Set background image for scorecard
        $pdf->Image('assets/img/certificate-bg2.jpg', 0, 0, 297, 210, '', '', '', false, 300, '', false, false, 0);
        
        // Exam scorecard content
        $scorecardHtml = '
        <style>
            .scorecard-title {
                color: #003366;
                font-size: 24pt;
                text-align: center;
                margin-bottom: 10mm;
                font-weight: bold;
            }
            .student-info {
                color: #333333;
                font-size: 14pt;
                text-align: center;
                margin-bottom: 10mm;
            }
            .exam-info {
                color: #333333;
                font-size: 12pt;
                text-align: center;
                margin-bottom: 5mm;
            }
            .score-container {
                margin: 10mm auto;
                width: 80%;
                text-align: center;
            }
            .score-box {
                background-color: #f0f0f0;
                border: 1px solid #cccccc;
                border-radius: 5mm;
                padding: 5mm;
                margin-bottom: 5mm;
            }
            .score-label {
                font-size: 12pt;
                color: #666666;
            }
            .score-value {
                font-size: 18pt;
                color: #003366;
                font-weight: bold;
            }
            .pass-status {
                font-size: 14pt;
                font-weight: bold;
                color: #008800;
                margin-top: 5mm;
            }
            .scorecard-footer {
                margin-top: 15mm;
                text-align: center;
                font-size: 9pt;
                color: #666666;
            }
            .grade-info {
                margin-top: 5mm;
                font-size: 14pt;
                color: #003366;
            }
        </style>
        
        <div class="scorecard-title">Exam Achievement Scorecard</div>
        
        <div class="student-info">
            <strong>Student:</strong> ' . htmlspecialchars($certificate_data['first_name'] . ' ' . $certificate_data['last_name']) . '
        </div>
        
        <div class="exam-info">
            <strong>Exam:</strong> ' . htmlspecialchars($certificate_data['exam_title']) . '<br>
            <strong>Course:</strong> ' . htmlspecialchars($certificate_data['course_title']) . '
        </div>
        
        <div class="score-container">
            <div class="score-box">
                <div class="score-label">Final Score</div>
                <div class="score-value">' . number_format($certificate_data['exam_score'], 1) . '%</div>
                <div class="score-label">Passing Score: ' . $certificate_data['passing_percentage'] . '%</div>
            </div>
            
            <div class="pass-status">PASSED</div>';
            
        // Calculate grade based on score
        $grade = '';
        $description = '';
        
        if ($certificate_data['exam_score'] >= 90) {
            $grade = 'A+';
            $description = 'Outstanding';
        } elseif ($certificate_data['exam_score'] >= 80) {
            $grade = 'A';
            $description = 'Excellent';
        } elseif ($certificate_data['exam_score'] >= 70) {
            $grade = 'B+';
            $description = 'Very Good';
        } elseif ($certificate_data['exam_score'] >= 60) {
            $grade = 'B';
            $description = 'Good';
        } elseif ($certificate_data['exam_score'] >= 50) {
            $grade = 'C';
            $description = 'Average';
        } else {
            $grade = 'D';
            $description = 'Pass';
        }
        
        $scorecardHtml .= '
            <div class="grade-info">
                Grade: <strong>' . $grade . '</strong> (' . $description . ')
            </div>
        </div>
        
        <div class="scorecard-footer">
            This scorecard is part of the certificate number ' . htmlspecialchars($certificate_data['certificate_number']) . '.<br>
            The authenticity of this document can be verified at our website using the verification code.
        </div>';
        
        // Write scorecard HTML to PDF
        $pdf->writeHTML($scorecardHtml, true, false, true, false, '');
    }
    
    // Output PDF
    $pdf->Output('certificate.pdf', 'I');
    exit;
}

// Download certificate if requested
if (isset($_GET['download']) && $certificate_data) {
    generateCertificatePDF($certificate_data);
}

// Get institute information
$institute_settings = [];
$institute_query = "SELECT setting_key, setting_value FROM site_settings WHERE setting_key IN ('institute_name', 'institute_logo', 'institute_address')";
$result = $conn->query($institute_query);

if ($result) {
    while ($row = $result->fetch_assoc()) {
        $institute_settings[$row['setting_key']] = $row['setting_value'];
    }
}

$institute_name = $institute_settings['institute_name'] ?? "Popular Computer Institute";
$institute_logo = $institute_settings['institute_logo'] ?? "assets/img/logo.png";
$institute_address = $institute_settings['institute_address'] ?? "Bhimpura No.1,Ballia,UP";
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Verify Certificate - Popular Computer Institute</title>
    <!-- Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #f5f5f5;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        .verification-container {
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
            padding: 30px;
            margin-top: 50px;
        }
        .header {
            text-align: center;
            margin-bottom: 30px;
        }
        .header img {
            max-height: 80px;
            margin-bottom: 20px;
        }
        .certificate-display {
            background-color: #f9f9f9;
            border-radius: 10px;
            padding: 20px;
            margin-top: 30px;
        }
        .certificate-header {
            text-align: center;
            border-bottom: 1px solid #ddd;
            padding-bottom: 20px;
            margin-bottom: 20px;
        }
        .certificate-details {
            display: flex;
            flex-wrap: wrap;
        }
        .certificate-detail {
            flex: 1;
            min-width: 250px;
            margin-bottom: 15px;
        }
        .certificate-label {
            font-weight: bold;
            color: #666;
            font-size: 0.9rem;
        }
        .certificate-value {
            font-size: 1.1rem;
        }
        .status-badge {
            font-size: 1.2rem;
            padding: 8px 15px;
            border-radius: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
    <div class="verification-container">
                    <div class="header">
                        <img src="assets/img/logo.png" alt="Popular Computer Institute Logo">
                        <h2>Certificate Verification</h2>
                        <p class="text-muted">Verify the authenticity of your certificate</p>
        </div>
        
                    <form method="POST" action="">
                        <div class="row g-3">
                            <div class="col-md-6">
                            <label for="certificate_number" class="form-label">Certificate Number</label>
                                <input type="text" class="form-control" id="certificate_number" name="certificate_number" value="<?php echo htmlspecialchars($certificate_number); ?>" required>
                            </div>
                            <div class="col-md-6">
                                <label for="verification_code" class="form-label">Verification Code</label>
                                <input type="text" class="form-control" id="verification_code" name="verification_code" value="<?php echo htmlspecialchars($verification_code); ?>" required>
                        </div>
                            <div class="col-12 mt-3">
                                <button type="submit" class="btn btn-primary w-100">Verify Certificate</button>
                            </div>
                        </div>
                    </form>
                    
                    <?php if ($verification_message): ?>
                    <div class="alert alert-<?php echo $verification_status; ?> mt-4">
                        <?php echo $verification_message; ?>
                    </div>
                    <?php endif; ?>
                    
                    <?php if ($certificate_data): ?>
                    <div class="certificate-display">
                        <div class="certificate-header">
                            <h4>Certificate Information</h4>
                            <span class="badge bg-success status-badge">Verified</span>
                        </div>
                        
                        <div class="certificate-details">
                            <div class="certificate-detail">
                                <div class="certificate-label">Student Name</div>
                                <div class="certificate-value"><?php echo htmlspecialchars($certificate_data['first_name'] . ' ' . $certificate_data['last_name']); ?></div>
                        </div>
                            
                            <div class="certificate-detail">
                                <div class="certificate-label">Course</div>
                                <div class="certificate-value"><?php echo htmlspecialchars($certificate_data['course_title']); ?></div>
                    </div>
                    
                            <?php if ($certificate_data['certificate_type'] === 'unified' || $certificate_data['certificate_type'] === 'course'): ?>
                            <div class="certificate-detail">
                                <div class="certificate-label">Completion Date</div>
                                <div class="certificate-value"><?php echo date('d M Y', strtotime($certificate_data['completion_date'])); ?></div>
                                </div>
                            <?php endif; ?>
                            
                            <?php if (($certificate_data['certificate_type'] === 'unified' || $certificate_data['certificate_type'] === 'exam') && !empty($certificate_data['exam_score'])): ?>
                            <div class="certificate-detail">
                                <div class="certificate-label">Exam</div>
                                <div class="certificate-value"><?php echo htmlspecialchars($certificate_data['exam_title']); ?></div>
                                </div>
                            
                            <div class="certificate-detail">
                                <div class="certificate-label">Exam Score</div>
                                <div class="certificate-value"><?php echo $certificate_data['exam_score']; ?>%</div>
                                </div>
                            <?php endif; ?>
                            
                            <div class="certificate-detail">
                                <div class="certificate-label">Certificate Number</div>
                                <div class="certificate-value"><?php echo htmlspecialchars($certificate_data['certificate_number']); ?></div>
                            </div>
                            
                            <div class="certificate-detail">
                                <div class="certificate-label">Issue Date</div>
                                <div class="certificate-value"><?php echo date('d M Y', strtotime($certificate_data['issue_date'] ?? $certificate_data['certificate_issue_date'] ?? date('Y-m-d'))); ?></div>
                            </div>
                        </div>
                        
                        <div class="text-center mt-4">
                            <a href="<?php echo $_SERVER['PHP_SELF']; ?>?download=1&cert=<?php echo urlencode($certificate_data['certificate_number']); ?>&code=<?php echo urlencode($certificate_data['verification_code'] ?? ''); ?>" class="btn btn-success">
                                <i class="fas fa-download me-2"></i> Download Certificate
                            </a>
                        </div>
                    </div>
                    <?php endif; ?>
                </div>
                
                <div class="text-center mt-4">
                    <a href="index.php" class="text-decoration-none">
                        <i class="fas fa-arrow-left me-2"></i> Back to Home
                    </a>
                </div>
            </div>
        </div>
    </div>
    
    <!-- Bootstrap 5 JS Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>