5

I am building a community site, in Drupal 7, and using the User Points module and the User Badges module.

My goal is that every user will always have only one badge, that will be presented near the user's name, everywhere on the website.

These are my specifications for the badge to be selected according to the amount of User Points:

  • 0-50 points = first badge
  • 51-100 points = second badge
  • 101-200 points = Third badge
  • 201-300 points = Forth badge
  • 301 points and more = Fifth badge

I tried to use User Points Contributed modules but unfortunately there is no Drupal 7 upgrade.

I tried using the Rules module, but didn't find a way to compare the current total amount of points of the user to my specifications, (for Rules Event "User was awarded points"), so that based on the result of that comparison, the Rules Action would eventually change the badge assigned to the user.

Any suggestion to get this to work?

  • Are you sure it wasn't answered in any of these questions? – Mołot Dec 27 '13 at 15:18
  • I saw all these questions. that is why wrote I tried to use User Points Contributed modules but it's not working on Drupal 7. In the question with the php, it's not a suitable solution for me, as I will have many badges, and I will always need to assign different badge to different user according to the amount of points (I updated the question). I will need to calculate the amount of current total points and compare to all the five steps. maybe is possible to adjust the php (which I am not sure how), but maybe there is a better way? – EB84 Dec 27 '13 at 17:05
  • Could the Achievements module be what you are looking for? There's also an in-depth discussion of your problem here. – jamix Dec 30 '13 at 17:51
  • I think this is a possible duplicate of drupal.stackexchange.com/q/83640/16550. – Jeroen Feb 28 '14 at 12:40
  • @jamix How would you do this using the Achievements module? – Pierre.Vriens Oct 30 '16 at 9:58
2

The key part of your question seems to be this:

I tried using the Rules module, but didn't find a way to compare the current total amount of points of the user to my specifications, (for Rules Event "User was awarded points"), so that based on the result of that comparison, the Rules Action would eventually change the badge assigned to the user.

Indeed, for Drupal 7, there doesn't seem to be some Rules Condition related to the current total user points for a user. Except by using custom PHP code as in this example (which you could use in a Rules Conditionn Execute custom PHP code condition, e.g. to do some type of comparison):

global $user;
$current_points = userpoints_get_current_points($user->uid, 'all');
if ($current_points < 0) {return TRUE;}

However, there is a Rules Action "Load user points of a user", whereas you can ask for either a specific category, or for all categories together. This Rules Action provides a Rules variable (for which you can specify its name and description) which contains the amount of user points for the requested category. You can then use this variable in subsequent Rules Actions.

If you then want to use that variable within a Rules Condition, you have 2 ways of doing so:

  • Include the condition in a Rules Component, whereas you trigger the execution of that Rules Component via a Rules Action following the "Load user points of a user" (and passing the variable containing the amount of user points as a variable for the Rules Component).
  • Use the Conditional Rules module, so that you can specify Rules Conditions within Rules Actions.

Here is a basic example that displays a message about the current total user points for a user, after the user performed an update of some node (= the Rules Event, which you can change to any Rules event that you want, e.g. to "User was awarded points", as in your question):

{ "rules_display_userpoints_after_updating_content" : {
    "LABEL" : "Display userpoints after updating content",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "userpoints_rules", "rules" ],
    "ON" : { "node_update" : [] },
    "DO" : [
      { "userpoints_rules_get_current_points" : {
          "USING" : { "user" : [ "site:current-user" ], "tid" : "all" },
          "PROVIDE" : { "loaded_points" : { "total_points" : "Number of points in all categories together" } }
        }
      },
      { "drupal_message" : { "message" : "You now have [total-points:value] points" } }
    ]
  }
}

Use the Rules UI to import the above rule in your own site if you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.