? .cvsignore
? userpoints_nc_d7.patch
? userpoints_nc_d7_2.patch
? userpoints_nc_d7_3.patch
? userpoints_nc_d7_4.patch
? userpoints_nc_d7_5.patch
? userpoints_nc_d7_6.patch
? userpoints_nc_d7_7.patch
Index: userpoints_nc.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/userpoints_nc/userpoints_nc.info,v
retrieving revision 1.1
diff -u -p -r1.1 userpoints_nc.info
--- userpoints_nc.info	9 Jan 2009 07:46:35 -0000	1.1
+++ userpoints_nc.info	11 Oct 2010 13:03:13 -0000
@@ -3,4 +3,5 @@ name = Userpoints Nodes and Comments
 description = Grants userpoints for posting nodes or comments or moderating comments
 package = Userpoints
 dependencies[] = userpoints
-core = 6.x
+files[]=userpoints_nc.module
+core = 7.x
Index: userpoints_nc.js
===================================================================
RCS file: userpoints_nc.js
diff -N userpoints_nc.js
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ userpoints_nc.js	11 Oct 2010 13:03:13 -0000
@@ -0,0 +1,22 @@
+// $Id$
+(function ($) {
+  Drupal.behaviors.userpointsNCNodeTypeFieldsetSummaries = {
+    attach: function (context) {
+      $('fieldset.userpoints-nc-nodetype-form', context).drupalSetSummary(function (context) {
+        if ($('#edit-userpoints-nc-enabled').is(':checked') && $('#edit-userpoints-nc-points').val() > 0) {
+          var params = {
+            '@points': $('#edit-userpoints-nc-points', context).val(),
+            '%category': $('select#edit-userpoints-nc-category :selected', context).text(),
+            '@points_comment': $('#edit-userpoints-nc-comment-points', context).val(),
+            '%category_comment': $('select#edit-userpoints-nc-comment-category :selected', context).text()
+          };
+          return Drupal.t('Content: @points (%category)<br />Comment: @points_comment (%category_comment).', params);
+        }
+        else {
+          return Drupal.t('No !points are assigned for posting content.', {'!points' : Drupal.settings.userpoints_nc.points});
+        }
+      });
+    }
+  };
+
+})(jQuery);
\ No newline at end of file
Index: userpoints_nc.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/userpoints_nc/userpoints_nc.module,v
retrieving revision 1.3.2.2
diff -u -p -r1.3.2.2 userpoints_nc.module
--- userpoints_nc.module	2 Mar 2010 22:27:46 -0000	1.3.2.2
+++ userpoints_nc.module	11 Oct 2010 13:03:13 -0000
@@ -1,353 +1,600 @@
-<?php
+  <?php
 
 //$Id: userpoints_nc.module,v 1.3.2.2 2010/03/02 22:27:46 kbahey Exp $
 
 // Copyright 2005 Khalid Baheyeldin http://2bits.com
 
