From ac83d14fa3ec54dbaca26dcfa79327e0bdc79048 Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Sat, 11 Apr 2015 14:39:45 +1000 Subject: [PATCH] Add home and away record to equation builder --- .../meta-boxes/class-sp-meta-box-equation.php | 2 + includes/class-sp-league-table.php | 39 ++++++++++++++++++- includes/class-sp-team.php | 28 +++++++++++-- includes/sp-core-functions.php | 12 ++++++ 4 files changed, 76 insertions(+), 5 deletions(-) diff --git a/includes/admin/post-types/meta-boxes/class-sp-meta-box-equation.php b/includes/admin/post-types/meta-boxes/class-sp-meta-box-equation.php index edff0290..b3726f22 100644 --- a/includes/admin/post-types/meta-boxes/class-sp-meta-box-equation.php +++ b/includes/admin/post-types/meta-boxes/class-sp-meta-box-equation.php @@ -42,6 +42,8 @@ class SP_Meta_Box_Equation { case 'outcome': $options[ 'Outcomes' ] = self::optgroup( 'sp_outcome' ); $options[ 'Outcomes' ]['$gamesback'] = __( 'Games Back', 'sportspress' ); + $options[ 'Outcomes' ]['$homerecord'] = __( 'Home Record', 'sportspress' ); + $options[ 'Outcomes' ]['$awayrecord'] = __( 'Away Record', 'sportspress' ); $options[ 'Outcomes' ]['$streak'] = __( 'Streak', 'sportspress' ); $options[ 'Outcomes' ]['$last5'] = __( 'Last 5', 'sportspress' ); $options[ 'Outcomes' ]['$last10'] = __( 'Last 10', 'sportspress' ); diff --git a/includes/class-sp-league-table.php b/includes/class-sp-league-table.php index 30d6ae19..75981a65 100644 --- a/includes/class-sp-league-table.php +++ b/includes/class-sp-league-table.php @@ -104,6 +104,10 @@ class SP_League_Table extends SP_Custom_Post{ $last5s = array(); $last10s = array(); + // Initialize record counters + $homerecords = array(); + $awayrecords = array(); + foreach ( $team_ids as $team_id ): if ( ! $team_id ) continue; @@ -115,10 +119,16 @@ class SP_League_Table extends SP_Custom_Post{ $last5s[ $team_id ] = array(); $last10s[ $team_id ] = array(); - // Add outcome types to team last counters + // Initialize team record counters + $homerecords[ $team_id ] = array(); + $awayrecords[ $team_id ] = array(); + + // Add outcome types to team last and record counters foreach( $outcome_labels as $key => $value ): $last5s[ $team_id ][ $key ] = 0; $last10s[ $team_id ][ $key ] = 0; + $homerecords[ $team_id ][ $key ] = 0; + $awayrecords[ $team_id ][ $key ] = 0; endforeach; // Initialize team totals @@ -187,6 +197,8 @@ class SP_League_Table extends SP_Custom_Post{ $minutes = get_post_meta( $event->ID, 'sp_minutes', true ); if ( $minutes === '' ) $minutes = get_option( 'sportspress_event_minutes', 90 ); + $i = 0; + foreach ( $results as $team_id => $team_result ): if ( ! in_array( $team_id, $team_ids ) ) @@ -229,6 +241,17 @@ class SP_League_Table extends SP_Custom_Post{ $last10s[ $team_id ][ $outcome ] ++; endif; + // Add to home or away record + if ( 0 === $i ) { + if ( array_key_exists( $team_id, $homerecords ) && array_key_exists( $outcome, $homerecords[ $team_id ] ) ) { + $homerecords[ $team_id ][ $outcome ] ++; + } + } else { + if ( array_key_exists( $team_id, $awayrecords ) && array_key_exists( $outcome, $awayrecords[ $team_id ] ) ) { + $awayrecords[ $team_id ][ $outcome ] ++; + } + } + endif; endforeach; @@ -246,6 +269,8 @@ class SP_League_Table extends SP_Custom_Post{ endforeach; endif; + $i++; + endforeach; endforeach; @@ -287,6 +312,16 @@ class SP_League_Table extends SP_Custom_Post{ $totals[ $team_id ]['last10'] = $last10; endforeach; + foreach ( $homerecords as $team_id => $homerecord ): + // Add home record to totals + $totals[ $team_id ]['homerecord'] = $homerecord; + endforeach; + + foreach ( $awayrecords as $team_id => $awayrecord ): + // Add away record to totals + $totals[ $team_id ]['awayrecord'] = $awayrecord; + endforeach; + $args = array( 'post_type' => 'sp_column', 'numberposts' => -1, @@ -348,7 +383,7 @@ class SP_League_Table extends SP_Custom_Post{ if ( '$gamesback' == $stat->equation ) $gb_column = $stat->post_name; - if ( ! in_array( $stat->equation, array( '$gamesback', '$streak', '$last5', '$last10' ) ) ): + if ( ! in_array( $stat->equation, array( '$gamesback', '$streak', '$last5', '$last10', '$homerecord', '$awayrecord' ) ) ): // Adjustments $adjustment = sp_array_value( $adjustments, $team_id, array() ); diff --git a/includes/class-sp-team.php b/includes/class-sp-team.php index 03257a50..b4093d22 100644 --- a/includes/class-sp-team.php +++ b/includes/class-sp-team.php @@ -77,7 +77,7 @@ class SP_Team extends SP_Custom_Post { foreach ( $div_ids as $div_id ): - $totals = array( 'eventsplayed' => 0, 'eventminutes' => 0, 'streak' => 0, 'last5' => null, 'last10' => null ); + $totals = array( 'eventsplayed' => 0, 'eventminutes' => 0, 'streak' => 0, 'last5' => null, 'last10' => null, 'homerecord' => null, 'awayrecord' => null ); foreach ( $result_labels as $key => $value ): $totals[ $key . 'for' ] = 0; @@ -95,10 +95,16 @@ class SP_Team extends SP_Custom_Post { $last5 = array(); $last10 = array(); - // Add outcome types to last counters + // Initialize record counters + $homerecord = array(); + $awayrecord = array(); + + // Add outcome types to last and record counters foreach( $outcome_labels as $key => $value ): $last5[ $key ] = 0; $last10[ $key ] = 0; + $homerecord[ $key ] = 0; + $awayrecord[ $key ] = 0; endforeach; // Get all events involving the team in current season @@ -150,6 +156,8 @@ class SP_Team extends SP_Custom_Post { $minutes = get_post_meta( $event->ID, 'sp_minutes', true ); if ( $minutes === '' ) $minutes = get_option( 'sportspress_event_minutes', 90 ); + $i = 0; + foreach ( $results as $team_id => $team_result ): if ( is_array( $team_result ) ): foreach ( $team_result as $key => $value ): if ( $team_id == $this->ID ): @@ -189,6 +197,17 @@ class SP_Team extends SP_Custom_Post { $last10[ $outcome ] ++; endif; + // Add to home or away record + if ( 0 === $i ) { + if ( array_key_exists( $outcome, $homerecord ) ) { + $homerecord[ $outcome ] ++; + } + } else { + if ( array_key_exists( $outcome, $awayrecord ) ) { + $awayrecord[ $outcome ] ++; + } + } + endif; endforeach; @@ -206,6 +225,7 @@ class SP_Team extends SP_Custom_Post { endif; endif; endforeach; endif; + $i++; endforeach; endforeach; @@ -228,9 +248,11 @@ class SP_Team extends SP_Custom_Post { $totals['streak'] = $abbreviation . $streak['count']; endif; - // Add last counters to totals + // Add last and record counters to totals $totals['last5'] = $last5; $totals['last10'] = $last10; + $totals['homerecord'] = $homerecord; + $totals['awayrecord'] = $awayrecord; // Generate array of placeholder values for each league $placeholders[ $div_id ] = array(); diff --git a/includes/sp-core-functions.php b/includes/sp-core-functions.php index 16e7c5a5..a295f1ad 100644 --- a/includes/sp-core-functions.php +++ b/includes/sp-core-functions.php @@ -1059,6 +1059,18 @@ if ( !function_exists( 'sp_solve' ) ) { return '-'; endif; + elseif ( strpos( $equation, '$homerecord' ) !== false ): + + // Return imploded string + $homerecord = sp_array_value( $vars, 'homerecord', array( 0 ) ); + return implode( '-', $homerecord ); + + elseif ( strpos( $equation, '$awayrecord' ) !== false ): + + // Return imploded string + $awayrecord = sp_array_value( $vars, 'awayrecord', array( 0 ) ); + return implode( '-', $awayrecord ); + endif; // Remove unnecessary variables from vars before calculating