diff --git a/admin/post-types/event.php b/admin/post-types/event.php index 06edd324..eb0a036f 100644 --- a/admin/post-types/event.php +++ b/admin/post-types/event.php @@ -31,13 +31,13 @@ add_filter( 'the_posts', 'sp_event_display_scheduled' ); function sp_event_meta_init() { remove_meta_box( 'submitdiv', 'sp_event', 'side' ); add_meta_box( 'submitdiv', __( 'Event', 'sportspress' ), 'post_submit_meta_box', 'sp_event', 'side', 'high' ); - add_meta_box( 'sp_teamsdiv', __( 'Teams', 'sportspress' ), 'sp_event_teams_meta', 'sp_event', 'side', 'high' ); + add_meta_box( 'sp_teamdiv', __( 'Teams', 'sportspress' ), 'sp_event_team_meta', 'sp_event', 'side', 'high' ); add_meta_box( 'sp_playersdiv', __( 'Players', 'sportspress' ), 'sp_event_players_meta', 'sp_event', 'normal', 'high' ); add_meta_box( 'sp_resultsdiv', __( 'Results', 'sportspress' ), 'sp_event_results_meta', 'sp_event', 'normal', 'high' ); add_meta_box( 'sp_articlediv', __( 'Article', 'sportspress' ), 'sp_event_article_meta', 'sp_event', 'normal', 'high' ); } -function sp_event_teams_meta( $post ) { +function sp_event_team_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 ); $players = (array)get_post_meta( $post->ID, 'sp_player', false ); diff --git a/admin/post-types/metric.php b/admin/post-types/metric.php index a42a5729..2a0f78ad 100644 --- a/admin/post-types/metric.php +++ b/admin/post-types/metric.php @@ -40,7 +40,7 @@ function sp_metric_equation_meta( $post ) {
ID, $piece, array( 'event' ) ); + sp_get_equation_selector( $post->ID, $piece, array( 'player_event' ) ); endforeach; ?>
diff --git a/admin/post-types/player.php b/admin/post-types/player.php index 4a4c8ced..21fdf8e7 100644 --- a/admin/post-types/player.php +++ b/admin/post-types/player.php @@ -53,10 +53,10 @@ function sp_player_stats_meta( $post ) { endforeach; // Generate array of all division ids - $div_id = array(); + $div_ids = array(); foreach ( $divisions as $key => $value ): if ( is_object( $value ) && property_exists( $value, 'term_id' ) ) - $div_id[] = $value->term_id; + $div_ids[] = $value->term_id; endforeach; // Loop through statistics for each team @@ -64,12 +64,12 @@ function sp_player_stats_meta( $post ) { if ( !$team_id ) continue; // Get all divisions populated with stats where available - $data[ $team_id ] = sp_array_combine( $div_id, $stats[ $team_id ] ); + $data[ $team_id ] = sp_array_combine( $div_ids, $stats[ $team_id ] ); // Get equations from statistics variables $equations = sp_get_var_equations( 'sp_metric' ); - foreach ( $div_id as $div_id ): + foreach ( $div_ids as $div_id ): $args = array( 'post_type' => 'sp_event', 'numberposts' => -1, diff --git a/admin/post-types/stat.php b/admin/post-types/stat.php index 6f4740df..75476fb1 100644 --- a/admin/post-types/stat.php +++ b/admin/post-types/stat.php @@ -41,7 +41,7 @@ function sp_stat_equation_meta( $post ) {ID, $piece, array( 'event', 'result', 'outcome' ) ); + sp_get_equation_selector( $post->ID, $piece, array( 'team_event', 'result', 'outcome' ) ); endforeach; ?>
diff --git a/admin/post-types/table.php b/admin/post-types/table.php index 3acc6052..240f6e2d 100644 --- a/admin/post-types/table.php +++ b/admin/post-types/table.php @@ -59,73 +59,95 @@ function sp_table_team_meta( $post ) { } function sp_table_stats_meta( $post ) { - $teams = (array)get_post_meta( $post->ID, 'sp_team', false ); - $stats = (array)get_post_meta( $post->ID, 'sp_stats', true ); - $division_id = sp_get_the_term_id( $post->ID, 'sp_div', 0 ); - $data = sp_array_combine( $teams, sp_array_value( $stats, $division_id, array() ) ); + $div_id = sp_get_the_term_id( $post->ID, 'sp_div', 0 ); + $team_ids = (array)get_post_meta( $post->ID, 'sp_team', false ); + $stats = (array)get_post_meta( $post->ID, 'sp_teams', true ); - // Generate array of placeholder values for each team - $placeholders = array(); - foreach ( $teams as $team ): - $args = array( - 'post_type' => 'sp_event', - 'meta_query' => array( - array( - 'key' => 'sp_team', - 'value' => $team - ) - ), - 'tax_query' => array( - array( - 'taxonomy' => 'sp_div', - 'field' => 'id', - 'terms' => $division_id - ) - ) - ); - $placeholders[ $team ] = sp_get_stats_row( $team, 'sp_team', $args, true ); + // Equation Operating System + $eos = new eqEOS(); + + // Get labels from result variables + $result_labels = (array)sp_get_var_labels( 'sp_result' ); + + // Get labels from outcome variables + $outcome_labels = (array)sp_get_var_labels( 'sp_outcome' ); + + // Get all divisions populated with stats where available + $data = sp_array_combine( $team_ids, $stats ); + + // Get equations from statistics variables + $equations = sp_get_var_equations( 'sp_stat' ); + + // Create entry for each team in totals + $totals = array(); + foreach ( $team_ids as $team_id ): + $totals[ $team_id ] = array( 'eventsplayed' => 0 ); + + foreach ( $result_labels as $key => $value ): + $totals[ $team_id ][ $key . 'for' ] = 0; + $totals[ $team_id ][ $key . 'against' ] = 0; + endforeach; + + foreach ( $outcome_labels as $key => $value ): + $totals[ $team_id ][ $key ] = 0; + endforeach; endforeach; - $type = 'sp_stat'; $args = array( - 'post_type' => $type, + 'post_type' => 'sp_event', 'numberposts' => -1, 'posts_per_page' => -1, - 'orderby' => 'menu_order', - 'order' => 'ASC', - 'exclude' => $postid + 'tax_query' => array( + array( + 'taxonomy' => 'sp_div', + 'field' => 'id', + 'terms' => $div_id + ) + ) ); - $vars = get_posts( $args ); + $events = get_posts( $args ); + + // Event loop + foreach( $events as $event ): + + $results = (array)get_post_meta( $event->ID, 'sp_results', true ); + + foreach ( $results as $team_id => $team_result ): + + // Increment events played + $totals[ $team_id ]['eventsplayed']++; + + foreach ( $team_result as $key => $value ): + + if ( $key == 'outcome' ): + if ( array_key_exists( $value, $totals[ $team_id ] ) ): + $totals[ $team_id ][ $value ]++; + endif; + else: + if ( array_key_exists( $key . 'for', $totals[ $team_id ] ) ): + $totals[ $team_id ][ $key . 'for' ] += $value; + endif; + endif; + + endforeach; + + endforeach; - $columns = array(); - foreach ( $vars as $var ): - $columns[ $var->post_name ] = $var->post_title; endforeach; - // Add first column label - array_unshift( $columns, __( 'Team', 'sportspress' ) ); + // Generate placeholder values for each team + $placeholders = array(); + foreach ( $team_ids as $team_id ): + $placeholders[ $team_id ] = array(); + foreach ( $equations as $key => $value ): + $placeholders[ $team_id ][ $key ] = $eos->solveIF( str_replace( ' ', '', $value ), $totals[ $team_id ] ); + endforeach; + endforeach; + // Get columns from statistics variables + $columns = sp_get_var_labels( 'sp_stat' ); - echo '$placeholders'; - echo ''; - print_r( $placeholders ); - echo ''; - - echo '$data'; - echo '
'; - print_r( $data ); - echo ''; - - echo '$division_id'; - echo '
'; - print_r( $division_id ); - echo ''; - - echo '$columns'; - echo '
'; - print_r( $columns ); - echo ''; - - sp_stats_table( $data, $placeholders, $division_id, $columns, false ); + sp_league_table( $columns, $data, $placeholders ); + sp_nonce(); } ?> \ No newline at end of file diff --git a/admin/post-types/team.php b/admin/post-types/team.php index acab29ea..587bf326 100644 --- a/admin/post-types/team.php +++ b/admin/post-types/team.php @@ -63,19 +63,19 @@ function sp_team_stats_meta( $post ) { endforeach; // Generate array of all division ids - $div_id = array(); + $div_ids = array(); foreach ( $divisions as $key => $value ): if ( is_object( $value ) && property_exists( $value, 'term_id' ) ) - $div_id[] = $value->term_id; + $div_ids[] = $value->term_id; endforeach; // Get all divisions populated with stats where available - $data = sp_array_combine( $div_id, $stats ); + $data = sp_array_combine( $div_ids, $stats ); // Get equations from statistics variables $equations = sp_get_var_equations( 'sp_stat' ); - foreach ( $div_id as $div_id ): + foreach ( $div_ids as $div_id ): $args = array( 'post_type' => 'sp_event', 'numberposts' => -1, @@ -103,7 +103,7 @@ function sp_team_stats_meta( $post ) { if ( $team_id == $post->ID ): if ( $key == 'outcome' ): if ( array_key_exists( $value, $totals ) ): - $totals[ $value]++; + $totals[ $value ]++; endif; else: if ( array_key_exists( $key . 'for', $totals ) ): @@ -121,9 +121,6 @@ function sp_team_stats_meta( $post ) { endforeach; endforeach; - // Total events attended is same as played for teams - $totals['eventsattended'] = $totals['eventsplayed']; - // Generate array of placeholder values for each division $placeholders[ $div_id ] = array(); foreach ( $equations as $key => $value ): diff --git a/sportspress-helpers.php b/sportspress-helpers.php index 360052fb..9722bc97 100644 --- a/sportspress-helpers.php +++ b/sportspress-helpers.php @@ -359,9 +359,12 @@ if ( !function_exists( 'sp_get_equation_selector' ) ) { // Add groups to options foreach ( $groups as $group ): switch ( $group ): - case 'event': + case 'player_event': $options[ __( 'Events', 'sportspress' ) ] = array( '$eventsattended' => __( 'Attended', 'sportspress' ), '$eventsplayed' => __( 'Played', 'sportspress' ) ); break; + case 'team_event': + $options[ __( 'Events', 'sportspress' ) ] = array( '$eventsplayed' => __( 'Played', 'sportspress' ) ); + break; case 'result': $options[ __( 'Results', 'sportspress' ) ] = sp_get_equation_optgroup_array( $postid, 'sp_result', array( 'for' => '→', 'against' => '←' ), null, false ); break; @@ -741,6 +744,46 @@ if ( !function_exists( 'sp_stats_table' ) ) { } } +if ( !function_exists( 'sp_league_table' ) ) { + function sp_league_table( $columns = array(), $data = array(), $placeholders = array() ) { + ?> +
| + + | + + |
|---|---|
| + + | + $label ): + $value = sp_array_value( $team_stats, $column, '' ); + $placeholder = sp_array_value( sp_array_value( $placeholders, $team_id, array() ), $column, 0 ); + ?> ++ + |