Add function to format event performance data
This commit is contained in:
@@ -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';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user