PluginProbe
Survey Maker by AYS / 5.2.1.8
Survey Maker by AYS v5.2.1.8
5.2.4.1 5.2.4.0 5.2.3.9 5.2.3.8 5.2.3.7 5.2.3.6 5.2.3.5 5.2.3.4 5.2.3.3 5.2.3.2 5.2.3.1 5.2.3.0 5.2.2.9 5.2.2.8 5.2.2.7 5.2.2.6 5.2.2.5 5.2.2.4 5.2.2.3 5.2.2.2 5.2.2.1 5.2.2.0 5.2.1.9 5.2.1.8 5.2.0.8 All 154 releases
survey-maker / includes / lists / class-survey-maker-each-submission-list-table.php

class-survey-maker-each-submission-list-table.php in Survey Maker by AYS 5.2.1.8, at includes/lists/class-survey-maker-each-submission-list-table.php

712 lines 26.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 ob_start();
3 class Survey_Each_Submission_List_Table extends WP_List_Table {
4 private $plugin_name;
5 /** Class constructor */
6 public function __construct($plugin_name) {
7 $this->plugin_name = $plugin_name;
8 parent::__construct( array(
9 'singular' => __( 'Each result', "survey-maker" ), //singular name of the listed records
10 'plural' => __( 'Each results', "survey-maker" ), //plural name of the listed records
11 'ajax' => false //does this table support ajax?
12 ) );
13 add_action( 'admin_notices', array( $this, 'each_results_notices' ) );
14 add_filter( 'default_hidden_columns', array( $this, 'get_hidden_columns'), 10, 2 );
15
16 }
17
18 /**
19 * Override of table nav to avoid breaking with bulk actions & according nonce field
20 */
21 public function display_tablenav( $which ) {
22 ?>
23 <div class="tablenav <?php echo esc_attr( $which ); ?>">
24
25 <div class="alignleft actions">
26 <?php $this->bulk_actions( $which ); ?>
27 </div>
28
29 <?php
30 $this->extra_tablenav( $which );
31 $this->pagination( $which );
32 ?>
33 <br class="clear" />
34 </div>
35 <?php
36 }
37
38 public function extra_tablenav( $which ){
39 global $wpdb;
40
41 if( is_super_admin() ){
42 $users_sql = "SELECT s.user_id
43 FROM " . $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "submissions AS s
44 WHERE survey_id = " . esc_sql( absint( $_GET['survey'] ) ) . "
45 GROUP BY user_id";
46 $users_res = $wpdb->get_results($users_sql, 'ARRAY_A');
47 $users = array();
48
49 $quiz_id = null;
50 $user_id = null;
51
52 if( isset( $_GET['filterbyuser'] )){
53 $user_id = absint( sanitize_text_field( $_GET['filterbyuser'] ) );
54 }
55
56 $clear_url = "?page=" . sanitize_text_field( $_REQUEST['page'] ) . "&survey=" . absint( sanitize_text_field( $_REQUEST['survey'] ) ) . "&ays_survey_tab=poststuff";
57 ?>
58 <div id="user-filter-div-<?php echo esc_attr( $which ); ?>" class="alignleft actions bulkactions">
59 <select name="filterbyuser-<?php echo esc_attr( $which ); ?>" id="bulk-action-selector-top2-<?php echo esc_attr( $which ); ?>">
60 <option value=""><?php echo esc_html__('Select User', "survey-maker"); ?></option>
61 <?php
62 foreach($users_res as $key => $user){
63 $selected = "";
64 if($user_id === intval($user['user_id'])){
65 $selected = "selected";
66 }
67 if(intval($user['user_id']) == 0){
68 $name = __( 'Guest', "survey-maker" );
69 }else{
70 $wpuser = get_userdata( intval($user['user_id']) );
71 $name = $wpuser ? $wpuser->data->display_name : __('Deleted User' , "survey-maker");
72 }
73 $users[$user['user_id']]['name'] = $name;
74 $users[$user['user_id']]['selected'] = $selected;
75 $users[$user['user_id']]['id'] = $user['user_id'];
76 }
77 sort($users);
78 foreach($users as $key => $user){
79 echo "<option ".esc_attr($user['selected'])." value='".esc_attr($user['id'])."'>".esc_attr($user['name'])."</option>";
80 }
81 ?>
82 </select>
83 <input type="button" id="doaction2-<?php echo esc_attr( $which ); ?>" class="user-filter-apply-<?php echo esc_attr( $which ); ?> button action ays-survey-question-tab-all-filter-button-<?php echo esc_attr( $which ); ?> ays-survey-question-filter-each-submission" value="Filter">
84 </div>
85 <a style="display:inline-block;" href="<?php echo esc_attr( $clear_url ); ?>" class="button action"><?php echo esc_html__( "Clear filters", "survey-maker" ); ?></a>
86 <?php
87 }
88 }
89
90 protected function get_views() {
91 $published_count = $this->readed_records_count();
92 $unpublished_count = $this->unread_records_count();
93 $all_count = $this->all_record_count();
94 $selected_all = "";
95 $selected_0 = "";
96 $selected_1 = "";
97 if(isset($_GET['fstatus'])){
98 switch( sanitize_text_field( $_GET['fstatus'] ) ){
99 case "unread":
100 $selected_0 = " style='font-weight:bold;' ";
101 break;
102 case "read":
103 $selected_1 = " style='font-weight:bold;' ";
104 break;
105 default:
106 $selected_all = " style='font-weight:bold;' ";
107 break;
108 }
109 }else{
110 $selected_all = " style='font-weight:bold;' ";
111 }
112 $link = "?page=" . esc_attr( sanitize_text_field( $_REQUEST['page'] ) ) . "&survey=" . esc_attr( sanitize_text_field( $_REQUEST['survey'] ) );
113 $status_links = array(
114 "all" => "<a ".$selected_all." href='".$link."&ays_survey_tab=poststuff'>". __("All", "survey-maker") ." (".$all_count.")</a>",
115 "readed" => "<a ".$selected_1." href='".$link."&fstatus=read&ays_survey_tab=poststuff'>". __("Read", "survey-maker") ." (".$published_count.")</a>",
116 "unreaded" => "<a ".$selected_0." href='".$link."&fstatus=unread&ays_survey_tab=poststuff'>". __("Unread", "survey-maker") . " (".$unpublished_count.")</a>"
117 );
118 return $status_links;
119 }
120
121 /**
122 * Retrieve customers data from the database
123 *
124 * @param int $per_page
125 * @param int $page_number
126 *
127 * @return mixed
128 */
129 public static function get_results( $per_page = 50, $page_number = 1 ) {
130
131 global $wpdb;
132 $sql = "SELECT * FROM " . $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "submissions";
133
134 $sql .= self::get_where_condition();
135
136 if ( ! empty( $_REQUEST['orderby'] ) ) {
137 $order_by = ( isset( $_REQUEST['orderby'] ) && sanitize_text_field( $_REQUEST['orderby'] ) != '' ) ? sanitize_text_field( $_REQUEST['orderby'] ) : 'id';
138 $order_by .= ( ! empty( $_REQUEST['order'] ) && strtolower( $_REQUEST['order'] ) == 'asc' ) ? ' ASC' : ' DESC';
139
140 $sql_orderby = sanitize_sql_orderby( $order_by );
141
142 if ( $sql_orderby ) {
143 $sql .= ' ORDER BY ' . $sql_orderby;
144 } else {
145 $sql .= ' ORDER BY id DESC';
146 }
147 }else{
148 $sql .= ' ORDER BY id DESC';
149 }
150
151 $sql .= " LIMIT $per_page";
152 $sql .= ' OFFSET ' . ( $page_number - 1 ) * $per_page;
153
154 $result = $wpdb->get_results( $sql, 'ARRAY_A' );
155 return $result;
156 }
157
158 public static function get_where_condition(){
159 global $wpdb;
160 $where = array();
161 $sql = '';
162
163 $search = ( isset( $_REQUEST['s'] ) ) ? sanitize_text_field( $_REQUEST['s'] ) : false;
164 if( $search ){
165 $s = array();
166 $s[] = sprintf( "`user_name` LIKE '%%%s%%' ", esc_sql( $wpdb->esc_like( $search ) ) );
167 $s[] = sprintf( "`user_email` LIKE '%%%s%%' ", esc_sql( $wpdb->esc_like( $search ) ) );
168 $s[] = sprintf( "POSITION( '%%%s%%' IN `submission_date` )", esc_sql( $wpdb->esc_like( $search ) ) );
169 $s[] = sprintf( "`unique_code` LIKE '%%%s%%' ", esc_sql( $wpdb->esc_like( $search ) ) );
170 // $s[] = ' `score` LIKE \'%'.$search.'%\' ';
171 $where[] = ' ( ' . implode(' OR ', $s) . ' ) ';
172 }
173
174 if ( isset( $_GET['fstatus'] ) && $_GET['fstatus'] != ''){
175 if( $_GET['fstatus'] == 'read' ){
176 $read = 1;
177 }elseif( $_GET['fstatus'] == 'unread' ){
178 $read = 2;
179 }
180
181 $where[] = ' `read` = ' . esc_sql( $read ) . ' ';
182 }
183
184 if( isset( $_REQUEST['filterbyuser'] ) ){
185 $user_id = absint( sanitize_text_field( $_REQUEST['filterbyuser'] ) );
186 $where[] = ' `user_id` = '.$user_id.' ';
187 }
188
189 if( isset( $_REQUEST['survey'] ) ){
190 $quiz_id = absint( sanitize_text_field( $_REQUEST['survey'] ) );
191 $where[] = ' `survey_id` = '.$quiz_id.' ';
192 }
193
194 if( ! empty($where) ){
195 $sql = " WHERE " . implode( " AND ", $where );
196 }
197 return $sql;
198 }
199
200
201 /**
202 * Delete a customer record.
203 *
204 * @param int $id customer ID
205 */
206 public static function delete_items( $id ) {
207 global $wpdb;
208 $wpdb->delete(
209 $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "submissions",
210 array('id' => $id),
211 array('%d')
212 );
213
214 $wpdb->delete(
215 $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "submissions_questions",
216 array('submission_id' => $id),
217 array('%d')
218 );
219 }
220
221
222 /**
223 * Mark as read a customer record.
224 *
225 * @param int $id customer ID
226 */
227 public static function mark_as_read_reports( $id ) {
228 global $wpdb;
229 $wpdb->update(
230 $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "submissions",
231 array('read' => 1),
232 array('id' => $id),
233 array('%d'),
234 array('%d')
235 );
236 }
237
238 /**
239 * Mark as unread a customer record.
240 *
241 * @param int $id customer ID
242 */
243 public static function mark_as_unread_reports( $id ) {
244 global $wpdb;
245 $wpdb->update(
246 $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "submissions",
247 array('read' => 2),
248 array('id' => $id),
249 array('%d'),
250 array('%d')
251 );
252 }
253
254 /**
255 * Returns the count of records in the database.
256 *
257 * @return null|string
258 */
259 public static function record_count() {
260 global $wpdb;
261
262 $sql = "SELECT COUNT(*) FROM " . $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "submissions";
263 $sql .= self::get_where_condition();
264 return $wpdb->get_var( $sql );
265 }
266
267 public static function all_record_count() {
268 global $wpdb;
269
270 $survey_id = absint( sanitize_text_field( $_REQUEST['survey'] ) );
271 $survey_id = ' WHERE `survey_id` = '.$survey_id.' ';
272 $sql = "SELECT COUNT(*) FROM " . $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "submissions ".$survey_id;
273
274 return $wpdb->get_var( $sql );
275 }
276
277 public static function unread_records_count() {
278 global $wpdb;
279
280 $survey_id = absint( sanitize_text_field( $_REQUEST['survey'] ) );
281 $survey_id = ' AND `survey_id` = '.$survey_id.' ';
282 $sql = "SELECT COUNT(*) FROM " . $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "submissions WHERE ( `read` = 0 OR `read` = 2 ) ".$survey_id;
283
284 return $wpdb->get_var( $sql );
285 }
286
287 public function readed_records_count() {
288 global $wpdb;
289
290 $survey_id = absint( sanitize_text_field( $_REQUEST['survey'] ) );
291 $survey_id = ' AND `survey_id` = '.$survey_id.' ';
292 $sql = "SELECT COUNT(*) FROM " . $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "submissions WHERE `read` = 1 ".$survey_id;
293
294 return $wpdb->get_var( $sql );
295 }
296
297 /** Text displayed when no customer data is available */
298 public function no_items() {
299 Survey_Maker_Data::survey_no_items_list_tables();
300 }
301
302 /**
303 * Render a column when no column specific method exist.
304 *
305 * @param array $item
306 * @param string $column_name
307 *
308 * @return mixed
309 */
310 public function column_default( $item, $column_name ) {
311 switch ($column_name) {
312 case 'user_id':
313 case 'user_ip':
314 case 'user_name':
315 case 'user_email':
316 case 'submission_date':
317 case 'unique_code':
318 case 'id':
319 return $item[$column_name];
320 break;
321 default:
322 return print_r($item, true); //Show the whole array for troubleshooting purposes
323 }
324 }
325
326 /**
327 * Render the bulk edit checkbox
328 *
329 * @param array $item
330 *
331 * @return string
332 */
333 public function column_cb( $item ) {
334 return sprintf(
335 '<input type="checkbox" class="ays_result_delete ays-survey-no-trigger" name="bulk-delete[]" value="%s" />', $item['id']
336 );
337 }
338
339 public function column_user_id( $item ) {
340 global $wpdb;
341
342 $delete_nonce = wp_create_nonce( $this->plugin_name . '-delete-each-result' );
343
344 $sql = "SELECT * FROM ".$wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "submissions" . " WHERE id=". absint( $item['id'] );
345 $result = $wpdb->get_row($sql, "ARRAY_A");
346
347 $user_id = absint( $item['user_id'] );
348 $class_red = '';
349 if($user_id == 0){
350 $name = __( "Guest", "survey-maker" );
351 }else{
352 $user = get_userdata($user_id);
353 if($user !== false){
354 $name = $user->data->display_name;
355 }else{
356 $name = __( "Deleted user", "survey-maker" );
357 $class_red = ' ays_color_red ';
358 }
359 }
360
361 if( intval( $item['read'] ) == 1 ){
362 $title = $name; //sprintf( '<a href="javascript:void(0)" data-result="%d" class="%s">%s</a><input type="hidden" value="%d" class="ays_result_read">', absint( $item['id'] ), 'ays-show-results'.$class_red, $name, $item['read']);
363 }else{
364 $title = sprintf( '%s<input type="hidden" value="%d" class="ays_result_read">', $name, absint( $item['read'] ) );
365 }
366 $title .= sprintf( '<input type="hidden" value="%d" class="ays_survey_result">', absint( $item['id']) );
367 $actions = array(
368 'view-details' => sprintf( '<a href="javascript:void(0);" data-result="%d" class="%s">%s</a>', absint( $item['id'] ), 'ays-show-results', __('Detailed report', "survey-maker")),
369
370 // 'view_detailed' => sprintf( '<a class="ays-survey-no-trigger ays-survey-view-detailed-button" data-message="this submission" href="#">%s</a>', __('View Detailed', "survey-maker") ),
371 'delete' => sprintf( '<a class="ays_confirm_del ays-survey-no-trigger" data-message="this submission" href="?page=%s&action=%s&survey=%s&item=%s&_wpnonce=%s">%s</a>', sanitize_text_field( $_REQUEST['page'] ), 'delete', absint( $result['survey_id'] ), absint( $item['id'] ), $delete_nonce, __('Delete', "survey-maker") )
372 );
373
374 return $title . $this->row_actions( $actions ) ;
375 }
376
377 public function column_duration( $item ) {
378 global $wpdb;
379
380 $passed_time = (isset($item['duration'])) ? $item['duration'] : null;
381 if($passed_time !== null){
382 $title = $passed_time . "s";
383 }else{
384 $title = __('No data', "survey-maker");
385 }
386
387 return $title;
388 }
389
390 public function column_unique_code( $item ) {
391 global $wpdb;
392 $unique_code = isset($item['unique_code']) && $item['unique_code'] != '' ? $item['unique_code'] : '<p style="text-align:center;">-</p>';
393 $unique_code_html = "<strong style='text-transform:uppercase!important;white-space:nowrap;'>" . $unique_code . "</strong>";
394 return $unique_code_html;
395 }
396
397 public function column_user_name( $item ) {
398 global $wpdb;
399 $user_name = isset($item['user_name']) && $item['user_name'] != '' ? stripslashes(nl2br( htmlentities( $item['user_name'] ) ) ) : '';
400 return $user_name;
401
402 }
403
404 public function column_user_email( $item ) {
405 global $wpdb;
406 $user_email = isset($item['user_email']) && $item['user_email'] != '' ? stripslashes(nl2br( htmlentities( $item['user_email'] ) ) ) : '';
407 return $user_email;
408
409 }
410
411 /**
412 * Associative array of columns
413 *
414 * @return array
415 */
416 public function get_columns() {
417 $columns = array(
418 'cb' => '<input type="checkbox" />',
419 'user_id' => __( 'WP User', "survey-maker" ),
420 'user_ip' => __( 'User IP', "survey-maker" ),
421 'user_name' => __( 'User Name', "survey-maker" ),
422 'user_email' => __( 'User Email', "survey-maker" ),
423 'submission_date' => __( 'Submission Date', "survey-maker" ),
424 'unique_code' => __( 'Unique Code', "survey-maker" ),
425 'id' => __( 'ID', "survey-maker" ),
426 );
427
428 return $columns;
429 }
430
431 public function ays_see_all_results( $mark_as_read_ids ){
432 global $wpdb;
433 if( empty( $mark_as_read_ids ) ){
434 return false;
435 }
436 $mark_as_read_ids = implode( ',', $mark_as_read_ids );
437 $sql = "UPDATE {$wpdb->prefix}aysquiz_reports
438 SET `read`=1
439 WHERE `id` IN (".$mark_as_read_ids.");";
440 $wpdb->query( $sql );
441 }
442
443 public function mark_as_read_all_results( $survey_id ){
444 global $wpdb;
445 $wpdb->update(
446 $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "submissions",
447 array( 'read' => 1 ),
448 array(
449 'read' => 0,
450 'survey_id' => $survey_id,
451 ),
452 array( '%d' ),
453 array( '%d', '%d' )
454 );
455 }
456
457 /**
458 * Columns to make sortable.
459 *
460 * @return array
461 */
462 public function get_sortable_columns() {
463 $sortable_columns = array(
464 'id' => array( 'id', true ),
465 'user_id' => array( 'user_id', true ),
466 'user_ip' => array( 'user_ip', true ),
467 'user_name' => array( 'user_name', true ),
468 'user_email' => array( 'user_email', true ),
469 'submission_date' => array( 'submission_date', true ),
470 'score' => array( 'score', true ),
471 'unique_code' => array( 'unique_code', true ),
472 );
473
474 return $sortable_columns;
475 }
476
477 /**
478 * Columns to make hidden.
479 *
480 * @return array
481 */
482 public function get_hidden_columns() {
483 $sortable_columns = array(
484 'unique_code'
485 );
486
487 return $sortable_columns;
488 }
489
490 /**
491 * Returns an associative array containing the bulk action
492 *
493 * @return array
494 */
495 public function get_bulk_actions() {
496 $actions = array(
497 'mark-as-read' => __( 'Mark as read', "survey-maker"),
498 'mark-as-unread' => __( 'Mark as unread', "survey-maker"),
499 'bulk-delete' => __( 'Delete', "survey-maker"),
500 );
501
502 return $actions;
503 }
504
505 /**
506 * Handles data query and filter, sorting, and pagination.
507 */
508 public function prepare_items() {
509
510 $this->_column_headers = $this->get_column_info();
511
512 /** Process bulk action */
513 $this->process_bulk_action();
514
515 $per_page = $this->get_items_per_page( 'quiz_each_results_per_page', 50 );
516 $current_page = $this->get_pagenum();
517 $total_items = self::record_count();
518
519 $this->set_pagination_args( array(
520 'total_items' => $total_items, //WE have to calculate the total number of items
521 'per_page' => $per_page //WE have to determine how many items to show on a page
522 ) );
523
524 $this->items = self::get_results( $per_page, $current_page);
525 }
526
527 public function process_bulk_action() {
528 //Detect when a bulk action is being triggered...
529 $message = 'deleted';
530 if ( 'delete' === $this->current_action() ) {
531
532 // In our file that handles the request, verify the nonce.
533 $nonce = sanitize_text_field( $_REQUEST['_wpnonce'] );
534
535 if ( ! wp_verify_nonce( $nonce, $this->plugin_name . '-delete-each-result' ) ) {
536 die( 'Go get a life script kiddies' );
537 } else {
538 self::delete_items( absint( sanitize_text_field($_GET['item']) ) );
539
540 // esc_url_raw() is used to prevent converting ampersand in url to "#038;"
541 // add_query_arg() return the current url
542
543 $url = esc_url_raw( remove_query_arg(array('action', 'item', '_wpnonce') ) );
544
545 $url = add_query_arg( array(
546 'status' => $message,
547 'ays_survey_tab' => 'poststuff',
548 ), $url );
549 wp_redirect( $url );
550 }
551
552 }
553
554
555 // If the mark-as-read bulk action is triggered
556 if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'mark-as-read' ) || ( isset( $_POST['action2'] ) && $_POST['action2'] == 'mark-as-read' ) ) {
557
558 $delete_ids = ( isset( $_POST['bulk-delete'] ) && ! empty( $_POST['bulk-delete'] ) ) ? esc_sql( $_POST['bulk-delete'] ) : array();
559
560 // loop over the array of record IDs and delete them
561 foreach ( $delete_ids as $id ) {
562 self::mark_as_read_reports( $id );
563 }
564
565 // esc_url_raw() is used to prevent converting ampersand in url to "#038;"
566 // add_query_arg() return the current url
567
568 $url = esc_url_raw( remove_query_arg(array('action', 'item', '_wpnonce') ) );
569
570 $message = 'marked-as-read';
571 $url = add_query_arg( array(
572 'status' => $message,
573 'ays_survey_tab' => 'poststuff',
574 ), $url );
575
576 wp_redirect( $url );
577 }
578
579 // If the mark-as-unread bulk action is triggered
580 if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'mark-as-unread' ) || ( isset( $_POST['action2'] ) && $_POST['action2'] == 'mark-as-unread' ) ) {
581
582 $delete_ids = ( isset( $_POST['bulk-delete'] ) && ! empty( $_POST['bulk-delete'] ) ) ? esc_sql( $_POST['bulk-delete'] ) : array();
583
584 // loop over the array of record IDs and delete them
585 foreach ( $delete_ids as $id ) {
586 self::mark_as_unread_reports( $id );
587 }
588
589 // esc_url_raw() is used to prevent converting ampersand in url to "#038;"
590 // add_query_arg() return the current url
591
592 $url = esc_url_raw( remove_query_arg(array('action', 'item', '_wpnonce') ) );
593
594 $message = 'marked-as-unread';
595 $url = add_query_arg( array(
596 'status' => $message,
597 'ays_survey_tab' => 'poststuff',
598 ), $url );
599
600 wp_redirect( $url );
601 }
602
603 // If the delete bulk action is triggered
604 if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'bulk-delete' ) || ( isset( $_POST['action2'] ) && $_POST['action2'] == 'bulk-delete' ) ) {
605
606 $delete_ids = ( isset( $_POST['bulk-delete'] ) && ! empty( $_POST['bulk-delete'] ) ) ? esc_sql( $_POST['bulk-delete'] ) : array();
607
608 // loop over the array of record IDs and delete them
609 foreach ( $delete_ids as $id ) {
610 self::delete_items( $id );
611 }
612
613 // esc_url_raw() is used to prevent converting ampersand in url to "#038;"
614 // add_query_arg() return the current url
615 $message = 'are-deleted';
616 $url = esc_url_raw( remove_query_arg(array('action', 'item', '_wpnonce') ) );
617
618 $url = add_query_arg( array(
619 'status' => $message,
620 'ays_survey_tab' => 'poststuff',
621 ), $url );
622
623 wp_redirect( $url );
624 }
625 }
626
627 public function each_results_notices(){
628 $status = (isset($_REQUEST['status'])) ? sanitize_text_field( $_REQUEST['status'] ) : '';
629
630 if ( empty( $status ) )
631 return;
632
633
634 if ( 'deleted' == $status )
635 $updated_message = __( 'Submission deleted.', "survey-maker" );
636 elseif ( 'seen' == $status )
637 $updated_message = __( 'Selected submissions have been marked as read.', "survey-maker" );
638 elseif ( 'marked-as-read' == $status )
639 $updated_message = __( 'Selected submissions have been marked as read.', "survey-maker" );
640 elseif ( 'marked-as-unread' == $status )
641 $updated_message = __( 'Selected submissions have been marked as unread.', "survey-maker" );
642 elseif ( 'are-deleted' == $status )
643 $updated_message = __( 'Selected submissions are deleted successfully.', "survey-maker" );
644
645 if ( empty( $updated_message ) )
646 return;
647
648 ?>
649 <div class="ays-survey-admin-notice notice notice-success is-dismissible">
650 <p> <?php echo esc_html($updated_message); ?> </p>
651 </div>
652 <?php
653 }
654
655 public function get_submision_line_chart($survey_id=0){
656 global $wpdb;
657
658 $sql = "SELECT DATE(`end_date`) AS date, COUNT(*) AS value FROM `{$wpdb->prefix}ayssurvey_submissions` WHERE `survey_id` = ". absint( $survey_id ) ." GROUP BY date";
659 $result = $wpdb->get_results( $sql, 'ARRAY_A' );
660
661 foreach ($result as $key => &$value) {
662 $value['value'] = intval($value['value']);
663 $value = array_values($value);
664 }
665
666 return $result;
667 }
668
669 public static function survey_users_count() {
670 global $wpdb;
671 global $wp_roles;
672
673 $survey_id = absint( sanitize_text_field( $_GET['survey'] ) );
674 $sql = "SELECT COUNT(*) FROM {$wpdb->prefix}ayssurvey_submissions WHERE survey_id = ". $survey_id ." AND user_id = 0";
675 $guests = $wpdb->get_var( $sql );
676 $sql = "SELECT COUNT(`{$wpdb->prefix}ayssurvey_submissions`.`id`) AS q, `{$wpdb->prefix}usermeta`.`meta_value` AS v
677 FROM `{$wpdb->prefix}ayssurvey_submissions`
678 JOIN `{$wpdb->prefix}usermeta`
679 ON `{$wpdb->prefix}usermeta`.`user_id` = `{$wpdb->prefix}ayssurvey_submissions`.`user_id`
680 WHERE `{$wpdb->prefix}ayssurvey_submissions`.`user_id` != 0
681 AND `{$wpdb->prefix}ayssurvey_submissions`.`survey_id` = ". $survey_id ."
682 AND `{$wpdb->prefix}usermeta`.`meta_key` = '{$wpdb->prefix}capabilities'
683 GROUP BY `{$wpdb->prefix}usermeta`.`meta_value`";
684 $results = $wpdb->get_results( $sql );
685 $user_roles = array();
686 $logged_in = 0;
687 foreach($results as $key => $value){
688 $role = maybe_unserialize($value->v);
689 if(is_array($role)){
690 while ($fruit_name = current($role)) {
691 if(array_key_exists(key($role), $wp_roles->roles)){
692 $user_roles[$key]['type'] = $wp_roles->roles[ key($role) ]['name'];
693 }
694 next($role);
695 }
696 }else{
697 $user_roles[$key]['type'] = $wp_roles->roles[ key($role) ]['name'];
698 }
699
700 $user_roles[$key]['percent'] = $value->q;
701
702 $logged_in += intval($value->q);
703 }
704
705 return array(
706 "guests" => $guests,
707 "loggedIn" => $logged_in,
708 "userRoles" => $user_roles
709 );
710 }
711 }
712