| Server IP : 104.21.80.248 / Your IP : 172.71.28.156 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/education/ |
Upload File : |
<?php
require_once 'config.php';
check_login();
// Check user type for access
if ($_SESSION['user_type'] != 'school' && $_SESSION['user_type'] != 'admin') {
header("Location: dashboard.php");
exit();
}
$page_title = "ข้อมูลสำหรับพิมพ์ - ระบบจัดการข้อมูลการเรียนต่อ สพม.ราชบุรี";
$user_id = $_SESSION['user_id'];
$user_type = $_SESSION['user_type'];
$current_year = get_current_academic_year();
// --- Fetch Continue Study Data ---
$sql_continue = "SELECT scs.*, un.name as uni_name, fn.name as faculty_name, fd.name as field_name, u.name as school_name
FROM student_continue_study scs
LEFT JOIN uni_name un ON scs.uni_id = un.id
LEFT JOIN faculty_name fn ON scs.faculty_id = fn.id
LEFT JOIN field_name fd ON scs.field_id = fd.id
LEFT JOIN user u ON scs.user_id = u.id
WHERE scs.academic_year = '$current_year'";
if ($user_type == 'school') {
$sql_continue .= " AND scs.user_id = $user_id";
}
$sql_continue .= " ORDER BY un.name ASC, fn.name ASC, fd.name ASC";
$result_continue = mysqli_query($conn, $sql_continue);
// --- Fetch Work Data ---
$sql_work = "SELECT sw.*, u.name as school_name
FROM student_work sw
LEFT JOIN user u ON sw.user_id = u.id
WHERE sw.academic_year = '$current_year'";
if ($user_type == 'school') {
$sql_work .= " AND sw.user_id = $user_id";
}
$sql_work .= " ORDER BY sw.created_at DESC";
$result_work = mysqli_query($conn, $sql_work);
// --- Calculate totals for the chart (before rendering HTML) ---
$chart_total_continue = 0;
// Re-fetch data for chart to avoid issues with mysqli_data_seek and large datasets, or use $all_rows later
$temp_result_continue = mysqli_query($conn, str_replace("ORDER BY un.name ASC, fn.name ASC, fd.name ASC", "", $sql_continue));
if ($temp_result_continue) {
while ($row = mysqli_fetch_assoc($temp_result_continue)) {
$chart_total_continue += $row['student_count'];
}
}
$chart_total_work = 0;
$temp_result_work = mysqli_query($conn, str_replace("ORDER BY sw.created_at DESC", "", $sql_work));
if ($temp_result_work) {
while ($row = mysqli_fetch_assoc($temp_result_work)) {
$chart_total_work += $row['student_count'];
}
}
// Store all continue study rows in an array for easier iteration and grouping
$all_continue_rows = [];
if ($result_continue) {
mysqli_data_seek($result_continue, 0); // Reset pointer if already used
while($r = mysqli_fetch_assoc($result_continue)) {
$all_continue_rows[] = $r;
}
}
// Start HTML output
include 'template/header.php';
?>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<style>
/* Print-specific styles */
@media print {
.main-content, .card-body {
background-color: #fff !important; /* Ensure white background for printing */
color: #000 !important; /* Ensure black text for printing */
}
body {
-webkit-print-color-adjust: exact; /* For better background color printing */
print-color-adjust: exact;
}
.navbar, .sidebar, .footer, .no-print {
display: none !important; /* Hide navigation, sidebar, and footer when printing */
}
.container-fluid {
width: 100% !important;
margin: 0 !important;
padding: 0 !important;
}
.card {
border: 1px solid #ccc !important; /* Visible border for cards */
page-break-inside: avoid; /* Prevent card from breaking across pages if possible */
margin-bottom: 20px; /* Space between cards */
}
.card-header {
background-color: #f8f9fa !important; /* Light header background for cards */
border-bottom: 1px solid #dee2e6 !important;
}
.table {
width: 100%;
border-collapse: collapse;
margin-bottom: 0; /* No extra margin for tables inside cards */
}
.table th, .table td {
border: 1px solid #000 !important; /* Ensure borders are black for printing */
padding: 8px;
font-size: 10pt;
}
.table thead th {
background-color: #DDEBF7 !important; /* Header row color */
-webkit-print-color-adjust: exact;
}
.table .table-primary,
.table .table-success,
.table .table-danger,
.table .table-warning,
.table .table-dark,
.table .table-info-custom { /* New class for overall total */
-webkit-print-color-adjust: exact; /* Ensure background colors print */
}
/* Specific colors for printing */
.table .table-primary { background-color: #A9D08E !important; } /* University header */
.table .table-success { background-color: #BDD7EE !important; } /* Faculty header */
.table .table-danger { background-color: #F8CBAD !important; } /* Faculty sub-total */
.table .table-warning { background-color: #FFC000 !important; } /* University total */
.table .table-dark { background-color: #bfbfbf !important; color: #000 !important; } /* Grand total for each section */
.table .table-info-custom { background-color: #66CCFF !important; color: #000 !important; } /* Overall grand total */
h1, h2, h3, h4, h5, h6 { color: #000 !important; } /* Ensure headings are black */
.badge { background-color: #6c757d !important; color: #fff !important; } /* Make badges print darker */
/* Chart specific print styles */
#studentComparisonChart {
max-width: 100%;
height: 400px !important; /* Fixed height for printing */
page-break-after: always; /* Ensure chart is on its own page if needed */
}
}
</style>
<div class="container-fluid main-content">
<div class="row no-print">
<div class="col-12">
<h2 class="text-white mb-4">
<i class="bi bi-printer me-2"></i>
ข้อมูลสำหรับพิมพ์
</h2>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="card shadow-sm">
<div class="card-header d-flex justify-content-between align-items-center">
<h5 class="mb-0">
<i class="bi bi-file-earmark-text me-2"></i>
ข้อมูลนักเรียน ปีการศึกษา <?php echo $current_year; ?>
</h5>
<button class="btn btn-primary no-print" onclick="window.print()">
<i class="bi bi-printer me-2"></i>พิมพ์หน้านี้
</button>
</div>
<div class="card-body">
<h4 class="mb-3">กราฟเปรียบเทียบจำนวนนักเรียน ปีการศึกษา <?php echo $current_year; ?></h4>
<div class="mb-5" style="width: 800px; height: 400px; margin: 0 auto;"> <canvas id="studentComparisonChart"></canvas>
</div>
<script>
// Register the datalabels plugin globally
Chart.register(ChartDataLabels);
document.addEventListener('DOMContentLoaded', function() {
const ctx = document.getElementById('studentComparisonChart').getContext('2d');
const studentComparisonChart = new Chart(ctx, {
type: 'bar', // Bar chart for comparison
data: {
labels: ['นักเรียนเรียนต่อ', 'นักเรียนประกอบอาชีพ'],
datasets: [{
label: 'จำนวนนักเรียน (คน)',
data: [<?php echo $chart_total_continue; ?>, <?php echo $chart_total_work; ?>],
backgroundColor: [
'rgba(54, 162, 235, 0.7)', // Blue for 'continue'
'rgba(75, 192, 192, 0.7)' // Green for 'work'
],
borderColor: [
'rgba(54, 162, 235, 1)',
'rgba(75, 192, 192, 1)'
],
borderWidth: 1
}]
},
options: {
responsive: true,
maintainAspectRatio: false, // Allows the canvas to respect the explicit width/height
plugins: {
title: {
display: true,
text: 'การเปรียบเทียบนักเรียนที่เรียนต่อและประกอบอาชีพ',
font: {
size: 16
}
},
legend: {
display: false // Hide legend if only one dataset
},
datalabels: { // Datalabels plugin configuration
anchor: 'center', // Position the label at the end of the bar
align: 'center', // Align label to the top of the bar
formatter: function(value, context) {
return value + ' คน'; // Add ' คน' suffix
},
font: {
weight: 'bold',
size: 14
},
color: 'black' // Label color
}
},
scales: {
y: {
beginAtZero: true,
title: {
display: true,
text: 'จำนวนนักเรียน'
},
ticks: {
// Ensure integers for student counts
callback: function(value) {
if (Number.isInteger(value)) {
return value;
}
}
}
},
x: {
title: {
display: true,
text: 'ประเภท'
}
}
}
}
});
});
</script>
<h4 class="mt-5 mb-3">ข้อมูลการเรียนต่อ</h4>
<?php
$has_continue_data = !empty($all_continue_rows); // Check if the array of rows is not empty
if ($has_continue_data) :
$total_continue_students_table = 0;
$current_uni = null;
$current_faculty = null;
$uni_total = 0;
$faculty_total = 0;
foreach ($all_continue_rows as $row) :
$total_continue_students_table += $row['student_count'];
// Start a new card for each university change
if ($row['uni_name'] != $current_uni) {
// Close previous university's card if it's not the first one
if ($current_uni !== null) {
// Display totals for the last faculty of the previous university
echo '<tr class="table-danger">';
echo '<td colspan="2" class="text-end fw-bold">รวมนักเรียนในคณะ ' . htmlspecialchars($current_faculty) . ':</td>';
echo '<td class="text-center fw-bold">' . number_format($faculty_total) . ' คน</td>';
if ($user_type == 'admin') {
echo '<td></td>'; // Empty cell for school_name column
}
echo '</tr>';
// Display total for the previous university
echo '</tbody>';
echo '<tfoot>';
echo '<tr class="table-warning">';
echo '<td colspan="2" class="text-end fw-bold">รวมนักเรียนในสถานศึกษา ' . htmlspecialchars($current_uni) . ':</td>';
echo '<td class="text-center fw-bold">' . number_format($uni_total) . ' คน</td>';
if ($user_type == 'admin') {
echo '<td></td>'; // Empty cell for school_name column
}
echo '</tr>';
echo '</tfoot>';
echo '</table>'; // Close table
echo '</div>'; // Close table-responsive
echo '</div>'; // Close card-body
echo '</div>'; // Close card
}
// Start new university
$current_uni = $row['uni_name'];
$uni_total = 0;
$current_faculty = null; // Reset faculty for new university
$faculty_total = 0; // Reset faculty total for new university
echo '<div class="card mb-4 shadow-sm page-break-inside-avoid">'; // New card for university, with page-break-inside-avoid
echo '<div class="card-header bg-primary text-white py-3">';
echo '<h5 class="mb-0 text-center fs-5">สถานศึกษา : ' . htmlspecialchars($current_uni) . '</h5>';
echo '</div>';
echo '<div class="card-body">';
echo '<div class="table-responsive">';
echo '<table class="table table-sm table-bordered table-hover mb-0" style="width: 100%;">';
echo '<thead>';
echo '<tr>';
echo '<th style="width: 30%;">คณะ</th>';
echo '<th style="width: 40%;">สาขาวิชา</th>';
echo '<th class="text-center" style="width: 15%;">จำนวน (คน)</th>';
if ($user_type == 'admin') {
echo '<th style="width: 15%;">ชื่อโรงเรียน</th>';
}
echo '</tr>';
echo '</thead>';
echo '<tbody>';
}
// Check if faculty changes within the same university
if ($row['faculty_name'] != $current_faculty) {
// Display previous faculty total if not the first faculty in this university
if ($current_faculty !== null) {
echo '<tr class="table-danger">';
echo '<td colspan="2" class="text-end fw-bold">รวมนักเรียนในคณะ ' . htmlspecialchars($current_faculty) . ':</td>';
echo '<td class="text-center fw-bold">' . number_format($faculty_total) . ' คน</td>';
if ($user_type == 'admin') {
echo '<td></td>'; // Empty cell for school_name column
}
echo '</tr>';
}
$current_faculty = $row['faculty_name'];
$faculty_total = 0; // Reset faculty total
$colspan_faculty_header = 3; // Default forคณะ, สาขาวิชา, จำนวน
if ($user_type == 'admin') {
$colspan_faculty_header = 4; // If admin, add school_name column
}
echo '<tr class="table-success fw-bold"><td colspan="' . $colspan_faculty_header . '" class="text-start ps-3 py-2 fs-6">คณะ: ' . htmlspecialchars($current_faculty) . '</td></tr>';
}
$uni_total += $row['student_count'];
$faculty_total += $row['student_count'];
?>
<tr>
<td class="ps-4"><?php echo htmlspecialchars($row['faculty_name']); ?></td>
<td class="ps-5"><?php echo htmlspecialchars($row['field_name']); ?></td>
<td class="text-center">
<span class="badge bg-info text-dark"><?php echo number_format($row['student_count']); ?></span>
</td>
<?php if ($user_type == 'admin'): ?>
<td><?php echo htmlspecialchars($row['school_name']); ?></td>
<?php endif; ?>
</tr>
<?php endforeach; ?>
<?php
// Display the last faculty/university total after loop ends
if ($current_uni !== null) {
echo '<tr class="table-danger">';
echo '<td colspan="2" class="text-end fw-bold">รวมนักเรียนในคณะ ' . htmlspecialchars($current_faculty) . ':</td>';
echo '<td class="text-center fw-bold">' . number_format($faculty_total) . ' คน</td>';
if ($user_type == 'admin') {
echo '<td></td>'; // Empty cell for school_name column
}
echo '</tr>';
echo '</tbody>';
echo '<tfoot>';
echo '<tr class="table-warning">';
echo '<td colspan="2" class="text-end fw-bold">รวมนักเรียนในสถานศึกษา ' . htmlspecialchars($current_uni) . ':</td>';
echo '<td class="text-center fw-bold">' . number_format($uni_total) . ' คน</td>';
if ($user_type == 'admin') {
echo '<td></td>'; // Empty cell for school_name column
}
echo '</tr>';
echo '</tfoot>';
echo '</table>'; // Close table
echo '</div>'; // Close table-responsive
echo '</div>'; // Close card-body
echo '</div>'; // Close card
}
?>
<div class="card mt-4 shadow-sm">
<div class="card-body">
<table class="table mb-0">
<tbody>
<tr class="table-dark">
<td class="text-end fw-bold">รวมนักเรียนเรียนต่อทั้งหมด (ทุกสถานศึกษา):</td>
<td class="text-center fw-bold" style="width: 20%;"><?php echo number_format($total_continue_students_table); ?> คน</td>
</tr>
</tbody>
</table>
</div>
</div>
<?php else: // No continue study data ?>
<div class="card mb-4 shadow-sm">
<div class="card-body text-center text-muted py-5">
<i class="bi bi-inbox" style="font-size: 4rem; color: #ccc;"></i><br>
<h5 class="mt-3">ยังไม่มีข้อมูลการเรียนต่อสำหรับปีการศึกษา <?php echo $current_year; ?></h5>
</div>
</div>
<?php endif; ?>
<h4 class="mt-5 mb-3">ข้อมูลการประกอบอาชีพ</h4>
<?php
$has_work_data = mysqli_num_rows($result_work) > 0;
?>
<div class="table-responsive">
<table class="table table-bordered table-sm">
<thead>
<tr>
<th style='background-color: #DDEBF7;'>ประเภท</th>
<th class="text-center" style='background-color: #DDEBF7;'>จำนวน (คน)</th>
<th class="text-center" style='background-color: #DDEBF7;'>วันที่บันทึก</th>
<?php if ($user_type == 'admin'): ?>
<th style='background-color: #DDEBF7;'>ชื่อโรงเรียน</th>
<?php endif; ?>
</tr>
</thead>
<tbody>
<?php
$total_work_students_table = 0;
mysqli_data_seek($result_work, 0); // Reset result pointer for re-iteration
if ($has_work_data) :
while ($row = mysqli_fetch_assoc($result_work)):
$total_work_students_table += $row['student_count'];
?>
<tr>
<td>ประกอบอาชีพ</td>
<td class="text-center">
<span class="badge bg-success"><?php echo number_format($row['student_count']); ?></span>
</td>
<td class="text-center"><?php echo date('d/m/Y H:i', strtotime($row['created_at'])); ?></td>
<?php if ($user_type == 'admin'): ?>
<td><?php echo htmlspecialchars($row['school_name']); ?></td>
<?php endif; ?>
</tr>
<?php endwhile; ?>
<?php else: // No work data ?>
<tr>
<td colspan="<?php echo ($user_type == 'admin' ? "4" : "3"); ?>" class="text-center text-muted py-3">
<i class="bi bi-inbox" style="font-size: 2rem; color: #ccc;"></i><br>
ยังไม่มีข้อมูลการประกอบอาชีพสำหรับปีการศึกษา <?php echo $current_year; ?>
</td>
</tr>
<?php endif; ?>
</tbody>
<tfoot>
<tr class="table-dark">
<td class="text-end fw-bold">รวมนักเรียนประกอบอาชีพทั้งหมด:</td>
<td class="text-center fw-bold"><?php echo number_format($total_work_students_table); ?> คน</td>
<td colspan="<?php echo ($user_type == 'admin' ? "2" : "1"); ?>"></td>
</tr>
</tfoot>
</table>
</div>
<h4 class="mt-5 mb-3">สรุปรวมข้อมูลทั้งหมด</h4>
<table class="table table-bordered table-sm">
<tbody>
<tr class="table-info-custom">
<td class="text-end fw-bold">รวมนักเรียนเรียนต่อทั้งหมด:</td>
<td class="text-center fw-bold" style="width: 20%;"><?php echo number_format($total_continue_students_table); ?> คน</td>
</tr>
<tr class="table-info-custom">
<td class="text-end fw-bold">รวมนักเรียนประกอบอาชีพทั้งหมด:</td>
<td class="text-center fw-bold" style="width: 20%;"><?php echo number_format($total_work_students_table); ?> คน</td>
</tr>
<tr class="table-dark">
<td class="text-end fw-bold">รวมนักเรียน (เรียนต่อ + ประกอบอาชีพ) ทั้งหมด:</td>
<td class="text-center fw-bold" style="width: 20%;"><?php echo number_format($total_continue_students_table + $total_work_students_table); ?> คน</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="row mt-4 no-print">
<div class="col-12">
<div class="card">
<div class="card-header">
<h5 class="mb-0">
<i class="bi bi-arrow-return-left me-2"></i>
กลับ
</h5>
</div>
<div class="card-body">
<a href="dashboard.php" class="btn btn-secondary"><i class="bi bi-house me-2"></i>กลับหน้าหลัก</a>
</div>
</div>
</div>
</div>
</div>
<?php include 'template/footer.php'; ?>