Last active
November 30, 2015 09:57
-
-
Save flymke/88c0a4c49e53b9755574 to your computer and use it in GitHub Desktop.
Workaround to get array_column() working for older PHP Versions in Bolder Surveys Plugin (http://codecanyon.net/item/bolder-surveys-for-wordpress). The file you need to edit is located under bolder-surveys/includes/admin/class-statistics-survey.php.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; // Exit if accessed directly | |
} | |
class Bolder_Survey_Statistics_Single { | |
/* | |
* Survey ID | |
*/ | |
public $survey_id; | |
/* | |
* Pie Charts var | |
*/ | |
public $pie_charts; | |
/* | |
* Current Theme Colors array | |
*/ | |
public $theme_colors; | |
function __construct( $survey_id ){ | |
global $status, $page, $_wp_admin_css_colors; | |
$this->theme_colors = $_wp_admin_css_colors[ get_user_option( 'admin_color' ) ]->colors; | |
$this->survey_id = $survey_id; | |
$this->pie_charts = array(); | |
} | |
/** | |
* Display page for individual survey statistics | |
*/ | |
function display() { | |
$survey_id = $this->survey_id; | |
$participants_q = $this->get_participants( $this->survey_id ); | |
$participants = ( empty( $participants_q ) ) ? 0 : count( $participants_q ); | |
$date_start = get_post_meta( $survey_id, 'date_start', true ); | |
$date_end = get_post_meta( $survey_id, 'date_stop', true ); | |
?> | |
<div class="wrap"> | |
<h1><?php _e( 'Bolder Survey Statistics', 'bolder-surveys' ); ?>: <?php echo get_the_title( $survey_id ); ?> | |
<a href="<?php echo admin_url( 'edit.php?post_type=bolder-surveys&page=survey-statistics' ); ?>" class="page-title-action">« <?php _e( 'Back to Statistics Homepage', 'bolder-surveys' ); ?></a></h1> | |
<?php if( isset( $_GET['reset'] ) && sanitize_title( $_GET['reset'] ) == 'successful' ) : ?> | |
<div class="message updated"><p><?php _e( 'Statistics for this survey have been successfully reset.', 'bolder-surveys' ); ?></p></div> | |
<?php endif; ?> | |
<div id="poststuff"> | |
<div id="post-body" class="columns-2"> | |
<div id="post-body-content" style="position: relative;"> | |
<div class="bolder-survey-stats"> | |
<?php if( $reset ) : ?> | |
<div class="updated"> | |
<p><?php _e( 'The answers for this survey have been successfully deleted!', 'bolder-surveys' ); ?></p> | |
</div> | |
<?php endif; ?> | |
<div class="chart-wrapper"> | |
<?php | |
$n = 0; | |
$survey_questions = $this->get_questions( $survey_id ); | |
foreach ($survey_questions as $q) : | |
if( $q['type'] == 'headline' || $q['type'] == 'break' ) continue; | |
$type = ( $q['type'] == 'grid' ) ? 'table' : 'chart'; | |
?> | |
<div class="<?php echo $type; ?> survey-element element-<?php echo $q['type']; ?> stat-question-<?php echo $q['id']; if( $n % 2 ) echo " last"; ?>"> | |
<?php $this->display_single_question( $q ); ?> | |
<br class="clear"> | |
</div> | |
<?php | |
$n++; | |
endforeach; | |
?> | |
</div> | |
</div> | |
</div> | |
<div id="postbox-container-1" class="postbox-container"> | |
<div class="survey-info-box"> | |
<h2 class="element-headline"><?php _e( 'Survey Information', 'bolder-surveys' ); ?></h2> | |
<div class="inner"> | |
<p class="survey-status"><?php $s = $this->get_survey_status( $survey_id ); echo '<span class="' . $s[1] . '">' . $s[0] . '</span>'; ?></p> | |
<p>Date Started:<span><?php echo date( 'M j, Y', strtotime( $date_start ) ); ?></span></p> | |
<p>Date Ended:<span><?php echo date( 'M j, Y', strtotime( $date_end ) ); ?></span></p> | |
<p>Participants:<span><?php echo $participants; ?></span></p> | |
</div> | |
<div id="major-publishing-actions" class="submitbox"> | |
<p style="margin: 0;"><a class="submitdelete deletion" href="<?php echo admin_url( 'edit.php?post_type=bolder-surveys&page=survey-statistics&survey_id=' . $survey_id . '&reset=true' ); ?>">Reset Survey Statistics</a></p> | |
</div> | |
</div> | |
</div> | |
<br class="clear"> | |
</div> | |
</div> | |
</div> | |
<script type="text/javascript"> | |
<?php $this->generate_chart_js(); ?> | |
</script> | |
<?php | |
} | |
/** | |
* Setup display output for single elements | |
*/ | |
function display_single_element( $survey_id, $element_id ) { | |
?> | |
<div class="wrap"> | |
<h1><?php _e( 'Bolder Survey Statistics', 'bolder-surveys' ); ?>: <?php echo get_the_title( $survey_id ); ?> | |
<a href="<?php echo admin_url( 'edit.php?post_type=bolder-surveys&page=survey-statistics&survey_id=' . absint( $survey_id ) ); ?>" class="page-title-action">« <?php _e( 'Back to Full Survey Statistics', 'bolder-surveys' ); ?></a></h1> | |
<div id="poststuff"> | |
<div id="post-body" class="columns-2"> | |
<div id="post-body-content" style="position: relative;"> | |
<div class="bolder-survey-stats"> | |
<?php | |
// retrieve requested element | |
$question = $this->get_question( (int) $survey_id, (int) $element_id ); | |
if( $question ) : | |
if( $question['type'] == 'headline' || $question['type'] == 'break' ) continue; | |
$type = ( $question['type'] == 'grid' ) ? 'table' : 'chart'; | |
?> | |
<div class="<?php echo $type; ?> survey-element element-<?php echo $question['type']; ?> stat-question-<?php echo $question['id']; if( $n % 2 ) echo " last"; ?>"> | |
<?php $this->display_single_question( $question, true ); ?> | |
<br class="clear"> | |
</div> | |
<?php | |
else : | |
endif; | |
?> | |
</div> | |
</div> | |
<script type="text/javascript"> | |
<?php $this->generate_chart_js(); ?> | |
</script> | |
<?php | |
} | |
/** | |
* Display page for individual survey statistics | |
*/ | |
function display_single_question( $question, $single = false ) { | |
// exit if question is not found | |
if( ! $question || ! is_array( $question ) ) | |
return; | |
$type = ( isset( $question['type'] ) ) ? $question['type'] : 'break'; | |
$args = unserialize( $question['args'] ); | |
$question += $args; | |
unset( $question['args'] ); | |
if( $type == 'headline' || $type == 'break' ) | |
return; | |
elseif( $type == 'grid' ) | |
$this->generate_grid_chart( $question, $single ); | |
elseif( $type =='question' && ( in_array( $args['question_type'], array( 'text', 'textarea' ) ) ) ) | |
$this->generate_text_results( $question, $single ); | |
else | |
$this->generate_pie_chart( $question, $single ); | |
} | |
/** | |
* Get questions for this survey | |
*/ | |
function get_questions( $survey_id ) { | |
global $wpdb; | |
return $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix . "beSurveys_elements WHERE sid='" . $survey_id . "' ORDER BY order_id ASC", ARRAY_A ); | |
} | |
/** | |
* Get question by element id | |
*/ | |
function get_question( $survey_id, $element_id ) { | |
global $wpdb; | |
return $wpdb->get_row( "SELECT * FROM " . $wpdb->prefix . "beSurveys_elements WHERE sid='" . $survey_id . "' AND id='" . $element_id . "' ORDER BY order_id ASC", ARRAY_A ); | |
} | |
/** | |
* Retrieve list of all surveys | |
*/ | |
function get_survey_list() { | |
global $wpdb; | |
$args = array( | |
'posts_per_page' => -1, | |
'post_type' => 'bolder-surveys', | |
); | |
$sel = get_posts( $args ); | |
if( count( $sel ) ) return $sel; | |
return false; | |
} | |
/** | |
* Retrieve all participants for a given survey | |
*/ | |
function get_participants( $survey_id ) { | |
global $wpdb; | |
$survey_id = absint( $survey_id ); | |
$sel = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix . "beSurveys_users WHERE sid='" . $survey_id . "' ORDER BY date DESC", ARRAY_A ); | |
if( count( $sel ) ) return $sel; | |
return false; | |
} | |
/** | |
* Retrieve status for a given survey | |
*/ | |
function get_survey_status( $survey_id ) { | |
$survey_id = absint( $survey_id ); | |
$date_start = get_post_meta( $survey_id, 'date_start', true ); | |
$date_end = get_post_meta( $survey_id, 'date_stop', true ); | |
$published = get_post_status( $survey_id ); | |
if( !isset( $date_start ) || !isset( $date_end ) ) return array( 'INVALID', 'error' ); | |
if( $published != 'publish' ) | |
return array( 'PENDING', 'notice' ); | |
$current_date = strtotime('now'); | |
if( strtotime($date_start) < $current_date && strtotime($date_end) > $current_date ) | |
return array( 'ACTIVE', 'success' ); | |
else | |
return array( 'INACTIVE', 'notice' ); | |
return array( 'INVALID', 'error' ); | |
} | |
/** | |
* Retrieve all answers for a given element (question) | |
*/ | |
function get_element_answers( $element_id ) { | |
global $wpdb; | |
$element_id = absint( $element_id ); | |
$sel = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix . "beSurveys_answers WHERE eid='" . $element_id . "'", ARRAY_A ); | |
if( count( $sel ) ) return $sel; | |
return false; | |
} | |
/** | |
* Display Pie Chart for Individual Question | |
*/ | |
function generate_pie_chart( $args, $single = false ) { | |
global $Bolder_Survey_Chart_Colors; | |
$pieces = array(); | |
$n = 0; | |
?> | |
<h4><?php echo stripslashes( $args['headline'] ); ?></h4> | |
<canvas id="pie-chart-<?php echo $args['id']; ?>" class="pie-chart-canvas" height="250" width="250"></canvas> | |
<?php if( $args['type'] == 'rating' ) { ?> | |
<div class="pie-chart-key"> | |
<h5>KEY</h5> | |
<ul> | |
<?php | |
$results = $this->process_rating_results( $args ); | |
if( $results ) : | |
$total = array_sum( $results ); | |
foreach ( $results as $value => $count ) { | |
$percent = number_format( ( $count * 100 ) / $total, 2 ); | |
if( $n == 5 ) echo "</ul><ul style=\"float:left;\">"; | |
echo "\t\t\t<li><span style=\"background:" . $Bolder_Survey_Chart_Colors[ $n ] . "\"></span>" . stripslashes( $value ) . " - " . $percent . "% (" . $count . ")</li>\n"; | |
$percent = ( $count * 100 ) / $total; | |
$pieces[] = array( 'value' => $percent, 'color' => $Bolder_Survey_Chart_Colors[ $n ] ); | |
$n++; | |
if( $n >= 10 ) $n = 0; | |
} | |
else : | |
_e( 'No results found.', 'bolder-surveys' ); | |
endif; | |
?> | |
</ul> | |
</div> | |
<?php | |
} elseif ( $args['type'] == 'question' ) { | |
?> | |
<div class="pie-chart-key"> | |
<h5>KEY</h5> | |
<ul> | |
<?php | |
$results = $this->process_question_results( $args ); | |
if( $results ) : | |
$total = 0; | |
foreach ( $results as $r ) | |
$total += $r['count']; | |
foreach ( $results as $value => $count ) { | |
$percent = number_format( ( $count['count'] * 100 ) / $total, 2 ); | |
$pieces[] = array( 'value' => $percent, 'color' => $Bolder_Survey_Chart_Colors[ $n ] ); | |
echo "\t\t\t<li><span style=\"background:" . $Bolder_Survey_Chart_Colors[ $n ] . "\"></span>" . stripslashes( stripslashes( $count['title'] ) ) . " - " . $percent . "% (" . $count['count'] . ")</li>\n"; | |
$n++; | |
if( $n >= 10 ) $n = 0; | |
} | |
else : | |
_e( 'No results found.', 'bolder-surveys' ); | |
endif; | |
?> | |
</ul> | |
</div> | |
<?php | |
} | |
echo '<div class="clear"></div>'; | |
if( ! $single ) echo '<p class="view-details"><a href="' . admin_url( 'edit.php?post_type=bolder-surveys&page=survey-statistics&survey_id=' . $args['sid'] . '&element=' . $args['id'] ) . '">' . __( 'View Details', 'bolder-surveys' ) . '</a></p>'; | |
$this->pie_charts[ $args['id'] ] = $pieces; | |
} | |
/** | |
* Display Bar Graph for Individual Question | |
*/ | |
function generate_grid_chart( $args, $single = false ) { | |
global $wpdb; | |
$columns = explode( '|', stripslashes( $args['options-col'] ) ); | |
$rows = explode( '|', stripslashes( $args['options-row'] ) ); | |
?> | |
<div class="grid chart-question-<?php echo $args['id']; ?>"> | |
<h4><?php echo stripslashes( $args['headline'] ); ?></h4> | |
<table id="survey-answers-grid-<?php echo $args['id']; ?>" class="bolder-surveys-admin-table"> | |
<thead> | |
<tr> | |
<th> </th> | |
<?php foreach( $columns as $val_c ) : ?> | |
<th scope="col"><?php echo $val_c; ?></th> | |
<?php | |
endforeach; | |
$items = $this->get_element_answers( $args['id'] ); | |
if( $items ) { | |
$results = $this->process_grid_results( $items ); | |
$for_chart = array(); | |
$data = ""; | |
} | |
?> | |
</tr> | |
</thead> | |
<tbody> | |
<?php foreach( $rows as $val_r ) : ?> | |
<tr> | |
<th scope="row"><?php echo $val_r; ?></th> | |
<?php foreach( $columns as $val_c ) : $max = max( $results[ sanitize_title( $val_r ) ] ); ?> | |
<td> | |
<?php | |
$r = $results[ sanitize_title( $val_r ) ][ sanitize_title( $val_c ) ]; | |
if( isset( $r ) && !is_null( $r ) ) { | |
if( $max == $r ) echo "<span>" . $r . "</span>"; | |
else echo $r; | |
} else echo 0; | |
?> | |
</td> | |
<?php endforeach; ?> | |
</tr> | |
<?php endforeach; ?> | |
</tbody> | |
</table> | |
</div> | |
<?php if( ! $single ) : ?><p class="view-details"><a href="<?php echo admin_url( 'edit.php?post_type=bolder-surveys&page=survey-statistics&survey_id=' . $args['sid'] . '&element=' . $args['id'] ); ?>"><?php _e( 'View Details', 'bolder-surveys' ); ?></a></p><?php endif; ?> | |
<?php | |
} | |
/** | |
* Unserialize text question answers and form into single array | |
*/ | |
function generate_text_results( $args, $single = false ) { | |
?> | |
<h4><?php echo stripslashes( $args['headline'] ); ?></h4> | |
<?php | |
$n = 0; | |
$results = $this->process_text_question_results( $args ); | |
if( $results ) { | |
if( ! $single ) $results = array_splice( $results, 0, 4 ); | |
$counts = $this->array_column( $results, 'count' ); | |
$total_counts = array_sum( $counts ); | |
foreach( $results as $value ) { | |
$width = ( $value['count'] / $total_counts ) * 100; | |
echo '<p><span><span style="background: ' . $this->theme_colors[2] . '; width: ' . $width . '%;"></span></span>' . $value['answer'] . ' (' . $value['count'] . ')</p>'; | |
$n++; | |
} | |
} | |
if( ! $single ) : | |
?> | |
<p class="view-details"><a href="<?php echo admin_url( 'edit.php?post_type=bolder-surveys&page=survey-statistics&survey_id=' . $args['sid'] . '&element=' . $args['id'] ); ?>"><?php _e( 'View All Answers', 'bolder-surveys' ); ?></a></p> | |
<?php | |
endif; | |
} | |
/** | |
* Create JS code to generate chart elements | |
*/ | |
function generate_chart_js() { | |
if( count( $this->pie_charts ) ) { | |
foreach ( $this->pie_charts as $id => $args ) { | |
echo "\n\tvar Data" . $id . " = ["; | |
foreach ( $args as $val ) | |
echo "{value: " . $val['value'] . ",color:'" . $val['color'] . "'},"; | |
echo "];\n\tvar myDoughnut = new Chart(document.getElementById('pie-chart-" . $id . "').getContext('2d')).Doughnut(Data" . $id . ")\n"; | |
} | |
} | |
} | |
/** | |
* Unserialize rating answers and form into single array | |
*/ | |
function process_rating_results( $rating ) { | |
$results = $temp = array(); | |
$answers = $this->get_element_answers( $rating['id'] ); | |
foreach ($answers as $value) { | |
if( isset( $temp[ $value['answer'] ] ) ) | |
$temp[ $value['answer'] ]++; | |
else $temp[ $value['answer'] ] = 1; | |
} | |
$max = ( $rating['rating_type'] == 'ten' ) ? 10 : 5; | |
for( $n = 1; $n <= $max; $n++ ) { | |
$results[ $n ] = ( isset( $temp[ $n ] ) ) ? $temp[ $n ] : 0; | |
} | |
return $results; | |
} | |
/** | |
* Unserialize selective question answers and form into single array | |
*/ | |
function process_question_results( $question ) { | |
$results = $temp = array(); | |
$answers = $this->get_element_answers( $question['id'] ); | |
if( $answers ) : | |
foreach( $answers as $value ) { | |
$key = sanitize_title( $value['answer'] ); | |
if( !isset( $results[ $key ] ) ) $results[ $key ] = array( 'title' => $value['answer'], 'count' => 0 ); | |
$results[ $key ]['count']++; | |
} | |
endif; | |
return $results; | |
} | |
/** | |
* Unserialize text question answers and form into single array | |
*/ | |
function process_text_question_results( $question ) { | |
$results = array(); | |
$answers = $this->get_element_answers( $question['id'] ); | |
if( $answers ) { | |
foreach( $answers as $value ) { | |
$data = unserialize( $value['answer'] ); | |
if( is_array( $data ) ) { | |
foreach ( $data as $val ) { | |
$key = sanitize_title( $val ); | |
if( !isset( $results[ $key ] ) ) $results[ $key ] = array( 'answer' => $val, 'count' => 0 ); | |
$results[ $key ]['count']++; | |
} | |
} elseif( !isset( $results[ sanitize_title( $data ) ] ) ) { | |
$results[ sanitize_title( $data ) ] = array( 'answer' => $value['answer'], 'count' => 0 ); | |
$results[ sanitize_title( $data ) ]['count']++; | |
} | |
} | |
// Obtain a list of columns | |
foreach ($results as $key => $row) { | |
$title[ $key ] = $row['answer']; | |
$count[ $key ] = $row['count']; | |
} | |
// Sort the data with volume descending, edition ascending | |
// Add $data as the last parameter, to sort by the common key | |
array_multisort($count, SORT_DESC, $title, SORT_ASC, $results); | |
} | |
return $results; | |
} | |
/** | |
* Unserialize grid answers and form into single array | |
*/ | |
function process_grid_results( $grid ) { | |
$results = array(); | |
foreach ($grid as $value) { | |
$ans = unserialize($value['answer']); | |
// Sort unserialized answers | |
foreach ( $ans as $key => $value ) { | |
if( !isset( $results[ $key ] ) ) $results[ $key ] = array(); | |
// If more than one col is selected for row | |
if( is_array( $value ) ) { | |
foreach ( $value as $value2 ) { | |
$value2 = sanitize_title( $value2 ); | |
if( !isset( $results[ $key ][ $value2 ] ) ) $results[ $key ][ $value2 ] = 0; | |
$results[ $key ][ sanitize_title( $value2 ) ]++; | |
} | |
} else { | |
if( !isset( $results[ $key ][ sanitize_title( $value ) ] ) ) $results[ $key ][ sanitize_title( $value ) ] = 0; | |
$results[ $key ][ sanitize_title( $value ) ]++; | |
} | |
} | |
} | |
return $results; | |
} | |
/** | |
* Retrieve statistics for recently taken surveys: number of participants per survey | |
*/ | |
function get_takers_by_survey() { | |
global $wpdb; | |
$sel = $wpdb->get_results( "SELECT sid, COUNT(sid) as total_takers FROM " . $wpdb->prefix . "beSurveys_users GROUP BY sid ORDER BY date", ARRAY_A ); | |
$surveys = array(); | |
foreach ($sel as $value) | |
$surveys[ $value['sid'] ] = $value['total_takers']; | |
if( count( $surveys ) ) return $surveys; | |
return false; | |
} | |
function array_column($input = null, $columnKey = null, $indexKey = null) | |
{ | |
// Using func_get_args() in order to check for proper number of | |
// parameters and trigger errors exactly as the built-in array_column() | |
// does in PHP 5.5. | |
$argc = func_num_args(); | |
$params = func_get_args(); | |
if ($argc < 2) { | |
trigger_error("array_column() expects at least 2 parameters, {$argc} given", E_USER_WARNING); | |
return null; | |
} | |
if (!is_array($params[0])) { | |
trigger_error( | |
'array_column() expects parameter 1 to be array, ' . gettype($params[0]) . ' given', | |
E_USER_WARNING | |
); | |
return null; | |
} | |
if (!is_int($params[1]) | |
&& !is_float($params[1]) | |
&& !is_string($params[1]) | |
&& $params[1] !== null | |
&& !(is_object($params[1]) && method_exists($params[1], '__toString')) | |
) { | |
trigger_error('array_column(): The column key should be either a string or an integer', E_USER_WARNING); | |
return false; | |
} | |
if (isset($params[2]) | |
&& !is_int($params[2]) | |
&& !is_float($params[2]) | |
&& !is_string($params[2]) | |
&& !(is_object($params[2]) && method_exists($params[2], '__toString')) | |
) { | |
trigger_error('array_column(): The index key should be either a string or an integer', E_USER_WARNING); | |
return false; | |
} | |
$paramsInput = $params[0]; | |
$paramsColumnKey = ($params[1] !== null) ? (string) $params[1] : null; | |
$paramsIndexKey = null; | |
if (isset($params[2])) { | |
if (is_float($params[2]) || is_int($params[2])) { | |
$paramsIndexKey = (int) $params[2]; | |
} else { | |
$paramsIndexKey = (string) $params[2]; | |
} | |
} | |
$resultArray = array(); | |
foreach ($paramsInput as $row) { | |
$key = $value = null; | |
$keySet = $valueSet = false; | |
if ($paramsIndexKey !== null && array_key_exists($paramsIndexKey, $row)) { | |
$keySet = true; | |
$key = (string) $row[$paramsIndexKey]; | |
} | |
if ($paramsColumnKey === null) { | |
$valueSet = true; | |
$value = $row; | |
} elseif (is_array($row) && array_key_exists($paramsColumnKey, $row)) { | |
$valueSet = true; | |
$value = $row[$paramsColumnKey]; | |
} | |
if ($valueSet) { | |
if ($keySet) { | |
$resultArray[$key] = $value; | |
} else { | |
$resultArray[] = $value; | |
} | |
} | |
} | |
return $resultArray; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment