diff --git a/admin/post-types/event.php b/admin/post-types/event.php index eb0a036f..b7aef404 100644 --- a/admin/post-types/event.php +++ b/admin/post-types/event.php @@ -75,45 +75,67 @@ function sp_event_team_meta( $post ) { function sp_event_players_meta( $post ) { $limit = get_option( 'sp_event_team_count' ); $teams = array_pad( array_slice( (array)get_post_meta( $post->ID, 'sp_team', false ), 0, $limit ), $limit, 0 ); - $stats = (array)get_post_meta( $post->ID, 'sp_players', true ); - - // Get columns from result variables - $columns = sp_get_var_labels( 'sp_metric', true ); // Teams - foreach ( $teams as $key => $team_id ): - - // Get results for players in the team - $players = sp_array_between( (array)get_post_meta( $post->ID, 'sp_player', false ), 0, $key ); - $data = sp_array_combine( $players, sp_array_value( $stats, $team_id, array() ) ); + if ( $teams == array_pad( array_slice( array(), 0, $limit ), $limit, 0 ) ): ?> -
-

- -
+

ID, 'sp_players', true ); + + // Get columns from result variables + $columns = sp_get_var_labels( 'sp_metric', true ); + + foreach ( $teams as $key => $team_id ): + + // Get results for players in the team + $players = sp_array_between( (array)get_post_meta( $post->ID, 'sp_player', false ), 0, $key ); + $data = sp_array_combine( $players, sp_array_value( $stats, $team_id, array() ) ); + + ?> +
+

+ +
+ ID, 'sp_team', false ), 0, $limit ), $limit, 0 ); - $results = (array)get_post_meta( $post->ID, 'sp_results', true ); + + // Teams + if ( $teams == array_pad( array_slice( array(), 0, $limit ), $limit, 0 ) ): - // Get columns from result variables - $columns = sp_get_var_labels( 'sp_result' ); + ?> +

+ -
- -
- ID, 'sp_results', true ); + + // Get columns from result variables + $columns = sp_get_var_labels( 'sp_result' ); + + // Get results for all teams + $data = sp_array_combine( $teams, $results ); + + ?> +
+ +
+ ID, 'sp_player', false ); - $stats = (array)get_post_meta( $post->ID, 'sp_stats', true ); - $division_id = sp_get_the_term_id( $post->ID, 'sp_div', 0 ); + $div_id = sp_get_the_term_id( $post->ID, 'sp_div', 0 ); $team_id = get_post_meta( $post->ID, 'sp_team', true ); - $data = sp_array_combine( $players, sp_array_value( $stats, $division_id, array() ) ); + $player_ids = (array)get_post_meta( $post->ID, 'sp_player', false ); + $stats = (array)get_post_meta( $post->ID, 'sp_players', true ); - // Generate array of placeholder values for each player - $placeholders = array(); - foreach ( $players as $player ): - $args = array( - 'post_type' => 'sp_event', - 'meta_query' => array( - array( - 'key' => 'sp_player', - 'value' => $player - ) - ), - 'tax_query' => array( - array( - 'taxonomy' => 'sp_div', - 'field' => 'id', - 'terms' => $division_id - ) - ) - ); - $placeholders[ $player ] = sp_get_stats_row( 'sp_player', $args, true ); + // Equation Operating System + $eos = new eqEOS(); + + // Get labels from result variables + $metric_labels = (array)sp_get_var_labels( 'sp_metric' ); + + // Get all divisions populated with stats where available + $data = sp_array_combine( $player_ids, $stats ); + + // Get equations from statistics variables + $equations = sp_get_var_equations( 'sp_metric' ); + + // Create entry for each player in totals + $totals = array(); + foreach ( $player_ids as $player_id ): + if ( ! $player_id ) + continue; + $totals[ $player_id ] = array( 'eventsattended' => 0, 'eventsplayed' => 0 ); + + foreach ( $metric_labels as $key => $value ): + $totals[ $player_id ][ $key ] = 0; + endforeach; endforeach; - // Get column names from settings - $stats_settings = get_option( 'sportspress_stats' ); - $columns = sp_get_eos_keys( $stats_settings['player'] ); + $args = array( + 'post_type' => 'sp_event', + 'numberposts' => -1, + 'posts_per_page' => -1, + 'tax_query' => array( + array( + 'taxonomy' => 'sp_div', + 'field' => 'id', + 'terms' => $div_id + ) + ), + 'meta_query' => array( + array( + 'key' => 'sp_team', + 'value' => $team_id, + ) + ) + ); + $events = get_posts( $args ); - // Add first column label - array_unshift( $columns, __( 'Player', 'sportspress' ) ); + // Event loop + foreach( $events as $event ): - sp_stats_table( $data, $placeholders, $division_id, $columns, false ); + $teams = (array)get_post_meta( $event->ID, 'sp_players', true ); + + if ( ! array_key_exists( $team_id, $teams ) ) + continue; + + $players = sp_array_value( $teams, $team_id, array() ); + + foreach ( $players as $player_id => $player_metrics ): + + // Increment events played + $totals[ $player_id ]['eventsplayed']++; + + foreach ( $player_metrics as $key => $value ): + + if ( array_key_exists( $key, $totals[ $player_id ] ) ): + $totals[ $player_id ][ $key ] += $value; + endif; + + endforeach; + + endforeach; + + endforeach; + + // Generate placeholder values for each team + $placeholders = array(); + foreach ( $player_ids as $player_id ): + $placeholders[ $player_id ] = array(); + foreach ( $equations as $key => $value ): + + if ( empty( $value ) ): + + // Reflect totals + $placeholders[ $player_id ][ $key ] = sp_array_value( sp_array_value( $totals, $player_id, array() ), $key, 0 ); + + else: + + // Calculate value + $placeholders[ $player_id ][ $key ] = $eos->solveIF( str_replace( ' ', '', $value ), sp_array_value( $totals, $player_id, array() ) ); + + endif; + + endforeach; + endforeach; + + sp_player_table( $metric_labels, $data, $placeholders ); + sp_nonce(); } ?> \ No newline at end of file diff --git a/admin/post-types/player.php b/admin/post-types/player.php index 21fdf8e7..fcec052e 100644 --- a/admin/post-types/player.php +++ b/admin/post-types/player.php @@ -59,6 +59,13 @@ function sp_player_stats_meta( $post ) { $div_ids[] = $value->term_id; endforeach; + if ( empty( $team_ids ) || $team_ids == array(0) ): + ?> +

