Display same position for teams tied in table
This commit is contained in:
@@ -15,6 +15,12 @@ class SP_League_Table extends SP_Custom_Post{
|
|||||||
/** @var array The sort priorities array. */
|
/** @var array The sort priorities array. */
|
||||||
public $priorities;
|
public $priorities;
|
||||||
|
|
||||||
|
/** @var array Positions of teams in the table. */
|
||||||
|
public $pos;
|
||||||
|
|
||||||
|
/** @var array Inremental value for team position. */
|
||||||
|
public $counter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns formatted data
|
* Returns formatted data
|
||||||
*
|
*
|
||||||
@@ -43,6 +49,10 @@ class SP_League_Table extends SP_Custom_Post{
|
|||||||
$totals = array();
|
$totals = array();
|
||||||
$placeholders = array();
|
$placeholders = array();
|
||||||
|
|
||||||
|
// Initialize incremental counter
|
||||||
|
$this->pos = array( 1 );
|
||||||
|
$this->counter = 1;
|
||||||
|
|
||||||
// Initialize streaks counter
|
// Initialize streaks counter
|
||||||
$streaks = array();
|
$streaks = array();
|
||||||
|
|
||||||
@@ -314,10 +324,17 @@ class SP_League_Table extends SP_Custom_Post{
|
|||||||
endif;
|
endif;
|
||||||
|
|
||||||
endforeach;
|
endforeach;
|
||||||
|
|
||||||
endforeach;
|
endforeach;
|
||||||
|
|
||||||
uasort( $merged, array( $this, 'sort' ) );
|
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
|
// Rearrange data array to reflect values
|
||||||
$data = array();
|
$data = array();
|
||||||
foreach( $merged as $key => $value ):
|
foreach( $merged as $key => $value ):
|
||||||
@@ -329,7 +346,7 @@ class SP_League_Table extends SP_Custom_Post{
|
|||||||
else:
|
else:
|
||||||
if ( ! is_array( $usecolumns ) )
|
if ( ! is_array( $usecolumns ) )
|
||||||
$usecolumns = array();
|
$usecolumns = array();
|
||||||
$labels = array_merge( array( 'name' => __( 'Team', 'sportspress' ) ), $columns );
|
$labels = array_merge( array( 'pos' => __( 'Pos', 'sportspress' ), 'name' => __( 'Team', 'sportspress' ) ), $columns );
|
||||||
$merged[0] = $labels;
|
$merged[0] = $labels;
|
||||||
return $merged;
|
return $merged;
|
||||||
endif;
|
endif;
|
||||||
@@ -365,4 +382,34 @@ class SP_League_Table extends SP_Custom_Post{
|
|||||||
// Default sort by alphabetical
|
// Default sort by alphabetical
|
||||||
return strcmp( sp_array_value( $a, 'name', '' ), sp_array_value( $b, 'name', '' ) );
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -113,7 +113,7 @@ foreach ( $data as $team_id => $row ):
|
|||||||
$output .= '<tr class="' . ( $i % 2 == 0 ? 'odd' : 'even' ) . $class . '">';
|
$output .= '<tr class="' . ( $i % 2 == 0 ? 'odd' : 'even' ) . $class . '">';
|
||||||
|
|
||||||
// Rank
|
// Rank
|
||||||
$output .= '<td class="data-rank">' . $before . ( $start + 1 ) . $after . '</td>';
|
$output .= '<td class="data-rank">' . $before . sp_array_value( $row, 'pos' ) . $after . '</td>';
|
||||||
|
|
||||||
$name_class = '';
|
$name_class = '';
|
||||||
|
|
||||||
@@ -135,7 +135,7 @@ foreach ( $data as $team_id => $row ):
|
|||||||
$output .= '<td class="data-name' . $name_class . '">' . $name . '</td>';
|
$output .= '<td class="data-name' . $name_class . '">' . $name . '</td>';
|
||||||
|
|
||||||
foreach( $labels as $key => $value ):
|
foreach( $labels as $key => $value ):
|
||||||
if ( $key == 'name' )
|
if ( in_array( $key, array( 'pos', 'name' ) ) )
|
||||||
continue;
|
continue;
|
||||||
if ( ! is_array( $columns ) || in_array( $key, $columns ) )
|
if ( ! is_array( $columns ) || in_array( $key, $columns ) )
|
||||||
$output .= '<td class="data-' . $key . '">' . $before . sp_array_value( $row, $key, '—' ) . $after . '</td>';
|
$output .= '<td class="data-' . $key . '">' . $before . sp_array_value( $row, $key, '—' ) . $after . '</td>';
|
||||||
|
|||||||
Reference in New Issue
Block a user