From d704c6bc1b1d254dd7c0e7e12b930eab28334d75 Mon Sep 17 00:00:00 2001 From: Takumi Date: Tue, 6 Aug 2013 23:02:18 +1000 Subject: [PATCH] Display player stats per team per league --- globals.php | 25 ++++++++---- helpers.php | 111 ++++++++++++++++++++++++++++++++++------------------ player.php | 75 +++++++++++++++++++++++++++++++---- 3 files changed, 157 insertions(+), 54 deletions(-) diff --git a/globals.php b/globals.php index 34e20d89..09d26aa9 100644 --- a/globals.php +++ b/globals.php @@ -55,19 +55,28 @@ $sportspress_options = array( 'settings' => array( 'sp_event_team_count' => 2, 'sp_team_stats_columns' => 'P: $played - W: $wins - D: $ties - L: $losses - F: $for - A: $against - GD: $for-$against - PTS: 3$wins+$ties' + W: $wins + D: $ties + L: $losses + F: $for + A: $against + GD: $for-$against + PTS: 3$wins+$ties', + 'sp_event_stats_columns' => 'Goals: $statsa + Assists: $statsb + Yellow Cards: $statsc + Red Cards: $statsd', + 'sp_player_stats_columns' => 'Attendances: $played + Goals: $statsa + Assists: $statsb + Yellow Cards: $statsc + Red Cards: $statsd', ) ); foreach( $sportspress_options as $optiongroupkey => $optiongroup ) { foreach( $optiongroup as $key => $value ) { - if ( get_option( $key ) === false ) + //if ( get_option( $key ) === false ) update_option( $key, $value ); } } diff --git a/helpers.php b/helpers.php index 525e23fb..3feffc94 100644 --- a/helpers.php +++ b/helpers.php @@ -55,6 +55,15 @@ if ( !function_exists( 'sp_array_combine' ) ) { } } +if ( !function_exists( 'sp_num_to_letter' ) ) { + function sp_num_to_letter( $num, $uppercase = false ) { + $num -= 0; + $letter = chr( ( $num % 26 ) + 97 ); + $letter .= ( floor( $num / 26 ) > 0 ) ? str_repeat( $letter, floor( $num / 26 ) ) : ''; + return ( $uppercase ? strtoupper( $letter ) : $letter ); + } +} + if ( !function_exists( 'sp_cpt_labels' ) ) { function sp_cpt_labels( $name, $singular_name ) { $labels = array( @@ -247,63 +256,77 @@ if ( !function_exists( 'sp_get_stats_row' ) ) { function sp_get_stats_row( $post_type = 'post', $args = array() ) { $args = array_merge( array( - 'meta_value' => 0, 'posts_per_page' => -1 ), (array)$args ); $posts = (array)get_posts( $args ); - foreach( $posts as $post ): - $post->sp_team = get_post_meta( $post->ID, 'sp_team', false ); - $post->sp_team_index = array_search( $args['meta_value'], $post->sp_team ); - $post->sp_result = get_post_meta( $post->ID, 'sp_result', false ); - endforeach; - // Load Equation Operating System + // Equation Operating System $eos = new eqEOS(); - // Define variables to use in EOS $vars = array(); - - $vars['played'] = sizeof( $posts ); - - $vars['wins'] = sizeof( array_filter( $posts, function( $post ) { - return array_count_values( $post->sp_result ) > 1 && max( $post->sp_result ) == $post->sp_result[ $post->sp_team_index ]; - } ) ); - - $vars['ties'] = sizeof( array_filter( $posts, function( $post ) { - return array_count_values( $post->sp_result ) == 1; - } ) ); - - $vars['losses'] = sizeof( array_filter( $posts, function( $post ) { - return array_count_values( $post->sp_result ) > 1 && min( $post->sp_result ) == $post->sp_result[ $post->sp_team_index ]; - } ) ); - - $vars['for'] = 0; - foreach( $posts as $post ): - $vars['for'] += $post->sp_result[ $post->sp_team_index ]; - endforeach; - - $vars['against'] = 0; - foreach( $posts as $post ): - $result = $post->sp_result; - unset( $result[ $post->sp_team_index ] ); - $vars['against'] += array_sum( $result ); - endforeach; - switch ($post_type): case 'sp_team': + + // Add object properties needed for retreiving event stats + foreach( $posts as $post ): + $post->sp_team = get_post_meta( $post->ID, 'sp_team', false ); + $post->sp_team_index = array_search( $args['meta_value'], $post->sp_team ); + $post->sp_result = get_post_meta( $post->ID, 'sp_result', false ); + endforeach; + + // Get team stats from all attended events + $vars['played'] = sizeof( $posts ); + $vars['wins'] = sizeof( array_filter( $posts, function( $post ) { return array_count_values( $post->sp_result ) > 1 && max( $post->sp_result ) == $post->sp_result[ $post->sp_team_index ]; } ) ); + $vars['ties'] = sizeof( array_filter( $posts, function( $post ) { return array_count_values( $post->sp_result ) == 1; } ) ); + $vars['losses'] = sizeof( array_filter( $posts, function( $post ) { return array_count_values( $post->sp_result ) > 1 && min( $post->sp_result ) == $post->sp_result[ $post->sp_team_index ]; } ) ); + $vars['for'] = 0; foreach( $posts as $post ): $vars['for'] += $post->sp_result[ $post->sp_team_index ]; endforeach; + $vars['against'] = 0; foreach( $posts as $post ): $result = $post->sp_result; unset( $result[ $post->sp_team_index ] ); $vars['against'] += array_sum( $result ); endforeach; + + // Get EOS array $rows = sp_get_eos_array( get_option( 'sp_team_stats_columns' ) ); break; + + case 'sp_player': + + // Create array of event stats columns + $columns = sp_get_eos_array( get_option( 'sp_event_stats_columns' ) ); + foreach ( $columns as $key => $value ): + $vars[ 'stats' . sp_num_to_letter( $key ) ] = 0; + endforeach; + + // Populate columns with player stats from events + foreach ( $posts as $post ): + $team_stats = get_post_meta( $post->ID, 'sp_stats', true ); + foreach ( $team_stats as $team_id => $stat ): + if ( array_key_exists( 1, $args['meta_query'] ) && $team_id != sp_array_value( $args['meta_query'][1], 'value', 0 ) ) continue; + $player_id = sp_array_value( $args['meta_query'][0], 'value', 0 ); + if ( !array_key_exists( $player_id, $stat ) ) continue; + foreach ( $stat[ $player_id ] as $key => $value ): + $vars[ 'stats' . sp_num_to_letter( $key ) ] += $value; + endforeach; + endforeach; + endforeach; + + // Add played event count to vars + $vars['played'] = sizeof( $posts ); + + // Get EOS array + $rows = sp_get_eos_array( get_option( 'sp_player_stats_columns' ) ); + break; + default: + $rows = array(); break; + endswitch; $output = array(); foreach ( $rows as $key => $value ): $row = explode( ':', $value ); - $output[] = $eos->solveIF( sp_array_value( $row, 1, 'played'), $vars ); + $output[] = $eos->solveIF( sp_array_value( $row, 1, '$played'), $vars ); endforeach; return $output; @@ -348,7 +371,7 @@ if ( !function_exists( 'sp_get_stats' ) ) { // Get fallback values switch ( $post_type ): - // Teams: all events attended in the league + // Team: all events attended in the league case 'sp_team': $args = array( 'post_type' => 'sp_event', @@ -360,6 +383,18 @@ if ( !function_exists( 'sp_get_stats' ) ) { $fallback = sp_get_stats_row( $args ); break; + // Player: all events attended in the league + case 'sp_player': + $args = array( + 'post_type' => 'sp_event', + 'meta_key' => 'sp_player', + 'meta_value' => $post_id, + 'taxonomy' => 'sp_league', + 'terms' => $subset_id + ); + $fallback = sp_get_stats_row( $args ); + break; + endswitch; // Merge fallback values with current subset @@ -404,7 +439,7 @@ if ( !function_exists( 'sp_stats_table' ) ) { break; default: $term = get_term( $key, $rowtype ); - $title = $term->name;; + $title = $term->name; break; endswitch; if ( empty( $title ) ) diff --git a/player.php b/player.php index c5d62caa..1d827dd2 100644 --- a/player.php +++ b/player.php @@ -37,20 +37,79 @@ function sp_player_stats_meta( $post ) { $leagues = (array)get_the_terms( $post->ID, 'sp_league' ); $stats = (array)get_post_meta( $post->ID, 'sp_stats', true ); - // Overall - $data = sp_array_combine( $teams, sp_array_value( $stats, 0, array() ) ); + // Generate array of all league ids + $league_ids = array( 0 ); + foreach ( $leagues as $key => $value ): + if ( is_object( $value ) && property_exists( $value, 'term_id' ) ) + $league_ids[] = $value->term_id; + endforeach; + + // Get all teams populated with overall stats where availabled + $data = sp_array_combine( $league_ids, sp_array_value( $stats, 0, array() ) ); + + // Generate array of placeholder values for each league + $placeholders = array(); + foreach ( $league_ids as $league_id ): + $args = array( + 'post_type' => 'sp_event', + 'meta_query' => array( + array( + 'key' => 'sp_player', + 'value' => $post->ID + ) + ) + ); + if ( $league_id ): + $args['tax_query'] = array( + array( + 'taxonomy' => 'sp_league', + 'field' => 'id', + 'terms' => $league_id + ) + ); + endif; + $placeholders[ $league_id ] = sp_get_stats_row( 'sp_player', $args ); + endforeach; ?>

- + term_id, array() ) ); + foreach ( $teams as $team ): + if ( !$team ) continue; + + // Get all leagues populated with stats where availabled + $data = sp_array_combine( $league_ids, sp_array_value( $stats, $team, array() ) ); + + // Generate array of placeholder values for each league + $placeholders = array(); + foreach ( $league_ids as $league_id ): + $args = array( + 'post_type' => 'sp_event', + 'meta_query' => array( + array( + 'key' => 'sp_player', + 'value' => $post->ID + ), + array( + 'key' => 'sp_team', + 'value' => $team + ) + ), + 'tax_query' => array( + array( + 'taxonomy' => 'sp_league', + 'field' => 'id', + 'terms' => $league_id + ) + ) + ); + $placeholders[ $league_id ] = sp_get_stats_row( 'sp_player', $args ); + endforeach; ?> -

name; ?>

- term_id, array( 'Team', 'Played', 'Goals', 'Assists', 'Yellow Cards', 'Red Cards' ) ); ?> +

+