Nick Lewis wrote about his 40+ essential modules, and I really missed two modules there - Rules and Flag. I commented that
My top 5 are: Views, CCK, Flag, Rules, Panels. I think that the Flag & Rules combo is less known in the community, but there are so many use cases they can cover. Because they are a bit abstracted it's hard for newbies to realize their potential.
I think there’s no better way to show my point, than a tutorial. Lazy people can even download the feature from here. This tutorial will be about having “User points” functionality without using the user points module. I’m not saying that module is bad, but sometimes I need multiple user points per user. Also, re-using existing modules always feels me with great joy. GREAT joy! Also, thinking about Drupal 7 with fields API, suddenly _user points can become _any-fieldable-entity points… The tutorial will cover the “Rule sets” concept in Rules module, which is also considered by many as a great mystery. Let’s define our mission:
If your eyes no longer see the above list as words, but as Drupal modules, then you probably think CCK, Content profile, Rules and Flag. We will add a content profile to each user. That profile will have a CCK field that will hold the user points. When user points reach zero, a “story editor” role will be removed from the user - denying access to creating new story content. For “reseting” the user points, we will use a flag on the user, that a site admin can click on. First preparations:
Before we set up our logic using Rules Let’s go over a bit about the concept of “Rule sets”. You should think of a rule sets as an API function. That function gets arguments and processes them. It’s up to an implementing module to use the API function properly. Like API functions rule sets are there in order to prevent duplication and to centralize actions. Let’s make the idea clear by thinking about points 1 and 2 in our mission. We have here two rules - one will deduct points from the user’s profile, and the other should remove the “story editor” role.
We could create two triggered rules both with the event “After saving new content”, and check in both rules that the content type is story. As you can see, there’s a duplication in the event and in the conditions. If our logic will change and be more complex the duplication might increase.
So, for such a case we will use the rule sets. We’ll define two arguments that are mandatory one is a node, in which we’ll pass the content profile. The other is the user. If you are a developer then think of it as _function user_points_logic($profile_content, $account).
In this rule sets we will add our two rules without even bothering to check if the new created content is indeed “story”. For that we’ll have a third rule, that will not be a rule inside a rule sets, but a triggered rule. This rule will be executed “After saving new content”, will check the condition of the content type is story, and then willload the user’s profile content and invoke our rule sets.
In terms of code, think of it as calling _user_points_logic() from _hook_nodeapi() upon node insert. Now that the concept should be clear, let’s see how we do it:
The implementation of “reset” points and grant points on user registration is similar in concept to our previous mission. We create a rule sets labeled “User points grant” which will set the field value to 3 and grant “story editor” role. This rule sets will be called from two different triggered rules - once upon content profile creation, and second when a site admin will “reset” the user points. How will the site admin “reset”? Easily - we’ll create a new user type flag called “Reset user points”, and in Rules create a triggered rule that is invoked on the event _A user has been flagged, under “Reset user points”.
That’s it. Download the feature, and add your own logic.
– Notice if you download the modules manually to use Flag module with version 6.x-2.x or higher and strongarm version 6.x-2.x or higher.