From e57a67bbb3fd4ba1139ea6a24712505868396e70 Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Thu, 5 Mar 2015 19:08:21 +1100 Subject: [PATCH] Add outcome and winner methods to event class --- includes/class-sp-event.php | 72 +++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/includes/class-sp-event.php b/includes/class-sp-event.php index 2a9cbb75..df8cc6f9 100644 --- a/includes/class-sp-event.php +++ b/includes/class-sp-event.php @@ -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'; }