Add equations to event results
This commit is contained in:
@@ -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' );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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',
|
||||
),
|
||||
|
||||
@@ -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 ) {
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
/**
|
||||
* Result Equation
|
||||
*
|
||||
* @author ThemeBoy
|
||||
* @category Admin
|
||||
* @package SportsPress/Admin/Meta_Boxes
|
||||
* @version 1.9
|
||||
*/
|
||||
|
||||
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_Result_Equation
|
||||
*/
|
||||
class SP_Meta_Box_Result_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 );
|
||||
}
|
||||
}
|
||||
@@ -84,6 +84,8 @@
|
||||
<th class="radio" scope="col"><?php _e( 'Primary', 'sportspress' ); ?></th>
|
||||
<th scope="col"><?php _e( 'Label', 'sportspress' ); ?></th>
|
||||
<th scope="col"><?php _e( 'Variables', 'sportspress' ); ?></th>
|
||||
<th scope="col"><?php _e( 'Equation', 'sportspress' ); ?></th>
|
||||
<th scope="col"><?php _e( 'Decimal Places', 'sportspress' ); ?></th>
|
||||
<th scope="col"><?php _e( 'Description', 'sportspress' ); ?></th>
|
||||
<th scope="col" class="edit"></th>
|
||||
</tr>
|
||||
@@ -91,7 +93,7 @@
|
||||
<tfoot>
|
||||
<tr>
|
||||
<th class="radio"><input type="radio" class="sp-primary-result-option" id="sportspress_primary_result_0" name="sportspress_primary_result" value="0" <?php checked( $selection, 0 ); ?>></th>
|
||||
<th colspan="4"><label for="sportspress_primary_result_0">
|
||||
<th colspan="6"><label for="sportspress_primary_result_0">
|
||||
<?php
|
||||
if ( sizeof( $data ) > 0 ):
|
||||
$default = end( $data );
|
||||
@@ -109,12 +111,14 @@
|
||||
<td class="radio"><input type="radio" class="sp-primary-result-option" id="sportspress_primary_result_<?php echo $row->post_name; ?>" name="sportspress_primary_result" value="<?php echo $row->post_name; ?>" <?php checked( $selection, $row->post_name ); ?>></td>
|
||||
<td class="row-title"><label for="sportspress_primary_result_<?php echo $row->post_name; ?>"><?php echo $row->post_title; ?></label></td>
|
||||
<td><code><?php echo $row->post_name; ?>for</code>, <code><?php echo $row->post_name; ?>against</code></td>
|
||||
<td><?php echo sp_get_post_equation( $row->ID ); ?></td>
|
||||
<td><?php echo sp_get_post_precision( $row->ID ); ?></td>
|
||||
<td><p class="description"><?php echo $row->post_excerpt; ?></p></td>
|
||||
<td class="edit"><a class="button" href="<?php echo get_edit_post_link( $row->ID ); ?>"><?php _e( 'Edit', 'sportspress' ); ?></s></td>
|
||||
</tr>
|
||||
<?php $i++; endforeach; else: ?>
|
||||
<tr class="alternate">
|
||||
<td colspan="5"><?php _e( 'No results found.', 'sportspress' ); ?></td>
|
||||
<td colspan="7"><?php _e( 'No results found.', 'sportspress' ); ?></td>
|
||||
</tr>
|
||||
<?php endif; ?>
|
||||
</table>
|
||||
@@ -236,7 +240,6 @@
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col"><?php _e( 'Label', 'sportspress' ); ?></th>
|
||||
<th scope="col"><?php _e( 'Key', 'sportspress' ); ?></th>
|
||||
<th scope="col"><?php _e( 'Equation', 'sportspress' ); ?></th>
|
||||
<th scope="col"><?php _e( 'Decimal Places', 'sportspress' ); ?></th>
|
||||
<th scope="col"><?php _e( 'Sort Order', 'sportspress' ); ?></th>
|
||||
@@ -247,7 +250,6 @@
|
||||
<?php if ( $data ): $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 $row->post_name; ?></td>
|
||||
<td><?php echo sp_get_post_equation( $row->ID ); ?></td>
|
||||
<td><?php echo sp_get_post_precision( $row->ID ); ?></td>
|
||||
<td><?php echo sp_get_post_order( $row->ID ); ?></td>
|
||||
@@ -341,7 +343,6 @@
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col"><?php _e( 'Label', 'sportspress' ); ?></th>
|
||||
<th scope="col"><?php _e( 'Key', 'sportspress' ); ?></th>
|
||||
<th scope="col"><?php _e( 'Equation', 'sportspress' ); ?></th>
|
||||
<th scope="col"><?php _e( 'Decimal Places', 'sportspress' ); ?></th>
|
||||
<th scope="col"><?php _e( 'Description', 'sportspress' ); ?></th>
|
||||
@@ -351,7 +352,6 @@
|
||||
<?php if ( $data ): $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 $row->post_name; ?></td>
|
||||
<td><?php echo sp_get_post_equation( $row->ID ); ?></td>
|
||||
<td><?php echo sp_get_post_precision( $row->ID ); ?></td>
|
||||
<td><p class="description"><?php echo $row->post_excerpt; ?></p></td>
|
||||
|
||||
Reference in New Issue
Block a user