35106-vm/student/request_room.php
Flatlogic Bot 9d4612b106 version2.0
2025-10-22 11:43:14 +00:00

62 lines
2.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]);
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'; ?>