-
Notifications
You must be signed in to change notification settings - Fork 0
/
friending_functions.php
151 lines (129 loc) · 6.97 KB
/
friending_functions.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
<?php
function load_friend_list($user_accountID,$conn){
$query_friend_list = "SELECT Account.* FROM Account WHERE (Account.accountID in (SELECT Friendship.friend1ID FROM Friendship WHERE Friendship.friend2ID = '$user_accountID')) OR (Account.accountID in (SELECT Friendship.friend2ID FROM Friendship WHERE Friendship.friend1ID = '$user_accountID'))";
$result = mysqli_query($conn,$query_friend_list);
return $result;
}
function search_by_name($name,$conn){
$search_by_name = "SELECT * FROM Account WHERE name LIKE '%$name%'";
$result = mysqli_query($conn,$search_by_name);
return $result;
}
function search_by_age($age,$conn){
$search_by_age = "SELECT * FROM Account WHERE age = '$age'";
$result = mysqli_query($conn,$search_by_age);
return $result;
}
function search_by_city($city,$conn){
$search_by_city = "SELECT * FROM Account WHERE city LIKE '%$city%'";
$result = mysqli_query($conn,$search_by_city);
return $result;
}
function search_by_country($country,$conn){
$search_by_country = "SELECT * FROM Account WHERE country LIKE '%$country%'";
$result = mysqli_query($conn,$search_by_country);
return $result;
}
function search_by_email($email,$conn){
$search_by_email = "SELECT * FROM Account WHERE email_address = '$email'";
$result = mysqli_query($conn,$search_by_email);
return $result;
}
function search_by_friend($friend_ID,$conn){
$query_search_by_friend = "SELECT * FROM Account WHERE accountID in (SELECT friend2ID FROM Friendship WHERE friend1ID = '$friend_ID' UNION SELECT friend1ID FROM Friendship WHERE friend2ID = '$friend_ID')";
$result = mysqli_query($conn,$query_search_by_friend);
return $result;
}
function delete_friend($user_accountID,$friend_accountID,$conn){
$query_remove_friend = "DELETE FROM Friendship WHERE (friend2ID = '$user_accountID' AND friend1ID = '$friend_accountID') OR (friend2ID = '$friend_accountID' AND friend1ID = '$user_accountID')";
mysqli_query($conn,$query_remove_friend);
}
function send_invitation($user_accountID,$friend_ID,$conn){
$query_send_invitation = "INSERT INTO Invitation (accountID, inviteeID) VALUES ($user_accountID,$friend_ID)";
$result = mysqli_query($conn,$query_send_invitation);
return $result;
}
function load_friend_invitation($user_accountID,$conn){
$query_friend_invitation = "SELECT Account.* FROM Account WHERE (Account.accountID in (SELECT Invitation.accountID FROM Invitation WHERE Invitation.inviteeID = '$user_accountID')) ";
$result = mysqli_query($conn,$query_friend_invitation);
return $result;
}
function load_sent_friend_invitation($user_accountID,$conn){
$query_sent_friend_invitation = "SELECT Account.* FROM Account WHERE Account.accountID in (SELECT Invitation.inviteeID FROM Invitation WHERE Invitation.accountID = '$user_accountID') ";
$result = mysqli_query($conn,$query_sent_friend_invitation);
return $result;
}
function accept_invitation($user_accountID,$friend_ID,$conn){
$query_accept_invitation = "INSERT INTO Friendship (friend1ID, friend2ID) VALUES ('$user_accountID','$friend_ID')";
mysqli_query($conn,$query_accept_invitation);
delete_invitation($friend_ID,$user_accountID,$conn);
}
function delete_invitation($user_accountID,$friend_ID,$conn){
$query_delete_invitation = "DELETE FROM Invitation WHERE accountID = '$user_accountID' AND inviteeID = '$friend_ID'";
mysqli_query($conn,$query_delete_invitation);
}
function reject_invitation($user_accountID,$friend_ID,$conn){
$query_reject_invitation = "UPDATE Invitation SET isRejected = 1 WHERE accountID = '$friend_ID' AND inviteeID = '$user_accountID'";
mysqli_query($conn,$query_reject_invitation);
}
function check_inv_status($user_accountID,$friend_ID,$conn){
$query_check_inv_status = "SELECT * FROM Invitation WHERE accountID = '$friend_ID' AND inviteeID = '$user_accountID'";
$result = mysqli_fetch_assoc(mysqli_query($conn,$query_check_inv_status))['isRejected'];
return $result;
}
function check_privacy_status($friend_ID,$conn){
$query_check_privacy_status = "SELECT * FROM Account WHERE accountID = '$friend_ID'";
$result = mysqli_fetch_assoc(mysqli_query($conn,$query_check_privacy_status))['privacy_setting'];
return $result;
}
function search_by_ID($friend_ID,$conn){
$query_get_name = "SELECT * FROM Account WHERE accountID = '$friend_ID'";
$result = mysqli_query($conn,$query_get_name);
return $result;
}
function get_recommendations($user_accountID,$conn){
$query_get_reco = "SELECT * FROM Account WHERE accountID IN ( SELECT accountID FROM (SELECT accountID,reason FROM Recommendation WHERE recommendeeID = $user_accountID UNION SELECT recommendeeID,reason FROM Recommendation WHERE accountID = $user_accountID ORDER BY reason) as temp) LIMIT 5";
$result = mysqli_query($conn,$query_get_reco);
return $result;
}
function delete_recommendations($user_accountID,$conn){
$query_delete_reco = "DELETE FROM Recommendation WHERE accountID = $user_accountID OR recommendeeID = $user_accountID";
mysqli_query($conn,$query_delete_reco);
}
function save_recommendations($user_email,$conn){
$user_accountID = mysqli_fetch_assoc(search_by_email($user_email,$conn))['accountID'];
$user_age = mysqli_fetch_assoc(search_by_ID($user_accountID,$conn))['age'];
$user_city = mysqli_fetch_assoc(search_by_ID($user_accountID,$conn))['city'];
$user_country = mysqli_fetch_assoc(search_by_ID($user_accountID,$conn))['country'];
$query_get_all_user_ID = "SELECT accountID from Account WHERE accountID !='$user_accountID'";
$load_all_userID = mysqli_query($conn,$query_get_all_user_ID);
while ($row = mysqli_fetch_assoc($load_all_userID)){
$temp = $row['accountID'];
calculate_reco_points($user_accountID,$temp,$user_age,$user_city,$user_country,$conn);
}
}
function calculate_reco_points($login_user_ID,$user_accountID,$age,$city,$country,$conn){
$points = 0;
$user_age = mysqli_fetch_assoc(search_by_ID($user_accountID,$conn))['age'];
$user_city = mysqli_fetch_assoc(search_by_ID($user_accountID,$conn))['city'];
$user_country = mysqli_fetch_assoc(search_by_ID($user_accountID,$conn))['country'];
$age_diff = $user_age-$age;
if ($age_diff<=3||$age_diff<=-3){
$points += 40;
} else if ($age_diff<=6||$age_diff>=-6){
$points += 20;
} else if ($age_diff<=10||$age_diff>=-10){
$points += 10;
}
if ($user_city==$city){
$points += 40;
}
if ($user_country==$country){
$points += 20;
}
$query_insert_reco = "INSERT INTO Recommendation (accountID, recommendeeID, reason) VALUES ($login_user_ID,$user_accountID,$points)";
if ($points>=60){
mysqli_query($conn,$query_insert_reco);
}
}
?>