forked from pietrosperoni/Vilfredo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendorse_confused_or_not.php
259 lines (220 loc) · 6.61 KB
/
endorse_confused_or_not.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
<?php
include('header.php');
//set_log(__FILE__." called....");
//set_log($_POST);
//print_array($_POST);
if (isset($_POST['proposal']) and isset($_POST['prev_proposal']))
{
foreach ($_POST['proposal'] as $key => $value)
{
if ($_POST['proposal'][$key] != $_POST['prev_proposal'][$key])
{
set_log("Vote for $key changed...");
$_SESSION["updatemap"] = true;
break;
}
}
}
$question = fetchValidIntValFromPostWithKey('question');
if ($question === false)
{
header("Location: error_page.php");
exit;
}
//set_log("Question $question");
if (IsQuestionWriting($question))
{
//set_message("user", "Sorry, question $question now in writing stage.");
//header("Location: messagepage.php?q=$question");
header("Location: viewquestion.php?q=$question");
exit;
}
// User is anonymous if anon checkbox has been clicked (is defined)
$is_anon = isset($_POST['anon']);
$generation = (int)$_POST['generation'];
$userid=isloggedin();
if ($is_anon)
{
//set_log("Form submitted anonymously");
// userid should be false
if ($userid)
{
set_log(" User $userid submitted anonymously whilst logged in!");
}
}
$is_subscribe = isset($_POST['subscribe']);
if ($userid) {
$sql = "SELECT * FROM updates WHERE question = ".$question." AND user = ".$userid." LIMIT 1 ";
$response = mysql_query($sql);
$row = mysql_fetch_array($response);
if ($row)
{
if(!$is_subscribe)
{
$sql = "DELETE FROM updates WHERE updates.question = " . $question . " AND updates.user = " . $userid . " ";
if (!mysql_query($sql)) error("Database update failed");
}
}
else
{
if($is_subscribe)
{
$how="asap";
$sql = 'INSERT INTO `updates` (`user`, `question`, `how`) VALUES (\'' . $userid . '\', \'' . $question . '\', \'' . $how . '\');';
if (!mysql_query($sql)) error("Database update failed");
}
}
}
$roundid;
$sql2 = "SELECT roundid FROM questions WHERE id = ".$question." LIMIT 1 ";
$response2 = mysql_query($sql2);
while ($row2 = mysql_fetch_array($response2))
{
$roundid = $row2[0];
}
DeleteGraph($question,$roundid);
$nEndorsers=CountEndorsers($question,$roundid);
$allproposals = getCurrentProposalIDs($question, $roundid);
$origids = getOriginalIDs($allproposals);
//set_log('$origids');
//set_log($origids);
if ($is_anon)
{
//$userid = getAnonymousUserForVoting($allproposals);
$userid = getAnonymousUser($question);
//$wait = getDelayForRemoteIP();
//set_log("Delay for this user should be $wait seconds");
//logUser($userid);
}
if (!$userid)
{
set_log("Error: Could not create anonymous user!");
}
$currentuserendorsements = getUserEndorsedFromList($userid, $allproposals);
//set_log("currentuserendorsements");
//set_log($currentuserendorsements);
$user_comment = (isset($_POST['user_comment'])) ? $_POST['user_comment'] : array();
$select_comment = (isset($_POST['select_comment'])) ? $_POST['select_comment'] : array();
$prev_proposal = (isset($_POST['prev_proposal'])) ? $_POST['prev_proposal'] : array();
$endorsedproposals = (isset($_POST['proposal'])) ? $_POST['proposal'] : array();
$prev_commentid = (isset($_POST['prev_commentid'])) ? $_POST['prev_commentid'] : array();
// Set endorse, oppoed and comments
//
foreach ($allproposals as $p)
{
//set_log("Processing proposal $p....");
if
(
( isset($prev_proposal[$p]) && ($endorsedproposals[$p] != $prev_proposal[$p]) ) ||
( isset($user_comment[$p]) && empty($user_comment[$p]) == false ) ||
( isset($prev_commentid[$p]) && isset($select_comment[$p]) && $prev_commentid[$p] != $select_comment[$p] ) ||
( isset($select_comment[$p]) )
)
{
//
// User endorses
//
//set_log("prev_proposal is " . $prev_proposal[$p]);
if ($endorsedproposals[$p] == "1" && !in_array($p, $currentuserendorsements))
{
//set_log("Adding endorsement for proposal $p...");
addEndorsement($userid, $p);
if ($prev_proposal[$p] == '2' || $prev_proposal[$p] == '3')
{ // Delete previous oppose entry
//set_log("Delete previous oppose entry for proposal $p...");
deleteUserOppose($userid, $p, $generation);
// Delete comment if no-one else supports it
if (isset($prev_commentid[$p]))
{
//set_log("Try to delete comment {$prev_commentid[$p]} for proposal $p...");
deleteComment((int)$prev_commentid[$p]);
}
}
}
// User Opposes
elseif ($endorsedproposals[$p] == "2" || $endorsedproposals[$p] == "3")
{
//set_log("User added or changed Oppose and/or Comment...");
if (in_array($p, $currentuserendorsements))
{
deleteEndorsement($userid, $p);
}
// Set oppose type
$type;
if ($endorsedproposals[$p] == "2")
{
$type = 'dislike';
}
else
{
$type = 'confused';
}
$type_change = $type != $prev_proposal[$p];
// Check for comment
// ( No comment set to 0 )
$previous_commentid = (int)$prev_commentid[$p];
$commentid = 0;
if (array_key_exists($p, $select_comment))
{
$commentid = (int)$select_comment[$p];
}
elseif (array_key_exists($p, $user_comment) && !empty($user_comment[$p]))
{
$new_comment = $user_comment[$p];
$commentid = commentExists($p, $generation, $type, $new_comment);
//set_log("ID of comment search = $commentid");
if (!$commentid)
{
//set_log("Adding new comment {$user_comment[$p]}");
//set_log("New comment not found - adding to comments for prop ID $p and origid {$origids[$p]}");
$commentid = addComment($userid, $p, $type, $generation, $user_comment[$p], $origids[$p]); // addorigid
}
}
//set_log("Set user oppose: ");
// Add new oppose entry or update type and commentid of existing one
setUserOppose($userid, $p, $type, $generation, $origids[$p], $commentid); // addorigid
if ($commentid != $previous_commentid)
{
// Delete comment if no-one else supports it
//set_log("comment id = $commentid and previous comment id = {$previous_commentid} - try and delete comment...");
deleteComment($previous_commentid);
}
}
}
else
{
if ($endorsedproposals[$p] == "1")
{
//set_log("No change for endorsed proposal $p - continue...");
}
else
{
//set_log("No change for opposed proposal $p - continue...");
}
continue;
}
}
/*
$hasvoted = (int)$_POST['hasvoted'];
if (!$hasvoted)
{
setuservoted($userid, $question, $roundid);
} */
$NEWnEndorsers=CountEndorsers($question,$roundid);
//DeleteGraph($question,$roundid);
if( $nEndorsers < $NEWnEndorsers)
{
AwareAuthorOfNewEndorsement($question);
}
$room = GetRoom($question);
$urlquery = CreateQuestionURL($question, $room);
if ($is_anon)
{
$urlquery = "?anon=$userid&query=viewquestion.php".$urlquery;
header("Location: anonvotesfeedback.php".$urlquery);
}
else
{
header("Location: viewquestion.php".$urlquery."#Voted");
}
?>