42 lines
1.1 KiB
PHP
42 lines
1.1 KiB
PHP
<?php
|
|
session_start();
|
|
|
|
if (!isset($_SESSION['user_role']) || $_SESSION['user_role'] !== 'admin') {
|
|
header('Location: index.php');
|
|
exit;
|
|
}
|
|
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
$month = date('Y-m');
|
|
$filename = "leave_report_{$month}.csv";
|
|
|
|
header('Content-Type: text/csv');
|
|
header('Content-Disposition: attachment; filename="' . $filename . '"');
|
|
|
|
$output = fopen('php://output', 'w');
|
|
|
|
fputcsv($output, ['Student Name', 'Leave Type', 'Start Date', 'End Date', 'Reason', 'Status']);
|
|
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT users.full_name AS student_name, leave_requests.* FROM leave_requests JOIN users ON leave_requests.student_id = users.id WHERE DATE_FORMAT(leave_requests.created_at, '%Y-%m') = ?");
|
|
$stmt->execute([$month]);
|
|
|
|
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
|
|
fputcsv($output, [
|
|
$row['student_name'],
|
|
$row['leave_type'],
|
|
$row['start_date'],
|
|
$row['end_date'],
|
|
$row['reason'],
|
|
$row['status']
|
|
]);
|
|
}
|
|
} catch (PDOException $e) {
|
|
// handle error
|
|
}
|
|
|
|
fclose($output);
|
|
exit;
|