Auto commit: 2025-10-22T11:48:26.772Z

This commit is contained in:
Flatlogic Bot 2025-10-22 11:48:26 +00:00
parent 9d4612b106
commit 6c55bd3d5d
4 changed files with 149 additions and 10 deletions

View File

@ -28,6 +28,31 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['allocate'])) {
// Update room occupancy
$stmt = $pdo->prepare("UPDATE Rooms SET occupied = occupied + 1 WHERE id = ?");
$stmt->execute([$room_id]);
// --- Email Notification on Manual Allocation ---
$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 FROM Rooms WHERE id = ?");
$stmt_room->execute([$room_id]);
$room = $stmt_room->fetch();
if ($student && $room) {
require_once __DIR__ . '/../mail/MailService.php';
$subject = 'You have been allocated a room';
$body_html = "
<h1>Room Allocation Update</h1>
<p>Hello {$student['name']},</p>
<p>An admin has allocated you to the following room:</p>
<ul>
<li><strong>Room Number:</strong> {$room['room_number']}</li>
</ul>
<p>You can view your allocation details in your student dashboard.</p>
";
MailService::sendMail($student['email'], $subject, $body_html);
}
// --- End Email Notification ---
}
header('Location: allocations.php');
exit();
@ -37,20 +62,35 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['allocate'])) {
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['deallocate'])) {
$allocation_id = $_POST['allocation_id'];
// Get allocation details
$stmt = $pdo->prepare("SELECT room_id FROM Allocations WHERE id = ?");
// Get allocation details for email notification before deleting
$stmt = $pdo->prepare("SELECT a.room_id, u.email, u.name, r.room_number FROM Allocations a JOIN Users u ON a.student_id = u.id JOIN Rooms r ON a.room_id = r.id WHERE a.id = ?");
$stmt->execute([$allocation_id]);
$allocation = $stmt->fetch();
$allocation_details = $stmt->fetch();
if ($allocation) {
// Remove allocation
$stmt = $pdo->prepare("DELETE FROM Allocations WHERE id = ?");
$stmt->execute([$allocation_id]);
if ($allocation_details) {
// 1. Delete the allocation record
$stmt_delete = $pdo->prepare("DELETE FROM Allocations WHERE id = ?");
$stmt_delete->execute([$allocation_id]);
// Update room occupancy
$stmt = $pdo->prepare("UPDATE Rooms SET occupied = occupied - 1 WHERE id = ?");
$stmt->execute([$allocation['room_id']]);
// 2. Update room occupancy
$stmt_update = $pdo->prepare("UPDATE Rooms SET occupied = occupied - 1 WHERE id = ?");
$stmt_update->execute([$allocation_details['room_id']]);
// 3. Send notification email
require_once __DIR__ . '/../mail/MailService.php';
$subject = 'You have been deallocated from your room';
$body_html = "
<h1>Room Allocation Update</h1>
<p>Hello {$allocation_details['name']},</p>
<p>An admin has deallocated you from the following room:</p>
<ul>
<li><strong>Room Number:</strong> {$allocation_details['room_number']}</li>
</ul>
<p>The room is now available for other students. You can request a new room from your dashboard.</p>
";
MailService::sendMail($allocation_details['email'], $subject, $body_html);
}
header('Location: allocations.php');
exit();
}

View File

@ -35,6 +35,30 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$stmt->execute([$student_id]);
$pdo->commit();
// --- Email Notification on Approval ---
$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 FROM Rooms WHERE id = ?");
$stmt_room->execute([$room_id]);
$room = $stmt_room->fetch();
if ($student && $room) {
require_once __DIR__ . '/../mail/MailService.php';
$subject = 'Your Room Request has been Approved';
$body_html = "
<h1>Congratulations, {$student['name']}!</h1>
<p>Your request for the following room has been <strong>approved</strong>:</p>
<ul>
<li><strong>Room Number:</strong> {$room['room_number']}</li>
</ul>
<p>You can now view your allocation details in your student dashboard.</p>
";
MailService::sendMail($student['email'], $subject, $body_html);
}
// --- End Email Notification ---
} catch (Exception $e) {
$pdo->rollBack();
die("Error: " . $e->getMessage());
@ -42,6 +66,27 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
} elseif (isset($_POST['reject'])) {
$stmt = $pdo->prepare("UPDATE RoomRequests SET status = 'rejected' WHERE request_id = ?");
$stmt->execute([$request_id]);
// --- Email Notification on Rejection ---
$stmt_user = $pdo->prepare("SELECT u.email, u.name, r.room_number FROM RoomRequests rr JOIN Users u ON rr.student_id = u.id JOIN Rooms r ON rr.room_id = r.id WHERE rr.request_id = ?");
$stmt_user->execute([$request_id]);
$request_details = $stmt_user->fetch();
if ($request_details) {
require_once __DIR__ . '/../mail/MailService.php';
$subject = 'Your Room Request has been Rejected';
$body_html = "
<h1>Room Request Update</h1>
<p>Hello {$request_details['name']},</p>
<p>We regret to inform you that your request for the following room has been <strong>rejected</strong>:</p>
<ul>
<li><strong>Room Number:</strong> {$request_details['room_number']}</li>
</ul>
<p>This may be because the room was allocated to another student or for other administrative reasons. You can request another available room.</p>
";
MailService::sendMail($request_details['email'], $subject, $body_html);
}
// --- End Email Notification ---
}
header('Location: requests.php');
exit;

View File

@ -60,6 +60,13 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") {
if ($stmt->execute([$name, $email, $hashed_password, $role, $gender, $year, $department])) {
$success = 'Registration successful! You can now <a href="login.php">log in</a>.';
// Send welcome email
require_once __DIR__ . '/../mail/MailService.php';
$subject = 'Welcome to Student Hostel';
$body = "<h1>Welcome, {$name}!</h1><p>Your account has been successfully created. You can now log in and request a room.</p>";
MailService::sendMail($email, $subject, $body);
} else {
$errors[] = 'Something went wrong. Please try again later.';
}

View File

@ -24,6 +24,53 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['request_room']) && !$
$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;
}