| 1 |
<?php |
| 2 |
ob_start(); |
| 3 |
class Submissions_List_Table extends WP_List_Table { |
| 4 |
private $plugin_name; |
| 5 |
private $title_length; |
| 6 |
/** Class constructor */ |
| 7 |
public function __construct($plugin_name) { |
| 8 |
$this->plugin_name = $plugin_name; |
| 9 |
$this->title_length = Survey_Maker_Data::get_listtables_title_length('submissions'); |
| 10 |
parent::__construct( array( |
| 11 |
'singular' => __( 'Submissions', "survey-maker" ), //singular name of the listed records |
| 12 |
'plural' => __( 'Submissions', "survey-maker" ), //plural name of the listed records |
| 13 |
'ajax' => false //does this table support ajax? |
| 14 |
) ); |
| 15 |
add_action( 'admin_notices', array( $this, 'submissions_notices' ) ); |
| 16 |
|
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Override of table nav to avoid breaking with bulk actions & according nonce field |
| 21 |
*/ |
| 22 |
public function display_tablenav( $which ) { |
| 23 |
?> |
| 24 |
<div class="tablenav <?php echo esc_attr( $which ); ?>"> |
| 25 |
|
| 26 |
<div class="alignleft actions"> |
| 27 |
<?php $this->bulk_actions( $which ); ?> |
| 28 |
</div> |
| 29 |
|
| 30 |
<?php |
| 31 |
$this->extra_tablenav( $which ); |
| 32 |
$this->pagination( $which ); |
| 33 |
?> |
| 34 |
<br class="clear" /> |
| 35 |
</div> |
| 36 |
<?php |
| 37 |
} |
| 38 |
|
| 39 |
public function extra_tablenav( $which ){ |
| 40 |
global $wpdb; |
| 41 |
|
| 42 |
$titles_sql = "SELECT s.title, s.id |
| 43 |
FROM " .$wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "survey_categories AS s |
| 44 |
WHERE s.status = 'published'"; |
| 45 |
$cat_titles = $wpdb->get_results($titles_sql); |
| 46 |
|
| 47 |
$cat_id = null; |
| 48 |
if( isset( $_GET['filterby'] )){ |
| 49 |
$cat_id = absint( sanitize_text_field( $_GET['filterby'] ) ); |
| 50 |
} |
| 51 |
$categories_select = array(); |
| 52 |
foreach($cat_titles as $key => $cat_title){ |
| 53 |
$selected = ""; |
| 54 |
if($cat_id === intval($cat_title->id)){ |
| 55 |
$selected = "selected"; |
| 56 |
} |
| 57 |
$categories_select[$cat_title->id]['title'] = $cat_title->title; |
| 58 |
$categories_select[$cat_title->id]['selected'] = $selected; |
| 59 |
$categories_select[$cat_title->id]['id'] = $cat_title->id; |
| 60 |
} |
| 61 |
sort($categories_select); |
| 62 |
?> |
| 63 |
<div id="category-filter-div-surveylist-<?php echo esc_attr($which);?>" class="alignleft actions bulkactions"> |
| 64 |
<select name="filterby-<?php echo esc_attr( $which ); ?>" id="survey-category-filter-<?php echo esc_attr( $which ); ?>"> |
| 65 |
<option value=""><?php echo esc_html__('Select Category',"survey-maker")?></option> |
| 66 |
<?php |
| 67 |
foreach($categories_select as $key => $cat_title){ |
| 68 |
echo "<option ".esc_attr($cat_title['selected'])." value='".esc_attr($cat_title['id'])."'>".esc_attr($cat_title['title'])."</option>"; |
| 69 |
} |
| 70 |
?> |
| 71 |
</select> |
| 72 |
<input type="button" id="doaction-<?php echo esc_attr( $which ); ?>" class="cat-filter-apply-<?php echo esc_attr( $which ); ?> button action ays-survey-question-tab-all-filter-button-<?php echo esc_attr( $which ); ?>" value="Filter"> |
| 73 |
</div> |
| 74 |
|
| 75 |
<a style="margin: 0px 8px 0 0;" href="?page=<?php echo esc_attr( sanitize_text_field( $_REQUEST['page'] ) ); ?>" class="button action"><?php echo esc_html__( "Clear filters", "survey-maker" ); ?></a> |
| 76 |
<?php |
| 77 |
|
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Retrieve customers data from the database |
| 83 |
* |
| 84 |
* @param int $per_page |
| 85 |
* @param int $page_number |
| 86 |
* |
| 87 |
* @return mixed |
| 88 |
*/ |
| 89 |
public static function get_items( $per_page = 50, $page_number = 1 ) { |
| 90 |
|
| 91 |
global $wpdb; |
| 92 |
$sql = "SELECT * FROM " . $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "surveys"; |
| 93 |
|
| 94 |
$sql .= self::get_where_condition(); |
| 95 |
|
| 96 |
if ( ! empty( $_REQUEST['orderby'] ) ) { |
| 97 |
$order_by = ( isset( $_REQUEST['orderby'] ) && sanitize_text_field( $_REQUEST['orderby'] ) != '' ) ? sanitize_text_field( $_REQUEST['orderby'] ) : 'id'; |
| 98 |
$order_by .= ( ! empty( $_REQUEST['order'] ) && strtolower( $_REQUEST['order'] ) == 'asc' ) ? ' ASC' : ' DESC'; |
| 99 |
|
| 100 |
$sql_orderby = sanitize_sql_orderby( $order_by ); |
| 101 |
|
| 102 |
if ( $sql_orderby ) { |
| 103 |
$sql .= ' ORDER BY ' . $sql_orderby; |
| 104 |
} else { |
| 105 |
$sql .= ' ORDER BY id DESC'; |
| 106 |
} |
| 107 |
}else{ |
| 108 |
$sql .= ' ORDER BY id DESC'; |
| 109 |
} |
| 110 |
|
| 111 |
$sql .= " LIMIT $per_page"; |
| 112 |
$sql .= ' OFFSET ' . ( $page_number - 1 ) * $per_page; |
| 113 |
|
| 114 |
$result = $wpdb->get_results( $sql, 'ARRAY_A' ); |
| 115 |
return $result; |
| 116 |
} |
| 117 |
|
| 118 |
public static function get_where_condition(){ |
| 119 |
global $wpdb; |
| 120 |
$where = array(); |
| 121 |
$sql = ''; |
| 122 |
|
| 123 |
// $search = ( isset( $_REQUEST['s'] ) ) ? esc_sql( $_REQUEST['s'] ) : false; |
| 124 |
// if( $search ){ |
| 125 |
// $s = array(); |
| 126 |
// $s[] = ' `user_name` LIKE \'%'.$search.'%\' '; |
| 127 |
// $s[] = ' `user_email` LIKE \'%'.$search.'%\' '; |
| 128 |
// $s[] = ' `user_phone` LIKE \'%'.$search.'%\' '; |
| 129 |
// $s[] = ' `score` LIKE \'%'.$search.'%\' '; |
| 130 |
// $where[] = ' ( ' . implode(' OR ', $s) . ' ) '; |
| 131 |
// } |
| 132 |
|
| 133 |
// if(isset( $_REQUEST['fstatus'] )){ |
| 134 |
// $fstatus = intval($_REQUEST['fstatus']); |
| 135 |
// switch($fstatus){ |
| 136 |
// case 0: |
| 137 |
// $where[] = ' `read` = 0 '; |
| 138 |
// break; |
| 139 |
// case 1: |
| 140 |
// $where[] = ' `read` = 1 '; |
| 141 |
// break; |
| 142 |
// } |
| 143 |
// } |
| 144 |
|
| 145 |
if(! empty( $_REQUEST['filterby'] ) && intval( $_REQUEST['filterby'] ) > 0){ |
| 146 |
$cat_id = intval( sanitize_text_field( $_REQUEST['filterby'] ) ); |
| 147 |
$where[] = ' FIND_IN_SET( "'.$cat_id.'", `category_ids` ) '; |
| 148 |
} |
| 149 |
|
| 150 |
if( isset( $_REQUEST['wpuser'] ) ){ |
| 151 |
$user_id = absint( sanitize_text_field( $_REQUEST['wpuser'] ) ); |
| 152 |
$where[] = ' `user_id` = '.$user_id.' '; |
| 153 |
} |
| 154 |
|
| 155 |
if( isset( $_REQUEST['survey'] ) ){ |
| 156 |
$quiz_id = absint( sanitize_text_field( $_REQUEST['survey'] ) ); |
| 157 |
$where[] = ' `survey_id` = '.$quiz_id.' '; |
| 158 |
} |
| 159 |
|
| 160 |
if( isset( $_REQUEST['s'] ) && $_REQUEST['s'] != "" ){ |
| 161 |
$each_survey = sanitize_text_field( $_REQUEST['s'] ); |
| 162 |
$do_search = ( $each_survey ) ? sprintf( " title LIKE '%%%s%%' ", esc_sql( $wpdb->esc_like( $each_survey ) ) ) : ''; |
| 163 |
$where[] = $do_search; |
| 164 |
} |
| 165 |
|
| 166 |
$where[] = ' `status` != "trashed" '; |
| 167 |
|
| 168 |
if( ! empty($where) ){ |
| 169 |
$sql = " WHERE " . implode( " AND ", $where ); |
| 170 |
} |
| 171 |
return $sql; |
| 172 |
} |
| 173 |
|
| 174 |
public static function get_report_by_quiz_id( $quiz_id ){ |
| 175 |
global $wpdb; |
| 176 |
|
| 177 |
$sql = "SELECT * FROM " . $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "survey_categories WHERE quiz_id=" . absint( $quiz_id ); |
| 178 |
|
| 179 |
$result = $wpdb->get_row($sql, 'ARRAY_A'); |
| 180 |
|
| 181 |
return $result; |
| 182 |
} |
| 183 |
|
| 184 |
public function get_report_by_id( $id ){ |
| 185 |
global $wpdb; |
| 186 |
|
| 187 |
$sql = "SELECT * FROM {$wpdb->prefix}aysquiz_reports WHERE id=" . absint( $id ); |
| 188 |
|
| 189 |
$result = $wpdb->get_row($sql, 'ARRAY_A'); |
| 190 |
|
| 191 |
return $result; |
| 192 |
} |
| 193 |
|
| 194 |
public function get_reports_titles(){ |
| 195 |
global $wpdb; |
| 196 |
|
| 197 |
$sql = "SELECT id, title FROM {$wpdb->prefix}aysquiz_quizes"; |
| 198 |
|
| 199 |
$result = $wpdb->get_results($sql, 'ARRAY_A'); |
| 200 |
|
| 201 |
return $result; |
| 202 |
} |
| 203 |
|
| 204 |
public static function get_results_dates($quiz_id){ |
| 205 |
global $wpdb; |
| 206 |
|
| 207 |
$sql = "SELECT MIN(DATE(`end_date`)) AS `min_date`, MAX(DATE(`end_date`)) AS `max_date` |
| 208 |
FROM {$wpdb->prefix}aysquiz_reports |
| 209 |
WHERE YEAR(DATE(`end_date`)) > 2000 "; |
| 210 |
if($quiz_id !== 0){ |
| 211 |
$sql .= " AND quiz_id = ". absint( $quiz_id ) ." "; |
| 212 |
} |
| 213 |
|
| 214 |
$result = $wpdb->get_row($sql, 'ARRAY_A'); |
| 215 |
|
| 216 |
return $result; |
| 217 |
} |
| 218 |
|
| 219 |
public function ays_see_all_results(){ |
| 220 |
global $wpdb; |
| 221 |
$sql = "UPDATE {$wpdb->prefix}aysquiz_reports SET `read`=1"; |
| 222 |
$wpdb->get_results($sql, 'ARRAY_A'); |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Delete a customer record. |
| 227 |
* |
| 228 |
* @param int $id customer ID |
| 229 |
*/ |
| 230 |
public static function delete_items( $id ) { |
| 231 |
global $wpdb; |
| 232 |
|
| 233 |
$wpdb->delete( |
| 234 |
$wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "submissions", |
| 235 |
array( 'survey_id' => $id ), |
| 236 |
array( '%d' ) |
| 237 |
); |
| 238 |
|
| 239 |
$wpdb->delete( |
| 240 |
$wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "submissions_questions", |
| 241 |
array( 'survey_id' => $id ), |
| 242 |
array( '%d' ) |
| 243 |
); |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Returns the count of records in the database. |
| 248 |
* |
| 249 |
* @return null|string |
| 250 |
*/ |
| 251 |
public static function record_count() { |
| 252 |
global $wpdb; |
| 253 |
|
| 254 |
$sql = "SELECT COUNT(*) FROM " . $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "surveys WHERE status != 'trashed'"; |
| 255 |
|
| 256 |
if( isset( $_GET['filterby'] ) && intval(sanitize_text_field($_GET['filterby'])) > 0){ |
| 257 |
$cat_id = intval( sanitize_text_field( $_GET['filterby'] ) ); |
| 258 |
$sql .= '&& FIND_IN_SET('.$cat_id.',category_ids) '; |
| 259 |
} |
| 260 |
|
| 261 |
if( isset( $_REQUEST['s'] ) && $_REQUEST['s'] != "" ){ |
| 262 |
$each_survey = sanitize_text_field( $_REQUEST['s'] ); |
| 263 |
$do_search = ( $each_survey ) ? sprintf( " title LIKE '%%%s%%' ", esc_sql( $wpdb->esc_like( $each_survey ) ) ) : ''; |
| 264 |
$sql .= 'AND '.$do_search; |
| 265 |
} |
| 266 |
|
| 267 |
return $wpdb->get_var( $sql ); |
| 268 |
} |
| 269 |
|
| 270 |
public static function unread_records_count( $id ) { |
| 271 |
global $wpdb; |
| 272 |
|
| 273 |
$sql = "SELECT COUNT(*) |
| 274 |
FROM ".$wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "submissions " . " |
| 275 |
WHERE ( `read` = 0 OR `read` = 2 ) AND survey_id=".absint( $id ); |
| 276 |
// $sql = "SELECT COUNT(*) FROM {$wpdb->prefix}aysquiz_reports WHERE `read` = 0;"; |
| 277 |
|
| 278 |
return $wpdb->get_var( $sql ); |
| 279 |
} |
| 280 |
|
| 281 |
public static function record_complete_filter_count() { |
| 282 |
global $wpdb; |
| 283 |
|
| 284 |
$sql = "SELECT COUNT(*) FROM (SELECT DISTINCT `quiz_id` FROM {$wpdb->prefix}aysquiz_reports ) AS quiz_ids"; |
| 285 |
|
| 286 |
return $wpdb->get_var( $sql ); |
| 287 |
} |
| 288 |
|
| 289 |
/** Text displayed when no customer data is available */ |
| 290 |
public function no_items() { |
| 291 |
Survey_Maker_Data::survey_no_items_list_tables(); |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Render a column when no column specific method exist. |
| 296 |
* |
| 297 |
* @param array $item |
| 298 |
* @param string $column_name |
| 299 |
* |
| 300 |
* @return mixed |
| 301 |
*/ |
| 302 |
public function column_default( $item, $column_name ) { |
| 303 |
switch ( $column_name ) { |
| 304 |
case 'quiz_title': |
| 305 |
case 'quiz_rate': |
| 306 |
case 'score': |
| 307 |
case 'user_count': |
| 308 |
case 'unreads': |
| 309 |
case 'category_ids': |
| 310 |
case 'last_sub_date': |
| 311 |
case 'id': |
| 312 |
return $item[ $column_name ]; |
| 313 |
break; |
| 314 |
default: |
| 315 |
return print_r( $item, true ); //Show the whole array for troubleshooting purposes |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* Render the bulk edit checkbox |
| 321 |
* |
| 322 |
* @param array $item |
| 323 |
* |
| 324 |
* @return string |
| 325 |
*/ |
| 326 |
function column_cb( $item ) { |
| 327 |
return sprintf( |
| 328 |
'<input type="checkbox" name="bulk-delete[]" value="%s" />', $item['id'] |
| 329 |
); |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Method for name column |
| 334 |
* |
| 335 |
* @param array $item an array of DB data |
| 336 |
* |
| 337 |
* @return string |
| 338 |
*/ |
| 339 |
function column_title( $item ) { |
| 340 |
global $wpdb; |
| 341 |
|
| 342 |
$delete_nonce = wp_create_nonce( $this->plugin_name . '-delete-result' ); |
| 343 |
|
| 344 |
$survey_title = stripcslashes( $item['title'] ); |
| 345 |
|
| 346 |
$q = esc_attr( $survey_title ); |
| 347 |
|
| 348 |
$restitle = Survey_Maker_Admin::ays_restriction_string( "word", $survey_title, $this->title_length ); |
| 349 |
$restitle = esc_attr($restitle); |
| 350 |
$result = $this->unread_records_count( $item['id'] ); |
| 351 |
|
| 352 |
if( intval( $result ) > 0 ){ |
| 353 |
$title = sprintf( '<a href="?page=%s&survey=%d" title="%s">%s</a><input type="hidden" value="%d" class="ays_result_read">', $this->plugin_name."-each-submission", absint( $item['id'] ), $q, $restitle, 0 ); |
| 354 |
}else{ |
| 355 |
$title = sprintf( '<a href="?page=%s&survey=%d" title="%s">%s</a>', $this->plugin_name."-each-submission", absint( $item['id'] ), $q, $restitle ); |
| 356 |
} |
| 357 |
// $title = sprintf( '<a href="?page=%s&survey=%d">%s</a><input type="hidden" value="%d" class="ays_result_read">', $this->plugin_name."-each-submission", absint( $item['id'] ), $restitle, absint( $item['id'] )); |
| 358 |
|
| 359 |
$actions = array( |
| 360 |
'view_details' => sprintf( '<a href="?page=%s&survey=%d">%s</a>', $this->plugin_name."-each-submission", absint( $item['id'] ), __('View details', "survey-maker")), |
| 361 |
'delete' => sprintf( '<a class="ays_confirm_del" data-message="%s" href="?page=%s&action=%s&item=%s&_wpnonce=%s" title="%s">Delete</a>', $restitle."'s submissions", sanitize_text_field( $_REQUEST['page'] ), 'delete', absint( $item['id'] ), $delete_nonce, __( 'Delete all submissions of this survey', "survey-maker" ) ) |
| 362 |
); |
| 363 |
|
| 364 |
return $title . $this->row_actions( $actions ); |
| 365 |
} |
| 366 |
|
| 367 |
function column_quiz_rate( $item ) { |
| 368 |
global $wpdb; |
| 369 |
|
| 370 |
$sql = "SELECT AVG(`score`) AS avg_score FROM {$wpdb->prefix}aysquiz_rates WHERE quiz_id=". absint( $item['id'] ); |
| 371 |
$result = $wpdb->get_var($sql); |
| 372 |
|
| 373 |
if($result == null){ |
| 374 |
return; |
| 375 |
} |
| 376 |
|
| 377 |
$res = round($result,1); |
| 378 |
|
| 379 |
return $res; |
| 380 |
} |
| 381 |
|
| 382 |
function column_score($item){ |
| 383 |
global $wpdb; |
| 384 |
|
| 385 |
$sql = "SELECT AVG(`score`) AS avg_score FROM {$wpdb->prefix}aysquiz_reports WHERE quiz_id=". absint( $item['id'] ); |
| 386 |
$result = $wpdb->get_var($sql); |
| 387 |
|
| 388 |
if($result == null){ |
| 389 |
return; |
| 390 |
} |
| 391 |
$res = round($result,2)."%"; |
| 392 |
|
| 393 |
return $res; |
| 394 |
} |
| 395 |
|
| 396 |
function column_user_count($item) { |
| 397 |
global $wpdb; |
| 398 |
|
| 399 |
$sql = "SELECT COUNT(*) |
| 400 |
FROM ".$wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "submissions" . " |
| 401 |
WHERE survey_id=". absint( $item['id'] ); |
| 402 |
$result = $wpdb->get_var($sql); |
| 403 |
|
| 404 |
return $result; |
| 405 |
} |
| 406 |
|
| 407 |
function column_unreads($item) { |
| 408 |
global $wpdb; |
| 409 |
$sql = "SELECT COUNT(*) |
| 410 |
FROM ".$wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "submissions " . " |
| 411 |
WHERE ( `read` = 0 OR `read` = 2 ) AND survey_id=". absint( $item['id'] ); |
| 412 |
$q = intval( $wpdb->get_var( $sql ) ); |
| 413 |
if($q != 0){ |
| 414 |
$q = "<p style='font-size:16px;font-weight:900;color:blue'>$q</p> |
| 415 |
<input type='hidden' class='ays_quiz_results_unreads' value='0'>"; |
| 416 |
} |
| 417 |
return $q; |
| 418 |
} |
| 419 |
function column_last_sub_date($item) { |
| 420 |
global $wpdb; |
| 421 |
$survey_id = isset($item['id']) && $item['id'] != "" ? $item['id'] : false; |
| 422 |
$submissions_table = $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "submissions"; |
| 423 |
$res = ''; |
| 424 |
if($survey_id){ |
| 425 |
$sql = "SELECT submission_date |
| 426 |
FROM ".$submissions_table." |
| 427 |
WHERE survey_id=". absint( $survey_id ) ." ORDER BY id DESC LIMIT 1"; |
| 428 |
$res = $wpdb->get_var( $sql ); |
| 429 |
} |
| 430 |
return $res; |
| 431 |
} |
| 432 |
|
| 433 |
function column_category_ids( $item ) { |
| 434 |
global $wpdb; |
| 435 |
|
| 436 |
// Survey categories IDs |
| 437 |
$category_ids = isset( $item['category_ids'] ) && $item['category_ids'] != '' ? $item['category_ids'] : ''; |
| 438 |
|
| 439 |
if( ! empty( $category_ids ) ){ |
| 440 |
$sql = "SELECT id,title FROM " . $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "survey_categories WHERE id IN (" . esc_sql( $category_ids ) . ")"; |
| 441 |
|
| 442 |
$results = $wpdb->get_results($sql, 'ARRAY_A'); |
| 443 |
$category_location = array(); |
| 444 |
foreach ($results as $key => $value) { |
| 445 |
$category_location[] = sprintf( '<a href="?page=%s&action=%s&id=%d" target="_blank">%s</a>', 'survey-maker-survey-categories', 'edit', $value['id'], $value['title']); |
| 446 |
} |
| 447 |
|
| 448 |
$titles = implode( ', ', $category_location ); |
| 449 |
|
| 450 |
return $titles; |
| 451 |
} |
| 452 |
|
| 453 |
return '-'; |
| 454 |
|
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
/** |
| 459 |
* Associative array of columns |
| 460 |
* |
| 461 |
* @return array |
| 462 |
*/ |
| 463 |
function get_columns() { |
| 464 |
$columns = array( |
| 465 |
'cb' => '<input type="checkbox" />', |
| 466 |
'title' => __( 'Survey', "survey-maker" ), |
| 467 |
// 'quiz_rate' => __( 'Average Rate', "survey-maker" ), |
| 468 |
// 'score' => __( 'Average Score', "survey-maker" ), |
| 469 |
'user_count' => __( 'Passed Users Count', "survey-maker" ), |
| 470 |
'unreads' => __( 'Unread results', "survey-maker" ), |
| 471 |
'category_ids' => __( 'Categories', "survey-maker" ), |
| 472 |
'last_sub_date' => __( 'Last submission date', "survey-maker" ), |
| 473 |
'id' => __( 'ID', "survey-maker" ), |
| 474 |
); |
| 475 |
|
| 476 |
return $columns; |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
/** |
| 481 |
* Columns to make sortable. |
| 482 |
* |
| 483 |
* @return array |
| 484 |
*/ |
| 485 |
public function get_sortable_columns() { |
| 486 |
$sortable_columns = array( |
| 487 |
'quiz_title' => array( 'title', true ), |
| 488 |
'id' => array( 'id', true ), |
| 489 |
); |
| 490 |
|
| 491 |
return $sortable_columns; |
| 492 |
} |
| 493 |
|
| 494 |
/** |
| 495 |
* Returns an associative array containing the bulk action |
| 496 |
* |
| 497 |
* @return array |
| 498 |
*/ |
| 499 |
public function get_bulk_actions() { |
| 500 |
$actions = array( |
| 501 |
'mark-as-read' => __( 'Mark as read', "survey-maker"), |
| 502 |
'mark-as-unread' => __( 'Mark as unread', "survey-maker"), |
| 503 |
'bulk-delete' => __( 'Delete', "survey-maker"), |
| 504 |
); |
| 505 |
|
| 506 |
return $actions; |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
/** |
| 511 |
* Handles data query and filter, sorting, and pagination. |
| 512 |
*/ |
| 513 |
public function prepare_items() { |
| 514 |
$this->_column_headers = $this->get_column_info(); |
| 515 |
|
| 516 |
/** Process bulk action */ |
| 517 |
$this->process_bulk_action(); |
| 518 |
|
| 519 |
$per_page = $this->get_items_per_page('survey_submissions_results_per_page', 20); |
| 520 |
|
| 521 |
$current_page = $this->get_pagenum(); |
| 522 |
$total_items = self::record_count(); |
| 523 |
// if(! empty( $_REQUEST['orderby'] ) && $_REQUEST['orderby'] == 'quiz_complete' ){ |
| 524 |
// $total_items = self::record_complete_filter_count(); |
| 525 |
// } |
| 526 |
$this->set_pagination_args(array( |
| 527 |
'total_items' => $total_items, //WE have to calculate the total number of items |
| 528 |
'per_page' => $per_page //WE have to determine how many items to show on a page |
| 529 |
)); |
| 530 |
$this->items = self::get_items( $per_page, $current_page ); |
| 531 |
} |
| 532 |
|
| 533 |
public function process_bulk_action() { |
| 534 |
//Detect when a bulk action is being triggered... |
| 535 |
$message = 'deleted'; |
| 536 |
if ( 'delete' === $this->current_action() ) { |
| 537 |
|
| 538 |
// In our file that handles the request, verify the nonce. |
| 539 |
$nonce = sanitize_text_field( $_REQUEST['_wpnonce'] ); |
| 540 |
|
| 541 |
if ( ! wp_verify_nonce( $nonce, $this->plugin_name . '-delete-result' ) ) { |
| 542 |
die( 'Go get a life script kiddies' ); |
| 543 |
} else { |
| 544 |
self::delete_items( absint( sanitize_text_field($_GET['item']) ) ); |
| 545 |
|
| 546 |
// esc_url_raw() is used to prevent converting ampersand in url to "#038;" |
| 547 |
// add_query_arg() return the current url |
| 548 |
|
| 549 |
$url = esc_url_raw( remove_query_arg(array('action', 'survey', '_wpnonce') ) ) . '&status=' . $message; |
| 550 |
wp_redirect( $url ); |
| 551 |
} |
| 552 |
|
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
// If the delete bulk action is triggered |
| 557 |
if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'bulk-delete' ) |
| 558 |
|| ( isset( $_POST['action2'] ) && $_POST['action2'] == 'bulk-delete' ) |
| 559 |
) { |
| 560 |
|
| 561 |
$delete_ids = ( isset( $_POST['bulk-delete'] ) && ! empty( $_POST['bulk-delete'] ) ) ? esc_sql( $_POST['bulk-delete'] ) : array(); |
| 562 |
|
| 563 |
// loop over the array of record IDs and delete them |
| 564 |
foreach ( $delete_ids as $id ) { |
| 565 |
self::delete_items( $id ); |
| 566 |
} |
| 567 |
|
| 568 |
// esc_url_raw() is used to prevent converting ampersand in url to "#038;" |
| 569 |
// add_query_arg() return the current url |
| 570 |
|
| 571 |
$url = esc_url_raw( remove_query_arg(array('action', 'survey', '_wpnonce') ) ) . '&status=' . $message; |
| 572 |
wp_redirect( $url ); |
| 573 |
} |
| 574 |
|
| 575 |
// If the mark-as-read bulk action is triggered |
| 576 |
if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'mark-as-read' ) || ( isset( $_POST['action2'] ) && $_POST['action2'] == 'mark-as-read' ) ) { |
| 577 |
|
| 578 |
$delete_ids = ( isset( $_POST['bulk-delete'] ) && ! empty( $_POST['bulk-delete'] ) ) ? esc_sql( $_POST['bulk-delete'] ) : array(); |
| 579 |
// loop over the array of record IDs and delete them |
| 580 |
foreach ( $delete_ids as $id ) { |
| 581 |
self::mark_as_read_reports( $id ); |
| 582 |
} |
| 583 |
|
| 584 |
// esc_url_raw() is used to prevent converting ampersand in url to "#038;" |
| 585 |
// add_query_arg() return the current url |
| 586 |
|
| 587 |
$url = esc_url_raw( remove_query_arg(array('action', 'item', '_wpnonce') ) ); |
| 588 |
|
| 589 |
$message = 'marked-as-read'; |
| 590 |
$url = add_query_arg( array( |
| 591 |
'status' => $message, |
| 592 |
'ays_survey_tab' => 'poststuff', |
| 593 |
), $url ); |
| 594 |
wp_redirect( $url ); |
| 595 |
} |
| 596 |
|
| 597 |
// If the mark-as-unread bulk action is triggered |
| 598 |
if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'mark-as-unread' ) || ( isset( $_POST['action2'] ) && $_POST['action2'] == 'mark-as-unread' ) ) { |
| 599 |
|
| 600 |
$delete_ids = ( isset( $_POST['bulk-delete'] ) && ! empty( $_POST['bulk-delete'] ) ) ? esc_sql( $_POST['bulk-delete'] ) : array(); |
| 601 |
|
| 602 |
// loop over the array of record IDs and delete them |
| 603 |
foreach ( $delete_ids as $id ) { |
| 604 |
self::mark_as_unread_reports( $id ); |
| 605 |
} |
| 606 |
|
| 607 |
// esc_url_raw() is used to prevent converting ampersand in url to "#038;" |
| 608 |
// add_query_arg() return the current url |
| 609 |
|
| 610 |
$url = esc_url_raw( remove_query_arg(array('action', 'item', '_wpnonce') ) ); |
| 611 |
|
| 612 |
$message = 'marked-as-unread'; |
| 613 |
$url = add_query_arg( array( |
| 614 |
'status' => $message, |
| 615 |
'ays_survey_tab' => 'poststuff', |
| 616 |
), $url ); |
| 617 |
|
| 618 |
wp_redirect( $url ); |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
|
| 623 |
public function results_notices(){ |
| 624 |
$status = (isset($_REQUEST['status'])) ? sanitize_text_field( $_REQUEST['status'] ) : ''; |
| 625 |
|
| 626 |
if ( empty( $status ) ) |
| 627 |
return; |
| 628 |
|
| 629 |
if ( 'created' == $status ) |
| 630 |
$updated_message = __( 'Quiz created.', "survey-maker" ); |
| 631 |
elseif ( 'updated' == $status ) |
| 632 |
$updated_message = __( 'Quiz saved.', "survey-maker" ); |
| 633 |
elseif ( 'deleted' == $status ) |
| 634 |
$updated_message = __( 'Quiz deleted.', "survey-maker" ); |
| 635 |
|
| 636 |
if ( empty( $updated_message ) ) |
| 637 |
return; |
| 638 |
|
| 639 |
?> |
| 640 |
<div class="ays-survey-admin-notice notice notice-success is-dismissible"> |
| 641 |
<p> <?php echo esc_html($updated_message); ?> </p> |
| 642 |
</div> |
| 643 |
<?php |
| 644 |
} |
| 645 |
|
| 646 |
/** |
| 647 |
* Mark as read a customer record. |
| 648 |
* |
| 649 |
* @param int $id customer ID |
| 650 |
*/ |
| 651 |
public static function mark_as_read_reports( $id ) { |
| 652 |
global $wpdb; |
| 653 |
$x = $wpdb->update( |
| 654 |
$wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "submissions", |
| 655 |
array('read' => 1), |
| 656 |
array('survey_id' => intval($id)), |
| 657 |
array('%d'), |
| 658 |
array('%d') |
| 659 |
); |
| 660 |
|
| 661 |
} |
| 662 |
|
| 663 |
/** |
| 664 |
* Mark as unread a customer record. |
| 665 |
* |
| 666 |
* @param int $id customer ID |
| 667 |
*/ |
| 668 |
public static function mark_as_unread_reports( $id ) { |
| 669 |
global $wpdb; |
| 670 |
$wpdb->update( |
| 671 |
$wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "submissions", |
| 672 |
array('read' => 2), |
| 673 |
array('survey_id' => $id), |
| 674 |
array('%d'), |
| 675 |
array('%d') |
| 676 |
); |
| 677 |
|
| 678 |
} |
| 679 |
} |
| 680 |
|