-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreply_feedback.php
More file actions
177 lines (157 loc) · 6.92 KB
/
reply_feedback.php
File metadata and controls
177 lines (157 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
session_start();
include 'connection.php';
// Authentication Check
if (!isset($_SESSION['staff']) || !isset($_SESSION['staff_id'])) {
header("Location: staff_login.php");
exit();
}
$feedback_id = $_POST['id'] ?? $_GET['id'] ?? null;
if (!$feedback_id) {
header("Location: notifications_communication.php?tab=feedback");
exit();
}
// Fetch feedback details
$stmt = $db->prepare("SELECT id, full_name, registration_no, message, submitted_at FROM feedback WHERE id = ?");
$stmt->bind_param("i", $feedback_id);
$stmt->execute();
$result = $stmt->get_result();
$feedback = $result->fetch_assoc();
if (!$feedback) {
header("Location: notifications_communication.php?tab=feedback");
exit();
}
// Fetch email from users table using registration_no
$reg_no = $feedback['registration_no'];
$stmt_email = $db->prepare("SELECT email FROM users WHERE registration_no = ?");
$stmt_email->bind_param("s", $reg_no);
$stmt_email->execute();
$stmt_email->bind_result($email);
$stmt_email->fetch();
$stmt_email->close();
$reply_message = "";
$success_message = "";
$error_message = "";
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$reply_message = trim($_POST['reply_message'] ?? '');
if (empty($reply_message)) {
$error_message = "Reply message cannot be empty.";
} else {
$staff_id = $_SESSION['staff_id'];
// Insert reply into feedback_replies table
$stmt = $db->prepare("SELECT registration_no FROM feedback WHERE id = ?");
$stmt->bind_param("i", $feedback_id);
$stmt->execute();
$stmt->bind_result($student_reg_no);
$stmt->fetch();
$stmt->close();
$stmt_insert = $db->prepare("INSERT INTO feedback_replies (feedback_id, reply_message, staff_id, replied_at, is_staff_only) VALUES (?, ?, ?, NOW(), 0)");
if ($stmt_insert === false) {
$error_message = "Failed to prepare reply statement: " . $db->error;
} else {
// Get staff name
$stmt_staff = $db->prepare("SELECT first_name, last_name FROM staff_users WHERE id = ?");
$stmt_staff->bind_param("i", $staff_id);
$stmt_staff->execute();
$stmt_staff->bind_result($first_name, $last_name);
$stmt_staff->fetch();
$stmt_staff->close();
$staff_name = trim($first_name . ' ' . $last_name);
if (empty($staff_name)) {
$staff_name = "Staff ID $staff_id";
}
$stmt_insert->bind_param("sss", $feedback_id, $reply_message, $staff_id);
if ($stmt_insert->execute()) {
$success_message = "Reply sent successfully.";
$reg_no = $feedback['registration_no'];
$stmt_email = $db->prepare("SELECT email FROM users WHERE registration_no = ?");
$stmt_email->bind_param("s", $reg_no);
$stmt_email->execute();
$stmt_email->bind_result($email);
$stmt_email->fetch();
$stmt_email->close();
$to = $email;
$subject = "Reply to your feedback from Booker Library Staff";
$headers = "From: Booker Library Staff <no-reply@bookerlibrary.com>\r\n";
$headers .= "Reply-To: booker.library@gmail.com\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
$email_message = "Dear " . $feedback['name'] . ",\n\n";
$email_message .= "You have received a reply to your feedback:\n\n";
$email_message .= $reply_message . "\n\n";
$email_message .= "Best regards,\nBooker Library Staff";
mail($to, $subject, $email_message, $headers);
$reply_message = "";
// Redirect to the thread view page to remain in the thread
$registration_no = $feedback['registration_no'] ?? '';
header("Location: staff_thread.php?registration_no=" . urlencode($registration_no));
exit();
} else {
$error_message = "Failed to send reply: " . $stmt_insert->error;
}
$stmt_insert->close();
}
}
}
// Fetch existing replies
$replies_stmt = $db->prepare("SELECT fr.reply_message, fr.replied_at, s.first_name, s.last_name FROM feedback_replies fr JOIN staff_users s ON fr.staff_id = s.id WHERE fr.feedback_id = ? ORDER BY fr.replied_at ASC");
$replies_stmt->bind_param("i", $feedback_id);
$replies_stmt->execute();
$replies_result = $replies_stmt->get_result();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Reply to Feedback | Booker Library Staff</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="container mt-5">
<h2>Reply to Feedback</h2>
<div class="card mb-4">
<div class="card-header">
Feedback from <?php echo htmlspecialchars($feedback['full_name']); ?> <<?php echo htmlspecialchars($email); ?>>
</div>
<div class="card-body">
<p><?php echo nl2br(htmlspecialchars($feedback['message'])); ?></p>
<small class="text-muted">Sent on <?php echo date('F j, Y, g:i a', strtotime($feedback['submitted_at'])); ?></small>
</div>
</div>
<h4>Replies</h4>
<?php if ($replies_result->num_rows > 0): ?>
<ul class="list-group mb-4">
<?php while ($reply = $replies_result->fetch_assoc()): ?>
<li class="list-group-item">
<strong><?php echo htmlspecialchars($reply['first_name'] . ' ' . $reply['last_name']); ?></strong>
<small class="text-muted">on <?php echo date('F j, Y, g:i a', strtotime($reply['replied_at'])); ?></small>
<p><?php echo nl2br(htmlspecialchars($reply['reply_message'])); ?></p>
</li>
<?php endwhile; ?>
</ul>
<?php else: ?>
<p>No replies yet.</p>
<?php endif; ?>
<?php if ($error_message): ?>
<div class="alert alert-danger"><?php echo htmlspecialchars($error_message); ?></div>
<?php endif; ?>
<?php if ($success_message): ?>
<div class="alert alert-success"><?php echo htmlspecialchars($success_message); ?></div>
<?php endif; ?>
<form method="POST" action="">
<input type="hidden" name="id" value="<?php echo htmlspecialchars($feedback_id); ?>" />
<div class="mb-3">
<label for="reply_message" class="form-label">Your Reply</label>
<textarea class="form-control" id="reply_message" name="reply_message" rows="4" required><?php echo htmlspecialchars($reply_message); ?></textarea>
</div>
<button type="submit" class="btn btn-success me-2">
<i class="bi bi-send"></i> Send Reply
</button>
<a href="staff_communications_center.php" class="btn btn-outline-secondary">
<i class="bi bi-arrow-left"></i> Back to Feedback
</a>
</form>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>