Display player stats per team per league
This commit is contained in:
13
globals.php
13
globals.php
@@ -61,13 +61,22 @@ $sportspress_options = array(
|
|||||||
F: $for
|
F: $for
|
||||||
A: $against
|
A: $against
|
||||||
GD: $for-$against
|
GD: $for-$against
|
||||||
PTS: 3$wins+$ties'
|
PTS: 3$wins+$ties',
|
||||||
|
'sp_event_stats_columns' => 'Goals: $statsa
|
||||||
|
Assists: $statsb
|
||||||
|
Yellow Cards: $statsc
|
||||||
|
Red Cards: $statsd',
|
||||||
|
'sp_player_stats_columns' => 'Attendances: $played
|
||||||
|
Goals: $statsa
|
||||||
|
Assists: $statsb
|
||||||
|
Yellow Cards: $statsc
|
||||||
|
Red Cards: $statsd',
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
foreach( $sportspress_options as $optiongroupkey => $optiongroup ) {
|
foreach( $sportspress_options as $optiongroupkey => $optiongroup ) {
|
||||||
foreach( $optiongroup as $key => $value ) {
|
foreach( $optiongroup as $key => $value ) {
|
||||||
if ( get_option( $key ) === false )
|
//if ( get_option( $key ) === false )
|
||||||
update_option( $key, $value );
|
update_option( $key, $value );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
107
helpers.php
107
helpers.php
@@ -55,6 +55,15 @@ if ( !function_exists( 'sp_array_combine' ) ) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( !function_exists( 'sp_num_to_letter' ) ) {
|
||||||
|
function sp_num_to_letter( $num, $uppercase = false ) {
|
||||||
|
$num -= 0;
|
||||||
|
$letter = chr( ( $num % 26 ) + 97 );
|
||||||
|
$letter .= ( floor( $num / 26 ) > 0 ) ? str_repeat( $letter, floor( $num / 26 ) ) : '';
|
||||||
|
return ( $uppercase ? strtoupper( $letter ) : $letter );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ( !function_exists( 'sp_cpt_labels' ) ) {
|
if ( !function_exists( 'sp_cpt_labels' ) ) {
|
||||||
function sp_cpt_labels( $name, $singular_name ) {
|
function sp_cpt_labels( $name, $singular_name ) {
|
||||||
$labels = array(
|
$labels = array(
|
||||||
@@ -247,63 +256,77 @@ if ( !function_exists( 'sp_get_stats_row' ) ) {
|
|||||||
function sp_get_stats_row( $post_type = 'post', $args = array() ) {
|
function sp_get_stats_row( $post_type = 'post', $args = array() ) {
|
||||||
$args = array_merge(
|
$args = array_merge(
|
||||||
array(
|
array(
|
||||||
'meta_value' => 0,
|
|
||||||
'posts_per_page' => -1
|
'posts_per_page' => -1
|
||||||
),
|
),
|
||||||
(array)$args
|
(array)$args
|
||||||
);
|
);
|
||||||
$posts = (array)get_posts( $args );
|
$posts = (array)get_posts( $args );
|
||||||
|
|
||||||
|
// Equation Operating System
|
||||||
|
$eos = new eqEOS();
|
||||||
|
|
||||||
|
$vars = array();
|
||||||
|
switch ($post_type):
|
||||||
|
case 'sp_team':
|
||||||
|
|
||||||
|
// Add object properties needed for retreiving event stats
|
||||||
foreach( $posts as $post ):
|
foreach( $posts as $post ):
|
||||||
$post->sp_team = get_post_meta( $post->ID, 'sp_team', false );
|
$post->sp_team = get_post_meta( $post->ID, 'sp_team', false );
|
||||||
$post->sp_team_index = array_search( $args['meta_value'], $post->sp_team );
|
$post->sp_team_index = array_search( $args['meta_value'], $post->sp_team );
|
||||||
$post->sp_result = get_post_meta( $post->ID, 'sp_result', false );
|
$post->sp_result = get_post_meta( $post->ID, 'sp_result', false );
|
||||||
endforeach;
|
endforeach;
|
||||||
|
|
||||||
// Load Equation Operating System
|
// Get team stats from all attended events
|
||||||
$eos = new eqEOS();
|
|
||||||
|
|
||||||
// Define variables to use in EOS
|
|
||||||
$vars = array();
|
|
||||||
|
|
||||||
$vars['played'] = sizeof( $posts );
|
$vars['played'] = sizeof( $posts );
|
||||||
|
$vars['wins'] = sizeof( array_filter( $posts, function( $post ) { return array_count_values( $post->sp_result ) > 1 && max( $post->sp_result ) == $post->sp_result[ $post->sp_team_index ]; } ) );
|
||||||
|
$vars['ties'] = sizeof( array_filter( $posts, function( $post ) { return array_count_values( $post->sp_result ) == 1; } ) );
|
||||||
|
$vars['losses'] = sizeof( array_filter( $posts, function( $post ) { return array_count_values( $post->sp_result ) > 1 && min( $post->sp_result ) == $post->sp_result[ $post->sp_team_index ]; } ) );
|
||||||
|
$vars['for'] = 0; foreach( $posts as $post ): $vars['for'] += $post->sp_result[ $post->sp_team_index ]; endforeach;
|
||||||
|
$vars['against'] = 0; foreach( $posts as $post ): $result = $post->sp_result; unset( $result[ $post->sp_team_index ] ); $vars['against'] += array_sum( $result ); endforeach;
|
||||||
|
|
||||||
$vars['wins'] = sizeof( array_filter( $posts, function( $post ) {
|
// Get EOS array
|
||||||
return array_count_values( $post->sp_result ) > 1 && max( $post->sp_result ) == $post->sp_result[ $post->sp_team_index ];
|
|
||||||
} ) );
|
|
||||||
|
|
||||||
$vars['ties'] = sizeof( array_filter( $posts, function( $post ) {
|
|
||||||
return array_count_values( $post->sp_result ) == 1;
|
|
||||||
} ) );
|
|
||||||
|
|
||||||
$vars['losses'] = sizeof( array_filter( $posts, function( $post ) {
|
|
||||||
return array_count_values( $post->sp_result ) > 1 && min( $post->sp_result ) == $post->sp_result[ $post->sp_team_index ];
|
|
||||||
} ) );
|
|
||||||
|
|
||||||
$vars['for'] = 0;
|
|
||||||
foreach( $posts as $post ):
|
|
||||||
$vars['for'] += $post->sp_result[ $post->sp_team_index ];
|
|
||||||
endforeach;
|
|
||||||
|
|
||||||
$vars['against'] = 0;
|
|
||||||
foreach( $posts as $post ):
|
|
||||||
$result = $post->sp_result;
|
|
||||||
unset( $result[ $post->sp_team_index ] );
|
|
||||||
$vars['against'] += array_sum( $result );
|
|
||||||
endforeach;
|
|
||||||
|
|
||||||
switch ($post_type):
|
|
||||||
case 'sp_team':
|
|
||||||
$rows = sp_get_eos_array( get_option( 'sp_team_stats_columns' ) );
|
$rows = sp_get_eos_array( get_option( 'sp_team_stats_columns' ) );
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'sp_player':
|
||||||
|
|
||||||
|
// Create array of event stats columns
|
||||||
|
$columns = sp_get_eos_array( get_option( 'sp_event_stats_columns' ) );
|
||||||
|
foreach ( $columns as $key => $value ):
|
||||||
|
$vars[ 'stats' . sp_num_to_letter( $key ) ] = 0;
|
||||||
|
endforeach;
|
||||||
|
|
||||||
|
// Populate columns with player stats from events
|
||||||
|
foreach ( $posts as $post ):
|
||||||
|
$team_stats = get_post_meta( $post->ID, 'sp_stats', true );
|
||||||
|
foreach ( $team_stats as $team_id => $stat ):
|
||||||
|
if ( array_key_exists( 1, $args['meta_query'] ) && $team_id != sp_array_value( $args['meta_query'][1], 'value', 0 ) ) continue;
|
||||||
|
$player_id = sp_array_value( $args['meta_query'][0], 'value', 0 );
|
||||||
|
if ( !array_key_exists( $player_id, $stat ) ) continue;
|
||||||
|
foreach ( $stat[ $player_id ] as $key => $value ):
|
||||||
|
$vars[ 'stats' . sp_num_to_letter( $key ) ] += $value;
|
||||||
|
endforeach;
|
||||||
|
endforeach;
|
||||||
|
endforeach;
|
||||||
|
|
||||||
|
// Add played event count to vars
|
||||||
|
$vars['played'] = sizeof( $posts );
|
||||||
|
|
||||||
|
// Get EOS array
|
||||||
|
$rows = sp_get_eos_array( get_option( 'sp_player_stats_columns' ) );
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
||||||
$rows = array();
|
$rows = array();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
endswitch;
|
endswitch;
|
||||||
|
|
||||||
$output = array();
|
$output = array();
|
||||||
foreach ( $rows as $key => $value ):
|
foreach ( $rows as $key => $value ):
|
||||||
$row = explode( ':', $value );
|
$row = explode( ':', $value );
|
||||||
$output[] = $eos->solveIF( sp_array_value( $row, 1, 'played'), $vars );
|
$output[] = $eos->solveIF( sp_array_value( $row, 1, '$played'), $vars );
|
||||||
endforeach;
|
endforeach;
|
||||||
|
|
||||||
return $output;
|
return $output;
|
||||||
@@ -348,7 +371,7 @@ if ( !function_exists( 'sp_get_stats' ) ) {
|
|||||||
// Get fallback values
|
// Get fallback values
|
||||||
switch ( $post_type ):
|
switch ( $post_type ):
|
||||||
|
|
||||||
// Teams: all events attended in the league
|
// Team: all events attended in the league
|
||||||
case 'sp_team':
|
case 'sp_team':
|
||||||
$args = array(
|
$args = array(
|
||||||
'post_type' => 'sp_event',
|
'post_type' => 'sp_event',
|
||||||
@@ -360,6 +383,18 @@ if ( !function_exists( 'sp_get_stats' ) ) {
|
|||||||
$fallback = sp_get_stats_row( $args );
|
$fallback = sp_get_stats_row( $args );
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
// Player: all events attended in the league
|
||||||
|
case 'sp_player':
|
||||||
|
$args = array(
|
||||||
|
'post_type' => 'sp_event',
|
||||||
|
'meta_key' => 'sp_player',
|
||||||
|
'meta_value' => $post_id,
|
||||||
|
'taxonomy' => 'sp_league',
|
||||||
|
'terms' => $subset_id
|
||||||
|
);
|
||||||
|
$fallback = sp_get_stats_row( $args );
|
||||||
|
break;
|
||||||
|
|
||||||
endswitch;
|
endswitch;
|
||||||
|
|
||||||
// Merge fallback values with current subset
|
// Merge fallback values with current subset
|
||||||
@@ -404,7 +439,7 @@ if ( !function_exists( 'sp_stats_table' ) ) {
|
|||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
$term = get_term( $key, $rowtype );
|
$term = get_term( $key, $rowtype );
|
||||||
$title = $term->name;;
|
$title = $term->name;
|
||||||
break;
|
break;
|
||||||
endswitch;
|
endswitch;
|
||||||
if ( empty( $title ) )
|
if ( empty( $title ) )
|
||||||
|
|||||||
75
player.php
75
player.php
@@ -37,20 +37,79 @@ function sp_player_stats_meta( $post ) {
|
|||||||
$leagues = (array)get_the_terms( $post->ID, 'sp_league' );
|
$leagues = (array)get_the_terms( $post->ID, 'sp_league' );
|
||||||
$stats = (array)get_post_meta( $post->ID, 'sp_stats', true );
|
$stats = (array)get_post_meta( $post->ID, 'sp_stats', true );
|
||||||
|
|
||||||
// Overall
|
// Generate array of all league ids
|
||||||
$data = sp_array_combine( $teams, sp_array_value( $stats, 0, array() ) );
|
$league_ids = array( 0 );
|
||||||
|
foreach ( $leagues as $key => $value ):
|
||||||
|
if ( is_object( $value ) && property_exists( $value, 'term_id' ) )
|
||||||
|
$league_ids[] = $value->term_id;
|
||||||
|
endforeach;
|
||||||
|
|
||||||
|
// Get all teams populated with overall stats where availabled
|
||||||
|
$data = sp_array_combine( $league_ids, sp_array_value( $stats, 0, array() ) );
|
||||||
|
|
||||||
|
// Generate array of placeholder values for each league
|
||||||
|
$placeholders = array();
|
||||||
|
foreach ( $league_ids as $league_id ):
|
||||||
|
$args = array(
|
||||||
|
'post_type' => 'sp_event',
|
||||||
|
'meta_query' => array(
|
||||||
|
array(
|
||||||
|
'key' => 'sp_player',
|
||||||
|
'value' => $post->ID
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
if ( $league_id ):
|
||||||
|
$args['tax_query'] = array(
|
||||||
|
array(
|
||||||
|
'taxonomy' => 'sp_league',
|
||||||
|
'field' => 'id',
|
||||||
|
'terms' => $league_id
|
||||||
|
)
|
||||||
|
);
|
||||||
|
endif;
|
||||||
|
$placeholders[ $league_id ] = sp_get_stats_row( 'sp_player', $args );
|
||||||
|
endforeach;
|
||||||
?>
|
?>
|
||||||
<p><strong><?php _e( 'Overall', 'sportspress' ); ?></strong></p>
|
<p><strong><?php _e( 'Overall', 'sportspress' ); ?></strong></p>
|
||||||
<?php sp_stats_table( $data, array(), 0, array( 'Team', 'Played', 'Goals', 'Assists', 'Yellow Cards', 'Red Cards' ) ); ?>
|
<?php sp_stats_table( $data, $placeholders, 0, array( 'Team', 'Played', 'Goals', 'Assists', 'Yellow Cards', 'Red Cards' ), true, 'sp_league' ); ?>
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
// Leagues
|
// Leagues
|
||||||
foreach ( $leagues as $league ):
|
foreach ( $teams as $team ):
|
||||||
if ( !$league ) continue;
|
if ( !$team ) continue;
|
||||||
$data = sp_array_combine( $teams, sp_array_value( $stats, $league->term_id, array() ) );
|
|
||||||
|
// Get all leagues populated with stats where availabled
|
||||||
|
$data = sp_array_combine( $league_ids, sp_array_value( $stats, $team, array() ) );
|
||||||
|
|
||||||
|
// Generate array of placeholder values for each league
|
||||||
|
$placeholders = array();
|
||||||
|
foreach ( $league_ids as $league_id ):
|
||||||
|
$args = array(
|
||||||
|
'post_type' => 'sp_event',
|
||||||
|
'meta_query' => array(
|
||||||
|
array(
|
||||||
|
'key' => 'sp_player',
|
||||||
|
'value' => $post->ID
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'key' => 'sp_team',
|
||||||
|
'value' => $team
|
||||||
|
)
|
||||||
|
),
|
||||||
|
'tax_query' => array(
|
||||||
|
array(
|
||||||
|
'taxonomy' => 'sp_league',
|
||||||
|
'field' => 'id',
|
||||||
|
'terms' => $league_id
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$placeholders[ $league_id ] = sp_get_stats_row( 'sp_player', $args );
|
||||||
|
endforeach;
|
||||||
?>
|
?>
|
||||||
<p><strong><?php echo $league->name; ?></strong></p>
|
<p><strong><?php echo get_the_title( $team ); ?></strong></p>
|
||||||
<?php sp_stats_table( $data, array(), $league->term_id, array( 'Team', 'Played', 'Goals', 'Assists', 'Yellow Cards', 'Red Cards' ) ); ?>
|
<?php sp_stats_table( $data, $placeholders, $team, array( 'Team', 'Played', 'Goals', 'Assists', 'Yellow Cards', 'Red Cards' ), true, 'sp_league' ); ?>
|
||||||
<?php
|
<?php
|
||||||
endforeach;
|
endforeach;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user