--- user2userpoints.module.old	2009-06-02 16:49:06.000000000 -0700
+++ user2userpoints.module	2009-06-02 21:29:09.000000000 -0700
@@ -14,6 +14,7 @@
 define('USER2USERPOINTS_TO_LABEL',        'user2userpoints_to_label');
 define('USER2USERPOINTS_AMOUNT_LABEL',    'user2userpoints_amount_label');
 define('USER2USERPOINTS_SHOW_CATEGORIES', 'user2userpoints_show_categories');
+define('USER2USERPOINTS_NODE_TYPES',      'user2userpoints_node_types');
 
 /**
  * Implements hook_user().
@@ -48,6 +49,7 @@
    *   [to] is uid or username of user points are given to
    *   [amount] (optional) is points quantity.  defaults to 1 if empty.
    *   [tid] (optional) taxonomy id, 'choose' to have user choose, 'all' to pass all param to userpoints api
+   *   [nid] (optional) node id
    * 
    * For example:
    *
@@ -56,11 +58,13 @@
    *   user2userpoints/johndoe/5     (apply 5 points to johndoe and let them select category)
    *   user2userpoints/johndoe/5/51  (apply 5 points to johndoe in in term 51)
    *   user2userpoints/11/5/51       (apply 5 points to uid #11 in in term 51. User may select another term)
+   *   user2userpoints/johndoe/5/51/234
+   *   user2userpoints/11/5/0/234
    */
   $items['user2userpoints'] = array(
     'title'            => 'Give !points',
     'page callback'    => 'drupal_get_form',
-    'page arguments'   => array('user2userpoints_giveform', 1, 2, 3),
+    'page arguments'   => array('user2userpoints_giveform', 1, 2, 3, 4),
     'access arguments' => array(USER2USERPOINTS_PERM_SEND),
     'type'             => MENU_LOCAL_TASK,
     'weight'           => 1,
@@ -75,12 +79,13 @@
 function user2userpoints_link($type, $node = NULL, $teaser = FALSE) {
   global $user;
   $links = array();
-  if ($node->uid != $user->uid) {
-    if ($node->type != 'page') {
+  if (isset($node) && $node->uid != $user->uid) {
+    $types = variable_get(USER2USERPOINTS_NODE_TYPES, NULL);
+    if (!isset($types) || $types[$node->type]) {
       if (user_access(USER2USERPOINTS_PERM_SEND)) {
         $links['user2userpoints'] = array(
           'title' => t('Give !points', userpoints_translation()),
-          'href'  => 'user2userpoints/'. $node->name
+          'href'  => 'user2userpoints/'. $node->name .'/0/0/'. $node->nid
         );
       }
     }
@@ -88,7 +93,7 @@
   return $links;
 }
 
-function user2userpoints_giveform(&$form_state,  $account = NULL, $amount = 0, $tid = 0) {
+function user2userpoints_giveform(&$form_state,  $account = NULL, $amount = 0, $tid = 0, $nid = 0) {
   if (is_numeric($account)) {
     $to = user_load(array('uid' => $to_id));
     $account =  $to->name;
@@ -101,6 +106,13 @@
     }
   }
 
+  $title = t('Give !points', userpoints_translation());
+  if ($nid) {
+    $node = node_load($nid);
+    $title = t('Give !points for %node', userpoints_translation() + array('%node' => $node->title));
+  }
+  drupal_set_title($title);
+
   $form = array();
 
   $form['points'] = array(
@@ -147,6 +159,12 @@
     }
   }
 
+  $form['points']['nid'] = array(
+    '#type'           => 'hidden',
+    '#default_value'  => $nid,
+    '#required'       => TRUE,
+  );
+
   $form[] = array(
     '#type'  => 'submit',
     '#value' => t('Give !Points', userpoints_translation())
@@ -203,21 +221,38 @@
   $field = (is_int($to_id)) ? 'uid' : 'name';
   $to = user_load(array($field => $to_id));
 
+  $nid = NULL;
+  $entity = NULL;
+  if ($form_state['values']['nid']) {
+    $nid = $form_state['values']['nid'];
+    $entity = 'node';
+  }
+
   $params = array(
-    'uid'    => $to->uid,
-    'points' => $points,
-    'event'  => 'From: '. $user->name,
-    'tid'    => $tid,
+    'uid'         => $to->uid,
+    'points'      => $points,
+    'event'       => 'From: '. $user->name,
+    'tid'         => $tid,
+    'operation'   => 'user2user',
+    'entity_id'   => $nid,
+    'entity_type' => $entity,
   );
   userpoints_userpointsapi($params);
 
   $params = array(
-    'uid'    => $user->uid,
-    'points' => -$points,
-    'event'  => 'To: '. $to->name,
-    'tid'    => $tid,
+    'uid'         => $user->uid,
+    'points'      => -$points,
+    'event'       => 'To: '. $to->name,
+    'tid'         => $tid,
+    'operation'   => 'user2user',
+    'entity_id'   => $nid,
+    'entity_type' => $entity,
   );
   userpoints_userpointsapi($params);
+
+  if ($nid) {
+    $form_state['redirect'] = 'node/'. $nid;
+  }
 }
 
 function user2userpoints_userpoints($op, $params = array()) {
@@ -261,9 +296,23 @@
         '#description'   => 'If a taxonomy id is passed via the url or post or get form, the taxonomy id will still be assigned. It will be passed along as a hidden input in the form.',
       );
 
+      $form['user2user'][USER2USERPOINTS_NODE_TYPES] = array(
+        '#title' => 'Content types',
+        '#type' => 'checkboxes',
+        '#options' => node_get_types('names'),
+        '#default_value' => variable_get(USER2USERPOINTS_NODE_TYPES, NULL),
+        '#description' => 'Content types for which the module will show the "Give points" link.',
+      );
+
       return $form;
       break;
   }
 }
 
+/**
+ * Get the points that a user received for a particular node.
+ */
+function user2userpoints_nodepoints($node) {
+  return db_result(db_query("SELECT SUM(points) FROM {userpoints_txn} WHERE operation='user2user' AND entity_type='node' AND entity_id=%d AND uid=%d", $node->nid, $node->uid));
+}
 
