-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin_functions.php
More file actions
789 lines (663 loc) · 28.1 KB
/
admin_functions.php
File metadata and controls
789 lines (663 loc) · 28.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
<?php
$GLOBALS['index_page'] = "Location: index.php";
$GLOBALS['visit_page'] = "Location: visit.php";
$GLOBALS['history_visit'] = "Location: history.php";
$GLOBALS['user_page'] = "Location: user.php";
$GLOBALS['payment_page'] = "Location: payment.php";
$GLOBALS['login_page'] = "Location: login.php";
$GLOBALS['error_page'] = "Location: error.php";
$GLOBALS['logout_page'] = "Location: logout.php";
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'database');
define('TABLE_BASIC_DATA', 'basic_data');
define("MAX_FILE_SIZE", 500000);
/**
* Deletes a user from the database.
*
* @param string $phone_number The phone number of the user to be deleted.
* @return string A message indicating success or failure.
*/
function delete_user($phone_number) {
// Ensure the session is active and the user is an admin
if (isset($_SESSION['login']) && $_SESSION['login'] === true && isset($_SESSION['admin']) && $_SESSION['admin'] === true) {
// Connect to the database
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
if ($conn->connect_error) {
return "Connection failed: " . $conn->connect_error;
}
// Prepare the SQL statement to delete the user
$stmt = $conn->prepare("DELETE FROM users WHERE phone_number = ?");
$stmt->bind_param("s", $phone_number);
// Execute the statement and check for errors
if ($stmt->execute()) {
$stmt->close();
$conn->close();
return "User deleted successfully";
} else {
$stmt->close();
$conn->close();
return "Error deleting user: " . $stmt->error;
}
} else {
return "Unauthorized access.";
}
}
/**
* Updates a user's admin status.
*
* @param string $phone_number The phone number of the user whose admin status is to be updated.
* @param bool $is_admin Whether the user should be granted admin privileges (true) or not (false).
* @return string A message indicating success or failure.
*/
function update_admin_status($phone_number, $is_admin) {
// Ensure the session is active and the user is an admin
if (isset($_SESSION['login']) && $_SESSION['login'] === true && isset($_SESSION['admin']) && $_SESSION['admin'] === true) {
// Connect to the database
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
if ($conn->connect_error) {
return "Connection failed: " . $conn->connect_error;
}
// Prepare the SQL statement to update the admin status
$stmt = $conn->prepare("UPDATE users SET admin = ? WHERE phone_number = ?");
$admin_status = $is_admin ? 1 : 0;
$stmt->bind_param("is", $admin_status, $phone_number);
// Execute the statement and check for errors
if ($stmt->execute()) {
$stmt->close();
$conn->close();
return "Admin status updated successfully";
} else {
$stmt->close();
$conn->close();
return "Error updating admin status: " . $stmt->error;
}
} else {
return "Unauthorized access.";
}
}
/**
* Retrieves the details of all users for administrative purposes.
*
* @return array An array of associative arrays, each representing a user's details.
*/
function get_all_user_details() {
// Ensure the session is active and the user is an admin
if (isset($_SESSION['login']) && $_SESSION['login'] === true && isset($_SESSION['admin']) && $_SESSION['admin'] === true) {
// Connect to the database
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
if ($conn->connect_error) {
return "Connection failed: " . $conn->connect_error;
}
// Prepare the SQL statement to retrieve all user details
$stmt = $conn->prepare("SELECT phone_number, name, last_name, height, weight, blood_type, birth_date, admin FROM users");
$stmt->execute();
$result = $stmt->get_result();
$user_details = [];
// Fetch the results into an associative array
while ($row = $result->fetch_assoc()) {
$user_details[] = $row;
}
// Close the statement and connection
$stmt->close();
$conn->close();
// Return the user details
return $user_details;
} else {
return "Unauthorized access.";
}
}
/**
* Resets the password for a user.
*
* @param string $phone_number The phone number of the user whose password is to be reset.
* @param string $new_password The new password to be set for the user.
* @return string A message indicating success or failure.
*/
function reset_user_password($phone_number, $new_password) {
// Ensure the session is active and the user is an admin
if (isset($_SESSION['login']) && $_SESSION['login'] === true && isset($_SESSION['admin']) && $_SESSION['admin'] === true) {
// Connect to the database
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
if ($conn->connect_error) {
return "Connection failed: " . $conn->connect_error;
}
// Prepare the SQL statement to update the user's password
$stmt = $conn->prepare("UPDATE users SET password = ? WHERE phone_number = ?");
$hashed_password = password_hash($new_password, PASSWORD_DEFAULT);
$stmt->bind_param("ss", $hashed_password, $phone_number);
log_user_activity($$_SESSION['admin_username'], 'Change Password', 'admin', "Admin changed user password, User number: $phone_number");
// Execute the statement and check for errors
if ($stmt->execute()) {
$stmt->close();
$conn->close();
return "Password reset successfully";
} else {
$stmt->close();
$conn->close();
return "Error resetting password: " . $stmt->error;
}
} else {
return "Unauthorized access.";
}
}
/**
* Logs in an admin with the given username and password.
*
* @param string $username The admin's username.
* @param string $password The admin's password.
* @return string Returns an error message if the login failed, otherwise redirects to the admin dashboard.
*/
function admin_login($username, $password) {
// Connect to the database
$pdo = new PDO('mysql:host='.DB_SERVER.';dbname='.DB_NAME, DB_USERNAME, DB_PASSWORD);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Prepare SQL statement to retrieve admin data
$stmt = $pdo->prepare("SELECT * FROM admins WHERE username=:username LIMIT 1");
$stmt->bindValue(':username', $username, PDO::PARAM_STR);
$stmt->execute();
// Fetch the admin data
$admin = $stmt->fetch(PDO::FETCH_ASSOC);
// Verify the password and login if valid
if ($admin && password_verify($password, $admin['password'])) {
session_start();
session_regenerate_id(true);
$_SESSION['admin_username'] = filter_var($admin['username'], FILTER_SANITIZE_STRING);
$_SESSION['admin_login'] = true;
header($GLOBALS['index_page']); // Redirect to the admin dashboard
exit();
} else {
return "Invalid username or password.";
}
}
/**
* Retrieves a list of all visits from the database.
*
* @return array An array of associative arrays, each representing a visit.
*/
function get_all_visits() {
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Prepare the SQL query to select all visits
$stmt = $conn->prepare("SELECT * FROM visit");
$stmt->execute();
// Fetch the results into an associative array
$result = $stmt->get_result();
$visit_list = array();
while ($row = $result->fetch_assoc()) {
$visit_list[] = $row;
}
// Close the statement and connection
$stmt->close();
$conn->close();
// Return the list of visits
return $visit_list;
}
/**
* Updates the role of a user in the database.
*
* @param string $phone_number The phone number of the user to update.
* @param string $new_role The new role to assign to the user.
* @return string A message indicating whether the update was successful or not.
*/
function update_user_role($phone_number, $new_role) {
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Prepare the SQL statement to update the user role
$stmt = $conn->prepare("UPDATE users SET role = ? WHERE phone_number = ?");
$stmt->bind_param('ss', $new_role, $phone_number);
// Execute the statement and check if it was successful
if ($stmt->execute()) {
$message = "User role updated successfully.";
} else {
$message = "Error updating user role: " . $stmt->error;
}
// Close the statement and connection
$stmt->close();
$conn->close();
return $message;
}
/**
* Retrieves a list of all payments from the database.
*
* @return array An array of associative arrays, each representing a payment.
*/
function get_all_payments() {
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Prepare the SQL query to select all payments
$stmt = $conn->prepare("SELECT * FROM payments");
$stmt->execute();
// Fetch the results into an associative array
$result = $stmt->get_result();
$payment_list = array();
while ($row = $result->fetch_assoc()) {
$payment_list[] = $row;
}
// Close the statement and connection
$stmt->close();
$conn->close();
return $payment_list;
}
/**
* Deletes a visit record from the database based on the provided visit ID.
*
* @param int $visit_id The ID of the visit to delete.
* @return string A message indicating whether the deletion was successful or not.
*/
function delete_visit($visit_id) {
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Prepare the SQL statement to delete the visit
$stmt = $conn->prepare("DELETE FROM visit WHERE id = ?");
$stmt->bind_param('i', $visit_id);
// Execute the statement and check if it was successful
if ($stmt->execute()) {
$message = "Visit deleted successfully.";
} else {
$message = "Error deleting visit: " . $stmt->error;
}
// Close the statement and connection
$stmt->close();
$conn->close();
return $message;
}
/**
* Deletes a payment record from the database based on the provided payment ID.
*
* @param int $payment_id The ID of the payment to delete.
* @return string A message indicating whether the deletion was successful or not.
*/
function delete_payment($payment_id) {
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Prepare the SQL statement to delete the payment
$stmt = $conn->prepare("DELETE FROM payments WHERE payment_id = ?");
$stmt->bind_param('i', $payment_id);
// Execute the statement and check if it was successful
if ($stmt->execute()) {
$message = "Payment deleted successfully.";
} else {
$message = "Error deleting payment: " . $stmt->error;
}
// Close the statement and connection
$stmt->close();
$conn->close();
return $message;
}
/**
* Retrieves a list of all users from the database.
*
* @return array An array of associative arrays, each representing a user.
*/
function get_all_users() {
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Prepare the SQL query to select all users
$stmt = $conn->prepare("SELECT * FROM users");
$stmt->execute();
// Fetch the results into an associative array
$result = $stmt->get_result();
$user_list = array();
while ($row = $result->fetch_assoc()) {
$user_list[] = $row;
}
// Close the statement and connection
$stmt->close();
$conn->close();
// Return the list of users
return $user_list;
}
/**
* Retrieves a list of all admins from the database.
*
* @return array An array of associative arrays, each representing an admin.
*/
function get_all_admins() {
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Prepare the SQL query to select all admins
$stmt = $conn->prepare("SELECT * FROM admins");
$stmt->execute();
// Fetch the results into an associative array
$result = $stmt->get_result();
$admin_list = array();
while ($row = $result->fetch_assoc()) {
$admin_list[] = $row;
}
// Close the statement and connection
$stmt->close();
$conn->close();
// Return the list of admins
return $admin_list;
}
/**
* Searches for users by name or phone number.
*
* @param string $search_query The search term (name or phone number).
* @return array An array of associative arrays representing users that match the search query.
*/
function search_user($search_query) {
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Prepare the SQL query to search for users by name or phone number
$search_query = '%' . $search_query . '%';
$stmt = $conn->prepare("SELECT * FROM users WHERE name LIKE ? OR phone_number LIKE ?");
$stmt->bind_param('ss', $search_query, $search_query);
$stmt->execute();
// Fetch the results into an associative array
$result = $stmt->get_result();
$user_list = array();
while ($row = $result->fetch_assoc()) {
$user_list[] = $row;
}
// Close the statement and connection
$stmt->close();
$conn->close();
// Return the list of users
return $user_list;
}
/**
* Adds a new admin to the database.
*
* @param string $username The new admin's username.
* @param string $password The new admin's password.
* @return string A message indicating whether the admin was added successfully or not.
*/
function add_admin($username, $password) {
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Hash the password before storing it
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
// Prepare the SQL statement to insert the new admin
$stmt = $conn->prepare("INSERT INTO admins (username, password) VALUES (?, ?)");
$stmt->bind_param('ss', $username, $hashed_password);
// Execute the statement and check if it was successful
if ($stmt->execute()) {
$message = "New admin added successfully.";
} else {
$message = "Error adding new admin: " . $stmt->error;
}
// Close the statement and connection
$stmt->close();
$conn->close();
return $message;
}
/**
* Removes an admin from the database.
*
* @param int $admin_id The ID of the admin to remove.
* @return string A message indicating whether the admin was removed successfully or not.
*/
function remove_admin($admin_id) {
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Prepare the SQL statement to delete the admin
$stmt = $conn->prepare("DELETE FROM admins WHERE id = ?");
$stmt->bind_param('i', $admin_id);
// Execute the statement and check if it was successful
if ($stmt->execute()) {
$message = "Admin removed successfully.";
} else {
$message = "Error removing admin: " . $stmt->error;
}
// Close the statement and connection
$stmt->close();
$conn->close();
return $message;
}
/**
* Retrieves details of a specific visit by its ID.
*
* @param int $visit_id The ID of the visit to retrieve.
* @return array|null An associative array representing the visit, or null if not found.
*/
function get_visit_details($visit_id) {
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Prepare the SQL query to select the visit by ID
$stmt = $conn->prepare("SELECT * FROM visit WHERE id = ?");
$stmt->bind_param('i', $visit_id);
$stmt->execute();
// Fetch the result into an associative array
$result = $stmt->get_result();
$visit_details = $result->fetch_assoc();
// Close the statement and connection
$stmt->close();
$conn->close();
// Return the visit details
return $visit_details;
}
/**
* Retrieves details of a specific payment by its ID.
*
* @param int $payment_id The ID of the payment to retrieve.
* @return array|null An associative array representing the payment, or null if not found.
*/
function get_payment_details($payment_id) {
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Prepare the SQL query to select the payment by ID
$stmt = $conn->prepare("SELECT * FROM payments WHERE payment_id = ?");
$stmt->bind_param('i', $payment_id);
$stmt->execute();
// Fetch the result into an associative array
$result = $stmt->get_result();
$payment_details = $result->fetch_assoc();
// Close the statement and connection
$stmt->close();
$conn->close();
// Return the payment details
return $payment_details;
}
/**
* Updates the details of a specific visit in the database.
*
* @param int $visit_id The ID of the visit to update.
* @param array $new_data An associative array of the new data to update (e.g., ['date' => '2024-08-21', 'description' => 'New Description']).
* @return string A message indicating whether the update was successful or not.
*/
function update_visit_details($visit_id, $new_data) {
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Prepare the SQL statement to update the visit details
$columns = '';
$values = [];
foreach ($new_data as $key => $value) {
$columns .= "$key = ?, ";
$values[] = $value;
}
$columns = rtrim($columns, ', ');
$values[] = $visit_id;
$stmt = $conn->prepare("UPDATE visit SET $columns WHERE id = ?");
$stmt->bind_param(str_repeat('s', count($new_data)) . 'i', ...$values);
// Execute the statement and check if it was successful
if ($stmt->execute()) {
$message = "Visit details updated successfully.";
} else {
$message = "Error updating visit details: " . $stmt->error;
}
// Close the statement and connection
$stmt->close();
$conn->close();
return $message;
}
/**
* Updates the details of a specific payment in the database.
*
* @param int $payment_id The ID of the payment to update.
* @param array $new_data An associative array of the new data to update (e.g., ['amount' => 100.00, 'status' => 'Completed']).
* @return string A message indicating whether the update was successful or not.
*/
function update_payment_details($payment_id, $new_data) {
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Prepare the SQL statement to update the payment details
$columns = '';
$values = [];
foreach ($new_data as $key => $value) {
$columns .= "$key = ?, ";
$values[] = $value;
}
$columns = rtrim($columns, ', ');
$values[] = $payment_id;
$stmt = $conn->prepare("UPDATE payments SET $columns WHERE payment_id = ?");
$stmt->bind_param(str_repeat('s', count($new_data)) . 'i', ...$values);
// Execute the statement and check if it was successful
if ($stmt->execute()) {
$message = "Payment details updated successfully.";
} else {
$message = "Error updating payment details: " . $stmt->error;
}
// Close the statement and connection
$stmt->close();
$conn->close();
return $message;
}
/**
* Exports the user data to a CSV file.
*
* @param string $filename The name of the CSV file to create.
* @return string A message indicating whether the export was successful or not.
*/
function export_users_to_csv($filename = 'users_export.csv') {
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Prepare the SQL query to select all users
$stmt = $conn->prepare("SELECT * FROM users");
$stmt->execute();
// Fetch the results
$result = $stmt->get_result();
// Open a file in write mode
$file = fopen($filename, 'w');
// Get the headers from the result set and write to the CSV file
$headers = array_keys($result->fetch_assoc());
fputcsv($file, $headers);
// Reset result pointer and write rows to the CSV file
$result->data_seek(0);
while ($row = $result->fetch_assoc()) {
fputcsv($file, $row);
}
// Close the file and statement
fclose($file);
$stmt->close();
$conn->close();
return "Users exported to $filename successfully.";
}
/**
* Sends a notification to users via phone_number or other messaging platform.
*
* @param array $user_ids An array of user IDs to send the notification to.
* @param string $message The message to send.
* @return string A message indicating whether the notifications were sent successfully or not.
*/
function send_notification($user_ids, $message) {
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Prepare the SQL statement to select the users by their IDs
$placeholders = implode(',', array_fill(0, count($user_ids), '?'));
$stmt = $conn->prepare("SELECT phone_number FROM users WHERE id IN ($placeholders)");
$stmt->bind_param(str_repeat('i', count($user_ids)), ...$user_ids);
$stmt->execute();
// Fetch the phone_numbers
$result = $stmt->get_result();
$phone_numbers = [];
while ($row = $result->fetch_assoc()) {
$phone_numbers[] = $row['phone_number'];
}
// Send the notification to each phone_number (this is a placeholder for the actual phone_number sending logic)
foreach ($phone_numbers as $phone_number) {
// mail($phone_number, 'Notification', $message); // Uncomment this line to actually send the phone_number
// You can use a third-party service like PHPMailer, SendGrid, etc.
}
// Close the statement and connection
$stmt->close();
$conn->close();
return "Notifications sent successfully.";
}
/**
* Deactivates a user account.
*
* @param int $user_id The ID of the user to deactivate.
* @return string A message indicating whether the account was deactivated successfully or not.
*/
function deactivate_user_account($user_id) {
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Prepare the SQL statement to deactivate the user account
$stmt = $conn->prepare("UPDATE users SET active = 0 WHERE id = ?");
$stmt->bind_param('i', $user_id);
// Execute the statement and check if it was successful
if ($stmt->execute()) {
$message = "User account deactivated successfully.";
} else {
$message = "Error deactivating user account: " . $stmt->error;
}
// Close the statement and connection
$stmt->close();
$conn->close();
return $message;
}
/**
* Reactivates a user account.
*
* @param int $user_id The ID of the user to reactivate.
* @return string A message indicating whether the account was reactivated successfully or not.
*/
function reactivate_user_account($user_id) {
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Prepare the SQL statement to reactivate the user account
$stmt = $conn->prepare("UPDATE users SET active = 1 WHERE id = ?");
$stmt->bind_param('i', $user_id);
// Execute the statement and check if it was successful
if ($stmt->execute()) {
$message = "User account reactivated successfully.";
} else {
$message = "Error reactivating user account: " . $stmt->error;
}
// Close the statement and connection
$stmt->close();
$conn->close();
return $message;
}
/**
* Retrieves the activity logs for a specific user.
*
* @param int $user_id The ID of the user whose activity logs are being retrieved.
* @return array An array of associative arrays representing the user's activity logs.
*/
function get_user_activity_logs($user_id) {
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Prepare the SQL query to select the user's activity logs
$stmt = $conn->prepare("SELECT * FROM activity_logs WHERE user_id = ?");
$stmt->bind_param('i', $user_id);
$stmt->execute();
// Fetch the results into an associative array
$result = $stmt->get_result();
$activity_logs = array();
while ($row = $result->fetch_assoc()) {
$activity_logs[] = $row;
}
// Close the statement and connection
$stmt->close();
$conn->close();
// Return the activity logs
return $activity_logs;
}
/**
* Allows an admin to write a medical prescription for a user (patient).
*
* @param int $admin_id The ID of the admin writing the prescription.
* @param int $user_id The ID of the user (patient) receiving the prescription.
* @param string $prescription_details The details of the prescription.
* @param string $notes Any additional notes regarding the prescription.
* @return string A message indicating the result of the prescription creation.
*/
function write_medical_prescription($admin_id, $user_id, $prescription_details, $notes = '') {
// Connect to the database
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Check for connection errors
if ($conn->connect_error) {
return "Connection failed: " . $conn->connect_error;
}
// Prepare and bind the SQL statement to insert the prescription
$stmt = $conn->prepare("INSERT INTO prescriptions (admin_id, user_id, prescription_details, notes, created_at) VALUES (?, ?, ?, ?, NOW())");
$stmt->bind_param('iiss', $admin_id, $user_id, $prescription_details, $notes);
// Check if the insertion was successful
if ($stmt->execute()) {
// Log the activity
log_user_activity($admin_id, 'Write Prescription', 'admin', "Admin wrote a prescription for user ID: $user_id.");
$message = "Prescription successfully created.";
} else {
$message = "Error writing prescription: " . $stmt->error;
}
// Close the statement and connection
$stmt->close();
$conn->close();
return $message;
}
?>