You are here

function userpoints_comment in User points Nodes and Comments 6

File

./userpoints_nc.module, line 232

Code

function userpoints_comment($comment, $op) {
  global $user;

  //static up_orig_uid, please read this thread http://drupal.org/node/183520
  static $up_orig_com_uid;
  $points = variable_get(USERPOINTS_POST_COMMENT, 0);
  $tid = variable_get(USERPOINTS_POST_COMMENT . '_category', 0);
  switch ($op) {
    case 'insert':
      $params = array(
        'points' => $points,
        'tid' => $tid,
        'uid' => $user->uid,
        'operation' => 'insert',
        'entity_id' => $comment['cid'],
        'entity_type' => 'comment',
      );
      userpoints_userpointsapi($params);
      break;
    case 'delete':
      $points = -$points;
      $params = array(
        'points' => $points,
        'tid' => $tid,
        'uid' => $comment->uid,
        'operation' => 'delete',
        'entity_id' => $comment->cid,
        'entity_type' => 'comment',
      );
      userpoints_userpointsapi($params);
      break;
    case 'moderate':
      $points = variable_get(USERPOINTS_MODERATE_COMMENT, 0);
      $tid = variable_get(USERPOINTS_MODERATE_COMMENT . '_category', 0);
      $params = array(
        'points' => $points,
        'tid' => $tid,
        'uid' => $comment->uid,
        'operation' => 'moderate',
        'entity_id' => $comment->cid,
        'entity_type' => 'comment',
      );
      userpoints_userpointsapi($params);
      break;
    case 'form':
      $up_orig_com_uid = $comment['uid']['#value'];
      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              ";
      $cid = $comment['cid'];
      $new_uid = $comment['uid'];
      $last_owner = db_fetch_object(db_query($sql, $cid, 'comment', 'insert', 'Ownership gain'));

      //Check the UID of the original to this user, if different add/substract points
      if ($new_uid != $last_owner->uid && is_numeric($last_owner->uid)) {

        //The owner has changed so we're removing from the

        //the original owner and giving to the new owner

        //Give to the original owner
        $points = $last_owner->points;
        $params = array(
          'points' => $points,
          'tid' => $tid,
          'uid' => $new_uid,
          'operation' => 'Ownership gain',
          'entity_id' => $cid,
          'entity_type' => 'comment',
        );
        userpoints_userpointsapi($params);

        //Take away from the original owner
        $params = array(
          'points' => -$points,
          'tid' => $tid,
          'uid' => $last_owner->uid,
          'operation' => 'Ownership loss',
          'entity_id' => $cid,
          'entity_type' => 'comment',
        );
        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 ($orig_uid != $comment['uid']) {
            $params = array(
              'points' => $points,
              'tid' => $tid,
              'uid' => $new_uid,
              'operation' => 'Ownership gain',
              'entity_id' => $cid,
              'entity_type' => 'comment',
            );
            userpoints_userpointsapi($params);

            //Take away from the original owner
            $params = array(
              'points' => -$points,
              'tid' => $tid,
              'uid' => $comment['uid'],
              'operation' => 'Ownership loss',
              'entity_id' => $new_uid,
              'entity_type' => 'comment',
            );
            userpoints_userpointsapi($params);
          }
        }
      }
      break;
  }
}