academy/app/Http/Controllers/DocumentDownloadController.php

334 lines
16 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Dompdf\Dompdf;
use Dompdf\Options;
class DocumentDownloadController extends Controller
{
/**
* Check if student is authenticated via session or auth guard
*/
private function isAuthenticated(Request $request)
{
return $request->session()->has('student_id') ||
$request->session()->has('student_log_id') ||
auth()->check();
}
/**
* Academic Calendar PDF Download
*/
public function downloadAcademicCalendar(Request $request)
{
if (!$this->isAuthenticated($request)) {
return redirect()->route('students')->with('error', 'Please login first to download official academic documents!');
}
$title = "Official Academic Calendar 2026";
$subtitle = "Semester Schedules, Lecture Weeks, Holidays & Important Deadlines";
$refNo = "REF: AC-2026-CAL";
$tables = [
[
'title' => '1. SEMESTER DATES & LECTURE WEEKS',
'headers' => ['Academic Event / Phase', 'Start Date', 'End Date', 'Status'],
'rows' => [
['Semester 1 Commencement', 'January 15, 2026', 'March 20, 2026', 'Active'],
['Mid-Semester Recess', 'March 21, 2026', 'March 29, 2026', 'Scheduled'],
['Practical Lab Intensive Weeks', 'March 30, 2026', 'May 15, 2026', 'Scheduled'],
['Semester 1 Final Examinations', 'June 01, 2026', 'June 20, 2026', 'Scheduled'],
['Semester 2 Commencement', 'July 15, 2026', 'November 30, 2026', 'Upcoming']
]
],
[
'title' => '2. INSTITUTIONAL & PUBLIC HOLIDAYS',
'headers' => ['Holiday / Event Name', 'Date(s)', 'Impact on Classes'],
'rows' => [
['National Independence Day', 'February 04, 2026', 'Campus Closed'],
['Sinhala & Tamil New Year Vacation', 'April 12 - April 18, 2026', 'No Lectures / Workshops'],
['Vesak Festival Holiday', 'May 23 - May 25, 2026', 'Campus Closed'],
['Annual Automotive Tech Symposium', 'August 20, 2026', 'Special Event (Mandatory Attendance)']
]
],
[
'title' => '3. CRITICAL ACADEMIC DEADLINES',
'headers' => ['Deadline Description', 'Cut-off Date', 'Action Required'],
'rows' => [
['Course Module Drop / Add Period', 'February 10, 2026', 'Submit online form'],
['Mid-Term Assignment 1 Submission', 'March 18, 2026', 'Upload via Portal'],
['Examination Fee Clearance', 'May 15, 2026', 'Settle at Finance Office']
]
]
];
return $this->renderPdf($title, $subtitle, $refNo, $tables, "Academic_Calendar_2026.pdf");
}
/**
* Student Handbook PDF Download
*/
public function downloadStudentHandbook(Request $request)
{
if (!$this->isAuthenticated($request)) {
return redirect()->route('students')->with('error', 'Please login first to download official academic documents!');
}
$title = "Student Handbook & Code of Conduct";
$subtitle = "Academic Regulations, Workshop Safety Rules & Grading Policies";
$refNo = "REF: HB-2026-REG";
$tables = [
[
'title' => '1. ATTENDANCE & ACADEMIC INTEGRITY POLICIES',
'headers' => ['Regulation Category', 'Requirement / Policy Detail', 'Penalty for Violation'],
'rows' => [
['Class Attendance', 'Minimum 80% attendance required for practical labs and lectures.', 'Barred from Final Exams'],
['Academic Honesty', 'Zero tolerance for plagiarism, cheating, or proxy attendance.', 'Disciplinary Hearing & Grade F'],
['Enrollment Status', 'Students must maintain continuous registration every term.', 'De-registration after 30 days']
]
],
[
'title' => '2. WORKSHOP & HIGH-VOLTAGE LAB SAFETY RULES',
'headers' => ['Safety Protocol', 'Required Equipment / PPE', 'Compliance Rule'],
'rows' => [
['General Workshop PPE', 'Safety boots (steel toe), protective eyewear, lab coats', 'Mandatory at all times'],
['Hybrid / EV High Voltage Labs', 'Class 0 rated insulated safety gloves (1000V rated)', 'Strict supervision required'],
['Vehicle Lift Operation', 'Hydraulic lift locks must be verified before underbody work', 'Never operate alone']
]
],
[
'title' => '3. ACADEMIC GRADING SCHEME',
'headers' => ['Grade', 'Percentage Range', 'GPA Value', 'Classification'],
'rows' => [
['A+', '90% - 100%', '4.00', 'High Distinction'],
['A', '80% - 89%', '3.70', 'Distinction'],
['B', '70% - 79%', '3.00', 'Credit Pass'],
['C', '55% - 69%', '2.00', 'Pass'],
['F', 'Below 50%', '0.00', 'Fail (Re-sit Required)']
]
]
];
return $this->renderPdf($title, $subtitle, $refNo, $tables, "Student_Handbook_2026.pdf");
}
/**
* Exam Code of Conduct PDF Download
*/
public function downloadExamCode(Request $request)
{
if (!$this->isAuthenticated($request)) {
return redirect()->route('students')->with('error', 'Please login first to download official academic documents!');
}
$title = "Exam Code of Conduct & Rules";
$subtitle = "Examination Hall Admission, Practical Station Rules & Disciplinary Code";
$refNo = "REF: EX-2026-RULES";
$tables = [
[
'title' => '1. EXAMINATION HALL ADMISSION RULES',
'headers' => ['Rule Item', 'Requirement Detail', 'Enforcement'],
'rows' => [
['Student Identification', 'Valid Student ID Card & Exam Admission Ticket required.', 'Strictly Enforced'],
['Entry Time Limit', 'Candidates admitted up to 30 minutes after commencement.', 'No extra time given'],
['Prohibited Items', 'Mobile phones, smartwatches, programmable notes, bags.', 'Immediate Confiscation']
]
],
[
'title' => '2. PRACTICAL DIAGNOSTIC EXAMINATIONS',
'headers' => ['Station Requirement', 'Procedure Standard', 'Evaluator Note'],
'rows' => [
['Station Attire', 'Clean workshop overall and non-slip safety shoes required.', 'Visual inspection at entry'],
['Tool Calibration', 'Verify diagnostic scan tools before beginning fault-finding.', 'Instructor verification'],
['Safety Shutoff', 'Always disengage high-voltage battery service plug first.', 'Immediate disqualification if missed']
]
]
];
return $this->renderPdf($title, $subtitle, $refNo, $tables, "Exam_Code_of_Conduct_2026.pdf");
}
/**
* Student Helpdesk Guide PDF Download
*/
public function downloadHelpdeskGuide(Request $request)
{
if (!$this->isAuthenticated($request)) {
return redirect()->route('students')->with('error', 'Please login first to download official academic documents!');
}
$title = "Student Helpdesk & Support Directory";
$subtitle = "Portal Assistance, Technical Enquiries & Department Contact List";
$refNo = "REF: HD-2026-SUPP";
$tables = [
[
'title' => '1. PORTAL & IT SUPPORT CHANNELS',
'headers' => ['Support Issue', 'Resolution Method', 'Response Time'],
'rows' => [
['Password & PIN Reset', 'Self-service PIN reset or visit IT Help Desk (Level 2)', 'Instant / 15 Mins'],
['Module Material Access', 'Email portal-support@autoedu.lk with Student ID', 'Within 2 Hours'],
['Wi-Fi & Email Setup', 'Visit Student IT Counter with Student ID Card', 'Immediate']
]
],
[
'title' => '2. DEPARTMENT CONTACT DIRECTORY',
'headers' => ['Department Name', 'Location', 'Extension', 'Official Email'],
'rows' => [
['Student Affairs & Registration', 'Admin Block - Ground Floor', 'Ext. 101', 'students@autoedu.lk'],
['Finance & Tuition Payments', 'Admin Block - Room 104', 'Ext. 104', 'finance@autoedu.lk'],
['Examinations & Results Branch', 'Academic Wing - Room 202', 'Ext. 108', 'exams@autoedu.lk'],
['Automotive Workshop Office', 'Lab Complex - Block B', 'Ext. 112', 'workshop@autoedu.lk']
]
]
];
return $this->renderPdf($title, $subtitle, $refNo, $tables, "Student_Helpdesk_Support_Guide.pdf");
}
/**
* Render HTML to DomPDF output
*/
private function renderPdf($title, $subtitle, $refNo, $tables, $filename)
{
$studentName = session('student_name', 'Authenticated Student');
$studentId = session('student_id', 'STU-2026-0042');
$dateStr = date('F d, Y');
$tablesHtml = '';
foreach ($tables as $table) {
$tablesHtml .= '<div class="section-header">' . htmlspecialchars($table['title']) . '</div>';
$tablesHtml .= '<table class="content-table"><thead><tr>';
foreach ($table['headers'] as $th) {
$tablesHtml .= '<th>' . htmlspecialchars($th) . '</th>';
}
$tablesHtml .= '</tr></thead><tbody>';
foreach ($table['rows'] as $row) {
$tablesHtml .= '<tr>';
foreach ($row as $cell) {
$tablesHtml .= '<td>' . htmlspecialchars($cell) . '</td>';
}
$tablesHtml .= '</tr>';
}
$tablesHtml .= 'tbody></table>';
}
$html = '
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>' . htmlspecialchars($title) . '</title>
<style>
@page { margin: 25px 35px; }
body { font-family: "Helvetica", "Arial", sans-serif; color: #1E293B; font-size: 11px; line-height: 1.5; }
.header-table { width: 100%; border-collapse: collapse; margin-bottom: 15px; border-bottom: 3px solid #3E51B8; padding-bottom: 10px; }
.logo-title { font-size: 20px; font-weight: bold; color: #0F172A; text-transform: uppercase; letter-spacing: 0.5px; }
.logo-sub { font-size: 10px; color: #64748B; margin-top: 3px; font-weight: normal; }
.meta-badge { background: #0F172A; color: #FFE618; font-size: 9.5px; font-weight: bold; padding: 4px 10px; border-radius: 4px; display: inline-block; text-align: right; }
.doc-banner { background: #3E51B8; color: #ffffff; padding: 14px 18px; border-radius: 6px; margin-bottom: 18px; }
.doc-banner h1 { margin: 0; font-size: 16px; font-weight: bold; text-transform: uppercase; }
.doc-banner p { margin: 4px 0 0 0; font-size: 10.5px; color: #E2E8F0; }
.student-info-bar { background: #F8FAFC; border: 1px solid #E2E8F0; border-radius: 6px; padding: 8px 14px; margin-bottom: 18px; width: 100%; border-collapse: collapse; }
.student-info-bar td { font-size: 10px; color: #334155; }
.student-info-bar td strong { color: #0F172A; }
.section-header { background: #F1F5F9; border-left: 4px solid #BC1A1A; padding: 6px 10px; font-size: 11.5px; font-weight: bold; color: #0F172A; margin-top: 16px; margin-bottom: 8px; text-transform: uppercase; }
.content-table { width: 100%; border-collapse: collapse; margin-bottom: 14px; }
.content-table th { background: #3E51B8; color: #ffffff; padding: 7px 10px; text-align: left; font-size: 10px; text-transform: uppercase; font-weight: bold; }
.content-table td { padding: 7px 10px; border-bottom: 1px solid #E2E8F0; font-size: 10.5px; color: #334155; }
.content-table tr:nth-child(even) td { background: #F8FAFC; }
.footer-box { margin-top: 25px; border-top: 1px solid #CBD5E1; padding-top: 10px; width: 100%; border-collapse: collapse; }
.footer-box td { font-size: 9px; color: #64748B; }
.stamp-box { border: 1.5px dashed #94A3B8; border-radius: 6px; padding: 8px; text-align: center; color: #475569; font-size: 8.5px; font-weight: bold; }
</style>
</head>
<body>
<!-- Header -->
<table class="header-table">
<tr>
<td>
<div class="logo-title">AutoEdu Automotive Academy</div>
<div class="logo-sub">Department of Automotive Engineering & Applied Research | ISO 9001 Certified</div>
</td>
<td style="text-align: right;">
<div class="meta-badge">' . htmlspecialchars($refNo) . '</div>
</td>
</tr>
</table>
<!-- Document Banner -->
<div class="doc-banner">
<h1>' . htmlspecialchars($title) . '</h1>
<p>' . htmlspecialchars($subtitle) . '</p>
</div>
<!-- Student Info Bar -->
<table class="student-info-bar">
<tr>
<td>Issued To: <strong>' . htmlspecialchars($studentName) . '</strong></td>
<td>Student ID: <strong>' . htmlspecialchars($studentId) . '</strong></td>
<td style="text-align: right;">Issue Date: <strong>' . htmlspecialchars($dateStr) . '</strong></td>
</tr>
</table>
<!-- Main Content Tables -->
' . $tablesHtml . '
<!-- Footer -->
<table class="footer-box">
<tr>
<td style="vertical-align: top;">
<strong>AutoEdu Automotive Engineering Institute</strong><br>
Main Campus, Academic Complex, Colombo, Sri Lanka<br>
Hotline: +94 11 234 5678 | Email: support@autoedu.lk | Web: www.autoedu.lk<br>
<em>Notice: This is an official computer-generated document verified for active student session.</em>
</td>
<td style="text-align: right; vertical-align: top; width: 150px;">
<div class="stamp-box">
<span style="color: #BC1A1A; font-weight: bold;">[ OFFICIAL SEAL ]</span><br>
AUTHENTICATED DIGITAL COPY
</div>
</td>
</tr>
</table>
</body>
</html>
';
try {
$options = new Options();
$options->set('isHtml5ParserEnabled', true);
$options->set('isRemoteEnabled', true);
$dompdf = new Dompdf($options);
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
return response($dompdf->output(), 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="' . $filename . '"',
'Cache-Control' => 'private, max-age=0, must-revalidate',
'Pragma' => 'public'
]);
} catch (\Exception $e) {
// Fallback response if rendering fails
return response($html, 200, [
'Content-Type' => 'text/html',
]);
}
}
}