Add outcome and winner methods to event class

This commit is contained in:
Brian Miyaji
2015-03-05 19:08:21 +11:00
parent 97a0b1e7e8
commit e57a67bbb3

View File

@@ -164,6 +164,78 @@ class SP_Event extends SP_Custom_Post{
return $output;
}
public function outcome( $single = true ) {
// Get teams from event
$teams = get_post_meta( $this->ID, 'sp_team', false );
// Initialize output
$output = array();
// Return empty array if there are no teams
if ( ! $teams ) return $output;
// Get results from event
$results = get_post_meta( $this->ID, 'sp_results', true );
// Loop through teams
foreach ( $teams as $team_id ) {
// Skip if not a team
if ( ! $team_id ) continue;
// Get team results from all results
$team_results = sp_array_value( $results, $team_id, null );
// Get outcome from team results
$team_outcome = sp_array_value( $team_results, 'outcome', null );
if ( null != $team_outcome ) {
// Make sure that we have an array of outcomes
$team_outcome = (array) $team_outcome;
// Use only first outcome if single
if ( $single ) {
$team_outcome = reset( $team_outcome );
}
// Add outcome to output
$output[ $team_id ] = $team_outcome;
}
}
return $output;
}
public function winner() {
// Get the first configured outcome
$outcome = get_posts( array(
'post_type' => 'sp_outcome',
'post_status' => 'publish',
'posts_per_page' => 1,
'orderby' => 'menu_order',
'order' => 'ASC',
) );
// Return if no outcomes available
if ( ! $outcome ) return null;
$outcome = reset( $outcome );
// Get event outcomes
$outcomes = self::outcome( false );
// Look for a team that meets the criteria
foreach ( $outcomes as $team_id => $team_outcomes ) {
if ( in_array( $outcome->post_name, $team_outcomes ) ) {
return $team_id;
}
}
// Return if no teams meet criteria
return null;
}
public function lineup_filter( $v ) {
return sp_array_value( $v, 'status', 'lineup' ) == 'lineup';
}