Add statistic order and priority sorting to league table

This commit is contained in:
ThemeBoy
2013-12-03 01:59:27 +11:00
parent 9fa6495ebe
commit 21e4719db1
6 changed files with 130 additions and 325 deletions

View File

@@ -25,27 +25,48 @@ function sp_stat_edit_columns() {
'cb' => '<input type="checkbox" />',
'title' => __( 'Label', 'sportspress' ),
'sp_equation' => __( 'Equation', 'sportspress' ),
'sp_order' => __( 'Sort Order', 'sportspress' )
);
return $columns;
}
add_filter( 'manage_edit-sp_stat_columns', 'sp_stat_edit_columns' );
function sp_stat_meta_init() {
add_meta_box( 'sp_equationdiv', __( 'Equation', 'sportspress' ), 'sp_stat_equation_meta', 'sp_stat', 'normal', 'high' );
add_meta_box( 'sp_detailsdiv', __( 'Details', 'sportspress' ), 'sp_stat_details_meta', 'sp_stat', 'normal', 'high' );
}
function sp_stat_equation_meta( $post ) {
function sp_stat_details_meta( $post ) {
$equation = explode( ' ', get_post_meta( $post->ID, 'sp_equation', true ) );
$order = get_post_meta( $post->ID, 'sp_order', true );
$priority = get_post_meta( $post->ID, 'sp_priority', true );
?>
<div>
<p class="sp-equation-selector">
<p><strong><?php _e( 'Equation', 'sportspress' ); ?></strong></p>
<p class="sp-equation-selector">
<?php
foreach ( $equation as $piece ):
sp_get_equation_selector( $post->ID, $piece, array( 'team_event', 'result', 'outcome' ) );
endforeach;
?>
</p>
<p><strong><?php _e( 'Sort Order', 'sportspress' ); ?></strong></p>
<p class="sp-order-selector">
<select name="sp_priority">
<?php
foreach ( $equation as $piece ):
sp_get_equation_selector( $post->ID, $piece, array( 'team_event', 'result', 'outcome' ) );
$options = array( '0' => __( 'Disable', 'sportspress' ), '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9', '10' => '10', );
foreach ( $options as $key => $value ):
printf( '<option value="%s" %s>%s</option>', $key, selected( true, $key == $priority, false ), $value );
endforeach;
?>
</p>
</div>
</select>
<select name="sp_order"<?php if ( ! $priority ): ?> disabled="disabled;"<?php endif; ?>>
<?php
$options = array( 'DESC' => __( 'Descending', 'sportspress' ), 'ASC' => __( 'Ascending', 'sportspress' ) );
foreach ( $options as $key => $value ):
printf( '<option value="%s" %s>%s</option>', $key, selected( true, $key == $order, false ), $value );
endforeach;
?>
</select>
</p>
<?php
sp_nonce();
}

View File

@@ -61,7 +61,7 @@ function sp_table_team_meta( $post ) {
function sp_table_stats_meta( $post ) {
$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 );
$table_stats = (array)get_post_meta( $post->ID, 'sp_teams', true );
// Equation Operating System
$eos = new eqEOS();
@@ -73,10 +73,7 @@ function sp_table_stats_meta( $post ) {
$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' );
$tempdata = sp_array_combine( $team_ids, $table_stats );
// Create entry for each team in totals
$totals = array();
@@ -152,20 +149,84 @@ function sp_table_stats_meta( $post ) {
endforeach;
$args = array(
'post_type' => 'sp_stat',
'numberposts' => -1,
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC'
);
$stats = get_posts( $args );
$columns = array();
$priorities = array();
foreach ( $stats as $stat ):
// Get post meta
$meta = get_post_meta( $stat->ID );
// Add equation to object
$stat->equation = sp_array_value( sp_array_value( $meta, 'sp_equation', array() ), 0, 0 );
// Add column name to columns
$columns[ $stat->post_name ] = $stat->post_title;
// Add order to priorities if priority is set and does not exist in array already
$priority = sp_array_value( sp_array_value( $meta, 'sp_priority', array() ), 0, 0 );
if ( $priority && ! array_key_exists( $priorities, $priority ) ):
$priorities[ $priority ] = array(
'column' => $stat->post_name,
'order' => sp_array_value( sp_array_value( $meta, 'sp_order', array() ), 0, 'DESC' )
);
endif;
endforeach;
// Sort priorities in descending order
ksort( $priorities );
// Fill in empty placeholder values for each team
foreach ( $team_ids as $team_id ):
if ( ! $team_id )
continue;
foreach ( $equations as $key => $value ):
if ( sp_array_value( $placeholders[ $team_id ], $key, '' ) == '' ):
$placeholders[ $team_id ][ $key ] = $eos->solveIF( str_replace( ' ', '', $value ), $totals[ $team_id ] );
foreach ( $stats as $stat ):
if ( sp_array_value( $placeholders[ $team_id ], $stat->post_name, '' ) == '' ):
$placeholders[ $team_id ][ $stat->post_name ] = $eos->solveIF( str_replace( ' ', '', $stat->equation ), $totals[ $team_id ] );
endif;
endforeach;
endforeach;
// Get columns from statistics variables
$columns = sp_get_var_labels( 'sp_stat' );
// Merge the data and placeholders arrays
$merged = array();
foreach( $tempdata as $team_id => $team_data ):
foreach( $team_data as $key => $value ):
if ( $value != '' ):
$merged[ $team_id ][ $key ] = $value;
elseif ( array_key_exists( $team_id, $placeholders ) && array_key_exists( $key, $placeholders[ $team_id ] ) ):
$merged[ $team_id ][ $key ] = $placeholders[ $team_id ][ $key ];
else:
endif;
endforeach;
endforeach;
uasort( $merged, function( $a, $b ) use ( $priorities ) {
foreach( $priorities as $priority ):
if ( sp_array_value( $a, $priority['column'], 0 ) != sp_array_value( $b, $priority['column'], 0 ) ):
$output = sp_array_value( $a, $priority['column'], 0 ) - sp_array_value( $b, $priority['column'], 0 );
if ( $priority['order'] == 'DESC' ) $output = 0 - $output;
return $output;
endif;
endforeach;
return 0;
});
// Rearrange data array to reflect statistics
$data = array();
foreach( $merged as $key => $value ):
$data[ $key ] = $tempdata[ $key ];
endforeach;
sp_league_table( $columns, $data, $placeholders );
sp_nonce();

View File

@@ -73,6 +73,15 @@ jQuery(document).ready(function($){
}
});
// Equation selector
$('.sp-order-selector select:first').change(function() {
if($(this).val() == '0') {
$(this).siblings().prop( 'disabled', true );
} else {
$(this).siblings().prop( 'disabled', false )
}
});
// Trigger equation selector
$('.sp-equation-selector select:last').change().siblings().change();

View File

@@ -53,6 +53,18 @@ function sp_manage_posts_custom_column( $column, $post_id ) {
get_post_meta ( $post_id, 'sp_equation', true )
);
break;
case 'sp_order':
$priority = get_post_meta ( $post_id, 'sp_priority', true );
if ( $priority ):
echo $priority . ' ' . str_replace(
array( 'DESC', 'ASC' ),
array( '&darr;', '&uarr;' ),
get_post_meta ( $post_id, 'sp_order', true )
);
else:
echo '—';
endif;
break;
case 'sp_player':
echo sp_the_posts( $post_id, 'sp_player' );
break;
@@ -166,6 +178,12 @@ function sp_save_post( $post_id ) {
// Update equation as string
update_post_meta( $post_id, 'sp_equation', implode( ' ', sp_array_value( $_POST, 'sp_equation', array() ) ) );
// Update sort order as string
update_post_meta( $post_id, 'sp_priority', sp_array_value( $_POST, 'sp_priority', '0' ) );
// Update sort order as string
update_post_meta( $post_id, 'sp_order', sp_array_value( $_POST, 'sp_order', 'DESC' ) );
break;
case ( 'sp_metric' ):

View File

@@ -1,22 +1,4 @@
<?php
if ( !function_exists( 'sp_get_array_depth' ) ) {
function sp_get_array_depth( $array ) {
$max_depth = 1;
if ( is_array( $array ) ):
foreach ( $array as $value ):
if ( is_array( $value ) ):
$depth = sp_get_array_depth( $value ) + 1;
if ( $depth > $max_depth )
$max_depth = $depth;
endif;
endforeach;
return $max_depth;
else:
return 0;
endif;
}
}
if ( !function_exists( 'sp_array_between' ) ) {
function sp_array_between ( $array = array(), $delimiter = 0, $index = 0 ) {
$keys = array_keys( $array, $delimiter );
@@ -148,16 +130,6 @@ if ( !function_exists( 'sp_dropdown_taxonomies' ) ) {
}
if ( !function_exists( 'sp_dropdown_pages' ) ) {
/*
<select name="sp_results[60][outcome]" id="sp_results[60][outcome]">
<option value="0">Select Outcome</option>
<option class="level-0" value="138">W</option>
<option class="level-0" value="139" selected="selected">L</option>
<option class="level-0" value="141">D</option>
</select>
*/
function sp_dropdown_pages( $args = array() ) {
$defaults = array(
'show_option_all' => false,
@@ -417,27 +389,6 @@ if ( !function_exists( 'sp_get_equation_selector' ) ) {
}
}
if ( !function_exists( 'sp_get_eos_rows' ) ) {
function sp_get_eos_rows( $raw ) {
$raw = str_replace( array( "\r\n", ' ' ), array( "\n", '' ), $raw );
$output = explode( "\n", $raw );
return $output;
}
}
if ( !function_exists( 'sp_get_eos_keys' ) ) {
function sp_get_eos_keys( $raw ) {
$raw = str_replace( array( "\r\n", ' :' ), array( "\n", ':' ), $raw );
$arr = explode( "\n", $raw );
$output = array();
foreach ( $arr as $value ):
$output[] = substr( $value, 0, strpos( $value, ':') );
endforeach;
return $output;
}
}
if ( !function_exists( 'sp_get_var_labels' ) ) {
function sp_get_var_labels( $post_type, $independent = false ) {
$args = array(
@@ -489,261 +440,6 @@ if ( !function_exists( 'sp_get_var_equations' ) ) {
}
}
if ( !function_exists( 'sp_get_stats_row' ) ) {
function sp_get_stats_row( $post_id, $post_type = 'post', $args = array(), $static = false ) {
$args = array_merge(
array(
'posts_per_page' => -1
),
(array)$args
);
$posts = (array)get_posts( $args );
// Equation Operating System
$eos = new eqEOS();
$vars = array();
$stats_settings = get_option( 'sportspress_stats' );
// Get dynamic stats
switch ( $post_type ):
case 'sp_team':
// All events attended by the team
$vars['eventsattended'] = $vars['eventsplayed'] = sizeof( $posts );
// Get result variables
$args = array(
'post_type' => 'sp_result',
'numberposts' => -1,
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC'
);
$result_vars = (array)get_posts( $args );
// Get outcome variables
$args = array(
'post_type' => 'sp_outcome',
'numberposts' => -1,
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC'
);
$outcome_vars = (array)get_posts( $args );
// Initialize outcome variables
foreach( $outcome_vars as $outcome_var ):
$vars[ $outcome_var->post_name ] = 0;
$vars[ $outcome_var->post_name . '_max' ] = 0;
$vars[ $outcome_var->post_name . '_min' ] = 0;
endforeach;
// Populate each result variable
foreach( $result_vars as $result_var ):
// Initialize and add for element to array
if ( ! array_key_exists( $result_var->post_name, $vars . '_for' ) ):
$vars[ $result_var->post_name . '_for' ] = 0;
endif;
// Initialize and add against element to array
if ( ! array_key_exists( $result_var->post_name, $vars . '_against' ) ):
$vars[ $result_var->post_name . '_against' ] = 0;
endif;
foreach( $posts as $event ):
// Get match statistics
$stats = get_post_meta( $event->ID, 'sp_stats', true );
// Get value for the team in this match
$value = (double) sp_array_value( $stats[ $post_id ][0], $result_var->post_name, 0 );
// Add value for
$vars[ $result_var->post_name . '_for' ] += $value;
// Add values against
foreach ( $stats as $team_post_id => $stat_array ):
if ( $team_post_id != $post_id ):
$vars[ $result_var->post_name . '_against' ] += sp_array_value( $stat_array[0], $result_var->post_name, 0 );
endif;
endforeach;
// Calculate outcome
// TODO
// Check if max or min, and replace if it is
// if ( $value > $vars[ $result->post_name . '_max' ] ) $vars[ $result->post_name . '_max' ] = $value;
// elseif ( $value < $vars[ $result->post_name . '_min' ] ) $vars[ $result->post_name . '_min' ] = $value;
endforeach;
endforeach;
// Get stats columns
$args = array(
'post_type' => 'sp_stat',
'numberposts' => -1,
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC'
);
$columns = (array)get_posts( $args );
break;
case 'sp_player':
// Get stats settings keys
$keys = sp_get_eos_keys( get_option( 'sp_player_stats_columns' ) );
// 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_rows( get_option( 'sp_event_stats_columns' ) );
foreach ( $columns as $key => $value ):
$row = explode( ':', $value );
$var_name = strtolower( preg_replace( '~[^\p{L}]++~u', '', end( $row ) ) );
$vars[ $var_name ] = 0;
$stats_keys[ $key ] = $var_name;
endforeach;
// Populate columns with player stats from events
foreach ( $posts as $post ):
$team_stats = get_post_meta( $post->ID, 'sp_stats', true );
foreach ( $team_stats as $team_id => $stat ):
if ( array_key_exists( 1, $args['meta_query'] ) && $team_id != sp_array_value( $args['meta_query'][1], 'value', 0 ) ) continue;
$player_id = sp_array_value( $args['meta_query'][0], 'value', 0 );
if ( !array_key_exists( $player_id, $stat ) ) continue;
foreach ( $stat[ $player_id ] as $key => $value ):
if ( !array_key_exists( $key, $stats_keys ) || !array_key_exists( $stats_keys[ $key ], $vars ) ) continue;
$vars[ $stats_keys[ $key ] ] += $value;
endforeach;
endforeach;
endforeach;
// Add appearances event count to vars
$vars['appearances'] = sizeof( $posts );
// Get EOS array
$rows = sp_get_eos_rows( get_option( 'sp_player_stats_columns' ) );
break;
default:
$columns = array();
break;
endswitch;
// Get dynamic stats
$dynamic = array();
foreach ( $columns as $column ):
$equation = get_post_meta( $column->ID, 'sp_equation', true );
//$dynamic[ $column->post_name ] = $eos->solveIF( $equation, $vars );
endforeach;
echo '<pre>';
print_r( $vars );
echo '</pre>';
if ( $static || true ):
// Get static stats
$static = (array)get_post_meta( $args['meta_query'][0]['value'], 'sp_stats', true );
$table = sp_array_value( $static, 0, array() );
if ( array_key_exists( 'tax_query', $args ) )
$row_id = $args['tax_query'][0]['terms'];
else
$row_id = 0;
$static = sp_array_value( $table, $row_id, array() );
// Combine static and dynamic stats
$output = array_filter( $static ) + $dynamic;
ksort( $output );
else:
$output = $dynamic;
endif;
return $output;
}
}
if ( !function_exists( 'sp_stats_table' ) ) {
function sp_stats_table( $stats = array(), $placeholders = array(), $index = 0, $columns = array(), $total = true, $rowtype = 'post', $slug = 'sp_stats' ) {
global $pagenow;
if ( !is_array( $stats ) )
$stats = array( __( 'Name', 'sportspress' ) );
?>
<table class="widefat sp-data-table">
<thead>
<tr>
<?php foreach ( $columns as $column ): ?>
<th><?php echo $column; ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php
$i = 0;
foreach ( $stats as $key => $values ):
if ( !$key ) continue;
?>
<tr class="sp-row sp-post<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>">
<td>
<?php
switch( $rowtype ):
case 'post':
$title = get_the_title( $key );
break;
default:
$term = get_term( $key, $rowtype );
$title = $term->name;
break;
endswitch;
if ( empty( $title ) )
$title = __( '(no title)' );
echo $title;
?>
</td>
<?php for ( $j = 0; $j < sizeof( $columns ) - 1; $j ++ ):
$value = sp_array_value( $values, $j, '' );
$placeholder = (int)sp_array_value( sp_array_value( $placeholders, $key, 0), $j, 0 );
?>
<td><input type="text" name="<?php echo $slug; ?>[<?php echo $index; ?>][<?php echo $key; ?>][]" value="<?php echo $value; ?>" placeholder="<?php echo $placeholder; ?>" /></td>
<?php endfor; ?>
</tr>
<?php
$i++;
endforeach;
if ( $total ):
$values = sp_array_value( $stats, 0, array() );
?>
<tr class="sp-row sp-total<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>">
<td><strong><?php _e( 'Total', 'sportspress' ); ?></strong></td>
<?php for ( $j = 0; $j < sizeof( $columns ) - 1; $j ++ ):
$value = sp_array_value( $values, $j, '' );
?>
<td><input type="text" name="<?php echo $slug; ?>[<?php echo $index; ?>][0][]" value="<?php echo $value; ?>" placeholder="0" /></td>
<?php endfor; ?>
</tr>
<?php endif; ?>
</tbody>
</table>
<?php
}
}
if ( !function_exists( 'sp_league_table' ) ) {
function sp_league_table( $columns = array(), $data = array(), $placeholders = array() ) {
?>

View File

@@ -27,8 +27,8 @@ include dirname( __FILE__ ) . '/lib/classes/eos.class.php' ;
// Globals
include dirname( __FILE__ ) . '/sportspress-globals.php' ;
// Helpers
require_once dirname( __FILE__ ) . '/sportspress-helpers.php';
// Functions
require_once dirname( __FILE__ ) . '/sportspress-functions.php';
// Settings
include dirname( __FILE__ ) . '/sportspress-settings.php' ;