forked from NoahY/q2a-poll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qa-poll-layer.php
431 lines (372 loc) · 17.7 KB
/
qa-poll-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
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
<?php
class qa_html_theme_layer extends qa_html_theme_base {
function doctype(){
qa_html_theme_base::doctype();
//qa_error_log($this->content);
if(qa_post_text('poll_vote')) {
return;
}
if($this->request == 'admin/permissions' && function_exists('qa_register_plugin_phrases') && qa_get_logged_in_level()>=QA_USER_LEVEL_ADMIN) {
$permits[] = 'permit_vote_poll';
$permits[] = 'permit_post_poll';
$permits[] = 'permit_close_poll';
$permits[] = 'permit_delete_poll';
foreach($permits as $optionname) {
$value = qa_opt($optionname);
$optionfield=array(
'id' => $optionname,
'label' => qa_lang_html('polls/'.$optionname).':',
'tags' => 'NAME="option_'.$optionname.'" ID="option_'.$optionname.'"',
'error' => qa_html(@$errors[$optionname]),
);
$widest=QA_PERMIT_USERS;
$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);
}
if (qa_opt('poll_enable')) {
global $qa_request;
if($qa_request == 'polls') {
$this->content['navigation']['sub'] = array('special'=>1);
}
else if($this->template == 'ask' && !qa_user_permit_error('permit_post_q') && !qa_opt('site_maintenance') && qa_permit_check('permit_post_poll')) {
$this->content['form']['tags'] .= ' onSubmit="pollSubmit(event)"';
$this->content['form']['fields'][] = array(
'label' => qa_lang('polls/checkbox_text'),
'tags' => 'STYLE="display:none;" CHECKED NAME="is_poll" ID="is_poll" onclick="jQuery(\'#qa-poll-ask-div\').toggle()"',
'type' => 'checkbox'
);
$this->content['form']['fields'][] = array(
'note' => '<div id="qa-poll-ask-div" style="display:none"><p class="qa-form-tall-label"><input type="checkbox" name="poll_multiple" CHECKED>'.qa_lang('polls/multiple_text').'</p><p class="qa-form-tall-label">'.qa_lang('polls/answers_text').'</p>
<input value="initiative:government" type="input" class="qa-poll-answer-text" class="qa-poll-answer-text" name="poll_answer_1" id="poll_answer_1"> <input type="button" class="qa-poll-answer-add" value="+" onclick="addPollAnswer(poll_answer_index)">
<br><input value="initiative:industry" type="input" class="qa-poll-answer-text" class="qa-poll-answer-text" name="poll_answer_2" id="poll_answer_2"> <input type="button" class="qa-poll-answer-add" value="+" onclick="addPollAnswer(poll_answer_index)">
<br><input value="initiative: both" type="input" class="qa-poll-answer-text" class="qa-poll-answer-text" name="poll_answer_3" id="poll_answer_3"> <input type="button" class="qa-poll-answer-add" value="+" onclick="addPollAnswer(poll_answer_index)">
<br><input value="compliance:2014-2015" type="input" class="qa-poll-answer-text" class="qa-poll-answer-text" name="poll_answer_4" id="poll_answer_4"> <input type="button" class="qa-poll-answer-add" value="+" onclick="addPollAnswer(poll_answer_index)">
<br><input value="compliance:2016-2018" type="input" class="qa-poll-answer-text" class="qa-poll-answer-text" name="poll_answer_5" id="poll_answer_5"> <input type="button" class="qa-poll-answer-add" value="+" onclick="addPollAnswer(poll_answer_index)">
<br><input value="compliance:2019-2023" type="input" class="qa-poll-answer-text" class="qa-poll-answer-text" name="poll_answer_6" id="poll_answer_6"> <input type="button" class="qa-poll-answer-add" value="+" onclick="addPollAnswer(poll_answer_index)">
<br><input value="compliance:2024-2030" type="input" class="qa-poll-answer-text" class="qa-poll-answer-text" name="poll_answer_7" id="poll_answer_7"> <input type="button" class="qa-poll-answer-add" value="+" onclick="addPollAnswer(poll_answer_index)">
</div>',
'type' => 'static',
);
}
$poll_array = qa_db_read_all_assoc(
qa_db_query_sub(
'SELECT * FROM ^postmeta WHERE meta_key=$',
'is_poll'
)
);
foreach($poll_array as $q) {
$poll[(int)$q['post_id']] = $q['meta_value'];
}
if(isset($this->content['q_view'])) {
$qid = $this->content['q_view']['raw']['postid'];
$author = $this->content['q_view']['raw']['userid'];
if(isset($poll[$qid])) { // is a poll
$this->poll = $poll[$qid];
if(qa_post_text('poll_delete') && (!qa_user_permit_error('permit_delete_poll') || qa_get_logged_in_userid() == $author)) {
$this->deletePoll($qid);
$this->content['error'] = 'Poll deleted.';
return;
}
else if (qa_post_text('poll_close') && (!qa_user_permit_error('permit_close_poll') || qa_get_logged_in_userid() == $author)) {
$this->closePoll($qid);
}
else if (qa_post_text('poll_reopen') && (!qa_user_permit_error('permit_close_poll') || qa_get_logged_in_userid() == $author)) {
$this->reopenPoll($qid);
}
// add post elements
// title
$this->content['title'] .= ' '.qa_lang('polls/question_title');
// poll div
$this->content['q_view']['content'] = @$this->content['q_view']['content'].'<div id="qa-poll-div"><div>Below you can express your personal preferences for the timing of this action and the initiator:</div>'.$this->getPollDiv($qid,qa_get_logged_in_userid()).'</div>';
// css class
$this->content['q_view']['main_form_tags'] = @$this->content['q_view']['main_form_tags'].' class="qa-poll"';
}
}
if(isset($this->content['q_list'])) {
foreach($this->content['q_list']['qs'] as $idx => $question) {
if(isset($poll[$question['raw']['postid']])) {
$this->content['q_list']['qs'][$idx]['title'] .= ' '.qa_lang('polls/question_title');
}
}
}
}
}
function html() {
if(qa_post_text('ajax_poll_id')) {
$this->output_raw($this->getPollDiv((int)qa_post_text('ajax_poll_id'),(int)qa_post_text('ajax_poll_voter'),qa_post_text('ajax_poll_vote'),qa_post_text('ajax_poll_cancel')));
return;
}
qa_html_theme_base::html();
}
function head_custom() {
if(qa_opt('poll_enable')) {
if($this->template == 'ask') {
$this->output_raw('<script>
var poll_answer_index = 5;
//jQuery("document").ready(function(){jQuery("#is_poll").removeAttr("checked")});
function addPollAnswer(idx) {
jQuery("#qa-poll-ask-div").append(\'<br/><input type="input" class="qa-poll-answer-text" name="poll_answer_\'+idx+\'" id="poll_answer_\'+idx+\'"> <input type="button" class="qa-poll-answer-add" value="+" onclick="addPollAnswer(poll_answer_index)">\');
poll_answer_index++;
}
function pollSubmit(e) {
if(jQuery("#is_poll").attr("checked")) {
var idx = 0, count = 0;
while(jQuery("#poll_answer_"+(++idx)).length) {
if(jQuery("#poll_answer_"+idx).val().length) {
count++;
}
if (count > 1) return true;
}
e.preventDefault();
alert("'.qa_lang('polls/choice_count_error').'");
return false;
}
}
</script>');
}
else if($this->template == 'question' && @$this->poll) {
$this->output('<style>',str_replace('^',QA_HTML_THEME_LAYER_URLTOROOT,qa_opt('poll_css')),'</style>');
if(qa_permit_check('permit_vote_poll'))
$this->output_raw("<script>
var arrGid = new Array();
function pollVote(gid,qid,uid,vid,cancel) {
if (typeof arrGid[gid] === 'undefined') arrGid[gid]=0;
if (arrGid[gid] == 1 && typeof cancel === 'undefined'){
// already votes, voting not allowed
}else{
if (cancel==1){
arrGid[gid] = 0;
}else{
arrGid[gid] = 1;
}
var dataString = 'ajax_poll_id='+qid+'&ajax_poll_voter='+uid+'&ajax_poll_vote='+vid+(cancel?'&ajax_poll_cancel='+cancel:'');
jQuery.ajax({
type: 'POST',
url: '".qa_self_html()."',
data: dataString,
success: function(data) {
if(/^[\\t\\n ]*###/.exec(data)) {
var error = data.replace(/^[\\t\\n ]*### */,'');
window.alert(error);
}
else {
jQuery('#qa-poll-div').html(data);
}
}
});
}
}
</script>");
}
}
qa_html_theme_base::head_custom();
}
function nav_list($navigation, $class, $level=null)
{
if($class == 'nav-sub' && in_array($this->template, array('plugin','questions')) && qa_opt('poll_enable') && qa_opt('poll_enable_subnav')) {
$navigation['polls'] = array(
'label' => qa_lang('polls/page_title'),
'url' => qa_path_html('polls'),
);
if($this->request == 'polls') {
unset($navigation['special']);
$newnav = qa_qs_sub_navigation(null,null);
$navigation = array_merge($newnav, $navigation);
unset($navigation['recent']['selected']);
$navigation['polls']['selected'] = true;
}
}
if(count($navigation) > 1 || $class != 'nav-sub') qa_html_theme_base::nav_list($navigation, $class, $level=null);
}
// worker
function getPollDiv($qid,$uid,$vid=null,$cancel=false) {
$author = $this->content['q_view']['raw']['userid'];
if(!$this->poll) {
$this->poll = qa_db_read_one_value(
qa_db_query_sub(
"SELECT meta_value FROM ^postmeta WHERE post_id=# AND meta_key='is_poll'",
$qid
),
true
);
}
$answers = qa_db_read_all_assoc(
qa_db_query_sub(
'SELECT BINARY content as content, votes, id FROM ^polls WHERE parentid=# ORDER BY content',
$qid
)
);
$voted = false;
if($this->poll != 2) {
foreach ($answers as $idx => $answer) {
$votes = explode(',',$answer['votes']);
if(in_array($uid,$votes)) {
$voted = true;
break;
}
}
}
// do voting
/*
echo "vid:".$vid."<br>";
echo "(int)vid:".(int)$vid."<br>";
echo "uid:".$uid."<br>";
echo "qa_permit_check('permit_vote_poll'):".qa_permit_check('permit_vote_poll')."<br>";
echo "this->poll (should be <9):".$this->poll."<br>";
echo "[<br>";
echo " qa_opt('poll_vote_change'):".qa_opt('poll_vote_change') ."<br>";
echo " OR !voted:".!$voted."<br>";
echo " OR this->poll (should equal 2):".$this->poll."<br>";
echo "]<br>";
*/
if($vid && $uid && qa_permit_check('permit_vote_poll') && $this->poll < 9 && (qa_opt('poll_vote_change') || !$voted || $this->poll == 2)) { // not closed, note voted or can change vote
$vid = (int)$vid;
foreach ($answers as $idx => $answer) {
$votes = explode(',',$answer['votes']);
if($answer['id'] == $vid && !$cancel) {
if(in_array($uid,$votes)) return '### you\'ve already voted, cheater!';
$answers[$idx]['votes'] = ($answers[$idx]['votes']?$answers[$idx]['votes'].',':'').$uid;
qa_db_query_sub(
'UPDATE ^polls SET votes=$ WHERE id=#',
$answers[$idx]['votes'], $vid
);
}
else if(in_array($uid,$votes) && ($this->poll != 2 || ($cancel && $answer['id'] == $vid))) { // unvoting
foreach($votes as $i => $vote) {
if($uid == $vote) {
unset($votes[$i]);
break;
}
}
$answers[$idx]['votes'] = implode(',',$votes);
qa_db_query_sub(
'UPDATE ^polls SET votes=$ WHERE id=#',
$answers[$idx]['votes'], $answer['id']
);
if($this->poll != 2) // cancel voted on unvoting
$voted = in_array($uid,$votes);
}
}
}
if(empty($answers)) return '### no choices found for poll!';
// $out = '<div id="qa-poll-choices-title">'.qa_lang('polls/answers_text').'</div><div id="qa-poll-choices">';
$out = '<div id="qa-poll-choices">';
// check if voted
$allow = true;
$totalvotes = 0;
$gid = 0;
$previousPollTitle = '';
foreach ($answers as $idx => $answer) {
$content = explode(":",qa_html($answer['content']));
$pollTitle = $content[0];
//because changes all polls in database is not straight forward, implement quick and dirty replace solution
$pollTitle = str_replace("initiative","Initiator",str_replace("compliance","Timing",$pollTitle));
$content = $content[1];
if ($previousPollTitle!=$pollTitle) {
$gid++;
}
$previousPollTitle = $pollTitle;
if(!$answer['votes']) $votes = array();
else $votes = explode(',',$answer['votes']);
$totalvotes += count($votes);
if(!$uid || !qa_permit_check('permit_vote_poll') || $this->poll > 9 || ($voted && !qa_opt('poll_vote_change') && !in_array($uid,$votes) && $this->poll != 2)) {
$answers[$idx]['vote'] = '<div class="qa-poll-disabled-button" title="'.qa_html(qa_lang('polls/disabled_button')).'"></div>';
}
else if($voted && !qa_opt('poll_vote_change') && $this->poll != 2) { // voted for this one, can't change, not multiple
$answers[$idx]['vote'] = '<div class="qa-poll-voted-button" title="'.qa_html(qa_lang('polls/voted_button')).'" onclick="alert(\''.qa_lang('polls/cannot_change').'\')"></div>';
}
else if(!in_array($uid,$votes))
$answers[$idx]['vote'] = '<div class="qa-poll-vote-button" title="'.qa_html(qa_lang('polls/vote_button')).'" onclick="pollVote('.$gid.','.$qid.','.$uid.','.$answer['id'].')"></div>';
else {
$answers[$idx]['vote'] = '<div class="qa-poll-voted-button" title="'.qa_html(qa_lang('polls/voted_button')).'" onclick="pollVote('.$gid.','.$qid.','.$uid.','.$answer['id'].',1)"></div>';
$out.="<script>arrGid[".$gid."] = 1;</script>";
$voted = true;
}
}
$previousPollTitle='';
foreach ($answers as $answer) {
if(!$answer['votes']) $votes = array();
else $votes = explode(',',$answer['votes']);
$content = explode(":",qa_html($answer['content']));
$pollTitle = $content[0];
//because changes all polls in database is not straight forward, implement quick and dirty replace solution
$pollTitle = str_replace("initiative","Initiator",str_replace("compliance","Timing",$pollTitle));
$content = $content[1];
if ($previousPollTitle!=$pollTitle) {
if($previousPollTitle!='') $out .='</div></div>';
$out .='<div id="qa-poll-choices-group-container"><div id="qa-poll-choices-group"><div id="qa-poll-choices-title">'.$pollTitle.':</div>';
}
$previousPollTitle = $pollTitle;
$out .= '<div class="qa-poll-choice">'.@$answer['vote'].'<span class="qa-poll-choice-title">'.$content.'</span>';
if(!qa_opt('poll_votes_hide') || $voted || qa_get_logged_in_level()>=QA_USER_LEVEL_ADMIN) {
$out .= ' ('.(count($votes)==1?qa_lang('main/1_vote'):str_replace('^',count($votes),qa_lang('main/x_votes'))).(qa_opt('poll_votes_percent') && !empty($votes) > 0? ', '.round(100*count($votes)/$totalvotes).'%' : '').')';
}
$out .= '<table class="qa-poll-votes"><tr>';
if($answer['votes'] && (!qa_opt('poll_votes_hide') || $voted || qa_get_logged_in_level()>=QA_USER_LEVEL_ADMIN)) { // don't show if user hasn't voted and admin option is checked
$c = 0;
while( ($c++) < count($votes)) {
$out .= '<td class="qa-poll-vote-block" title="'.(count($votes)==1?qa_lang('main/1_vote'):str_replace('^',count($votes),qa_lang('main/x_votes'))).'"></td>';
}
}
else $out .= '<td class="qa-poll-vote-block-empty"></td>';
$out .= '</tr></table></div>';
}
$out .= "</div></div>";
if($this->poll > 9) { // poll closed
$out .= '<div class="qa-poll-closed">'.qa_lang('polls/poll_closed').'</div>';
if(!qa_user_permit_error('permit_close_poll') || qa_get_logged_in_userid() == $author)
$out .= '<input type="hidden" class="qa-poll-button" title="'.qa_lang('polls/reopen_poll_hover').'" value="'.qa_lang('polls/reopen_poll').'" name="poll_reopen">';
}
else if(!qa_user_permit_error('permit_close_poll') || qa_get_logged_in_userid() == $author)
$out .= '<input type="hidden" class="qa-poll-button" title="'.qa_lang('polls/close_poll_hover').'" value="'.qa_lang('polls/close_poll').'" name="poll_close">';
if(!qa_user_permit_error('permit_delete_poll') || qa_get_logged_in_userid() == $author)
$out .= '<input type="hidden" class="qa-poll-button" title="'.qa_lang('polls/delete_poll_hover').'" value="'.qa_lang('polls/delete_poll').'" name="poll_delete">';
$out .= '</div>';
$out .= "<script type='text/javascript'>
$(document).ready(function(){
strContent = \$('.qa-q-view-where-data')[0].innerHTML;
intPosBegin=strContent.indexOf('>')+1;
intPosEnd=strContent.indexOf('</a>');
strImageName = strContent.substr(intPosBegin,intPosEnd-intPosBegin).toLowerCase().replace(' ','-').replace(' ','-').replace(' ','-').replace(' ','-').replace(' ','-');
\$('.question .qa-voting').append('<a href=\"/ImplementationPlan/'+strImageName+'\"><img src=\"/ImplementationPlan/'+strImageName+'.png\" style=\"border:0px;width:100px\"></a>');
});
</script>";
return $out;
}
function deletePoll($qid) {
qa_db_query_sub(
'DELETE FROM ^postmeta WHERE post_id=# AND meta_key=$',
$qid,'is_poll'
);
}
function closePoll($qid) {
qa_db_query_sub(
'UPDATE ^postmeta SET meta_value=# WHERE post_id=# AND meta_key=$',
10+$this->poll,$qid,'is_poll'
);
$this->poll = $this->poll+10;
}
function reopenPoll($qid) {
qa_db_query_sub(
'UPDATE ^postmeta SET meta_value=# WHERE post_id=# AND meta_key=$',
$this->poll-10,$qid,'is_poll'
);
$this->poll = $this->poll-10;
}
}
?>