? .cvsignore
? after.png
? before.png
? rules_port.patch
? rules_port2.patch
? rules_port3.patch
? userpoints_admin_vertical_tabs.patch
Index: userpoints_rules.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/userpoints/userpoints_rules.module,v
retrieving revision 1.1.4.1
diff -u -p -r1.1.4.1 userpoints_rules.module
--- userpoints_rules.module	30 Jul 2010 11:20:22 -0000	1.1.4.1
+++ userpoints_rules.module	19 Oct 2010 17:08:58 -0000
@@ -9,11 +9,52 @@
 /**
  * Rules events -Trigger event when user points awarded.
  */
-function userpoints_rules_userpoints($op, $params='') {
+function userpoints_rules_userpoints($op, &$params = array()) {
+  // Transactions without points are not passed to rules for now.
+  if (!is_array($params) || !isset($params['points'])) {
+   return;
+  }
+
+  $event = '';
   switch ($op) {
-  case 'points after':
-    $account = user_load($params['uid']);
-    rules_invoke_event('userpoints_event_points_awarded', $account, (object)$params);
-    break;
+    case 'points after':
+        $event = 'userpoints_event_points_awarded_after';
+        break;
+
+    case 'points before':
+        $event = 'userpoints_event_points_awarded_before';
+        break;
+  }
+
+  if (!empty($event)) {
+    // Convert to an object.
+    $userpoints_transaction = (object) $params;
+
+    // Provide uid as wrapped user entity.
+    $userpoints_transaction->user = entity_metadata_wrapper('user', $userpoints_transaction->uid);
+
+    $userpoints_transaction->entity = NULL;
+    if (!empty($userpoints_transaction->entity_type) && !empty($userpoints_transaction->entity_id)) {
+      // Make entity available as lazy loading wrapper.
+      $userpoints_transaction->entity = entity_metadata_wrapper($userpoints_transaction->entity_type, $userpoints_transaction->entity_id);
+    }
+    // Invoke rules event.
+    rules_invoke_event($event, $userpoints_transaction);
+    // Convert back to keep any changes to the properties.
+    $params = (array)$userpoints_transaction;
+
+    $params['uid'] = $userpoints_transaction->user->getIdentifier();
+    unset($params['user']);
+
+    if ($userpoints_transaction->entity) {
+      $params['entity_type'] = $userpoints_transaction->entity->type();
+      try {
+        $params['entity_id'] = $userpoints_transaction->entity->getIdentifier();
+      } catch (Exception $e) {
+        debug('bla');
+        throw $e;
+      }
+    }
+    unset($params['entity']);
   }
 }
\ No newline at end of file
Index: userpoints_rules.rules.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/userpoints/userpoints_rules.rules.inc,v
retrieving revision 1.1.4.1
diff -u -p -r1.1.4.1 userpoints_rules.rules.inc
--- userpoints_rules.rules.inc	30 Jul 2010 11:20:22 -0000	1.1.4.1
+++ userpoints_rules.rules.inc	19 Oct 2010 17:08:58 -0000
@@ -7,26 +7,6 @@
  */
 
 /**
- * Implements hook_rules_data_type_info().
- */
-function userpoints_rules_rules_data_type_info() {
-  return array(
-    'userpoints_transaction' => array(
-      'label' => t('Userpoints transaction'),
-      'property info' => array(
-        'points' => array('label' => t('!Points', userpoints_translation()), 'type' => 'integer'),
-        'tid' => array('label' => t('Userpoints category'), 'type' => 'integer'),
-        'entity_type' => array('label' => t('Entity type'), 'type' => 'text'),
-        'entity_id' => array('label' => t('Entity id'), 'type' => 'integer'),
-        'description' => array('label' => t('Description'), 'type' => 'text'),
-        'operation' => array('label' => t('Operation'), 'type' => 'text'),
-      ),
-      'hidden' => TRUE,
-    ),
-  );
-}
-
-/**
  * Implements hook_rules_action_info().
  */
 function userpoints_rules_rules_action_info() {
@@ -34,198 +14,286 @@ function userpoints_rules_rules_action_i
     'userpoints_action_grant_points' => array(
       'label' => t('Grant !points to a user', userpoints_translation()),
       'parameter' => array(
-          'user' => array('type' => 'user', 'label' => t('User')),
-          'points' => array('type' => 'integer', 'label' => t('!Points', userpoints_translation())),
-          'tid' => array('label' => t('Userpoints category'), 'type' => 'integer'),
-          'entity_type' => array('label' => t('Entity type'), 'type' => 'text'),
-          'entity_id' => array('label' => t('Entity id'), 'type' => 'integer'),
-          'description' => array('label' => t('Description'), 'type' => 'text'),
-          'operation' => array('label' => t('Operation'), 'type' => 'text'),
+        'user' => array(
+          'type' => 'user',
+          'label' => t('User'),
+          'description' => t('The user that will receive the !points', userpoints_translation()),
+        ),
+        'points' => array(
+          'type' => 'integer',
+          'label' => t('!Points', userpoints_translation()),
+          'description' => t('Amount of !points to give or take.', userpoints_translation()),
+          'restriction' => 'input',
+        ),
+        'tid' => array(
+          'label' => t('!Points category', userpoints_translation()),
+          'type' => 'integer',
+          'options list' => 'userpoints_rules_get_categories',
+          'restriction' => 'input',
+        ),
+        'entity' => array(
+          'label' => t('Entity'),
+          'type' => 'entity',
+          'description' => t('The entity to which this transaction refers.'),
+          'optional' => TRUE,
+         ),
+        'description' => array(
+          'label' => t('Description'),
+          'type' => 'text',
+          'description' => t('Can contain the reason why the points have been granted.'),
+          'restriction' => 'input',
+          'optional' => TRUE,
+        ),
+        'operation' => array(
+          'label' => t('Operation'),
+          'type' => 'text',
+          'description' => t('Describes the operation (Insert/Remove/...).'),
+          'restriction' => 'input',
+        ),
+        'reference' => array(
+          'label' => t('Reference'),
+          'type' => 'text',
+          'description' => t('Can contain a reference for this transaction.'),
+          'optional' => TRUE,
         ),
+        'display' => array(
+          'label' => t('Display'),
+          'type' => 'boolean',
+          'description' => t('Whether or not to show a message to the user when this !points transaction is added.', userpoints_translation()),
+          'default value' => TRUE,
+        ),
+        'status' => array(
+          'label' => t('Moderate'),
+          'type' => 'text',
+          'description' => t('Whether or not this !points transaction should be moderated.', userpoints_translation()),
+          'options list' => 'userpoints_rules_moderate',
+        ),
+        'expirydate' => array(
+          'label' => t('Expiration Date'),
+          'type' => 'date',
+          'description' => t('Define when the !points should expire.', userpoints_translation()),
+          'optional' => TRUE,
+        ),
+      ),
+      'group' => t('!Points', userpoints_translation()),
+    ),
+    'userpoints_rules_get_current_points' => array(
+      'label' => t('Load !points of a user', userpoints_translation()),
+      'parameter' => array(
+        'user' => array(
+          'type' => 'user',
+          'label' => t('User'),
+          'description' => t('The user that will receive the !points', userpoints_translation()),
+        ),
+        'tid' => array(
+          'label' => t('!Points category', userpoints_translation()),
+          'type' => 'text',
+          'options list' => 'userpoints_rules_get_all_categories',
+        ),
+      ),
+      'new variables' => array(
+        'loaded_points' => array(
+          'type' => 'integer',
+          'label' => t('Number of !points in the specified category.', userpoints_translation()),
+        ),
+      ),
       'group' => t('!Points', userpoints_translation()),
     ),
   );
 }
 
 /**
- * Implements hook_rules_event_info().
+ * Wrapper function for userpoints_get_categories().
+ *
+ * Rules.module uses different arguments for option list callbacks than
+ * userpoints_get_categories expectes.
  */
