-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroom_booking_functions.php
More file actions
351 lines (318 loc) · 11.4 KB
/
room_booking_functions.php
File metadata and controls
351 lines (318 loc) · 11.4 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
<?php
function rb_get_time_slots()
{
return [
'08:00-09:00',
'09:00-10:00',
'10:00-11:00',
'11:00-12:00',
'12:00-13:00',
'13:00-14:00',
'14:00-15:00',
'15:00-16:00',
'16:00-17:00',
'17:00-18:00'
];
}
function rb_get_rooms($conn)
{
$rooms = [];
$sql = "SELECT id, name, location, capacity, is_active FROM rooms ORDER BY name ASC";
if ($result = $conn->query($sql)) {
while ($row = $result->fetch_assoc()) {
$rooms[] = $row;
}
$result->free();
}
return $rooms;
}
function rb_create_room($conn, $name, $location, $capacity, $is_active)
{
$stmt = $conn->prepare("INSERT INTO rooms (name, location, capacity, is_active) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssii", $name, $location, $capacity, $is_active);
$stmt->execute();
$stmt->close();
}
function rb_update_room($conn, $id, $name, $location, $capacity, $is_active)
{
$stmt = $conn->prepare("UPDATE rooms SET name = ?, location = ?, capacity = ?, is_active = ? WHERE id = ?");
$stmt->bind_param("ssiii", $name, $location, $capacity, $is_active, $id);
$stmt->execute();
$stmt->close();
}
function rb_delete_room($conn, $id)
{
$stmt = $conn->prepare("DELETE FROM rooms WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->close();
}
/**
* Get slot statuses for a specific room and date
* Returns array: ['time_slot' => 'status'] where status is 'Approved', 'Pending', 'Rejected', or null
*/
function rb_get_slot_statuses($conn, $room_id, $booking_date)
{
$slots = [];
$stmt = $conn->prepare("SELECT time_slot, status FROM room_bookings WHERE room_id = ? AND booking_date = ?");
$stmt->bind_param("is", $room_id, $booking_date);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$slots[$row['time_slot']] = $row['status'];
}
$stmt->close();
return $slots;
}
/**
* Get the status of a specific slot
* Returns: 'Approved', 'Pending', 'Rejected', or null if slot is available
*/
function rb_get_slot_status($conn, $room_id, $booking_date, $time_slot)
{
$stmt = $conn->prepare("SELECT status FROM room_bookings WHERE room_id = ? AND booking_date = ? AND time_slot = ? LIMIT 1");
$stmt->bind_param("iss", $room_id, $booking_date, $time_slot);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
$stmt->close();
return $row ? $row['status'] : null;
}
/**
* Check if a slot can be booked based on strict validation rules
* Returns: array with 'can_book' (bool) and 'reason' (string)
*
* Rules:
* - Approved: CANNOT book (blocked)
* - Pending: CANNOT book (blocked)
* - Rejected: CAN book (allowed for new request)
* - No record: CAN book (available)
*/
function rb_can_book_slot($conn, $room_id, $booking_date, $time_slot, $is_focal_person = false)
{
$status = rb_get_slot_status($conn, $room_id, $booking_date, $time_slot);
if ($status === 'Approved') {
return [
'can_book' => false,
'reason' => 'Already Booked',
'status' => 'Approved'
];
}
if ($status === 'Pending') {
return [
'can_book' => false,
'reason' => 'Pending Approval',
'status' => 'Pending'
];
}
if ($status === 'Rejected') {
// Rejected slots can be re-booked (especially by focal person)
return [
'can_book' => true,
'reason' => 'Rejected',
'status' => 'Rejected'
];
}
// No record exists - slot is available
return [
'can_book' => true,
'reason' => 'Available',
'status' => null
];
}
/**
* STRICT backend validation for booking requests
* This function MUST be called before creating any booking to prevent bypassing UI
*
* @param mysqli $conn Database connection
* @param int $room_id Room ID
* @param string $booking_date Booking date (Y-m-d format)
* @param string $time_slot Time slot string
* @param bool $is_focal_person Whether the requester is a focal person
* @return array ['valid' => bool, 'error' => string|null]
*/
function rb_validate_booking_request($conn, $room_id, $booking_date, $time_slot, $is_focal_person = false)
{
// Validate inputs
if ($room_id <= 0) {
return ['valid' => false, 'error' => 'Invalid room ID'];
}
if (empty($booking_date) || !preg_match('/^\d{4}-\d{2}-\d{2}$/', $booking_date)) {
return ['valid' => false, 'error' => 'Invalid booking date format'];
}
if (empty($time_slot)) {
return ['valid' => false, 'error' => 'Time slot is required'];
}
// Check slot status
$slot_check = rb_can_book_slot($conn, $room_id, $booking_date, $time_slot, $is_focal_person);
if (!$slot_check['can_book']) {
return [
'valid' => false,
'error' => 'Slot is not available: ' . $slot_check['reason']
];
}
return ['valid' => true, 'error' => null];
}
// Legacy function for backward compatibility
function rb_get_booked_slots($conn, $room_id, $booking_date)
{
$statuses = rb_get_slot_statuses($conn, $room_id, $booking_date);
$slots = [];
foreach ($statuses as $slot => $status) {
// Only mark as "booked" if Approved or Pending (blocked slots)
if ($status === 'Approved' || $status === 'Pending') {
$slots[$slot] = true;
}
}
return $slots;
}
/**
* Create a new booking with STRICT validation
* Backend validation ensures Approved and Pending slots cannot be booked
*
* @param mysqli $conn Database connection
* @param int $faculty_id Faculty member ID
* @param int|null $department_id Department ID
* @param int $room_id Room ID
* @param string $booking_date Booking date (Y-m-d format)
* @param string $time_slot Time slot string
* @param string $event_title Event title
* @param int $num_persons Number of persons
* @param bool $is_focal_person Whether the requester is a focal person
* @return int|false Booking ID on success, false on failure
*/
function rb_create_booking($conn, $faculty_id, $department_id, $room_id, $booking_date, $time_slot, $event_title, $num_persons, $is_focal_person = false)
{
// STRICT backend validation - blocks Approved and Pending slots
$validation = rb_validate_booking_request($conn, $room_id, $booking_date, $time_slot, $is_focal_person);
if (!$validation['valid']) {
return false;
}
// Additional validation
if ($faculty_id <= 0 || $room_id <= 0 || empty($event_title) || $num_persons <= 0) {
return false;
}
// If slot was previously rejected, we can create a new booking
// Otherwise, ensure slot is truly available (no existing record)
$existing_status = rb_get_slot_status($conn, $room_id, $booking_date, $time_slot);
// Double-check: if status exists and is not Rejected, block it
if ($existing_status !== null && $existing_status !== 'Rejected') {
return false;
}
// Insert new booking with Pending status
$stmt = $conn->prepare("INSERT INTO room_bookings (faculty_id, department_id, room_id, booking_date, time_slot, event_title, num_persons, status) VALUES (?, ?, ?, ?, ?, ?, ?, 'Pending')");
$stmt->bind_param("iiisssi", $faculty_id, $department_id, $room_id, $booking_date, $time_slot, $event_title, $num_persons);
$ok = $stmt->execute();
$id = $ok ? $stmt->insert_id : false;
$stmt->close();
return $id;
}
function rb_get_faculty_bookings($conn, $faculty_id)
{
$items = [];
$sql = "SELECT b.*, r.name AS room_name, r.location, d.name AS department_name
FROM room_bookings b
INNER JOIN rooms r ON b.room_id = r.id
LEFT JOIN departments d ON b.department_id = d.depart_id
WHERE b.faculty_id = ?
ORDER BY b.booking_date DESC, b.time_slot ASC";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $faculty_id);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$items[] = $row;
}
$stmt->close();
return $items;
}
function rb_get_all_bookings($conn)
{
$items = [];
$sql = "SELECT b.*, r.name AS room_name, r.location, d.name AS department_name, f.first_name, f.last_name
FROM room_bookings b
INNER JOIN rooms r ON b.room_id = r.id
LEFT JOIN departments d ON b.department_id = d.depart_id
INNER JOIN faculty f ON b.faculty_id = f.id
ORDER BY b.booking_date DESC, b.time_slot ASC";
if ($result = $conn->query($sql)) {
while ($row = $result->fetch_assoc()) {
$items[] = $row;
}
$result->free();
}
return $items;
}
function rb_update_booking_status($conn, $booking_id, $status)
{
$allowed = ['Pending', 'Approved', 'Rejected'];
if (!in_array($status, $allowed, true)) {
return;
}
$stmt = $conn->prepare("UPDATE room_bookings SET status = ? WHERE id = ?");
$stmt->bind_param("si", $status, $booking_id);
$stmt->execute();
$stmt->close();
}
function rb_get_booking($conn, $booking_id)
{
$sql = "SELECT b.*, r.name AS room_name, r.location, d.name AS department_name, f.first_name, f.last_name
FROM room_bookings b
INNER JOIN rooms r ON b.room_id = r.id
LEFT JOIN departments d ON b.department_id = d.depart_id
INNER JOIN faculty f ON b.faculty_id = f.id
WHERE b.id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $booking_id);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
$stmt->close();
return $row;
}
function rb_get_booking_pdf_path($conn, $booking_id)
{
$stmt = $conn->prepare("SELECT pdf_path FROM room_booking_pdfs WHERE booking_id = ? LIMIT 1");
$stmt->bind_param("i", $booking_id);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
$stmt->close();
return $row ? $row['pdf_path'] : null;
}
function rb_set_booking_pdf_path($conn, $booking_id, $pdf_path)
{
$existing = rb_get_booking_pdf_path($conn, $booking_id);
if ($existing !== null) {
$stmt = $conn->prepare("UPDATE room_booking_pdfs SET pdf_path = ? WHERE booking_id = ?");
$stmt->bind_param("si", $pdf_path, $booking_id);
$stmt->execute();
$stmt->close();
} else {
$stmt = $conn->prepare("INSERT INTO room_booking_pdfs (booking_id, pdf_path) VALUES (?, ?)");
$stmt->bind_param("is", $booking_id, $pdf_path);
$stmt->execute();
$stmt->close();
}
}
function rb_get_calendar_data($conn, $room_id, $year, $month)
{
$start = sprintf('%04d-%02d-01', $year, $month);
$end = date('Y-m-t', strtotime($start));
$data = [];
$stmt = $conn->prepare("SELECT booking_date, status FROM room_bookings WHERE room_id = ? AND booking_date BETWEEN ? AND ?");
$stmt->bind_param("iss", $room_id, $start, $end);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$day = (int)date('j', strtotime($row['booking_date']));
if (!isset($data[$day])) {
$data[$day] = ['Pending' => 0, 'Approved' => 0, 'Rejected' => 0];
}
if (isset($data[$day][$row['status']])) {
$data[$day][$row['status']]++;
}
}
$stmt->close();
return $data;
}