From 0811dabd28e38dfd3219da3362bc5df0dab137f6 Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Sun, 19 Oct 2014 16:46:43 +1100 Subject: [PATCH] Display same position for teams tied in table --- includes/class-sp-league-table.php | 49 +++++++++++++++++++++++++++++- templates/league-table.php | 4 +-- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/includes/class-sp-league-table.php b/includes/class-sp-league-table.php index 5ee90be6..92c7201a 100644 --- a/includes/class-sp-league-table.php +++ b/includes/class-sp-league-table.php @@ -15,6 +15,12 @@ class SP_League_Table extends SP_Custom_Post{ /** @var array The sort priorities array. */ public $priorities; + /** @var array Positions of teams in the table. */ + public $pos; + + /** @var array Inremental value for team position. */ + public $counter; + /** * Returns formatted data * @@ -43,6 +49,10 @@ class SP_League_Table extends SP_Custom_Post{ $totals = array(); $placeholders = array(); + // Initialize incremental counter + $this->pos = array( 1 ); + $this->counter = 1; + // Initialize streaks counter $streaks = array(); @@ -314,10 +324,17 @@ class SP_League_Table extends SP_Custom_Post{ endif; endforeach; + endforeach; uasort( $merged, array( $this, 'sort' ) ); + uasort( $merged, array( $this, 'calculate_pos' ) ); + + foreach ( $merged as $team_id => $team_columns ) { + $merged[ $team_id ]['pos'] = array_shift( $this->pos ); + } + // Rearrange data array to reflect values $data = array(); foreach( $merged as $key => $value ): @@ -329,7 +346,7 @@ class SP_League_Table extends SP_Custom_Post{ else: if ( ! is_array( $usecolumns ) ) $usecolumns = array(); - $labels = array_merge( array( 'name' => __( 'Team', 'sportspress' ) ), $columns ); + $labels = array_merge( array( 'pos' => __( 'Pos', 'sportspress' ), 'name' => __( 'Team', 'sportspress' ) ), $columns ); $merged[0] = $labels; return $merged; endif; @@ -365,4 +382,34 @@ class SP_League_Table extends SP_Custom_Post{ // Default sort by alphabetical return strcmp( sp_array_value( $a, 'name', '' ), sp_array_value( $b, 'name', '' ) ); } + + /** + * Find accurate position of teams. + * + * @param array $a + * @param array $b + * @return int + */ + public function calculate_pos( $a, $b ) { + $this->counter ++; + + // Loop through priorities + foreach( $this->priorities as $priority ): + + // Proceed if columns are not equal + if ( sp_array_value( $a, $priority['column'], 0 ) != sp_array_value( $b, $priority['column'], 0 ) ): + + // Increment if not equal + $this->pos[] = $this->counter; + + return 1; + + endif; + + endforeach; + + // Repeat position if equal + $this->pos[] = end( $this->pos ); + return 1; + } } diff --git a/templates/league-table.php b/templates/league-table.php index c39064fd..9cbf6279 100644 --- a/templates/league-table.php +++ b/templates/league-table.php @@ -113,7 +113,7 @@ foreach ( $data as $team_id => $row ): $output .= ''; // Rank - $output .= '' . $before . ( $start + 1 ) . $after . ''; + $output .= '' . $before . sp_array_value( $row, 'pos' ) . $after . ''; $name_class = ''; @@ -135,7 +135,7 @@ foreach ( $data as $team_id => $row ): $output .= '' . $name . ''; foreach( $labels as $key => $value ): - if ( $key == 'name' ) + if ( in_array( $key, array( 'pos', 'name' ) ) ) continue; if ( ! is_array( $columns ) || in_array( $key, $columns ) ) $output .= '' . $before . sp_array_value( $row, $key, '—' ) . $after . '';