From 193897a10570635627140201fb8a209ce927362c Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Sun, 20 Sep 2015 21:59:35 +1000 Subject: [PATCH] Add equations to event results --- includes/admin/class-sp-admin-assets.php | 4 +- .../post-types/class-sp-admin-meta-boxes.php | 7 ++ .../class-sp-meta-box-event-results.php | 78 +++++++++++++++++++ .../class-sp-meta-box-result-equation.php | 29 +++++++ includes/admin/views/html-admin-config.php | 12 +-- 5 files changed, 122 insertions(+), 8 deletions(-) create mode 100644 includes/admin/post-types/meta-boxes/class-sp-meta-box-result-equation.php diff --git a/includes/admin/class-sp-admin-assets.php b/includes/admin/class-sp-admin-assets.php index 1ed69ad2..b323c33b 100755 --- a/includes/admin/class-sp-admin-assets.php +++ b/includes/admin/class-sp-admin-assets.php @@ -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_column', 'sp_statistic' ) ) ) { + if ( in_array( $screen->id, array( 'sp_result', '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_column', 'sp_statistic' ) ) ) { + if ( in_array( $screen->id, array( 'sp_result', 'sp_column', 'sp_statistic' ) ) ) { wp_enqueue_script( 'sportspress-admin-equationbuilder' ); } } diff --git a/includes/admin/post-types/class-sp-admin-meta-boxes.php b/includes/admin/post-types/class-sp-admin-meta-boxes.php index ebe94698..5e2ff1d4 100644 --- a/includes/admin/post-types/class-sp-admin-meta-boxes.php +++ b/includes/admin/post-types/class-sp-admin-meta-boxes.php @@ -41,6 +41,13 @@ class SP_Admin_Meta_Boxes { 'title' => __( 'Details', 'sportspress' ), 'save' => 'SP_Meta_Box_Result_Details::save', 'output' => 'SP_Meta_Box_Result_Details::output', + 'context' => 'side', + 'priority' => 'default', + ), + 'equation' => array( + 'title' => __( 'Equation', 'sportspress' ), + 'save' => 'SP_Meta_Box_Result_Equation::save', + 'output' => 'SP_Meta_Box_Result_Equation::output', 'context' => 'normal', 'priority' => 'high', ), diff --git a/includes/admin/post-types/meta-boxes/class-sp-meta-box-event-results.php b/includes/admin/post-types/meta-boxes/class-sp-meta-box-event-results.php index 8245543c..d2f4e7cf 100644 --- a/includes/admin/post-types/meta-boxes/class-sp-meta-box-event-results.php +++ b/includes/admin/post-types/meta-boxes/class-sp-meta-box-event-results.php @@ -37,6 +37,84 @@ class SP_Meta_Box_Event_Results { $results = (array)sp_array_value( $_POST, 'sp_results', array() ); $main_result = get_option( 'sportspress_primary_result', null ); + // Get player performance + $performance = sp_array_value( $_POST, 'sp_players', array() ); + + // Initialize finished + $finished = false; + + // Check if any results are recorded + if ( ! $finished ) { + foreach ( $results as $team => $team_results ) { + foreach ( $team_results as $result ) { + if ( '' !== $result ) { + $finished = true; + break; + } + } + } + } + + // Check if any performance is recorded + if ( ! $finished ) { + foreach ( $performance as $team => $players ) { + foreach ( $players as $player => $pp ) { + if ( 0 >= $player ) continue; + foreach ( $pp as $pv ) { + if ( '' !== trim( $pv ) ) { + $finished = true; + break; + } + } + } + } + } + + if ( $finished ) { + // Get results with equations + $args = array( + 'post_type' => 'sp_result', + 'numberposts' => -1, + 'posts_per_page' => -1, + 'meta_query' => array( + array( + 'key' => 'sp_equation', + 'compare' => 'EXISTS', + ), + ), + ); + $dynamic_results = get_posts( $args ); + + $equations = array(); + $precisions = array(); + foreach ( $dynamic_results as $result ) { + $equations[ $result->post_name ] = get_post_meta( $result->ID, 'sp_equation', true ); + $precisions[ $result->post_name ] = (int) get_post_meta( $result->ID, 'sp_precision', true ); + } + + + // Apply equations to empty results + foreach ( $equations as $key => $equation ) { + if ( '' == $equation ) continue; + foreach ( $results as $team => $team_results ) { + if ( '' === sp_array_value( $team_results, $key, '' ) ) { + $totals = array(); + $players = sp_array_value( $performance, $team, array() ); + foreach ( $players as $player => $pp ) { + foreach ( $pp as $pk => $pv ) { + $value = sp_array_value( $totals, $pk, 0 ); + $value += floatval( $pv ); + $totals[ $pk ] = $value; + } + } + $totals[ 'eventsplayed' ] = 1; + $totals = apply_filters( 'sportspress_event_result_equation_vars', $totals, $performance, $team ); + $results[ $team ][ $key ] = sp_solve( $equation, $totals, sp_array_value( $precisions, $key, 0 ), '' ); + } + } + } + } + // Auto outcome $primary_results = array(); foreach ( $results as $team => $team_results ) { diff --git a/includes/admin/post-types/meta-boxes/class-sp-meta-box-result-equation.php b/includes/admin/post-types/meta-boxes/class-sp-meta-box-result-equation.php new file mode 100644 index 00000000..1d4acb8d --- /dev/null +++ b/includes/admin/post-types/meta-boxes/class-sp-meta-box-result-equation.php @@ -0,0 +1,29 @@ +ID, 'sp_equation', true ); + $groups = array( 'performance' ); + self::builder( $post->post_title, $equation, $groups ); + } +} diff --git a/includes/admin/views/html-admin-config.php b/includes/admin/views/html-admin-config.php index 16dad60b..0f418e6c 100644 --- a/includes/admin/views/html-admin-config.php +++ b/includes/admin/views/html-admin-config.php @@ -84,6 +84,8 @@ + + @@ -91,7 +93,7 @@ > -