Add basic EOS league table functionality
This commit is contained in:
@@ -31,13 +31,13 @@ add_filter( 'the_posts', 'sp_event_display_scheduled' );
|
||||
function sp_event_meta_init() {
|
||||
remove_meta_box( 'submitdiv', 'sp_event', 'side' );
|
||||
add_meta_box( 'submitdiv', __( 'Event', 'sportspress' ), 'post_submit_meta_box', 'sp_event', 'side', 'high' );
|
||||
add_meta_box( 'sp_teamsdiv', __( 'Teams', 'sportspress' ), 'sp_event_teams_meta', 'sp_event', 'side', 'high' );
|
||||
add_meta_box( 'sp_teamdiv', __( 'Teams', 'sportspress' ), 'sp_event_team_meta', 'sp_event', 'side', 'high' );
|
||||
add_meta_box( 'sp_playersdiv', __( 'Players', 'sportspress' ), 'sp_event_players_meta', 'sp_event', 'normal', 'high' );
|
||||
add_meta_box( 'sp_resultsdiv', __( 'Results', 'sportspress' ), 'sp_event_results_meta', 'sp_event', 'normal', 'high' );
|
||||
add_meta_box( 'sp_articlediv', __( 'Article', 'sportspress' ), 'sp_event_article_meta', 'sp_event', 'normal', 'high' );
|
||||
}
|
||||
|
||||
function sp_event_teams_meta( $post ) {
|
||||
function sp_event_team_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 );
|
||||
$players = (array)get_post_meta( $post->ID, 'sp_player', false );
|
||||
|
||||
@@ -40,7 +40,7 @@ function sp_metric_equation_meta( $post ) {
|
||||
<p class="sp-equation-selector">
|
||||
<?php
|
||||
foreach ( $equation as $piece ):
|
||||
sp_get_equation_selector( $post->ID, $piece, array( 'event' ) );
|
||||
sp_get_equation_selector( $post->ID, $piece, array( 'player_event' ) );
|
||||
endforeach;
|
||||
?>
|
||||
</p>
|
||||
|
||||
@@ -53,10 +53,10 @@ function sp_player_stats_meta( $post ) {
|
||||
endforeach;
|
||||
|
||||
// Generate array of all division ids
|
||||
$div_id = array();
|
||||
$div_ids = array();
|
||||
foreach ( $divisions as $key => $value ):
|
||||
if ( is_object( $value ) && property_exists( $value, 'term_id' ) )
|
||||
$div_id[] = $value->term_id;
|
||||
$div_ids[] = $value->term_id;
|
||||
endforeach;
|
||||
|
||||
// Loop through statistics for each team
|
||||
@@ -64,12 +64,12 @@ function sp_player_stats_meta( $post ) {
|
||||
if ( !$team_id ) continue;
|
||||
|
||||
// Get all divisions populated with stats where available
|
||||
$data[ $team_id ] = sp_array_combine( $div_id, $stats[ $team_id ] );
|
||||
$data[ $team_id ] = sp_array_combine( $div_ids, $stats[ $team_id ] );
|
||||
|
||||
// Get equations from statistics variables
|
||||
$equations = sp_get_var_equations( 'sp_metric' );
|
||||
|
||||
foreach ( $div_id as $div_id ):
|
||||
foreach ( $div_ids as $div_id ):
|
||||
$args = array(
|
||||
'post_type' => 'sp_event',
|
||||
'numberposts' => -1,
|
||||
|
||||
@@ -41,7 +41,7 @@ function sp_stat_equation_meta( $post ) {
|
||||
<p class="sp-equation-selector">
|
||||
<?php
|
||||
foreach ( $equation as $piece ):
|
||||
sp_get_equation_selector( $post->ID, $piece, array( 'event', 'result', 'outcome' ) );
|
||||
sp_get_equation_selector( $post->ID, $piece, array( 'team_event', 'result', 'outcome' ) );
|
||||
endforeach;
|
||||
?>
|
||||
</p>
|
||||
|
||||
@@ -59,73 +59,95 @@ function sp_table_team_meta( $post ) {
|
||||
}
|
||||
|
||||
function sp_table_stats_meta( $post ) {
|
||||
$teams = (array)get_post_meta( $post->ID, 'sp_team', false );
|
||||
$stats = (array)get_post_meta( $post->ID, 'sp_stats', true );
|
||||
$division_id = sp_get_the_term_id( $post->ID, 'sp_div', 0 );
|
||||
$data = sp_array_combine( $teams, sp_array_value( $stats, $division_id, array() ) );
|
||||
$div_id = sp_get_the_term_id( $post->ID, 'sp_div', 0 );
|
||||
$team_ids = (array)get_post_meta( $post->ID, 'sp_team', false );
|
||||
$stats = (array)get_post_meta( $post->ID, 'sp_teams', true );
|
||||
|
||||
// Generate array of placeholder values for each team
|
||||
$placeholders = array();
|
||||
foreach ( $teams as $team ):
|
||||
$args = array(
|
||||
'post_type' => 'sp_event',
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'key' => 'sp_team',
|
||||
'value' => $team
|
||||
)
|
||||
),
|
||||
'tax_query' => array(
|
||||
array(
|
||||
'taxonomy' => 'sp_div',
|
||||
'field' => 'id',
|
||||
'terms' => $division_id
|
||||
)
|
||||
)
|
||||
);
|
||||
$placeholders[ $team ] = sp_get_stats_row( $team, 'sp_team', $args, true );
|
||||
// Equation Operating System
|
||||
$eos = new eqEOS();
|
||||
|
||||
// Get labels from result variables
|
||||
$result_labels = (array)sp_get_var_labels( 'sp_result' );
|
||||
|
||||
// Get labels from outcome variables
|
||||
$outcome_labels = (array)sp_get_var_labels( 'sp_outcome' );
|
||||
|
||||
// Get all divisions populated with stats where available
|
||||
$data = sp_array_combine( $team_ids, $stats );
|
||||
|
||||
// Get equations from statistics variables
|
||||
$equations = sp_get_var_equations( 'sp_stat' );
|
||||
|
||||
// Create entry for each team in totals
|
||||
$totals = array();
|
||||
foreach ( $team_ids as $team_id ):
|
||||
$totals[ $team_id ] = array( 'eventsplayed' => 0 );
|
||||
|
||||
foreach ( $result_labels as $key => $value ):
|
||||
$totals[ $team_id ][ $key . 'for' ] = 0;
|
||||
$totals[ $team_id ][ $key . 'against' ] = 0;
|
||||
endforeach;
|
||||
|
||||
foreach ( $outcome_labels as $key => $value ):
|
||||
$totals[ $team_id ][ $key ] = 0;
|
||||
endforeach;
|
||||
endforeach;
|
||||
|
||||
$type = 'sp_stat';
|
||||
$args = array(
|
||||
'post_type' => $type,
|
||||
'post_type' => 'sp_event',
|
||||
'numberposts' => -1,
|
||||
'posts_per_page' => -1,
|
||||
'orderby' => 'menu_order',
|
||||
'order' => 'ASC',
|
||||
'exclude' => $postid
|
||||
'tax_query' => array(
|
||||
array(
|
||||
'taxonomy' => 'sp_div',
|
||||
'field' => 'id',
|
||||
'terms' => $div_id
|
||||
)
|
||||
)
|
||||
);
|
||||
$vars = get_posts( $args );
|
||||
$events = get_posts( $args );
|
||||
|
||||
// Event loop
|
||||
foreach( $events as $event ):
|
||||
|
||||
$results = (array)get_post_meta( $event->ID, 'sp_results', true );
|
||||
|
||||
foreach ( $results as $team_id => $team_result ):
|
||||
|
||||
// Increment events played
|
||||
$totals[ $team_id ]['eventsplayed']++;
|
||||
|
||||
foreach ( $team_result as $key => $value ):
|
||||
|
||||
if ( $key == 'outcome' ):
|
||||
if ( array_key_exists( $value, $totals[ $team_id ] ) ):
|
||||
$totals[ $team_id ][ $value ]++;
|
||||
endif;
|
||||
else:
|
||||
if ( array_key_exists( $key . 'for', $totals[ $team_id ] ) ):
|
||||
$totals[ $team_id ][ $key . 'for' ] += $value;
|
||||
endif;
|
||||
endif;
|
||||
|
||||
endforeach;
|
||||
|
||||
endforeach;
|
||||
|
||||
$columns = array();
|
||||
foreach ( $vars as $var ):
|
||||
$columns[ $var->post_name ] = $var->post_title;
|
||||
endforeach;
|
||||
|
||||
// Add first column label
|
||||
array_unshift( $columns, __( 'Team', 'sportspress' ) );
|
||||
// Generate placeholder values for each team
|
||||
$placeholders = array();
|
||||
foreach ( $team_ids as $team_id ):
|
||||
$placeholders[ $team_id ] = array();
|
||||
foreach ( $equations as $key => $value ):
|
||||
$placeholders[ $team_id ][ $key ] = $eos->solveIF( str_replace( ' ', '', $value ), $totals[ $team_id ] );
|
||||
endforeach;
|
||||
endforeach;
|
||||
|
||||
// Get columns from statistics variables
|
||||
$columns = sp_get_var_labels( 'sp_stat' );
|
||||
|
||||
echo '$placeholders';
|
||||
echo '<pre>';
|
||||
print_r( $placeholders );
|
||||
echo '</pre>';
|
||||
|
||||
echo '$data';
|
||||
echo '<pre>';
|
||||
print_r( $data );
|
||||
echo '</pre>';
|
||||
|
||||
echo '$division_id';
|
||||
echo '<pre>';
|
||||
print_r( $division_id );
|
||||
echo '</pre>';
|
||||
|
||||
echo '$columns';
|
||||
echo '<pre>';
|
||||
print_r( $columns );
|
||||
echo '</pre>';
|
||||
|
||||
sp_stats_table( $data, $placeholders, $division_id, $columns, false );
|
||||
sp_league_table( $columns, $data, $placeholders );
|
||||
sp_nonce();
|
||||
}
|
||||
?>
|
||||
@@ -63,19 +63,19 @@ function sp_team_stats_meta( $post ) {
|
||||
endforeach;
|
||||
|
||||
// Generate array of all division ids
|
||||
$div_id = array();
|
||||
$div_ids = array();
|
||||
foreach ( $divisions as $key => $value ):
|
||||
if ( is_object( $value ) && property_exists( $value, 'term_id' ) )
|
||||
$div_id[] = $value->term_id;
|
||||
$div_ids[] = $value->term_id;
|
||||
endforeach;
|
||||
|
||||
// Get all divisions populated with stats where available
|
||||
$data = sp_array_combine( $div_id, $stats );
|
||||
$data = sp_array_combine( $div_ids, $stats );
|
||||
|
||||
// Get equations from statistics variables
|
||||
$equations = sp_get_var_equations( 'sp_stat' );
|
||||
|
||||
foreach ( $div_id as $div_id ):
|
||||
foreach ( $div_ids as $div_id ):
|
||||
$args = array(
|
||||
'post_type' => 'sp_event',
|
||||
'numberposts' => -1,
|
||||
@@ -103,7 +103,7 @@ function sp_team_stats_meta( $post ) {
|
||||
if ( $team_id == $post->ID ):
|
||||
if ( $key == 'outcome' ):
|
||||
if ( array_key_exists( $value, $totals ) ):
|
||||
$totals[ $value]++;
|
||||
$totals[ $value ]++;
|
||||
endif;
|
||||
else:
|
||||
if ( array_key_exists( $key . 'for', $totals ) ):
|
||||
@@ -121,9 +121,6 @@ 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 ):
|
||||
|
||||
@@ -359,9 +359,12 @@ if ( !function_exists( 'sp_get_equation_selector' ) ) {
|
||||
// Add groups to options
|
||||
foreach ( $groups as $group ):
|
||||
switch ( $group ):
|
||||
case 'event':
|
||||
case 'player_event':
|
||||
$options[ __( 'Events', 'sportspress' ) ] = array( '$eventsattended' => __( 'Attended', 'sportspress' ), '$eventsplayed' => __( 'Played', 'sportspress' ) );
|
||||
break;
|
||||
case 'team_event':
|
||||
$options[ __( 'Events', 'sportspress' ) ] = array( '$eventsplayed' => __( 'Played', 'sportspress' ) );
|
||||
break;
|
||||
case 'result':
|
||||
$options[ __( 'Results', 'sportspress' ) ] = sp_get_equation_optgroup_array( $postid, 'sp_result', array( 'for' => '→', 'against' => '←' ), null, false );
|
||||
break;
|
||||
@@ -741,6 +744,46 @@ if ( !function_exists( 'sp_stats_table' ) ) {
|
||||
}
|
||||
}
|
||||
|
||||
if ( !function_exists( 'sp_league_table' ) ) {
|
||||
function sp_league_table( $columns = array(), $data = array(), $placeholders = array() ) {
|
||||
?>
|
||||
<table class="widefat sp-data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th><?php _e( 'Team', '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 ):
|
||||
if ( !$team_id ) continue;
|
||||
$div = get_term( $team_id, 'sp_div' );
|
||||
?>
|
||||
<tr class="sp-row sp-post<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>">
|
||||
<td>
|
||||
<?php echo get_the_title( $team_id ); ?>
|
||||
</td>
|
||||
<?php foreach( $columns as $column => $label ):
|
||||
$value = sp_array_value( $team_stats, $column, '' );
|
||||
$placeholder = sp_array_value( sp_array_value( $placeholders, $team_id, array() ), $column, 0 );
|
||||
?>
|
||||
<td><input type="text" name="sp_teams[<?php echo $team_id; ?>][<?php echo $column; ?>]" value="<?php echo $value; ?>" placeholder="<?php echo $placeholder; ?>" /></td>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
<?php
|
||||
$i++;
|
||||
endforeach;
|
||||
?>
|
||||
</tbody>
|
||||
</table>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
|
||||
if ( !function_exists( 'sp_team_stats_table' ) ) {
|
||||
function sp_team_stats_table( $columns = array(), $data = array(), $placeholders = array() ) {
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user