0

I created a rule to grant users User Points when they added a comment on a node. However, I'd like to grant them these points only once. That means: they don't get more points when they react a second or a third time on the same node, but they will be granted points nevertheless after commenting on another node.

How can I do that?

Jeroen
  • 544
  • 3
  • 27

2 Answers2

0

Rules must have: Events: Before saving a comment

Conditions: this code

global $user;
$node  =array();
$query = db_select('comment', 'c');
$query->fields('c', array('nid'));
$query->condition('c.uid', $user->uid);
$result = $query->execute();
while ($res = $result->fetchAll()) {
foreach ($res AS $re) {
$node[] = $re->nid;
}
}

if(!in_array($comment->nid, $node)){
return TRUE;
}

Actions: grant users User Points

=============================

Conditions: Execute custom PHP code or create custom Conditions

/**
 * Implements hook_rules_condition_info().
 */
function MYMODULE_rules_condition_info() {
  $conditions = array();
  $conditions['MYCONDITION'] = array(
    'label' => t('lable'),
    'parameter' => array(
      'user' => array(
        'type' => 'user',
        'label' => t('User'),
      ),
    ),
    'group' => t('Custom'),
    'callbacks' => array(
      'execute' => 'MYMODULE_rules_condition_MYCONDITION',
    ),
  );
  return $conditions;
}
function MYMODULE_rules_condition_MYCONDITION($user) {
$node  =array();
$query = db_select('comment', 'c');
$query->fields('c', array('nid'));
$query->condition('c.uid', $user->uid);
$result = $query->execute();
while ($res = $result->fetchAll()) {
foreach ($res AS $re) {
$node[] = $re->nid;
}
}

if(!in_array($comment->nid, $node)){
return TRUE;
}
}
QArea
  • 4,955
  • 1
  • 12
  • 22
0

I solved my problem with the Flags module: I created the flags called 'Commented on this node' & 'First reaction'* and rule called 'Commented on a node'. These are my rule's settings:

EVENTS: After saving a new comment

CONDITIONS:

  • Content is of type Parameter: Content: [comment:node], Content types: Article
  • NOT Node is flagged Parameter: Flag: Commented on this node, node: [comment:node], User on whose behalf to check: [comment:author]

ACTIONS:

  • Grant points to a user Parameter: User: [comment:author], Points: 2, Points category: Reaction, Entity: [comment:node], Description: New reaction, Operation: Add, Display: false, Moderate: Automatically approved
  • Flag a Node Parameter: Flag: Commented on a node, Node: [comment:node], User on whose behalf to flag: [comment:author], Skip permission check: false
  • Flag a Comment: Parameter: Flag: First reaction, Comment: [comment], User on whose behalf to flag: [comment:author], Skip permission check: false

So, every time a user adds a comment to a certain node for the first time, the node is flagged as 'Commented on a node', the reaction is flagged as 'First reaction' and the user who added the comment is rewarded 2 points.

**I use the 'First comment' flag in a related question.*

Community
  • 1
  • 1
Jeroen
  • 544
  • 3
  • 27