--- C:/Net/drupal6/sites/default/modules/userpoints/userpoints_rules.rules.inc	Tue Apr 26 08:32:26 2011
+++ C:/Net/drupal6/sites/default/modules/userpoints/userpoints_rules.rules.inc.new	Thu May 12 12:49:45 2011
@@ -55,13 +55,34 @@
  * Rules action - grant points to a user.
  */
 function userpoints_action_grant_points($account, $settings) {
-  userpoints_userpointsapi(array('uid' => $account->uid, 'points' => $settings['points'], 'tid' => $settings['tid'], 'entity_type' => $settings['entity_type'], 'entity_id' => $settings['entity_id'], 'description' => $settings['description'], 'operation' => $settings['operation']));
+  $data = array(
+    'uid' => $account->uid,
+    'points' => $settings['points'],
+    'tid' => $settings['tid'],
+    'entity_type' => $settings['entity_type'],
+    'entity_id' => $settings['entity_id'],
+    'description' => $settings['description'],
+    'operation' => $settings['operation'],
+  );
+  
+  if (!empty($settings['expirydate'])) {
+    $rules_date = new rules_data_type_date();
+    $value = $rules_date->check_value(array('label' => t('Points expiration date')), $settings['expirydate']);
+    $data['expirydate'] = strtotime($value);
+  }
+  $res = userpoints_userpointsapi($data);
+  if ($res['status']) {
+    rules_log(t('Userpoints: added @num points to user %user', array('@num' => $settings['points'], '%user' => $account->name)));
+  }
+  else {
+    rules_log(t('Userpoints: error adding points to %user! Error: %message', array('%message' => $res['reason'], '%user' => $account->name)));
+  }
 }
 
 /**
  * Rules action form configuration - allow number of points to be set.
  */
-function userpoints_action_grant_points_form($settings = array(), &$form) {
+function userpoints_action_grant_points_form($settings = array(), &$form, &$form_state) {
   $form['settings']['points'] = array(
     '#type' => 'textfield',
     '#title' => t('Number of points'),
@@ -85,6 +106,15 @@
     '#default_value' => isset($settings['entity_type']) ? $settings['entity_type'] : '',
     '#description' => t("Entity type. ex. 'node' or 'user'")
   );
+  $rules_date = new rules_data_type_date();
+  $info = array(
+    'label' => t('Points expiration date'),
+    'description' => t('Insert an expiration date for the awarned points'),
+    'required' => FALSE,
+  );
+  $value = isset($settings['expirydate']) ? $settings['expirydate'] : '';
+  $form['settings']['expirydate'] = $rules_date->get_default_input_form($info, $value, $form_state);
+  
   $form['settings']['entity_id'] = array(
     '#type' => 'textfield',
     '#title' => t('Entity Id'),
@@ -92,7 +122,19 @@
     '#description' => t('ID of an entity in the Database. ex. $node->id or $user->uid')
   );
   $form['settings']['tid'] = taxonomy_form(userpoints_get_vid(), isset($settings['tid']) ? $settings['tid'] : '', 'The category ID');
+}
 
+/**
+ * Rules action form configuration validation
+ */
+function userpoints_action_grant_points_validate(&$form, &$form_state) {
+  $rules_date = new rules_data_type_date();
+  $values = $form_state['values']['settings'];
+  $check = $rules_date->check_value(array('label' => t('Points expiration date')), $values['expirydate']);
+  
+  if (is_null($check)) {
+    form_set_error('settings][expirydate', t('Wrong Expiry date format'));
+  }
 }
 
 
