-
Notifications
You must be signed in to change notification settings - Fork 0
/
getMessageNotification.php
86 lines (77 loc) · 2.88 KB
/
getMessageNotification.php
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
<?php
session_start();
require_once("config-students.php");
require_once("config-teachers.php");
if (isset($_SESSION['userlogin'])) {
$myUser = $_SESSION['userlogin']['id'];
$myEmail = $_SESSION['userlogin']['email'];
$name = $_SESSION['userlogin']['name'];
$surname = $_SESSION['userlogin']['surname'];
$teacher_list = 'none';
$student_list = 'none';
$all_messages = 'none';
if ($_SESSION['userlogin']['type'] == 'teacher'){
$sql = "SELECT * FROM teachers where email != '$myEmail'";
$stmt = $db->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($result) {
$teacher_list = $result;
} else {
echo '';
}
$sql = "SELECT * FROM messages WHERE (FIND_IN_SET(:userid, reciever_list) OR FIND_IN_SET('all_teachers', reciever_list)) ORDER BY id ASC";
$stmt = $db->prepare($sql);
$stmt->bindParam(':userid', $myUser, PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
} else if ($_SESSION['userlogin']['type'] == 'student'){
$sql = "SELECT * FROM students where email != '$myEmail'";
$stmt = $db->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($result) {
$student_list = $result;
} else {
echo '';
}
$sql = "SELECT * FROM messages WHERE (FIND_IN_SET(:userid, reciever_list) OR FIND_IN_SET('all_students', reciever_list)) ORDER BY id ASC";
$stmt = $db->prepare($sql);
$stmt->bindParam(':userid', $myUser, PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
if ($result) {
// The current ID exists in the receiver list
$last_message = end($result);
} else {
// The current ID does not exist in the receiver list
echo '';
}
$current_time = date("Y-m-d H:i:s");
$current_hour = date("H", strtotime($current_time));
$current_minute = date("i", strtotime($current_time));
if (isset($last_message)){
$hour_of_last_message = date("H", strtotime($last_message['time']));
$minute_of_last_message = date("i", strtotime($last_message['time']));
}
if (isset($last_message) && $current_hour == $hour_of_last_message && $current_minute == $minute_of_last_message) {
// Prepare the response as an associative array
$response = array(
'sender_name' => $last_message['sender_name']
);
} else {
$response = array(
'sender_name' => 'none'
);
}
// Check if it's an AJAX request
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') {
// Set the response content type as JSON
header('Content-Type: application/json');
// Return the response as JSON
echo json_encode($response);
exit(); // Terminate the script
}
?>