From cac416fdbc6da324cb0c7b03cbc8744465072fbb Mon Sep 17 00:00:00 2001
From: Brian Miyaji
Date: Thu, 8 Jun 2017 18:06:18 +1000
Subject: [PATCH] Add total type selector for average-based player statistics
---
assets/js/admin/sportspress-admin.js | 8 ++---
.../class-sp-meta-box-player-statistics.php | 20 ++++++------
.../class-sp-meta-box-statistic-details.php | 13 ++++++++
includes/class-sp-player.php | 31 ++++++++++++++++---
4 files changed, 54 insertions(+), 18 deletions(-)
diff --git a/assets/js/admin/sportspress-admin.js b/assets/js/admin/sportspress-admin.js
index 9f188e5a..55d8bb25 100644
--- a/assets/js/admin/sportspress-admin.js
+++ b/assets/js/admin/sportspress-admin.js
@@ -239,7 +239,7 @@ jQuery(document).ready(function($){
});
// Total stats calculator
- $(".sp-data-table .sp-total input[data-sp-format=number]").on("updateTotal", function() {
+ $(".sp-data-table .sp-total input[data-sp-format=number][data-sp-total-type=total]").on("updateTotal", function() {
index = $(this).parent().index();
var sum = 0;
$(this).closest(".sp-data-table").find(".sp-post").each(function() {
@@ -248,7 +248,7 @@ jQuery(document).ready(function($){
val = $(this).find("td").eq(index).find("input").attr("placeholder");
}
if($.isNumeric(val)) {
- sum += parseInt(val, 10);
+ sum += parseFloat(val, 10);
}
});
$(this).attr("placeholder", sum);
@@ -257,12 +257,12 @@ jQuery(document).ready(function($){
// Activate total stats calculator
if($(".sp-data-table .sp-total").size()) {
$(".sp-data-table .sp-post td input").on("keyup", function() {
- $(this).closest(".sp-data-table").find(".sp-total td").eq($(this).parent().index()).find("input").trigger("updateTotal");
+ $(this).closest(".sp-data-table").find(".sp-total td").eq($(this).parent().index()).find("input[data-sp-format=number][data-sp-total-type=total]").trigger("updateTotal");
});
}
// Trigger total stats calculator
- $(".sp-data-table .sp-total input").trigger("updateTotal");
+ $(".sp-data-table .sp-total input[data-sp-format=number][data-sp-total-type=total]").trigger("updateTotal");
// Sync inputs
$(".sp-sync-input").on("keyup", function() {
diff --git a/includes/admin/post-types/meta-boxes/class-sp-meta-box-player-statistics.php b/includes/admin/post-types/meta-boxes/class-sp-meta-box-player-statistics.php
index 620f03a0..04069632 100644
--- a/includes/admin/post-types/meta-boxes/class-sp-meta-box-player-statistics.php
+++ b/includes/admin/post-types/meta-boxes/class-sp-meta-box-player-statistics.php
@@ -33,16 +33,16 @@ class SP_Meta_Box_Player_Statistics {
?>
name; ?>
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 ) {
?>
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 {
?>
name; ?> —
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 ) {
?>
—
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 '';
+ echo '';
?>
diff --git a/includes/admin/post-types/meta-boxes/class-sp-meta-box-statistic-details.php b/includes/admin/post-types/meta-boxes/class-sp-meta-box-statistic-details.php
index fdd4edd6..d87199c3 100644
--- a/includes/admin/post-types/meta-boxes/class-sp-meta-box-statistic-details.php
+++ b/includes/admin/post-types/meta-boxes/class-sp-meta-box-statistic-details.php
@@ -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 {
?>
+
+
+
+
@@ -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 ) );
diff --git a/includes/class-sp-player.php b/includes/class-sp-player.php
index a56ce3d7..9577f7bf 100644
--- a/includes/class-sp-player.php
+++ b/includes/class-sp-player.php
@@ -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 ):