-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages.php
199 lines (137 loc) · 5.67 KB
/
messages.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
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
<?php
/**
* Created by PhpStorm.
* User: Shalitha Suranga
* Date: 4/20/2017
* Time: 6:51 AM
*/
require("includes/config.php");
header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json');
if(isset($_GET["token"])){
if($_GET["token"]==AP_TOKEN){
/* --------------- list degrees ------------ */
if(isset($_GET["getmessagethreads"])) {
$lecturer_id=$db->real_escape_string($_GET["lecturer"]);
$result = $db->query("
SELECT
COUNT(sm_message.id) AS messagecount,
CONCAT(sm_student.firstname,' ',sm_student.lastname) AS studentname,
sm_student.id
FROM
sm_message
INNER JOIN sm_student ON sm_message.from_user=sm_student.id
WHERE sm_message.to_user=$lecturer_id
GROUP BY sm_message.from_user
ORDER BY messagecount DESC
");
$result2 = $db->query("
SELECT
COUNT(sm_message.id) AS messagecount,
sm_student.id
FROM
sm_message
INNER JOIN sm_student ON sm_message.from_user=sm_student.id
WHERE sm_message.to_user=$lecturer_id AND sm_message.is_open='0'
GROUP BY sm_message.from_user
");
$info1=array();
$info2=array();
while($node=$result->fetch_assoc()) {
$info1[]=$node;
}
while($node=$result2->fetch_assoc()) {
$info2[$node["id"]]=$node["messagecount"];
}
echo(json_encode(array(
"total"=>$info1,
"unread"=>$info2
)));
}
/* --------------- load message thread ------------ */
if(isset($_GET["getmessagethread"])) {
$lecturer_id=$db->real_escape_string($_GET["lecturer"]);
$student_id=$db->real_escape_string($_GET["student"]);
$offset=$db->real_escape_string($_GET["offset"]);
$limit=10;
$result = $db->query("
SELECT
sm_message.*
FROM
sm_message
WHERE ((sm_message.from_user=$lecturer_id AND sm_message.to_user='$student_id')
OR (sm_message.to_user=$lecturer_id AND sm_message.from_user='$student_id'))
ORDER BY id DESC LIMIT $limit OFFSET $offset
");
if ($result->num_rows) {
$info=array();
$ids="";
while($node=$result->fetch_assoc()) {
$info[]=$node;
$ids.="'$node[id]',";
}
$ids=preg_replace("/,$/","",$ids);
$db->query("UPDATE sm_message SET is_open=1 WHERE id IN($ids) AND ((sm_message.from_user=$lecturer_id AND sm_message.to_user='$student_id')
OR (sm_message.to_user=$lecturer_id AND sm_message.from_user='$student_id')) AND
sm_message.is_open='0'");
echo(json_encode($info));
} else {
echo(json_encode(array(
"error" => "notfound"
)));
}
}
/* --------------- incoming messages ------------ */
if(isset($_GET["getmessagethreadincoming"])) {
$lecturer_id=$db->real_escape_string($_GET["lecturer"]);
$student_id=$db->real_escape_string($_GET["student"]);
$timestamp=$db->real_escape_string($_GET["timestamp"]);
$result = $db->query("
SELECT
sm_message.*
FROM
sm_message
WHERE
(sm_message.to_user=$lecturer_id AND sm_message.from_user='$student_id') AND
sm_message.is_open='0' AND id>'$timestamp'
ORDER BY id DESC
");
if ($result->num_rows) {
$info=array();
$ids="";
while($node=$result->fetch_assoc()){
$info[]=$node;
$ids.="'$node[id]',";
}
$ids=preg_replace("/,$/","",$ids);
$db->query("UPDATE sm_message SET is_open=1 WHERE id IN($ids) AND
(sm_message.to_user=$lecturer_id AND sm_message.from_user='$student_id') AND
sm_message.is_open='0'");
echo(json_encode($info));
} else {
echo(json_encode(array(
"error" => "notfound"
)));
}
}
/* --------------- add message ------------ */
if(isset($_GET["addmessage"])) {
$lecturer_id=$db->real_escape_string($_GET["lecturer"]);
$student_id=$db->real_escape_string($_GET["student"]);
$message=$db->real_escape_string($_GET["message"]);
$result = $db->query("
INSERT INTO sm_message(id, from_user,to_user,content,is_open)
VALUES('".date("Y-m-d H:i:s")."','$lecturer_id','$student_id','$message',0);
");
if ($result) {
echo(json_encode(array(
"success" => $db->insert_id
)));
} else {
echo(json_encode(array(
"error" => "notfound"
)));
}
}
}
}