From cb2bd663e195c80d9351635ab80c2f96aaa51199 Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Thu, 17 Sep 2015 17:06:26 +1000 Subject: [PATCH] Calculate subset results --- includes/class-sp-league-table.php | 8 +++++++- includes/class-sp-player-list.php | 22 ++++++++++++++++------ includes/class-sp-player.php | 20 +++++++++++++++++++- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/includes/class-sp-league-table.php b/includes/class-sp-league-table.php index 632543e0..4e4f3547 100644 --- a/includes/class-sp-league-table.php +++ b/includes/class-sp-league-table.php @@ -163,7 +163,7 @@ class SP_League_Table extends SP_Custom_Post{ 'numberposts' => -1, 'posts_per_page' => -1, 'orderby' => 'post_date', - 'order' => 'DESC', + 'order' => 'ASC', 'meta_query' => array( array( 'key' => 'sp_format', @@ -196,6 +196,8 @@ class SP_League_Table extends SP_Custom_Post{ $events = get_posts( $args ); + $e = 0; + // Event loop foreach ( $events as $event ): @@ -265,9 +267,11 @@ class SP_League_Table extends SP_Custom_Post{ else: if ( array_key_exists( $team_id, $totals ) && is_array( $totals[ $team_id ] ) && array_key_exists( $key . 'for', $totals[ $team_id ] ) ): $totals[ $team_id ][ $key . 'for' ] += $value; + $totals[ $team_id ][ $key . 'for' . ( $e + 1 ) ] = $value; foreach( $results as $other_team_id => $other_result ): if ( $other_team_id != $team_id && array_key_exists( $key . 'against', $totals[ $team_id ] ) ): $totals[ $team_id ][ $key . 'against' ] += sp_array_value( $other_result, $key, 0 ); + $totals[ $team_id ][ $key . 'against' . ( $e + 1 ) ] = sp_array_value( $other_result, $key, 0 ); endif; endforeach; endif; @@ -279,6 +283,8 @@ class SP_League_Table extends SP_Custom_Post{ endforeach; + $e++; + endforeach; foreach ( $streaks as $team_id => $streak ): diff --git a/includes/class-sp-player-list.php b/includes/class-sp-player-list.php index 1a19933a..c3bbf753 100644 --- a/includes/class-sp-player-list.php +++ b/includes/class-sp-player-list.php @@ -92,7 +92,7 @@ class SP_Player_List extends SP_Custom_Post { ); endif; - if ( $team ): + if ( $team && apply_filters( 'sportspress_has_teams', true ) ): $args['meta_query'] = array( array( 'key' => 'sp_team', @@ -158,8 +158,7 @@ class SP_Player_List extends SP_Custom_Post { endforeach; foreach ( $result_labels as $key => $value ): - $totals[ $player_id ][ $key . 'for' ] = 0; - $totals[ $player_id ][ $key . 'against' ] = 0; + $totals[ $player_id ][ $key . 'for' ] = $totals[ $player_id ][ $key . 'against' ] = 0; endforeach; // Get metrics @@ -221,7 +220,7 @@ class SP_Player_List extends SP_Custom_Post { $events = get_posts( $args ); // Event loop - foreach ( $events as $event ): + foreach ( $events as $i => $event ): $results = (array)get_post_meta( $event->ID, 'sp_results', true ); $team_performance = get_post_meta( $event->ID, 'sp_players', true ); $minutes = get_post_meta( $event->ID, 'sp_minutes', true ); @@ -338,9 +337,14 @@ class SP_Player_List extends SP_Custom_Post { endforeach; endif; else: + + // Add to total $value = sp_array_value( $totals[ $player_id ], $result_slug . 'for', 0 ); $value += $team_result; $totals[ $player_id ][ $result_slug . 'for' ] = $value; + + // Add subset + $totals[ $player_id ][ $result_slug . 'for' . ( $i + 1 ) ] = $team_result; endif; endforeach; @@ -349,15 +353,21 @@ class SP_Player_List extends SP_Custom_Post { foreach ( $results as $team_results ): unset( $team_results['outcome'] ); foreach ( $team_results as $result_slug => $team_result ): + + // Add to total $value = sp_array_value( $totals[ $player_id ], $result_slug . 'against', 0 ); $value += $team_result; $totals[ $player_id ][ $result_slug . 'against' ] = $value; + + // Add subset + $totals[ $player_id ][ $result_slug . 'against' . ( $i + 1 ) ] = $team_result; endforeach; endforeach; endif; endif; endforeach; endif; endforeach; endif; + $i++; endforeach; foreach ( $streaks as $player_id => $streak ): @@ -501,7 +511,7 @@ class SP_Player_List extends SP_Custom_Post { foreach( $this->columns as $key ): if ( $key == 'number' ): $labels[ $key ] = '#'; - elseif ( $key == 'team' ): + elseif ( $key == 'team' && apply_filters( 'sportspress_has_teams', true ) ): $labels[ $key ] = __( 'Team', 'sportspress' ); elseif ( array_key_exists( $key, $columns ) ): $labels[ $key ] = $columns[ $key ]; @@ -520,7 +530,7 @@ class SP_Player_List extends SP_Custom_Post { $labels = array(); if ( in_array( 'number', $this->columns ) ) $labels['number'] = '#'; $labels['name'] = __( 'Player', 'sportspress' ); - if ( in_array( 'team', $this->columns ) ) $labels['team'] = __( 'Team', 'sportspress' ); + if ( in_array( 'team', $this->columns ) && apply_filters( 'sportspress_has_teams', true ) ) $labels['team'] = __( 'Team', 'sportspress' ); $merged[0] = array_merge( $labels, $columns ); return $merged; diff --git a/includes/class-sp-player.php b/includes/class-sp-player.php index b2ff71eb..d2c764d4 100644 --- a/includes/class-sp-player.php +++ b/includes/class-sp-player.php @@ -93,6 +93,9 @@ class SP_Player extends SP_Custom_Post { // Get labels from outcome variables $outcome_labels = (array)sp_get_var_labels( 'sp_outcome' ); + // Get labels from result variables + $result_labels = (array)sp_get_var_labels( 'sp_result' ); + // Generate array of all season ids and season names $div_ids = array(); $season_names = array(); @@ -129,6 +132,10 @@ class SP_Player extends SP_Custom_Post { $totals[ $key ] = 0; endforeach; + foreach ( $result_labels as $key => $value ): + $totals[ $key . 'for' ] = $totals[ $key . 'against' ] = 0; + endforeach; + // Initialize streaks counter $streak = array( 'name' => '', 'count' => 0, 'fire' => 1 ); @@ -186,7 +193,7 @@ class SP_Player extends SP_Custom_Post { $events = get_posts( $args ); // Event loop - foreach( $events as $event ): + foreach( $events as $i => $event ): $results = (array)get_post_meta( $event->ID, 'sp_results', true ); $team_performance = (array)get_post_meta( $event->ID, 'sp_players', true ); $minutes = get_post_meta( $event->ID, 'sp_minutes', true ); @@ -300,9 +307,14 @@ class SP_Player extends SP_Custom_Post { endforeach; endif; else: + + // Add to total $value = sp_array_value( $totals, $result_slug . 'for', 0 ); $value += $team_result; $totals[ $result_slug . 'for' ] = $value; + + // Add subset + $totals[ $result_slug . 'for' . ( $i + 1 ) ] = $team_result; endif; endforeach; @@ -311,14 +323,20 @@ class SP_Player extends SP_Custom_Post { foreach ( $results as $team_results ): unset( $team_results['outcome'] ); foreach ( $team_results as $result_slug => $team_result ): + + // Add to total $value = sp_array_value( $totals, $result_slug . 'against', 0 ); $value += $team_result; $totals[ $result_slug . 'against' ] = $value; + + // Add subset + $totals[ $result_slug . 'against' . ( $i + 1 ) ] = $team_result; endforeach; endforeach; endif; endif; endforeach; + $i++; endforeach; // Compile streaks counter and add to totals