diff --git a/admin/allocations.php b/admin/allocations.php index 20a0dac..0abb255 100644 --- a/admin/allocations.php +++ b/admin/allocations.php @@ -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 = " +
Hello {$student['name']},
+An admin has allocated you to the following room:
+You can view your allocation details in your student dashboard.
+ "; + 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 = " +Hello {$allocation_details['name']},
+An admin has deallocated you from the following room:
+The room is now available for other students. You can request a new room from your dashboard.
+ "; + MailService::sendMail($allocation_details['email'], $subject, $body_html); } + header('Location: allocations.php'); exit(); } diff --git a/admin/requests.php b/admin/requests.php index 45a732a..3da6e74 100644 --- a/admin/requests.php +++ b/admin/requests.php @@ -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 = " +Your request for the following room has been approved:
+You can now view your allocation details in your student dashboard.
+ "; + 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 = " +Hello {$request_details['name']},
+We regret to inform you that your request for the following room has been rejected:
+This may be because the room was allocated to another student or for other administrative reasons. You can request another available room.
+ "; + MailService::sendMail($request_details['email'], $subject, $body_html); + } + // --- End Email Notification --- } header('Location: requests.php'); exit; diff --git a/auth/signup.php b/auth/signup.php index 9436f54..bf03fc0 100644 --- a/auth/signup.php +++ b/auth/signup.php @@ -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 log in.'; + + // Send welcome email + require_once __DIR__ . '/../mail/MailService.php'; + $subject = 'Welcome to Student Hostel'; + $body = "Your account has been successfully created. You can now log in and request a room.
"; + MailService::sendMail($email, $subject, $body); + } else { $errors[] = 'Something went wrong. Please try again later.'; } diff --git a/student/request_room.php b/student/request_room.php index 026c272..53c9915 100644 --- a/student/request_room.php +++ b/student/request_room.php @@ -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 = " +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; }