Add equation-based box score performance values

This commit is contained in:
Brian Miyaji
2016-11-01 13:42:31 +11:00
parent 1d5f3c739a
commit dabedf7d41
13 changed files with 143 additions and 13 deletions

View File

@@ -365,7 +365,7 @@ jQuery(document).ready(function($){
$(".sp-format-selector select:first").change(function() {
$precisionselector = $(".sp-precision-selector input:first");
$equationselector = $(".sp-equation-selector select");
$equationdiv = $("#sp_equationdiv");
// Precision settings
if($(this).val() == "decimal" || $(this).val() == "time") {
@@ -375,10 +375,10 @@ jQuery(document).ready(function($){
}
// Equation settings
if($(this).val() == "custom") {
$equationselector.prop( "disabled", true );
if($(this).val() == "equation") {
$equationdiv.show();
} else {
$equationselector.prop( "disabled", false );
$equationdiv.hide();
}
});

View File

@@ -55,7 +55,7 @@ class SP_Admin_Assets {
wp_enqueue_style( 'sportspress-admin-customize-styles', SP()->plugin_url() . '/assets/css/customize.css', array(), SP_VERSION );
}
if ( in_array( $screen->id, array( 'sp_result', 'sp_column', 'sp_statistic' ) ) ) {
if ( in_array( $screen->id, array( 'sp_result', 'sp_performance', 'sp_column', 'sp_statistic' ) ) ) {
wp_enqueue_style( 'sportspress-admin-equation-styles', SP()->plugin_url() . '/assets/css/equation.css', array(), SP_VERSION );
}
@@ -136,7 +136,7 @@ class SP_Admin_Assets {
}
// Edit equation
if ( in_array( $screen->id, array( 'sp_result', 'sp_column', 'sp_statistic' ) ) ) {
if ( in_array( $screen->id, array( 'sp_result', 'sp_performance', 'sp_column', 'sp_statistic' ) ) ) {
wp_enqueue_script( 'sportspress-admin-equationbuilder' );
}
}

View File

@@ -161,6 +161,8 @@ class SP_Admin_Sports {
update_post_meta( $id, 'sp_color', sp_array_value( $performance, 'color', null ) );
update_post_meta( $id, 'sp_section', sp_array_value( $performance, 'section', -1 ) );
update_post_meta( $id, 'sp_format', sp_array_value( $performance, 'format', 'number' ) );
update_post_meta( $id, 'sp_equation', sp_array_value( $performance, 'equation', null ) );
update_post_meta( $id, 'sp_precision', sp_array_value( $performance, 'precision', 0 ) );
$i ++;
}

View File

@@ -60,6 +60,13 @@ class SP_Admin_Meta_Boxes {
'context' => 'normal',
'priority' => 'high',
),
'equation' => array(
'title' => __( 'Equation', 'sportspress' ),
'save' => 'SP_Meta_Box_Performance_Equation::save',
'output' => 'SP_Meta_Box_Performance_Equation::output',
'context' => 'normal',
'priority' => 'high',
),
),
'sp_column' => array(
'details' => array(

View File

@@ -120,6 +120,13 @@ class SP_Meta_Box_Equation {
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'sp_format',
'value' => 'equation',
'compare' => '!=',
),
),
);
$vars = get_posts( $args );

View File

@@ -157,6 +157,13 @@ class SP_Meta_Box_Event_Performance {
'posts_per_page' => 100,
'orderby' => 'menu_order',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'sp_format',
'value' => 'equation',
'compare' => '!=',
),
),
);
$performances = get_posts( $args );
@@ -293,6 +300,7 @@ class SP_Meta_Box_Event_Performance {
</th>
<?php } ?>
<?php foreach ( $labels as $key => $label ): ?>
<?php if ( 'equation' === sp_array_value( $formats, $key, 'number' ) ) continue; ?>
<th>
<?php if ( $has_checkboxes ): ?>
<label for="sp_columns_<?php echo $key; ?>">
@@ -334,6 +342,7 @@ class SP_Meta_Box_Event_Performance {
<td>&nbsp;</td>
<?php } ?>
<?php foreach( $labels as $column => $label ):
if ( 'equation' === sp_array_value( $formats, $column, 'number' ) ) continue;
$player_id = 0;
$player_performance = sp_array_value( $data, $player_id, array() );
$value = sp_array_value( $player_performance, $column, '' );
@@ -397,6 +406,7 @@ class SP_Meta_Box_Event_Performance {
</td>
<?php } ?>
<?php foreach( $labels as $column => $label ):
if ( 'equation' === sp_array_value( $formats, $column, 'number' ) ) continue;
$value = sp_array_value( $player_performance, $column, '' );
$intval = intval( $value );
$placeholder = sp_get_format_placeholder( sp_array_value( $formats, $column, 'number' ) );

View File

@@ -39,6 +39,7 @@ class SP_Meta_Box_Performance_Details extends SP_Meta_Box_Config {
if ( '' === $format ) {
$format = 'number';
}
$precision = get_post_meta( $post->ID, 'sp_precision', true );
?>
<p><strong><?php _e( 'Variable', 'sportspress' ); ?></strong></p>
<p>
@@ -60,13 +61,17 @@ class SP_Meta_Box_Performance_Details extends SP_Meta_Box_Config {
<p class="sp-format-selector">
<select name="sp_format">
<?php
$options = apply_filters( 'sportspress_performance_formats', array( 'number' => __( 'Number', 'sportspress' ), 'text' => __( 'Text', 'sportspress' ) ) );
$options = apply_filters( 'sportspress_performance_formats', array( 'number' => __( 'Number', 'sportspress' ), 'text' => __( 'Text', 'sportspress' ), 'equation' => __( 'Equation', 'sportspress' ) ) );
foreach ( $options as $key => $value ):
printf( '<option value="%s" %s>%s</option>', $key, selected( $key == $format, true, false ), $value );
endforeach;
?>
</select>
</p>
<p><strong><?php _e( 'Decimal Places', 'sportspress' ); ?></strong></p>
<p>
<input name="sp_precision" type="text" size="4" id="sp_precision" value="<?php echo $precision; ?>" placeholder="0">
</p>
<?php
if ( 'auto' === get_option( 'sportspress_player_columns', 'auto' ) ) {
$visible = get_post_meta( $post->ID, 'sp_visible', true );
@@ -103,6 +108,7 @@ class SP_Meta_Box_Performance_Details extends SP_Meta_Box_Config {
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_format', sp_array_value( $_POST, 'sp_format', 'number' ) );
update_post_meta( $post_id, 'sp_precision', sp_array_value( $_POST, 'sp_precision', 0 ) );
if ( 'auto' === get_option( 'sportspress_player_columns', 'auto' ) ) {
update_post_meta( $post_id, 'sp_visible', sp_array_value( $_POST, 'sp_visible', 1 ) );
}

View File

@@ -0,0 +1,29 @@
<?php
/**
* Performance Equation
*
* @author ThemeBoy
* @category Admin
* @package SportsPress/Admin/Meta_Boxes
* @version 2.1
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! class_exists( 'SP_Meta_Box_Equation' ) )
include( 'class-sp-meta-box-equation.php' );
/**
* SP_Meta_Box_Performance_Equation
*/
class SP_Meta_Box_Performance_Equation extends SP_Meta_Box_Equation {
/**
* Output the metabox
*/
public static function output( $post ) {
$equation = get_post_meta( $post->ID, 'sp_equation', true );
$groups = array( 'performance' );
self::builder( $post->post_title, $equation, $groups );
}
}

View File

@@ -97,6 +97,8 @@ class SP_Event extends SP_Custom_Post{
$vars = get_posts( $args );
$labels = array();
$formats = array();
$equations = array();
foreach ( $vars as $var ) {
$labels[ $var->post_name ] = $var->post_title;
$format = get_post_meta( $var->ID, 'sp_format', true );
@@ -104,6 +106,19 @@ class SP_Event extends SP_Custom_Post{
$format = 'number';
}
$formats[ $var->post_name ] = $format;
if ( 'equation' === $format ) {
$equation = get_post_meta( $var->ID, 'sp_equation', true );
$precision = get_post_meta( $var->ID, 'sp_precision', true );
if ( empty( $equation ) ) $equation = 0;
if ( empty( $precision ) ) $precision = 0;
$equations[ $var->post_name ] = array(
'equation' => $equation,
'precision' => $precision,
);
}
}
$order = (array)get_post_meta( $this->ID, 'sp_order', true );
@@ -165,6 +180,27 @@ class SP_Event extends SP_Custom_Post{
unset( $labels['number'] );
endif;
// Calculate equation-based performance
if ( sizeof( $equations ) ):
foreach ( $performance as $team => $players ):
foreach ( $players as $player => $player_performance ):
if ( ! $player ) continue;
// Prepare existing values for equation calculation
$vars = $player_performance;
foreach ( $vars as $key => $var ):
if ( empty( $var ) ) $vars[ $key ] = 0;
endforeach;
$vars = array_merge( $vars, array( 'eventsplayed' => 1 ) );
foreach ( $equations as $key => $equation ):
$performance[ $team ][ $player ][ $key ] = sp_solve( $equation['equation'], $vars, $equation['precision'] );
endforeach;
endforeach;
endforeach;
endif;
// Add minutes to box score values
if ( 'yes' == get_option( 'sportspress_event_performance_show_minutes', 'yes' ) ):
$timeline = $this->timeline();
if ( ! empty( $timeline ) ):

View File

@@ -465,7 +465,7 @@ if ( !function_exists( 'sp_get_post_format' ) ) {
function sp_get_post_format( $post_id ) {
$format = get_post_meta ( $post_id, 'sp_format', true );
if ( isset( $format ) ):
$options = apply_filters( 'sportspress_performance_formats', array( 'number' => __( 'Number', 'sportspress' ), 'time' => __( 'Time', 'sportspress' ), 'text' => __( 'Text', 'sportspress' ) ) );
$options = apply_filters( 'sportspress_performance_formats', array( 'number' => __( 'Number', 'sportspress' ), 'text' => __( 'Text', 'sportspress' ), 'equation' => __( 'Equation', 'sportspress' ) ) );
return sp_array_value( $options, $format, __( 'Number', 'sportspress' ) );
else:
return __( 'Number', 'sportspress' );

View File

@@ -78,7 +78,7 @@ if ( ! isset( $class ) ) $class = null;
$format = sp_array_value( $formats, $key, 'number' );
$placeholder = sp_get_format_placeholder( $format );
$value = '&mdash;';
$value = '-';
if ( $key == 'position' ):
if ( array_key_exists( $key, $row ) && $row[ $key ] != '' ):
$positions = array();

View File

@@ -121,7 +121,7 @@ if ( ! isset( $subs ) ) $subs = array();
$format = sp_array_value( $formats, $key, 'number' );
$placeholder = sp_get_format_placeholder( $format );
$value = '&mdash;';
$value = '-';
if ( $key == 'position' ):
$positions = array();
if ( array_key_exists( $key, $row ) && $row[ $key ] != '' ):
@@ -151,7 +151,7 @@ if ( ! isset( $subs ) ) $subs = array();
$totals[ $key ] = $placeholder;
endif;
if ( 'text' !== $format ) {
if ( 'number' === $format ) {
$totals[ $key ] += $value;
}
@@ -174,6 +174,22 @@ if ( ! isset( $subs ) ) $subs = array();
$i++;
endforeach;
if ( sizeof( $equations ) ):
// Prepare total values for equation calculation
$vars = $totals;
foreach ( $vars as $key => $var ):
if ( empty( $var ) ) $vars[ $key ] = 0;
endforeach;
$vars = array_merge( $vars, array( 'eventsplayed' => 1 ) );
// Calculate equation-based totals
foreach ( $equations as $key => $equation ):
$totals[ $key ] = sp_solve( $equation['equation'], $vars, $equation['precision'] );
endforeach;
endif;
?>
</tbody>
<?php endif; ?>

View File

@@ -77,16 +77,30 @@ if ( is_array( $teams ) ):
$columns = get_posts( $args );
// Get formats
// Get formats and equations
$formats = array();
$equations = array();
// Add to formats
// Add to formats and equations
foreach ( $columns as $column ) {
$format = get_post_meta( $column->ID, 'sp_format', true );
if ( '' === $format ) {
$format = 'number';
}
$formats[ $column->post_name ] = $format;
if ( 'equation' === $format ) {
$equation = get_post_meta( $column->ID, 'sp_equation', true );
$precision = get_post_meta( $column->ID, 'sp_precision', true );
if ( empty( $equation ) ) $equation = 0;
if ( empty( $precision ) ) $precision = 0;
$equations[ $column->post_name ] = array(
'equation' => $equation,
'precision' => $precision,
);
}
}
if ( $is_individual ) {
@@ -109,6 +123,7 @@ if ( is_array( $teams ) ):
'caption' => __( 'Box Score', 'sportspress' ),
'labels' => $labels,
'formats' => $formats,
'equations' => $equations,
'mode' => $mode,
'data' => $data,
'event' => $event,
@@ -219,6 +234,7 @@ if ( is_array( $teams ) ):
'caption' => 0 == $s && $team_id ? sp_get_team_name( $team_id, $abbreviate_teams ) : null,
'labels' => $labels[ $section_id ],
'formats' => $formats,
'equations' => $equations,
'mode' => $mode,
'data' => $data[ $section_id ],
'event' => $event,
@@ -255,6 +271,7 @@ if ( is_array( $teams ) ):
'caption' => $team_id ? sp_get_team_name( $team_id, $abbreviate_teams ) : null,
'labels' => $labels,
'formats' => $formats,
'equations' => $equations,
'mode' => $mode,
'data' => $data,
'event' => $event,