Player list basic EOS functionality

This commit is contained in:
ThemeBoy
2013-12-02 05:04:29 +11:00
parent 21cf90be9c
commit 59f0d80277
5 changed files with 219 additions and 123 deletions

View File

@@ -75,45 +75,67 @@ function sp_event_team_meta( $post ) {
function sp_event_players_meta( $post ) {
$limit = get_option( 'sp_event_team_count' );
$teams = array_pad( array_slice( (array)get_post_meta( $post->ID, 'sp_team', false ), 0, $limit ), $limit, 0 );
$stats = (array)get_post_meta( $post->ID, 'sp_players', true );
// Get columns from result variables
$columns = sp_get_var_labels( 'sp_metric', true );
// Teams
foreach ( $teams as $key => $team_id ):
// Get results for players in the team
$players = sp_array_between( (array)get_post_meta( $post->ID, 'sp_player', false ), 0, $key );
$data = sp_array_combine( $players, sp_array_value( $stats, $team_id, array() ) );
if ( $teams == array_pad( array_slice( array(), 0, $limit ), $limit, 0 ) ):
?>
<div>
<p><strong><?php echo $team_id ? get_the_title( $team_id ) : sprintf( __( 'Select %s' ), 'Team' ); ?></strong></p>
<?php sp_event_players_table( $columns, $data, $team_id ); ?>
</div>
<p><strong><?php echo $team_id ? get_the_title( $team_id ) : sprintf( __( 'Select %s' ), 'Teams' ); ?></strong></p>
<?php
endforeach;
else:
$stats = (array)get_post_meta( $post->ID, 'sp_players', true );
// Get columns from result variables
$columns = sp_get_var_labels( 'sp_metric', true );
foreach ( $teams as $key => $team_id ):
// Get results for players in the team
$players = sp_array_between( (array)get_post_meta( $post->ID, 'sp_player', false ), 0, $key );
$data = sp_array_combine( $players, sp_array_value( $stats, $team_id, array() ) );
?>
<div>
<p><strong><?php echo $team_id ? get_the_title( $team_id ) : sprintf( __( 'Select %s' ), 'Team' ); ?></strong></p>
<?php if ( $team_id ) sp_event_players_table( $columns, $data, $team_id ); ?>
</div>
<?php
endforeach;
endif;
}
function sp_event_results_meta( $post ) {
$limit = get_option( 'sp_event_team_count' );
$teams = array_pad( array_slice( (array)get_post_meta( $post->ID, 'sp_team', false ), 0, $limit ), $limit, 0 );
$results = (array)get_post_meta( $post->ID, 'sp_results', true );
// Teams
if ( $teams == array_pad( array_slice( array(), 0, $limit ), $limit, 0 ) ):
// Get columns from result variables
$columns = sp_get_var_labels( 'sp_result' );
?>
<p><strong><?php echo $team_id ? get_the_title( $team_id ) : sprintf( __( 'Select %s' ), 'Teams' ); ?></strong></p>
<?php
// Get results for all teams
$data = sp_array_combine( $teams, $results );
else:
?>
<div>
<?php sp_event_results_table( $columns, $data ); ?>
</div>
<?php
$results = (array)get_post_meta( $post->ID, 'sp_results', true );
// Get columns from result variables
$columns = sp_get_var_labels( 'sp_result' );
// Get results for all teams
$data = sp_array_combine( $teams, $results );
?>
<div>
<?php sp_event_results_table( $columns, $data ); ?>
</div>
<?php
endif;
}
function sp_event_article_meta( $post ) {

View File

@@ -73,41 +73,104 @@ function sp_list_player_meta( $post ) {
}
function sp_list_stats_meta( $post ) {
$players = (array)get_post_meta( $post->ID, 'sp_player', false );
$stats = (array)get_post_meta( $post->ID, 'sp_stats', true );
$division_id = sp_get_the_term_id( $post->ID, 'sp_div', 0 );
$div_id = sp_get_the_term_id( $post->ID, 'sp_div', 0 );
$team_id = get_post_meta( $post->ID, 'sp_team', true );
$data = sp_array_combine( $players, sp_array_value( $stats, $division_id, array() ) );
$player_ids = (array)get_post_meta( $post->ID, 'sp_player', false );
$stats = (array)get_post_meta( $post->ID, 'sp_players', true );
// Generate array of placeholder values for each player
$placeholders = array();
foreach ( $players as $player ):
$args = array(
'post_type' => 'sp_event',
'meta_query' => array(
array(
'key' => 'sp_player',
'value' => $player
)
),
'tax_query' => array(
array(
'taxonomy' => 'sp_div',
'field' => 'id',
'terms' => $division_id
)
)
);
$placeholders[ $player ] = sp_get_stats_row( 'sp_player', $args, true );
// Equation Operating System
$eos = new eqEOS();
// Get labels from result variables
$metric_labels = (array)sp_get_var_labels( 'sp_metric' );
// Get all divisions populated with stats where available
$data = sp_array_combine( $player_ids, $stats );
// Get equations from statistics variables
$equations = sp_get_var_equations( 'sp_metric' );
// Create entry for each player in totals
$totals = array();
foreach ( $player_ids as $player_id ):
if ( ! $player_id )
continue;
$totals[ $player_id ] = array( 'eventsattended' => 0, 'eventsplayed' => 0 );
foreach ( $metric_labels as $key => $value ):
$totals[ $player_id ][ $key ] = 0;
endforeach;
endforeach;
// Get column names from settings
$stats_settings = get_option( 'sportspress_stats' );
$columns = sp_get_eos_keys( $stats_settings['player'] );
$args = array(
'post_type' => 'sp_event',
'numberposts' => -1,
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'sp_div',
'field' => 'id',
'terms' => $div_id
)
),
'meta_query' => array(
array(
'key' => 'sp_team',
'value' => $team_id,
)
)
);
$events = get_posts( $args );
// Add first column label
array_unshift( $columns, __( 'Player', 'sportspress' ) );
// Event loop
foreach( $events as $event ):
sp_stats_table( $data, $placeholders, $division_id, $columns, false );
$teams = (array)get_post_meta( $event->ID, 'sp_players', true );
if ( ! array_key_exists( $team_id, $teams ) )
continue;
$players = sp_array_value( $teams, $team_id, array() );
foreach ( $players as $player_id => $player_metrics ):
// Increment events played
$totals[ $player_id ]['eventsplayed']++;
foreach ( $player_metrics as $key => $value ):
if ( array_key_exists( $key, $totals[ $player_id ] ) ):
$totals[ $player_id ][ $key ] += $value;
endif;
endforeach;
endforeach;
endforeach;
// Generate placeholder values for each team
$placeholders = array();
foreach ( $player_ids as $player_id ):
$placeholders[ $player_id ] = array();
foreach ( $equations as $key => $value ):
if ( empty( $value ) ):
// Reflect totals
$placeholders[ $player_id ][ $key ] = sp_array_value( sp_array_value( $totals, $player_id, array() ), $key, 0 );
else:
// Calculate value
$placeholders[ $player_id ][ $key ] = $eos->solveIF( str_replace( ' ', '', $value ), sp_array_value( $totals, $player_id, array() ) );
endif;
endforeach;
endforeach;
sp_player_table( $metric_labels, $data, $placeholders );
sp_nonce();
}
?>

View File

@@ -59,6 +59,13 @@ function sp_player_stats_meta( $post ) {
$div_ids[] = $value->term_id;
endforeach;
if ( empty( $team_ids ) || $team_ids == array(0) ):
?>
<p><strong><?php printf( __( 'Select %s', 'sportspress' ), __( 'Teams', 'sportspress' ) ); ?></strong></p>
<?php
return;
endif;
// Loop through statistics for each team
foreach ( $team_ids as $team_id ):
if ( !$team_id ) continue;