Include manually entered stats in player totals calculation

This commit is contained in:
Brian Miyaji
2017-03-23 10:16:22 +11:00
parent 6c5491a8cb
commit bd8352c812

View File

@@ -669,6 +669,21 @@ class SP_Player extends SP_Custom_Post {
endforeach; endforeach;
endif; endif;
// Calculate total statistics
$totals = array(
'name' => __( 'Total', 'sportspress' ),
'team' => 0,
);
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;
endforeach;
endforeach;
$merged[-1] = $totals;
if ( $admin ): if ( $admin ):
$labels = array(); $labels = array();
if ( is_array( $usecolumns ) ): foreach ( $usecolumns as $key ): if ( is_array( $usecolumns ) ): foreach ( $usecolumns as $key ):
@@ -678,6 +693,7 @@ class SP_Player extends SP_Custom_Post {
$labels[ $key ] = $columns[ $key ]; $labels[ $key ] = $columns[ $key ];
endif; endif;
endforeach; 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 );
else: else:
if ( is_array( $usecolumns ) ): if ( is_array( $usecolumns ) ):
@@ -696,35 +712,9 @@ class SP_Player extends SP_Custom_Post {
$labels['name'] = __( 'Season', 'sportspress' ); $labels['name'] = __( 'Season', 'sportspress' );
$labels['team'] = __( 'Team', 'sportspress' ); $labels['team'] = __( 'Team', 'sportspress' );
} }
if ( 'yes' === get_option( 'sportspress_player_show_total', 'no' ) ) { if ( 'no' === get_option( 'sportspress_player_show_total', 'no' ) ) {
// Get totals calculated from events unset( $merged[-1] );
$total_placeholders = sp_array_value( $placeholders, 0, array() );
// Get totals as entered directly and filter out the empty values
$total_data = sp_array_value( $data, 0, array() );
$total_data = array_filter( $total_data, 'sp_filter_non_empty' );
// Get totals of all seasons as entered manually
$totals = array();
foreach ( $merged as $season => $stats ) {
foreach ( $stats as $key => $value ) {
$value = floatval( $value );
$totals[ $key ] = sp_array_value( $totals, $key, 0 ) + $value;
}
}
// Merge with direct values
foreach ( $total_data as $key => $value ) {
if ( '' === $value ) {
$total_data[ $key ] = sp_array_value( $totals, $key, 0 );
}
}
// Then merge with placeholder values
$total = array_merge( $total_placeholders, $total_data );
$merged[-1] = $total;
$merged[-1]['name'] = __( 'Total', 'sportspress' );
} }
// Convert to time notation // Convert to time notation
@@ -752,7 +742,7 @@ class SP_Player extends SP_Custom_Post {
endif; endif;
$merged[0] = array_merge( $labels, $columns ); $merged[0] = array_merge( $labels, $columns );
return $merged; return $merged;
endif; endif;
} }