Display statistics on player profile page

This commit is contained in:
Brian Miyaji
2014-01-14 00:58:18 +11:00
parent d8c1b13646
commit bd919ad121
6 changed files with 273 additions and 152 deletions

View File

@@ -535,8 +535,8 @@ if ( !function_exists( 'sportspress_edit_league_table' ) ) {
} }
} }
if ( !function_exists( 'sportspress_edit_player_table' ) ) { if ( !function_exists( 'sportspress_edit_player_list_table' ) ) {
function sportspress_edit_player_table( $columns = array(), $data = array(), $placeholders = array() ) { function sportspress_edit_player_list_table( $columns = array(), $data = array(), $placeholders = array() ) {
?> ?>
<div class="sp-data-table-container"> <div class="sp-data-table-container">
<table class="widefat sp-data-table"> <table class="widefat sp-data-table">
@@ -628,7 +628,7 @@ if ( !function_exists( 'sportspress_edit_team_columns_table' ) ) {
} }
if ( !function_exists( 'sportspress_edit_player_statistics_table' ) ) { if ( !function_exists( 'sportspress_edit_player_statistics_table' ) ) {
function sportspress_edit_player_statistics_table( $columns = array(), $data = array(), $placeholders = array() ) { function sportspress_edit_player_statistics_table( $team_id, $columns = array(), $data = array(), $placeholders = array() ) {
?> ?>
<div class="sp-data-table-container"> <div class="sp-data-table-container">
<table class="widefat sp-data-table"> <table class="widefat sp-data-table">
@@ -643,31 +643,23 @@ if ( !function_exists( 'sportspress_edit_player_statistics_table' ) ) {
<tbody> <tbody>
<?php <?php
$i = 0; $i = 0;
foreach ( $data as $team_id => $team_stats ): foreach ( $data as $div_id => $div_stats ):
if ( empty( $team_stats ) ): if ( !$div_id ) continue;
?> $div = get_term( $div_id, 'sp_season' );
<td><strong><?php printf( __( 'Select %s', 'sportspress' ), __( 'Team', 'sportspress' ) ); ?></strong></td> ?>
<?php <tr class="sp-row sp-post<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>">
continue; <td>
endif; <?php echo $div->name; ?>
foreach ( $team_stats as $div_id => $div_stats ): </td>
if ( !$div_id ) continue; <?php foreach( $columns as $column => $label ):
$div = get_term( $div_id, 'sp_season' ); $value = sportspress_array_value( $div_stats, $column, '' );
?> $placeholder = sportspress_array_value( sportspress_array_value( $placeholders, $div_id, array() ), $column, 0 );
<tr class="sp-row sp-post<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>"> ?>
<td> <td><input type="text" name="sp_statistics[<?php echo $team_id; ?>][<?php echo $div_id; ?>][<?php echo $column; ?>]" value="<?php echo $value; ?>" placeholder="<?php echo $placeholder; ?>" /></td>
<?php echo $div->name; ?> <?php endforeach; ?>
</td> </tr>
<?php foreach( $columns as $column => $label ): <?php
$value = sportspress_array_value( $div_stats, $column, '' ); $i++;
$placeholder = sportspress_array_value( sportspress_array_value( sportspress_array_value( $placeholders, $team_id, array() ), $div_id, array() ), $column, 0 );
?>
<td><input type="text" name="sp_statistics[<?php echo $team_id; ?>][<?php echo $div_id; ?>][<?php echo $column; ?>]" value="<?php echo $value; ?>" placeholder="<?php echo $placeholder; ?>" /></td>
<?php endforeach; ?>
</tr>
<?php
$i++;
endforeach;
endforeach; endforeach;
?> ?>
</tbody> </tbody>
@@ -1232,8 +1224,8 @@ if ( !function_exists( 'sportspress_get_league_table_data' ) ) {
if ( $breakdown ): if ( $breakdown ):
return array( $columns, $data, $placeholders, $merged ); return array( $columns, $data, $placeholders, $merged );
else: else:
array_unshift( $columns, __( 'Team', 'sportspress' ) ); $labels = array_merge( array( 'name' => __( 'Team', 'sportspress' ) ), $columns );
$merged[0] = $columns; $merged[0] = $labels;
return $merged; return $merged;
endif; endif;
} }
@@ -1440,13 +1432,148 @@ if ( !function_exists( 'sportspress_get_player_list_data' ) ) {
if ( $breakdown ): if ( $breakdown ):
return array( $columns, $data, $placeholders, $merged ); return array( $columns, $data, $placeholders, $merged );
else: else:
array_unshift( $columns, __( 'Player', 'sportspress' ) ); $labels = array_merge( array( 'name' => __( 'Player', 'sportspress' ) ), $columns );
$merged[0] = $columns; $merged[0] = $labels;
return $merged; return $merged;
endif; endif;
} }
} }
if ( !function_exists( 'sportspress_get_player_statistics_data' ) ) {
function sportspress_get_player_statistics_data( $post_id, $team_id, $breakdown = false ) {
$seasons = (array)get_the_terms( $post_id, 'sp_season' );
$stats = (array)get_post_meta( $post_id, 'sp_statistics', true );
// Get labels from statistic variables
$statistic_labels = (array)sportspress_get_var_labels( 'sp_statistic' );
// Generate array of all season ids and season names
$div_ids = array();
$season_names = array();
foreach ( $seasons as $season ):
if ( is_object( $season ) && property_exists( $season, 'term_id' ) && property_exists( $season, 'name' ) ):
$div_ids[] = $season->term_id;
$season_names[ $season->term_id ] = $season->name;
endif;
endforeach;
$tempdata = array();
// Get all seasons populated with stats where available
$tempdata = sportspress_array_combine( $div_ids, sportspress_array_value( $stats, $team_id, array() ) );
// Get equations from statistics variables
$equations = sportspress_get_var_equations( 'sp_statistic' );
foreach ( $div_ids as $div_id ):
$totals = array( 'eventsattended' => 0, 'eventsplayed' => 0 );
foreach ( $statistic_labels as $key => $value ):
$totals[ $key ] = 0;
endforeach;
$args = array(
'post_type' => 'sp_event',
'numberposts' => -1,
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'sp_team',
'value' => $team_id
),
array(
'key' => 'sp_player',
'value' => $post_id
)
),
'tax_query' => array(
array(
'taxonomy' => 'sp_season',
'field' => 'id',
'terms' => $div_id
)
)
);
$events = get_posts( $args );
foreach( $events as $event ):
$totals['eventsattended']++;
$totals['eventsplayed']++; // TODO: create tab for substitutes in sidebar
$team_statistics = (array)get_post_meta( $event->ID, 'sp_players', true );
if ( array_key_exists( $team_id, $team_statistics ) ):
$players = sportspress_array_value( $team_statistics, $team_id, array() );
if ( array_key_exists( $post_id, $players ) ):
$player_statistics = sportspress_array_value( $players, $post_id, array() );
foreach ( $player_statistics as $key => $value ):
if ( array_key_exists( $key, $totals ) ):
$totals[ $key ] += $value;
endif;
endforeach;
endif;
endif;
endforeach;
// Generate array of placeholder values for each league
$placeholders[ $div_id ] = array();
foreach ( $equations as $key => $value ):
if ( empty( $value ) ):
// Reflect totals
$placeholders[ $div_id ][ $key ] = sportspress_array_value( $totals, $key, 0 );
else:
// Calculate value
if ( sizeof( $events ) > 0 ):
$placeholders[ $div_id ][ $key ] = sportspress_solve( $value, $totals );
else:
$placeholders[ $div_id ][ $key ] = 0;
endif;
endif;
endforeach;
endforeach;
// Get columns from statistics variables
$columns = sportspress_get_var_labels( 'sp_statistic' );
// Merge the data and placeholders arrays
$merged = array();
foreach( $placeholders as $season_id => $season_data ):
// Add season name to row
$merged[ $season_id ] = array( 'name' => sportspress_array_value( $season_names, $season_id, '&nbsp;' ) );
foreach( $season_data as $key => $value ):
// Use static data if key exists and value is not empty, else use placeholder
if ( array_key_exists( $season_id, $tempdata ) && array_key_exists( $key, $tempdata[ $season_id ] ) && $tempdata[ $season_id ][ $key ] != '' ):
$merged[ $season_id ][ $key ] = $tempdata[ $season_id ][ $key ];
else:
$merged[ $season_id ][ $key ] = $value;
endif;
endforeach;
endforeach;
if ( $breakdown ):
return array( $columns, $tempdata, $placeholders, $merged );
else:
$labels = array_merge( array( 'name' => __( 'Season', 'sportspress' ) ), $columns );
$merged[0] = $labels;
return $merged;
endif;
}
}
if ( !function_exists( 'sportspress_highlight_admin_menu' ) ) { if ( !function_exists( 'sportspress_highlight_admin_menu' ) ) {
function sportspress_highlight_admin_menu() { function sportspress_highlight_admin_menu() {
global $parent_file, $submenu_file; global $parent_file, $submenu_file;

View File

@@ -7,14 +7,22 @@ function sportspress_the_content( $content ) {
global $post; global $post;
// Display league table // Display league table
$content .= sportspress_league_table( $post->ID ); $content = sportspress_league_table( $post->ID ) . $content;
elseif ( is_singular( 'sp_list' ) && in_the_loop() ): elseif ( is_singular( 'sp_list' ) && in_the_loop() ):
global $post; global $post;
// Display player list // Display player list
$content .= sportspress_player_list( $post->ID ); $content = sportspress_player_list( $post->ID ) . $content;
elseif ( is_singular( 'sp_player' ) && in_the_loop() ):
global $post;
// Display player list
$content = sportspress_player_statistics( $post->ID ) . $content;
endif; endif;

View File

@@ -85,6 +85,6 @@ function sportspress_list_stats_meta( $post ) {
list( $columns, $data, $placeholders, $merged ) = sportspress_get_player_list_data( $post->ID, true ); list( $columns, $data, $placeholders, $merged ) = sportspress_get_player_list_data( $post->ID, true );
sportspress_edit_player_table( $columns, $data, $placeholders ); sportspress_edit_player_list_table( $columns, $data, $placeholders );
sportspress_nonce(); sportspress_nonce();
} }

View File

@@ -35,13 +35,16 @@ function sportspress_player_meta_init( $post ) {
$teams = (array)get_post_meta( $post->ID, 'sp_team', false ); $teams = (array)get_post_meta( $post->ID, 'sp_team', false );
$seasons = (array)get_the_terms( $post->ID, 'sp_season' ); $seasons = (array)get_the_terms( $post->ID, 'sp_season' );
// First one is empty
unset( $teams[0] );
remove_meta_box( 'submitdiv', 'sp_player', 'side' ); remove_meta_box( 'submitdiv', 'sp_player', 'side' );
add_meta_box( 'submitdiv', __( 'Publish' ), 'post_submit_meta_box', 'sp_player', 'side', 'high' ); add_meta_box( 'submitdiv', __( 'Publish' ), 'post_submit_meta_box', 'sp_player', 'side', 'high' );
remove_meta_box( 'postimagediv', 'sp_player', 'side' ); remove_meta_box( 'postimagediv', 'sp_player', 'side' );
add_meta_box( 'postimagediv', __( 'Photo', 'sportspress' ), 'post_thumbnail_meta_box', 'sp_player', 'side', 'high' ); add_meta_box( 'postimagediv', __( 'Photo', 'sportspress' ), 'post_thumbnail_meta_box', 'sp_player', 'side', 'high' );
add_meta_box( 'sp_teamdiv', __( 'Teams', 'sportspress' ), 'sportspress_player_team_meta', 'sp_player', 'side', 'high' ); add_meta_box( 'sp_teamdiv', __( 'Teams', 'sportspress' ), 'sportspress_player_team_meta', 'sp_player', 'side', 'high' );
if ( $teams && $teams != array(0) && $seasons && is_array( $seasons ) && is_object( $seasons[0] ) ): if ( $teams && ! empty( $teams ) && $seasons && is_array( $seasons ) && is_object( $seasons[0] ) ):
add_meta_box( 'sp_statsdiv', __( 'Player Statistics', 'sportspress' ), 'sportspress_player_stats_meta', 'sp_player', 'normal', 'high' ); add_meta_box( 'sp_statsdiv', __( 'Player Statistics', 'sportspress' ), 'sportspress_player_stats_meta', 'sp_player', 'normal', 'high' );
endif; endif;
@@ -57,31 +60,10 @@ function sportspress_player_team_meta( $post ) {
function sportspress_player_stats_meta( $post ) { function sportspress_player_stats_meta( $post ) {
$team_ids = (array)get_post_meta( $post->ID, 'sp_team', false ); $team_ids = (array)get_post_meta( $post->ID, 'sp_team', false );
$seasons = (array)get_the_terms( $post->ID, 'sp_season' );
$stats = (array)get_post_meta( $post->ID, 'sp_statistics', true );
// Equation Operating System
$eos = new eqEOS();
// Get labels from statistic variables
$statistic_labels = (array)sportspress_get_var_labels( 'sp_statistic' );
// Generate array of all league ids
$div_ids = array();
foreach ( $seasons as $key => $value ):
if ( is_object( $value ) && property_exists( $value, 'term_id' ) )
$div_ids[] = $value->term_id;
endforeach;
// First one is empty
unset( $team_ids[0] ); unset( $team_ids[0] );
if ( empty( $team_ids ) ):
?>
<p><strong><?php printf( __( 'Select %s', 'sportspress' ), __( 'Teams', 'sportspress' ) ); ?></strong></p>
<?php
return;
endif;
// Initialize placeholders array // Initialize placeholders array
$placeholders = array(); $placeholders = array();
@@ -90,97 +72,15 @@ function sportspress_player_stats_meta( $post ) {
// Loop through statistics for each team // Loop through statistics for each team
foreach ( $team_ids as $team_id ): foreach ( $team_ids as $team_id ):
$data = array();
// Get all seasons populated with stats where available
$data[ $team_id ] = sportspress_array_combine( $div_ids, sportspress_array_value( $stats, $team_id, array() ) );
// Get equations from statistics variables
$equations = sportspress_get_var_equations( 'sp_statistic' );
foreach ( $div_ids as $div_id ):
$totals = array( 'eventsattended' => 0, 'eventsplayed' => 0 );
foreach ( $statistic_labels as $key => $value ):
$totals[ $key ] = 0;
endforeach;
$args = array(
'post_type' => 'sp_event',
'numberposts' => -1,
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'sp_team',
'value' => $team_id
),
array(
'key' => 'sp_player',
'value' => $post->ID
)
),
'tax_query' => array(
array(
'taxonomy' => 'sp_season',
'field' => 'id',
'terms' => $div_id
)
)
);
$events = get_posts( $args );
foreach( $events as $event ):
$totals['eventsattended']++;
$totals['eventsplayed']++; // TODO: create tab for substitutes in sidebar
$team_statistics = (array)get_post_meta( $event->ID, 'sp_players', true );
if ( array_key_exists( $team_id, $team_statistics ) ):
$players = sportspress_array_value( $team_statistics, $team_id, array() );
if ( array_key_exists( $post->ID, $players ) ):
$player_statistics = sportspress_array_value( $players, $post->ID, array() );
foreach ( $player_statistics as $key => $value ):
if ( array_key_exists( $key, $totals ) ):
$totals[ $key ] += $value;
endif;
endforeach;
endif;
endif;
endforeach;
// Generate array of placeholder values for each league
$placeholders[ $team_id ][ $div_id ] = array();
foreach ( $equations as $key => $value ):
if ( empty( $value ) ):
// Reflect totals
$placeholders[ $team_id ][ $div_id ][ $key ] = sportspress_array_value( $totals, $key, 0 );
else:
// Calculate value
if ( sizeof( $events ) > 0 ):
$placeholders[ $team_id ][ $div_id ][ $key ] = sportspress_solve( $value, $totals );
else:
$placeholders[ $team_id ][ $div_id ][ $key ] = 0;
endif;
endif;
endforeach;
endforeach;
// Get columns from statistics variables
$columns = sportspress_get_var_labels( 'sp_statistic' );
if ( $team_num > 1 ): if ( $team_num > 1 ):
?> ?>
<p><strong><?php echo get_the_title( $team_id ); ?></strong></p> <p><strong><?php echo get_the_title( $team_id ); ?></strong></p>
<?php <?php
endif; endif;
sportspress_edit_player_statistics_table( $columns, $data, $placeholders ); list( $columns, $data, $placeholders, $merged ) = sportspress_get_player_statistics_data( $post->ID, $team_id, true );
sportspress_edit_player_statistics_table( $team_id, $columns, $data, $placeholders );
endforeach; endforeach;
} }

View File

@@ -1,7 +1,7 @@
(function($) { (function($) {
// Data tables // League Table Sorting
$(".sp-data-table").dataTable({ $(".sp-league-table").dataTable({
"aaSorting": [], "aaSorting": [],
"bAutoWidth": false, "bAutoWidth": false,
"bFilter": false, "bFilter": false,
@@ -15,8 +15,43 @@
} }
}, },
"aoColumnDefs": [ "aoColumnDefs": [
{ "asSorting": [ "asc" ], "aTargets": [ 0 ] } { "asSorting": [ "asc" ], "aTargets": [ 0 ] },
] ]
}); });
// Player List Sorting
$(".sp-player-list").dataTable({
"aaSorting": [],
"bAutoWidth": false,
"bFilter": false,
"bInfo": false,
"bPaginate": false,
"bSort": true,
"oLanguage": {
"oAria": {
"sSortAscending": "",
"sSortDescending": ""
}
},
"aoColumnDefs": [
{ "asSorting": [ "asc" ], "aTargets": [ 0 ] },
]
});
// Player Statistics Sorting
$(".sp-player-statistics").dataTable({
"aaSorting": [],
"bAutoWidth": false,
"bFilter": false,
"bInfo": false,
"bPaginate": false,
"bSort": true,
"oLanguage": {
"oAria": {
"sSortAscending": "",
"sSortDescending": ""
}
}
});
})(jQuery); })(jQuery);

View File

@@ -14,10 +14,10 @@ if ( !function_exists( 'sportspress_league_table' ) ) {
$output .= '<th class="column-number">' . __( '#', 'sportspress' ) . '</th>'; $output .= '<th class="column-number">' . __( '#', 'sportspress' ) . '</th>';
foreach( $labels as $key => $label ): foreach( $labels as $key => $label ):
$output .= '<th class="column-' . ( $key ? $key : 'name' ) . '">' . $label . '</th>'; $output .= '<th class="column-' . $key . '">' . $label . '</th>';
endforeach; endforeach;
$output .= '</tr>' . '</th>' . '</thead>' . '<tbody>'; $output .= '</tr>' . '</thead>' . '<tbody>';
$i = 1; $i = 1;
@@ -68,10 +68,10 @@ if ( !function_exists( 'sportspress_player_list' ) ) {
$output .= '<th class="column-number">' . __( '#', 'sportspress' ) . '</th>'; $output .= '<th class="column-number">' . __( '#', 'sportspress' ) . '</th>';
foreach( $labels as $key => $label ): foreach( $labels as $key => $label ):
$output .= '<th class="column-' . ( $key ? $key : 'name' ) . '">' . $label . '</th>'; $output .= '<th class="column-' . $key . '">' . $label . '</th>';
endforeach; endforeach;
$output .= '</tr>' . '</th>' . '</thead>' . '<tbody>'; $output .= '</tr>' . '</thead>' . '<tbody>';
$i = 1; $i = 1;
@@ -104,3 +104,54 @@ if ( !function_exists( 'sportspress_player_list' ) ) {
} }
} }
if ( !function_exists( 'sportspress_player_statistics' ) ) {
function sportspress_player_statistics( $id ) {
$team_ids = (array)get_post_meta( $id, 'sp_team', false );
// First one is empty
unset( $team_ids[0] );
// Loop through statistics for each team
foreach ( $team_ids as $team_id ):
$data = sportspress_get_player_statistics_data( $id, $team_id );
// The first row should be column labels
$labels = $data[0];
// Remove the first row to leave us with the actual data
unset( $data[0] );
$output = '<table class="sp-player-statistics sp-data-table">' . '<thead>' . '<tr>';
foreach( $labels as $key => $label ):
$output .= '<th class="column-' . $key . '">' . $label . '</th>';
endforeach;
$output .= '</tr>' . '</thead>' . '<tbody>';
$i = 1;
foreach( $data as $season_id => $row ):
$output .= '<tr>';
foreach( $labels as $key => $value ):
$output .= '<td class="column-' . $key . '">' . sportspress_array_value( $row, $key, '—' ) . '</td>';
endforeach;
$output .= '</tr>';
endforeach;
$output .= '</tbody>' . '</table>';
endforeach;
return $output;
}
}