Repository navigation
Expand file tree
/
Copy pathcommunication_room.php
More file actions
286 lines (248 loc) · 9.91 KB
/
Copy pathcommunication_room.php
File metadata and controls
286 lines (248 loc) · 9.91 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
<?php
session_start();
include 'includes/db_connect.php';
// 1. Security: Check Login
if (!isset($_SESSION['user_id']) || !isset($_GET['booking_id'])) {
header("Location: index.php");
exit();
}
$booking_id = intval($_GET['booking_id']);
$user_id = intval($_SESSION['user_id']);
$user_role = $_SESSION['role'];
// 2. Handle "Claim Chat" Action
if (isset($_POST['claim_chat'])) {
if ($user_role == 'student') {
$conn->query("UPDATE bookings SET student_id = $user_id WHERE id = $booking_id");
} elseif ($user_role == 'listener') {
$conn->query("UPDATE bookings SET listener_id = $user_id WHERE id = $booking_id");
}
header("Location: communication_room.php?booking_id=$booking_id");
exit();
}
// 3. Fetch Booking Details
$sql = "SELECT * FROM bookings WHERE id = $booking_id";
$result = $conn->query($sql);
if ($result->num_rows == 0) {
die("Error: Chat not found (Invalid Booking ID).");
}
$booking = $result->fetch_assoc();
// 4. Permission Check
$is_authorized = false;
$mismatch_msg = "";
if ($user_role == 'student') {
if (intval($booking['student_id']) === $user_id) $is_authorized = true;
else $mismatch_msg = "Chat belongs to Student ID: " . $booking['student_id'];
} elseif ($user_role == 'listener') {
if (intval($booking['listener_id']) === $user_id) $is_authorized = true;
else $mismatch_msg = "Chat belongs to Listener ID: " . $booking['listener_id'];
}
// 5. Unauthorized View
if (!$is_authorized) {
echo "<h2 style='text-align:center; margin-top:50px;'>⛔ Permission Error</h2>";
echo "<p style='text-align:center;'>$mismatch_msg</p>";
echo "<form method='POST' style='text-align:center;'><button name='claim_chat' style='padding:10px;'>Fix: Claim This Chat</button></form>";
exit();
}
// 6. Determine Chat Title
$chat_title = "Chat";
if ($user_role == 'listener') {
$chat_title = "Client: " . htmlspecialchars($booking['form_name']);
} else {
$l_id = $booking['listener_id'];
$l_res = $conn->query("SELECT alias FROM listener_profiles WHERE user_id = $l_id");
if($l_res->num_rows > 0){
$l_data = $l_res->fetch_assoc();
$chat_title = "Peer Listener: " . htmlspecialchars($l_data['alias']);
} else {
$chat_title = "Peer Listener";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Chat - PeerLink</title>
<link rel="stylesheet" href="css/style.css?v=<?php echo time(); ?>">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
/* DESKTOP STYLES (Default) */
body { background-color: #f4f7f6; margin: 0; }
.chat-container {
max-width: 800px;
margin: 30px auto;
background: white;
border-radius: 20px;
height: 85vh;
display: flex;
flex-direction: column;
box-shadow: 0 10px 30px rgba(0,0,0,0.1);
overflow: hidden;
border: 1px solid #ddd;
}
.chat-header {
background: #003366;
color: white;
padding: 15px 20px;
display: flex;
justify-content: space-between;
align-items: center;
flex-shrink: 0;
z-index: 10;
}
.chat-body {
flex: 1;
padding: 20px;
overflow-y: auto;
background: #f4f6f9;
display: flex;
flex-direction: column;
gap: 10px;
scroll-behavior: smooth;
}
.chat-footer {
padding: 15px;
border-top: 1px solid #ddd;
background: white;
display: flex;
gap: 10px;
flex-shrink: 0;
align-items: center;
}
/* Message Bubbles */
.message-row { display: flex; width: 100%; margin-bottom: 5px; }
.my-message { justify-content: flex-end; }
.their-message { justify-content: flex-start; }
.chat-bubble { padding: 10px 16px; border-radius: 18px; font-size: 15px; max-width: 75%; word-wrap: break-word; line-height: 1.4; box-shadow: 0 1px 2px rgba(0,0,0,0.1); }
.bubble-blue { background-color: #007bff; color: white; border-bottom-right-radius: 4px; }
.bubble-gray { background-color: #e4e6eb; color: #050505; border-bottom-left-radius: 4px; }
.btn-end { background-color: #444; color: white; padding: 6px 12px; border: none; border-radius: 15px; cursor: pointer; font-weight: bold; text-decoration: none; font-size: 12px; margin-left: 5px; }
.btn-report { background-color: #dc3545; }
/* =========================================
SMARTPHONE VIEW FIXES (Responsive)
========================================= */
@media (max-width: 768px) {
body, html {
height: 100%;
overflow: hidden; /* Prevent body scroll */
background-color: white;
}
.chat-container {
width: 100%;
max-width: 100%;
height: 100vh; /* Full Height */
margin: 0;
border-radius: 0;
border: none;
box-shadow: none;
}
.chat-header {
padding: 12px 15px;
font-size: 16px;
}
.chat-body {
padding: 15px 10px;
}
.chat-footer {
padding: 10px;
background: #fff;
border-top: 1px solid #eee;
position: sticky;
bottom: 0;
}
/* Adjust Input for Mobile */
#msg_input {
font-size: 16px; /* Prevents iOS zoom */
padding: 10px 15px;
}
.btn-end {
padding: 8px 12px;
font-size: 13px;
}
}
</style>
</head>
<body class="dashboard-page">
<div class="chat-container">
<div class="chat-header">
<h3 style="margin:0; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 50%;">
<?php echo $chat_title; ?>
</h3>
<div style="display: flex; align-items: center;">
<?php if ($user_role == 'listener'): ?>
<form action="terminate_session.php" method="POST" style="margin:0;">
<input type="hidden" name="booking_id" value="<?php echo $booking_id; ?>">
<button type="submit" name="action" value="end" class="btn-end">End</button>
</form>
<form action="terminate_session.php" method="POST" style="margin:0;" onsubmit="return confirm('Report this student?');">
<input type="hidden" name="booking_id" value="<?php echo $booking_id; ?>">
<button type="submit" name="action" value="report" class="btn-end btn-report">Report</button>
</form>
<?php endif; ?>
<?php $back_link = ($user_role == 'listener') ? 'listener_dashboard.php' : 'student_home.php'; ?>
<a href="<?php echo $back_link; ?>" class="btn-end" style="background:#222;">Exit</a>
</div>
</div>
<div class="chat-body" id="chat_box"></div>
<div class="chat-footer">
<input type="text" id="msg_input" style="flex:1; padding:12px 20px; border-radius:25px; border:1px solid #ccc; outline:none;" placeholder="Type a message...">
<button onclick="sendMessage()" style="padding:10px 20px; border-radius:25px; border:none; background:#003366; color:white; font-weight:bold; cursor:pointer; margin-left: 5px;">
Send
</button>
</div>
</div>
<script>
var booking_id = <?php echo $booking_id; ?>;
function loadMessages() {
$.ajax({
url: "load_data.php",
method: "GET",
data: { booking_id: booking_id },
success: function(data) {
var chatBox = $("#chat_box");
var currentScroll = chatBox.scrollTop();
var scrollHeight = chatBox[0].scrollHeight;
var height = chatBox.height();
var wasAtBottom = (scrollHeight - currentScroll <= height + 100); // 100px threshold
$("#chat_box").html(data);
if (wasAtBottom) { scrollToBottom(); }
}
});
}
function sendMessage() {
var msg = $("#msg_input").val();
if (msg.trim() == "") return;
$.ajax({
url: "post_data.php",
method: "POST",
data: { booking_id: booking_id, message: msg },
success: function(response) {
if (response.trim() === "Sent") {
$("#msg_input").val("");
loadMessages();
setTimeout(scrollToBottom, 100);
} else {
alert("Message Failed: " + response);
}
},
error: function(xhr, status, error) {
alert("Connection Error: " + error);
}
});
}
function scrollToBottom() {
var chatBox = document.getElementById("chat_box");
chatBox.scrollTop = chatBox.scrollHeight;
}
$(document).ready(function() {
loadMessages();
setInterval(loadMessages, 5000);
setTimeout(scrollToBottom, 500);
});
$("#msg_input").keypress(function(e) {
if(e.which == 13) sendMessage();
});
</script>
</body>
</html>