From 9354103af01c6aaeb9c402919659dab4e06d29cf Mon Sep 17 00:00:00 2001 From: ThemeBoy Date: Mon, 2 Dec 2013 02:25:21 +1100 Subject: [PATCH] Add single player metrics EOS table functionality --- admin/post-types/event.php | 2 +- admin/post-types/player.php | 132 ++++++++++++++++++++---------------- admin/post-types/team.php | 9 ++- assets/css/admin.css | 10 +-- assets/js/admin.js | 10 +-- sportspress-actions.php | 14 ++-- sportspress-helpers.php | 56 +++++++++++++-- 7 files changed, 147 insertions(+), 86 deletions(-) diff --git a/admin/post-types/event.php b/admin/post-types/event.php index 147d018a..06edd324 100644 --- a/admin/post-types/event.php +++ b/admin/post-types/event.php @@ -75,7 +75,7 @@ function sp_event_teams_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 ); + $stats = (array)get_post_meta( $post->ID, 'sp_players', true ); // Get columns from result variables $columns = sp_get_var_labels( 'sp_metric', true ); diff --git a/admin/post-types/player.php b/admin/post-types/player.php index 314edb48..4a4c8ced 100644 --- a/admin/post-types/player.php +++ b/admin/post-types/player.php @@ -35,98 +35,112 @@ function sp_player_team_meta( $post ) { } function sp_player_stats_meta( $post ) { - $teams = (array)get_post_meta( $post->ID, 'sp_team', false ); + $team_ids = (array)get_post_meta( $post->ID, 'sp_team', false ); $divisions = (array)get_the_terms( $post->ID, 'sp_div' ); - $stats = (array)get_post_meta( $post->ID, 'sp_stats', true ); + $stats = (array)get_post_meta( $post->ID, 'sp_metrics', true ); - // Get column names from settings - $stats_settings = get_option( 'sportspress_stats' ); - $columns = sp_get_eos_keys( $stats_settings['player'] ); + // Equation Operating System + $eos = new eqEOS(); - // Add first column label - array_unshift( $columns, __( 'Division', 'sportspress' ) ); + // Get labels from metric variables + $metric_labels = (array)sp_get_var_labels( 'sp_metric' ); + + $totals = array( 'eventsattended' => 0, 'eventsplayed' => 0 ); + $placeholders = array(); + + foreach ( $metric_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 teams populated with overall stats where availabled - $data = sp_array_combine( $division_ids, sp_array_value( $stats, 0, array() ) ); + // Loop through statistics for each team + foreach ( $team_ids as $team_id ): + if ( !$team_id ) continue; - // Generate array of placeholder values for each division - $placeholders = array(); - foreach ( $division_ids as $division_id ): - $args = array( - 'post_type' => 'sp_event', - 'meta_query' => array( - array( - 'key' => 'sp_player', - 'value' => $post->ID - ) - ) - ); - if ( $division_id ): - $args['tax_query'] = array( - array( - 'taxonomy' => 'sp_div', - 'field' => 'id', - 'terms' => $division_id - ) - ); - endif; - $placeholders[ $division_id ] = sp_get_stats_row( 'sp_player', $args ); - endforeach; - ?> -

- 'sp_event', + 'numberposts' => -1, + 'posts_per_page' => -1, 'meta_query' => array( + 'relation' => 'AND', + array( + 'key' => 'sp_team', + 'value' => $team_id + ), array( 'key' => 'sp_player', 'value' => $post->ID - ), - array( - 'key' => 'sp_team', - 'value' => $team ) ), 'tax_query' => array( array( 'taxonomy' => 'sp_div', 'field' => 'id', - 'terms' => $division_id + 'terms' => $div_id ) ) ); - $placeholders[ $division_id ] = sp_get_stats_row( 'sp_player', $args ); + $events = get_posts( $args ); + foreach( $events as $event ): + $totals['eventsattended']++; + $totals['eventsplayed']++; // TODO: create tab for substitutes in sidebar + $team_metrics = (array)get_post_meta( $event->ID, 'sp_players', true ); + if ( array_key_exists( $team_id, $team_metrics ) ): + $players = sp_array_value( $team_metrics, $team_id, array() ); + if ( array_key_exists( $post->ID, $players ) ): + $player_metrics = sp_array_value( $players, $post->ID, array() ); + foreach ( $player_metrics as $key => $value ): + if ( array_key_exists( $key, $totals ) ): + $totals[ $key ] += $value; + endif; + endforeach; + endif; + endif; + endforeach; + + // Generate array of placeholder values for each division + $placeholders[ $team_id ][ $div_id ] = array(); + foreach ( $equations as $key => $value ): + + if ( empty( $value ) ): + + // Reflect totals + $placeholders[ $team_id ][ $div_id ][ $key ] = sp_array_value( $totals, $key, 0 ); + + else: + + // Calculate value + $placeholders[ $team_id ][ $div_id ][ $key ] = $eos->solveIF( str_replace( ' ', '', $value ), $totals ); + + endif; + + endforeach; + endforeach; - ?> -

- +

