You are here

function userpoints_nodeapi in User points Nodes and Comments 6

File

./userpoints_nc.module, line 130

Code

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);
  $tid = variable_get(USERPOINTS_POST . $node->type . '_category', 0);
  switch ($op) {
    case 'insert':
      $params = array(
        'points' => $points,
        'tid' => $tid,
        'uid' => $node->uid,
        'operation' => 'insert',
        'entity_id' => $node->nid,
        'entity_type' => 'node',
      );
      userpoints_userpointsapi($params);
      break;
    case 'delete':
      if (variable_get(USERPOINTS_POST . '_undo_points_on_delete', true)) {
        $points = -$points;
        $params = array(
          'points' => $points,
          'tid' => $tid,
          'uid' => $node->uid,
          'operation' => 'delete',
          '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 \n              FROM {userpoints_txn} \n              WHERE entity_id = %d AND entity_type = '%s' \n              AND (operation = '%s' OR operation ='%s')\n              ORDER BY time_stamp DESC\n              LIMIT 1\n              ";
      $last_owner = db_fetch_object(db_query($sql, $node->nid, 'node', 'insert', 'Ownership gain'));

      //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,
          'tid' => $tid,
          '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,
          'tid' => $tid,
          'uid' => $last_owner->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,
              'tid' => $tid,
              '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,
              'tid' => $tid,
              'uid' => $up_orig_uid,
              'operation' => 'Ownership loss',
              'entity_id' => $node->nid,
              'entity_type' => 'node',
            );
            userpoints_userpointsapi($params);
          }
        }
      }
      break;
  }
}