Add total type selector for average-based player statistics
This commit is contained in:
@@ -239,7 +239,7 @@ jQuery(document).ready(function($){
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Total stats calculator
|
// Total stats calculator
|
||||||
$(".sp-data-table .sp-total input[data-sp-format=number]").on("updateTotal", function() {
|
$(".sp-data-table .sp-total input[data-sp-format=number][data-sp-total-type=total]").on("updateTotal", function() {
|
||||||
index = $(this).parent().index();
|
index = $(this).parent().index();
|
||||||
var sum = 0;
|
var sum = 0;
|
||||||
$(this).closest(".sp-data-table").find(".sp-post").each(function() {
|
$(this).closest(".sp-data-table").find(".sp-post").each(function() {
|
||||||
@@ -248,7 +248,7 @@ jQuery(document).ready(function($){
|
|||||||
val = $(this).find("td").eq(index).find("input").attr("placeholder");
|
val = $(this).find("td").eq(index).find("input").attr("placeholder");
|
||||||
}
|
}
|
||||||
if($.isNumeric(val)) {
|
if($.isNumeric(val)) {
|
||||||
sum += parseInt(val, 10);
|
sum += parseFloat(val, 10);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
$(this).attr("placeholder", sum);
|
$(this).attr("placeholder", sum);
|
||||||
@@ -257,12 +257,12 @@ jQuery(document).ready(function($){
|
|||||||
// Activate total stats calculator
|
// Activate total stats calculator
|
||||||
if($(".sp-data-table .sp-total").size()) {
|
if($(".sp-data-table .sp-total").size()) {
|
||||||
$(".sp-data-table .sp-post td input").on("keyup", function() {
|
$(".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");
|
$(this).closest(".sp-data-table").find(".sp-total td").eq($(this).parent().index()).find("input[data-sp-format=number][data-sp-total-type=total]").trigger("updateTotal");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Trigger total stats calculator
|
// Trigger total stats calculator
|
||||||
$(".sp-data-table .sp-total input").trigger("updateTotal");
|
$(".sp-data-table .sp-total input[data-sp-format=number][data-sp-total-type=total]").trigger("updateTotal");
|
||||||
|
|
||||||
// Sync inputs
|
// Sync inputs
|
||||||
$(".sp-sync-input").on("keyup", function() {
|
$(".sp-sync-input").on("keyup", function() {
|
||||||
|
|||||||
@@ -33,16 +33,16 @@ class SP_Meta_Box_Player_Statistics {
|
|||||||
?>
|
?>
|
||||||
<p><strong><?php echo $league->name; ?></strong></p>
|
<p><strong><?php echo $league->name; ?></strong></p>
|
||||||
<?php
|
<?php
|
||||||
list( $columns, $data, $placeholders, $merged, $seasons_teams, $has_checkboxes, $formats ) = $player->data( $league->term_id, true );
|
list( $columns, $data, $placeholders, $merged, $seasons_teams, $has_checkboxes, $formats, $total_types ) = $player->data( $league->term_id, true );
|
||||||
self::table( $post->ID, $league->term_id, $columns, $data, $placeholders, $merged, $seasons_teams, $has_checkboxes && $i == 0, true, $formats );
|
self::table( $post->ID, $league->term_id, $columns, $data, $placeholders, $merged, $seasons_teams, $has_checkboxes && $i == 0, true, $formats, $total_types );
|
||||||
$i ++;
|
$i ++;
|
||||||
endforeach;
|
endforeach;
|
||||||
if ( $show_career_totals ) {
|
if ( $show_career_totals ) {
|
||||||
?>
|
?>
|
||||||
<p><strong><?php _e( 'Career Total', 'sportspress' ); ?></strong></p>
|
<p><strong><?php _e( 'Career Total', 'sportspress' ); ?></strong></p>
|
||||||
<?php
|
<?php
|
||||||
list( $columns, $data, $placeholders, $merged, $seasons_teams ) = $player->data( 0, true );
|
list( $columns, $data, $placeholders, $merged, $seasons_teams, $has_checkboxes, $formats, $total_types ) = $player->data( 0, true );
|
||||||
self::table( $post->ID, 0, $columns, $data, $placeholders, $merged, $seasons_teams );
|
self::table( $post->ID, 0, $columns, $data, $placeholders, $merged, $seasons_teams, false, false, $formats, $total_types );
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Determine order of sections
|
// Determine order of sections
|
||||||
@@ -60,16 +60,16 @@ class SP_Meta_Box_Player_Statistics {
|
|||||||
?>
|
?>
|
||||||
<p><strong><?php echo $league->name; ?> — <?php echo $section_label; ?></strong></p>
|
<p><strong><?php echo $league->name; ?> — <?php echo $section_label; ?></strong></p>
|
||||||
<?php
|
<?php
|
||||||
list( $columns, $data, $placeholders, $merged, $seasons_teams, $has_checkboxes ) = $player->data( $league->term_id, true, $section_id );
|
list( $columns, $data, $placeholders, $merged, $seasons_teams, $has_checkboxes, $formats, $total_types ) = $player->data( $league->term_id, true, $section_id );
|
||||||
self::table( $post->ID, $league->term_id, $columns, $data, $placeholders, $merged, $seasons_teams, $has_checkboxes && $i == 0 && $s == 0, $s == 0 );
|
self::table( $post->ID, $league->term_id, $columns, $data, $placeholders, $merged, $seasons_teams, $has_checkboxes && $i == 0 && $s == 0, $s == 0, $formats, $total_types );
|
||||||
$i ++;
|
$i ++;
|
||||||
endforeach;
|
endforeach;
|
||||||
if ( $show_career_totals ) {
|
if ( $show_career_totals ) {
|
||||||
?>
|
?>
|
||||||
<p><strong><?php _e( 'Career Total', 'sportspress' ); ?> — <?php echo $section_label; ?></strong></p>
|
<p><strong><?php _e( 'Career Total', 'sportspress' ); ?> — <?php echo $section_label; ?></strong></p>
|
||||||
<?php
|
<?php
|
||||||
list( $columns, $data, $placeholders, $merged, $seasons_teams ) = $player->data( 0, true, $section_id );
|
list( $columns, $data, $placeholders, $merged, $seasons_teams, $has_checkboxes, $formats, $total_types ) = $player->data( 0, true, $section_id );
|
||||||
self::table( $post->ID, 0, $columns, $data, $placeholders, $merged, $seasons_teams, $has_checkboxes && $i == 0 && $s == 0, $s == 0 );
|
self::table( $post->ID, 0, $columns, $data, $placeholders, $merged, $seasons_teams, $has_checkboxes && $i == 0 && $s == 0, $s == 0, $formats, $total_types );
|
||||||
}
|
}
|
||||||
$s ++;
|
$s ++;
|
||||||
}
|
}
|
||||||
@@ -88,7 +88,7 @@ class SP_Meta_Box_Player_Statistics {
|
|||||||
/**
|
/**
|
||||||
* Admin edit table
|
* Admin edit table
|
||||||
*/
|
*/
|
||||||
public static function table( $id = null, $league_id, $columns = array(), $data = array(), $placeholders = array(), $merged = array(), $leagues = array(), $has_checkboxes = false, $team_select = false, $formats = array() ) {
|
public static function table( $id = null, $league_id, $columns = array(), $data = array(), $placeholders = array(), $merged = array(), $leagues = array(), $has_checkboxes = false, $team_select = false, $formats = array(), $total_types = array() ) {
|
||||||
$readonly = false;
|
$readonly = false;
|
||||||
$teams = array_filter( get_post_meta( $id, 'sp_team', false ) );
|
$teams = array_filter( get_post_meta( $id, 'sp_team', false ) );
|
||||||
?>
|
?>
|
||||||
@@ -124,7 +124,7 @@ class SP_Meta_Box_Player_Statistics {
|
|||||||
if ( $readonly )
|
if ( $readonly )
|
||||||
echo $value ? $value : $placeholder;
|
echo $value ? $value : $placeholder;
|
||||||
else
|
else
|
||||||
echo '<input type="text" name="sp_statistics[' . $league_id . '][0][' . $column . ']" value="' . esc_attr( $value ) . '" placeholder="' . esc_attr( $placeholder ) . '"' . ( $readonly ? ' disabled="disabled"' : '' ) . ' data-sp-format="number" />';
|
echo '<input type="text" name="sp_statistics[' . $league_id . '][0][' . $column . ']" value="' . esc_attr( $value ) . '" placeholder="' . esc_attr( $placeholder ) . '"' . ( $readonly ? ' disabled="disabled"' : '' ) . ' data-sp-format="number" data-sp-total-type="' . sp_array_value( $total_types, $column, 'total' ) . '" />';
|
||||||
?></td>
|
?></td>
|
||||||
<?php endforeach; ?>
|
<?php endforeach; ?>
|
||||||
</tr>
|
</tr>
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ class SP_Meta_Box_Statistic_Details extends SP_Meta_Box_Config {
|
|||||||
$precision = get_post_meta( $post->ID, 'sp_precision', true );
|
$precision = get_post_meta( $post->ID, 'sp_precision', true );
|
||||||
$section = get_post_meta( $post->ID, 'sp_section', true );
|
$section = get_post_meta( $post->ID, 'sp_section', true );
|
||||||
$format = get_post_meta( $post->ID, 'sp_format', true );
|
$format = get_post_meta( $post->ID, 'sp_format', true );
|
||||||
|
$total = get_post_meta( $post->ID, 'sp_total_type', true );
|
||||||
$visible = get_post_meta( $post->ID, 'sp_visible', true );
|
$visible = get_post_meta( $post->ID, 'sp_visible', true );
|
||||||
|
|
||||||
// Defaults
|
// Defaults
|
||||||
@@ -65,6 +66,17 @@ class SP_Meta_Box_Statistic_Details extends SP_Meta_Box_Config {
|
|||||||
?>
|
?>
|
||||||
</select>
|
</select>
|
||||||
</p>
|
</p>
|
||||||
|
<p><strong><?php _e( 'Career Total', 'sportspress' ); ?></strong></p>
|
||||||
|
<p>
|
||||||
|
<select name="sp_total_type">
|
||||||
|
<?php
|
||||||
|
$options = apply_filters( 'sportspress_statistic_totals', array( 'total' => __( 'Total', 'sportspress' ), 'average' => __( 'Average', 'sportspress' ) ) );
|
||||||
|
foreach ( $options as $key => $value ):
|
||||||
|
printf( '<option value="%s" %s>%s</option>', $key, selected( $key == $total, true, false ), $value );
|
||||||
|
endforeach;
|
||||||
|
?>
|
||||||
|
</select>
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
<strong><?php _e( 'Visible', 'sportspress' ); ?></strong>
|
<strong><?php _e( 'Visible', 'sportspress' ); ?></strong>
|
||||||
<i class="dashicons dashicons-editor-help sp-desc-tip" title="<?php _e( 'Display in player profile?', 'sportspress' ); ?>"></i>
|
<i class="dashicons dashicons-editor-help sp-desc-tip" title="<?php _e( 'Display in player profile?', 'sportspress' ); ?>"></i>
|
||||||
@@ -92,6 +104,7 @@ class SP_Meta_Box_Statistic_Details extends SP_Meta_Box_Config {
|
|||||||
public static function save( $post_id, $post ) {
|
public static function save( $post_id, $post ) {
|
||||||
self::delete_duplicate( $_POST );
|
self::delete_duplicate( $_POST );
|
||||||
update_post_meta( $post_id, 'sp_section', (int) sp_array_value( $_POST, 'sp_section', -1 ) );
|
update_post_meta( $post_id, 'sp_section', (int) sp_array_value( $_POST, 'sp_section', -1 ) );
|
||||||
|
update_post_meta( $post_id, 'sp_total_type', sp_array_value( $_POST, 'sp_total_type', 'total' ) );
|
||||||
update_post_meta( $post_id, 'sp_format', sp_array_value( $_POST, 'sp_format', 'number' ) );
|
update_post_meta( $post_id, 'sp_format', sp_array_value( $_POST, 'sp_format', 'number' ) );
|
||||||
update_post_meta( $post_id, 'sp_precision', (int) sp_array_value( $_POST, 'sp_precision', 1 ) );
|
update_post_meta( $post_id, 'sp_precision', (int) sp_array_value( $_POST, 'sp_precision', 1 ) );
|
||||||
update_post_meta( $post_id, 'sp_visible', sp_array_value( $_POST, 'sp_visible', 1 ) );
|
update_post_meta( $post_id, 'sp_visible', sp_array_value( $_POST, 'sp_visible', 1 ) );
|
||||||
|
|||||||
@@ -613,6 +613,7 @@ class SP_Player extends SP_Custom_Post {
|
|||||||
$columns = array_merge( $performance_labels, $stats );
|
$columns = array_merge( $performance_labels, $stats );
|
||||||
|
|
||||||
$formats = array();
|
$formats = array();
|
||||||
|
$total_types = array();
|
||||||
|
|
||||||
$args = array(
|
$args = array(
|
||||||
'post_type' => array( 'sp_performance', 'sp_statistic' ),
|
'post_type' => array( 'sp_performance', 'sp_statistic' ),
|
||||||
@@ -640,6 +641,12 @@ class SP_Player extends SP_Custom_Post {
|
|||||||
$format = 'number';
|
$format = 'number';
|
||||||
}
|
}
|
||||||
$formats[ $post->post_name ] = $format;
|
$formats[ $post->post_name ] = $format;
|
||||||
|
|
||||||
|
$total_type = get_post_meta( $post->ID, 'sp_total_type', true );
|
||||||
|
if ( '' === $total_type ) {
|
||||||
|
$total_type = 'total';
|
||||||
|
}
|
||||||
|
$total_types[ $post->post_name ] = $total_type;
|
||||||
}
|
}
|
||||||
$columns = array_merge( $column_order, $columns );
|
$columns = array_merge( $column_order, $columns );
|
||||||
$usecolumns = array_merge( $usecolumn_order, $usecolumns );
|
$usecolumns = array_merge( $usecolumn_order, $usecolumns );
|
||||||
@@ -670,19 +677,35 @@ class SP_Player extends SP_Custom_Post {
|
|||||||
endif;
|
endif;
|
||||||
|
|
||||||
// Calculate total statistics
|
// Calculate total statistics
|
||||||
$totals = array(
|
$career = array(
|
||||||
'name' => __( 'Total', 'sportspress' ),
|
'name' => __( 'Total', 'sportspress' ),
|
||||||
'team' => '-',
|
'team' => '-',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Add values from all seasons for total-based statistics
|
||||||
foreach ( $merged as $season => $stats ):
|
foreach ( $merged as $season => $stats ):
|
||||||
if ( ! is_array( $stats ) ) continue;
|
if ( ! is_array( $stats ) ) continue;
|
||||||
foreach ( $stats as $key => $value ):
|
foreach ( $stats as $key => $value ):
|
||||||
if ( in_array( $key, array( 'name', 'team' ) ) ) continue;
|
if ( in_array( $key, array( 'name', 'team' ) ) ) continue;
|
||||||
$value = floatval( $value );
|
$value = floatval( $value );
|
||||||
$totals[ $key ] = sp_array_value( $totals, $key, 0 ) + $value;
|
$career[ $key ] = sp_array_value( $career, $key, 0 ) + $value;
|
||||||
endforeach;
|
endforeach;
|
||||||
endforeach;
|
endforeach;
|
||||||
$merged[-1] = $totals;
|
|
||||||
|
// Calculate average-based statistics from performance
|
||||||
|
foreach ( $posts as $post ) {
|
||||||
|
$type = get_post_meta( $post->ID, 'sp_total_type', 'total' );
|
||||||
|
if ( 'average' !== $type ) continue;
|
||||||
|
$value = sp_array_value( $equations, $post->post_name, null );
|
||||||
|
if ( null === $value || ! isset( $value['equation'] ) ) continue;
|
||||||
|
$precision = sp_array_value( $value, 'precision', 0 );
|
||||||
|
$career[ $post->post_name ] = sp_solve( $value['equation'], $totals, $precision );
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add career totals to merged array
|
||||||
|
$manual_career = sp_array_value( $data, 0, array() );
|
||||||
|
$manual_career = array_filter( $manual_career, 'sp_filter_non_empty' );
|
||||||
|
$merged[-1] = array_merge( $career, $manual_career );
|
||||||
|
|
||||||
if ( $admin ):
|
if ( $admin ):
|
||||||
$labels = array();
|
$labels = array();
|
||||||
@@ -694,7 +717,7 @@ class SP_Player extends SP_Custom_Post {
|
|||||||
endif;
|
endif;
|
||||||
endforeach; endif;
|
endforeach; endif;
|
||||||
$placeholders[0] = $merged[-1];
|
$placeholders[0] = $merged[-1];
|
||||||
return array( $labels, $data, $placeholders, $merged, $leagues, $has_checkboxes, $formats );
|
return array( $labels, $data, $placeholders, $merged, $leagues, $has_checkboxes, $formats, $total_types );
|
||||||
else:
|
else:
|
||||||
if ( is_array( $usecolumns ) ):
|
if ( is_array( $usecolumns ) ):
|
||||||
foreach ( $columns as $key => $label ):
|
foreach ( $columns as $key => $label ):
|
||||||
|
|||||||
Reference in New Issue
Block a user