You are here

function userpoints_nc_node_presave in User points Nodes and Comments 7

Implements hook_node_update().

File

./userpoints_nc.module, line 351

Code

function userpoints_nc_node_presave($node) {

  // Check if enabled for this node type.
  if (!userpoints_nc_get_setting('enabled', $node->type, TRUE)) {
    return;
  }
  $tid = userpoints_nc_get_setting('category', $node->type);
  $points = userpoints_nc_get_setting('points', $node->type);

  // Check whether the node is published or unpublished nodes should be counted
  // too.
  $published_or_all = $node->status == NODE_PUBLISHED || !variable_get('userpoints_nc_published_only', TRUE);

  // Check if the node is published and published only setting is enabled.
  $published_only = $node->status == NODE_PUBLISHED && variable_get('userpoints_nc_published_only', TRUE);

  // Check if it is a new node.
  if (empty($node->nid)) {
    return;
  }

  // Load the current status of the node from the database.
  $old_status = db_query('SELECT status FROM {node} WHERE nid = :nid', array(
    ':nid' => $node->nid,
  ))
    ->fetchField();

  // Find the last points granted on this node inserts and ownership gains.
  $active_operations = array(
    'userpoints_nc_node_insert',
    'userpoints_nc_node_publish',
    'userpoints_nc_node_unpublish',
    'userpoints_nc_node_gain',
  );
  $sql = "SELECT points, uid FROM {userpoints_txn} WHERE entity_id = :id AND entity_type = :type AND operation IN (:operation) ORDER BY time_stamp DESC";
  $last_owner = db_query_range($sql, 0, 1, array(
    ':id' => $node->nid,
    ':type' => 'node',
    ':operation' => $active_operations,
  ))
    ->fetchObject();

  // If ownership has changed, add/subtract points.
  if ($last_owner && $node->uid != $last_owner->uid) {

    // Add to the new node owner.
    if ($published_or_all) {
      $params = array(
        'points' => $last_owner->points,
        'tid' => $tid,
        'uid' => $node->uid,
        'operation' => 'userpoints_nc_node_gain',
        'entity_id' => $node->nid,
        'entity_type' => 'node',
      );
      userpoints_userpointsapi($params);
    }

    // Only subtract points if the node was published before.
    if (userpoints_nc_get_setting('ownership_deduct', $node->type, TRUE) && ($old_status == NODE_PUBLISHED || !variable_get('userpoints_nc_published_only', TRUE))) {
      $params = array(
        'points' => -$last_owner->points,
        'tid' => $tid,
        'uid' => $last_owner->uid,
        'operation' => 'userpoints_nc_node_loss',
        'entity_id' => $node->nid,
        'entity_type' => 'node',
      );
      userpoints_userpointsapi($params);
    }
  }
  else {
    if ($published_only && $old_status == NODE_NOT_PUBLISHED) {
      $params = array(
        'points' => $points,
        'tid' => $tid,
        'uid' => $node->uid,
        'operation' => 'userpoints_nc_node_publish',
        'entity_id' => $node->nid,
        'entity_type' => 'node',
      );
      userpoints_userpointsapi($params);
    }
    else {
      if (userpoints_nc_get_setting('delete_deduct', $node->type, TRUE) && $points != 0 && $old_status == NODE_PUBLISHED && $node->status == NODE_NOT_PUBLISHED && variable_get('userpoints_nc_published_only', TRUE)) {
        $params = array(
          'points' => -$points,
          'tid' => $tid,
          'uid' => $node->uid,
          'operation' => 'userpoints_nc_node_unpublish',
          'entity_id' => $node->nid,
          'entity_type' => 'node',
        );
        userpoints_userpointsapi($params);
      }
    }
  }
}