+ + + + + + + + + + + + $player_stats ): + if ( !$player_id ) continue; + $div = get_term( $player_id, 'sp_div' ); + ?> + + + $label ): + $value = sp_array_value( $player_stats, $column, '' ); + $placeholder = sp_array_value( sp_array_value( $placeholders, $player_id, array() ), $column, 0 ); + ?> + + + + + +
+ +
+ @@ -799,24 +839,32 @@ if ( !function_exists( 'sp_team_stats_table' ) ) { $div_stats ): - if ( !$div_id ) continue; - $div = get_term( $div_id, 'sp_div' ); + if ( empty( $data ) ): ?> - - - name; ?> - - $label ): - $value = sp_array_value( $div_stats, $column, '' ); - $placeholder = sp_array_value( sp_array_value( $placeholders, $div_id, array() ), $column, 0 ); - ?> - - - + + + $div_stats ): + if ( !$div_id ) continue; + $div = get_term( $div_id, 'sp_div' ); + ?> + + + name; ?> + + $label ): + $value = sp_array_value( $div_stats, $column, '' ); + $placeholder = sp_array_value( sp_array_value( $placeholders, $div_id, array() ), $column, 0 ); + ?> + + + + @@ -840,6 +888,12 @@ if ( !function_exists( 'sp_player_metrics_table' ) ) { $team_stats ): + if ( empty( $team_stats ) ): + ?> + + $div_stats ): if ( !$div_id ) continue; $div = get_term( $div_id, 'sp_div' ); @@ -968,56 +1022,6 @@ if ( !function_exists( 'sp_event_players_table' ) ) { } } -if ( !function_exists( 'sp_someother_table' ) ) { - function sp_someother_table( $columns = array(), $data = array(), $placeholders = array(), $team_id ) { - ?> - - - - - - - - - - - $player_metrics ): - if ( !$player_id ) continue; - ?> - - - $label ): - $value = sp_array_value( $player_metrics, $column, '' ); - $placeholder = (int)sp_array_value( sp_array_value( $placeholders, $player_id, 0), $column, 0 ); - ?> - - - - - - - $label ): - $player_id = 0; - $value = sp_array_value( $player_metrics, $column, '' ); - $placeholder = (int)sp_array_value( sp_array_value( $placeholders, $player_id, 0), $column, 0 ); - ?> - - - - -
- -
-