-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeedback.php
More file actions
466 lines (436 loc) · 16.1 KB
/
feedback.php
File metadata and controls
466 lines (436 loc) · 16.1 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
<?php
session_start();
include "connection.php";
$success_message = "";
$error_message = "";
// Check if user is staff (simple check, adjust as needed)
$is_staff = $_SESSION['user_role'] ?? '' === 'staff';
$student_reg_no = $_SESSION['student_reg_no'] ?? '';
// Handle new feedback submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['new_feedback'])) {
$message = trim($_POST['message'] ?? '');
$student_reg_no = $_SESSION['student_reg_no'] ?? '';
if ($message === '') {
$error_message = "Please enter your feedback message.";
} elseif (strlen($message) > 1000) {
$error_message = "Feedback message must be 1000 characters or less.";
} else {
// Remove staff_id to avoid foreign key constraint error
// Fetch student's full name
$first_name = '';
$last_name = '';
$stmt_name = $db->prepare("SELECT first_name, last_name FROM users WHERE registration_no = ?");
if ($stmt_name) {
$stmt_name->bind_param("s", $student_reg_no);
$stmt_name->execute();
$stmt_name->bind_result($first_name, $last_name);
$stmt_name->fetch();
$stmt_name->close();
}
$full_name = trim($first_name . ' ' . $last_name);
if (empty($full_name)) {
$full_name = "Anonymous";
}
$stmt = $db->prepare("INSERT INTO feedback (full_name, registration_no, message, parent_feedback_id, submitted_at) VALUES (?, ?, ?, NULL, NOW())");
if ($stmt) {
$stmt->bind_param("sss", $full_name, $student_reg_no, $message);
if ($stmt->execute()) {
$success_message = "Thank you for your feedback! Your message has been sent to the staff.";
} else {
$error_message = "Failed to submit feedback: " . $stmt->error;
}
$stmt->close();
} else {
$error_message = "Failed to prepare feedback submission: " . $db->error;
}
}
}
// Handle staff reminder submission
if ($is_staff && $_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['new_reminder'])) {
$reminder_message = trim($_POST['reminder_message'] ?? '');
$target_student_reg_no = trim($_POST['target_student_reg_no'] ?? '');
if ($reminder_message === '' || $target_student_reg_no === '') {
$error_message = "Please provide both student registration number and reminder message.";
} else {
$staff_id = $_SESSION['user_id'] ?? 0;
$stmt = $db->prepare("INSERT INTO staff_reminders (student_reg_no, staff_id, reminder_message, created_at) VALUES (?, ?, ?, NOW())");
if ($stmt) {
$stmt->bind_param("sis", $target_student_reg_no, $staff_id, $reminder_message);
if ($stmt->execute()) {
$success_message = "Reminder sent successfully.";
} else {
$error_message = "Failed to send reminder: " . $stmt->error;
}
$stmt->close();
} else {
$error_message = "Failed to prepare reminder submission: " . $db->error;
}
}
}
// Fetch all feedback messages by the logged-in student, including replies from staff
$threads = [];
$sql = "SELECT f.id AS feedback_id, f.full_name AS name, f.message, f.submitted_at, f.staff_id, f.registration_no, f.parent_feedback_id
FROM feedback f
WHERE f.registration_no = ? AND f.message IS NOT NULL AND f.message != ''
ORDER BY f.submitted_at DESC";
$stmt = $db->prepare($sql);
if ($stmt) {
$stmt->bind_param("s", $student_reg_no);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
// Top-level feedback messages
if ($row['parent_feedback_id'] === null) {
$threads[$row['feedback_id']] = [
'id' => $row['feedback_id'],
'name' => $row['name'],
'message' => $row['message'],
'submitted_at' => $row['submitted_at'],
'staff_id' => $row['staff_id'],
'replies' => []
];
}
}
$stmt->close();
}
// Fetch replies from feedback_replies table and attach to threads
$replies_sql = "SELECT fr.id, fr.feedback_id, 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 IN (" . implode(',', array_keys($threads)) . ")
ORDER BY fr.replied_at ASC";
if (!empty($threads)) {
$replies_result = $db->query($replies_sql);
if ($replies_result) {
while ($reply = $replies_result->fetch_assoc()) {
$feedback_id = $reply['feedback_id'];
if (isset($threads[$feedback_id])) {
$threads[$feedback_id]['replies'][] = [
'id' => $reply['id'],
'name' => trim($reply['first_name'] . ' ' . $reply['last_name']),
'message' => $reply['reply_message'],
'submitted_at' => $reply['replied_at'],
'staff_id' => null
];
}
}
}
}
// Fetch staff reminders for the logged-in student
$staff_reminders = [];
if (!$is_staff && $student_reg_no !== '') {
$stmt_reminders = $db->prepare("SELECT sr.id, sr.reminder_message, sr.created_at, s.first_name, s.last_name
FROM staff_reminders sr
JOIN staff_users s ON sr.staff_id = s.id
WHERE sr.student_reg_no = ?
ORDER BY sr.created_at DESC");
if ($stmt_reminders) {
$stmt_reminders->bind_param("s", $student_reg_no);
$stmt_reminders->execute();
$result_reminders = $stmt_reminders->get_result();
while ($row = $result_reminders->fetch_assoc()) {
$staff_reminders[] = $row;
}
$stmt_reminders->close();
}
}
// Handle delete feedback submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_feedback_id'])) {
$delete_feedback_id = intval($_POST['delete_feedback_id']);
// Verify ownership before deleting
$stmt = $db->prepare("SELECT id FROM feedback WHERE id = ? AND registration_no = ?");
$stmt->bind_param("is", $delete_feedback_id, $student_reg_no);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
$stmt->close();
$del_stmt = $db->prepare("DELETE FROM feedback WHERE id = ?");
$del_stmt->bind_param("i", $delete_feedback_id);
if ($del_stmt->execute()) {
$success_message = "Feedback deleted successfully.";
} else {
$error_message = "Failed to delete feedback: " . $del_stmt->error;
}
$del_stmt->close();
} else {
$error_message = "You are not authorized to delete this feedback.";
$stmt->close();
}
}
// Handle new feedback submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['new_feedback'])) {
$message = trim($_POST['message'] ?? '');
$student_reg_no = $_SESSION['student_reg_no'] ?? '';
if ($message === '') {
$error_message = "Please enter your feedback message.";
} elseif (strlen($message) > 1000) {
$error_message = "Feedback message must be 1000 characters or less.";
} else {
// Remove staff_id to avoid foreign key constraint error
// Fetch student's full name
$first_name = '';
$last_name = '';
$stmt_name = $db->prepare("SELECT first_name, last_name FROM users WHERE registration_no = ?");
if ($stmt_name) {
$stmt_name->bind_param("s", $student_reg_no);
$stmt_name->execute();
$stmt_name->bind_result($first_name, $last_name);
$stmt_name->fetch();
$stmt_name->close();
}
$full_name = trim($first_name . ' ' . $last_name);
if (empty($full_name)) {
$full_name = "Anonymous";
}
$stmt = $db->prepare("INSERT INTO feedback (full_name, registration_no, message, parent_feedback_id, submitted_at) VALUES (?, ?, ?, NULL, NOW())");
if ($stmt) {
$stmt->bind_param("sss", $full_name, $student_reg_no, $message);
if ($stmt->execute()) {
$success_message = "Thank you for your feedback! Your message has been sent to the staff.";
} else {
$error_message = "Failed to submit feedback: " . $stmt->error;
}
$stmt->close();
} else {
$error_message = "Failed to prepare feedback submission: " . $db->error;
}
}
}
// Fetch all feedback messages by the logged-in student, including replies from staff
$threads = [];
$sql = "SELECT f.id AS feedback_id, f.full_name AS name, f.message, f.submitted_at, f.staff_id, f.registration_no, f.parent_feedback_id
FROM feedback f
WHERE f.registration_no = ? AND f.message IS NOT NULL AND f.message != ''
ORDER BY f.submitted_at DESC";
$stmt = $db->prepare($sql);
if ($stmt) {
$stmt->bind_param("s", $student_reg_no);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
// Top-level feedback messages
if ($row['parent_feedback_id'] === null) {
$threads[$row['feedback_id']] = [
'id' => $row['feedback_id'],
'name' => $row['name'],
'message' => $row['message'],
'submitted_at' => $row['submitted_at'],
'staff_id' => $row['staff_id'],
'replies' => []
];
}
}
$stmt->close();
}
// Fetch replies from feedback_replies table and attach to threads
$replies_sql = "SELECT fr.id, fr.feedback_id, 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 IN (" . implode(',', array_keys($threads)) . ")
ORDER BY fr.replied_at ASC";
if (!empty($threads)) {
$replies_result = $db->query($replies_sql);
if ($replies_result) {
while ($reply = $replies_result->fetch_assoc()) {
$feedback_id = $reply['feedback_id'];
if (isset($threads[$feedback_id])) {
$threads[$feedback_id]['replies'][] = [
'id' => $reply['id'],
'name' => trim($reply['first_name'] . ' ' . $reply['last_name']),
'message' => $reply['reply_message'],
'submitted_at' => $reply['replied_at'],
'staff_id' => null
];
}
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Feedback | Booker Library</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet" />
<style>
body {
background: linear-gradient(135deg, #4a90e2, #50e3c2);
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
margin: 0;
padding: 0;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
color: #333;
}
.feedback-container {
background: #fff;
border-radius: 16px;
box-shadow: 0 12px 30px rgba(0, 0, 0, 0.15);
max-width: 600px;
width: 100%;
padding: 40px 50px;
box-sizing: border-box;
animation: fadeInUp 0.6s ease forwards;
max-height: 90vh;
overflow-y: auto;
}
@keyframes fadeInUp {
from {
opacity: 0;
transform: translateY(30px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
h1 {
font-weight: 700;
color: #2c3e50;
margin-bottom: 20px;
text-align: center;
font-size: 2.5rem;
}
p.subtitle {
text-align: center;
color: #7f8c8d;
margin-bottom: 40px;
font-size: 1.1rem;
}
label {
font-weight: 600;
color: #34495e;
}
.form-control {
border-radius: 12px;
border: 1.5px solid #bdc3c7;
padding: 14px 20px;
font-size: 1rem;
transition: border-color 0.3s ease;
}
.form-control:focus {
border-color: #4a90e2;
box-shadow: 0 0 8px rgba(74, 144, 226, 0.5);
outline: none;
}
textarea.form-control {
resize: vertical;
min-height: 120px;
background-color: white;
cursor: auto;
}
.btn-submit {
background: linear-gradient(135deg, #4a90e2, #50e3c2);
border: none;
color: white;
font-weight: 700;
font-size: 1.1rem;
padding: 14px;
border-radius: 12px;
width: 100%;
cursor: pointer;
transition: background 0.3s ease;
box-shadow: 0 6px 15px rgba(74, 144, 226, 0.4);
}
.btn-submit:hover {
background: linear-gradient(135deg, #3a78c2, #3bc3b2);
box-shadow: 0 8px 20px rgba(59, 195, 178, 0.6);
}
.btn-back {
margin-top: 15px;
display: block;
width: 100%;
text-align: center;
color: #4a90e2;
font-weight: 600;
text-decoration: none;
font-size: 1rem;
transition: color 0.3s ease;
}
.btn-back:hover {
color: #357abd;
text-decoration: underline;
}
.feedback-thread {
margin-top: 30px;
}
.feedback-message {
background-color: #f0f4f8;
border-radius: 12px;
padding: 15px 20px;
margin-bottom: 10px;
white-space: pre-wrap;
font-size: 1rem;
color: #2c3e50;
}
.feedback-reply {
background-color: #d0e7ff;
border-radius: 12px;
padding: 12px 18px;
margin-left: 30px;
margin-bottom: 10px;
font-size: 0.95rem;
color: #34495e;
position: relative;
}
.feedback-reply .reply-time {
font-size: 0.75rem;
color: #666;
position: absolute;
bottom: 5px;
right: 10px;
}
</style>
</head>
<body>
<div class="feedback-container">
<h1><i class="fas fa-comment-dots"></i> Feedback</h1>
<p class="subtitle">View your feedback messages and staff replies below.</p>
<?php if ($success_message): ?>
<div class="alert alert-success"><?php echo htmlspecialchars($success_message); ?></div>
<?php endif; ?>
<?php if ($error_message): ?>
<div class="alert alert-danger"><?php echo htmlspecialchars($error_message); ?></div>
<?php endif; ?>
<form method="POST" action="feedback.php">
<input type="hidden" name="new_feedback" value="1" />
<label for="message">Send New Feedback</label>
<textarea
id="message"
name="message"
class="form-control"
rows="6"
required
><?php echo htmlspecialchars($message ?? ''); ?></textarea>
<button type="submit" class="btn-submit">Submit Feedback</button>
</form>
<?php if ($is_staff): ?>
<hr />
<h2>Send Reminder to Student</h2>
<form method="POST" action="feedback.php">
<input type="hidden" name="new_reminder" value="1" />
<div class="mb-3">
<label for="target_student_reg_no" class="form-label">Student Registration Number</label>
<input type="text" id="target_student_reg_no" name="target_student_reg_no" class="form-control" required />
</div>
<div class="mb-3">
<label for="reminder_message" class="form-label">Reminder Message</label>
<textarea id="reminder_message" name="reminder_message" class="form-control" rows="4" required></textarea>
</div>
<button type="submit" class="btn-submit">Send Reminder</button>
</form>
<?php endif; ?>
<div class="feedback-thread">
<!-- Removed existing feedback messages and replies display as per user request -->
</div>
<!-- Removed Back to Feedback button as per user request -->
<a href="student_inbox.php" class="btn-back" style="margin-top: 10px;">Inbox</a>
<a href="student_dashboard.php" class="btn-back" style="margin-top: 10px;">Back to Dashboard</a>
</div>
<script src="https://kit.fontawesome.com/a076d05399.js" crossorigin="anonymous"></script>
</body>
</html>