Calculate sum/average statistics, delete duplicate configs, sort player list by number

This commit is contained in:
Brian Miyaji
2014-01-25 05:42:33 +11:00
parent a4aa963755
commit 8db2a05fa3
7 changed files with 177 additions and 81 deletions

View File

@@ -75,6 +75,9 @@ function sportspress_manage_posts_custom_column( $column, $post_id ) {
case 'sp_precision':
echo sportspress_get_post_precision( $post_id );
break;
case 'sp_calculate':
echo sportspress_get_post_calculate( $post_id );
break;
case 'sp_player':
echo sportspress_the_posts( $post_id, 'sp_player' );
break;

View File

@@ -11,7 +11,9 @@ function sportspress_sanitize_title( $title ) {
if ( ! $key ) $key = $_POST['post_title'];
$title = sportspress_get_eos_safe_slug( $key, sportspress_array_value( $_POST, 'ID', 'var' ) );
$id = sportspress_array_value( $_POST, 'post_ID', 'var' );
$title = sportspress_get_eos_safe_slug( $key, $id );
elseif ( isset( $_POST ) && array_key_exists( 'post_type', $_POST ) && $_POST['post_type'] == 'sp_event' ):

View File

@@ -47,7 +47,24 @@ function sportspress_save_post( $post_id ) {
break;
case ( 'sp_result' ):
// Delete posts with duplicate key
sportspress_delete_duplicate_post( $_POST );
break;
case ( 'sp_outcome' ):
// Delete posts with duplicate key
sportspress_delete_duplicate_post( $_POST );
break;
case ( 'sp_column' ):
// Delete posts with duplicate key
sportspress_delete_duplicate_post( $_POST );
// Update equation as string
update_post_meta( $post_id, 'sp_equation', implode( ' ', sportspress_array_value( $_POST, 'sp_equation', array() ) ) );
@@ -65,8 +82,11 @@ function sportspress_save_post( $post_id ) {
case ( 'sp_statistic' ):
// Update equation as string
update_post_meta( $post_id, 'sp_equation', implode( ' ', sportspress_array_value( $_POST, 'sp_equation', array() ) ) );
// Delete posts with duplicate key
sportspress_delete_duplicate_post( $_POST );
// Update calculation method as string
update_post_meta( $post_id, 'sp_calculate', sportspress_array_value( $_POST, 'sp_calculate', 'DESC' ) );
break;

View File

@@ -54,7 +54,7 @@ function sportspress_column_details_meta( $post ) {
<p class="sp-equation-selector">
<?php
foreach ( $equation as $piece ):
sportspress_get_equation_selector( $post->ID, $piece, array( 'team_event', 'result', 'outcome' ) );
sportspress_equation_selector( $post->ID, $piece, array( 'team_event', 'result', 'outcome' ) );
endforeach;
?>
</p>

View File

@@ -25,7 +25,7 @@ function sportspress_statistic_edit_columns() {
'cb' => '<input type="checkbox" />',
'title' => __( 'Label', 'sportspress' ),
'sp_positions' => __( 'Positions', 'sportspress' ),
'sp_equation' => __( 'Equation', 'sportspress' ),
'sp_calculate' => __( 'Calculate', 'sportspress' ),
);
return $columns;
}
@@ -36,15 +36,11 @@ function sportspress_statistic_meta_init() {
}
function sportspress_statistic_equation_meta( $post ) {
$equation = explode( ' ', get_post_meta( $post->ID, 'sp_equation', true ) );
$calculate = get_post_meta( $post->ID, 'sp_calculate', true );
?>
<p><strong><?php _e( 'Equation', 'sportspress' ); ?></strong></p>
<p class="sp-equation-selector">
<?php
foreach ( $equation as $piece ):
sportspress_get_equation_selector( $post->ID, $piece, array( 'player_event' ) );
endforeach;
?>
<p><strong><?php _e( 'Calculate', 'sportspress' ); ?></strong></p>
<p class="sp-calculate-selector">
<?php sportspress_calculate_selector( $post->ID, $calculate ); ?>
</p>
<?php
sportspress_nonce();

View File

@@ -46,14 +46,14 @@ $data = get_posts( $args );
<tr>
<th><?php _e( 'Label', 'sportspress' ); ?></th>
<th><?php _e( 'Positions', 'sportspress' ); ?></th>
<th><?php _e( 'Equation', 'sportspress' ); ?></th>
<th><?php _e( 'Calculate', 'sportspress' ); ?></th>
</tr>
</thead>
<?php $i = 0; foreach ( $data as $row ): ?>
<tr<?php if ( $i % 2 == 0 ) echo ' class="alternate"'; ?>>
<td class="row-title"><?php echo $row->post_title; ?></td>
<td><?php echo get_the_terms ( $row->ID, 'sp_position' ) ? the_terms( $row->ID, 'sp_position' ) : sprintf( __( 'All %s', 'sportspress' ), __( 'positions', 'sportspress' ) ); ?></td>
<td><?php echo sportspress_get_post_equation( $row->ID ); ?></td>
<td><?php echo sportspress_get_post_calculate( $row->ID ); ?></td>
</tr>
<?php $i++; endforeach; ?>
<tfoot>

View File

@@ -95,8 +95,8 @@ if ( !function_exists( 'sportspress_get_term_labels' ) ) {
if ( !function_exists( 'sportspress_get_the_term_id' ) ) {
function sportspress_get_the_term_id( $post_id, $taxonomy, $index ) {
$terms = get_the_terms( $post_id, $taxonomy );
if ( is_array( $terms ) && array_key_exists( $index, $terms ) ):
$term = $terms[0];
if ( is_array( $terms ) && sizeof( $terms ) > 0 ):
$term = reset( $terms );
if ( is_object( $term ) && property_exists( $term, 'term_id' ) )
return $term->term_id;
else
@@ -142,6 +142,21 @@ if ( !function_exists( 'sportspress_get_post_precision' ) ) {
}
}
if ( !function_exists( 'sportspress_get_post_calculate' ) ) {
function sportspress_get_post_calculate( $post_id ) {
$calculate = get_post_meta ( $post_id, 'sp_calculate', true );
if ( $calculate ):
return str_replace(
array( 'sum', 'average' ),
array( __( 'Sum', 'sportspress' ), __( 'Average', 'sportspress' ) ),
$calculate
);
else:
return __( 'Sum', 'sportspress' );
endif;
}
}
if ( !function_exists( 'sportspress_get_post_equation' ) ) {
function sportspress_get_post_equation( $post_id ) {
$equation = get_post_meta ( $post_id, 'sp_equation', true );
@@ -310,8 +325,15 @@ if ( !function_exists( 'sportspress_post_checklist' ) ) {
<?php
$selected = sportspress_array_between( (array)get_post_meta( $post_id, $meta, false ), 0, $index );
$posts = get_pages( array( 'post_type' => $meta, 'number' => 0 ) );
if ( empty( $posts ) )
$posts = get_posts( array( 'post_type' => $meta, 'numberposts' => -1, 'post_per_page' => -1 ) );
if ( empty( $posts ) ):
$query = array( 'post_type' => $meta, 'numberposts' => -1, 'post_per_page' => -1 );
if ( $meta == 'sp_player' ):
$query['meta_key'] = 'sp_number';
$query['orderby'] = 'meta_value_num';
$query['order'] = 'ASC';
endif;
$posts = get_posts( $query );
endif;
foreach ( $posts as $post ):
$parents = get_post_ancestors( $post );
if ( $filter ):
@@ -352,6 +374,22 @@ if ( !function_exists( 'sportspress_post_checklist' ) ) {
}
}
if ( !function_exists( 'sportspress_calculate_selector' ) ) {
function sportspress_calculate_selector( $postid, $selected = null ) {
$options = array(
'sum' => __( 'Sum', 'sportspress' ),
'average' => __( 'Average', 'sportspress' ),
);
?>
<select name="sp_calculate">
<?php foreach( $options as $key => $name ): ?>
<option value="<?php echo $key; ?>" <?php selected ( $key, $selected ); ?>><?php echo $name; ?></option>
<?php endforeach; ?>
</select>
<?php
}
}
if ( !function_exists( 'sportspress_get_equation_optgroup_array' ) ) {
function sportspress_get_equation_optgroup_array( $postid, $type = null, $variations = null, $defaults = null, $totals = true ) {
$arr = array();
@@ -392,8 +430,8 @@ if ( !function_exists( 'sportspress_get_equation_optgroup_array' ) ) {
}
}
if ( !function_exists( 'sportspress_get_equation_selector' ) ) {
function sportspress_get_equation_selector( $postid, $selected = null, $groups = array() ) {
if ( !function_exists( 'sportspress_equation_selector' ) ) {
function sportspress_equation_selector( $postid, $selected = null, $groups = array() ) {
if ( ! isset( $postid ) )
return;
@@ -507,6 +545,28 @@ if ( !function_exists( 'sportspress_get_var_equations' ) ) {
}
}
if ( !function_exists( 'sportspress_get_var_calculates' ) ) {
function sportspress_get_var_calculates( $post_type ) {
$args = array(
'post_type' => $post_type,
'numberposts' => -1,
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC'
);
$vars = get_posts( $args );
$output = array();
foreach ( $vars as $var ):
$calculate = get_post_meta( $var->ID, 'sp_calculate', true );
$output[ $var->post_name ] = $calculate;
endforeach;
return $output;
}
}
if ( !function_exists( 'sportspress_edit_calendar_table' ) ) {
function sportspress_edit_calendar_table( $data = array() ) {
if ( empty( $data ) ):
@@ -599,6 +659,7 @@ if ( !function_exists( 'sportspress_edit_player_list_table' ) ) {
<table class="widefat sp-data-table">
<thead>
<tr>
<th>#</th>
<th><?php _e( 'Player', 'sportspress' ); ?></th>
<?php foreach ( $columns as $label ): ?>
<th><?php echo $label; ?></th>
@@ -611,8 +672,10 @@ if ( !function_exists( 'sportspress_edit_player_list_table' ) ) {
foreach ( $data as $player_id => $player_stats ):
if ( !$player_id ) continue;
$div = get_term( $player_id, 'sp_season' );
$number = get_post_meta( $player_id, 'sp_number', true );
?>
<tr class="sp-row sp-post<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>">
<td><?php echo ( $number ? $number : '&nbsp;' ); ?></td>
<td>
<?php echo get_the_title( $player_id ); ?>
</td>
@@ -739,7 +802,7 @@ if ( !function_exists( 'sportspress_edit_player_statistics_table' ) ) {
<?php foreach( $columns as $column => $label ):
?>
<td><?php
$value = sportspress_array_value( sportspress_array_value( $data, $div_id, array() ), $column, 0 );
$value = sportspress_array_value( sportspress_array_value( $data, $div_id, array() ), $column, null );
$placeholder = sportspress_array_value( sportspress_array_value( $placeholders, $div_id, array() ), $column, 0 );
echo '<input type="text" name="sp_statistics[' . $league_id . '][' . $div_id . '][' . $column . ']" value="' . $value . '" placeholder="' . $placeholder . '"' . ( $readonly ? ' disabled="disabled"' : '' ) . ' />';
?></td>
@@ -1616,7 +1679,7 @@ if ( !function_exists( 'sportspress_get_league_table_data' ) ) {
foreach( $merged as $key => $value ):
$data[ $key ] = $tempdata[ $key ];
endforeach;
if ( $breakdown ):
return array( $columns, $data, $placeholders, $merged );
else:
@@ -1641,9 +1704,6 @@ if ( !function_exists( 'sportspress_get_player_list_data' ) ) {
// Get all leagues populated with stats where available
$tempdata = sportspress_array_combine( $player_ids, $stats );
// Get equations from statistics variables
$equations = sportspress_get_var_equations( 'sp_statistic' );
// Create entry for each player in totals
$totals = array();
$placeholders = array();
@@ -1662,11 +1722,11 @@ if ( !function_exists( 'sportspress_get_player_list_data' ) ) {
$static = get_post_meta( $player_id, 'sp_statistics', true );
// Create placeholders entry for the player
$placeholders[ $player_id ] = array();
$placeholders[ $player_id ] = array( 'eventsplayed' => 0 );
// Add static statistics to placeholders
if ( array_key_exists( $league_id, $static ) && array_key_exists( $div_id, $static[ $league_id ] ) ):
$placeholders[ $player_id ] = $static[ $league_id ][ $div_id ];
$placeholders[ $player_id ] = array_merge( $placeholders[ $player_id ], $static[ $league_id ][ $div_id ] );
endif;
endforeach;
@@ -1735,8 +1795,7 @@ if ( !function_exists( 'sportspress_get_player_list_data' ) ) {
);
$statistics = get_posts( $args );
$columns = array();
$priorities = array();
$columns = array( 'eventsplayed' => __( 'Played', 'sportspress' ) );
foreach ( $statistics as $statistic ):
@@ -1749,31 +1808,44 @@ if ( !function_exists( 'sportspress_get_player_list_data' ) ) {
// Add column name to columns
$columns[ $statistic->post_name ] = $statistic->post_title;
// Add order to priorities if priority is set and does not exist in array already
$priority = sportspress_array_value( sportspress_array_value( $meta, 'sp_priority', array() ), 0, 0 );
if ( $priority && ! array_key_exists( $priority, $priorities ) ):
$priorities[ $priority ] = array(
'column' => $statistic->post_name,
'order' => sportspress_array_value( sportspress_array_value( $meta, 'sp_order', array() ), 0, 'DESC' )
);
endif;
endforeach;
// Sort priorities in descending order
ksort( $priorities );
// Fill in empty placeholder values for each player
foreach ( $player_ids as $player_id ):
if ( ! $player_id )
continue;
// Add events played as an object to statistics for placeholder calculations
$epstat = new stdClass();
$epstat->post_name = 'eventsplayed';
array_unshift( $statistics, $epstat );
foreach ( $statistics as $statistic ):
if ( sportspress_array_value( $placeholders[ $player_id ], $statistic->post_name, '' ) == '' ):
// Reflect totals
$placeholders[ $player_id ][ $statistic->post_name ] = sportspress_array_value( sportspress_array_value( $totals, $player_id, array() ), $statistic->post_name, 0 );
if ( $statistic->post_name == 'eventsplayed' ):
$calculate = 'sum';
else:
$calculate = get_post_meta( $statistic->ID, 'sp_calculate', true );
endif;
if ( $calculate && $calculate == 'average' ):
// Reflect average
$eventsplayed = (int)sportspress_array_value( $totals[ $player_id ], 'eventsplayed', 0 );
if ( ! $eventsplayed ):
$placeholders[ $player_id ][ $statistic->post_name ] = 0;
else:
$placeholders[ $player_id ][ $statistic->post_name ] = sportspress_array_value( sportspress_array_value( $totals, $player_id, array() ), $statistic->post_name, 0 ) / $eventsplayed;
endif;
else:
// Reflect total
$placeholders[ $player_id ][ $statistic->post_name ] = sportspress_array_value( sportspress_array_value( $totals, $player_id, array() ), $statistic->post_name, 0 );
endif;
endif;
endforeach;
@@ -1785,7 +1857,7 @@ if ( !function_exists( 'sportspress_get_player_list_data' ) ) {
foreach( $placeholders as $player_id => $player_data ):
// Add player name to row
$merged[ $player_id ] = array( 'name' => get_the_title( $player_id ) );
$merged[ $player_id ] = array( 'number' => get_post_meta( $player_id, 'sp_number', true ), 'name' => get_the_title( $player_id ), 'eventsplayed' => 0 );
foreach( $player_data as $key => $value ):
@@ -1799,30 +1871,6 @@ if ( !function_exists( 'sportspress_get_player_list_data' ) ) {
endforeach;
endforeach;
uasort( $merged, function( $a, $b ) use ( $priorities ) {
// Loop through priorities
foreach( $priorities as $priority ):
// Proceed if columns are not equal
if ( sportspress_array_value( $a, $priority['column'], 0 ) != sportspress_array_value( $b, $priority['column'], 0 ) ):
// Compare column values
$output = sportspress_array_value( $a, $priority['column'], 0 ) - sportspress_array_value( $b, $priority['column'], 0 );
// Flip value if descending order
if ( $priority['order'] == 'DESC' ) $output = 0 - $output;
return $output;
endif;
endforeach;
// Default sort by alphabetical
return strcmp( sportspress_array_value( $a, 'name', '' ), sportspress_array_value( $b, 'name', '' ) );
});
// Rearrange data array to reflect statistics
$data = array();
foreach( $merged as $key => $value ):
@@ -1886,7 +1934,9 @@ if ( !function_exists( 'sportspress_get_player_statistics_data' ) ) {
$tempdata = sportspress_array_combine( $div_ids, sportspress_array_value( $stats, $league_id, array() ) );
// Get equations from statistics variables
$equations = sportspress_get_var_equations( 'sp_statistic' );
$equations = sportspress_get_var_calculates( 'sp_statistic' );
$equations['eventsplayed'] = 'sum';
foreach ( $div_ids as $div_id ):
@@ -1950,19 +2000,20 @@ if ( !function_exists( 'sportspress_get_player_statistics_data' ) ) {
$placeholders[ $div_id ] = array();
foreach ( $equations as $key => $value ):
if ( empty( $value ) ):
if ( $value == 'average' ):
// Reflect totals
$placeholders[ $div_id ][ $key ] = sportspress_array_value( $totals, $key, 0 );
// Reflect average
$eventsplayed = (int)sportspress_array_value( $totals, 'eventsplayed', 0 );
if ( ! $eventsplayed ):
$placeholders[ $div_id ][ $key ] = 0;
else:
$placeholders[ $div_id ][ $key ] = sportspress_array_value( $totals, $key, 0 ) / $eventsplayed;
endif;
else:
// Calculate value
if ( sizeof( $events ) > 0 ):
$placeholders[ $div_id ][ $key ] = sportspress_solve( $value, $totals );
else:
$placeholders[ $div_id ][ $key ] = 0;
endif;
// Reflect total
$placeholders[ $div_id ][ $key ] = sportspress_array_value( $totals, $key, 0 );
endif;
@@ -1971,7 +2022,10 @@ if ( !function_exists( 'sportspress_get_player_statistics_data' ) ) {
endforeach;
// Get columns from statistics variables
$columns = sportspress_get_var_labels( 'sp_statistic' );
$columns = array_merge(
array( 'eventsplayed' => __( 'Played', 'sportspress' ) ),
sportspress_get_var_labels( 'sp_statistic' )
);
// Merge the data and placeholders arrays
$merged = array();
@@ -2010,7 +2064,7 @@ if ( !function_exists( 'sportspress_get_player_statistics_data' ) ) {
if ( $admin ):
return array( $columns, $tempdata, $placeholders, $merged, $seasons_teams );
else:
$labels = array_merge( array( 'name' => __( 'Season', 'sportspress' ), 'team' => __( 'Team', 'sportspress' ) ), $columns );
$labels = array_merge( array( 'name' => __( 'Season', 'sportspress' ), 'team' => __( 'Team', 'sportspress' ), 'eventsplayed' => __( 'Played', 'sportspress' ) ), $columns );
$merged[0] = $labels;
return $merged;
endif;
@@ -2018,6 +2072,27 @@ if ( !function_exists( 'sportspress_get_player_statistics_data' ) ) {
}
}
if ( !function_exists( 'sportspress_delete_duplicate_post' ) ) {
function sportspress_delete_duplicate_post( &$post ) {
global $wpdb;
$key = isset( $post['sp_key'] ) ? $post['sp_key'] : null;
if ( ! $key ) $key = $post['post_title'];
$id = sportspress_array_value( $post, 'post_ID', 'var' );
$title = sportspress_get_eos_safe_slug( $key, $id );
$check_sql = "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1";
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $title, $post['post_type'], $id ) );
if ( $post_name_check ):
wp_delete_post( $post_name_check, true );
$post['post_status'] = 'draft';
endif;
return $post_name_check;
}
}
if ( !function_exists( 'sportspress_highlight_admin_menu' ) ) {
function sportspress_highlight_admin_menu() {
global $parent_file, $submenu_file;