Add total type selector for average-based player statistics

This commit is contained in:
Brian Miyaji
2017-06-08 18:06:18 +10:00
parent 1144aa2094
commit cac416fdbc
4 changed files with 54 additions and 18 deletions

View File

@@ -613,6 +613,7 @@ class SP_Player extends SP_Custom_Post {
$columns = array_merge( $performance_labels, $stats );
$formats = array();
$total_types = array();
$args = array(
'post_type' => array( 'sp_performance', 'sp_statistic' ),
@@ -640,6 +641,12 @@ class SP_Player extends SP_Custom_Post {
$format = 'number';
}
$formats[ $post->post_name ] = $format;
$total_type = get_post_meta( $post->ID, 'sp_total_type', true );
if ( '' === $total_type ) {
$total_type = 'total';
}
$total_types[ $post->post_name ] = $total_type;
}
$columns = array_merge( $column_order, $columns );
$usecolumns = array_merge( $usecolumn_order, $usecolumns );
@@ -670,19 +677,35 @@ class SP_Player extends SP_Custom_Post {
endif;
// Calculate total statistics
$totals = array(
$career = array(
'name' => __( 'Total', 'sportspress' ),
'team' => '-',
);
// Add values from all seasons for total-based statistics
foreach ( $merged as $season => $stats ):
if ( ! is_array( $stats ) ) continue;
foreach ( $stats as $key => $value ):
if ( in_array( $key, array( 'name', 'team' ) ) ) continue;
$value = floatval( $value );
$totals[ $key ] = sp_array_value( $totals, $key, 0 ) + $value;
$career[ $key ] = sp_array_value( $career, $key, 0 ) + $value;
endforeach;
endforeach;
$merged[-1] = $totals;
// Calculate average-based statistics from performance
foreach ( $posts as $post ) {
$type = get_post_meta( $post->ID, 'sp_total_type', 'total' );
if ( 'average' !== $type ) continue;
$value = sp_array_value( $equations, $post->post_name, null );
if ( null === $value || ! isset( $value['equation'] ) ) continue;
$precision = sp_array_value( $value, 'precision', 0 );
$career[ $post->post_name ] = sp_solve( $value['equation'], $totals, $precision );
}
// Add career totals to merged array
$manual_career = sp_array_value( $data, 0, array() );
$manual_career = array_filter( $manual_career, 'sp_filter_non_empty' );
$merged[-1] = array_merge( $career, $manual_career );
if ( $admin ):
$labels = array();
@@ -694,7 +717,7 @@ class SP_Player extends SP_Custom_Post {
endif;
endforeach; endif;
$placeholders[0] = $merged[-1];
return array( $labels, $data, $placeholders, $merged, $leagues, $has_checkboxes, $formats );
return array( $labels, $data, $placeholders, $merged, $leagues, $has_checkboxes, $formats, $total_types );
else:
if ( is_array( $usecolumns ) ):
foreach ( $columns as $key => $label ):