Fix league table position miscalculation

This commit is contained in:
Brian Miyaji
2015-03-08 22:08:47 +11:00
parent 81fbe8aeee
commit ed7deaa0f7

View File

@@ -91,8 +91,11 @@ class SP_League_Table extends SP_Custom_Post{
$placeholders = array(); $placeholders = array();
// Initialize incremental counter // Initialize incremental counter
$this->pos = array( 1 ); $this->pos = 0;
$this->counter = 1; $this->counter = 0;
// Initialize team compare
$this->compare = null;
// Initialize streaks counter // Initialize streaks counter
$streaks = array(); $streaks = array();
@@ -145,6 +148,13 @@ class SP_League_Table extends SP_Custom_Post{
'posts_per_page' => -1, 'posts_per_page' => -1,
'orderby' => 'post_date', 'orderby' => 'post_date',
'order' => 'DESC', 'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'sp_format',
'value' => apply_filters( 'sportspress_competitive_event_formats', array( 'league' ) ),
'compare' => 'IN',
),
),
'tax_query' => array( 'tax_query' => array(
'relation' => 'AND', 'relation' => 'AND',
), ),
@@ -371,12 +381,9 @@ class SP_League_Table extends SP_Custom_Post{
uasort( $merged, array( $this, 'sort' ) ); uasort( $merged, array( $this, 'sort' ) );
// Create temp array and calculate position of teams for ties // Calculate position of teams for ties
$temp = $merged;
uasort( $temp, array( $this, 'calculate_pos' ) );
foreach ( $merged as $team_id => $team_columns ) { foreach ( $merged as $team_id => $team_columns ) {
$merged[ $team_id ]['pos'] = array_shift( $this->pos ); $merged[ $team_id ]['pos'] = $this->calculate_pos( $team_columns );
} }
// Rearrange data array to reflect values // Rearrange data array to reflect values
@@ -434,26 +441,28 @@ class SP_League_Table extends SP_Custom_Post{
* @param array $b * @param array $b
* @return int * @return int
*/ */
public function calculate_pos( $a, $b ) { public function calculate_pos( $columns ) {
$this->counter++; $this->counter++;
// Replace compare data and use last set
$compare = $this->compare;
$this->compare = $columns;
// Loop through priorities // Loop through priorities
foreach( $this->priorities as $priority ): foreach( $this->priorities as $priority ):
// Proceed if columns are not equal // Proceed if columns are not equal
if ( sp_array_value( $a, $priority['column'], 0 ) != sp_array_value( $b, $priority['column'], 0 ) ): if ( sp_array_value( $columns, $priority['column'], 0 ) !== sp_array_value( $compare, $priority['column'], 0 ) ):
// Increment if not equal // Increment if not equal
$this->pos[] = $this->counter; $this->pos = $this->counter;
return $this->counter;
return 0;
endif; endif;
endforeach; endforeach;
// Repeat position if equal // Repeat position if equal
$this->pos[] = end( $this->pos ); return $this->pos;
return 0;
} }
} }