Show ranking position in Statistics page / prediction of a game
-
In the statistics page showing the predictions for a game: “statistics/?view=matchpredictions&match=15”, I’d like to add a column to the left of the username showing their rank overall. In addition, I’d like to sort the predictions by this column.
What would be the best way to do this?
Thank you!
Giovanni
-
This data set has a filter that you can use. I have a plugin that adds the score to this data set and then orders on the score.
You can rewrite this plugin to not add the score, but the ranking of the user and then sort on that ‘column’ in the data set. Then you have the sorting done.And this view in the plugin also has a template that you can overwrite with the following hook:
footballpool_matchpredictions_row_template
. The ranking that you added to the data set, can also be used in this template if you add it to the template.You can get inspiration from the function
shortcode_user_ranking()
in the shortcode class to determine the ranking for a user.oh man – i’ve been at it for a few hours and my object orienting programming is very rusty. =(
any help would be appreciated!!!
ok! i got it to work!!!
<?php /** * Plugin Name: Football Pool Match Predictions Extension with Ranking * Description: Order match predictions by ranking * Version: 2.1 * Author: Antoine Hurkmans + Giovanni Dubois * Author URI: mailto:[email protected] * License: MIT */ // Save this plugin in the wp-content/plugins folder and activate it // class FootballPoolExtensionMatchPredictions { public static function init_extension() { if ( ! is_admin() ) { add_filter( 'footballpool_matchpredictions_template_start', array( __CLASS__, 'add_ranking_column_template_start'), null, 2); add_filter( 'footballpool_matchpredictions_row_template', array( __CLASS__, 'add_ranking_column_template_row'), null, 2); add_filter( 'footballpool_matchpredictions_row_params', array( __CLASS__, 'add_ranking_param'), null, 3); add_filter( 'footballpool_statistics_matchpredictions', array( __CLASS__, 'change_matchpredictions' ), null, 2 ); } } public static function change_matchpredictions( $predictions, $match_info ) { // add score & ranking array_walk( $predictions, array( __CLASS__, 'add_ranking' ), $match_info ); // remove users without predictions $predictions = array_filter( $predictions, array( __CLASS__, 'remove_inactive_users' ) ); // sort by ranking usort( $predictions, array( __CLASS__, 'sort_table' ) ); return $predictions; } public static function add_ranking( &$prediction, $key, $match_info ) { $pool = new Football_Pool_Pool; //$prediction['rank'] = $pool->get_user_rank( $prediction['user_id'], FOOTBALLPOOL_RANKING_DEFAULT , date("Y-m-d H:i:s") ); $prediction['score'] = $pool->calc_score( $match_info['home_score'], $match_info['away_score'], $prediction['home_score'], $prediction['away_score'], $prediction['has_joker'], $match_info['id'], $prediction['user_id'] ); } public static function add_ranking_column_template_row ( $row_template , $match_info ) { $pool = new Football_Pool_Pool(); $row_template = '<tr> <td class="ranking" style="text-align: center;">%rank%</td> <td><a href="%user_url%">%user_name%</a></td> <td class="home">%home_score%</td> <td class="match-hyphen">-</td> <td class="away">%away_score%</td>'; if ( $pool->has_jokers ) { $row_template .= '<td title="%joker_title_text%"><span class="nopointer %joker_css_class%"></span></td>'; } $row_template .= '<td class="score">%score%</td></tr>'; return $row_template; } public static function add_ranking_column_template_start ( $template_start , $match_info ){ $pool = new Football_Pool_Pool(); $template_start = sprintf( '<table class="matchinfo statistics"> <tr><th class="ranking">%s</th><th class="username">%s</th> <th colspan="%d">%s</th><th>%s</th></tr>', __( 'rank', 'football-pool'), __( 'name', 'football-pool' ), ( $pool->has_jokers ? 4 : 3 ), __( 'prediction', 'football-pool' ), __( 'score', 'football-pool' ) ); return $template_start; } public static function add_ranking_param ( $row_params, $match_info, $user_id ){ $pool = new Football_Pool_Pool(); $row_params['rank'] = $pool->get_user_rank( $user_id, FOOTBALLPOOL_RANKING_DEFAULT , date("Y-m-d H:i:s") ); return $row_params; } public static function remove_inactive_users( $prediction ) { return ! is_null( $prediction['home_score'] ) && ! is_null( $prediction['away_score'] ); } public static function sort_table( $a, $b ) { if ( $a['score'] == $b['score'] ) return 0; return ( $a['score'] < $b['score'] ) ? 1 : -1; } } add_filter( 'plugins_loaded', array( 'FootballPoolExtensionMatchPredictions', 'init_extension' ) );
it’s dirty, but it works! ?? any feedback appreciated!
thing i’m having issues with is sorting the ranking, but at least i can show the ranking now!
fixed!
attached cleaner version:
<?php /** * Plugin Name: Football Pool Match Predictions Extension with Ranking * Description: Display user ranking and order match predictions by ranking * Version: 2.1 * Author: Antoine Hurkmans + Giovanni Dubois * Author URI: mailto:[email protected] * License: MIT */ // Save this plugin in the wp-content/plugins folder and activate it // class FootballPoolExtensionMatchPredictions { public static function init_extension() { if ( ! is_admin() ) { add_filter( 'footballpool_matchpredictions_template_start', array( __CLASS__, 'add_ranking_column_template_start'), null, 2); add_filter( 'footballpool_matchpredictions_row_template', array( __CLASS__, 'add_ranking_column_template_row'), null, 2); add_filter( 'footballpool_matchpredictions_row_params', array( __CLASS__, 'add_ranking_param'), null, 3); add_filter( 'footballpool_statistics_matchpredictions', array( __CLASS__, 'change_matchpredictions' ), null, 2 ); } } public static function change_matchpredictions( $predictions, $match_info ) { // add score & ranking array_walk( $predictions, array( __CLASS__, 'add_ranking' ), $match_info ); // remove users without predictions $predictions = array_filter( $predictions, array( __CLASS__, 'remove_inactive_users' ) ); // sort by ranking usort( $predictions, array( __CLASS__, 'sort_table' ) ); return $predictions; } public static function add_ranking( &$prediction, $key, $match_info ) { $pool = new Football_Pool_Pool; $prediction['rank'] = $pool->get_user_rank( $prediction['user_id'], FOOTBALLPOOL_RANKING_DEFAULT , date("Y-m-d H:i:s") ); $prediction['score'] = $pool->calc_score( $match_info['home_score'], $match_info['away_score'], $prediction['home_score'], $prediction['away_score'], $prediction['has_joker'], $match_info['id'], $prediction['user_id'] ); } public static function add_ranking_column_template_row ( $row_template , $match_info ) { $pool = new Football_Pool_Pool(); $row_template = '<tr> <td class="ranking" style="text-align: center;">%rank%</td> <td><a href="%user_url%">%user_name%</a></td> <td class="home">%home_score%</td> <td class="match-hyphen">-</td> <td class="away">%away_score%</td>'; if ( $pool->has_jokers ) { $row_template .= '<td title="%joker_title_text%"><span class="nopointer %joker_css_class%"></span></td>'; } $row_template .= '<td class="score">%score%</td></tr>'; return $row_template; } public static function add_ranking_column_template_start ( $template_start , $match_info ){ $pool = new Football_Pool_Pool(); $template_start = sprintf( '<table class="matchinfo statistics"> <tr><th class="ranking">%s</th><th class="username">%s</th> <th colspan="%d">%s</th><th>%s</th></tr>', __( 'rank', 'football-pool'), __( 'name', 'football-pool' ), ( $pool->has_jokers ? 4 : 3 ), __( 'prediction', 'football-pool' ), __( 'score', 'football-pool' ) ); return $template_start; } public static function add_ranking_param ( $row_params, $match_info, $user_id ){ $pool = new Football_Pool_Pool(); $row_params['rank'] = $pool->get_user_rank( $user_id, FOOTBALLPOOL_RANKING_DEFAULT , date("Y-m-d H:i:s") ); return $row_params; } public static function remove_inactive_users( $prediction ) { return ! is_null( $prediction['home_score'] ) && ! is_null( $prediction['away_score'] ); } public static function sort_table( $a, $b ) { if ( $a['rank'] == $b['rank'] ) return 0; return ( $a['rank'] > $b['rank'] ) ? 1 : -1; } } add_filter( 'plugins_loaded', array( 'FootballPoolExtensionMatchPredictions', 'init_extension' ) );
ah — i noticed that by activating this plugin, the logged in user’s row is no longer highlighted, how can i do that?
thanks!
First of all: great job! And thanks for sharing.
Second: Only feedback that comes to mind, is if performance becomes an issue, then you may consider not doing the ranking lookup twice (once for the ordering and once for the template params). You can cache these lookups and use them later. Or get all rankings in one go (instead of per user), store them in an array and do lookups from the array.
But if it performs OK, then don’t bother ??Third:
For the highlighting of the current user you can add another param to the row that sets a css class. And then add that param also to the template.Something like:
public static function add_ranking_column_template_row ( $row_template , $match_info ) { $pool = new Football_Pool_Pool(); $row_template = '<tr class="%current_user_css%"> <td class="ranking" style="text-align: center;">%rank%</td> <td><a href="%user_url%">%user_name%</a></td> <td class="home">%home_score%</td> <td class="match-hyphen">-</td> <td class="away">%away_score%</td>'; if ( $pool->has_jokers ) { $row_template .= '<td title="%joker_title_text%"><span class="nopointer %joker_css_class%"></span></td>'; } $row_template .= '<td class="score">%score%</td></tr>'; return $row_template; } public static function add_ranking_param ( $row_params, $match_info, $user_id ){ $pool = new Football_Pool_Pool(); $row_params['rank'] = $pool->get_user_rank( $user_id, FOOTBALLPOOL_RANKING_DEFAULT , date("Y-m-d H:i:s") ); $row_params['current_user_css'] = ( $user_id == get_current_user_id() ? 'current-user' : '' ); return $row_params; }
I just added it to your existing functions and didn’t test ??
HTH
Awesome thanks!! Will test soon!!
An additional question, what’s the best way to get the user points? I’ve received request to display the points as well next to rank.
Where did they info exist?
Thanks!
You want the total score I presume, not the points for this match, because that is already there. Football_Pool_Pool class also has a function for user score:
$score = $pool->get_user_score( $user_id, FOOTBALLPOOL_RANKING_DEFAULT , date("Y-m-d H:i:s") ); if ( $score === null ) $score = '??'; // or pass something else to the params
btw. I do have some feedback on the extension. I think it makes more sense to get the ranking at the time of the match and not current time. Otherwise you will get the ranking and ordering the same on all match prediction pages when you are some days ahead in the tournament. The date of the match is part of the match_info array.
Same applies to the user score, of course.thanks! i tried this but it returns blanks:
$row_params['rank'] = $pool->get_user_rank( $user_id, FOOTBALLPOOL_RANKING_DEFAULT , date("Y-m-d H:i:s", $match_info['match_datetime']) ); $row_params['points'] = $pool->get_user_score( $user_id, FOOTBALLPOOL_RANKING_DEFAULT , date("Y-m-d H:i:s"),$match_info['match_datetime']);
what am i missing?
figured it out – timestamp. =)
something interesting though is that using the timestamp # it gives me the ranking as of the time of the match and it includes the points for that specific match.
- The topic ‘Show ranking position in Statistics page / prediction of a game’ is closed to new replies.