diff --git a/admin/post-types/event.php b/admin/post-types/event.php index 867e1896..147d018a 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_teamdiv', __( 'Teams', 'sportspress' ), 'sp_event_team_meta', 'sp_event', 'side', 'high' ); - add_meta_box( 'sp_statsdiv', __( 'Statistics', 'sportspress' ), 'sp_event_stats_meta', 'sp_event', 'normal', 'high' ); + add_meta_box( 'sp_teamsdiv', __( 'Teams', 'sportspress' ), 'sp_event_teams_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_team_meta( $post ) { +function sp_event_teams_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 ); @@ -72,13 +72,13 @@ function sp_event_team_meta( $post ) { sp_nonce(); } -function sp_event_stats_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_stats', true ); // Get columns from result variables - $columns = sp_get_var_columns( 'sp_metric', true ); + $columns = sp_get_var_labels( 'sp_metric', true ); // Teams foreach ( $teams as $key => $team_id ): @@ -104,7 +104,7 @@ function sp_event_results_meta( $post ) { $results = (array)get_post_meta( $post->ID, 'sp_results', true ); // Get columns from result variables - $columns = sp_get_var_columns( 'sp_result' ); + $columns = sp_get_var_labels( 'sp_result' ); // Get results for all teams $data = sp_array_combine( $teams, $results ); diff --git a/admin/post-types/team.php b/admin/post-types/team.php index d0768781..8ad8f73a 100644 --- a/admin/post-types/team.php +++ b/admin/post-types/team.php @@ -41,21 +41,48 @@ function sp_team_stats_meta( $post ) { $divisions = (array)get_the_terms( $post->ID, 'sp_div' ); $stats = (array)get_post_meta( $post->ID, 'sp_stats', 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' ); + + // Merge result and outcome variable labels + $labels = array_merge( $result_labels, $outcome_labels ); + + $totals = array( 'eventsplayed' => 0 ); + $placeholders = array(); + + foreach ( $result_labels as $key => $value ): + $totals[ $key . 'for' ] = 0; + $totals[ $key . 'against' ] = 0; + endforeach; + + foreach ( $outcome_labels as $key => $value ): + $totals[ $key ] = 0; + endforeach; + // Generate array of all division ids - $division_ids = array( 0 ); + $div_id = array(); foreach ( $divisions as $key => $value ): if ( is_object( $value ) && property_exists( $value, 'term_id' ) ) - $division_ids[] = $value->term_id; + $div_id[] = $value->term_id; endforeach; // Get all divisions populated with stats where available - $data = sp_array_combine( $division_ids, sp_array_value( $stats, 0, array() ) ); + $data = sp_array_combine( $div_id, $stats ); - // Generate array of placeholder values for each division - $placeholders = array(); - foreach ( $division_ids as $division_id ): + // Get equations from statistics variables + $equations = sp_get_var_equations( 'sp_stat' ); + + foreach ( $div_id as $div_id ): $args = array( 'post_type' => 'sp_event', + 'numberposts' => -1, + 'posts_per_page' => -1, 'meta_query' => array( array( 'key' => 'sp_team', @@ -66,21 +93,50 @@ function sp_team_stats_meta( $post ) { array( 'taxonomy' => 'sp_div', 'field' => 'id', - 'terms' => $division_id + 'terms' => $div_id ) ) ); - $placeholders[ $division_id ] = sp_get_stats_row( 'sp_team', $args ); + $events = get_posts( $args ); + foreach( $events as $event ): + $results = (array)get_post_meta( $event->ID, 'sp_results', true ); + $totals['eventsplayed']++; + foreach ( $results as $team_id => $team_result ): + foreach ( $team_result as $key => $value ): + if ( $team_id == $post->ID ): + if ( $key == 'outcome' ): + if ( array_key_exists( $value, $totals ) ): + $totals[ $value]++; + endif; + else: + if ( array_key_exists( $key . 'for', $totals ) ): + $totals[ $key . 'for' ] += $value; + endif; + endif; + else: + if ( $key != 'outcome' ): + if ( array_key_exists( $key . 'against', $totals ) ): + $totals[ $key . 'against' ] += $value; + endif; + endif; + endif; + endforeach; + endforeach; + endforeach; + + // Generate array of placeholder values for each division + $placeholders[ $div_id ] = array(); + foreach ( $equations as $key => $value ): + $placeholders[ $div_id ][ $key ] = $eos->solveIF( str_replace( ' ', '', $value ), $totals ); + endforeach; + + //$placeholders[ $division_id ] = sp_get_stats_row( 'sp_team', $args ); endforeach; - // Get column names from settings - $stats_settings = get_option( 'sportspress_stats' ); - $columns = sp_get_eos_keys( $stats_settings['team'] ); + // Get columns from statistics variables + $columns = sp_get_var_labels( 'sp_stat' ); - // Add first column label - array_unshift( $columns, __( 'Division', 'sportspress' ) ); - - sp_stats_table( $data, $placeholders, 0, $columns, false, 'sp_div' ); + sp_team_stats_table( $columns, $data, $placeholders ); sp_nonce(); } ?> \ No newline at end of file diff --git a/assets/css/admin.css b/assets/css/admin.css index 980df0bd..3a8f599b 100644 --- a/assets/css/admin.css +++ b/assets/css/admin.css @@ -90,17 +90,16 @@ #sp_profilediv .wp-editor-container { background-color:#fff; } -#sp_statsdiv .widefat td { +.sp-stats-table td { padding: 4px 7px; line-height: 2; } -#sp_statsdiv .widefat td:first-child { +.sp-stats-table td:first-child { white-space: nowrap; } -#sp_statsdiv .widefat input[type="text"], -#sp_statsdiv .widefat input[type="number"], -#sp_resultsdiv .widefat input[type="text"], -#sp_resultsdiv .widefat input[type="number"] { +.sp-stats-table input[type="text"], +.sp-stats-table input[type="number"], +.sp-stats-table select { min-width: 14px; width: 100%; } diff --git a/sportspress-actions.php b/sportspress-actions.php index 7110519f..493bedce 100644 --- a/sportspress-actions.php +++ b/sportspress-actions.php @@ -122,8 +122,12 @@ function sp_save_post( $post_id ) { if ( !isset( $_POST['sportspress_nonce'] ) || ! wp_verify_nonce( $_POST['sportspress_nonce'], plugin_basename( __FILE__ ) ) ) return $post_id; switch ( $_POST['post_type'] ): case ( 'sp_team' ): + + // Update stats update_post_meta( $post_id, 'sp_stats', sp_array_value( $_POST, 'sp_stats', array() ) ); + break; + case ( 'sp_event' ): // Get results diff --git a/sportspress-helpers.php b/sportspress-helpers.php index abab1940..1cbcb66f 100644 --- a/sportspress-helpers.php +++ b/sportspress-helpers.php @@ -334,7 +334,7 @@ if ( !function_exists( 'sp_get_equation_optgroup_array' ) ) { foreach ( $vars as $var ): if ( $totals ) $arr[ '$' . $var->post_name ] = $var->post_title; foreach ( $variations as $key => $value ): - $arr[ '$' . $var->post_name . '_' . $key ] = $var->post_title . ' ' . $value; + $arr[ '$' . $var->post_name . $key ] = $var->post_title . ' ' . $value; endforeach; endforeach; else: @@ -360,7 +360,7 @@ if ( !function_exists( 'sp_get_equation_selector' ) ) { foreach ( $groups as $group ): switch ( $group ): case 'event': - $options[ __( 'Events', 'sportspress' ) ] = array( '$events_attended' => __( 'Attended', 'sportspress' ), '$events_played' => __( 'Played', 'sportspress' ) ); + $options[ __( 'Events', 'sportspress' ) ] = array( '$eventsattended' => __( 'Attended', 'sportspress' ), '$eventsplayed' => __( 'Played', 'sportspress' ) ); break; case 'result': $options[ __( 'Results', 'sportspress' ) ] = sp_get_equation_optgroup_array( $postid, 'sp_result', array( 'for' => '→', 'against' => '←' ), null, false ); @@ -435,8 +435,8 @@ if ( !function_exists( 'sp_get_eos_keys' ) ) { } -if ( !function_exists( 'sp_get_var_columns' ) ) { - function sp_get_var_columns( $post_type, $indies = false ) { +if ( !function_exists( 'sp_get_var_labels' ) ) { + function sp_get_var_labels( $post_type, $independent = false ) { $args = array( 'post_type' => $post_type, 'numberposts' => -1, @@ -444,7 +444,7 @@ if ( !function_exists( 'sp_get_var_columns' ) ) { 'orderby' => 'menu_order', 'order' => 'ASC' ); - if ( $indies ): + if ( $independent ): $args['meta_query'] = array( array( 'key' => 'sp_equation', @@ -464,6 +464,28 @@ if ( !function_exists( 'sp_get_var_columns' ) ) { } } +if ( !function_exists( 'sp_get_var_equations' ) ) { + function sp_get_var_equations( $post_type ) { + $args = array( + 'post_type' => $post_type, + 'numberposts' => -1, + 'posts_per_page' => -1, + 'orderby' => 'menu_order', + 'order' => 'ASC' + ); + + $vars = get_posts( $args ); + + $output = array(); + foreach ( $vars as $var ): + $equation = get_post_meta( $var->ID, 'sp_equation', true ); + $output[ $var->post_name ] = $equation; + endforeach; + + return $output; + } +} + if ( !function_exists( 'sp_get_stats_row' ) ) { function sp_get_stats_row( $post_id, $post_type = 'post', $args = array(), $static = false ) { $args = array_merge( @@ -486,7 +508,7 @@ if ( !function_exists( 'sp_get_stats_row' ) ) { case 'sp_team': // All events attended by the team - $vars['events_attended'] = $vars['events_played'] = sizeof( $posts ); + $vars['eventsattended'] = $vars['eventsplayed'] = sizeof( $posts ); // Get result variables $args = array( @@ -719,6 +741,46 @@ if ( !function_exists( 'sp_stats_table' ) ) { } } +if ( !function_exists( 'sp_team_stats_table' ) ) { + function sp_team_stats_table( $columns = array(), $data = array(), $placeholders = array() ) { + ?> + + + + + + + + + + + $div_stats ): + if ( !$div_id ) continue; + $div = get_term( $div_id, 'sp_div' ); + ?> + + + $label ): + $value = sp_array_value( $div_stats, $column, '' ); + $placeholder = sp_array_value( sp_array_value( $placeholders, $div_id, array() ), $column, 0 ); + ?> + + + + + +
+ name; ?> +
+