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 = "
Hello {$student['name']},
You have successfully requested the following room:
You will receive another email once an admin has reviewed your request.
"; MailService::sendMail($student_email, $subject_student, $body_student_html); // 2. Notification email to admin $subject_admin = 'New Room Request Submitted'; $body_admin_html = "A new room request has been submitted.
Please log in to the admin dashboard to approve or reject this request.
"; // 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(); ?>