-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase_function.php
241 lines (176 loc) · 8.91 KB
/
database_function.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
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
<?php
session_start();
include "connect.php";
if($_GET['function'] == "getEvents"){ // when we want to get all the events
$query = 'SELECT * FROM events';
if($_POST["userFilter"] != null){
if($_POST["type"] != null){
$query = $query.' WHERE userid = ' . $_POST["userFilter"] .' AND deleted_at IS NULL AND typeid = '.$_POST["type"];
}else{
$query = $query.' WHERE userid = ' . $_POST["userFilter"] .' AND deleted_at IS NULL';
}
} else {
if($_POST["type"] != null){
$query = $query.' WHERE deleted_at IS NULL AND typeid = '.$_POST["type"];
}else{
$query = $query.' WHERE deleted_at IS NULL';
}
}
$stmt = $con->prepare($query);
$stmt->execute();
$res = $stmt->fetchAll();
echo json_encode($res);
}else if($_GET['function'] == "createEvent"){ // when we want to create an event
$stmt = $con->prepare('INSERT INTO events (title, description, latitude, longitude, userid, typeid) VALUES (:title, :description, :latitude, :longitude, :userid, :typeId)');
$stmt->execute(array(
":title" => $_POST["title"],
":description" => $_POST["description"],
":latitude" => $_POST["latitude"],
":longitude" => $_POST["longitude"],
":userid" => $_SESSION["userid"],
":typeId" => $_POST["typeId"]
));
}else if($_GET['function'] == "getEvent"){ // when we want to get a specific event
$stmt = $con->prepare('SELECT events.*, user.username, user.verifiedeventcount FROM events JOIN user ON events.userid = user.userid WHERE eventid = ?');
$stmt->execute(array($_GET["eventId"]));
$res = $stmt->fetch();
echo json_encode($res);
}else if($_GET['function'] == "checkVoting"){ // when we want to check for the user's voting status
$stmt = $con->prepare("SELECT * FROM voting WHERE userid = ? AND eventid = ?");
$stmt->execute(array($_SESSION['userid'], $_GET['id']));
$count = $stmt->rowCount();
echo $count;
}else if($_GET['function'] == "incVote"){ // increasing the voting of a specific event
$stmt = $con->prepare("SELECT votes, userid, verified FROM events WHERE eventid = ?");
$stmt->execute(array($_GET["id"]));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$count = $stmt->rowCount();
if($count > 0){
$votes = $row[votes];
$userid = $row[userid];
$verified = $row[verified];
$votes++;
}
if($votes >= 5 && !$verified){
$stmt = $con->prepare("UPDATE events SET votes = ?, verified = true WHERE eventid = ?");
} else {
$stmt = $con->prepare("UPDATE events SET votes = ? WHERE eventid = ?");
}
$stmt->execute(array($votes, $_GET["id"]));
$stmt = $con->prepare("INSERT INTO voting (userid, eventid) VALUES (:userid, :eventid)");
$stmt->execute(array(
":userid" => $_SESSION["userid"],
":eventid" => $_GET["id"]
));
if($votes >= 5 && !$verified){
$stmt = $con->prepare("SELECT verifiedeventcount from user where userid = ?");
$stmt->execute(array($userid));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$count = $stmt->rowCount();
if($count > 0){
$verifiedeventcount = $row[verifiedeventcount];
$verifiedeventcount++;
}
$stmt = $con->prepare("UPDATE user SET verifiedeventcount = ? WHERE userid = ?");
$stmt->execute(array($verifiedeventcount, $userid));
}
}else if($_GET['function'] == "decVote"){ // decreasing the votes of a specific event
$stmt = $con->prepare("SELECT votes FROM events WHERE eventid = ?");
$stmt->execute(array($_GET["id"]));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$count = $stmt->rowCount();
if($count > 0){
$votes = $row[votes];
$votes--;
}
$stmt = $con->prepare("UPDATE events SET votes = ? WHERE eventid = ?");
$stmt->execute(array($votes, $_GET["id"]));
$stmt = $con->prepare("INSERT INTO voting (userid, eventid) VALUES (:userid, :eventid)");
$stmt->execute(array(
":userid" => $_SESSION["userid"],
":eventid" => $_GET["id"]
));
}else if($_GET['function'] == "editEvent"){ // when we want to update and edit the information of an event
$stmt = $con->prepare('UPDATE events SET title = ?, description = ?, typeid = ? WHERE eventid = ?');
$stmt->execute(array(
$_POST["title"],
$_POST["description"],
$_POST["typeid"],
$_POST["eventid"]
));
}else if($_GET['function'] == "deleteEvent"){ // when we want to delete an event
$stmt = $con->prepare('UPDATE events SET deleted_at = ? WHERE eventid = ?');
$stmt->execute(array(date("Y-m-d H:i:s"), $_POST["eventid"]));
}else if($_GET['function'] == "setDuration"){ // when we want to set the duration of an event
$stmt = $con->prepare("UPDATE events SET duration = ? WHERE eventid = ? AND duration IS NULL");
$stmt->execute(array($_GET["duration"], $_GET["eventid"]));
}else if($_GET['function'] == "setEndDate"){ // when we want to set the endDate of an event
$stmt = $con->prepare("UPDATE events SET endDate = ? WHERE eventid = ? AND deleted_at IS NULL");
$stmt->execute(array(
$_GET["endDate"],
$_GET["eventid"]
));
}else if($_GET['function'] == "getDuration"){ // when we want to get the duration specified for an event
$duration = 0;
$stmt = $con->prepare("SELECT duration FROM events WHERE eventid = ? AND duration IS NOT NULL");
$stmt->execute(array($_GET["eventid"]));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$count = $stmt->rowCount();
if($count > 0){
$duration = $row["duration"];
}
echo $duration;
}else if($_GET['function'] == "getEndDate"){ // when we want to get the endDate specified for an event
$endDate = 0;
$stmt = $con->prepare("SELECT endDate FROM events WHERE eventid = ? AND endDate IS NOT NULL AND deleted_at IS NULL");
$stmt->execute(array($_GET["eventid"]));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$count = $stmt->rowCount();
if($count > 0){
$endDate = $row["endDate"];
}
echo $endDate;
}else if($_GET['function'] == "getUserInfo"){ // when we want to get the user's information
$stmt = $con->prepare("SELECT * FROM user WHERE userid = ?");
$stmt->execute(array($_SESSION["userid"]));
$count = $stmt->rowCount();
if($count > 0){
$row = $stmt->fetch(PDO::FETCH_ASSOC);
}
echo json_encode($row);
}else if($_GET['function'] == "checkUsername"){ // when we want to check the availability of the user's username
$user = $_POST["username"];
$stmt = $con->prepare('SELECT username FROM user WHERE username = ?');
$stmt->execute(array($user));
$count = $stmt->rowCount();
echo $count;
}else if($_GET['function'] == "editUserInfo"){ // when we want to edit the user's information
$username = $_POST["username"];
$password = sha1($_POST["password"]);
$email = $_POST["email"];
$name = $_POST["name"];
$stmt = $con->prepare("UPDATE user SET username = ?, password = ?, email = ?, name = ? WHERE userid = ?");
$stmt->execute(array(
$username,
$password,
$email,
$name,
$_SESSION["userid"]
));
}else if($_GET['function'] == "update_endDate_duration"){ // when an event changes types (event or question) make sure to update the event
$stmt = $con->prepare("UPDATE events SET duration = NULL, endDate = NULL WHERE eventid = ?");
$stmt->execute(array($_GET["eventId"]));
}else if($_GET['function'] == "answerQuestion"){
$stmt = $con->prepare('INSERT INTO answers (userid, eventid, answertext) VALUES (:userid, :eventid, :answertext)');
$stmt->execute(array(
":userid" => $_SESSION["userid"],
":eventid" => $_POST["eventid"],
":answertext" => $_POST["answertext"]
));
}else if($_GET["function"] == "getAnswers"){
$query = 'SELECT answers.*, user.username FROM answers JOIN user ON answers.userid = user.userid WHERE eventid = ' . $_GET['eventid'];
$stmt = $con->prepare($query);
$stmt->execute();
$res = $stmt->fetchAll();
echo json_encode($res);
}