0 ); $placeholders = array(); @@ -99,8 +96,8 @@ function sp_team_stats_meta( $post ) { ); $events = get_posts( $args ); foreach( $events as $event ): - $results = (array)get_post_meta( $event->ID, 'sp_results', true ); $totals['eventsplayed']++; + $results = (array)get_post_meta( $event->ID, 'sp_results', true ); foreach ( $results as $team_id => $team_result ): foreach ( $team_result as $key => $value ): if ( $team_id == $post->ID ): @@ -124,13 +121,15 @@ 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 ): $placeholders[ $div_id ][ $key ] = $eos->solveIF( str_replace( ' ', '', $value ), $totals ); endforeach; - //$placeholders[ $division_id ] = sp_get_stats_row( 'sp_team', $args ); endforeach; // Get columns from statistics variables diff --git a/assets/css/admin.css b/assets/css/admin.css index 3a8f599b..6d2976d5 100644 --- a/assets/css/admin.css +++ b/assets/css/admin.css @@ -90,16 +90,16 @@ #sp_profilediv .wp-editor-container { background-color:#fff; } -.sp-stats-table td { +.sp-data-table td { padding: 4px 7px; line-height: 2; } -.sp-stats-table td:first-child { +.sp-data-table td:first-child { white-space: nowrap; } -.sp-stats-table input[type="text"], -.sp-stats-table input[type="number"], -.sp-stats-table select { +.sp-data-table input[type="text"], +.sp-data-table input[type="number"], +.sp-data-table select { min-width: 14px; width: 100%; } diff --git a/assets/js/admin.js b/assets/js/admin.js index 4c53ac22..60b8b6b1 100644 --- a/assets/js/admin.js +++ b/assets/js/admin.js @@ -41,10 +41,10 @@ jQuery(document).ready(function($){ }); // Total stats calculator - $('.sp-stats-table .sp-total input').on('updateTotal', function() { + $('.sp-data-table .sp-total input').on('updateTotal', function() { index = $(this).parent().index(); var sum = 0; - $(this).closest('.sp-stats-table').find('.sp-post').each(function() { + $(this).closest('.sp-data-table').find('.sp-post').each(function() { val = $(this).find('td').eq(index).find('input').val(); if(val == '') { val = $(this).find('td').eq(index).find('input').attr('placeholder'); @@ -57,9 +57,9 @@ jQuery(document).ready(function($){ }); // Activate total stats calculator - if($('.sp-stats-table .sp-total').size()) { - $('.sp-stats-table .sp-post td input').on('keyup', function() { - $(this).closest('.sp-stats-table').find('.sp-total td').eq($(this).parent().index()).find('input').trigger('updateTotal'); + 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'); }); } diff --git a/sportspress-actions.php b/sportspress-actions.php index 493bedce..d2612bc1 100644 --- a/sportspress-actions.php +++ b/sportspress-actions.php @@ -125,7 +125,7 @@ function sp_save_post( $post_id ) { // Update stats update_post_meta( $post_id, 'sp_stats', sp_array_value( $_POST, 'sp_stats', array() ) ); - + break; case ( 'sp_event' ): @@ -136,8 +136,8 @@ function sp_save_post( $post_id ) { // Update results update_post_meta( $post_id, 'sp_results', $results ); - // Update stats - update_post_meta( $post_id, 'sp_stats', sp_array_value( $_POST, 'sp_stats', array() ) ); + // Update player metrics + update_post_meta( $post_id, 'sp_players', sp_array_value( $_POST, 'sp_players', array() ) ); // Update team array sp_update_post_meta_recursive( $post_id, 'sp_team', sp_array_value( $_POST, 'sp_team', array() ) ); @@ -176,9 +176,15 @@ function sp_save_post( $post_id ) { break; case ( 'sp_player' ): - update_post_meta( $post_id, 'sp_stats', sp_array_value( $_POST, 'sp_stats', array() ) ); + + // Update player metrics + update_post_meta( $post_id, 'sp_metrics', sp_array_value( $_POST, 'sp_metrics', array() ) ); + + // Update team array sp_update_post_meta_recursive( $post_id, 'sp_team', sp_array_value( $_POST, 'sp_team', array() ) ); + break; + case ( 'sp_staff' ): sp_update_post_meta_recursive( $post_id, 'sp_team', sp_array_value( $_POST, 'sp_team', array() ) ); break; diff --git a/sportspress-helpers.php b/sportspress-helpers.php index 1cbcb66f..360052fb 100644 --- a/sportspress-helpers.php +++ b/sportspress-helpers.php @@ -682,7 +682,7 @@ if ( !function_exists( 'sp_stats_table' ) ) { if ( !is_array( $stats ) ) $stats = array( __( 'Name', 'sportspress' ) ); ?> - +
@@ -744,7 +744,7 @@ if ( !function_exists( 'sp_stats_table' ) ) { if ( !function_exists( 'sp_team_stats_table' ) ) { function sp_team_stats_table( $columns = array(), $data = array(), $placeholders = array() ) { ?> -
+
@@ -781,10 +781,52 @@ if ( !function_exists( 'sp_team_stats_table' ) ) { } } +if ( !function_exists( 'sp_player_metrics_table' ) ) { + function sp_player_metrics_table( $columns = array(), $data = array(), $placeholders = array() ) { + ?> +
+ + + + + + + + + + $team_stats ): + foreach ( $team_stats as $div_id => $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( sp_array_value( $placeholders, $team_id, array() ), $div_id, array() ), $column, 0 ); + ?> + + + + + +
+ name; ?> +
+ - +
@@ -838,7 +880,7 @@ if ( !function_exists( 'sp_event_results_table' ) ) { if ( !function_exists( 'sp_event_players_table' ) ) { function sp_event_players_table( $columns = array(), $data = array(), $team_id ) { ?> -
+
@@ -860,7 +902,7 @@ if ( !function_exists( 'sp_event_players_table' ) ) { $label ): $value = sp_array_value( $player_metrics, $column, '' ); ?> - + - + @@ -886,7 +928,7 @@ 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 ) { ?> -
+