I'm creating this issue as a follow-up to our Rules port (so we don't lose track of it). It would be great to create replacement patterns within Rules for the Userpoints transaction (including points awarded, category of points, approved or moderated, reason, expiration, and timestamp). This way, you could include all of this Userpoints-specific data within a Private Message generated by Rules (for instance).

--Ben

Comments

berdir’s picture

Component: Code: userpoints_rules/userpoints_workflow_ng » Code: userpoints API
Status: Active » Needs review
StatusFileSize
new16.03 KB

Ok, attaching an initial patch.

This basically declares tons of tokens for two categories:

- points/maxpoints tokens for user entities. This already existed but has not been ported from D6. Note that you can choose a specific category or all for example with user:points:category-1 or user:maxpoints:all but that will currently not be displayed in the rules UI because it does not display tokens hierarchically like token.module does, see #1032356-5: Add token replacement support for non-entities.

- Defines tokens for userpoints_transaction. I haven't tested everything, but that is basically how it works. That's what the initial issue was about. I've only tested that this basically works and by far not all possible tokens. So there is a lot of testing necessary. I guess the names and descriptions can use some work too.

berdir’s picture

StatusFileSize
new16.08 KB

New patch, this one should work. The other one used a wrong name for the transaction tokens.

BenK’s picture

Status: Needs review » Needs work

Just got done testing the latest patch. It's a lot of nice functionality. Just some bugs to work out.

To test, I set up a rule on the "User was awarded points" event. I set up an action to "Send a private message". And then I included all of the available tokens that were visible in the UI, plus the current points and max points that you suggested in your comments. I then used the regular "Add points" form to trigger the event.

Here were the notices I received after submitting the "Add points" form:

Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 178 of /Users/benkaplan/git/drupal-7.0/includes/entity.inc).
Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->cacheGet() (line 354 of /Users/benkaplan/git/drupal-7.0/includes/entity.inc).
Notice: Undefined variable: time in _userpoints_transaction() (line 904 of /Users/benkaplan/git/drupal-7.0/sites/all/modules/userpoints/userpoints.module).
Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 178 of /Users/benkaplan/git/drupal-7.0/includes/entity.inc).
Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->cacheGet() (line 354 of /Users/benkaplan/git/drupal-7.0/includes/entity.inc).
Notice: Undefined property: stdClass::$time-stamp in userpoints_tokens() (line 506 of /Users/benkaplan/git/drupal-7.0/sites/all/modules/userpoints/userpoints.module).
Warning: date_timezone_set() expects parameter 1 to be DateTime, boolean given in format_date() (line 1905 of /Users/benkaplan/git/drupal-7.0/includes/common.inc).
Warning: date_format() expects parameter 1 to be DateTime, boolean given in format_date() (line 1915 of /Users/benkaplan/git/drupal-7.0/includes/common.inc).

And here were the results I got in my sent private message:

Here are the details of your points award:

Property name
^ (User: The user that will receive the points)

8
^ (Points: Amount of points to give or take.)

1
^ (Points category: The category to which these transaction belongs)

sfsd
^ (Entity: The entity to which this transaction refers.)

Manually entered description here.
^ (Description: Can contain the reason why the points have been given.)

my_reference
^ (Reference: Can contain a reference for this transaction.)

admin
^ (Operation: Describes the operation (Insert/Remove/...).)

Manually entered description here.
^ (Reason: The reason why the points were granted.)

^ (Timestamp: Time when the points were given.)

Wed, 12/31/1969 - 16:00
^ (Expiry date: Time when the points will expire.)

No
^ (Display: Wether to show a message to the user for this transaction or not.)

Approved
^ (Status: Status of this transaction.)

And here are your total points (in category 1):
< INVALID TOKEN [userpoints:points:category-1] >

And here are your max points in all categories:
< INVALID TOKEN [userpoints:maxpoints:all] >

I just wanted to show you my results first. I'll itemize the bugs to look at in a subsequent comment.

--Ben

BenK’s picture

So here is my bug to-do list based on the above results:

1. The [userpoints_transaction:user] is displaying the words "Property name" instead of the actual username.

2. The [userpoints_transaction:tid] is displaying the category id instead of the actual category name. This may not be a bug. But can we have a token for the actual category name available too? Also, if the points award is in the "General" category no category id shows up (since it doesn't have one). Maybe we need a special case for this?

3. The [userpoints_transaction:time-stamp] token is completely blank. Nothing is returned.

4. If an expiration date is not set for the transaction, the [userpoints_transaction:expirydate] token is displaying a Dec. 31, 1969 date.

5. I can't get the current points and maxpoints tokens to work. I tried this: [userpoints:points:category-1] and [userpoints:maxpoints:all]. Am I entering these wrong or is this a bug?

6. All of the notices described above are appearing after submitting the "Add points" form.

7. It would nice if there were a token that specified a negative points transaction as a positive value. This way you could write a subject that says "You lost 8 points." You can't currently do this because the points will always be displayed as -8. Also, it would be nice to have a token that said either "gained" or "lost". This way, you wouldn't have to specify a rules condition for a positive or negative points award and configure separate messages for each. But just a thought... this may not be feasible.

In addition to these bugs, I also noticed the following PM additional items while doing my testing. I am reporting on this issue to be complete, but I can make some new issues in the Private Message issue queue:

PM a) On the "Private Message Author" field, can we have a "Switch to direct input mode" button? This way, the message could be from a user who serves as the site administrator.