-define('USERPOINTS_POST',                'userpoints_post_');
-define('USERPOINTS_POST_COMMENT',        'userpoints_post_comment');
-define('USERPOINTS_MODERATE_COMMENT',    'userpoints_moderate_comment');
-//define a variable to trigger the use of the v2bug please read drupal.org/node/183520
-define('USERPOINTS_USE_V2BUG',    'userpoints_use_v2bug');
-
-function userpoints_nc_help($path, $arg) {
-  switch ($path) {
-    case 'admin/settings/userpoints_nc':
-      return t('<strong>UP:</strong> Some basic interfaces for userpoints, such as posting nodes, comments, ...etc.');
-  }
+function userpoints_nc_form_node_type_form_alter(&$form, &$form_state) {
+  $form['userpoints_nc'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('!Points Awards', userpoints_translation()),
+    '#collapsible' => TRUE,
+    '#collapsed' => FALSE,
+    '#access' => user_access('administer userpoints'),
+    '#weight' => 25,
+    '#group' => 'additional_settings',
+    '#attached' => array(
+      'js' => array(
+        'userpoints_nc' => drupal_get_path('module', 'userpoints_nc') . '/userpoints_nc.js',
+      ),
+    ),
+    '#attributes' => array('class' => array('userpoints-nc-nodetype-form')),
+  );
+  
+  $type = $form['#node_type']->type;
+  $form['userpoints_nc']['userpoints_nc_enabled'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Enabled'),
+    '#default_value' => userpoints_nc_get_setting('enabled', $type, TRUE),
+    '#description' => t('If checked, !points can be awarded for creating or commenting upon content of this type.', userpoints_translation()),
+  );
+
+  $form['userpoints_nc']['userpoints_nc_ownership_deduct'] = array(
+    '#type'          => 'checkbox',
+    '#title'         => t('Deduct !points when the content author changes', userpoints_translation()),
+    '#default_value' => userpoints_nc_get_setting('ownership_deduct', $type, TRUE),
+    '#description'   => t('If checked, the old content author will lose any !points that he previously gained.', userpoints_translation()),
+    '#states' => array(
+      'visible' => array(
+        ':input[name="userpoints_nc_enabled"]' => array('checked' => TRUE),
+      ),
+    ),
+  );
+
+  $form['userpoints_nc']['userpoints_nc_delete_deduct'] = array(
+    '#type'          => 'checkbox',
+    '#title'         => t('Deduct !points when content is deleted or unpublished', userpoints_translation()),
+    '#default_value' => userpoints_nc_get_setting('delete_deduct', $type, TRUE),
+    '#description'   => t('If checked, the content author will lose any !points that he previously gained.', userpoints_translation()),
+    '#states' => array(
+      'visible' => array(
+        ':input[name="userpoints_nc_enabled"]' => array('checked' => TRUE),
+      ),
+    ),
+  );
+
+  $form['userpoints_nc']['userpoints_nc_points'] = array(
+    '#type'          => 'textfield',
+    '#title'         => t('!Points for new content', array_merge(userpoints_translation())),
+    '#default_value' => userpoints_nc_get_setting('points', $type),
+    '#size'          => 5,
+    '#maxlength'     => 5,
+    '#description' => t('Set the number of !points to be awarded when a user adds content of this type.', userpoints_translation()),
+    '#states' => array(
+      'visible' => array(
+        ':input[name="userpoints_nc_enabled"]' => array('checked' => TRUE),
+      ),
+    ),
+  );
+
+  $form['userpoints_nc']['userpoints_nc_category'] = array(
+    '#type'          => 'select',
+    '#title'         => t('!Points category for new content', array_merge(userpoints_translation())),
+    '#description'   => t('Choose the category of !points to be awarded when a user adds content of this type.', userpoints_translation()),
+    '#options'       => userpoints_get_categories(),
+    '#default_value' => userpoints_nc_get_setting('category', $type),
+    '#states' => array(
+      'visible' => array(
+        ':input[name="userpoints_nc_enabled"]' => array('checked' => TRUE),
+      ),
+    ),
+  );
+
+  $form['userpoints_nc']['userpoints_nc_comment_points'] = array(
+    '#type'          => 'textfield',
+    '#title'         => t('!Points for new comments', userpoints_translation()),
+    '#description'   => t('Set the number of !points to be awarded when a user adds a comment.', userpoints_translation()),
+    '#default_value' => userpoints_nc_get_setting('comment_points', $type),
+    '#size'          => 5,
+    '#maxlength'     => 5,
+    '#states' => array(
+      'visible' => array(
+        ':input[name="userpoints_nc_enabled"]' => array('checked' => TRUE),
+      ),
+    ),
+  );
+
+  $form['userpoints_nc']['userpoints_nc_comment_category'] = array(
+    '#type'          => 'select',
+    '#title'         => t('!Points category for new comments', array_merge(userpoints_translation())),
+    '#description'   => t('Choose the category of !points to be awarded when a user adds a comment.', userpoints_translation()),
+    '#options'       => userpoints_get_categories(),
+    '#default_value' => userpoints_nc_get_setting('comment_category', $type),
+    '#states' => array(
+      'visible' => array(
+        ':input[name="userpoints_nc_enabled"]' => array('checked' => TRUE),
+      ),
+    ),
+  );
 }
 
+/**
+ * Implements hook_userpoints().
+ */
 function userpoints_nc_userpoints($op, $params = array()) {
   switch ($op) {
     case 'setting':
-      $group = 'node';
-      $form[$group] = array(
+      $form['userpoints_nc'] = array(
         '#type'        => 'fieldset',
         '#collapsible' => TRUE,
         '#collapsed'   => TRUE,
-        '#title'       => t('!Points for posting nodes', userpoints_translation()),
+        '#title'       => t('Content and comments'),
+        '#group'       => 'settings_additional',
+      );
+
+      $form['userpoints_nc']['userpoints_nc_enabled'] = array(
+        '#type' => 'checkbox',
+        '#title' => t('Enabled by default'),
+        '#default_value' => userpoints_nc_get_setting('enabled', NULL, TRUE),
+        '#description' => t('If checked, all content types award !points by default. This can be overridden for each content type on the content type edit page.', userpoints_translation()),
       );
-      
-      $form[$group][USERPOINTS_POST . '_undo_points_on_delete'] = array(
+
+      $form['userpoints_nc']['userpoints_nc_published_only'] = array(
         '#type'          => 'checkbox',
-        '#title'         => t('Take away !points on node delete', array_merge(userpoints_translation())),
-        '#default_value' => variable_get(USERPOINTS_POST . '_undo_points_on_delete', true),
-        );
-
-      $tree = taxonomy_get_tree(userpoints_get_vid());
-      $category_options = array();
-      $category_options[0] = t('<Uncategorized>');
-      if (!empty($tree)) {
-        foreach ($tree as $term) {
-          $category_options[$term->tid] = str_repeat('--', $term->depth) . $term->name;
-        }
-      }
-      foreach (node_get_types() as $type => $name) {
-        $form[$group][USERPOINTS_POST . $type] = array(
-          '#type'          => 'textfield',
-          '#title'         => t('!Points for posting a !node-name', array_merge(userpoints_translation(), array('!node-name' => $name->name))),
-          '#default_value' => variable_get(USERPOINTS_POST . $type, '0'),
-          '#size'          => 5,
-          '#maxlength'     => 5,
-          );
-
-        $form[$group][USERPOINTS_POST . $type . '_category'] = array(
-          '#type'          => 'select',
-          '#title'         => t('Assign !Points for posting a !node-name to category', array_merge(userpoints_translation(), array('!node-name' => $name->name))),
-          '#options'       => $category_options,
-          '#default_value' => variable_get(USERPOINTS_POST . $type . '_category', 0),
-          );
-    
-      }
-      $group = 'comment';
-      $form[$group] = array(
-        '#type'        => 'fieldset',
-        '#collapsible' => TRUE,
-        '#collapsed'   => TRUE,
-        '#title'       => t('!Points for posting comments', userpoints_translation()),
+        '#title'         => t('Only award !points for published nodes and comments.', userpoints_translation()),
+        '#default_value' => variable_get('userpoints_nc_published_only', TRUE),
+        '#description'   => t('If checked, users only receive !points when content or comments are published.', userpoints_translation())
       );
-      $form[$group][USERPOINTS_POST_COMMENT] = array(
+
+      $form['userpoints_nc']['userpoints_nc_ownership_deduct'] = array(
+        '#type'          => 'checkbox',
+        '#title'         => t('Deduct !points when the content author changes.', userpoints_translation()),
+        '#default_value' => variable_get('userpoints_nc_ownership_deduct', TRUE),
+        '#description'   => t('If checked, the old content author will lose any !points that he previously gained. This setting can be overridden for each content type.', userpoints_translation())
+      );
+
+      $form['userpoints_nc']['userpoints_nc_delete_deduct'] = array(
+        '#type'          => 'checkbox',
+        '#title'         => t('Deduct !points when content is deleted or unpublished', userpoints_translation()),
+        '#default_value' => variable_get('userpoints_nc_ownership_deduct', TRUE),
+        '#description'   => t('If checked, the content author will lose any points that he previously gained. This setting can be overridden for each content type.', userpoints_translation())
+      );
+
+      $form['userpoints_nc']['userpoints_nc_points'] = array(
         '#type'          => 'textfield',
-        '#title'         => t('!Points for posting a comment', userpoints_translation()),
-        '#default_value' => variable_get(USERPOINTS_POST_COMMENT, 0),
+        '#title'         => t('Default !points for new content', userpoints_translation()),
+        '#description'   => t('Set the default number of !points to be awarded when a user adds new content. This can be overridden for each content type.', userpoints_translation()),
+        '#default_value' => userpoints_nc_get_setting('points'),
         '#size'          => 5,
         '#maxlength'     => 5,
-        );
+      );
 
-      $form[$group][USERPOINTS_POST_COMMENT . '_category'] = array(
+      $form['userpoints_nc']['userpoints_nc_category'] = array(
         '#type'          => 'select',
-        '#title'         => t('Assign !Points for posting a comment to category', array_merge(userpoints_translation())),
-        '#options'       => $category_options,
-        '#default_value' => variable_get(USERPOINTS_POST_COMMENT . '_category', 0),
-        );
+        '#title'         => t('Default !points category for new content', userpoints_translation()),
+        '#description'   => t('Choose the category of !points to be used by default when a user adds new content. This can be overridden for each content type.', userpoints_translation()),
+        '#options'       => userpoints_get_categories(),
+        '#default_value' => userpoints_nc_get_setting('category'),
+      );
 
-      $form[$group][USERPOINTS_MODERATE_COMMENT] = array(
+      $form['userpoints_nc']['userpoints_nc_comment_points'] = array(
         '#type'          => 'textfield',
-        '#title'         => t('!Points for moderating a comment', userpoints_translation()),
-        '#default_value' => variable_get(USERPOINTS_MODERATE_COMMENT, 0),
+        '#title'         => t('Default !points for new comments', userpoints_translation()),
+        '#description'   => t('Set the default number of !points to be awarded when a user adds a comment. This can be overridden for each content type.', userpoints_translation()),
+        '#default_value' => userpoints_nc_get_setting('comment_points'),
         '#size'          => 5,
         '#maxlength'     => 5,
-        );
+      );
 
-      $form[$group][USERPOINTS_MODERATE_COMMENT . '_category'] = array(
+      $form['userpoints_nc']['userpoints_nc_comment_category'] = array(
         '#type'          => 'select',
-        '#title'         => t('Assign !Points for posting a comment to category', array_merge(userpoints_translation())),
-        '#options'       => $category_options,
-        '#default_value' => variable_get(USERPOINTS_MODERATE_COMMENT . '_category', 0),
-        );
-
-      $group = 'v2compatibility';
-      $form[$group] = array(
-        '#type'        => 'fieldset',
-        '#collapsible' => TRUE,
-        '#collapsed'   => TRUE,
-        '#title'       => t('userpoints_basic events version 2.14 Compatibility', userpoints_translation()),
-        '#description' => t('If you have used userpoints_basic prior to version 2.14 you may need to enable this fix.<br />
-                             If you have not used userpoints_basic prior to v3 you do NOT need to (nor should) enable this fix. <br />
-                             This fix only affects the behavior of how !points are handled when ownership of a node/comment
-                             changes. In version 2 data on the original owner was not kept and a static variable was used to 
-                             store this information. Unfortunately people have experienced unusual side effects such as
-                             !points moving from one owner to a completely random user. All !points accrued with version 3 will
-                             not exhibit this behavior thus this fix is unnecessary. 
-                             Only check this box under the following conditions <br />
-                             1) You have existing !points granted by a userpoints version prior to version 3 <br />
-                             2) If ownership changes on a node/comment you want points to follow ownership <br />
-                             <i>All !points accrued by version 3 will move ownership automatically this setting only affects !points accrued with v2</i>
-                             <br />
-                             3) You have read <a href="http://drupal.org/node/183520">Article 183520</a> on drupal.org and 
-                                understand that there could be unusual issues with ownership moves', userpoints_translation()),
-      );
-      $form[$group][USERPOINTS_USE_V2BUG] = array(
-        '#type'          => 'checkbox',
-        '#title'         => t('I agree to the above description and want to use the V2 bug fix'),
-        '#default_value' => variable_get(USERPOINTS_USE_V2BUG, false),
+        '#title'         => t('Default !points category for new comments', userpoints_translation()),
+        '#description'   => t('Choose the category of !points to be used by default when a user adds a comment. This can be overridden for each content type.', userpoints_translation()),
+        '#options'       => userpoints_get_categories(),
+        '#default_value' => userpoints_nc_get_setting('comment_category'),
       );
 
       return $form;
       break;
   }
 }
-  
-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':
+
+/**
+ * Helper function to return a setting.
+ *
+ * If a node type is passed in, the specific setting will be used with a
+ * fallback to the global settings.
+ *
+ * @param $name
+ *   Name of the setting: points, category, comment_points, comment_category.
+ * @param $type
+ *   Node type, if passed in, the function looks for a node type specifc setting.
+ *
+ * @return
+ *   The configured setting value.
+ */
+function userpoints_nc_get_setting($name, $type = NULL, $default = 0) {
+  if (!empty($type)) {
+    return variable_get('userpoints_nc_' . $name  . '_' . $type, variable_get('userpoints_nc_' . $name, $default));
+  }
+  else {
+    return variable_get('userpoints_nc_' . $name, $default);
+  }
+}
+
+/**
+ * Implements hook_node_delete().
+ */
+function userpoints_nc_node_delete($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);
+  $published_or_all = $node->status == NODE_PUBLISHED || !variable_get('userpoints_nc_published_only', TRUE);
+
+  if (userpoints_nc_get_setting('delete_deduct', $node->type, TRUE) && $published_or_all && $points != 0) {
+    $params = array(
+      'points' => -$points,
+      'tid' => $tid,
+      'uid' => $node->uid,
+      'operation' => 'userpoints_nc_node_delete',
+      'entity_id' => $node->nid,
+      'entity_type' => 'node',
+    );
+    userpoints_userpointsapi($params);
+  }
+}
+
+/**
+ * Implements hook_node_insert().
+ */
+function userpoints_nc_node_insert($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);
+  $published_or_all = $node->status == NODE_PUBLISHED || !variable_get('userpoints_nc_published_only', TRUE);
+
+  // Only add points if node is published or published only setting is FALSE.
+  if ($points != 0 && $published_or_all) {
+    $params = array(
+      'points' => $points,
+      'tid' => $tid,
+      'uid' => $node->uid,
+      'operation' => 'userpoints_nc_node_insert',
+      'entity_id' => $node->nid,
+      'entity_type' => 'node'
+    );
+    userpoints_userpointsapi($params);
+  }
+}
+
+/**
+ * Implements hook_node_update().
+ */
+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);
+  $published_or_all = $node->status == NODE_PUBLISHED || !variable_get('userpoints_nc_published_only', TRUE);
+  $published_only = $node->status == NODE_PUBLISHED && variable_get('userpoints_nc_published_only', TRUE);
+
+  // Check if it is a new node.
+  if (!$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.
+  $sql = "SELECT points, uid FROM {userpoints_txn} WHERE entity_id = :id AND entity_type = :type AND operation LIKE :operation ORDER BY time_stamp DESC";
+  $last_owner = db_query_range($sql, 0, 1, array(':id' => $node->nid, ':type' => 'node', ':operation' => 'userpoints_nc_%'))->fetchObject();
+  // If ownership has changed, add/substract points.
+  if ($last_owner && $node->uid != $last_owner->uid) {
+    // Add to the new node owner.
+    if ($published_or_all) {
       $params = array(
-        'points' => $points,
+        'points' => $last_owner->points,
         'tid' => $tid,
         'uid' => $node->uid,
-        'operation' => 'insert',
+        'operation' => 'userpoints_nc_node_gain',
         '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' => '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'));
-      
-      //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' => $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,
-              '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;
-  }
-}
+    }
 
