Index: user2userpoints/user2userpoints.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/userpoints_contrib/user2userpoints/user2userpoints.info,v
retrieving revision 1.3
diff -u -p -r1.3 user2userpoints.info
--- user2userpoints/user2userpoints.info	18 Dec 2008 20:13:08 -0000	1.3
+++ user2userpoints/user2userpoints.info	8 Feb 2011 17:31:38 -0000
@@ -3,4 +3,4 @@ name = User to User Points
 description = Users are able to send points to other users.
 package = Userpoints
 dependencies[] = userpoints
-core = 6.x
+core = 7.x
Index: user2userpoints/user2userpoints.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/userpoints_contrib/user2userpoints/user2userpoints.module,v
retrieving revision 1.4.2.2
diff -u -p -r1.4.2.2 user2userpoints.module
--- user2userpoints/user2userpoints.module	27 Nov 2009 02:39:48 -0000	1.4.2.2
+++ user2userpoints/user2userpoints.module	8 Feb 2011 17:31:38 -0000
@@ -4,41 +4,39 @@
 /**
  * @file
  *
- * user2userpoints uses userpoints api to give users ability to give points to others.
+ * user2userpoints uses userpoints api to give users ability to give points to
+ * others.
  *
  * @ingroup userpoints_contrib
  */
 
-define('USER2USERPOINTS_PERM_SEND',       'user2userpoints_send');
-define('USER2USERPOINTS_FIELDSET_TITLE',  'user2userpoints_fieldset_title');
-define('USER2USERPOINTS_TO_LABEL',        'user2userpoints_to_label');
-define('USER2USERPOINTS_AMOUNT_LABEL',    'user2userpoints_amount_label');
-define('USER2USERPOINTS_SHOW_CATEGORIES', 'user2userpoints_show_categories');
-define('USER2USERPOINTS_SHOW_LINKS',     'user2userpoints_show_links');
-
 /**
- * Implements hook_user().
+ * Implements hook_user_view().
  */
