Add single player metrics EOS table functionality
This commit is contained in:
@@ -75,7 +75,7 @@ function sp_event_teams_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_stats', true );
|
||||
$stats = (array)get_post_meta( $post->ID, 'sp_players', true );
|
||||
|
||||
// Get columns from result variables
|
||||
$columns = sp_get_var_labels( 'sp_metric', true );
|
||||
|
||||
@@ -35,98 +35,112 @@ function sp_player_team_meta( $post ) {
|
||||
}
|
||||
|
||||
function sp_player_stats_meta( $post ) {
|
||||
$teams = (array)get_post_meta( $post->ID, 'sp_team', false );
|
||||
$team_ids = (array)get_post_meta( $post->ID, 'sp_team', false );
|
||||
$divisions = (array)get_the_terms( $post->ID, 'sp_div' );
|
||||
$stats = (array)get_post_meta( $post->ID, 'sp_stats', true );
|
||||
$stats = (array)get_post_meta( $post->ID, 'sp_metrics', true );
|
||||
|
||||
// Get column names from settings
|
||||
$stats_settings = get_option( 'sportspress_stats' );
|
||||
$columns = sp_get_eos_keys( $stats_settings['player'] );
|
||||
// Equation Operating System
|
||||
$eos = new eqEOS();
|
||||
|
||||
// Add first column label
|
||||
array_unshift( $columns, __( 'Division', 'sportspress' ) );
|
||||
// Get labels from metric variables
|
||||
$metric_labels = (array)sp_get_var_labels( 'sp_metric' );
|
||||
|
||||
$totals = array( 'eventsattended' => 0, 'eventsplayed' => 0 );
|
||||
$placeholders = array();
|
||||
|
||||
foreach ( $metric_labels as $key => $value ):
|
||||
$totals[ $key ] = 0;
|
||||
endforeach;
|
||||
|
||||
// Generate array of all division ids
|
||||
$division_ids = array( 0 );
|
||||
$div_id = array();
|
||||
foreach ( $divisions as $key => $value ):
|
||||
if ( is_object( $value ) && property_exists( $value, 'term_id' ) )
|
||||
$division_ids[] = $value->term_id;
|
||||
$div_id[] = $value->term_id;
|
||||
endforeach;
|
||||
|
||||
// Get all teams populated with overall stats where availabled
|
||||
$data = sp_array_combine( $division_ids, sp_array_value( $stats, 0, array() ) );
|
||||
// Loop through statistics for each team
|
||||
foreach ( $team_ids as $team_id ):
|
||||
if ( !$team_id ) continue;
|
||||
|
||||
// Generate array of placeholder values for each division
|
||||
$placeholders = array();
|
||||
foreach ( $division_ids as $division_id ):
|
||||
$args = array(
|
||||
'post_type' => 'sp_event',
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'key' => 'sp_player',
|
||||
'value' => $post->ID
|
||||
)
|
||||
)
|
||||
);
|
||||
if ( $division_id ):
|
||||
$args['tax_query'] = array(
|
||||
array(
|
||||
'taxonomy' => 'sp_div',
|
||||
'field' => 'id',
|
||||
'terms' => $division_id
|
||||
)
|
||||
);
|
||||
endif;
|
||||
$placeholders[ $division_id ] = sp_get_stats_row( 'sp_player', $args );
|
||||
endforeach;
|
||||
?>
|
||||
<p><strong><?php _e( 'Overall', 'sportspress' ); ?></strong></p>
|
||||
<?php
|
||||
// Get all divisions populated with stats where available
|
||||
$data[ $team_id ] = sp_array_combine( $div_id, $stats[ $team_id ] );
|
||||
|
||||
sp_stats_table( $data, $placeholders, 0, $columns, true, 'sp_div' );
|
||||
// Get equations from statistics variables
|
||||
$equations = sp_get_var_equations( 'sp_metric' );
|
||||
|
||||
// Divisions
|
||||
foreach ( $teams as $team ):
|
||||
if ( !$team ) continue;
|
||||
|
||||
// Get all divisions populated with stats where availabled
|
||||
$data = sp_array_combine( $division_ids, sp_array_value( $stats, $team, array() ) );
|
||||
|
||||
// Generate array of placeholder values for each division
|
||||
$placeholders = array();
|
||||
foreach ( $division_ids as $division_id ):
|
||||
foreach ( $div_id as $div_id ):
|
||||
$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
|
||||
),
|
||||
array(
|
||||
'key' => 'sp_team',
|
||||
'value' => $team
|
||||
)
|
||||
),
|
||||
'tax_query' => array(
|
||||
array(
|
||||
'taxonomy' => 'sp_div',
|
||||
'field' => 'id',
|
||||
'terms' => $division_id
|
||||
'terms' => $div_id
|
||||
)
|
||||
)
|
||||
);
|
||||
$placeholders[ $division_id ] = sp_get_stats_row( 'sp_player', $args );
|
||||
$events = get_posts( $args );
|
||||
foreach( $events as $event ):
|
||||
$totals['eventsattended']++;
|
||||
$totals['eventsplayed']++; // TODO: create tab for substitutes in sidebar
|
||||
$team_metrics = (array)get_post_meta( $event->ID, 'sp_players', true );
|
||||
if ( array_key_exists( $team_id, $team_metrics ) ):
|
||||
$players = sp_array_value( $team_metrics, $team_id, array() );
|
||||
if ( array_key_exists( $post->ID, $players ) ):
|
||||
$player_metrics = sp_array_value( $players, $post->ID, array() );
|
||||
foreach ( $player_metrics as $key => $value ):
|
||||
if ( array_key_exists( $key, $totals ) ):
|
||||
$totals[ $key ] += $value;
|
||||
endif;
|
||||
endforeach;
|
||||
endif;
|
||||
endif;
|
||||
endforeach;
|
||||
|
||||
// Generate array of placeholder values for each division
|
||||
$placeholders[ $team_id ][ $div_id ] = array();
|
||||
foreach ( $equations as $key => $value ):
|
||||
|
||||
if ( empty( $value ) ):
|
||||
|
||||
// Reflect totals
|
||||
$placeholders[ $team_id ][ $div_id ][ $key ] = sp_array_value( $totals, $key, 0 );
|
||||
|
||||
else:
|
||||
|
||||
// Calculate value
|
||||
$placeholders[ $team_id ][ $div_id ][ $key ] = $eos->solveIF( str_replace( ' ', '', $value ), $totals );
|
||||
|
||||
endif;
|
||||
|
||||
endforeach;
|
||||
|
||||
endforeach;
|
||||
?>
|
||||
<p><strong><?php echo get_the_title( $team ); ?></strong></p>
|
||||
<?php
|
||||
|
||||
sp_stats_table( $data, $placeholders, $team, $columns, true, 'sp_div' );
|
||||
// Get columns from statistics variables
|
||||
$columns = sp_get_var_labels( 'sp_metric' );
|
||||
|
||||
?>
|
||||
<p><strong><?php echo get_the_title( $team_id ); ?></strong></p>
|
||||
<?php
|
||||
|
||||
sp_player_metrics_table( $columns, $data, $placeholders );
|
||||
|
||||
endforeach;
|
||||
|
||||
}
|
||||
|
||||
function sp_player_profile_meta( $post ) {
|
||||
|
||||
@@ -50,9 +50,6 @@ function sp_team_stats_meta( $post ) {
|
||||
// Get labels from outcome variables
|
||||
$outcome_labels = (array)sp_get_var_labels( 'sp_outcome' );
|
||||
|
||||
// Merge result and outcome variable labels
|
||||
$labels = array_merge( $result_labels, $outcome_labels );
|
||||
|
||||
$totals = array( 'eventsplayed' => 0 );
|
||||
$placeholders = array();
|
||||
|
||||
@@ -99,8 +96,8 @@ function sp_team_stats_meta( $post ) {
|
||||
);
|
||||
$events = get_posts( $args );
|
||||
foreach( $events as $event ):
|
||||
$results = (array)get_post_meta( $event->ID, 'sp_results', true );
|
||||
$totals['eventsplayed']++;
|
||||
$results = (array)get_post_meta( $event->ID, 'sp_results', true );
|
||||
foreach ( $results as $team_id => $team_result ):
|
||||
foreach ( $team_result as $key => $value ):
|
||||
if ( $team_id == $post->ID ):
|
||||
@@ -124,13 +121,15 @@ function sp_team_stats_meta( $post ) {
|
||||
endforeach;
|
||||
endforeach;
|
||||
|
||||
// Total events attended is same as played for teams
|
||||
$totals['eventsattended'] = $totals['eventsplayed'];
|
||||
|
||||
// Generate array of placeholder values for each division
|
||||
$placeholders[ $div_id ] = array();
|
||||
foreach ( $equations as $key => $value ):
|
||||
$placeholders[ $div_id ][ $key ] = $eos->solveIF( str_replace( ' ', '', $value ), $totals );
|
||||
endforeach;
|
||||
|
||||
//$placeholders[ $division_id ] = sp_get_stats_row( 'sp_team', $args );
|
||||
endforeach;
|
||||
|
||||
// Get columns from statistics variables
|
||||
|
||||
@@ -90,16 +90,16 @@
|
||||
#sp_profilediv .wp-editor-container {
|
||||
background-color:#fff;
|
||||
}
|
||||
.sp-stats-table td {
|
||||
.sp-data-table td {
|
||||
padding: 4px 7px;
|
||||
line-height: 2;
|
||||
}
|
||||
.sp-stats-table td:first-child {
|
||||
.sp-data-table td:first-child {
|
||||
white-space: nowrap;
|
||||
}
|
||||
.sp-stats-table input[type="text"],
|
||||
.sp-stats-table input[type="number"],
|
||||
.sp-stats-table select {
|
||||
.sp-data-table input[type="text"],
|
||||
.sp-data-table input[type="number"],
|
||||
.sp-data-table select {
|
||||
min-width: 14px;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
@@ -41,10 +41,10 @@ jQuery(document).ready(function($){
|
||||
});
|
||||
|
||||
// Total stats calculator
|
||||
$('.sp-stats-table .sp-total input').on('updateTotal', function() {
|
||||
$('.sp-data-table .sp-total input').on('updateTotal', function() {
|
||||
index = $(this).parent().index();
|
||||
var sum = 0;
|
||||
$(this).closest('.sp-stats-table').find('.sp-post').each(function() {
|
||||
$(this).closest('.sp-data-table').find('.sp-post').each(function() {
|
||||
val = $(this).find('td').eq(index).find('input').val();
|
||||
if(val == '') {
|
||||
val = $(this).find('td').eq(index).find('input').attr('placeholder');
|
||||
@@ -57,9 +57,9 @@ jQuery(document).ready(function($){
|
||||
});
|
||||
|
||||
// Activate total stats calculator
|
||||
if($('.sp-stats-table .sp-total').size()) {
|
||||
$('.sp-stats-table .sp-post td input').on('keyup', function() {
|
||||
$(this).closest('.sp-stats-table').find('.sp-total td').eq($(this).parent().index()).find('input').trigger('updateTotal');
|
||||
if($('.sp-data-table .sp-total').size()) {
|
||||
$('.sp-data-table .sp-post td input').on('keyup', function() {
|
||||
$(this).closest('.sp-data-table').find('.sp-total td').eq($(this).parent().index()).find('input').trigger('updateTotal');
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -125,7 +125,7 @@ function sp_save_post( $post_id ) {
|
||||
|
||||
// Update stats
|
||||
update_post_meta( $post_id, 'sp_stats', sp_array_value( $_POST, 'sp_stats', array() ) );
|
||||
|
||||
|
||||
break;
|
||||
|
||||
case ( 'sp_event' ):
|
||||
@@ -136,8 +136,8 @@ function sp_save_post( $post_id ) {
|
||||
// Update results
|
||||
update_post_meta( $post_id, 'sp_results', $results );
|
||||
|
||||
// Update stats
|
||||
update_post_meta( $post_id, 'sp_stats', sp_array_value( $_POST, 'sp_stats', array() ) );
|
||||
// Update player metrics
|
||||
update_post_meta( $post_id, 'sp_players', sp_array_value( $_POST, 'sp_players', array() ) );
|
||||
|
||||
// Update team array
|
||||
sp_update_post_meta_recursive( $post_id, 'sp_team', sp_array_value( $_POST, 'sp_team', array() ) );
|
||||
@@ -176,9 +176,15 @@ function sp_save_post( $post_id ) {
|
||||
break;
|
||||
|
||||
case ( 'sp_player' ):
|
||||
update_post_meta( $post_id, 'sp_stats', sp_array_value( $_POST, 'sp_stats', array() ) );
|
||||
|
||||
// Update player metrics
|
||||
update_post_meta( $post_id, 'sp_metrics', sp_array_value( $_POST, 'sp_metrics', array() ) );
|
||||
|
||||
// Update team array
|
||||
sp_update_post_meta_recursive( $post_id, 'sp_team', sp_array_value( $_POST, 'sp_team', array() ) );
|
||||
|
||||
break;
|
||||
|
||||
case ( 'sp_staff' ):
|
||||
sp_update_post_meta_recursive( $post_id, 'sp_team', sp_array_value( $_POST, 'sp_team', array() ) );
|
||||
break;
|
||||
|
||||
@@ -682,7 +682,7 @@ if ( !function_exists( 'sp_stats_table' ) ) {
|
||||
if ( !is_array( $stats ) )
|
||||
$stats = array( __( 'Name', 'sportspress' ) );
|
||||
?>
|
||||
<table class="widefat sp-stats-table">
|
||||
<table class="widefat sp-data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<?php foreach ( $columns as $column ): ?>
|
||||
@@ -744,7 +744,7 @@ if ( !function_exists( 'sp_stats_table' ) ) {
|
||||
if ( !function_exists( 'sp_team_stats_table' ) ) {
|
||||
function sp_team_stats_table( $columns = array(), $data = array(), $placeholders = array() ) {
|
||||
?>
|
||||
<table class="widefat sp-stats-table">
|
||||
<table class="widefat sp-data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th><?php _e( 'Division', 'sportspress' ); ?></th>
|
||||
@@ -781,10 +781,52 @@ if ( !function_exists( 'sp_team_stats_table' ) ) {
|
||||
}
|
||||
}
|
||||
|
||||
if ( !function_exists( 'sp_player_metrics_table' ) ) {
|
||||
function sp_player_metrics_table( $columns = array(), $data = array(), $placeholders = array() ) {
|
||||
?>
|
||||
<table class="widefat sp-data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th><?php _e( 'Division', 'sportspress' ); ?></th>
|
||||
<?php foreach ( $columns as $label ): ?>
|
||||
<th><?php echo $label; ?></th>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php
|
||||
$i = 0;
|
||||
foreach ( $data as $team_id => $team_stats ):
|
||||
foreach ( $team_stats as $div_id => $div_stats ):
|
||||
if ( !$div_id ) continue;
|
||||
$div = get_term( $div_id, 'sp_div' );
|
||||
?>
|
||||
<tr class="sp-row sp-post<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>">
|
||||
<td>
|
||||
<?php echo $div->name; ?>
|
||||
</td>
|
||||
<?php foreach( $columns as $column => $label ):
|
||||
$value = sp_array_value( $div_stats, $column, '' );
|
||||
$placeholder = sp_array_value( sp_array_value( sp_array_value( $placeholders, $team_id, array() ), $div_id, array() ), $column, 0 );
|
||||
?>
|
||||
<td><input type="text" name="sp_metrics[<?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;
|
||||
?>
|
||||
</tbody>
|
||||
</table>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
|
||||
if ( !function_exists( 'sp_event_results_table' ) ) {
|
||||
function sp_event_results_table( $columns = array(), $data = array() ) {
|
||||
?>
|
||||
<table class="widefat sp-stats-table">
|
||||
<table class="widefat sp-data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th><?php _e( 'Team', 'sportspress' ); ?></th>
|
||||
@@ -838,7 +880,7 @@ if ( !function_exists( 'sp_event_results_table' ) ) {
|
||||
if ( !function_exists( 'sp_event_players_table' ) ) {
|
||||
function sp_event_players_table( $columns = array(), $data = array(), $team_id ) {
|
||||
?>
|
||||
<table class="widefat sp-stats-table">
|
||||
<table class="widefat sp-data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th><?php _e( 'Player', 'sportspress' ); ?></th>
|
||||
@@ -860,7 +902,7 @@ if ( !function_exists( 'sp_event_players_table' ) ) {
|
||||
<?php foreach( $columns as $column => $label ):
|
||||
$value = sp_array_value( $player_metrics, $column, '' );
|
||||
?>
|
||||
<td><input type="text" name="sp_stats[<?php echo $team_id; ?>][<?php echo $player_id; ?>][<?php echo $column; ?>]" value="<?php echo $value; ?>" placeholder="0" /></td>
|
||||
<td><input type="text" name="sp_players[<?php echo $team_id; ?>][<?php echo $player_id; ?>][<?php echo $column; ?>]" value="<?php echo $value; ?>" placeholder="0" /></td>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
<?php
|
||||
@@ -874,7 +916,7 @@ if ( !function_exists( 'sp_event_players_table' ) ) {
|
||||
$player_metrics = $data[0];
|
||||
$value = sp_array_value( $player_metrics, $column, '' );
|
||||
?>
|
||||
<td><input type="text" name="sp_stats[<?php echo $team_id; ?>][<?php echo $player_id; ?>][<?php echo $column; ?>]" value="<?php echo $value; ?>" placeholder="0" /></td>
|
||||
<td><input type="text" name="sp_players[<?php echo $team_id; ?>][<?php echo $player_id; ?>][<?php echo $column; ?>]" value="<?php echo $value; ?>" placeholder="0" /></td>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
</tbody>
|
||||
@@ -886,7 +928,7 @@ if ( !function_exists( 'sp_event_players_table' ) ) {
|
||||
if ( !function_exists( 'sp_someother_table' ) ) {
|
||||
function sp_someother_table( $columns = array(), $data = array(), $placeholders = array(), $team_id ) {
|
||||
?>
|
||||
<table class="widefat sp-stats-table">
|
||||
<table class="widefat sp-data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th><?php _e( 'Player', 'sportspress' ); ?></th>
|
||||
|
||||
Reference in New Issue
Block a user