Hello,

This module seems like it's going to come in handy for quite a few things, so thanks! What I'm trying to do right now is set it up so that points from the Userpoints module are only given out once a node is PUBLISHED. The default setting in Userpoints is to award them upon node creation, which is not ideal.

Now, I could use the "moderated points" option in Userpoints, but that will mean I'll have two steps of moderation (one for approving the node, and one to approve the point). I don't want that. I want to check "publish" and have the node be published and award the user one point all in one move.

I've got this working properly to some degree. I set Userpoints to "auto-approve," and turned off "Userpoints basic" (which gives points upon node creation), and I've set up a Workflow-ng rule like this:

- If node is of type: item
- And node status is: yes (published)
- Action: award author one point
- Action: send email to author

THIS WORKS JUST FINE, except for one thing. If I then go into the node and edit anything in it for a second time, the author is awarded ANOTHER point, and receives ANOTHER email! This is not desirable.

How can i set this up so that the rule is only triggered ONCE?

Please help. Thank you. :-D

Comments

roper.’s picture

Anybody got any suggestions?

I've applied the actions to a "Content has been updated" event. What's the difference between that and the "Content is going to be saved" event? Would that make a difference?

Thanks.

roper.’s picture

Come on guys, a little help here..? :-/

Seems like common functionality. I've even seen a couple other threads related, I know someone can explain this. Please?

FireBoss’s picture

I had this same problem on my site, a work at home article directory, http://www.fireboss.net

I didn't use Workflow-ng for my solution. I did freelance php/mysql programming for over a decade, so I dug into the code.

The real problem lies in the userpoints_nc module.

What you want to do is keep all of the modules enabled and keep a point value set for posting a node.
Then, you need to modify the file userpoints_nc/userpoints_nc.module