PM b) The "Private Message Subject" field is too short in length (both visually and in the number of characters that can be included). Once we start adding tokens here, it eats up characters very fast. We should lengthen it.

--Ben

berdir’s picture

Some preliminary feedback:

5. These are tokens on the user not on the transaction (a transaction doesn't have total/max points). So you'd need to use userpoints:user:points:category-1 but userpoints:user seems to be broken (your point 1) for some reason. I think I've tried that though, strange.

6. Should be possible to add something like userpoints:absolute-points, will investigate.

7. Not sure about that. Might result in more problems than it solves, just like the old default userpoints status message. Instead, what about providing a token that is basically the same as the one that is displayed by default when you gain points? Something like userpoints:message.

PM stuff) Yes, please open separate issues for that there. I assume for b) you just mean the field in the privatemsg rules integration form, not the actual resulting subject that is displayed?

berdir’s picture

StatusFileSize
new17.74 KB

Ok, new patch.

1. Fixed, it wasn't correctly loading the user.

2. tid is now a token of type term, meaning that you can do stuff like userpoints:tid:tid or userpoints:tid:name. userpoints:tid is the same thing as userpoints:tid:name now. Note that for the general category, only tid and name will work. The only way to implement the others would be to create a real term for the general category and that is something to think about in a separate issue. Not sure if it's worth doing that.

3. Fixed.

4. Expiry date now displays t('Never') if 0, just like the transaction view page.

5. See above.

6. Fixed.

7. Added a "points-abs" token for the absolute value. As suggested above, we can think about adding a message token but I'd like to do that in a follow-up issue.

berdir’s picture

Status: Needs work » Needs review
BenK’s picture

Status: Needs review » Needs work

I've begun testing of the latest patch and noticed that the new "points-abs" token is causing the following notice:

Notice: Undefined property: stdClass::$points-abs in userpoints_tokens() (line 477 of /Users/benkaplan/git/drupal-7.0/sites/all/modules/userpoints/userpoints.module).

--Ben

BenK’s picture

I've conducted more testing and confirmed that 1, 2, 3, 4, and 6 are now all fixed.

So in addition to the notice mentioned in comment #8 above, here is the one item left to do:

5. I still can't get the category points total token or maxpoints token to work. I've tried using [userpoints:user:points:category-1] and [userpoints:user:maxpoints:all] as my tokens, but they come back as an Invalid Token. Do those tokens work for you?

--Ben

berdir’s picture

Status: Needs work » Needs review
StatusFileSize
new18.33 KB

- Fixed the notice.

- You need to use [userpoints_transaction:...] just like all other tokens. There was however a bug, that should be fixed now.

BenK’s picture

Status: Needs review » Needs work

I've tested the latest patch and things are working very well. The notice is gone and the [userpoints_transaction:user:points:category-1] and [userpoints_transaction:user:maxpoints:all] tokens are working well.

Just one new thing I noticed:

I tried to create a chained token to give me the uid of the user being awarded points (something very useful, for instance, for constructing a URL to the user's points summary page). To do this, I tried using the following token:

[userpoints_transaction:user:uid]

But when this token was evaulated, here is what printed in my private message:

Property uid

So am I doing this correctly? Or is this a bug? We probably need some way to get the uid for the purpose of constructing URLs.

--Ben

P.S. Also, one small typo I noticed: In the description for the display token, "Wether" should be spelled "Whether".

berdir’s picture

That's not our problem :) I'm just passing the user object forward, I don't deal with uid's at all. This is a rules issue. There is already an issue open about a related problem... It was #915058: Add a 'no_unwrap' option to parameters, the follow-up questions I asked there. I suggest you create a new issue there. The problem is that $user is a wrapped entity, so try creating an issue like "Token don't work with wrapped entities" and then point to this and the other linked thread. You probably want to create this issue against the entity module, actually...

Will fix the typo and then commit this later...

BenK’s picture

Status: Needs work » Reviewed & tested by the community

Thanks for the explanation. I created an issue in the Entity issue queue here:

http://drupal.org/node/1108598

And will look forward to the commit when you have a chance! ;-)

--Ben

berdir’s picture

Status: Reviewed & tested by the community » Fixed

Commited, thanks for testing.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.