root/wp-polls/trunk/polls-logs.php

Revision 49932, 17.8 kB (checked in by GamerZ, 1 month ago)

Fixed MYSQL Charset Issue

Line 
1 <?php
2 /*
3 +----------------------------------------------------------------+
4 |                                                                                            |
5 |    WordPress 2.5 Plugin: WP-Polls 2.31                                        |
6 |    Copyright (c) 2008 Lester "GaMerZ" Chan                                    |
7 |                                                                                            |
8 |    File Written By:                                                                    |
9 |    - Lester "GaMerZ" Chan                                                            |
10 |    - http://lesterchan.net                                                            |
11 |                                                                                            |
12 |    File Information:                                                                    |
13 |    - Polls Logs                                                                            |
14 |    - wp-content/plugins/wp-polls/polls-logs.php                                |
15 |                                                                                            |
16 +----------------------------------------------------------------+
17 */
18
19
20 ### Check Whether User Can Manage Polls
21 if(!current_user_can('manage_polls')) {
22     die('Access Denied');
23 }
24
25
26 ### Variables
27 $pollip_answers = array();
28 $poll_question_data = $wpdb->get_row("SELECT pollq_multiple, pollq_question, pollq_totalvoters FROM $wpdb->pollsq WHERE pollq_id = $poll_id");
29 $poll_question = stripslashes($poll_question_data->pollq_question);
30 $poll_totalvoters = intval($poll_question_data->pollq_totalvoters);
31 $poll_multiple = intval($poll_question_data->pollq_multiple);
32 $poll_registered = $wpdb->get_var("SELECT COUNT(pollip_userid) FROM $wpdb->pollsip WHERE pollip_qid = $poll_id AND pollip_userid > 0");
33 $poll_comments = $wpdb->get_var("SELECT COUNT(pollip_user) FROM $wpdb->pollsip WHERE pollip_qid = $poll_id AND pollip_user != '".__('Guest', 'wp-polls')."' AND pollip_userid = 0");
34 $poll_guest = $wpdb->get_var("SELECT COUNT(pollip_user) FROM $wpdb->pollsip WHERE pollip_qid = $poll_id AND pollip_user = '".__('Guest', 'wp-polls')."'");
35 $poll_totalrecorded = ($poll_registered+$poll_comments+$poll_guest);
36 $poll_answers_data = $wpdb->get_results("SELECT polla_aid, polla_answers FROM $wpdb->pollsa WHERE polla_qid = $poll_id ORDER BY ".get_option('poll_ans_sortby').' '.get_option('poll_ans_sortorder'));
37 $poll_voters = $wpdb->get_col("SELECT DISTINCT pollip_user FROM $wpdb->pollsip WHERE pollip_qid = $poll_id AND pollip_user != '".__('Guest', 'wp-polls')."' ORDER BY pollip_user ASC");
38 $poll_logs_count = $wpdb->get_var("SELECT COUNT(pollip_id) FROM $wpdb->pollsip WHERE pollip_qid = $poll_id");
39
40
41 ### Process Filters
42 if(!empty($_POST['do'])) {
43     $registered_sql = '';
44     $comment_sql = '';
45     $guest_sql = '';
46     $users_voted_for_sql = '';
47     $what_user_voted_sql = '';
48     $num_choices_sql = '';
49     $num_choices_sign_sql = '';
50     $order_by = '';
51     switch(intval($_POST['filter'])) {
52         case 1:
53             $users_voted_for = intval($_POST['users_voted_for']);
54             $exclude_registered = intval($_POST['exclude_registered']);
55             $exclude_comment = intval($_POST['exclude_comment']);
56             $exclude_guest = intval($_POST['exclude_guest']);
57             $users_voted_for_sql = "AND pollip_aid = $users_voted_for";
58             if($exclude_registered) {
59                 $registered_sql = 'AND pollip_userid = 0';
60             }
61             if($exclude_comment) {
62                 if(!$exclude_registered) {
63                     $comment_sql = 'AND pollip_userid > 0';
64                 } else {
65                     $comment_sql = 'AND pollip_user = \''.__('Guest', 'wp-polls').'\'';
66                 }
67             }
68             if($exclude_guest) {
69                 $guest_sql  = 'AND pollip_user != \''.__('Guest', 'wp-polls').'\'';
70             }
71             $order_by = 'pollip_timestamp DESC';
72             break;
73         case 2:
74             $exclude_registered_2 = intval($_POST['exclude_registered_2']);
75             $exclude_comment_2 = intval($_POST['exclude_comment_2']);
76             $num_choices = intval($_POST['num_choices']);
77             $num_choices_sign = addslashes($_POST['num_choices_sign']);
78             switch($num_choices_sign) {
79                 case 'more':
80                     $num_choices_sign_sql = '>';
81                     break;
82                 case 'more_exactly':
83                     $num_choices_sign_sql = '>=';
84                     break;
85                 case 'exactly':
86                     $num_choices_sign_sql = '=';
87                     break;
88                 case 'less_exactly':
89                     $num_choices_sign_sql = '<=';
90                     break;
91                 case 'less':
92                     $num_choices_sign_sql = '<';
93                     break;
94             }
95             if($exclude_registered_2) {
96                 $registered_sql = 'AND pollip_userid = 0';
97             }
98             if($exclude_comment_2) {
99                 if(!$exclude_registered_2) {
100                     $comment_sql = 'AND pollip_userid > 0';
101                 } else {
102                     $comment_sql = 'AND pollip_user = \''.__('Guest', 'wp-polls').'\'';
103                 }
104             }
105             $guest_sql  = 'AND pollip_user != \''.__('Guest', 'wp-polls').'\'';
106             $num_choices_query = $wpdb->get_col("SELECT pollip_user, COUNT(pollip_ip) AS num_choices FROM $wpdb->pollsip WHERE pollip_qid = $poll_id GROUP BY pollip_ip, pollip_user HAVING num_choices $num_choices_sign_sql $num_choices");
107             $num_choices_sql = 'AND pollip_user IN (\''.implode('\',\'',$num_choices_query).'\')';
108             $order_by = 'pollip_user, pollip_ip';
109             break;
110         case 3;
111             $what_user_voted = addslashes($_POST['what_user_voted']);
112             $what_user_voted_sql = "AND pollip_user = '$what_user_voted'";
113             $order_by = 'pollip_user, pollip_ip';
114             break;
115     }
116     $poll_ips = $wpdb->get_results("SELECT $wpdb->pollsip.* FROM $wpdb->pollsip WHERE pollip_qid = $poll_id $users_voted_for_sql $registered_sql $comment_sql $guest_sql $what_user_voted_sql $num_choices_sql ORDER BY $order_by");
117 } else {
118     $poll_ips = $wpdb->get_results("SELECT pollip_aid, pollip_ip, pollip_host, pollip_timestamp, pollip_user FROM $wpdb->pollsip WHERE pollip_qid = $poll_id ORDER BY pollip_aid ASC, pollip_user ASC LIMIT 100");
119 }
120 ?>
121 <?php if(!empty($text)) { echo '<!-- Last Action --><div id="message" class="updated fade">'.stripslashes($text).'</div>'; } else { echo '<div id="message" class="updated" style="display: none;"></div>'; } ?>
122 <div class="wrap">
123     <h2><?php _e('Poll\'s Logs', 'wp-polls'); ?></h2>
124     <h3><?php echo $poll_question; ?></h3>
125     <p>
126         <?php printf(__('There are a total of <strong>%s</strong> recorded votes for this poll.', 'wp-polls'), number_format_i18n($poll_totalrecorded)); ?><br />
127         <?php printf(__('<strong>&raquo;</strong> <strong>%s</strong> vote(s) are voted by registered users', 'wp-polls'), number_format_i18n($poll_registered)); ?><br />
128         <?php printf(__('<strong>&raquo;</strong> <strong>%s</strong> vote(s) are voted by comment authors', 'wp-polls'), number_format_i18n($poll_comments)); ?><br />
129         <?php printf(__('<strong>&raquo;</strong> <strong>%s</strong> vote(s) are voted by guests', 'wp-polls'), number_format_i18n($poll_guest)); ?>
130     </p>
131 </div>
132 <?php if($poll_totalrecorded > 0) { ?>
133 <div class="wrap">
134     <h2><?php _e('Filter Poll\'s Logs', 'wp-polls') ?></h2>
135     <table width="100%"  border="0" cellspacing="0" cellpadding="0">
136         <tr>
137             <td width="50%">
138                 <form method="post" action="<?php echo htmlspecialchars($base_page); ?>&amp;mode=logs&amp;id=<?php echo $poll_id; ?>">
139                 <p style="display: none;"><input type="hidden" name="filter" value="1" /></p>
140                 <table class="form-table">
141                     <tr>
142                         <th scope="row" valign="top"><?php _e('Display All Users That Voted For', 'wp-polls'); ?></th>
143                         <td>
144                             <select name="users_voted_for" size="1">
145                                 <?php
146                                     if($poll_answers_data) {
147                                         foreach($poll_answers_data as $data) {
148                                             $polla_id = intval($data->polla_aid);
149                                             $polla_answers = stripslashes(strip_tags(htmlspecialchars($data->polla_answers)));
150                                             if($polla_id  == $users_voted_for) {
151                                                 echo '<option value="'.$polla_id .'" selected="selected">'.$polla_answers.'</option>';
152                                             } else {
153                                                 echo '<option value="'.$polla_id .'">'.$polla_answers.'</option>';
154                                             }
155                                             $pollip_answers[$polla_id] = $polla_answers;
156                                         }
157                                     }
158                                 ?>
159                             </select>
160                         </td>
161                     </tr>
162                     <tr>
163                         <th scope="row" valign="top"><?php _e('Voters To EXCLUDE', 'wp-polls'); ?></th>
164                         <td>
165                             <input type="checkbox" id="exclude_registered_1" name="exclude_registered" value="1" <?php checked('1', $exclude_registered); ?> />&nbsp;<label for="exclude_registered_1"><?php _e('Registered Users', 'wp-polls'); ?></label><br />
166                             <input type="checkbox" id="exclude_comment_1" name="exclude_comment" value="1" <?php checked('1', $exclude_comment); ?> />&nbsp;<label for="exclude_comment_1"><?php _e('Comment Authors', 'wp-polls'); ?></label><br />
167                             <input type="checkbox" id="exclude_guest_1" name="exclude_guest" value="1" <?php checked('1', $exclude_guest); ?> />&nbsp;<label for="exclude_guest_1"><?php _e('Guests', 'wp-polls'); ?></label>
168                         </td>
169                     </tr>
170                     <tr>
171                         <td colspan="2" align="center"><input type="submit" name="do" value="<?php _e('Filter', 'wp-polls'); ?>" class="button" /></td>
172                     </tr>
173                 </table>
174                 </form>
175             </td>
176             <td width="50%">
177                 <?php if($poll_multiple > 0) { ?>
178                     <form method="post" action="<?php echo htmlspecialchars($base_page); ?>&amp;mode=logs&amp;id=<?php echo $poll_id; ?>">
179                     <p style="display: none;"><input type="hidden" name="filter" value="2" /></p>
180                     <table class="form-table">
181                         <tr>
182                             <th scope="row" valign="top"><?php _e('Display Users That Voted For', 'wp-polls'); ?></th>
183                             <td>
184                                 <select name="num_choices_sign" size="1">
185                                     <option value="more" <?php selected('more', $num_choices_sign); ?>><?php _e('More Than', 'wp-polls'); ?></option>
186                                     <option value="more_exactly" <?php selected('more_exactly', $num_choices_sign); ?>><?php _e('More Than Or Exactly', 'wp-polls'); ?></option>
187                                     <option value="exactly" <?php selected('exactly', $num_choices_sign); ?>><?php _e('Exactly', 'wp-polls'); ?></option>
188                                     <option value="less_exactly" <?php selected('less_exactly', $num_choices_sign); ?>><?php _e('Less Than Or Exactly', 'wp-polls'); ?></option>
189                                     <option value="less" <?php selected('less', $num_choices_sign); ?>><?php _e('Less Than', 'wp-polls'); ?></option>
190                                 </select>
191                                 &nbsp;&nbsp;
192                                 <select name="num_choices" size="1">
193                                     <?php
194                                         for($i = 1; $i <= $poll_multiple; $i++) {
195                                             if($i == 1) {
196                                                 echo '<option value="1">'.__('1 Answer', 'wp-polls').'</option>';
197                                             } else {
198                                                 if($i == $num_choices) {
199                                                     echo '<option value="'.$i.'" selected="selected">'.sprintf(__('%s Answers', 'wp-polls'), $i).'</option>';
200                                                 } else {
201                                                     echo '<option value="'.$i.'">'.sprintf(__('%s Answers', 'wp-polls'), $i).'</option>';
202                                                 }
203                                             }
204                                         }
205                                     ?>
206                                 </select>
207                             </td>
208                         </tr>
209                         <tr>
210                             <th scope="row" valign="top"><?php _e('Voters To EXCLUDE', 'wp-polls'); ?></th>
211                             <td>
212                                 <input type="checkbox" id="exclude_registered_2" name="exclude_registered_2" value="1" <?php checked('1', $exclude_registered_2); ?> />&nbsp;<label for="exclude_registered_2"><?php _e('Registered Users', 'wp-polls'); ?></label><br />
213                                 <input type="checkbox" id="exclude_comment_2" name="exclude_comment_2" value="1" <?php checked('1', $exclude_comment_2); ?> />&nbsp;<label for="exclude_comment_2"><?php _e('Comment Authors', 'wp-polls'); ?></label><br />
214                                 <?php _e('Guests will automatically be excluded', 'wp-polls'); ?>
215                             </td>
216                         </tr>
217                         <tr>
218                             <td colspan="2" align="center"><input type="submit" name="do" value="<?php _e('Filter', 'wp-polls'); ?>" class="button" /></td>
219                         </tr>
220                     </table>
221                     </form>
222                 <?php } else { ?>
223                     &nbsp;
224                 <?php } // End if($poll_multiple > -1) ?>               
225             </td>
226         </tr>
227         <tr>
228             <td>
229                 <?php if($poll_voters) { ?>
230                 <form method="post" action="<?php echo htmlspecialchars($base_page); ?>&amp;mode=logs&amp;id=<?php echo $poll_id; ?>">
231                 <p style="display: none;"><input type="hidden" name="filter" value="3" /></p>
232                 <table class="form-table">
233                     <tr>
234                         <th scope="row" valign="top"><?php _e('Display What This User Has Voted', 'wp-polls'); ?></th>
235                         <td>
236                             <select name="what_user_voted" size="1">
237                                 <?php
238                                     if($poll_voters) {
239                                         foreach($poll_voters as $pollip_user) {
240                                             if($pollip_user == $what_user_voted) {
241                                                 echo '<option value="'.stripslashes(htmlspecialchars($pollip_user)).'" selected="selected">'.stripslashes(htmlspecialchars($pollip_user)).'</option>';
242                                             } else {
243                                                 echo '<option value="'.stripslashes(htmlspecialchars($pollip_user)).'">'.stripslashes(htmlspecialchars($pollip_user)).'</option>';
244                                             }
245                                         }
246                                     }
247                                 ?>
248                             </select>
249                         </td>
250                     </tr>
251                     <tr>
252                         <td colspan="2" align="center"><input type="submit" name="do" value="<?php _e('Filter', 'wp-polls'); ?>" class="button" /></td>
253                     </tr>
254                 </table>
255                 </form>
256                 <?php } else { ?>
257                     &nbsp;
258                 <?php } // End if($poll_multiple > -1) ?>       
259             </td>
260             <td align="center"><input type="button" value="<?php _e('Clear Filter', 'wp-polls'); ?>" onclick="self.location.href = '<?php echo htmlspecialchars($base_page); ?>&amp;mode=logs&amp;id=<?php echo $poll_id; ?>';" class="button" /></td>
261         </tr>
262     </table>
263 </div>
264 <p>&nbsp;</p>
265 <?php } // End if($poll_totalrecorded > 0) ?>
266 <div class="wrap">
267     <h2><?php _e('Poll Logs', 'wp-polls'); ?></h2>
268     <div id="poll_logs_display">
269         <?php
270             if($poll_ips) {
271                 if(empty($_POST['do'])) {
272                     echo '<p>'.__('This default filter is limited to display only <strong>100</strong> records.', 'wp-polls').'</p>';
273                 }
274                 echo '<table class="widefat">'."\n";
275                 $k = 1;
276                 $j = 0;
277                 $poll_last_aid = -1;
278                 if(intval($_POST['filter']) > 1) {
279                     echo "<tr class=\"thead\">\n";
280                     echo "<th>".__('Answer', 'wp-polls')."</th>\n";
281                     echo "<th>".__('IP', 'wp-polls')."</th>\n";
282                     echo "<th>".__('Host', 'wp-polls')."</th>\n";
283                     echo "<th>".__('Date', 'wp-polls')."</th>\n";
284                     echo "</tr>\n";
285                     foreach($poll_ips as $poll_ip) {
286                         $pollip_aid = intval($poll_ip->pollip_aid);
287                         $pollip_user = stripslashes($poll_ip->pollip_user);
288                         $pollip_ip = $poll_ip->pollip_ip;
289                         $pollip_host = $poll_ip->pollip_host;
290                         $pollip_date = mysql2date(sprintf(__('%s @ %s', 'wp-polls'), get_option('date_format'), get_option('time_format')), gmdate('Y-m-d H:i:s', $poll_ip->pollip_timestamp));
291                         if($i%2 == 0) {
292                             $style = '';
293                         }  else {
294                             $style = 'class="alternate"';
295                         }
296                         if($pollip_user != $temp_pollip_user) {
297                             echo '<tr class="highlight">'."\n";
298                             echo "<td colspan=\"4\"><strong>".__('User', 'wp-polls')." $k: $pollip_user</strong></td>\n";
299                             echo '</tr>';
300                             $k++;
301                         }       
302                         echo "<tr $style>\n";
303                         echo "<td>{$pollip_answers[$pollip_aid]}</td>\n";
304                         echo "<td>$pollip_ip</td>\n";
305                         echo "<td>$pollip_host</td>\n";
306                         echo "<td>$pollip_date</td>\n";
307                         echo "</tr>\n";
308                         $temp_pollip_user = $pollip_user;               
309                         $i++;
310                         $j++;
311                     }
312                 } else {
313                     foreach($poll_ips as $poll_ip) {
314                         $pollip_aid = intval($poll_ip->pollip_aid);
315                         $pollip_user = stripslashes($poll_ip->pollip_user);
316                         $pollip_ip = $poll_ip->pollip_ip;
317                         $pollip_host = $poll_ip->pollip_host;
318                         $pollip_date = mysql2date(sprintf(__('%s @ %s', 'wp-polls'), get_option('date_format'), get_option('time_format')), gmdate('Y-m-d H:i:s', $poll_ip->pollip_timestamp));
319                         if($pollip_aid != $poll_last_aid) {
320                             if($pollip_aid == 0) {
321                                 echo "<tr class=\"highlight\">\n<td colspan=\"4\"><strong>$pollip_answers[$pollip_aid]</strong></td>\n</tr>\n";
322                             } else {
323                                 echo "<tr class=\"highlight\">\n<td colspan=\"4\"><strong>".__('Answer', 'wp-polls')." $k: $pollip_answers[$pollip_aid]</strong></td>\n</tr>\n";
324                                 $k++;
325                             }
326                             echo "<tr class=\"thead\">\n";
327                             echo "<th>".__('No.', 'wp-polls')."</th>\n";
328                             echo "<th>".__('User', 'wp-polls')."</th>\n";
329                             echo "<th>".__('IP/Host', 'wp-polls')."</th>\n";
330                             echo "<th>".__('Date', 'wp-polls')."</th>\n";
331                             echo "</tr>\n";
332                             $i = 1;
333                         }
334                         if($i%2 == 0) {
335                             $style = '';
336                         }  else {
337                             $style = 'class="alternate"';
338                         }
339                         echo "<tr $style>\n";
340                         echo "<td>$i</td>\n";
341                         echo "<td>$pollip_user</td>\n";
342                         echo "<td>$pollip_ip / $pollip_host</td>\n";
343                         echo "<td>$pollip_date</td>\n";
344                         echo "</tr>\n";
345                         $poll_last_aid = $pollip_aid;
346                         $i++;
347                         $j++;
348                     }
349                 }
350                 echo "<tr class=\"highlight\">\n";
351                 echo "<td colspan=\"4\">".sprintf(__('Total number of records that matches this filter: <strong>%s</strong>', 'wp-polls'), number_format_i18n($j))."</td>";
352                 echo "</tr>\n";
353                 echo '</table>'."\n";
354             }
355         ?>
356     </div>
357     <?php if(!empty($_POST['do'])) { ?>
358         <br class="clear" /><div id="poll_logs_display_none" style="text-align: center; display: <?php if(!$poll_ips) { echo 'block'; } else { echo 'none'; } ?>;" ><?php _e('No poll logs matches the filter.', 'wp-polls'); ?></div>
359     <?php } else { ?>
360         <br class="clear" /><div id="poll_logs_display_none" style="text-align: center; display: <?php if(!$poll_logs_count) { echo 'block'; } else { echo 'none'; } ?>;" ><?php _e('No poll logs available for this poll.', 'wp-polls'); ?></div>
361     <?php } ?>
362 </div>
363 <p>&nbsp;</p>
364
365 <!-- Delete Poll Logs -->
366 <div class="wrap">
367     <h2><?php _e('Delete Poll Logs', 'wp-polls'); ?></h2>
368     <br class="clear" />
369     <div align="center" id="poll_logs">
370         <?php if($poll_logs_count) { ?>
371             <strong><?php _e('Are You Sure You Want To Delete Logs For This Poll Only?', 'wp-polls'); ?></strong><br /><br />
372             <input type="checkbox" id="delete_logs_yes" name="delete_logs_yes" value="yes" />&nbsp;<label for="delete_logs_yes"><?php _e('Yes', 'wp-polls'); ?></label><br /><br />
373             <input type="button" name="do" value="<?php _e('Delete Logs For This Poll Only', 'wp-polls'); ?>" class="button" onclick="delete_this_poll_logs(<?php echo $poll_id; ?>, '<?php printf(js_escape(__('You are about to delete poll logs for this poll \'%s\' ONLY. This action is not reversible.', 'wp-polls')), htmlspecialchars($poll_question_text)); ?>');" />
374         <?php
375             } else {
376                 _e('No poll logs available for this poll.', 'wp-polls');
377             }
378         ?>
379     </div>
380     <p><?php _e('Note: If your logging method is by IP and Cookie or by Cookie, users may still be unable to vote if they have voted before as the cookie is still stored in their computer.', 'wp-polls'); ?></p>
381 </div>
Note: See TracBrowser for help on using the browser.