Hide other Leagues
-
Hi there
Is there a option to hide other leagues where the user is not part of? So he is only able to see the members and the ranking of his league?
(= every member is only able to see the assigned league)
Thanks
Dominik
-
Not sure if you can completely hide all other users. For the ranking you can use the [fp-ranking] shortcode with the value “user” for league. That will displays the ranking for the logged in user.
The statistics page has an extension to only show users from your own league in the chart settings (if you enabled the charts). But not sure if there is a fallback for non-authenticated users. If so, you might have to prevent users from accessing those pages with another plugin. And I think you can still enter other user ID’s via the querystring.For some other parts of the plugin, e.g. the user predictions page, you’ll have to check if you can filter out users. There are a lot of options in extending the plugin, but – as said – I don’t know if you’re able to filter out everything.
Hi,
Related to this topic, I want to hide users from other leagues in the user’s prediction page. Can the “football-pool-user-selector-own-league-only” extension be adapted to do this? I’m not an expert in php but I can try!The user selector works different from the user prediction page. So, you’ll have to find another way to filter the users on that page. I think you can hook into the
footballpool_statistics_matchpredictions
filter. But the data structure is different, so you’ll have to come up with a way to filter that array.You can, of course, use the other extension to see which function(s) you can use to determine the users in a league.
Thank you Antonie,
I’m trying to find a solution for this but, please, I need your help. I’m thinking that maybe I can work over the extension “football-pool-matchpredictions-order” where you use the function “remove_inactive_users”. I’m trying to add the function filter_users( $users ) which returns the users array for a specific league but, indeed the data structure is different. How does the $prediction parameter work?. For instance you are using this parameter like this: $prediction[‘score’] = $pool->calc_score… Can it be used with the $users array from the mentioned function? Please, If I get this working I will go right away to the donation page ??
Best Regards!
The predictions array is just a big array of the rows that you see on that page. Plus some extra info. And luckily for you, it also holds the league for the user, so you can filter the array quite easily.
What really helps, is when you just output the var to a file or to the screen. Then you see the data structure that you have to work with. Below the structure of the
$predictions
array:array ( 0 => array ( 'home_team_id' => '1', 'away_team_id' => '2', 'home_score' => '1', 'away_score' => '2', 'has_joker' => '0', 'user_id' => '67', 'user_name' => 'Albert', 'league_id' => '3', ), 1 => array ( 'home_team_id' => '1', 'away_team_id' => '2', 'home_score' => '1', 'away_score' => '2', 'has_joker' => '0', 'user_id' => '72', 'user_name' => 'Andrew', 'league_id' => '3', ), ... etc ... )
What my extension also does, is add the score to the predictions array, so it can be used to sort the array on. So, after my extension is done, the
$predictions
array looks like this:array ( 0 => array ( 'home_team_id' => '1', 'away_team_id' => '2', 'home_score' => '1', 'away_score' => '2', 'has_joker' => '0', 'user_id' => '67', 'user_name' => 'Albert', 'league_id' => '3', 'score' => array ( 'score' => 0, 'full' => 0, 'toto' => 0, 'goal_bonus' => 1, 'goal_diff_bonus' => 0, ), ), ... etc ... )
Not really important for you, but it just shows what the add_score function is for. Again, all you have to do, is filter out the array elements with league Id’s other than the one you are looking for. In short, this is as simple as this:
// remove users from other leagues $predictions = array_filter( $predictions, function( $p ) { return (int) $p['league_id'] === 2; } );
Where the number 2 should be replaced with the league of the user that is visiting the page.
Hope this makes sense.
p.s. I noticed on my dev install that the extension is kinda old and is not working on the newest version of the plugin. I had to change the
add_score()
function to this:public static function add_score( &$prediction, $key, $match_info ) { $pool = new Football_Pool_Pool; $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'] ); }
Thank you very much Antonie! It works perfectly!!
Now I need to do the same for the bonus questions predictions page. Where should I start from?
Look up the code that displays this data and look for a filter that you can use. If there is one, then you can use it. If not, then it’s not possible to extend the plugin on that point.
I did this lookup for you, the filter is
footballpool_statistics_bonusquestion
. Now hook into this filter to return a changed data set.As said earlier, I would normally first print the input parameter for the filter to see what I am working with:
array ( 0 => array ( 'user_id' => '67', 'name' => 'Albert', 'answer' => NULL, 'correct' => NULL, 'points' => NULL, ), 1 => array ( 'user_id' => '72', 'name' => 'Andrew', 'answer' => NULL, 'correct' => NULL, 'points' => NULL, ), ...etc... )
Now think of a way to filter this array. Two options come to mind:
1. First add the league number for every user in the array and then filter out the ones that are not in the league of the user. Almost the same approach as you already have for the matches.
or
2. Create an array with all the user Id’s in the current user’s league and use that array to filter the other array.
Got it working! Thank you Antonie. Now I’m stuck in the code that was supposed to be the easiest one… filtering the ranking. I’m using the following shortcode:
[fp-ranking league=”user” num=”50″ date=”now”]
And it isn’t working… it keeps showing all the registered users.
Please your comments.
I tried it in my install and it works as expected. Do you have any extensions enabled that might interfere with the correct working of the shortcode? Or another plugin that is causing trouble (e.g. caching).
Can you explain the code to get the current users league-id into the code snipped you shared? I tried with the code from the other plugin, but I could not get it to work.
Or @fegs1204 do you have your plugins working? Could you please share them?// remove users from other leagues $predictions = array_filter( $predictions, function( $p ) { return (int) $p['league_id'] === <strong>this code</strong>; } );
League of the logged on user:
$user_id = get_current_user_id(); $pool = new Football_Pool_Pool(); $league = $pool->get_league_for_user( $user_id );
May need some checks for correct values.
Thanks @antoineh but if I do this i can`t see any tips in the list.
public static function change_matchpredictions( $predictions, $match_info ) { // add score array_walk( $predictions, array( __CLASS__, 'add_score' ), $match_info ); $user_id = get_current_user_id(); $pool = new Football_Pool_Pool(); $league = $pool->get_league_for_user( $user_id ); // remove users from other leagues $predictions = array_filter( $predictions, function( $p ) { return (int) $p['league_id'] === ( $league ); } ); // sort by score usort( $predictions, array( __CLASS__, 'sort_table' ) ); return $predictions; }
@mastermax12 this is how I did it:
class FootballPoolExtensionMatchPredictions {
public static function init_extension() {
if ( ! is_admin() ) {
add_filter( ‘footballpool_statistics_matchpredictions’, array( __CLASS__, ‘change_matchpredictions’ ), null, 2 );
}
}public static function change_matchpredictions( $predictions, $match_info ) {
// add score
array_walk( $predictions, array( __CLASS__, ‘add_score’ ), $match_info );
// remove users without predictions
//$predictions = array_filter( $predictions, array( __CLASS__, ‘remove_inactive_users’ ) );
// sort by score
usort( $predictions, array( __CLASS__, ‘sort_table’ ) );// remove users from other leagues
$predictions = array_filter( $predictions, array( __CLASS__, ‘Filter_user_league’ ) );return $predictions;
}public static function Filter_user_league ( $p ) {
$user_id = get_current_user_id();
if ( $user_id > 0 ) {
$pool = new Football_Pool_Pool();
$league_name = $pool->get_league_for_user( $user_id );
}return (int) $p[‘league_id’] === $league_name;
}public static function add_score( &$prediction, $key, $match_info ) {
$pool = new Football_Pool_Pool;
$prediction[‘score’] = $pool->calc_score(
$match_info[‘home_score’],
$match_info[‘away_score’],
$prediction[‘home_score’],
$prediction[‘away_score’],
$prediction[‘has_joker’],
null
);
}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;
}}
Best Regards!
@fegs1204 I tried your code from the last post but I only receive the Chart on my display, no list at all.
Did I missed something?
Best
GerritThat should work @gerrit_bes
<?php /** * Plugin Name: Football Pool Match Predictions Extension * Description: Order match predictions by score and don't show users without predictions. * Version: 2.0 * Author: Antoine Hurkmans * 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_statistics_matchpredictions', array( __CLASS__, 'change_matchpredictions' ), null, 2 ); } } public static function change_matchpredictions( $predictions, $match_info ) { // add score array_walk( $predictions, array( __CLASS__, 'add_score' ), $match_info ); // remove users from other leagues //$predictions = array_filter( $predictions, function( $p ) { //return (int) $p['league_id'] === 5; //} ); $predictions = array_filter( $predictions, array( __CLASS__, 'Filter_user_league' ) ); // remove users without predictions // $predictions = array_filter( $predictions, array( __CLASS__, 'remove_inactive_users' ) ); // sort by score usort( $predictions, array( __CLASS__, 'sort_table' ) ); return $predictions; } public static function Filter_user_league ( $p ) { $user_id = get_current_user_id(); if ( $user_id > 0 ) { $pool = new Football_Pool_Pool(); $league_name = $pool->get_league_for_user( $user_id ); } return (int) $p['league_id'] === $league_name; } public static function add_score( &$prediction, $key, $match_info ) { $pool = new Football_Pool_Pool; $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 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' ) );
@fegs1204 do you have code for the questions, too? (Thanks for the other code btw ?? )
- The topic ‘Hide other Leagues’ is closed to new replies.