| Server IP : 172.67.187.206 / Your IP : 172.71.28.155 Web Server : Apache/2.4.25 (Win32) OpenSSL/1.0.2j PHP/5.6.30 System : Windows NT WIN-ECQAAA40806 6.2 build 9200 (Windows Server 2012 Standard Edition) i586 User : SYSTEM ( 0) PHP Version : 5.6.30 Disable Function : NONE MySQL : ON | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /Inetpub/www/training/user/ |
Upload File : |
<?php
include("../session.php");
if ($role != 'user') {
header("Location: ../index.php");
exit;
}
include("../db.php");
// ตรวจสอบว่ามีการส่ง subject ที่ต้องการหรือไม่
if (!isset($_GET['subject'])) {
header("Location: dashboard.php");
exit;
}
// ถอดรหัส subject ID
$subject_id = base64_decode($_GET['subject']);
// ดึงข้อมูลผู้ใช้
$user_id = $_SESSION['user_id'];
$user_query = mysqli_query($conn, "SELECT * FROM users WHERE id = $user_id");
$user = mysqli_fetch_assoc($user_query);
// ดึงข้อมูลวิชา
$subject_query = mysqli_query($conn, "SELECT * FROM subjects WHERE id = $subject_id");
$subject = mysqli_fetch_assoc($subject_query);
// ตรวจสอบว่าผู้ใช้ได้สอบผ่านวิชานี้จริงหรือไม่
$result_query = mysqli_query($conn, "SELECT * FROM exam_results
WHERE user_id = $user_id
AND subject_id = $subject_id
AND passed = 1
ORDER BY test_date DESC LIMIT 1");
if (!$result = mysqli_fetch_assoc($result_query)) {
header("Location: dashboard.php");
exit;
}
// ตรวจสอบว่า FPDF มีในระบบหรือไม่
if (!file_exists('../fpdf/fpdf.php')) {
// ถ้าไม่มี FPDF ให้ดาวน์โหลด
mkdir('../fpdf', 0755, true);
$fpdf_url = 'http://www.fpdf.org/en/download/fpdf184.tgz';
$fpdf_file = '../fpdf.tgz';
file_put_contents($fpdf_file, file_get_contents($fpdf_url));
// แตก tar.gz ไฟล์
$phar = new PharData($fpdf_file);
$phar->extractTo('../');
rename('../fpdf184', '../fpdf');
unlink($fpdf_file);
}
// ใช้ FPDF สร้าง PDF
require('../fpdf/fpdf.php');
// สร้างคลาสที่รองรับภาษาไทย
class PDF_Thai extends FPDF
{
function Header()
{
// ว่างไว้เพื่อ override ฟังก์ชัน header
}
function Footer()
{
// ว่างไว้เพื่อ override ฟังก์ชัน footer
}
// ฟังก์ชันสำหรับแสดงข้อความภาษาไทย (แบบพื้นฐาน)
function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
{
// แปลง UTF-8 เป็น ISO-8859-11 (Thai) หรือใช้ encoding อื่นๆ
$txt = iconv('UTF-8', 'cp874', $txt);
parent::Cell($w, $h, $txt, $border, $ln, $align, $fill, $link);
}
function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=false)
{
$txt = iconv('UTF-8', 'cp874', $txt);
parent::MultiCell($w, $h, $txt, $border, $align, $fill);
}
}
// กำหนด Content-Type เป็น PDF
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="certificate_' . $subject['name'] . '.pdf"');
// สร้าง PDF
$pdf = new PDF_Thai('L', 'mm', 'A4');
$pdf->AddPage();
// กำหนดฟอนต์ (ใช้ฟอนต์มาตรฐานแทน)
$pdf->SetFont('Arial', 'B', 30);
// กรอบเกียรติบัตร
$pdf->SetLineWidth(1);
$pdf->SetDrawColor(0, 0, 0);
$pdf->Rect(10, 10, 277, 190);
// กรอบด้านใน
$pdf->SetLineWidth(0.5);
$pdf->SetDrawColor(0, 0, 150);
$pdf->Rect(15, 15, 267, 180);
// หัวเกียรติบัตร
$pdf->SetFont('Arial', 'B', 36);
$pdf->SetTextColor(0, 0, 150);
$pdf->Cell(0, 30, 'เกียรติบัตร', 0, 1, 'C');
// ข้อความรับรอง
$pdf->SetFont('Arial', '', 20);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(0, 10, 'ขอมอบเกียรติบัตรฉบับนี้เพื่อแสดงว่า', 0, 1, 'C');
// ชื่อผู้ได้รับ
$pdf->SetFont('Arial', 'B', 30);
$pdf->Cell(0, 20, $user['username'], 0, 1, 'C');
// ข้อความสำเร็จ
$pdf->SetFont('Arial', '', 20);
$pdf->Cell(0, 10, 'ได้ผ่านการทดสอบวิชา', 0, 1, 'C');
// ชื่อวิชา
$pdf->SetFont('Arial', 'B', 28);
$pdf->SetTextColor(150, 0, 0);
$pdf->Cell(0, 20, $subject['name'], 0, 1, 'C');
// คะแนนที่ได้
$pdf->SetFont('Arial', '', 18);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(0, 10, 'ด้วยคะแนน ' . $result['score'] . ' คะแนน คิดเป็น ' . $result['percentage'] . '%', 0, 1, 'C');
// วันที่สอบ
$thai_month = array(
1 => 'มกราคม', 2 => 'กุมภาพันธ์', 3 => 'มีนาคม', 4 => 'เมษายน', 5 => 'พฤษภาคม', 6 => 'มิถุนายน',
7 => 'กรกฎาคม', 8 => 'สิงหาคม', 9 => 'กันยายน', 10 => 'ตุลาคม', 11 => 'พฤศจิกายน', 12 => 'ธันวาคม'
);
$test_date = strtotime($result['test_date']);
$thai_date = date('j', $test_date) . ' ' . $thai_month[date('n', $test_date)] . ' พ.ศ. ' . (date('Y', $test_date) + 543);
$pdf->SetFont('Arial', '', 16);
$pdf->Cell(0, 30, 'สอบเมื่อวันที่ ' . $thai_date, 0, 1, 'C');
// ลายเซ็น
$pdf->SetFont('Arial', 'B', 16);
$pdf->Cell(0, 20, '...............................................', 0, 1, 'C');
$pdf->SetFont('Arial', '', 14);
$pdf->Cell(0, 10, 'ผู้อำนวยการ', 0, 1, 'C');
// QR Code สำหรับตรวจสอบเกียรติบัตร (สมมติ)
$pdf->SetFont('Arial', '', 8);
$pdf->Cell(0, 50, 'หมายเลขเกียรติบัตร: ' . md5($user_id . $subject_id . $result['test_date']), 0, 1, 'R');
$pdf->Cell(0, 5, 'วันที่ออกเกียรติบัตร: ' . date('d/m/Y'), 0, 1, 'R');
// แสดงผล PDF
$pdf->Output('I', 'certificate_' . $subject['name'] . '.pdf');
exit;
?>