function userpoints_nc_comment_presave in User points Nodes and Comments 7
Implements hook_comment_insert().
File
- ./
userpoints_nc.module, line 459
Code
function userpoints_nc_comment_presave($comment) {
$node = node_load($comment->nid);
// Check if enabled for this node type.
if (!userpoints_nc_get_setting('comment_enabled', $node->type, TRUE)) {
return;
}
$tid = userpoints_nc_get_setting('comment_category', $node->type);
$points = userpoints_nc_get_setting('comment_points', $node->type);
$published_or_all = $comment->status == COMMENT_PUBLISHED || !variable_get('userpoints_nc_comment_published_only', TRUE);
$published_only = $comment->status == COMMENT_PUBLISHED && variable_get('userpoints_nc_comment_published_only', TRUE);
if (!$comment->cid) {
return;
}
// Load the current status of the node from the database.
$old_status = db_query('SELECT status FROM {comment} WHERE cid = :cid', array(
':cid' => $comment->cid,
))
->fetchField();
// Find the last points granted on this node inserts and ownership gains.
$active_operations = array(
'userpoints_nc_comment_insert',
'userpoints_nc_comment_publish',
'userpoints_nc_comment_unpublish',
'userpoints_nc_comment_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' => $comment->cid,
':type' => 'comment',
':operation' => $active_operations,
))
->fetchObject();
// If ownership has changed, add/subtract points.
if ($last_owner && $comment->uid != $last_owner->uid) {
// Add to the new node owner.
if ($points != 0 && $published_or_all) {
$params = array(
'points' => $last_owner->points,
'tid' => $tid,
'uid' => $comment->uid,
'operation' => 'userpoints_nc_comment_gain',
'entity_id' => $comment->cid,
'entity_type' => 'comment',
);
userpoints_userpointsapi($params);
}
// Only subtract points if he was published before.
if (userpoints_nc_get_setting('comment_ownership_deduct', $node->type, TRUE) && ($old_status == NODE_PUBLISHED || $published_or_all)) {
$params = array(
'points' => -$last_owner->points,
'tid' => $tid,
'uid' => $last_owner->uid,
'operation' => 'userpoints_nc_comment_loss',
'entity_id' => $comment->cid,
'entity_type' => 'comment',
);
userpoints_userpointsapi($params);
}
}
else {
if ($published_only && $old_status == NODE_NOT_PUBLISHED) {
$params = array(
'points' => $points,
'tid' => $tid,
'uid' => $comment->uid,
'operation' => 'userpoints_nc_comment_publish',
'entity_id' => $comment->cid,
'entity_type' => 'comment',
);
userpoints_userpointsapi($params);
}
else {
if (userpoints_nc_get_setting('comment_delete_deduct', $node->type, TRUE) && $points != 0 && $old_status == COMMENT_PUBLISHED && $comment->status == COMMENT_NOT_PUBLISHED && variable_get('userpoints_nc_comment_published_only', TRUE)) {
$params = array(
'points' => -$points,
'tid' => $tid,
'uid' => $comment->uid,
'operation' => 'userpoints_nc_comment_unpublish',
'entity_id' => $comment->cid,
'entity_type' => 'comment',
);
userpoints_userpointsapi($params);
}
}
}
}