| 1 |
<?php |
| 2 |
/* |
| 3 |
+----------------------------------------------------------------+ |
| 4 |
| | |
| 5 |
| WordPress 2.0 Plugin: WP-Polls 2.04 | |
| 6 |
| Copyright (c) 2005 Lester "GaMerZ" Chan | |
| 7 |
| | |
| 8 |
| File Written By: | |
| 9 |
| - Lester "GaMerZ" Chan | |
| 10 |
| - http://www.lesterchan.net | |
| 11 |
| | |
| 12 |
| File Information: | |
| 13 |
| - Poll Archive | |
| 14 |
| - wp-polls.php | |
| 15 |
| | |
| 16 |
+----------------------------------------------------------------+ |
| 17 |
*/ |
| 18 |
|
| 19 |
|
| 20 |
### Wordpress Header |
| 21 |
require(dirname(__FILE__).'/wp-blog-header.php'); |
| 22 |
|
| 23 |
### Function: Poll Page Title |
| 24 |
add_filter('wp_title', 'poll_pagetitle'); |
| 25 |
function poll_pagetitle($poll_pagetitle) { |
| 26 |
return $poll_pagetitle.' » Polls'; |
| 27 |
} |
| 28 |
|
| 29 |
### Polls Variables |
| 30 |
$page = intval($_GET['page']); |
| 31 |
$polls_questions = array(); |
| 32 |
$polls_answers = array(); |
| 33 |
$polls_ip = array(); |
| 34 |
$polls_perpage = intval(get_settings('poll_archive_perpage')); |
| 35 |
$poll_questions_ids = '0'; |
| 36 |
$poll_voted = false; |
| 37 |
$poll_voted_aid = 0; |
| 38 |
$poll_id = 0; |
| 39 |
|
| 40 |
### Get Total Polls |
| 41 |
$total_polls = $wpdb->get_var("SELECT COUNT(pollq_id) FROM $wpdb->pollsq"); |
| 42 |
|
| 43 |
### Checking $page and $offset |
| 44 |
if (empty($page) || $page == 0) { $page = 1; } |
| 45 |
if (empty($offset)) { $offset = 0; } |
| 46 |
|
| 47 |
### Determin $offset |
| 48 |
$offset = ($page-1) * $polls_perpage; |
| 49 |
|
| 50 |
### Determine Max Number Of Polls To Display On Page |
| 51 |
if(($offset + $polls_perpage) > $total_polls) { |
| 52 |
$max_on_page = $total_polls; |
| 53 |
} else { |
| 54 |
$max_on_page = ($offset + $polls_perpage); |
| 55 |
} |
| 56 |
|
| 57 |
### Determine Number Of Polls To Display On Page |
| 58 |
if (($offset + 1) > ($total_polls)) { |
| 59 |
$display_on_page = $total_polls; |
| 60 |
} else { |
| 61 |
$display_on_page = ($offset + 1); |
| 62 |
} |
| 63 |
|
| 64 |
### Determing Total Amount Of Pages |
| 65 |
$total_pages = ceil($total_polls / $polls_perpage); |
| 66 |
|
| 67 |
### Make Sure Poll Is Not Disabled |
| 68 |
if(intval(get_settings('poll_currentpoll')) != -1 && $page < 2) { |
| 69 |
// Hardcoded Poll ID Is Not Specified |
| 70 |
if(intval($temp_poll_id) == 0) { |
| 71 |
// Current Poll ID Is Not Specified |
| 72 |
if(intval(get_settings('poll_currentpoll')) == 0) { |
| 73 |
// Get Lastest Poll ID |
| 74 |
$poll_id = intval(get_settings('poll_latestpoll')); |
| 75 |
} else { |
| 76 |
// Get Current Poll ID |
| 77 |
$poll_id = intval(get_settings('poll_currentpoll')); |
| 78 |
} |
| 79 |
// Get Hardcoded Poll ID |
| 80 |
} else { |
| 81 |
$poll_id = intval($temp_poll_id); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
### Get Poll Questions |
| 86 |
$questions = $wpdb->get_results("SELECT * FROM $wpdb->pollsq WHERE pollq_id != $poll_id ORDER BY pollq_id DESC LIMIT $offset, $polls_perpage"); |
| 87 |
if($questions) { |
| 88 |
foreach($questions as $question) { |
| 89 |
$polls_questions[] = array('id' => intval($question->pollq_id), 'question' => stripslashes($question->pollq_question), 'timestamp' => $question->pollq_timestamp, 'totalvotes' => intval($question->pollq_totalvotes)); |
| 90 |
$poll_questions_ids .= intval($question->pollq_id).', '; |
| 91 |
} |
| 92 |
$poll_questions_ids = substr($poll_questions_ids, 0, -2); |
| 93 |
} |
| 94 |
|
| 95 |
### Get Poll Answers |
| 96 |
$answers = $wpdb->get_results("SELECT polla_aid, polla_qid, polla_answers, polla_votes FROM $wpdb->pollsa WHERE polla_qid IN ($poll_questions_ids) ORDER BY ".get_settings('poll_ans_result_sortby').' '.get_settings('poll_ans_result_sortorder')); |
| 97 |
if($answers) { |
| 98 |
foreach($answers as $answer) { |
| 99 |
$polls_answers[] = array('aid' => intval($answer->polla_aid), 'qid' => intval($answer->polla_qid), 'answers' => stripslashes($answer->polla_answers), 'votes' => intval($answer->polla_votes)); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
### Get Poll IPs |
| 104 |
$ips = $wpdb->get_results("SELECT pollip_qid, pollip_aid FROM $wpdb->pollsip WHERE pollip_qid IN ($poll_questions_ids) AND pollip_ip = '".get_ipaddress()."'"); |
| 105 |
if($ips) { |
| 106 |
foreach($ips as $ip) { |
| 107 |
$polls_ips[] = array('qid' => intval($ip->pollip_qid), 'aid' => intval($ip->pollip_aid)); |
| 108 |
} |
| 109 |
} |
| 110 |
### Function: Check Voted To Get Voted Answer |
| 111 |
function check_voted($poll_id) { |
| 112 |
global $polls_ips; |
| 113 |
$temp_voted_aid = 0; |
| 114 |
if(intval($_COOKIE["voted_$poll_id"]) > 0) { |
| 115 |
$temp_voted_aid = intval($_COOKIE["voted_$poll_id"]); |
| 116 |
} else { |
| 117 |
if($polls_ips) { |
| 118 |
foreach($polls_ips as $polls_ip) { |
| 119 |
if($polls_ip['qid'] == $poll_id) { |
| 120 |
$temp_voted_aid = $polls_ip['aid']; |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
return $temp_voted_aid; |
| 126 |
} |
| 127 |
?> |
| 128 |
<?php get_header(); ?> |
| 129 |
<div id="content" class="narrowcolumn"> |
| 130 |
<?php |
| 131 |
if($page < 2) { |
| 132 |
echo "<!-- <Currrent Poll> -->\n"; |
| 133 |
echo '<h2 class="pagetitle">'.__('Current Poll').'</h2>'."\n"; |
| 134 |
// Current Poll |
| 135 |
if(intval(get_settings('poll_currentpoll')) == -1) { |
| 136 |
echo get_settings('poll_template_disable'); |
| 137 |
} else { |
| 138 |
// User Click on View Results Link |
| 139 |
if(intval($_GET['pollresult']) == 1) { |
| 140 |
display_pollresult($poll_id); |
| 141 |
// Check Whether User Has Voted |
| 142 |
} else { |
| 143 |
// Check Cookie First |
| 144 |
$voted_cookie = check_voted_cookie($poll_id); |
| 145 |
if($voted_cookie > 0) { |
| 146 |
display_pollresult($poll_id, $voted_cookie); |
| 147 |
// Check IP If Cookie Cannot Be Found |
| 148 |
} else { |
| 149 |
$voted_ip = check_voted_ip($poll_id); |
| 150 |
if($voted_ip > 0) { |
| 151 |
display_pollresult($poll_id, $voted_ip); |
| 152 |
// User Never Vote. Display Poll Voting Form |
| 153 |
} else { |
| 154 |
display_pollvote($poll_id); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
echo "<!-- </Currrent Poll> -->\n"; |
| 160 |
} |
| 161 |
?> |
| 162 |
<!-- <Poll Archives> --> |
| 163 |
<h2 class="pagetitle"><?php _e('Polls Archive'); ?></h2> |
| 164 |
<?php |
| 165 |
foreach($polls_questions as $polls_question) { |
| 166 |
// Is The Poll Total Votes 0? |
| 167 |
$poll_totalvotes_zero = true; |
| 168 |
if($polls_question['totalvotes'] > 0) { |
| 169 |
$poll_totalvotes_zero = false; |
| 170 |
} |
| 171 |
// Poll Question Variables |
| 172 |
$template_question = stripslashes(get_settings('poll_template_resultheader')); |
| 173 |
$template_question = str_replace("%POLL_QUESTION%", $polls_question['question'], $template_question); |
| 174 |
$template_question = str_replace("%POLL_ID%", $polls_question['id'], $template_question); |
| 175 |
$template_question = str_replace("%POLL_TOTALVOTES%", $polls_question['totalvotes'], $template_question); |
| 176 |
// Print Out Result Header Template |
| 177 |
echo $template_question; |
| 178 |
foreach($polls_answers as $polls_answer) { |
| 179 |
if($polls_question['id'] == $polls_answer['qid']) { |
| 180 |
// Calculate Percentage And Image Bar Width |
| 181 |
if(!$poll_totalvotes_zero) { |
| 182 |
if($polls_answer['votes'] > 0) { |
| 183 |
$poll_answer_percentage = round((($polls_answer['votes']/$polls_question['totalvotes'])*100)); |
| 184 |
$poll_answer_imagewidth = round($poll_answer_percentage*0.9); |
| 185 |
} else { |
| 186 |
$poll_answer_percentage = 0; |
| 187 |
$poll_answer_imagewidth = 1; |
| 188 |
} |
| 189 |
} else { |
| 190 |
$poll_answer_percentage = 0; |
| 191 |
$poll_answer_imagewidth = 1; |
| 192 |
} |
| 193 |
// Let User See What Options They Voted |
| 194 |
if(check_voted($polls_question['id']) == $polls_answer['aid']) { |
| 195 |
// Results Body Variables |
| 196 |
$template_answer = stripslashes(get_settings('poll_template_resultbody2')); |
| 197 |
$template_answer = str_replace("%POLL_ANSWER_ID%", $polls_answer['aid'], $template_answer); |
| 198 |
$template_answer = str_replace("%POLL_ANSWER%", $polls_answer['answers'], $template_answer); |
| 199 |
$template_answer = str_replace("%POLL_ANSWER_VOTES%", $polls_answer['votes'], $template_answer); |
| 200 |
$template_answer = str_replace("%POLL_ANSWER_PERCENTAGE%", $poll_answer_percentage, $template_answer); |
| 201 |
$template_answer = str_replace("%POLL_ANSWER_IMAGEWIDTH%", $poll_answer_imagewidth, $template_answer); |
| 202 |
// Print Out Results Body Template |
| 203 |
echo $template_answer; |
| 204 |
} else { |
| 205 |
// Results Body Variables |
| 206 |
$template_answer = stripslashes(get_settings('poll_template_resultbody')); |
| 207 |
$template_answer = str_replace("%POLL_ANSWER_ID%", $polls_answer['aid'], $template_answer); |
| 208 |
$template_answer = str_replace("%POLL_ANSWER%", $polls_answer['answers'], $template_answer); |
| 209 |
$template_answer = str_replace("%POLL_ANSWER_VOTES%", $polls_answer['votes'], $template_answer); |
| 210 |
$template_answer = str_replace("%POLL_ANSWER_PERCENTAGE%", $poll_answer_percentage, $template_answer); |
| 211 |
$template_answer = str_replace("%POLL_ANSWER_IMAGEWIDTH%", $poll_answer_imagewidth, $template_answer); |
| 212 |
// Print Out Results Body Template |
| 213 |
echo $template_answer; |
| 214 |
} |
| 215 |
// Delete Away From Array |
| 216 |
unset($polls_answer['answers']); |
| 217 |
} |
| 218 |
} |
| 219 |
// Results Footer Variables |
| 220 |
$template_footer = stripslashes(get_settings('poll_template_resultfooter')); |
| 221 |
$template_footer = str_replace("%POLL_TOTALVOTES%", $polls_question['totalvotes'], $template_footer); |
| 222 |
// Print Out Results Footer Template |
| 223 |
echo $template_footer; |
| 224 |
echo "<br /><hr class=\"Divider\" />\n"; |
| 225 |
} |
| 226 |
?> |
| 227 |
<!-- </Poll Archives> --> |
| 228 |
|
| 229 |
<!-- <Paging> --> |
| 230 |
<?php |
| 231 |
if($total_polls > 0) { |
| 232 |
?> |
| 233 |
<br /> |
| 234 |
<table width="100%" cellspacing="0" cellpadding="0" border="0"> |
| 235 |
<tr> |
| 236 |
<td align="left" width="50%"> |
| 237 |
<?php |
| 238 |
if($page > 1 && ((($page*$polls_perpage)-($polls_perpage-1)) <= $total_polls)) { |
| 239 |
echo '<b>«</b> <a href="wp-polls.php?page='.($page-1).'" title="« '.__('Previous Page').'">'.__('Previous Page').'</a>'; |
| 240 |
} else { |
| 241 |
echo ' '; |
| 242 |
} |
| 243 |
?> |
| 244 |
</td> |
| 245 |
<td align="right" width="50%"> |
| 246 |
<?php |
| 247 |
if($page >= 1 && ((($page*$polls_perpage)+1) <= $total_polls)) { |
| 248 |
echo '<a href="wp-polls.php?page='.($page+1).'" title="'.__('Next Page').' »">'.__('Next Page').'</a> <b>»</b>'; |
| 249 |
} else { |
| 250 |
echo ' '; |
| 251 |
} |
| 252 |
?> |
| 253 |
</td> |
| 254 |
</tr> |
| 255 |
<tr> |
| 256 |
<td colspan="2" align="center"> |
| 257 |
<?php _e('Pages'); ?> (<?echo $total_pages; ?>) : |
| 258 |
<?php |
| 259 |
if ($page >= 4) { |
| 260 |
echo '<b><a href="wp-polls.php?page=1" title="'.__('Go to First Page').'">« '.__('First').'</a></b> ... '; |
| 261 |
} |
| 262 |
if($page > 1) { |
| 263 |
echo ' <b><a href="wp-polls.php?page='.($page-1).'" title="« '.__('Go to Page').' '.($page-1).'">«</a></b> '; |
| 264 |
} |
| 265 |
for($i = $page - 2 ; $i <= $page +2; $i++) { |
| 266 |
if ($i >= 1 && $i <= $total_pages) { |
| 267 |
if($i == $page) { |
| 268 |
echo "<b>[$i]</b> "; |
| 269 |
} else { |
| 270 |
echo '<a href="wp-polls.php?page='.($i).'" title="'.__('Page').' '.$i.'">'.$i.'</a> '; |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
if($page < $total_pages) { |
| 275 |
echo ' <b><a href="wp-polls.php?page='.($page+1).'" title="'.__('Go to Page').' '.($page+1).' »">»</a></b> '; |
| 276 |
} |
| 277 |
if (($page+2) < $total_pages) { |
| 278 |
echo ' ... <b><a href="wp-polls.php?page='.($total_pages).'" title="'.__('Go to Last Page').'">'.__('Last').' »</a></b>'; |
| 279 |
} |
| 280 |
?> |
| 281 |
</td> |
| 282 |
</tr> |
| 283 |
</table> |
| 284 |
<!-- </Paging> --> |
| 285 |
<?php |
| 286 |
} |
| 287 |
?> |
| 288 |
</div> |
| 289 |
<?php |
| 290 |
get_sidebar(); |
| 291 |
get_footer(); |
| 292 |
?> |