-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
309 lines (263 loc) · 10.8 KB
/
functions.php
File metadata and controls
309 lines (263 loc) · 10.8 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
<?php
// Common functions for the clinic system
// Start session if not already started
function startSession() {
if (session_status() == PHP_SESSION_NONE) {
// Check if headers have already been sent
if (!headers_sent()) {
session_start();
}
}
}
// Check if user is logged in
function isLoggedIn() {
startSession();
return isset($_SESSION['user_id']) && isset($_SESSION['role']);
}
// Check if user has specific role
function hasRole($role) {
startSession();
return isset($_SESSION['role']) && $_SESSION['role'] === $role;
}
// Check if user has any of the specified roles
function hasAnyRole($roles) {
startSession();
return isset($_SESSION['role']) && in_array($_SESSION['role'], $roles);
}
// Helper function to get root-relative path
function getRootPath($file) {
$scriptPath = $_SERVER['SCRIPT_NAME'];
$scriptDir = dirname($scriptPath);
// Remove leading slash and count directory separators
$scriptDir = ltrim($scriptDir, '/');
// Count how many directory levels deep we are
// /Clinic-System-2/patients -> "Clinic-System-2/patients" -> 1 slash -> 1 level up
$depth = substr_count($scriptDir, '/');
if ($depth > 0) {
// We're in a subdirectory, go up to root
return str_repeat('../', $depth) . $file;
}
// We're in root, use direct path
return $file;
}
// Redirect if not logged in
function requireLogin() {
if (!isLoggedIn()) {
header('Location: ' . getRootPath('login.php'));
exit();
}
}
// Redirect if user doesn't have required role
function requireRole($role) {
requireLogin();
if (!hasRole($role)) {
header('Location: ' . getRootPath('unauthorized.php'));
exit();
}
}
// Redirect if user doesn't have any of the required roles
function requireAnyRole($roles) {
requireLogin();
if (!hasAnyRole($roles)) {
header('Location: ' . getRootPath('unauthorized.php'));
exit();
}
}
// Sanitize input data
function sanitizeInput($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
// Format date for display
function formatDate($date) {
return date('M d, Y', strtotime($date));
}
// Format datetime for display
function formatDateTime($datetime) {
return date('M d, Y g:i A', strtotime($datetime));
}
// Format time for display
function formatTime($time) {
return date('g:i A', strtotime($time));
}
// Generate random password
function generatePassword($length = 8) {
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
return substr(str_shuffle($chars), 0, $length);
}
// Get user's full name
function getFullName($firstName, $lastName) {
return trim($firstName . ' ' . $lastName);
}
// Get current user info
function getCurrentUser($db) {
if (!isLoggedIn()) {
return null;
}
$stmt = $db->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$_SESSION['user_id']]);
return $stmt->fetch();
}
// Enhanced activity logging
function logActivity($db, $action, $details = '') {
if (!isLoggedIn()) {
return;
}
$ip_address = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
try {
$stmt = $db->prepare("INSERT INTO activity_log (user_id, action, details, ip_address) VALUES (?, ?, ?, ?)");
$stmt->execute([$_SESSION['user_id'], $action, $details, $ip_address]);
} catch (Exception $e) {
// Silently fail if logging fails
error_log("Activity log error: " . $e->getMessage());
}
}
// Notification functions
function createNotification($db, $user_id, $title, $message, $type = 'system') {
try {
$stmt = $db->prepare("INSERT INTO notifications (user_id, title, message, type) VALUES (?, ?, ?, ?)");
$stmt->execute([$user_id, $title, $message, $type]);
return true;
} catch (Exception $e) {
error_log("Notification error: " . $e->getMessage());
return false;
}
}
function getUnreadNotifications($db, $user_id) {
try {
$stmt = $db->prepare("SELECT * FROM notifications WHERE user_id = ? AND is_read = FALSE ORDER BY created_at DESC");
$stmt->execute([$user_id]);
return $stmt->fetchAll();
} catch (Exception $e) {
return [];
}
}
function getNotificationCount($db, $user_id) {
try {
$stmt = $db->prepare("SELECT COUNT(*) as count FROM notifications WHERE user_id = ? AND is_read = FALSE");
$stmt->execute([$user_id]);
$result = $stmt->fetch();
return $result['count'] ?? 0;
} catch (Exception $e) {
return 0;
}
}
function markNotificationAsRead($db, $notification_id, $user_id) {
try {
$stmt = $db->prepare("UPDATE notifications SET is_read = TRUE WHERE id = ? AND user_id = ?");
$stmt->execute([$notification_id, $user_id]);
return true;
} catch (Exception $e) {
return false;
}
}
function markAllNotificationsAsRead($db, $user_id) {
try {
$stmt = $db->prepare("UPDATE notifications SET is_read = TRUE WHERE user_id = ?");
$stmt->execute([$user_id]);
return true;
} catch (Exception $e) {
return false;
}
}
// Notification helpers for common actions
function notifyAppointmentBooked($db, $patient_user_id, $doctor_user_id, $appointment_date, $appointment_time) {
// Notify patient
createNotification($db, $patient_user_id, 'Appointment Booked',
"Your appointment has been scheduled for " . formatDate($appointment_date) . " at " . formatTime($appointment_time), 'appointment');
// Notify doctor
createNotification($db, $doctor_user_id, 'New Appointment',
"A new appointment has been scheduled for " . formatDate($appointment_date) . " at " . formatTime($appointment_time), 'appointment');
}
function notifyAppointmentStatusChange($db, $patient_user_id, $status, $appointment_date, $appointment_time) {
$statusMessages = [
'confirmed' => 'Your appointment has been confirmed',
'cancelled' => 'Your appointment has been cancelled',
'completed' => 'Your appointment has been completed'
];
$message = ($statusMessages[$status] ?? 'Your appointment status has been updated') .
" for " . formatDate($appointment_date) . " at " . formatTime($appointment_time);
createNotification($db, $patient_user_id, 'Appointment Update', $message, 'appointment');
}
// Helper to fetch doctor user id
function getDoctorUserId($db, $doctor_id) {
try {
$stmt = $db->prepare("SELECT user_id FROM doctors WHERE id = ?");
$stmt->execute([$doctor_id]);
$doctor = $stmt->fetch();
return $doctor['user_id'] ?? null;
} catch (Exception $e) {
return null;
}
}
// Notify patient (and doctor if needed) when medicine order is submitted
function notifyMedicineOrderSubmitted($db, $patient_user_id, $medicine_name, $requiresApproval = false, $doctor_id = null, $patient_name = null) {
$title = $requiresApproval ? 'Medicine Approval Requested' : 'Medicine Order Confirmed';
$message = $requiresApproval
? "Your request for {$medicine_name} has been sent to your doctor for approval."
: "Your order for {$medicine_name} has been confirmed. Please follow the usage instructions.";
createNotification($db, $patient_user_id, $title, $message, $requiresApproval ? 'warning' : 'success');
if ($requiresApproval && $doctor_id) {
$doctor_user_id = getDoctorUserId($db, $doctor_id);
if ($doctor_user_id) {
$patientLabel = $patient_name ?: 'A patient';
$doctorMessage = "{$patientLabel} requested {$medicine_name} and needs your approval.";
createNotification($db, $doctor_user_id, 'Medicine Approval Needed', $doctorMessage, 'warning');
}
}
}
// Notify patient when doctor updates medicine order status
function notifyMedicineOrderStatus($db, $patient_user_id, $medicine_name, $status) {
$statusMessages = [
'approved' => "Your request for {$medicine_name} has been approved.",
'rejected' => "Your request for {$medicine_name} has been rejected. Please consult your doctor for alternatives.",
'fulfilled' => "{$medicine_name} has been marked as fulfilled and will be ready for pickup shortly."
];
$message = $statusMessages[$status] ?? "Your order for {$medicine_name} has been updated.";
$typeMap = [
'approved' => 'success',
'rejected' => 'warning',
'fulfilled' => 'success'
];
$notificationType = $typeMap[$status] ?? 'system';
createNotification($db, $patient_user_id, 'Medicine Order Update', $message, $notificationType);
}
// Helper function to ensure doctor approval columns exist
function ensureDoctorApprovalColumns($db) {
try {
// Check if approval_status column exists
$stmt = $db->query("SHOW COLUMNS FROM doctors LIKE 'approval_status'");
if ($stmt->rowCount() == 0) {
// Column doesn't exist, add it
$db->exec("ALTER TABLE doctors ADD COLUMN approval_status ENUM('pending', 'approved', 'rejected') DEFAULT 'approved' AFTER user_id");
// Add other related columns if they don't exist
$stmt = $db->query("SHOW COLUMNS FROM doctors LIKE 'approval_notes'");
if ($stmt->rowCount() == 0) {
$db->exec("ALTER TABLE doctors ADD COLUMN approval_notes TEXT AFTER approval_status");
}
$stmt = $db->query("SHOW COLUMNS FROM doctors LIKE 'approved_by'");
if ($stmt->rowCount() == 0) {
$db->exec("ALTER TABLE doctors ADD COLUMN approved_by INT AFTER approval_notes");
}
$stmt = $db->query("SHOW COLUMNS FROM doctors LIKE 'approved_at'");
if ($stmt->rowCount() == 0) {
$db->exec("ALTER TABLE doctors ADD COLUMN approved_at DATETIME AFTER approved_by");
}
// Update existing doctors to approved (they were added by admin)
$db->exec("UPDATE doctors SET approval_status = 'approved', approved_at = NOW() WHERE approval_status IS NULL OR approval_status = ''");
// Create index if it doesn't exist
try {
$db->exec("CREATE INDEX idx_approval_status ON doctors(approval_status)");
} catch (Exception $e) {
// Index might already exist, ignore
}
}
return true;
} catch (Exception $e) {
error_log("Error ensuring doctor approval columns: " . $e->getMessage());
return false;
}
}
?>