-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':
+    // Only subtract points if he was published before.
+    if (userpoints_nc_get_setting('ownership_deduct', $node->type, TRUE) && ($old_status == NODE_PUBLISHED || $published_or_all)) {
       $params = array(
-        'points' => $points,
+        'points' => -$last_owner->points,
         'tid' => $tid,
-        'uid' => $user->uid,
-        'operation' => 'insert',
-        'entity_id' => $comment['cid'],
-        'entity_type' => 'comment'
+        'uid' => $last_owner->uid,
+        'operation' => 'userpoints_nc_node_loss',
+        'entity_id' => $node->nid,
+        'entity_type' => 'node'
       );
       userpoints_userpointsapi($params);
-      break;
-    case 'delete':
-      $points = -$points;
+    }
+  }
+  // Check if node has been published.
+  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);
+  }
+  // Check if node was unpublished and points need to be subtracted.
+  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);
+  }
+}
+
+/**
+ * Implements hook_comment_insert().
+ */
+function userpoints_nc_comment_insert($comment) {
+  $node = node_load($comment->nid);
+  // Check if enabled for this node type.
+  if (!userpoints_nc_get_setting('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_published_only', TRUE);
+
+  if ($points != 0 && $published_or_all) {
+    $params = array(
+      'points' => $points,
+      'tid' => $tid,
+      'uid' => $comment->uid,
+      'operation' => 'userpoints_nc_comment_insert',
+      'entity_id' => $comment->cid,
+      'entity_type' => 'comment'
+    );
+    userpoints_userpointsapi($params);
+  }
+}
+
+/**
+ * Implements hook_comment_insert().
+ */
+function userpoints_nc_comment_presave($comment) {
+  global $user;
+
+  $node = node_load($comment->nid);
+  // Check if enabled for this node type.
+  if (!userpoints_nc_get_setting('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_published_only', TRUE);
+  $published_only = $comment->status == COMMENT_PUBLISHED && variable_get('userpoints_nc_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.
+  $sql = "SELECT points, uid FROM {userpoints_txn} WHERE entity_id = :id AND entity_type = :type AND operation LIKE :operation ORDER BY time_stamp DESC";
+  $last_owner = db_query_range($sql, 0, 1, array(':id' => $comment->cid, ':type' => 'comment', ':operation' => 'userpoints_nc_%'))->fetchObject();
+
+  // If ownership has changed, add/substract points.
+  if ($last_owner && $comment->uid != $last_owner->uid) {
+    // Add to the new node owner.
+    if ($points != 0 && $published_or_all) {
       $params = array(
-        'points' => $points,
+        'points' => $last_owner->points,
         'tid' => $tid,
         'uid' => $comment->uid,
-        'operation' => 'delete',
+        'operation' => 'userpoints_nc_comment_gain',
         'entity_id' => $comment->cid,
-        'entity_type' => 'comment'
+        'entity_type' => 'comment',
       );
       userpoints_userpointsapi($params);
-      break;
-    case 'moderate':
-      $points = variable_get(USERPOINTS_MODERATE_COMMENT, 0);
-      $tid = variable_get(USERPOINTS_MODERATE_COMMENT . '_category', 0);
+    }
+
+    // Only subtract points if he was published before.
+    if (userpoints_nc_get_setting('ownership_deduct', $node->type, TRUE) && ($old_status == NODE_PUBLISHED || $published_or_all)) {
       $params = array(
-        'points' => $points,
+        'points' => -$last_owner->points,
         'tid' => $tid,
-        'uid' => $comment->uid,
-        'operation' => 'moderate',
+        'uid' => $last_owner->uid,
+        'operation' => 'userpoints_nc_comment_loss',
         'entity_id' => $comment->cid,
-        'entity_type' => 'comment'
+        'entity_type' => 'comment',
       );
       userpoints_userpointsapi($params);
+    }
+  }
+  // Check if node has been published.
+  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);
+  }
+  // Check if node was unpublished and points need to be subtracted.
+  else if (userpoints_nc_get_setting('delete_deduct', $node->type, TRUE) && $points != 0 && $old_status == COMMENT_PUBLISHED && $comment->status == COMMENT_NOT_PUBLISHED && variable_get('userpoints_nc_published_only', TRUE)) {
+     $params = array(
+      'points' => -$points,
+      'tid' => $tid,
+      'uid' => $node->uid,
+      'operation' => 'userpoints_nc_comment_unpublish',
+        'entity_id' => $comment->cid,
+        'entity_type' => 'comment',
+    );
+    userpoints_userpointsapi($params);
+  }
+}
+
+/**
+ * Implements hook_comment_delete().
+ */
+function userpoints_nc_comment_delete($comment) {
+  $node = node_load($comment->nid);
+  // Check if enabled for this node type.
+  if (!userpoints_nc_get_setting('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_published_only', TRUE);
+
+  if (userpoints_nc_get_setting('delete_deduct', $node->type, TRUE) && $points != 0 && $published_or_all) {
+    $params = array(
+      'points' => -$points,
+      'tid' => $tid,
+      'uid' => $comment->uid,
+      'operation' => 'userpoints_nc_comment_delete',
+      'entity_id' => $comment->cid,
+      'entity_type' => 'comment'
+    );
+    userpoints_userpointsapi($params);
+  }
+}
+
+/**
+ * Implements hook_userpoints_info().
+ */
+function userpoints_nc_userpoints_info() {
+  return array(
+    'userpoints_nc_node_insert' => array(
+      'description callback' => 'userpoints_nc_description_callback',
+      'description' => t('Content was created.'),
+    ),
+    'userpoints_nc_node_publish' => array(
+      'description callback' => 'userpoints_nc_description_callback',
+    ),
+    'userpoints_nc_node_unpublish' => array(
+      'description callback' => 'userpoints_nc_description_callback',
+    ),
+    'userpoints_nc_node_delete' => array(
+      'description callback' => 'userpoints_nc_description_callback',
+    ),
+    'userpoints_nc_node_gain' => array(
+      'description callback' => 'userpoints_nc_description_callback',
+    ),
+    'userpoints_nc_node_loss' => array(
+      'description callback' => 'userpoints_nc_description_callback',
+    ),
+    'userpoints_nc_comment_insert' => array(
+      'description callback' => 'userpoints_nc_description_callback',
+    ),
+    'userpoints_nc_comment_publish' => array(
+      'description callback' => 'userpoints_nc_description_callback',
+    ),
+    'userpoints_nc_comment_unpublish' => array(
+      'description callback' => 'userpoints_nc_description_callback',
+    ),
+    'userpoints_nc_comment_delete' => array(
+      'description callback' => 'userpoints_nc_description_callback',
+    ),
+    'userpoints_nc_comment_gain' => array(
+      'description callback' => 'userpoints_nc_description_callback',
+    ),
+    'userpoints_nc_comment_loss' => array(
+      'description callback' => 'userpoints_nc_description_callback',
+    ),
+  );
+}
+
+/**
+ * Description callback for userpoint operation description/reason.
+ */
+function userpoints_nc_description_callback($operation, $entity) {
+
+  $arguments = array();
+  // Try to load content type name.
+  if ($operation->entity_type == 'comment' && $entity) {
+    $node = node_load($entity->nid);
+    $arguments['%title'] = $node->title;
+  }
+  elseif ($operation->entity_type == 'node' && $entity) {
+    $arguments['%title'] = $entity->title;
+  }
+
+  // Fallback to string content if the above failed for some reason.
+  if (empty($arguments['%title'])) {
+    $arguments['%title'] = t('content');
+  } else {
+    // Shorten title if necessary.
+    if (drupal_strlen($arguments['%title']) > 33) {
+      $arguments['%title'] = truncate_utf8($arguments['%title'], 30, TRUE, TRUE);
+    }
+  }
+
+  switch ($operation->operation) {
+    case 'userpoints_nc_node_insert':
+      return t('Added new %title.', $arguments);
       break;
-    case 'form':
-      $up_orig_com_uid = $comment['uid']['#value'];
+
+    case 'userpoints_nc_node_publish':
+      return t('Published %title.', $arguments);
       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
-              ";
-      $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);
-          }
-        }
-      }
+
+    case 'userpoints_nc_node_unpublish':
+      return t('Unpublished %title.', $arguments);
+      break;
+
+    case 'userpoints_nc_node_delete':
+      return t('Deleted %title.', $arguments);
+      break;
+
+    case 'userpoints_nc_node_gain':
+      return t('Credited as %title author.', $arguments);
       break;
-  }
-}
 
+    case 'userpoints_nc_node_loss':
+      return t('Removed as %title author.', $arguments);
+      break;
+
+    case 'userpoints_nc_comment_insert':
+      return t('Commented on %title.', $arguments);
+      break;
+
+    case 'userpoints_nc_comment_publish':
+      return t('Published comment on %title.', $arguments);
+      break;
+
+    case 'userpoints_nc_comment_unpublish':
+      return t('Unpublished comment on %title.', $arguments);
+      break;
+
+    case 'userpoints_nc_comment_delete':
+      return t('Deleted comment on %title.', $arguments);
+      break;
+
+    case 'userpoints_nc_comment_gain':
+      return t('Credited as %title comment author.', $arguments);
+      break;
+
+    case 'userpoints_nc_comment_loss':
+      return t('Removed as %title comment author.', $arguments);
+      break;
+  }
+}
\ No newline at end of file
