PluginProbe
Poll Maker by AYS – Versus Polls, Anonymous Polls, Image Polls / 6.4.5
Poll Maker by AYS – Versus Polls, Anonymous Polls, Image Polls v6.4.5
6.4.9 6.4.8 6.4.7 6.4.6 6.4.5 6.4.4 6.4.3 6.4.2 6.4.1 6.4.0 6.3.9 6.3.8 6.3.7 6.3.6 6.3.5 6.3.4 6.3.3 5.6.0 5.6.1 5.6.2 5.6.3 5.6.4 5.6.5 5.6.6 5.6.7 All 152 releases
poll-maker / includes / lists / class-poll-maker-each-results-poll-list-table.php

class-poll-maker-each-results-poll-list-table.php in Poll Maker by AYS – Versus Polls, Anonymous Polls, Image Polls 6.4.5, at includes/lists/class-poll-maker-each-results-poll-list-table.php

565 lines 15.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 ob_start();
3
4 class Pma_Each_Results_List_Table extends WP_List_Table {
5 private $plugin_name;
6
7 /** Class constructor */
8 public function __construct( $plugin_name ) {
9 $this->plugin_name = $plugin_name;
10 parent::__construct(array(
11 'singular' =>esc_html__('Each result', "poll-maker"), //singular name of the listed records
12 'plural' =>esc_html__('Each results', "poll-maker"), //plural name of the listed records
13 'ajax' => false //does this table support ajax?
14 ));
15 add_action('admin_notices', array($this, 'eachresults_notices'));
16
17 }
18
19
20 /**
21 * Retrieve customers data from the database
22 *
23 * @param int $per_page
24 * @param int $page_number
25 *
26 * @return mixed
27 */
28 public static function get_results( $per_page = 50, $page_number = 1 ) {
29
30 global $wpdb;
31
32 $sql = "SELECT r.*, a.answer
33 FROM {$wpdb->prefix}ayspoll_reports as r
34 INNER JOIN {$wpdb->prefix}ayspoll_answers as a
35 ON r.answer_id = a.id
36 WHERE a.poll_id = " . absint($_GET['poll']);
37 $where_cond = "";
38 if (isset($_REQUEST['s']) && $_REQUEST['s'] != '') {
39 if (filter_var($_REQUEST['s'], FILTER_VALIDATE_EMAIL)) {
40 $where_cond .= " AND r.user_email LIKE ('%".$_REQUEST['s']."%')";
41 }
42 else{
43 $where_cond .= " AND a.answer LIKE ('%".$_REQUEST['s']."%')";
44 }
45 }
46 $sql .= $where_cond;
47 if (!empty($_REQUEST['orderby'])) {
48 $order_by = ( isset( $_REQUEST['orderby'] ) && sanitize_text_field( $_REQUEST['orderby'] ) != '' ) ? sanitize_text_field( $_REQUEST['orderby'] ) : 'id';
49 $order_by .= ( ! empty( $_REQUEST['order'] ) && strtolower( $_REQUEST['order'] ) == 'asc' ) ? ' ASC' : ' DESC';
50
51 $sql_orderby = sanitize_sql_orderby($order_by);
52
53 if ( $sql_orderby ) {
54 $sql .= ' ORDER BY r.' . $sql_orderby;
55 } else {
56 $sql .= ' ORDER BY r.id DESC';
57 }
58
59 } else {
60 $sql .= " ORDER BY r.id DESC";
61 }
62 $sql .= " LIMIT $per_page";
63 $sql .= ' OFFSET ' . ($page_number - 1) * $per_page;
64
65 $result = $wpdb->get_results($sql, 'ARRAY_A');
66 return $result;
67 }
68
69 public function get_votes_count_by_time_interval( $interval, $poll = 0 ) {
70 global $wpdb;
71 $today = date("Y-m-d");
72 $interval--;
73 $start_day = date('Y-m-d', strtotime("-$interval day"));
74 if ($interval == 0) {
75 $where = "
76 DATE({$wpdb->prefix}ayspoll_reports.vote_date) = '$today'";
77 } else {
78 $where = "
79 DATE({$wpdb->prefix}ayspoll_reports.vote_date) <= '$today'
80 AND DATE({$wpdb->prefix}ayspoll_reports.vote_date) > '$start_day'";
81 }
82 if ($poll > 0) {
83 $where .= " AND {$wpdb->prefix}ayspoll_answers.poll_id='$poll'";
84 }
85 $sql = "SELECT
86 {$wpdb->prefix}ayspoll_answers.poll_id,
87 {$wpdb->prefix}ayspoll_polls.title,
88 Count({$wpdb->prefix}ayspoll_answers.poll_id) AS votes,
89 DATE({$wpdb->prefix}ayspoll_reports.vote_date) AS date
90 FROM
91 {$wpdb->prefix}ayspoll_answers
92 JOIN {$wpdb->prefix}ayspoll_polls ON {$wpdb->prefix}ayspoll_polls.id = {$wpdb->prefix}ayspoll_answers.poll_id
93 JOIN {$wpdb->prefix}ayspoll_reports ON {$wpdb->prefix}ayspoll_reports.answer_id = {$wpdb->prefix}ayspoll_answers.id
94 WHERE
95 EXISTS(
96 SELECT id FROM {$wpdb->prefix}ayspoll_answers
97 WHERE id={$wpdb->prefix}ayspoll_reports.answer_id
98 )=1 AND "
99 . $where .
100 "GROUP BY
101 {$wpdb->prefix}ayspoll_polls.title";
102 $res = $wpdb->get_results($sql, "ARRAY_A");
103 $votes_count = array_sum(array_column($res, 'votes'));
104
105 return $votes_count;
106 }
107
108 public function get_voting_first_day() {
109 global $wpdb;
110 $sql = "SELECT
111 DATE(Min({$wpdb->prefix}ayspoll_reports.vote_date)) AS min_date
112 FROM
113 {$wpdb->prefix}ayspoll_reports WHERE EXISTS(SELECT id FROM {$wpdb->prefix}ayspoll_answers WHERE id={$wpdb->prefix}ayspoll_reports.answer_id)=1";
114 $date = $wpdb->get_var($sql);
115
116 return $date;
117 }
118
119 public function get_poll_data_by_day( $day, $id ) {
120 global $wpdb;
121 $sql = "SELECT
122 {$wpdb->prefix}ayspoll_answers.poll_id AS id,
123 {$wpdb->prefix}ayspoll_polls.title,
124 COUNT({$wpdb->prefix}ayspoll_answers.poll_id) AS polling_count,
125 DATE({$wpdb->prefix}ayspoll_reports.vote_date) AS vote_date_day
126 FROM
127 {$wpdb->prefix}ayspoll_reports
128 JOIN {$wpdb->prefix}ayspoll_answers ON {$wpdb->prefix}ayspoll_reports.answer_id = {$wpdb->prefix}ayspoll_answers.id
129 JOIN {$wpdb->prefix}ayspoll_polls ON {$wpdb->prefix}ayspoll_answers.poll_id = {$wpdb->prefix}ayspoll_polls.id
130 WHERE DATE({$wpdb->prefix}ayspoll_reports.vote_date)='$day' AND
131 {$wpdb->prefix}ayspoll_answers.poll_id ='$id'
132 GROUP BY {$wpdb->prefix}ayspoll_answers.poll_id";
133 $results = $wpdb->get_row($sql, 'ARRAY_A');
134
135 return $results;
136 }
137
138 public function get_poll_data_all( $poll_id = 0 ) {
139 global $wpdb;
140 $sql = "SELECT * FROM
141 {$wpdb->prefix}ayspoll_reports
142 INNER JOIN {$wpdb->prefix}ayspoll_answers
143 ON {$wpdb->prefix}ayspoll_reports.answer_id = {$wpdb->prefix}ayspoll_answers.id
144 WHERE {$wpdb->prefix}ayspoll_answers.poll_id = " . absint($_GET['poll']);
145 $res = $wpdb->get_results($sql, 'ARRAY_A');
146
147 if (empty($res)) {
148 return $res;
149 }
150
151 $start_day = strtotime($this->get_voting_first_day());
152 $end_day = time();
153 $datediff = $end_day - $start_day;
154 $day_count = ceil($datediff / (60 * 60 * 24));
155 $data = array();
156 $next = date('Y-m-d', $start_day);
157
158 if ($day_count > 0) {
159 $polls = $this->get_polls();
160 for ( $i = 1; $i <= $day_count; $i++ ) {
161 $day = array();
162
163 $temp = $this->get_poll_data_by_day($next, $poll_id);
164 if (!empty($temp)) {
165 $day[] = $temp;
166 } else {
167 $day[] = array(
168 'id' => $poll_id,
169 'title' => $polls[array_search($poll_id, array_column($polls, 'id'))]['title'],
170 'polling_count' => 0,
171 'vote_date_day' => $next,
172 'user_ip' => $res['user_ip'],
173 'answer_id' => $res['answer_id']
174 );
175 }
176
177 $data[] = $day;
178 $next = date('Y-m-d', strtotime("+$i day", $start_day));
179 }
180 }
181
182 return $data;
183 }
184
185 public function get_poll_data_chart( $poll_id ) {
186 global $wpdb;
187
188 $sql = "SELECT a.*,
189 JSON_EXTRACT(p.styles, '$.show_chart_type') AS show_chart_type,
190 JSON_EXTRACT(p.styles, '$.result_in_rgba') AS result_in_rgba,
191 JSON_EXTRACT(p.styles, '$.main_color') AS main_color,
192 JSON_EXTRACT(p.styles, '$.poll_enable_answer_image_after_voting') AS show_answer_images,
193 JSON_EXTRACT(p.styles, '$.show_chart_type_google_height') AS show_chart_type_google_height,
194 p.type,
195 p.view_type
196 FROM `{$wpdb->prefix}ayspoll_answers` AS a
197 JOIN `{$wpdb->prefix}ayspoll_polls` AS p ON a.poll_id = p.id
198 WHERE a.poll_id = $poll_id";
199 $results = $wpdb->get_results($sql, "ARRAY_A");
200
201 if (!empty($results) && $results[0]['type'] === 'text') {
202 $consolidated_results = [];
203 foreach ($results as $result) {
204 $answer = $result['answer'];
205 if (!isset($consolidated_results[$answer])) {
206 $consolidated_results[$answer] = $result;
207 } else {
208 $consolidated_results[$answer]['votes'] += $result['votes'];
209 }
210 }
211 return array_values($consolidated_results);
212 }
213
214 return $results;
215
216 }
217
218
219 public static function get_report_by_id( $id ) {
220 global $wpdb;
221
222 $sql = "SELECT * FROM {$wpdb->prefix}ayspoll_reports WHERE id=" . absint(intval($id));
223
224 $result = $wpdb->get_row($sql, 'ARRAY_A');
225
226 return $result;
227 }
228
229 /**
230 * Delete a customer record.
231 *
232 * @param int $id customer ID
233 */
234 public static function delete_results( $id ) {
235 global $wpdb;
236 $wpdb->delete(
237 "{$wpdb->prefix}ayspoll_reports",
238 array('id' => $id),
239 array('%d')
240 );
241 }
242
243
244 /**
245 * Returns the count of records in the database.
246 *
247 * @return null|string
248 */
249 public static function record_count() {
250 global $wpdb;
251
252 $sql = "SELECT
253 COUNT(r.id)
254 FROM
255 {$wpdb->prefix}ayspoll_reports as r
256 INNER JOIN {$wpdb->prefix}ayspoll_answers as a
257 ON r.answer_id = a.id
258 WHERE a.poll_id = " . absint($_GET['poll']);
259
260 return $wpdb->get_var($sql);
261 }
262
263 /** Text displayed when no customer data is available */
264 public function no_items() {
265 _e('There are no results yet.', "poll-maker");
266 }
267
268
269 /**
270 * Render a column when no column specific method exist.
271 *
272 * @param array $item
273 * @param string $column_name
274 *
275 * @return mixed
276 */
277 public function column_default( $item, $column_name ) {
278 $other_info = json_decode($item['other_info'], true);
279
280 switch ( $column_name ) {
281 case 'user_ip':
282 case 'answer_id':
283 case 'vote_date':
284 case 'vote_reason':
285 case 'unread':
286 return $item[$column_name];
287 break;
288 case 'user_id':
289 $user =esc_html__("Guest", "poll-maker");
290 if (!empty($item[$column_name]) && $item[$column_name] > 0) {
291 $user = get_user_by('ID', $item[$column_name]) ? get_user_by('ID', $item[$column_name])->display_name : '';
292 }
293 return $user;
294 break;
295 case 'user_email':
296 $email = '';
297
298 if ($item[$column_name] != '') {
299 $email = $item[$column_name];
300 } elseif (isset($other_info['email']) && !empty($other_info['email'])) {
301 $email = $other_info['email'];
302 }
303 return $email;
304 break;
305 case 'user_name':
306 $name = '';
307
308 if (isset($other_info['name']) && !empty($other_info['name'])) {
309 $name = $other_info['name'];
310 }
311 elseif (isset($other_info['Name']) && !empty($other_info['Name'])) {
312 $name = $other_info['Name'];
313 }
314 return $name;
315 break;
316 case 'user_phone':
317 if (!empty($other_info)) {
318 return isset($other_info['phone']) ? $other_info['phone'] : "";
319 } else {
320 return "";
321 }
322 break;
323 default:
324 return print_r($item, true); //Show the whole array for troubleshooting purposes
325 break;
326 }
327 }
328
329 /**
330 * Render the bulk edit checkbox
331 *
332 * @param array $item
333 *
334 * @return string
335 */
336 function column_cb( $item ) {
337 return sprintf(
338 '<input type="checkbox" name="bulk-action[]" value="%s" />', $item['id']
339 );
340 }
341
342 /**
343 * Method for name column
344 *
345 * @param array $item an array of DB data
346 *
347 * @return string
348 */
349 function column_answer_id( $item ) {
350 $poll_ajax_show_details_report_nonce = wp_create_nonce( 'poll-maker-ajax-show-details-report-nonce' );
351
352 $answer = '<a href="javascript:void(0)" data-result="'. absint($item['id']) .'" class="ays-show-results">' . stripslashes($item['answer']) . '</a>';
353 $answer .= '<input type="hidden" id="poll_maker_ajax_show_details_report_nonce" name="poll_maker_ajax_show_details_report_nonce" value="' . $poll_ajax_show_details_report_nonce . '">';
354 return $answer;
355 }
356
357 function column_vote_date( $item ) {
358 return date('H:i:s d.m.Y', strtotime($item['vote_date']));
359 }
360
361 function column_vote_reason( $item ) {
362 $info = json_decode($item['other_info'], true);
363 $vote_reason = '';
364
365 if (isset($info['voteReason']) && !empty($info['voteReason'])) {
366 $vote_reason = $info['voteReason'];
367 }
368
369 if (isset($info['vote_reason']) && !empty($info['vote_reason'])) {
370 $vote_reason = $info['vote_reason'];
371 }
372
373 return $vote_reason;
374 }
375
376 function column_unread( $item ) {
377 $unread = $item['unread'] == 1 ? "unread-result" : "";
378
379 return "<div class='unread-result-badge $unread'></div>";
380 }
381
382
383 /**
384 * Associative array of columns
385 *
386 * @return array
387 */
388 function get_columns() {
389 $columns = array(
390 'cb' => '<input type="checkbox" />',
391 'answer_id' =>esc_html__('Answer', "poll-maker"),
392 'user_ip' =>esc_html__('User IP', "poll-maker"),
393 'user_id' =>esc_html__('WP User', "poll-maker"),
394 'user_email' =>esc_html__('User Email', "poll-maker"),
395 'user_name' =>esc_html__('User Name', "poll-maker"),
396 'user_phone' =>esc_html__('User Phone', "poll-maker"),
397 'vote_date' =>esc_html__('Vote Datetime', "poll-maker"),
398 'vote_reason' =>esc_html__('Vote Reason', "poll-maker"),
399 'unread' =>esc_html__('Read Status', "poll-maker")
400 );
401
402 return $columns;
403 }
404
405
406 /**
407 * Columns to make sortable.
408 *
409 * @return array
410 */
411 public function get_sortable_columns() {
412 $sortable_columns = array(
413 'id' => array('id', true),
414 'answer_id' => array('answer_id', true),
415 'user_ip' => array('user_ip', true),
416 'user_id' => array('user_id', true),
417 'user_email' => array('user_email', true),
418 'vote_date' => array('vote_date', true),
419 // 'vote_reason' => array('vote_reason', true),
420 'unread' => array('unread', true),
421 );
422
423
424 return $sortable_columns;
425 }
426
427 /**
428 * Returns an associative array containing the bulk action
429 *
430 * @return array
431 */
432 public function get_bulk_actions() {
433 $actions = array(
434 'bulk-read' =>esc_html__('Mark as read', "poll-maker"),
435 'bulk-delete' =>esc_html__('Delete', "poll-maker"),
436 );
437
438 return $actions;
439 }
440
441
442 /**
443 * Handles data query and filter, sorting, and pagination.
444 */
445 public function prepare_items() {
446
447 $this->_column_headers = $this->get_column_info();
448 /** Process bulk action */
449 $this->process_bulk_action();
450
451 $per_page = $this->get_items_per_page('poll_each_results_per_page', 50);
452 $current_page = $this->get_pagenum();
453 $total_items = self::record_count();
454
455 $this->set_pagination_args(array(
456 'total_items' => $total_items, //WE have to calculate the total number of items
457 'per_page' => $per_page //WE have to determine how many items to show on a page
458 ));
459
460 $this->items = self::get_results($per_page, $current_page);
461 }
462
463 public static function mark_as_read_reports( $id ) {
464 global $wpdb;
465 $wpdb->update(
466 "{$wpdb->prefix}ayspoll_reports",
467 array(
468 "unread" => 0
469 ),
470 array(
471 "id" => absint($id)
472 )
473 );
474 }
475
476 public static function delete_reports( $id ) {
477 global $wpdb;
478 $res_answ = self::get_report_by_id($id);
479
480 $res_votes_count = $wpdb->get_var(
481 "SELECT votes FROM {$wpdb->prefix}ayspoll_answers WHERE id =".$res_answ['answer_id']
482 );
483
484 if (intval($res_votes_count) > 0) {
485 $wpdb->update(
486 "{$wpdb->prefix}ayspoll_answers",
487 array(
488 "votes" => intval($res_votes_count) - 1
489 ),
490 array(
491 'id' => $res_answ['answer_id']
492 )
493 );
494 }
495
496 $res = $wpdb->delete(
497 "{$wpdb->prefix}ayspoll_reports",
498 array(
499 "id" => absint($id)
500 )
501 );
502
503 return $res;
504 }
505
506 public function process_bulk_action() {
507 // If the delete bulk action is triggered
508 if ((isset($_POST['action']) && $_POST['action'] == 'bulk-delete')
509 || (isset($_POST['action2']) && $_POST['action2'] == 'bulk-delete')
510 ) {
511 if (!empty($_POST['bulk-action']) && is_array($_POST['bulk-action'])) {
512 $delete_ids = array_map('intval', $_POST['bulk-action']);
513
514 foreach ($delete_ids as $id) {
515 self::delete_reports($id);
516 }
517 }
518 $message = 'deleted';
519 $url = esc_url_raw(remove_query_arg(['action', 'result', '_wpnonce'])) . '&ays_poll_tab_results=tab2' . '&status=' . $message;
520 wp_redirect($url);
521 } elseif ((isset($_POST['action']) && $_POST['action'] == 'bulk-read')
522 || (isset($_POST['action2']) && $_POST['action2'] == 'bulk-read')
523 ) {
524
525 $read_ids = esc_sql($_POST['bulk-action']);
526
527 // loop over the array of record IDs and mark as read them
528 foreach ( $read_ids as $id ) {
529 echo $id . "<br>";
530 self::mark_as_read_reports($id);
531 }
532
533 $message = 'read';
534 $url = esc_url_raw(remove_query_arg(['action', 'result', '_wpnonce'])) . '&ays_poll_tab_results=tab2' . '&status=' . $message;
535 wp_redirect($url);
536 }
537 }
538
539 public function eachresults_notices() {
540 $status = (isset($_REQUEST['status'])) ? sanitize_text_field($_REQUEST['status']) : '';
541
542 if (empty($status)) {
543 return;
544 }
545
546 if ('deleted' == $status) {
547 $updated_message = esc_html( esc_html__('Result(s) deleted.', "poll-maker"));
548 }
549 if ('read' == $status) {
550 $updated_message = esc_html( esc_html__('Result(s) marked as read.', "poll-maker"));
551 }
552
553 if (empty($updated_message)) {
554 return;
555 }
556
557 ?>
558 <div class="ays-poll-admin-notice notice notice-success is-dismissible">
559 <p>
560 <?php echo $updated_message; ?>
561 </p>
562 </div>
563 <?php
564 }
565 }