-function userpoints_rules_rules_event_info() {
-  return array(
-    'userpoints_event_points_awarded' => array(
-       'label' => t('User was awarded !points', userpoints_translation()),
-       'variables' => array(
-          'user' => array('type' => 'user', 'label' => t('User')),
-          'userpoints_transaction' => array('type' => 'userpoints_transaction', 'label' => t('Userpoints transaction'))
-        ),
-       'group' => t('!Points', userpoints_translation()),
-    ),
-  );
+function userpoints_rules_get_categories() {
+  return userpoints_get_categories();
 }
 
 /**
- * Rules action - grant points to a user.
+ * Simple callback that lists the categories including an option for all.
  */
-function userpoints_action_grant_points($account, $points, $tid, $type, $id, $desc, $op) {
-  userpoints_userpointsapi(array(
-    'uid' => $account->uid,
-    'points' => $points,
-    'tid' => $tid,
-    'entity_type' => $type,
-    'entity_id' => $id,
-    'description' => $desc,
-    'operation' => $op,
-  ));
+function userpoints_rules_get_all_categories() {
+  return array('all' => t('All categories')) + userpoints_get_categories();
 }
 
 /**
- * Rules action form configuration - allow number of points to be set.
+ * Simple callback that liss the possible moderate values.
  */
-function userpoints_action_grant_points_form2($settings = array(), &$form) {
-  $form['settings']['points'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Number of points'),
-    '#default_value' => isset($settings['points']) ? $settings['points'] : '',
-    '#description' => t('The number of !points to award. You may enter a negative number as-well', userpoints_translation())
-  );
-  $form['settings']['description'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Description'),
-    '#default_value' => isset($settings['description']) ? $settings['description'] : '',
-    '#description' => t('Points description')
-  );
-  $form['settings']['operation'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Operation'),
-    '#default_value' => isset($settings['operation']) ? $settings['operation'] : ''
-  );
-  $form['settings']['entity_type'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Entity Type'),
-    '#default_value' => isset($settings['entity_type']) ? $settings['entity_type'] : '',
-    '#description' => t("Entity type. ex. 'node' or 'user'")
-  );
-  $form['settings']['entity_id'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Entity Id'),
-    '#default_value' => isset($settings['entity_id']) ? $settings['entity_id'] : '',
-    '#description' => t('ID of an entity in the Database. ex. $node->id or $user->uid')
+function userpoints_rules_moderate() {
+  return array(
+    'default' => t('Use the site default'),
+    'approved' => t('Automatically approved'),
+    'moderated' => t('Added to moderation'),
   );
-  //$form['settings']['tid'] = taxonomy_form(userpoints_get_vid(), isset($settings['tid']) ? $settings['tid'] : '', 'The category ID');
-  return $form;
 }
 
-
 /**
- * Rules Conditions - compare userpoints with a defined amount
+ * Implements hook_rules_data_info().
  */
+function userpoints_rules_rules_data_info() {
+  return array(
+    'userpoints_transaction' => array(
+      'label' => t('!Points transaction', userpoints_translation()),
+      'wrap' => TRUE,
+      'property info' => _userpoints_rules_userpoints_transaction_properties(),
+    ),
+  );
+}
 
 /**
- * Implements hook_rules_condition_info().
+ * Returns properties of userpoints_transaction data object.
  */
-function userpoints_rules_rules_condition_info() {
+function _userpoints_rules_userpoints_transaction_properties() {
   return array(
-    'userpoints_rules_category' => array(
-      'label' => t('Check the transaction category'),
-      'parameter' => array(
-        'userpoints_transaction' => array('type' => 'userpoints_transaction', 'label' => t('Userpoints transaction'))
-      ),
-      'group' => t('!Points', userpoints_translation()),
+    'user' => array(
+      'type' => 'user',
+      'label' => t('User'),
+      'description' => t('The user that will receive the !points', userpoints_translation()),
+      'setter callback' => 'entity_metadata_verbatim_set',
     ),
-    'userpoints_rules_amount_before' => array(
-      'label' => t('Compare Userpoints before the transaction'),
-      'parameter' => array(
-        'user' => array('type' => 'user', 'label' => t('User')),
-        'userpoints_transaction' => array('type' => 'userpoints_transaction', 'label' => t('Userpoints transaction'))
-      ),
-      'group' => t('!Points', userpoints_translation()),
+    'points' => array(
+      'type' => 'integer',
+      'label' => t('!Points', userpoints_translation()),
+      'description' => t('Amount of !points to give or take.', userpoints_translation()),
+      'restriction' => 'input',
+      'setter callback' => 'entity_metadata_verbatim_set',
     ),
-    'userpoints_rules_transaction_amount' => array(
-      'label' => t('Compare the transaction amount'),
-      'parameter' => array(
-        'userpoints_transaction' => array('type' => 'userpoints_transaction', 'label' => t('Userpoints transaction'))
-      ),
-      'group' => t('!Points', userpoints_translation()),
+    'tid' => array(
+      'label' => t('!Points category', userpoints_translation()),
+      'type' => 'integer',
+      'options list' => 'userpoints_rules_get_categories',
+      'restriction' => 'input',
+      'setter callback' => 'entity_metadata_verbatim_set',
     ),
-    'userpoints_rules_amount' => array(
-      'label' => t('Compare current Userpoints'),
-      'parameter' => array(
-        'user' => array('type' => 'user', 'label' => t('User')),
-      ),
-      'group' => t('!Points', userpoints_translation()),
+    'entity' => array(
+      'label' => t('Entity'),
+      'type' => 'entity',
+      'description' => t('The entity to which this transaction refers.'),
+      'restriction' => 'input',
+      'optional' => TRUE,
+      'getter callback' => 'entity_metadata_verbatim_get',
+     ),
+    'description' => array(
+      'label' => t('Description'),
+      'type' => 'text',
+      'description' => t('Can contain the reason why the points have been given.'),
+      'restriction' => 'input',
+      'optional' => TRUE,
+      'setter callback' => 'entity_metadata_verbatim_set',
+    ),
+    'reference' => array(
+      'label' => t('Reference'),
+      'type' => 'text',
+      'description' => t('Can contain a reference for this transaction.'),
+      'optional' => TRUE,
+      'setter callback' => 'entity_metadata_verbatim_set',
+    ),
+    'operation' => array(
+      'label' => t('Operation'),
+      'type' => 'text',
+      'description' => t('Describes the operation (Insert/Remove/...).'),
+      'restriction' => 'input',
+      'setter callback' => 'entity_metadata_verbatim_set',
+    ),
+    'time-stamp' => array(
+      'label' => t('Timestamp'),
+      'type' => 'date',
+      'description' => t('Time when the points were given.'),
+      'setter callback' => 'entity_metadata_verbatim_set',
+      'getter callback' => 'entity_metadata_verbatim_get',
+    ),
+    'expirydate' => array(
+      'label' => t('Expiry date'),
+      'type' => 'date',
+      'description' => t('Time when the points will expire.'),
+      'setter callback' => 'entity_metadata_verbatim_set',
+      'getter callback' => 'entity_metadata_verbatim_get',
+    ),
+    'display' => array(
+      'label' => t('Display'),
+      'type' => 'boolean',
+      'description' => t('Wether to show a message to the user for this transaction or not.'),
+      'setter callback' => 'entity_metadata_verbatim_set',
+    ),
+    'status' => array(
+      'label' => t('Status'),
+      'type' => 'integer',
+      'description' => t('Status of this transaction.'),
+      'options list' => 'userpoints_txn_status',
     ),
   );
 }
 
 /**
- * Rules Condition form configuration - set the amount to compare
+ * Implements hook_rules_event_info().
  */
-function userpoints_rules_amount_form2($settings = array(), &$form, $use_category = TRUE) {
-  if ($use_category) {
-    $form['settings']['type'] = array(
-      '#type' => 'select',
-      '#title' => t('Userpoints category'),
-      '#options' => userpoints_get_categories(),
-      '#default_value' => isset($settings['type']) ? $settings['type'] : '',
-      '#required' => TRUE,
-    );
-  }
-
-  $form['settings']['amount'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Amount to compare'),
-    '#default_value' => isset($settings['amount']) ? $settings['amount'] : '',
-    '#description' => t('The amount to compare if userpoints are >=. Example:30, will trigger the condition if the user userpoints are >= than 30 points.')
+function userpoints_rules_rules_event_info() {
+  return array(
+    'userpoints_event_points_awarded_before' => array(
+       'label' => t('User will be awarded !points', userpoints_translation()),
+       'variables' => array(
+          'userpoints_transaction' => array(
+            'type' => 'userpoints_transaction',
+            'label' => t('!Points transaction', userpoints_translation()),
+          )
+        ),
+       'group' => t('!Points', userpoints_translation()),
+    ),
+    'userpoints_event_points_awarded_after' => array(
+       'label' => t('User was awarded !points', userpoints_translation()),
+       'variables' => array(
+          'userpoints_transaction' => array(
+            'type' => 'userpoints_transaction',
+            'label' => t('!Points transaction', userpoints_translation()),
+          )
+        ),
+       'group' => t('!Points', userpoints_translation()),
+    ),
   );
 }
 
 /**
- * Condition: check current Userpoints
- */
-function userpoints_rules_amount($account, $settings) {
-  $balance = userpoints_get_current_points($account->uid, $settings['type']);
-  return $balance >= $settings['amount'];
-}
-
-/**
- * Condition: check transaction amount
- */
-function userpoints_rules_transaction_amount($transaction, $settings) {
-  return $transaction['points'] >= $settings['amount'];
-}
-
-function userpoints_rules_transaction_amount_form2($settings = array(), &$form) {
-  return userpoints_rules_amount_form($settings, $form, FALSE);
-}
-
-/**
- * Condition: check Userpoints before the transaction
+ * Rules action - grant points to a user.
  */
-function userpoints_rules_amount_before($account, $transaction, $settings) {
-  $balance = userpoints_get_current_points($account->uid, $settings['type']) - $transaction['points'];
-  return $balance >= $settings['amount'];
-}
-
-function userpoints_rules_amount_before_form2($settings = array(), &$form) {
-  return userpoints_rules_amount_form($settings, $form);
+function userpoints_action_grant_points($account, $points, $tid, $entity, $desc, $op, $reference, $display, $moderate, $expirydate, $options, $state) {
+  // The passed in $entity is the unwrapped object. To get type and id, we need
+  // the wrapped version of it.
+  $entity = $state->currentArguments['entity'];
+  
+  // Map $moderate value to the actual value used by the API.
+  $moderate_mapping = array(
+    'default' => NULL,
+    'approved' => FALSE,
+    'moderated' => TRUE,
+  );
+  $params = array(
+    'uid' => $account->uid,
+    'points' => $points,
+    'tid' => $tid,
+    'entity_type' => $entity ? $entity->type() : NULL,
+    'entity_id' => $entity ? $entity->getIdentifier() : NULL,
+    'description' => $desc,
+    'operation' => $op,
+    'reference' => $reference,
+    'display' => $display,
+    'moderate' => $moderate_mapping[$moderate],
+  );
+  userpoints_userpointsapi($params);
 }
 
-/**
- * Condition: check the transaction category
- */
-function userpoints_rules_category($transaction, $settings) {
-  return $transaction['tid'] == $settings['category'];
+function userpoints_action_grant_points_form_alter(&$form, &$form_state) {
+  // Empty value by default.
+  $form['parameter']['expirydate']['settings']['expirydate']['#default_value'] = '';
+  // Operation is a single line textfield.
+  $form['parameter']['operation']['settings']['operation']['#type'] = 'textfield';
 }
 
 /**
- * Condition: check the transaction category configuration form
+ * Rules action - load points of a user.
  */
-function userpoints_rules_category_form2($settings = array(), &$form) {
-  $form['settings']['category'] = array(
-    '#type' => 'select',
-    '#title' => t('Userpoints category'),
-    '#options' => userpoints_get_categories(),
-    '#default_value' => isset($settings['category']) ? $settings['category'] : '',
-    '#required' => TRUE,
-  );
+function userpoints_rules_get_current_points($account, $tid) {
+  return array('loaded_points' => userpoints_get_current_points($account->uid, $tid));
 }