-function user2userpoints_user($op, &$edit, &$account, $category = NULL) {
-  if ($op == 'view') {
-    $url = 'user2userpoints/'. $account->name;
-    $items[t('!Points', userpoints_translation())] = array(
-      'value' => l(t('Give !points to '. $account->name, userpoints_translation()), $url),
-      'class' => 'member',
+function user2userpoints_user_view_alter(&$build) {
+  global $user;
+  if (user_access('give points to another user') && $build['#account']->uid != $user->uid) {
+    $build['userpoints']['actions']['#links']['user2userpoints'] = array(
+      'href' => 'user2userpoints/' . $build['#account']->name,
+      'title' => t('Give !points', userpoints_translation()),
+      'query' => drupal_get_destination(),
     );
-    return array(t('!Points', userpoints_translation()) => $items);
   }
 }
 
 /**
- * Implements hook_perm().
+ * Implements hook_permission().
  */
-function user2userpoints_perm() {
-  return array(USER2USERPOINTS_PERM_SEND);
+function user2userpoints_permission() {
+  return array(
+    'give points to another user' => array(
+      'title' => t('Give points to another user'),
+    ),
+  );
 }
 
 /**
- * Implementation of hook_menu().
+ * Implements hook_menu().
  */
 function user2userpoints_menu() {
 
@@ -60,156 +58,167 @@ function user2userpoints_menu() {
    */
   $items['user2userpoints'] = array(
     'title'            => 'Give !points',
+    'title arguments'  => userpoints_translation(),
     'page callback'    => 'drupal_get_form',
     'page arguments'   => array('user2userpoints_giveform', 1, 2, 3),
-    'access arguments' => array(USER2USERPOINTS_PERM_SEND),
-    'type'             => MENU_LOCAL_TASK,
-    'weight'           => 1,
+    'access arguments' => array('give points to another user'),
   );
 
   return $items;
 }
 
 /**
- * Display the give points link below nodes and comments.
+ * Implements hook_node_view().
  */
-function user2userpoints_link($type, $node = NULL, $teaser = FALSE) {
+function user2userpoints_node_view($node, $view_mode) {
   global $user;
-  if (variable_get(USER2USERPOINTS_SHOW_LINKS,TRUE)) {
+  $enabled_types = array_filter(variable_get('user2userpoints_show_links', array()));
+  if (in_array($node->type, $enabled_types) && user_access('give points to another user') && $node->uid == $user->uid) {
     $links = array();
-    if ($node->uid != $user->uid) {
-      if ($node->type != 'page') {
-        if (user_access(USER2USERPOINTS_PERM_SEND)) {
-          $links['user2userpoints'] = array(
-          'title' => t('Give !points', userpoints_translation()),
-          'href'  => 'user2userpoints/'. $node->name
-          );
-        }
-      }
-    }
-    return $links;
+    $links['user2userpoints'] = array(
+      'title' => t('Give !points to author', userpoints_translation()),
+      'href'  => 'user2userpoints/' . $node->name,
+      'query' => drupal_get_destination(),
+    );
+    $node->content['links']['user2userpoints'] = array(
+      '#theme' => 'links__user2userpoints',
+      '#links' => $links,
+      '#attributes' => array('class' => array('links', 'inline')),
+    );
   }
 }
 
-function user2userpoints_giveform(&$form_state,  $account = NULL, $amount = 0, $tid = 0) {
-  if (is_numeric($account)) {
-    $to = user_load(array('uid' => $to_id));
-    $account =  $to->name;
+/**
+ * Form builder function to display form to give another user points.
+ */
+function user2userpoints_giveform($form, &$form_state,  $account = NULL, $amount = 0, $tid = NULL) {
+  if ($account > 0) {
+    $to = user_load($account);
+    $account = $to->name;
   }
 
-  $default_tid = variable_get(USERPOINTS_CATEGORY_DEFAULT_TID, NULL);
-  if ($default_tid) {
-    if (! $tid) {
-      $tid = $default_tid;
-    }
+  if ($tid === NULL) {
+    $tid = variable_get('user2userpoints_default_category', userpoints_get_default_tid());
   }
 
-  $form = array();
-
-  $form['points'] = array(
-    '#type'        => 'fieldset',
-    '#title'       => variable_get(USER2USERPOINTS_FIELDSET_TITLE, 'Give Points'),
-    '#collapsible' => TRUE,
-    '#collapsed'   => FALSE,
+  $form['description'] = array(
+    '#markup' => t('When you give !points to another user, the corresponding number of !points will be deducted from your !points balance.', userpoints_translation()),
   );
 
-  $form['points']['to'] = array(
+  $form['to'] = array(
     '#type'              => 'textfield',
-    '#title'             => variable_get(USER2USERPOINTS_TO_LABEL, 'To'),
+    '#title'             => t('To'),
     '#autocomplete_path' => 'user/autocomplete',
     '#default_value'     => $account,
     '#size'              => 20,
     '#maxlength'         => 64,
-    '#required'          => TRUE
+    '#required'          => TRUE,
   );
 
-  $form['points']['amount'] = array(
+  $form['amount'] = array(
     '#type'          => 'textfield',
-    '#title'         => variable_get(USER2USERPOINTS_AMOUNT_LABEL, 'Amount'),
+    '#title'         => t('Amount'),
     '#default_value' => $amount,
     '#size'          => 20,
-    '#required'      => TRUE
+    '#required'      => TRUE,
   );
 
-  if ($default_tid) {
-    if (variable_get(USER2USERPOINTS_SHOW_CATEGORIES, 1) === 0 ) {
-      $form['points']['tid'] = array(
-      '#type'          => 'hidden',
-      '#default_value' => $tid,
-      '#required'      => TRUE,
-      );
-    }
-    else {
-      $form['points']['tid'] = array(
-      '#type'          => 'select',
-      '#title'         =>  t('Type'),
-      '#default_value' => $tid,
-      '#required'      => TRUE,
-      '#options'       => userpoints_get_categories()
-      );
-    }
+  if (!variable_get('user2userpoints_show_categories', 1)) {
+    $form['tid'] = array(
+    '#type'          => 'hidden',
+    '#default_value' => $tid,
+    '#required'      => TRUE,
+    );
   }
+  else {
+    $form['tid'] = array(
+    '#type'          => 'select',
+    '#title'         =>  t('Type'),
+    '#default_value' => $tid,
+    '#required'      => TRUE,
+    '#options'       => userpoints_get_categories()
+    );
+  }
+
+  $form['reason'] = array(
+    '#type'        => 'textarea',
+    '#title'       => t('Reason'),
+    '#description' => t('Provide a reason that will be shown to the user recieving the points. This is optional.')
+  );
 
-  $form[] = array(
+  $form['submit'] = array(
     '#type'  => 'submit',
     '#value' => t('Give !Points', userpoints_translation())
   );
   return $form;
 }
 
-function user2userpoints_giveform_validate($form, $form_state) {
+/**
+ * Validate form to give another user points.
+ */
+function user2userpoints_giveform_validate($form, &$form_state) {
   global $user;
 
-  /* Check the name to be valid. */
+  // Check the name to be valid.
   if (!empty($form_state['values']['to'])) {
     $to_id = $form_state['values']['to'];
-    $field = (is_int($to_id)) ? 'uid' : 'name';
-    $to = user_load(array($field => $to_id));
+    if ($to_id > 0) {
+      $to = user_load($to_id);
+    }
+    else {
+      $to = user_load_by_name($to_id);
+    }
 
     if (!$to->uid) {
       form_set_error('to', t('That is not a valid user.'));
+      return;
     }
 
     if ($to->uid == $user->uid) {
       form_set_error('to', t("You can't give !points to yourself.", userpoints_translation()));
+      return;
     }
   }
 
   /* Check the value to be valid. */
-  if (!empty($form_state['values']['amount'])) {
-    if ($form_state['values']['amount'] < 1) {
-      form_set_error('amount', t("You can't give less than one !point.", userpoints_translation()));
-    }
-
-    $tid = ($form_state['values']['tid']) ? $form_state['values']['tid'] : arg(2);
-    if (!$tid) {
-      $tid = 'all';
-    }
+  if ($form_state['values']['amount'] < 1) {
+    form_set_error('amount', t("You can't give less than one !point.", userpoints_translation()));
+    return;
+  }
 
-    if ($form_state['values']['amount'] > userpoints_get_current_points($user->uid, $tid)) {
-      form_set_error('amount', t("You don't have enough !points for that.", userpoints_translation()));
-    }
+  $tid = isset($form_state['values']['tid']) ? $form_state['values']['tid'] : userpoints_get_default_tid();
+  $current_points = userpoints_get_current_points($user->uid, $tid);
+  if ($form_state['values']['amount'] > $current_points) {
+    $categories = userpoints_get_categories();
+    $arguments = array_merge(userpoints_translation() + array('@points' => $current_points, '@category' => $categories[$tid]));
+    form_set_error('amount', t("You don't have enough !points available in the @category category to give away that many. You currently have @points !points available.", $arguments));
+    return;
   }
+  // Save account so that it can be used by the submit function.
+  $form_state['validated_account'] = $to;
 }
 
+/**
+ * Submit function to give another user points.
+ */
 function user2userpoints_giveform_submit($form, &$form_state) {
   global $user;
 
   $points = $form_state['values']['amount'];
-  $to_id  = $form_state['values']['to'];
+  $to = $form_state['validated_account'];
 
   $tid = NULL;
-  if ($form_state['values']['tid']) {
+  if (isset($form_state['values']['tid'])) {
     $tid = $form_state['values']['tid'];
   }
 
-  $field = (is_int($to_id)) ? 'uid' : 'name';
-  $to = user_load(array($field => $to_id));
-
   $params = array(
     'uid'    => $to->uid,
     'points' => $points,
-    'event'  => 'From: '. $user->name,
+    'operation'  => 'user2userpoints_received',
+    'entity_type' => 'user',
+    'entity_id' => $user->uid,
+    'description' => $form_state['values']['reason'],
     'tid'    => $tid,
   );
   userpoints_userpointsapi($params);
@@ -217,12 +226,59 @@ function user2userpoints_giveform_submit
   $params = array(
     'uid'    => $user->uid,
     'points' => -$points,
-    'event'  => 'To: '. $to->name,
+    'operation'  => 'user2userpoints_given',
+    'entity_type' => 'user',
+    'entity_id' => $to->uid,
+    'description' => $form_state['values']['reason'],
     'tid'    => $tid,
   );
   userpoints_userpointsapi($params);
 }
 
+/**
+ * Implements hook_userpoints_info().
+ */
+function user2userpoints_userpoints_info() {
+  return array(
+    'user2userpoints_received' => array(
+      'description callback' => 'user2userpoints_description_callback',
+    ),
+    'user2userpoints_given' => array(
+      'description callback' => 'user2userpoints_description_callback',
+    ),
+  );
+}
+
+/**
+ * Description callback for userpoint operation description/reason.
+ */
+function user2userpoints_description_callback($operation, $entity) {
+
+  $arguments = userpoints_translation();
+  // Load the username.
+  if ($operation->entity_type == 'user' && $entity) {
+    $arguments['%name'] = format_username($entity);
+  }
+
+  // Fallback to string content if the above failed for some reason.
+  if (empty($arguments['%title'])) {
+    $arguments['%title'] = t('user');
+  }
+
+  switch ($operation->operation) {
+    case 'user2userpoints_given':
+      return t('Gave !points to %name', $arguments);
+      break;
+
+    case 'user2userpoints_given':
+      return t('Received !points from %name', $arguments);
+      break;
+  }
+}
+
+/**
+ * Implements hook_userpoints().
+ */
 function user2userpoints_userpoints($op, $params = array()) {
   switch ($op) {
     case 'setting':
@@ -231,47 +287,34 @@ function user2userpoints_userpoints($op,
         '#title'       => t('User 2 User Points'),
         '#collapsible' => TRUE,
         '#collapsed'   => TRUE,
+        '#group'       => 'settings_additional',
+        '#weight'      => 30,
       );
 
-      $form['user2user'][USER2USERPOINTS_FIELDSET_TITLE] = array(
-        '#type'          => 'textfield',
-        '#title'         => t('Fieldset heading to use in the interface'),
-        '#default_value' => variable_get(USER2USERPOINTS_FIELDSET_TITLE, t('Give !Points', userpoints_translation())),
-        '#size'          => 40,
-        '#maxlength'     => 40,
-      );
-
-      $form['user2user'][USER2USERPOINTS_TO_LABEL] = array(
-        '#type'          => 'textfield',
-        '#title'         => t('Word to use in the interface "to" label !Points', userpoints_translation()),
-        '#default_value' => variable_get(USER2USERPOINTS_TO_LABEL, 'To'),
-        '#size'          => 20,
-        '#maxlength'     => 20,
+      $form['user2user']['user2userpoints_show_categories'] = array(
+        '#type'          => 'checkbox',
+        '#title'         => t('Show categories selection if there is more than one'),
+        '#default_value' => variable_get('user2userpoints_show_categories', TRUE),
+        '#description'   => t('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_AMOUNT_LABEL] = array(
-        '#type'          => 'textfield',
-        '#title'         => t('Word to use in the interface "amount" label !Points', userpoints_translation()),
-        '#default_value' => variable_get(USER2USERPOINTS_AMOUNT_LABEL, 'Amount'),
-        '#size'          => 20,
-        '#maxlength'     => 20,
+      $options = userpoints_get_categories(NULL);
+      $form['user2user']['user2userpoints_default_category'] = array(
+        '#type' => 'select',
+        '#title' => t('Default category to use when donating !points', userpoints_translation()),
+        '#default_value' => variable_get('user2userpoints_default_category', userpoints_get_default_tid()),
+        '#options' => $options,
+        '#description' => t('Choose which category is selected by default when donating !points. This also works if the category selection is not shown but it can be overriden with an argument.', userpoints_translation()),
       );
 
-      $form['user2user'][USER2USERPOINTS_SHOW_CATEGORIES] = array(
-        '#type'          => 'checkbox',
-        '#title'         => 'Show category select input if categories enabled',
-        '#default_value' => variable_get(USER2USERPOINTS_SHOW_CATEGORIES, TRUE),
-        '#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_SHOW_LINKS] = array(
-        '#type'          => 'checkbox',
-        '#title'         => t("Show 'Give Userpoints' links beneath nodes"),
-        '#default_value' => TRUE,
-        '#description'   => t("Nodes that are not of type 'page' will have a 'Give points' link added to them"),
+      $form['user2user']['user2userpoints_show_links'] = array(
+        '#type'          => 'checkboxes',
+        '#title'         => t('Choose which content types should show a donate points link'),
+        '#default_value' => variable_get('user2userpoints_show_links', array()),
+        '#options'       => node_type_get_names(),
+        '#description'   => t('Only the checked content types will show a donate points link.')
       );
       return $form;
       break;
   }
 }
-
-
