From 5c462689f8d4db408ac2ce816ebdfd06f1cc7776 Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Tue, 6 May 2014 17:21:37 +1000 Subject: [PATCH] Add function to format event performance data --- includes/class-sp-event.php | 52 +++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/includes/class-sp-event.php b/includes/class-sp-event.php index dc7987c9..7cf756e4 100644 --- a/includes/class-sp-event.php +++ b/includes/class-sp-event.php @@ -23,11 +23,59 @@ class SP_Event extends SP_Custom_Post{ return $post_status; } + public function performance() { + $teams = (array)get_post_meta( $this->ID, 'sp_team', false ); + $performance = (array)get_post_meta( $this->ID, 'sp_players', true ); + $performance_labels = sp_get_var_labels( 'sp_performance' ); + $output = array(); + foreach( $teams as $i => $team_id ): + $players = sp_array_between( (array)get_post_meta( $this->ID, 'sp_player', false ), 0, $i ); + $data = sp_array_combine( $players, sp_array_value( $performance, $team_id, array() ) ); + + $totals = array(); + foreach( $performance_labels as $key => $label ): + $totals[ $key ] = 0; + endforeach; + + foreach( $data as $player_id => $player_performance ): + foreach( $performance_labels as $key => $label ): + if ( array_key_exists( $key, $totals ) ): + $totals[ $key ] += sp_array_value( $player_performance, $key, 0 ); + endif; + endforeach; + endforeach; + + foreach( $totals as $key => $value ): + $manual_total = sp_array_value( sp_array_value( $performance, 0, array() ), $key, null ); + if ( $manual_total != null ): + $totals[ $key ] = $manual_total; + endif; + endforeach; + + $lineup = array_filter( $data, array( $this, 'lineup_filter' ) ); + $subs = array_filter( $data, array( $this, 'sub_filter' ) ); + + foreach ( $subs as $sub_id => $sub ): + if ( ! $sub_id ) + continue; + $lineup[ sp_array_value( $sub, 'sub', 0 ) ]['sub'] = $sub_id; + endforeach; + + $output[ $team_id ] = array( + 'lineup' => $lineup, + 'subs' => $subs, + 'total' => $totals + ); + endforeach; + + return $output; + } + public function lineup_filter( $v ) { - return sp_array_value( $v, 'sub', false ) == false; + return sp_array_value( $v, 'status', 'lineup' ) == 'lineup'; } public function sub_filter( $v ) { - return sp_array_value( $v, 'sub', false ); + return sp_array_value( $v, 'status', 'lineup' ) == 'sub'; } }