35106-vm/student/request_room.php
2025-10-22 11:48:26 +00:00

109 lines
4.4 KiB
PHP

<?php
require_once '../includes/header.php';
require_once '../db/config.php';
if (!isset($_SESSION['id']) || $_SESSION['role'] !== 'Student') {
header('Location: ../auth/login.php');
exit;
}
$student_id = $_SESSION['id'];
$pdo = db();
// Check if student has a pending or approved request
$stmt = $pdo->prepare("SELECT * FROM RoomRequests WHERE student_id = ? AND (status = 'pending' OR status = 'approved')");
$stmt->execute([$student_id]);
$existing_request = $stmt->fetch();
// Check if student is already allocated a room
$stmt = $pdo->prepare("SELECT * FROM Allocations WHERE student_id = ?");
$stmt->execute([$student_id]);
$allocation = $stmt->fetch();
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['request_room']) && !$existing_request && !$allocation) {
$room_id = $_POST['room_id'];
$stmt = $pdo->prepare("INSERT INTO RoomRequests (student_id, room_id) VALUES (?, ?)");
$stmt->execute([$student_id, $room_id]);
// --- Email Notification ---
// Fetch student and room details for the email
$stmt_user = $pdo->prepare("SELECT email, name FROM Users WHERE id = ?");
$stmt_user->execute([$student_id]);
$student = $stmt_user->fetch();
$stmt_room = $pdo->prepare("SELECT room_number, details FROM Rooms WHERE id = ?");
$stmt_room->execute([$room_id]);
$room = $stmt_room->fetch();
if ($student && $room) {
require_once __DIR__ . '/../mail/MailService.php';
// 1. Confirmation email to student
$student_email = $student['email'];
$subject_student = 'Room Request Submitted';
$body_student_html = "
<h1>Your Room Request has been Submitted</h1>
<p>Hello {$student['name']},</p>
<p>You have successfully requested the following room:</p>
<ul>
<li><strong>Room Number:</strong> {$room['room_number']}</li>
<li><strong>Details:</strong> {$room['details']}</li>
</ul>
<p>You will receive another email once an admin has reviewed your request.</p>
";
MailService::sendMail($student_email, $subject_student, $body_student_html);
// 2. Notification email to admin
$subject_admin = 'New Room Request Submitted';
$body_admin_html = "
<h1>New Room Request</h1>
<p>A new room request has been submitted.</p>
<ul>
<li><strong>Student Name:</strong> {$student['name']}</li>
<li><strong>Student Email:</strong> {$student['email']}</li>
<li><strong>Room Number:</strong> {$room['room_number']}</li>
<li><strong>Details:</strong> {$room['details']}</li>
</ul>
<p>Please log in to the admin dashboard to approve or reject this request.</p>
";
// Sending to default admin address configured in .env (MAIL_TO)
MailService::sendMail(null, $subject_admin, $body_admin_html);
}
// --- End Email Notification ---
header('Location: request_room.php');
exit;
}
// Fetch available rooms (not occupied)
$stmt = $pdo->query("SELECT * FROM Rooms WHERE id NOT IN (SELECT room_id FROM Allocations)");
$available_rooms = $stmt->fetchAll();
?>
<div class="container mt-5">
<h2>Request a Room</h2>
<?php if ($allocation): ?>
<div class="alert alert-info">You are already allocated to a room.</div>
<?php elseif ($existing_request): ?>
<div class="alert alert-warning">
You have a <?php echo htmlspecialchars($existing_request['status']); ?> room request. Please wait for an admin to review it.
</div>
<?php else: ?>
<form method="POST">
<div class="form-group">
<label for="room_id">Select a Room:</label>
<select class="form-control" id="room_id" name="room_id" required>
<?php foreach ($available_rooms as $room): ?>
<option value="<?php echo $room['id']; ?>"><?php echo htmlspecialchars($room['room_number']); ?> - <?php echo htmlspecialchars($room['details']); ?></option>
<?php endforeach; ?>
</select>
</div>
<button type="submit" name="request_room" class="btn btn-primary mt-3">Submit Request</button>
</form>
<?php endif; ?>
<a href="dashboard.php" class="btn btn-secondary mt-3">Back to Dashboard</a>
</div>
<?php require_once '../includes/footer.php'; ?>