1

I am not sure how to go about this. I use the Userpoints module in Drupal 7 to attribute points to users who create nodes ('reviews'). I also want to allow them to gain some points if they supersede or modify an existing node... but only if the previous creation/mod was done over 20 days before.

How can I do this with Rules? Or else, how should I code it manually?

  • I will have a look at it in the next week. A bit short on time at the moment (the dreaded deadline...). Thanks for your answer anyway. – Pierre_colin Sep 6 '16 at 16:32
1

Sorry to say, but the previous answer has some challenges:

  • Rules Event After saving new content is not the right event.
  • I disagree with the "You will need a custom module ...", since there is an out-of-the-Rules-box Rules Condition you can use ... Read on for more details on that.

Here is a rule (in Rules Export format) that does what you're asking for:

{ "rules_grant_userpoints_after_updating_old_content" : {
    "LABEL" : "Grant userpoints after updating old content",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "userpoints_rules" ],
    "ON" : { "node_update" : [] },
    "IF" : [
      { "data_is" : {
          "data" : [ "node-unchanged:changed" ],
          "op" : "\u003C",
          "value" : "-20 days"
        }
      }
    ],
    "DO" : [
      { "userpoints_action_grant_points" : {
          "user" : [ "site:current-user" ],
          "points" : "123",
          "tid" : "0",
          "entity" : [ "" ],
          "description" : "Modify existing node (nid=[node:nid]) that was not updated in the last 20 days.",
          "operation" : "Grant points",
          "display" : "1",
          "moderate" : "default"
        }
      },
      { "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" } }
    ]
  }
}

Some more details about the above rule:

  • Rules Event: After updating existing content (you may want to limit it to selected content types only).
  • Rules Condition: Token [node-unchanged:changed] is at least from 20 days ago.
  • Rules Actions:
    • Grant 123 user points (adapt the number and the other options as you want).
    • Load the current userpoints balance.
    • Display a message with the current userpoints balance.

If you have the Rules UI enabled (and the Userpoints Rules module, you should be able to just import the above rule in your own site.

PS: After you're convinced the rule works as you want, you may want to remove the last 2 Rules Actions about the message shown to the user after user points were granted.

  • This worked as well. It is a good non-code alternative. Thanks for the input! – Pierre_colin Sep 14 '16 at 7:27
0

Giving points for adding new Review nodes:

  1. Create new Rule; event: After saving new content + restrict by type: Review.

  2. Action: Grant points to a user.

Note: you need to have Userpoints rules integration installed.


Giving points for updating Review nodes after certain period of time

You will need a custom module where you will create a new rule Condition using hook_rules_condition_info().
This condition will check if previous node revision is older than XX days (in your case 20 days).

Here's an example:

function YOUR_MODULE_rules_condition_info() {
  return array(
    'YOUR_MODULE_condition_compare_revision' => array(
      'label' => t('Compare date of last revision'),
      'parameter' => array(
        'node' => array(
          'label' => t('Node'),
          'type' => 'node',
        ),
      ),
    ),
  );
}

function YOUR_MODULE_condition_compare_revision($node) {
  $revision = db_select('node_revision', 'r')
    ->fields('r', array('nid'))
    ->condition('nid', $node->nid)
    // Make sure this is not the last revision.
    ->condition('vid', $node->vid, '!=')
    // Make sure this revision is older than your desired period of time.
    ->condition('timestamp', REQUEST_TIME - 20 * 24 * 60 * 60, '<=')
    ->execute()
    ->fetchObject();

  return $revision;
}

This could would go to YOUR_MODULE.rules.inc.

Note: I wrote this quickly and without testing. Should be working fine though.

  • Thanks a lot for your help. This was critical in achieving my goal. I do however want to make a correction. Since I want the user to get points only if the 'previous' update is older than a certain date, I actually needed to make sure that there were no reviews between the date of the latest review and 20 days before. I therefore used: ->condition('timestamp', array(REQUEST_TIME - 15 * 24 * 60 * 60, REQUEST_TIME), 'BETWEEN') ->execute() ->fetchAll(); – Pierre_colin May 24 '15 at 21:49
  • Glad it was useful. If it solved your problem, accept the answer as a solution. It will help others with similar problem in the future, comments can get deleted. – Aram Boyajyan May 25 '15 at 7:48

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.