Ranking by points accumulated not working for me
-
Hi there,
Great plugin. Just having one problem. I want users to be ranked by total points accumulated over all time.
I have selected the option
“Users are ranked according to the total amount of yebo credits they have accumulated.”
but users are still ranked by their *balance* and not the total.
I’ve tried uninstalling and reinstalling. I’ve clicked on the ‘calculate totals’ button but still ranks are calculated by balance and not total.
Hope you can help.
-
Hey.
When you select to rank users according to their total accumulated over time you will start off with your current balance. But you will notice that if you start using your points, the total accumulated will still remain the same.
This is true especially with new installations as a users current balance is what we have as their current total gain.
Example:
John gains 1 point each day and by day 30 have 30 points.
At this point, Johns total gains is 30.
John then spends these 30 points in the store so now his balance is 0.
His rank would not change at this stage since his total gain is still 30 since we only count point that are given to a user not taken.So if you install myCRED and everyone starts off at 1 point, everyones gain at that point is 1 and with that have the rank that corresponds to 1 point.
If you have been using myCRED and first now enable this feature, all users gains will be counted from the moment you enable this feature and not retroactively.
Thanks for getting back to me. I understand that’s how it should work but that’s not what’s happening for me.
I set up two ranks for testing this out
0-20 newbie
21-9999999 fledglingI had a user on 26 points. His rank was fledgling which is what I expected. Then I bought access to a post for 20 points. His balance went down to 6 but also his rank changed from fledgling to newbie. If it was working as explained above then surely his rank should have stayed as fledgling?
I double checked that I have the option ticked for
“Users are ranked according to the total amount of yebo credits they have accumulated.”
Does it make any difference if the user above got his points from another user using the transfer widget?
Sorry to be a nuisance. I know it’s hard to debug without seeing my setup. Any other ideas of things to try?
Ok so in the myCRED-addon-ranks.php file which is located in plugins/mycred/addons/ranks/, you will find the following code starting on line 356:
// Ranks are based on total else { // Update total $new_total = mycred_update_users_total( $request, $mycred ); // Get total before update $total_before = $this->core->number( $new_total-$request['amount'] ); // Make sure update went well and that there has been an actual increase if ( $new_total !== false && $new_total != $total_before ) mycred_find_users_rank( $request['user_id'], true, $request['amount'], $this->core->get_cred_id() . '_total' ); }
Change this to:
// Ranks are based on total elseif ( $request['amount'] > 0 ) { // Update total $new_total = mycred_update_users_total( '', $request, $mycred ); mycred_find_users_rank( $request['user_id'], true, 0, $this->core->get_cred_id() . '_total' ); }
This way, the ranks add-on should only check for new ranks if a user is gaining points (higher then zero).
Sorry for troubling you again. I edited the code as you suggested but it’s still not behaving how I would expect.
I was able to get the correct total points to show by editing
public function get_users_cred( $user_id = '', $type = '' ) { if ( empty( $user_id ) ) return $this->zero(); if ( empty( $type ) ) $type = $this->get_cred_id(); //I added this line $type = 'mycred_default_total'; $balance = get_user_meta( $user_id, $type, true ); if ( empty( $balance ) ) $balance = $this->zero(); ...
But then I realised that this will not work because
– Users can transfer points back and forth between each other as a way of generating free points.
– When forum posts are trashed, points are not deducted from mycred_default_total which means that users could post lots of junk replies and not lose those points when their posts were trashed.
– The leaderboard widget seems to use some other function to calculate the balance because even with the edit above it always shows the balance (not total points earned).
I don’t think it’s a case of only adding the +ve points. In my case to calculate the proper cumulative total, I would need to add up all points (+ve and -ve) apart from points used to pay for content.
Basically I’m building an educational site where users are rewarded with points for getting involved (forum posts, adding comments, submitting content etc). They can then use those points to access educational resources. I want to show the total points earned so people can see which users have contributed the most to the site.
Hope this makes sense. Maybe I am misunderstanding something.
Hey.
The change you made will force myCRED to always get users total creds which means that people will have zero or incorrect balances. You should REMOVE this fix or you will break myCRED and I will not be able to help you.
The rank by total points needs to be improved and I need to find a better way to calculate the total points since as you mentioned if someone just gains points for all kinds of things i.e. SPAM comments or post forums, then the rank will never change even if you adjust his balance.
Alternativily, one could query the log and put together all positive point gains then remove any adjustments i.e. forum removals or manual point deductions in order to try and get a total.
I think I will change the total counter to one that queries the log and ignores any purchases done with points, getting a total that way. This would allow you to still adjust the users balance.
Thanks for replying again. Awesome support ??
Yes, I knew the edit I did yesterday wasn’t a permanent solution. I was just trying a few things out.
It would be great if you could make the changes suggested above. It would be nice to have a neat solution all within mycred.
In the meantime, I came up with a solution that works for me right now. I basically use the standard rank by balance option in mycred. I then added a mycred_add filter to functions.php where I keep track of mycred_total_spent in the user meta table. In bbpress / buddypress or wherever I show the rank I just add the mycred_total_spent to the balance before displaying it. I also added a function to get the mycred rank from the points value so I can get the correct rank for the (balance + mycred_total_spent). Seems to be working how I want it now and no edits to mycred needed which is great.
Thanks for the great plugin and also responding to my questions. Appreciate your effort.
As I said I will look into this and come up with a better solution for 1.3.3 which is due December 1st.
In the meanwhile, maybe you would be interested in sharing your filter solution? Maybe there are other myCRED members with the same issue who could use your solution in the meantime.
ok…
I added this to functions.php
add_filter( 'mycred_add', 'my_mycred_track_spending', 10, 3 ); function my_mycred_track_spending( $reply, $request, $mycred ) { if ( $request['ref'] == 'buy_content' ) { $oldAmount = get_user_meta($request['user_id'], 'mycred_total_spent', true ); update_user_meta( $request['user_id'], 'mycred_total_spent', $oldAmount - $request['amount'] ); } return $reply; } function getRank($credits){ global $wpdb; $mycred = mycred_get_settings(); $ranks = $wpdb->get_results( " SELECT rank.ID AS ID, rank.post_title AS title, min.meta_value AS min, max.meta_value AS max FROM {$wpdb->posts} rank INNER JOIN {$wpdb->postmeta} min ON ( min.post_id = rank.ID AND min.meta_key = 'mycred_rank_min' ) INNER JOIN {$wpdb->postmeta} max ON ( max.post_id = rank.ID AND max.meta_key = 'mycred_rank_max' ) WHERE post_type = 'mycred_rank' AND post_status = 'publish';", 'ARRAY_A' ); // Loop though each rank foreach ( $ranks as $rank ) { // If balance fits break with this ranks details if ( $mycred->number( $rank['min'] ) <= $credits && $mycred->number( $rank['max'] ) >= $credits ) { $rank_id = $rank['ID']; $rank_title = $rank['title']; break; } } unset( $ranks ); return $rank_title; }
Then whenever you need to show the total credits gained over all time you can do something like this…
(this is just an example I used in loop-single-reply.php to show the total points earned by the user in each bbPress forum reply)
$mycred = mycred_get_settings(); $balance = mycred_get_users_cred($reply_user_id); $spentAmount = get_user_meta($reply_user_id, 'mycred_total_spent', true ); $totalCredits = $balance + $spentAmount; echo '<div class="user-info-detail">'.$mycred->format_creds($totalCredits).' ['.getRank($totalCredits).']</div>';
- The topic ‘Ranking by points accumulated not working for me’ is closed to new replies.