Let us reference event vars in team stats

This commit is contained in:
Takumi
2013-08-23 00:53:31 +10:00
parent 90f59d5648
commit 9fee1468c3
5 changed files with 99 additions and 13 deletions

View File

@@ -105,16 +105,33 @@ function sp_save_post( $post_id ) {
update_post_meta( $post_id, 'sp_stats', sp_array_value( $_POST, 'sp_stats', array() ) );
break;
case ( 'sp_event' ):
update_post_meta( $post_id, 'sp_stats', sp_array_value( $_POST, 'sp_stats', array() ) );
// Get results
$results = (array)sp_array_value( $_POST, 'sp_results', array() );
// Update results
update_post_meta( $post_id, 'sp_results', $results );
// Delete result values
delete_post_meta( $post_id, 'sp_result');
$stats = (array)sp_array_value( $_POST, 'sp_stats', array() );
foreach ( $stats as $table ) {
$total = sp_array_value( sp_array_value( $table, 0, null ), 0, null );
add_post_meta( $post_id, 'sp_result', $total );
}
// Add result values for each team (first column of results table)
$teams = (array)sp_array_value( $results, 0, null );
foreach ( $teams as $team ):
add_post_meta( $post_id, 'sp_result', sp_array_value( $team, 0, null ) );
endforeach;
// Update stats
update_post_meta( $post_id, 'sp_stats', sp_array_value( $_POST, 'sp_stats', array() ) );
// Update team array
sp_update_post_meta_recursive( $post_id, 'sp_team', sp_array_value( $_POST, 'sp_team', array() ) );
// Update player array
sp_update_post_meta_recursive( $post_id, 'sp_player', sp_array_value( $_POST, 'sp_player', array() ) );
break;
case ( 'sp_player' ):
update_post_meta( $post_id, 'sp_stats', sp_array_value( $_POST, 'sp_stats', array() ) );
sp_update_post_meta_recursive( $post_id, 'sp_team', sp_array_value( $_POST, 'sp_team', array() ) );

View File

@@ -31,6 +31,7 @@ function sp_event_meta_init() {
add_meta_box( 'submitdiv', __( 'Event', 'sportspress' ), 'post_submit_meta_box', 'sp_event', 'side', 'high' );
add_meta_box( 'sp_teamdiv', __( 'Teams', 'sportspress' ), 'sp_event_team_meta', 'sp_event', 'side', 'high' );
add_meta_box( 'sp_articlediv', __( 'Article', 'sportspress' ), 'sp_event_article_meta', 'sp_event', 'normal', 'high' );
add_meta_box( 'sp_resultsdiv', __( 'Results', 'sportspress' ), 'sp_event_results_meta', 'sp_event', 'normal', 'high' );
add_meta_box( 'sp_statsdiv', __( 'Statistics', 'sportspress' ), 'sp_event_stats_meta', 'sp_event', 'normal', 'high' );
}
@@ -73,21 +74,54 @@ function sp_event_article_meta( $post ) {
wp_editor( $post->post_content, 'content' );
}
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 );
// Get results for all teams
$data = sp_array_combine( $teams, sp_array_value( $results, 0, array() ) );
// Get column names from settings
$stats_settings = get_option( 'sportspress_stats' );
$columns = sp_get_eos_keys( $stats_settings['event'] );
// Add first column label
array_unshift( $columns, __( 'Team', 'sportspress' ) );
?>
<div>
<?php sp_stats_table( $data, array(), 0, $columns, false, 'post', 'sp_results' ); ?>
</div>
<?php
}
function sp_event_stats_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_stats', true );
// Get column names from settings
$stats_settings = get_option( 'sportspress_stats' );
$columns = sp_get_eos_keys( $stats_settings['player'] );
// Add first column label
array_unshift( $columns, __( 'Player', 'sportspress' ) );
// Teams
foreach ( $teams as $key => $value ):
// 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, $value, array() ) );
?>
<div>
<p><strong><?php echo $value ? get_the_title( $value ) : sprintf( __( 'Select %s' ), 'Team' ); ?></strong></p>
<?php sp_stats_table( $data, array(), $value, array( 'Player', 'Goals', 'Assists', 'Yellow Cards', 'Red Cards' ), true ); ?>
<?php sp_stats_table( $data, array(), $value, $columns, true ); ?>
</div>
<?php
endforeach;
}

View File

@@ -287,18 +287,35 @@ if ( !function_exists( 'sp_get_stats_row' ) ) {
switch ($post_type):
case 'sp_team':
// Get stats settings columns
$columns = sp_get_eos_array( $stats_settings['event'] );
// Setup variables
$results = array();
foreach ( $columns as $key => $value ):
$column = explode( ':', $value );
$var_name = preg_replace("/[^A-Za-z0-9 ]/", '', sp_array_value( $column, 1 ) );
$results[] = $var_name;
$vars[ $var_name ] = 0;
endforeach;
// Add object properties needed for retreiving event stats
foreach( $posts as $post ):
$post->sp_team = get_post_meta( $post->ID, 'sp_team', false );
$post->sp_team_index = array_search( $args['meta_query'][0]['value'], $post->sp_team );
$post->sp_result = get_post_meta( $post->ID, 'sp_result', false );
$post->sp_results = sp_array_value( sp_array_value( sp_array_value( get_post_meta( $post->ID, 'sp_results', false ), 0, array() ), 0, array() ), $args['meta_query'][0]['value'], array() );
foreach( $results as $key => $value ):
if ( !array_key_exists( $value, $vars ) ) $vars[ $value ] = 0;
$vars[ $value ] += sp_array_value( $post->sp_results, $key, 0 );
endforeach;
endforeach;
// Get team stats from all attended events
$vars['appearances'] = 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['greater'] = 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['equal'] = sizeof( array_filter( $posts, function( $post ) { return array_count_values( $post->sp_result ) == 1; } ) );
$vars['less'] = 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;
@@ -308,6 +325,15 @@ if ( !function_exists( 'sp_get_stats_row' ) ) {
case 'sp_player':
// Get stats settings keys
$keys = sp_get_eos_keys( $stats_settings['player'] );
// Add object properties needed for retreiving event stats
foreach( $posts as $post ):
$post->sp_player = get_post_meta( $post->ID, 'sp_team', false );
$post->sp_player_index = array_search( $args['meta_query'][0]['value'], $post->sp_player );
endforeach;
// Create array of event stats columns
$columns = sp_get_eos_array( get_option( 'sp_event_stats_columns' ) );
foreach ( $columns as $key => $value ):

View File

@@ -98,7 +98,9 @@
white-space: nowrap;
}
#sp_statsdiv .widefat input[type="text"],
#sp_statsdiv .widefat input[type="number"] {
#sp_statsdiv .widefat input[type="number"],
#sp_resultsdiv .widefat input[type="text"],
#sp_resultsdiv .widefat input[type="number"] {
min-width: 14px;
width: 100%;
}

View File

@@ -85,6 +85,13 @@ function sp_table_stats_meta( $post ) {
$placeholders[ $team ] = sp_get_stats_row( 'sp_team', $args, true );
endforeach;
sp_stats_table( $data, $placeholders, $league_id, array( 'Team', 'P', 'W', 'D', 'L', 'F', 'A', 'GD', 'Pts' ), false );
// Get column names from settings
$stats_settings = get_option( 'sportspress_stats' );
$columns = sp_get_eos_keys( $stats_settings['team'] );
// Add first column label
array_unshift( $columns, __( 'Team', 'sportspress' ) );
sp_stats_table( $data, $placeholders, $league_id, $columns, false );
}
?>