Around/at line 100 (my code is different so I may be a bit off on the line #),
look for the line that says: function userpoints_nodeapi...

You need to delete that entire function and replace it with the code below.
You DO NOT want to remove any code starting with this line: function userpoints_comment($comment, $op) {

function userpoints_nodeapi(&$node, $op, $teaser, $page) {
  //static up_orig_uid please read drupal.org/node/183520 
  static $up_orig_uid;
  $points = variable_get(USERPOINTS_POST . $node->type, 0);
  switch ($op) {
    case 'insert':
      if ($node->status == 1)
	  {
	  $params = array(
        'points' => $points,
        'uid' => $node->uid,
        'operation' => 'insert',
        'entity_id' => $node->nid,
        'entity_type' => 'node'
      );
      userpoints_userpointsapi($params);
	  }
      break;
    case 'delete':
       $sql = "SELECT operation,points, uid 
              FROM {userpoints_txn} 
              WHERE entity_id = %d AND entity_type = '%s' 
              AND (operation = '%s' OR operation ='%s' OR operation ='%s' OR operation ='%s')
              ORDER BY time_stamp DESC
              LIMIT 1
              ";

	   
  
      $last_owner = db_fetch_object(db_query($sql, $node->nid, 'node', 'insert', 'Ownership gain', 'operation', 'Ownership loss'));
	  		


if (isset($last_owner->uid) && ($last_owner->operation == 'insert' || $last_owner->operation == 'Ownership gain') && variable_get(USERPOINTS_POST . '_undo_points_on_delete', true)) {
        $points = -$points;
        $params = array(
          'points' => $points,
          'uid' => $node->uid,
          'operation' => 'operation',
          'entity_id' => $node->nid,
          'entity_type' => 'node',
        );
        userpoints_userpointsapi($params);
      }
      break;
    case 'prepare':
      $up_orig_uid = $node->uid;
      break;
    case 'update':
      //Find the last points granted on this node inserts and ownership gains
	  
      $sql = "SELECT points, uid 
              FROM {userpoints_txn} 
              WHERE entity_id = %d AND entity_type = '%s' 
              AND (operation = '%s' OR operation ='%s')
              ORDER BY time_stamp DESC
              LIMIT 1
              ";

	 
      $last_owner = db_fetch_object(db_query($sql, $node->nid, 'node', 'insert', 'Ownership gain'));
   	  		
       $sql = "SELECT operation,points, uid 
              FROM {userpoints_txn} 
              WHERE entity_id = %d AND entity_type = '%s' 
              AND (operation = '%s' OR operation ='%s' OR operation ='%s' OR operation ='%s')
              ORDER BY time_stamp DESC
              LIMIT 1
              ";


       $last_owner2 = db_fetch_object(db_query($sql, $node->nid, 'node', 'insert', 'Ownership gain', 'operation', 'Ownership loss'));
	  		

			$sql = "SELECT status 
              FROM {node} 
              WHERE nid = %d 
              LIMIT 1
              ";

if ((!isset($last_owner2->uid) || ($last_owner2->uid == $node->uid && $last_owner2->operation == "operation")) && $node->status == 1)
	  {
			$params = array(
				'points' => $points,
				'uid' => $node->uid,
				'operation' => 'insert',
				'entity_id' => $node->nid,
				'entity_type' => 'node'
				);
			userpoints_userpointsapi($params);
		}
	if ($last_owner2->uid == $node->uid && ($last_owner2->operation == "insert" || $last_owner2->operation == "Ownership gain") && $status_check->status == 0)
	  {
	        $params = array(
          'points' => -$points,
          'uid' => $node->uid,
          'operation' => 'operation',
          'entity_id' => $node->nid,
          'entity_type' => 'node'
        );
			userpoints_userpointsapi($params);
		}
	  
      //Check the UID of the original to this user, if different add/substract points
      if ($node->uid != $last_owner->uid && is_numeric($last_owner->uid) ) {
        //Check to see if this user has already lost the points for
        // Add to the new node owner
        $params = array(
          'points' => $points,
          'uid' => $node->uid,
          'operation' => 'Ownership gain',
          'entity_id' => $node->nid,
          'entity_type' => 'node'
        );
        userpoints_userpointsapi($params);
        // subtract from the original node owner
        $params = array(
          'points' => -$points,
          'uid' => $up_orig_uid,
          'operation' => 'Ownership loss',
          'entity_id' => $node->nid,
          'entity_type' => 'node'
        );
        userpoints_userpointsapi($params);
      }
      else {
        //We failed to pull a matching operation via the DB
        //If the user wants to use the V2BUG we'll use it..
        //please read drupal.org/node/183520
        if (variable_get(USERPOINTS_USE_V2BUG, false)) {
          if ($node->uid != $up_orig_uid) {
            // Add to the new node owner
            $params = array(
              'points' => $points,
              'uid' => $node->uid,
              'operation' => 'Ownership gain',
              'entity_id' => $node->nid,
              'entity_type' => 'node'
            );
            userpoints_userpointsapi($params);
            // subtract from the original node owner
            $params = array(
              'points' => -$points,
              'uid' => $up_orig_uid,
              'operation' => 'Ownership loss',
              'entity_id' => $node->nid,
              'entity_type' => 'node'
            );
            userpoints_userpointsapi($params);
          }
        }
      }
      break;
  }
}

Sorry for the dirty looking code, just threw all of this together real quick and didn't feel like making it tidy :).

After that, simply upload the new userpoints_nc.module file to it's module directory.
This code will add point transactions for the following actions:
Add points when node is set to published
Remove points when node is deleted
Remove points when node is set to unpublished
Add points when node is posted and published at the same time (by admin, perhaps).

This code will cover point awards and removals for node publishing, unpublishing, republishing, reunpublishing?...

The code will not add points if the node is published but the user already got points when posting the node in the first place (handy for making this code live, so you can still approve pending nodes without having to correct points afterwards).

This code has almost entirely been tested. I didn't really test anything with changing authors but I believe I coded the function to handle that properly (didn't edit the existing code that handled that and took that code into account when determining rules for awarding points in code that was added/changed in other areas). I just finished this code for my site after not being able to find a solution already made, so I thought posting it here would help someone else that doesn't have the coding knowledge that I have.

Something to note:
I have no knowledge of the V2BUG and did not mess with that portion of this code. If you have that setting enabled, this function will properly not operate any differently but I have not tested any of that.

kinnauri’s picture

looks like one line is lost in this patch, insert this:
$status_check = db_fetch_object(db_query($sql, $node->nid));

right after
$sql = "SELECT operation,points, uid
FROM {userpoints_txn}
WHERE entity_id = %d AND entity_type = '%s'
AND (operation = '%s' OR operation ='%s' OR operation ='%s' OR operation ='%s')
ORDER BY time_stamp DESC
LIMIT 1
";