-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile-action-ajax.php
671 lines (554 loc) · 18.9 KB
/
profile-action-ajax.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
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
<?php
/**
* This file provides the "API" for handling profile actions (for both teachers and organisations).
* It is designed to be called using an AJAX POST request with variables passed in as a JSON string produced by the
* JavaScript function JSON.stringify(data).
*
* Items in uppercase without "" are constants in the constants.php file. On the right-hand side of => that is the string to pass in with javascript
*
* An action (ACTION => action) specifies the set of operations this is supposed to carry out.
* You can specifiy the specific operation by passing in an action parameter (ACTION_PARAM => action_param).
*
* This responds with either an error message or a JSON object with the variables success (true if succeeded, false if not)
* and a message (either the message representing the operation that succeeded or an error message);
*
* The following actions and action parameters are supported (with the success messages defined):
* CONNECT => connect - Process connections between two users
* ADD => add - Add a connection between these users. Returns "PENDING" if connection request is sent (with success true) or "ALREADY CONNECTED" (with success false) if users already have a connection (pending or accepted) (This is the default if no action_param provided)
* ACCEPT => accept - Updates an existing connection by updating the status pending to accepted. Returns "ACCEPTED" (with success true) if successful, or "NOT CONNECTED" if no connection entry exists
* REMOVE => remove - Removes a connection entry between these users. Returns "REMOVED" (with success true) when the connection is removed
* BLOCK => block - Processes blocking of users
* ADD => add - Add a new block for these users. Returns "BLOCKED" (with success true) when the block is added. Does not check if the blcok already exists. (This is the default if no action_param provided)
* REMOVE => remove - Removes an existing block if it exists. Returns "REMOVED" (with success true) when the block is removed.
* FOLLOW => follow - Processes following for a teacher to an organisation
* ADD => add - Adds a new follow entry for the teacher and organisation. Returns "FOLLOWED" (with success true) when the follow is added. Does not check if it already exists. (This is the default if no action_param provided)
* REMOVE => remove - Removes an existing follow if it exists. Returns "REMOVED" (with success true) when the follow is removed
*
* The other 2 parameters (SENDER => sender and DESTINATION => destination) determine the usernames of the 2 users that are interacting.
* For a connection, the destination is the user the sender user wants to connect with (or remove/accept etc.).
* For a block, the destination is the teacher to block and the sender is the teacher blocking them
* For a follow, the destination is the id of the organisation to follow and the sender is the teacher username that wants to follow them.
*
* An example request is as follows:
* var url = "profile-action-ajax.php";
* var data = {};
* data['action'] = "connect";
* data['sender'] = "user-wanting-to-connect";
* data['destination'] = "user-to-send-connection-request-to";
* data['action_param'] = "add";
*
* ajaxRequest.open("POST", url, true);
* var json = JSON.stringify(data);
* ajaxRequest.send(json);
*
* On success of that request, "PENDING" should be returned, meaning that the connection request was sent successfully
*/
require "database.php";
require "constants.php";
require "ajax.php";
require "notifications_utils.php";
$sender = "";
$destination = "";
$action = "";
$action_param = "";
/**
* Throw error
*/
function throwError() {
die("Script called with missing variables");
}
/**
* Parses any post parameters
*/
function profile_action_parsePOST() {
global $sender;
global $destination;
global $action;
global $action_param;
if (isset($_POST[ACTION])) {
$action = $_POST[ACTION];
} else {
throwError();
}
if (isset($_POST[ACTION_PARAM])) {
$action_param = $_POST[ACTION_PARAM];
}
if (isset($_POST[SENDER])) {
$sender = $_POST[SENDER];
} else {
throwError();
}
if (isset($_POST[DESTINATION])) {
$destination = $_POST[DESTINATION];
} else {
throwError();
}
}
/**
* Checks if the users are already connected or not
*/
function isConnected() {
global $sender;
global $destination;
global $conn;
$sql = "SELECT * FROM connections WHERE (destination = ? AND sender = ?) OR (destination = ? AND sender = ?) LIMIT 1;";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("ssss", $param_dest1, $param_send1, $param_dest2, $param_send2);
$param_dest1 = $destination;
$param_send1 = $sender;
$param_dest2 = $sender;
$param_send2 = $destination;
$connected = false;
if ($stmt->execute()) {
$stmt->store_result();
$connected = $stmt->num_rows == 1;
} else {
die("Database error: {$stmt->error}");
}
$stmt->close();
return $connected;
} else {
die("Database error: {$conn->error}");
}
return false;
}
/**
* Delete a connection
*/
function deleteConnection() {
global $conn;
global $destination;
global $sender;
$sql = "DELETE FROM connections WHERE destination = ? AND sender = ?;";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("ss", $param_destination, $param_sender);
$param_destination = $destination;
$param_sender = $sender;
if (!$stmt->execute()) {
die("Database error: {$stmt->error}");
}
$stmt->close();
respond(true, "REMOVED");
} else {
die("Database error: {$conn->error}");
}
}
/**
* Accept the provided connection
*/
function acceptConnection() {
if (isConnected()) {
global $conn;
global $destination;
global $sender;
$sql = "UPDATE connections SET status='accepted' WHERE destination = ? AND sender = ?;";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("ss", $param_destination, $param_sender);
$param_destination = $destination;
$param_sender = $sender;
if (!$stmt->execute()) {
die("Database error: {$stmt->error}");
}
$notification = new ConnectionAcceptedNotification($destination, $sender, false, "teacher_profile.php?username={$destination}", null);
addNotification($notification);
$stmt->close();
respond(true, "ACCEPTED");
exit;
} else {
die("Database error: {$conn->error}");
}
}
respond(false, "NOT CONNECTED");
}
/**
* Create the connection as a request
*/
function createConnection() {
if (!isConnected()) {
global $conn;
global $destination;
global $sender;
$sql = "INSERT INTO connections (destination, sender, status) VALUES (?, ?, 'pending');";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("ss", $param_destination, $param_sender);
$param_destination = $destination;
$param_sender = $sender;
if (!$stmt->execute()) {
die("Database error: {$stmt->error}");
}
$stmt->close();
$data = array('username' => $sender);
$url = "teacher_profile.php?" . http_build_query($data);
$notification = new ConnectionNotification($sender, $destination, false, $url, null);
addNotification($notification);
respond(true, "PENDING");
} else {
die("Database error: {$conn->error}");
}
}
respond(false, "ALREADY CONNECTED");
}
/**
* Processes the connection
*/
function processConnection() {
global $action_param;
if ($action_param == REMOVE) {
deleteConnection();
} else if ($action_param == ADD || empty($action_param)) {
createConnection();
} else if ($action_param == ACCEPT) {
acceptConnection();
} else {
die("Unsupported action_param provided: {$action_param}");
}
}
/**
* Deletes the block of this teacher
*/
function deleteBlock() {
global $conn;
global $destination;
global $sender;
$sql = "DELETE FROM blocked_teachers WHERE blocked_teacher = ? AND blocker = ?;";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("ss", $param_destination, $param_sender);
$param_destination = $destination;
$param_sender = $sender;
if (!$stmt->execute()) {
die("Database error: {$stmt->error}");
}
$stmt->close();
respond(true, "REMOVED");
} else {
die("Database error: {$conn->error}");
}
}
/**
* Adds the block to the databas
*/
function addBlock() {
global $conn;
global $destination;
global $sender;
$sql = "INSERT INTO blocked_teachers (blocked_teacher, blocker) VALUES (?, ?);";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("ss", $param_destination, $param_sender);
$param_destination = $destination;
$param_sender = $sender;
if (!$stmt->execute()) {
die("Database error: {$stmt->error}");
}
$stmt->close();
respond(true, "BLOCKED");
} else {
die("Database error: {$conn->error}");
}
}
/**
* Process blocking of a user
*/
function processBlock() {
global $action_param;
if ($action_param == REMOVE) {
deleteBlock();
} else if ($action_param == ADD || empty($action_param)) {
addBlock();
} else {
die("Unsupported action_param provided: {$action_param}");
}
}
/**
* Removes the follow
*/
function removeFollow() {
global $conn;
global $destination;
global $sender;
$sql = "DELETE FROM followed_organisations WHERE teacher_username = ? AND organisation_id = ?;";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("si", $param_user, $param_id);
$param_user = $sender;
$param_id = $destination;
if (!$stmt->execute()) {
die("Database error: {$stmt->error}");
}
$stmt->close();
respond(true, "REMOVED");
} else {
die("Database error: {$conn->error}");
}
}
/**
* Gets the teacher that followed the organisation
*/
function getTeacherFollowing($teacher_username) {
global $conn;
$sql = "SELECT username, first_name, last_name, headline, profile_photo FROM teachers WHERE username = ?;";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("s", $param_username);
$param_username = $teacher_username;
$data = array();
if ($stmt->execute()) {
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$data['username'] = $row['username'];
$data['first_name'] = $row['first_name'];
$data['last_name'] = $row['last_name'];
$data['headline'] = $row['headline'];
$data['profile_photo'] = $row['profile_photo'];
}
} else {
die("Database error: {$stmt->error}");
}
$stmt->close();
return $data;
} else {
die("Database error: {$conn->error}");
}
}
/**
* Get the ussername of the organisation
*/
function getOrganisationUsername($organisation_id) {
global $conn;
$sql = "SELECT username FROM organisations WHERE organisation_id = ?;";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("i", $param_id);
$param_id = $organisation_id;
$username = null;
if ($stmt->execute()) {
$result = $stmt->get_result();
$row = $result->fetch_assoc();
if ($row) {
$username = $row['username'];
}
} else {
die("Database error: {$stmt->error}");
}
$stmt->close();
return $username;
} else {
die("Database error: {$conn->error}");
}
}
/**
* Adds a new follow
*/
function addFollow() {
global $conn;
global $destination;
global $sender;
$sql = "INSERT INTO followed_organisations (teacher_username, organisation_id) VALUES (?, ?);";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("si", $param_user, $param_id);
$param_user = $sender;
$param_id = $destination;
if (!$stmt->execute()) {
die("Database error: {$stmt->error}");
}
$org_username = getOrganisationUsername($destination);
if ($org_username != null) {
$notification = new FollowNotification($sender, $org_username, false, "teacher_profile.php?username={$sender}", null);
addNotification($notification);
}
$stmt->close();
$data = getTeacherFollowing($sender);
respondData(true, "FOLLOWED", $data);
} else {
die("Database error: {$conn->error}");
}
}
/**
* Process the following of an organisation
*/
function processFollow() {
global $action_param;
if ($action_param == REMOVE) {
removeFollow();
} else if ($action_param == ADD || empty($action_param)) {
addFollow();
} else {
die("Unsupported action_param provided: {$action_param}");
}
}
/**
* After insert, we update the target url
*/
function updateInviteTargetURL($invitation_id) {
global $conn;
$sql = "UPDATE notifications SET target_link = ? WHERE id = ?;";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("si", $param_link, $param_id);
$data = array('invitation_id' => $invitation_id);
$param_link = "organisation_invites.php?" . http_build_query($data);
$param_id = $invitation_id;
$stmt->execute();
$stmt->close();
}
}
/**
* Process the addition of the invite to the database
*/
function processAddOrganisationInvite() {
global $sender;
global $destination;
global $conn;
$notification = new OrgInviteNotification($sender, $destination, false, "organisation_invites.php", null);
$sql = "INSERT INTO notifications (username, sender, type, target_link) VALUES (?, ?, ?, ?);";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("ssss", $param_username, $param_sender, $param_type, $param_target);
$param_username = $notification->getReceiver();
$param_sender = $notification->getSender();
$param_type = $notification->getType();
$param_target = $notification->getTarget_link();
if (!$stmt->execute()) {
die("Database error: {$stmt->error}");
}
$id = $stmt->insert_id;
updateInviteTargetURL($id);
$stmt->close();
} else {
die("Database error: {$conn->error}");
}
respond(true, "INVITED");
}
/**
* Remove the invite from the database
*/
function removeInvite() {
global $conn;
if (isset($_POST[INVITATION_ID])) {
$invitation_id = $_POST[INVITATION_ID];
$sql = "DELETE FROM notifications WHERE id = ?;";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("i", $param_id);
$param_id = $invitation_id;
if (!$stmt->execute()) {
die("Database error: {$stmt->error}");
}
} else {
die("Database error: {$conn->error}");
}
$stmt->close();
} else {
die("When accepting/rejecting organisation invites, an INVITATION_ID needs to be provided");
}
}
/**
* Get the id of the organisation sender
*/
function getOrganisationId($sender) {
global $conn;
$sql = "SELECT organisation_id FROM organisations WHERE username = ?;";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("s", $param_username);
$param_username = $sender;
$organisation_id = -1;
if ($stmt->execute()) {
$result = $stmt->get_result();
if ($row = $result->fetch_assoc()) {
$organisation_id = $row['organisation_id'];
}
} else {
die("Database error: {$stmt->error}");
}
$stmt->close();
return $organisation_id;
} else {
die("Database error: {$conn->error}");
}
}
/**
* If the user already has a current organisation, it is deleted
*/
function deleteExistingOrganisation() {
global $conn;
global $destination;
$sql = "DELETE FROM organisation_members WHERE teacher_username = ?;";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("s", $param_username);
$param_username = $destination;
if (!$stmt->execute()) {
die("Database error: {$stmt->error}");
}
$stmt->close();
} else {
die("Database error: {$conn->error}");
}
}
/**
* Process the acceptance of an organisation invite
*/
function processAcceptOrganisationInvite() {
global $sender;
global $destination;
global $conn;
$organisation_id = getOrganisationId($sender);
if ($organisation_id != -1) {
deleteExistingOrganisation();
$sql = "INSERT INTO organisation_members (teacher_username, organisation_id) VALUES (?, ?);";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("si", $param_username, $param_id);
$param_username = $destination;
$param_id = $organisation_id;
if (!$stmt->execute()) {
die("Database error: {$stmt->error}");
}
$stmt->close();
removeInvite();
respond(true, "ACCEPTED");
} else {
die("Database error: {$conn->error}");
}
} else {
respond(false, "Organisation with username {$sender} does not exist");
}
}
function processRejectOrganisationInvite() {
removeInvite();
respond(true, "REMOVED");
}
/**
* This AJAX script has been modified to allow use with organisation invites to teachers.
* While not strictly an action related to the profile, the invitation process starts at the teacher's
* profile page. The result of the invitation is also displayed on the profile so, as well as the create
* invitation functionality, the accepting and rejection of them have been added here too
*/
/**
* Send an organisation invite to the destination user
*/
function processOrganisationInvite() {
global $action_param;
if ($action_param == ADD) {
processAddOrganisationInvite();
} else if ($action_param == ACCEPT) {
processAcceptOrganisationInvite();
} else if ($action_param == REMOVE) {
processRejectOrganisationInvite();
} else {
die("Unsupported action_param provided: {$action_param}");
}
}
/**
* Process the requested action
*/
function process() {
global $action;
if ($action == CONNECT) {
processConnection();
} else if ($action == BLOCK) {
processBlock();
} else if ($action == FOLLOW) {
processFollow();
} else if ($action == ORGANISATION_INVITE) {
processOrganisationInvite();
} else {
die("Unsupported action provided: {$action}");
}
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
profile_action_parsePOST();
process();
}
?>