-
Notifications
You must be signed in to change notification settings - Fork 4
/
qa-comment-voting-layer.php
357 lines (299 loc) · 11.3 KB
/
qa-comment-voting-layer.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
<?php
class qa_html_theme_layer extends qa_html_theme_base {
// check for post
function doctype()
{
if(!isset($_POST['ajax_comment_vote'])) {
qa_html_theme_base::doctype();
if($this->request == 'admin/permissions' && function_exists('qa_register_plugin_phrases') && qa_get_logged_in_level()>=QA_USER_LEVEL_ADMIN) {
$permits[] = 'permit_vote_c';
foreach($permits as $optionname) {
$value = qa_opt($optionname);
$optionfield=array(
'id' => $optionname,
'label' => qa_lang_html('comment_voting/'.$optionname).':',
'tags' => 'NAME="option_'.$optionname.'" ID="option_'.$optionname.'"',
'error' => qa_html(@$errors[$optionname]),
);
$widest=QA_PERMIT_ALL;
$narrowest=QA_PERMIT_ADMINS;
$permitoptions=qa_admin_permit_options($widest, $narrowest, (!QA_FINAL_EXTERNAL_USERS) && qa_opt('confirm_user_emails'));
if (count($permitoptions)>1)
qa_optionfield_make_select($optionfield, $permitoptions, $value,
($value==QA_PERMIT_CONFIRMED) ? QA_PERMIT_USERS : min(array_keys($permitoptions)));
$this->content['form']['fields'][$optionname]=$optionfield;
$this->content['form']['fields'][$optionname.'_points']= array(
'id' => $optionname.'_points',
'tags' => 'NAME="option_'.$optionname.'_points" ID="option_'.$optionname.'_points"',
'type'=>'number',
'value'=>qa_opt($optionname.'_points'),
'prefix'=>qa_lang_html('admin/users_must_have').' ',
'note'=>qa_lang_html('admin/points')
);
$checkboxtodisplay[$optionname.'_points']='(option_'.$optionname.'=='.qa_js(QA_PERMIT_POINTS).') ||(option_'.$optionname.'=='.qa_js(QA_PERMIT_POINTS_CONFIRMED).')';
}
qa_set_display_rules($this->content, $checkboxtodisplay);
}
}
}
function html()
{
$this->logged_in_userid = qa_get_logged_in_userid();
if(qa_opt('voting_on_cs')) {
if(isset($_POST['ajax_comment_vote'])) $this->ajaxCommentVote($_POST['ajax_comment_vote'],$_POST['ajax_comment_vote_id']);
else qa_html_theme_base::html();
}
else qa_html_theme_base::html();
}
function head_script() {
qa_html_theme_base::head_script();
if(qa_opt('voting_on_cs') && isset($this->content['q_view'])) {
$this->output("
<script type='text/javascript'>
function ajaxCommentVote(elem, oldvote)
{
var ens=elem.getAttribute('name').split('_');
var postid=ens[1];
var vote=parseInt(ens[2]);
var anchor=ens[3];
var which=parseInt(ens[4]);
var dataString = 'ajax_comment_vote_id='+postid+'&ajax_comment_vote='+vote;
jQuery.ajax({
type: 'POST',
url: '".qa_self_html()."',
data: dataString,
dataType: 'json',
success: function(json) {
if (json.status=='1') {
switch(vote) {
case 1:
var up = 0;
var up_type = '-selected';
var down_type = false;
break;
case -1:
var down = 0;
var up_type = false;
var down_type = '-selected';
break;
case 0:
var up = 1;
var down = -1;
var up_type = '';
var down_type = '';
break;
}".(!qa_opt('voting_down_cs')?"
down_type = false;
":"")."
elem.parentNode.innerHTML = (up_type!==false?'<div class=\"comment-vote-item'+up_type+'\" name=\"vote_'+ens[1]+'_'+up+'_c'+ens[1]+'_1\" onclick=\"ajaxCommentVote(this);\" title=\"'+json.up+'\">▲</div>':'')+(parseInt(json.data)!=0?'<div id=\"voting_'+ens[1]+'\" title=\"json.up\">'+(json.data!='0'?json.data:'')+'</div>':'')+(down_type!==false?'<div class=\"comment-vote-item'+down_type+'\" name=\"vote_'+ens[1]+'_'+down+'_c'+ens[1]+'_-1\" onclick=\"ajaxCommentVote(this);\" title=\"'+json.down+'\">▼</div>':'');
} else if (json.status=='0') {
var mess=document.getElementById('errorbox');
if (!mess) {
var mess=document.createElement('div');
mess.id='errorbox';
mess.className='qa-error';
mess.innerHTML=json.data;
}
var postelem=document.getElementById(anchor);
postelem.parentNode.insertBefore(mess, postelem);
} else {
alert('Unexpected response from server - please try again.');
}
}
});
return false;
}
</script>");
}
}
function head_custom() {
qa_html_theme_base::head_custom();
if(qa_opt('voting_on_cs')) {
$this->output('
<style>
.comment-votable-container {
width:100% !important;
}
.comment-vote-container {
padding-right: 4px;
text-align: center;
vertical-align:top;
width:12px;
}
.comment-vote-item {
color: silver;
}
.comment-vote-item:hover {
color:black;
cursor:pointer;
}
.comment-vote-item-selected {
color:orange;
cursor:pointer;
}
.comment-vote-item-disabled {
display:none;
}
</style>');
}
}
// theme replacement functions
function q_view($q_view)
{
if(qa_opt('voting_on_cs')) {
$this->comment_votes = $this->logged_in_userid?qa_db_read_all_assoc(qa_db_query_sub('SELECT ^uservotes.vote AS vote, ^uservotes.postid AS postid FROM ^posts,^uservotes WHERE ^uservotes.vote<>0 AND ^uservotes.userid=# AND ^uservotes.postid=^posts.postid AND ^posts.type=$',$this->logged_in_userid, 'C')):null;
}
qa_html_theme_base::q_view($q_view);
}
function c_item_main($c_item)
{
global $topage;
if(qa_opt('voting_on_cs') && is_array($this->comment_votes) && isset($c_item['content']) && !isset($c_item['url']) && !strpos($c_item['content'],'question-closed-message')) {
$vote=0;
$flag=0;
foreach($this->comment_votes as $vote) {
if($vote['postid'] == $c_item['raw']['postid']) {
$vote = (int)$vote['vote'];
break;
}
}
$netvotes = ($c_item['raw']['netvotes']!=0?$c_item['raw']['netvotes']:'');
if(qa_permit_check('permit_vote_c')) {
$this->output('<table class="comment-votable-container"><tr><td class="comment-vote-container">');
switch($vote) {
case 1:
$up = 0;
$up_type = '-selected';
$down_type = false;
break;
case -1:
$down = 0;
$down_type = '-selected';
$up_type = false;
break;
default:
$up = 1;
$down = -1;
$up_type = '';
$down_type = '';
break;
}
if(!qa_opt('voting_down_cs') && $vote != -1) $down_type = false;
// don't allow for own user
if($c_item['raw']['userid'] != qa_get_logged_in_userid())
$this->output(($up_type!==false?'<div class="comment-vote-item'.$up_type.'" name="vote_'.$c_item['raw']['postid'].'_'.$up.'_c'.$c_item['raw']['postid'].'_1" onclick="ajaxCommentVote(this);" title="'.qa_lang_html('main/vote'.($up == 0?'d':'').'_up_popup').'">▲</div>':'').($netvotes?'<div id="voting_'.$c_item['raw']['postid'].'">'.$netvotes.'</div>':'').($down_type!==false?'<div class="comment-vote-item'.$down_type.'" onclick="ajaxCommentVote(this);" name="vote_'.$c_item['raw']['postid'].'_'.$down.'_c'.$c_item['raw']['postid'].'_-1" title="'.qa_lang_html('main/vote'.($down == 0?'d':'').'_down_popup').'">▼</div>':''));
else if ($netvotes) {
$this->output('<div id="voting_'.$c_item['raw']['postid'].'">'.$netvotes.'</div>');
}
$this->output('</td><td>');
qa_html_theme_base::c_item_main($c_item);
$this->output('</td></tr></table>');
}
else if ($netvotes){
$this->output('<table class="comment-votable-container"><tr><td class="comment-vote-container"><div id="voting_'.$c_item['raw']['postid'].'">'.$netvotes.'</div></td><td>');
qa_html_theme_base::c_item_main($c_item);
$this->output('</td></tr></table>');
}
else qa_html_theme_base::c_item_main($c_item);
}
else qa_html_theme_base::c_item_main($c_item);
}
// db variable
var $logged_in_userid;
var $comment_votes;
// worker
function ajaxCommentVote($vote, $postid) {
global $topage,$qa_cookieid;
$post=qa_db_select_with_pending(qa_db_full_post_selectspec($this->logged_in_userid, $postid));
$voteerror = $this->comment_vote_error_html($post, $this->logged_in_userid, $topage);
if ($voteerror===false) {
$this->comment_vote_set($post, $this->logged_in_userid, qa_get_logged_in_handle(), $qa_cookieid, $vote);
$comment = qa_db_single_select(qa_db_full_post_selectspec(null, $postid));
$votes = $comment['netvotes'];
$up_text = qa_lang_html('main/vote'.($vote == 1?'d':'').'_up_popup');
$down_text = qa_lang_html('main/vote'.($vote == -1?'d':'').'_down_popup');
echo '{"status":"1","data":"'.$votes.'","up":"'.$up_text.'","down":"'.$down_text.'"}';
} else {
echo '{"status":"0","data":"'.$voteerror.'"}';
}
}
// 'hacked' function
function comment_vote_error_html($post, $userid, $topage)
/*
Check if $userid can vote on $post, on the page $topage.
Return an HTML error to display if there was a problem, or false if it's OK.
*/
{
require_once QA_INCLUDE_DIR.'qa-app-users.php';
if (
is_array($post) &&
qa_opt('permit_vote_c') &&
( (!isset($post['raw']['userid'])) || (!isset($userid)) || ((int)$post['raw']['userid']!=$userid) )
) {
switch (qa_user_permit_error('permit_vote_c', 'V')) {
case 'login':
return qa_insert_login_links(qa_lang_html('main/vote_must_login'), $topage);
break;
case 'confirm':
return qa_insert_login_links(qa_lang_html('main/vote_must_confirm'), $topage);
break;
case 'limit':
return qa_lang_html('main/vote_limit');
break;
default:
return qa_lang_html('users/no_permission');
break;
case false:
return false;
}
} else
return qa_lang_html('main/vote_not_allowed'); // voting option should not have been presented (but could happen due to options change)
}
function comment_vote_set($post, $userid, $handle, $cookieid, $vote)
/*
Actually set (application level) the $vote (-1/0/1) by $userid (with $handle and $cookieid) on $postid.
Handles user points, recounting and event reports as appropriate.
*/
{
require_once QA_INCLUDE_DIR.'qa-db-votes.php';
/*
require_once QA_INCLUDE_DIR.'qa-db-points.php';
require_once QA_INCLUDE_DIR.'qa-db-hotness.php';
require_once QA_INCLUDE_DIR.'qa-app-limits.php';
*/
$vote=(int)min(1, max(-1, $vote));
$oldvote=(int)qa_db_uservote_get($post['postid'], $userid);
qa_db_uservote_set($post['postid'], $userid, $vote);
qa_db_post_recount_votes($post['postid']);
/*
$postisanswer=($post['basetype']=='A');
$columns=array();
if ( ($vote>0) || ($oldvote>0) )
$columns[]=$postisanswer ? 'aupvotes' : 'qupvotes';
if ( ($vote<0) || ($oldvote<0) )
$columns[]=$postisanswer ? 'adownvotes' : 'qdownvotes';
qa_db_points_update_ifuser($userid, $columns);
qa_db_points_update_ifuser($post['userid'], array($postisanswer ? 'avoteds' : 'qvoteds', 'upvoteds', 'downvoteds'));
if ($post['basetype']=='Q')
qa_db_hotness_update($post['postid']);
if ($vote<0)
$action=$postisanswer ? 'a_vote_down' : 'q_vote_down';
elseif ($vote>0)
$action=$postisanswer ? 'a_vote_up' : 'q_vote_up';
else
$action=$postisanswer ? 'a_vote_nil' : 'q_vote_nil';
*/
if ($vote<0)
$action='c_vote_down';
elseif ($vote>0)
$action='c_vote_up';
else
$action='c_vote_nil';
//qa_report_write_action($userid, null, $action, null, null, null);
qa_report_event($action, $userid, $handle, $cookieid, array(
'postid' => $post['postid'],
'vote' => $vote,
'oldvote' => $oldvote,
));
}
}