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

@@ -33,16 +33,16 @@ class SP_Meta_Box_Player_Statistics {
?>
<p><strong><?php echo $league->name; ?></strong></p>
<?php
list( $columns, $data, $placeholders, $merged, $seasons_teams, $has_checkboxes, $formats ) = $player->data( $league->term_id, true );
self::table( $post->ID, $league->term_id, $columns, $data, $placeholders, $merged, $seasons_teams, $has_checkboxes && $i == 0, true, $formats );
list( $columns, $data, $placeholders, $merged, $seasons_teams, $has_checkboxes, $formats, $total_types ) = $player->data( $league->term_id, true );
self::table( $post->ID, $league->term_id, $columns, $data, $placeholders, $merged, $seasons_teams, $has_checkboxes && $i == 0, true, $formats, $total_types );
$i ++;
endforeach;
if ( $show_career_totals ) {
?>
<p><strong><?php _e( 'Career Total', 'sportspress' ); ?></strong></p>
<?php
list( $columns, $data, $placeholders, $merged, $seasons_teams ) = $player->data( 0, true );
self::table( $post->ID, 0, $columns, $data, $placeholders, $merged, $seasons_teams );
list( $columns, $data, $placeholders, $merged, $seasons_teams, $has_checkboxes, $formats, $total_types ) = $player->data( 0, true );
self::table( $post->ID, 0, $columns, $data, $placeholders, $merged, $seasons_teams, false, false, $formats, $total_types );
}
} else {
// Determine order of sections
@@ -60,16 +60,16 @@ class SP_Meta_Box_Player_Statistics {
?>
<p><strong><?php echo $league->name; ?> &mdash; <?php echo $section_label; ?></strong></p>
<?php
list( $columns, $data, $placeholders, $merged, $seasons_teams, $has_checkboxes ) = $player->data( $league->term_id, true, $section_id );
self::table( $post->ID, $league->term_id, $columns, $data, $placeholders, $merged, $seasons_teams, $has_checkboxes && $i == 0 && $s == 0, $s == 0 );
list( $columns, $data, $placeholders, $merged, $seasons_teams, $has_checkboxes, $formats, $total_types ) = $player->data( $league->term_id, true, $section_id );
self::table( $post->ID, $league->term_id, $columns, $data, $placeholders, $merged, $seasons_teams, $has_checkboxes && $i == 0 && $s == 0, $s == 0, $formats, $total_types );
$i ++;
endforeach;
if ( $show_career_totals ) {
?>
<p><strong><?php _e( 'Career Total', 'sportspress' ); ?> &mdash; <?php echo $section_label; ?></strong></p>
<?php
list( $columns, $data, $placeholders, $merged, $seasons_teams ) = $player->data( 0, true, $section_id );
self::table( $post->ID, 0, $columns, $data, $placeholders, $merged, $seasons_teams, $has_checkboxes && $i == 0 && $s == 0, $s == 0 );
list( $columns, $data, $placeholders, $merged, $seasons_teams, $has_checkboxes, $formats, $total_types ) = $player->data( 0, true, $section_id );
self::table( $post->ID, 0, $columns, $data, $placeholders, $merged, $seasons_teams, $has_checkboxes && $i == 0 && $s == 0, $s == 0, $formats, $total_types );
}
$s ++;
}
@@ -88,7 +88,7 @@ class SP_Meta_Box_Player_Statistics {
/**
* Admin edit table
*/
public static function table( $id = null, $league_id, $columns = array(), $data = array(), $placeholders = array(), $merged = array(), $leagues = array(), $has_checkboxes = false, $team_select = false, $formats = array() ) {
public static function table( $id = null, $league_id, $columns = array(), $data = array(), $placeholders = array(), $merged = array(), $leagues = array(), $has_checkboxes = false, $team_select = false, $formats = array(), $total_types = array() ) {
$readonly = false;
$teams = array_filter( get_post_meta( $id, 'sp_team', false ) );
?>
@@ -124,7 +124,7 @@ class SP_Meta_Box_Player_Statistics {
if ( $readonly )
echo $value ? $value : $placeholder;
else
echo '<input type="text" name="sp_statistics[' . $league_id . '][0][' . $column . ']" value="' . esc_attr( $value ) . '" placeholder="' . esc_attr( $placeholder ) . '"' . ( $readonly ? ' disabled="disabled"' : '' ) . ' data-sp-format="number" />';
echo '<input type="text" name="sp_statistics[' . $league_id . '][0][' . $column . ']" value="' . esc_attr( $value ) . '" placeholder="' . esc_attr( $placeholder ) . '"' . ( $readonly ? ' disabled="disabled"' : '' ) . ' data-sp-format="number" data-sp-total-type="' . sp_array_value( $total_types, $column, 'total' ) . '" />';
?></td>
<?php endforeach; ?>
</tr>

View File

@@ -26,6 +26,7 @@ class SP_Meta_Box_Statistic_Details extends SP_Meta_Box_Config {
$precision = get_post_meta( $post->ID, 'sp_precision', true );
$section = get_post_meta( $post->ID, 'sp_section', true );
$format = get_post_meta( $post->ID, 'sp_format', true );
$total = get_post_meta( $post->ID, 'sp_total_type', true );
$visible = get_post_meta( $post->ID, 'sp_visible', true );
// Defaults
@@ -65,6 +66,17 @@ class SP_Meta_Box_Statistic_Details extends SP_Meta_Box_Config {
?>
</select>
</p>
<p><strong><?php _e( 'Career Total', 'sportspress' ); ?></strong></p>
<p>
<select name="sp_total_type">
<?php
$options = apply_filters( 'sportspress_statistic_totals', array( 'total' => __( 'Total', 'sportspress' ), 'average' => __( 'Average', 'sportspress' ) ) );
foreach ( $options as $key => $value ):
printf( '<option value="%s" %s>%s</option>', $key, selected( $key == $total, true, false ), $value );
endforeach;
?>
</select>
</p>
<p>
<strong><?php _e( 'Visible', 'sportspress' ); ?></strong>
<i class="dashicons dashicons-editor-help sp-desc-tip" title="<?php _e( 'Display in player profile?', 'sportspress' ); ?>"></i>
@@ -92,6 +104,7 @@ class SP_Meta_Box_Statistic_Details extends SP_Meta_Box_Config {
public static function save( $post_id, $post ) {
self::delete_duplicate( $_POST );
update_post_meta( $post_id, 'sp_section', (int) sp_array_value( $_POST, 'sp_section', -1 ) );
update_post_meta( $post_id, 'sp_total_type', sp_array_value( $_POST, 'sp_total_type', 'total' ) );
update_post_meta( $post_id, 'sp_format', sp_array_value( $_POST, 'sp_format', 'number' ) );
update_post_meta( $post_id, 'sp_precision', (int) sp_array_value( $_POST, 'sp_precision', 1 ) );
update_post_meta( $post_id, 'sp_visible', sp_array_value( $_POST, 'sp_visible', 1 ) );

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 ):