? role_expire-rewrite-1.patch
Index: role_expire.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/role_expire/role_expire.install,v
retrieving revision 1.2
diff -u -p -r1.2 role_expire.install
--- role_expire.install	26 Feb 2009 20:08:34 -0000	1.2
+++ role_expire.install	7 May 2009 08:54:59 -0000
@@ -3,7 +3,7 @@
 
 /**
  * @file
- * Role Expire install
+ * Role expire install.
  */
 
 
@@ -11,33 +11,34 @@
  * Implementation of hook_schema.
  */
 function role_expire_schema() {
-  $roles = user_roles(true);
-  unset($roles[DRUPAL_AUTHENTICATED_RID]);
-      
-  $schema = array();
-  foreach ($roles as $rid => $role) {
-    $schema['role_expire_'. $rid] = array(
-      'description' => t('Expiry information for '. $role),
-      'fields' => array(
-        'uid' => array(
-          'type' => 'int',
-          'length' => 11,
-          'unsigned' => TRUE,
-          'not null' => TRUE,
-          'description' => t('User ID connected with expiry date.')
-        ),
-        'expiry_timestamp' => array(
-          'description' => t('Role expiry timestamp.'),
-          'type' => 'varchar',
-          'not null' => TRUE,
-          'length' => 255)
-        ),
-      'indexes' => array(
-        'uid' => array('uid'),
-        )
-      );
-  }
-  
+  $schema['role_expire'] = array(
+    'description' => t('Expiration time for the user roles.'),
+    'fields' => array(
+      'uid' => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'description' => t('User ID connected with expiration time.')
+      ),  
+      'rid' => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0,
+        'description' => 'The role ID assigned to the user.',
+      ),  
+      'expiry_timestamp' => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0,      
+        'description' => t('Role expiration timestamp.'),
+      )
+    ),
+    'indexes' => array(
+      'uid' => array('uid'),
+    ),
+  );
   return $schema;
 }
 
Index: role_expire.js
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/role_expire/role_expire.js,v
retrieving revision 1.1
diff -u -p -r1.1 role_expire.js
--- role_expire.js	13 Feb 2009 19:24:06 -0000	1.1
+++ role_expire.js	7 May 2009 08:54:59 -0000
@@ -4,7 +4,7 @@
  * @file
  * Role Expire js
  *
- * Set of jQuery related rputines
+ * Set of jQuery related routines.
  */
 
 $(document).ready(function() {
Index: role_expire.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/role_expire/role_expire.module,v
retrieving revision 1.3
diff -u -p -r1.3 role_expire.module
--- role_expire.module	16 Apr 2009 21:56:14 -0000	1.3
+++ role_expire.module	7 May 2009 08:54:59 -0000
@@ -5,10 +5,112 @@
  * @file
  * Role Expire module
  *
- * Enables user roles to expire on given time and day.
+ * Enables user roles to expire on given time.
  */
 
 
+/*******************************************************************************
+ * API functions
+ ******************************************************************************/
+
+/**
+ * API function; Get expiration time of a user role.
+ * @param $uid
+ *   User ID.
+ * @param $rid
+ *   Role ID.
+ * @return
+ *  Array with the expiration time.
+ */
+function role_expire_get_user_role_expiry_time($uid, $rid) {
+  $result = db_fetch_array(db_query("SELECT expiry_timestamp FROM {role_expire} WHERE uid=%d AND rid = %d", $uid, $rid));
+  return (!empty($result)) ? $result['expiry_timestamp'] : '';
+}
+
+/**
+ * API function; Get expiration of all roles of a user.
+ * @param $uid
+ *   User ID.
+ * @param $rid
+ *   Role ID.
+ * @return
+ *  Array with the expiration time.
+ */
+function role_expire_get_all_user_records($uid) {
+  $return = array();
+  $result = db_query("SELECT rid, expiry_timestamp FROM {role_expire} WHERE uid=%d", $uid);
+  while ($row = db_fetch_array($result)) {
+    $return[$row['rid']] = $row['expiry_timestamp'];
+  }
+  return $return;
+}
+
+/**
+ * API function; Delete a record from the database.
+ * 
+ * @param $rid
+ *   Role ID.
+ * @param $uid
+ *   User ID.
+ * @return
+ */
+function role_expire_delete_record($uid, $rid) {
+  db_query("DELETE FROM {role_expire} WHERE uid=%d AND rid = %d", $uid, $rid);
+}
+/**
+ * API function; Delete all user expirations.
+ * 
+ * @param $rid
+ *   Role ID.
+ * @param $uid
+ *   User ID.
+ * @return
+ */
+function role_expire_delete_user_records($uid) {
+  db_query("DELETE FROM {role_expire} WHERE uid = %d", uid);
+}
+    
+
+/**
+ * API function; Insert or update a record in the database.
+ * 
+ * @param $rid
+ *   Role ID.
+ * @param $uid
+ *   User ID.
+ * @param $expiry_time
+ *   The expiration time string.
+ */
+function role_expire_write_record($uid, $rid, $expiry_timestamp) {
+  $result =  db_query("UPDATE {role_expire} SET expiry_timestamp = %d WHERE uid = %d AND rid = %d", $expiry_timestamp, $uid, $rid);
+  if (!db_affected_rows()) {
+    db_query("INSERT INTO {role_expire} (uid, rid, expiry_timestamp) VALUES (%d, %d, %d)", $uid, $rid, $expiry_timestamp);
+  }
+}
+
+/**
+ * API function; Get all records that should be expired.
+ * 
+ * @param $time
+ *   Optional. The time to check, if not set it will check current time.
+ */
+function role_expire_get_expired($time = '') {
+  $return = array();
+  if (!$time) {
+    $time = time();
+  }
+  $result = db_query("SELECT rid, uid, expiry_timestamp FROM {role_expire} WHERE expiry_timestamp <= %d", $time);
+  while ($row = db_fetch_array($result)) {
+    $return[] = $row;
+  }
+  return $return;
+}
+
+
+/*******************************************************************************
+ * Hook implementations
+ ******************************************************************************/
+
 /**
  * Implementation of hook_views_api().
  */
@@ -20,314 +122,171 @@ function role_expire_views_api() {
  * Implementation of hook_perm().
  */
 function role_expire_perm() {
-  return array("view all role expiry dates");
+  return array('administer role expire');
 }
 
 /**
- * Implementation of hook_form_alter().
+ * Implementation of hook_form_FORM-ID_alter().
  */
-function role_expire_form_alter(&$form, $form_state, $form_id) {
-  if (user_access("administer permissions") && user_access("administer users")) {
-    if ($form_id == "user_register") {
-      drupal_add_js(drupal_get_path('module', 'role_expire').'/role_expire.js', 'module');
-      
-      $form['roles']['#attributes'] = array('class' => 'role-expire-roles');
-      
-      $roles = user_roles(true);
-      unset($roles[DRUPAL_AUTHENTICATED_RID]);
-        
-      $i = 100;
-      foreach ($roles as $rid => $role) {
-        if ($account->role_expire[$rid]) $expiry_date = date("d-m-Y G:i:s", $account->role_expire[$rid]);
-        else $expiry_date = '';
-        $form['role_expire_' . $rid] = array(
-          '#title' => t("%role expires", array('%role' => $role)),
-          '#type' => 'textfield',
-          '#default_value' => $expiry_date,
-          '#weight' => $i,
-          '#attributes' => array('class' => 'role-expire-role-expiry'),
-          '#description' => t("Leave blank for indefinite time, enter date and time in format: <em>dd-mm-yyyy hh:mm:ss</em> or use relative time i.e. 1 day, 2 months, 1 year, 3 years.")
-        );
-        $i ++;
-      }
-    }    
-
-    if ($form_id == "user_profile_form") {
-      $form['account']['roles']['#attributes'] = array('class' => 'role-expire-roles');
-    }
-  }
+function role_expire_form_user_register_alter(&$form, $form_state) {
+  $form = array_merge_recursive($form, role_expire_add_expiration_input());
 }
 
 /**
+ * Implementation of hook_form_FORM-ID_alter().
+ */
+function role_expire_form_user_profile_form_alter(&$form, $form_state) {
+  $form['account']['roles']['#attributes'] = array('class' => 'role-expire-roles');
+} 
+
+/**
  * Implementation of hook_user().
  */
 function role_expire_user($op, &$edit, &$account, $category = NULL) {
-  if ($op == 'form') {
-    if (user_access("administer permissions") && user_access("administer users")) {
-      drupal_add_js(drupal_get_path('module', 'role_expire').'/role_expire.js', 'module');
-      
-      $roles = user_roles(true);
-      unset($roles[DRUPAL_AUTHENTICATED_RID]);
-      
-      $i = 100;
-      foreach ($roles as $rid => $role) {
-        if ($account->role_expire[$rid]) $expiry_date = date("d-m-Y G:i:s", $account->role_expire[$rid]);
-        else $expiry_date = '';
-        $form['account']['role_expire_' . $rid] = array(
-          '#title' => t("%role expires", array('%role' => $role)),
-          '#type' => 'textfield',
-          '#default_value' => $expiry_date,
-          '#weight' => $i,
-          '#attributes' => array('class' => 'role-expire-role-expiry'),
-          '#description' => t("Leave blank for indefinite time, enter date and time in format: <em>dd-mm-yyyy hh:mm:ss</em> or use relative time i.e. 1 day, 2 months, 1 year, 3 years.")
-        );
-        $i ++;
-      }
-
-      return $form;
-    }
-  } 
-  else if ($op == 'submit') {
-    if (isset($edit["roles"])) {
-      $roles = user_roles(true);
-      unset($roles[DRUPAL_AUTHENTICATED_RID]);  
-      
-      // Make $edit array look better
-      $edit["role_expire"] = array();
-      foreach ($edit["roles"] as $rid => $role) {
-        $edit["role_expire"][$rid] = $edit["role_expire_" . $rid];
-        unset($edit["role_expire_" . $rid]);
-      }
-    }
-  }
-  else if ($op == 'validate') {
-    if (isset($edit["roles"])) {
-      foreach ($edit["roles"] as $rid => $role) {
-        if ($edit["role_expire_" . $role]) {
-          $expiry_time = strtotime($edit["role_expire_" . $role]);
+  switch ($op) {    
+  case 'form':
+      return role_expire_add_expiration_input($account);
+    break;
+    
+  case 'validate':
+    if (isset($edit['roles'])) {
+      $time = time();
+      foreach ($edit['roles'] as $rid => $role) {
+        if ($edit['role_expire_'. $role]) {
+          $expiry_time = strtotime($edit['role_expire_'. $role]);
           if (!$expiry_time) {
-            form_set_error("role_expire_" . $role, t("Role expiry date is not in correct format."));
+            form_set_error('role_expire_'. $role, t("Role expiry time is not in correct format."));
           }
-          if ($expiry_time <= time()) {
-            form_set_error("role_expire_" . $role, t("Role expiry date must be in the future."));
+          if ($expiry_time <= $time) {
+            form_set_error('role_expire_'. $role, t("Role expiry time must be in the future."));
           }
         }
       }
     }
-  } 
-  else if ($op == 'insert') {
-    $roles = user_roles(true);
-    unset($roles[DRUPAL_AUTHENTICATED_RID]);  
-    
-    // Insert roles expiry information for user
-    foreach ($edit["roles"] as $rid => $role) {
-      if ($edit["role_expire_" . $rid]) {
-        // Check if table exists, if not, create it!
-        if (!db_table_exists("role_expire_" . $rid)) {
-          _role_expire_create_table($rid);
-        }
-      
-        $expiry_time = strtotime($edit["role_expire_" . $rid]);
-        db_query("INSERT INTO {role_expire_%d} (uid, expiry_timestamp) VALUES (%d, '%s')", $rid, $account->uid, $expiry_time);
-      }
-     
-      unset($edit["role_expire"][$rid]);
-    }
-  } 
-  else if ($op == 'update') {
-    
-    $roles = user_roles(true);
-    unset($roles[DRUPAL_AUTHENTICATED_RID]);  
-    
-    // Update expiry date for each role
-    foreach ($roles as $rid => $role) {
-
-      // The role is being added
-      if (isset($edit["roles"][$rid])) {
+    break;
         
-          // Expiry variable has been provided
-          if ($edit["role_expire"][$rid]) {
-            // Check if table exists, if not, create it!
-            $expiry_time = strtotime($edit["role_expire"][$rid]);
-            if (!db_table_exists("role_expire_" . $rid)) {
-              _role_expire_create_table($rid);
-              db_query("INSERT INTO {role_expire_%d} (uid, expiry_timestamp) VALUES (%d, '%s')", $rid, $account->uid, $expiry_time);
-            } 
-            else {
-              $et = db_result(db_query("SELECT expiry_timestamp FROM {role_expire_%d} WHERE uid=%d", $rid, $account->uid));
-              if ($et) {
-                db_query("UPDATE {role_expire_%d} SET expiry_timestamp='%s' WHERE uid=%d", $rid, $expiry_time, $account->uid);
-              } 
-              else {
-                db_query("INSERT INTO {role_expire_%d} (uid, expiry_timestamp) VALUES (%d, '%s')", $rid, $account->uid, $expiry_time);
-              }
-            }
-            
-          // Expiry date has been cleared (delete expiry timestamp from database)
-          } 
-          else if ($edit["role_expire"][$rid] === "") {
-            if (db_table_exists("role_expire_" . $rid)) {
-              db_query("DELETE FROM {role_expire_%d} WHERE uid=%d", $rid, $account->uid);
-            }
-          }   
-          unset($edit["role_expire"][$rid]);    
-      
-      // The role is removed or ommited (not present in user's $edit array)
-      } 
-      else if ($category == 'account') {
-        if (db_table_exists("role_expire_" . $rid)) {
-          db_query("DELETE FROM {role_expire_%d} WHERE uid=%d", $rid, $account->uid);
-        }
-      }       
+  case 'submit':
+    // We go over all existing roles, because use might have disabled a role.
+    $edit['role_expire'] = array();
+    foreach (_role_expire_get_role() as $rid => $role) {
+      // Normalize role expire array.
+      $edit["role_expire"][$rid] = !empty($edit['role_expire_' . $rid]) && !empty($edit['roles'][$rid]) ? $edit['role_expire_' . $rid] : '';
+      unset($edit['role_expire_'. $rid]);
     }
-  } 
-  else if ($op == 'delete') {
-    $roles = user_roles(true);
-    unset($roles[DRUPAL_AUTHENTICATED_RID]);  
+    break;
     
-    // Delete from all role expiry data for role related tables (if they exist)
-    foreach ($roles as $rid => $role) {
-      if (db_table_exists('role_expire_'. $rid)) {
-        db_query("DELETE FROM {role_expire_%d} WHERE uid=%d", $rid, $account->uid);
+  case 'update':
+  case 'insert':  
+    // Add roles expiry information for the user role.
+    $edit += array('role_expire' => array());
+    foreach ($edit['role_expire'] as $rid => $role) {   
+      if ($role) {
+        $expiry_timestamp = strtotime($role);
+        role_expire_write_record($account->uid, $rid, $expiry_timestamp);
       }
-    }
-  } 
-  else if ($op == 'load') {
-    $account->role_expire = array();
-    
-    $roles = user_roles(true);
-    unset($roles[DRUPAL_AUTHENTICATED_RID]);  
-    
-    foreach($roles as $rid => $role) {
-      if (db_table_exists('role_expire_'. $rid)) {
-        $expiry_timestamp = db_result(db_query("SELECT expiry_timestamp FROM {role_expire_%d} WHERE uid=%d", $rid, $account->uid));
-        $account->role_expire[$rid] = $expiry_timestamp;
+      else {
+        // User input is blank, so delete record.
+        role_expire_delete_record($account->uid, $rid);
       }
     }
+    // We can remove this now.
+    unset($edit['role_expire']);
+    break;
+     
+
+  case 'delete':    
+    // Delete user records.
+    role_expire_delete_user_records($uid);
+    break;
     
-  } 
-  else if ($op == 'view') {
-    if (user_access("view all role expiry dates") || $GLOBALS["user"]->uid == $account->uid) {
-        
-      if ($account->roles) {
-        foreach ($account->roles as $key => $val) {
-          if ($account->role_expire[$key]) $roles[$key] =  t("%role - expires on %timedate", array('%role' => $val, '%timedate' => format_date($account->role_expire[$key])));
-          else $roles[$key] = t("%role - granted indefinitely", array('%role' => $val));
-        }
-        
-        unset($roles[DRUPAL_AUTHENTICATED_RID]);
+  case 'load':
+   // We don't load the information to the user object. Other modules can use 
+   // our API to query the information. 
+   break;
     
-        if (count($roles) > 0) {
-          $account->content['summary']['roles'] = array(
+  case'view':
+    global $user;
+    if (user_access('administer role expire') || user_access('administer users') || $user->uid == $account->uid) {
+      $roles = array();
+      $expiry_roles = role_expire_get_all_user_records($account->uid);
+      foreach ($account->roles as $key => $val) {
+        if ($expiry_roles[$key]) {
+          $roles[$key] =  t("%role role - expires on %timedate", array('%role' => ucfirst($val), '%timedate' => format_date($expiry_roles[$key])));
+        }
+      }
+      if ($roles) {
+        $account->content['summary']['role_expire'] = array(
           '#type' => 'item',
-          '#title' => t('Roles'),
+          '#title' => t('Role expiration'),
           '#value' => theme('item_list', $roles),
           '#attributes' => array('class' => 'role-expiry-roles'),
-          '#access' => (user_access("view all role expiry dates") || $GLOBALS["user"]->uid == $account->uid),
-          );
-        }
+        );
       }
     }
+    break;
   }
+
 }
 
 /**
  * Implementation of hook_cron().
  */
-function role_expire_cron() {    
-  $roles = user_roles(true);
-  unset($roles[DRUPAL_AUTHENTICATED_RID]);  
-        
-  // Update roles expiry for user
-  foreach ($roles as $rid => $role) {
-    // Check if table exists, if not, create it!       
-    if (db_table_exists("role_expire_" . $rid)) {
-      $result = db_query("SELECT uid FROM {role_expire_%d} WHERE expiry_timestamp<=%d", $rid, time());
-      while ($row = db_fetch_object($result)) {
-        role_expire_remove_role($row->uid, $rid);
-      }
-    } 
-    else {
-      _role_expire_create_table($rid);
+function role_expire_cron() { 
+  if ($expires = role_expire_get_expired()) {
+    $roles = _role_expire_get_role();
+    foreach ($expires as $expire) {
+
+      // Remove the role from the user.
+      $account = user_load($expire['uid']);
+      unset($account->roles[$expire['rid']]);
+      user_save($account, array('roles' => $account->roles), NULL);
+      
+      // Remove the role expiration record.
+      role_expire_delete_record($expire['uid'], $expire['rid']);
+      watchdog('role expire', 'Remove role @role from user @account.', array('@role' => $roles[$expire['rid']], '@account' => $account->name));    
     }
   }
 }
 
-function role_expire_set_expiry_date($uid, $rid, $expiry_timestamp) {
-  // Check if user is in role
-  $account = user_load(array("uid" => $uid));
-  if ($account !== FALSE && isset($account->roles[$rid])) {
-  
-    // Check if table exists, if not, create it!
-    if (!db_table_exists("role_expire_". $rid)) {
-      _role_expire_create_table($rid);
-    } 
-    else {
-      db_query("DELETE FROM {role_expire_%d} WHERE uid=%d", $rid, $uid);
-    }
-      
-    db_query("INSERT INTO {role_expire_%d} (uid, expiry_timestamp) VALUES (%d, '%s')", $rid, $uid, $expiry_timestamp);
-  }
-}
 
-function role_expire_clear_expiry_date($uid, $rid) {
-  // Check if user is in role
-  $account = user_load(array("uid" => $uid));
-  if ($account !== FALSE && isset($account->roles[$rid])) {  
-    // Check if table exists, if not, create it!
-    if (db_table_exists("role_expire_". $rid)) {
-      db_query("DELETE FROM {role_expire_%d} WHERE uid=%d", $rid, $uid);
+/**
+ * Add form element that accepts the role expiration time.
+ * 
+ * @param $account
+ *   The user object.
+ * @return
+ *   Form element.
+ */
+function role_expire_add_expiration_input($account = NULL) {
+  $form = array();
+  if (user_access('administer users') || user_access('administer role expire')) {
+    drupal_add_js(drupal_get_path('module', 'role_expire') .'/role_expire.js', 'module');
+    $form['roles']['#attributes'] = array('class' => 'role-expire-roles');    
+
+    foreach (_role_expire_get_role() as $rid => $role) {
+      $expiry_timestamp = role_expire_get_user_role_expiry_time($account->uid, $rid);
+      $form['role_expire_'. $rid] = array(
+        '#title' => t("%role role expiration time", array('%role' => ucfirst($role))),
+        '#type' => 'textfield',
+        '#default_value' => !empty($expiry_timestamp) ? date("d-m-Y G:i:s", $expiry_timestamp) : '',
+        '#attributes' => array('class' => 'role-expire-role-expiry'),
+        '#description' => t("Leave blank for indefinite time, enter date and time in format: <em>dd-mm-yyyy hh:mm:ss</em> or use relative time i.e. 1 day, 2 months, 1 year, 3 years.")
+      );
     }
   }
+  return $form;  
 }
 
-function role_expire_add_role($uid, $rid, $expiry_timestamp = NULL) {
-  $role = db_result(db_query("SELECT name FROM {role} WHERE rid = %d", $rid));
-  $account = user_load(array('uid' => (int)$uid));
-  
-  // Skip adding the role to the user if they already have it.
-  if ($account !== FALSE && !isset($account->roles[$rid])) {
-    $roles = $account->roles + array($rid => $role);
-    user_save($account, array('roles' => $roles));
-  }
-  
-  if ($expiry_timestamp) {
-    role_expire_set_expiry_date($uid, $rid, $expiry_timestamp);
-  }
-}
-
-function role_expire_remove_role($uid, $rid) {
-  $account = user_load(array('uid' => (int)$uid));
-
-  // Skip removing the role from the user if they already don't have it.
-  if ($account !== FALSE && isset($account->roles[$rid])) {
-    $roles = $account->roles;
-    unset($roles[$rid]);
-    user_save($account, array('roles' => $roles));
-  }
-}
+/*******************************************************************************
+ * API functions
+ ******************************************************************************/
 
-function role_expire_get_expiry_date($uid, $rid) {
-  if (db_table_exists("role_expire_" . $rid)) {
-    $expiry_timestamp = db_result(db_query("SELECT expiry_timestamp FROM {role_expire_%d} WHERE uid=%d", $rid, $uid));
-    
-    if ($expiry_timestamp) {
-      return $expiry_timestamp;
-    } else {
-      return FALSE;
-    }
-  } 
-  else { 
-    return FALSE;
-  }
+/**
+ * Helper function; Get valid roles.
+ * @return unknown_type
+ */
+function _role_expire_get_role() {
+  $roles = user_roles(TRUE);
+  unset($roles[DRUPAL_AUTHENTICATED_RID]);
+  return $roles;
 }
-
-function _role_expire_create_table($rid) {
-  $schema = drupal_get_schema_unprocessed("role_expire");
-  _drupal_initialize_schema("role_expire", $schema);
-      
-  $ret = array();
-  db_create_table($ret, "role_expire_". $rid, $schema["role_expire_". $rid]);
-  
-  return $ret;
-}
\ No newline at end of file
Index: role_expire.rules.inc
===================================================================
RCS file: role_expire.rules.inc
diff -N role_expire.rules.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ role_expire.rules.inc	7 May 2009 08:54:59 -0000
@@ -0,0 +1,113 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Rules integration for the role expire module.
+ */
+
+/**
+ * Implementation of hook_rules_action_info().
+ */
+function role_expire_rules_action_info() {
+  $items['role_expire_rules_action_set_role_expire'] = array(
+    'label' => t('Add expire time to role'),
+    'help' => t('Add an expiration time to a role a user has. This action does not add the role, only the expiration time.'),
+    'arguments' => array(
+      'user' => array(
+        'type' => 'user',
+        'label' => t('The user which the expiration time will be added'),
+      ), 
+    ),
+    'eval input' => array('timestamp'),
+    'module' => 'Role expire',    
+  );
+  
+  $items['role_expire_rules_action_remove_role_expire'] = array(
+    'label' => t('Remove expire time from role'),
+    'help' => t('Remove an expiration time to a role a user has. This action does not remove the role, only the expiration time.'),
+    'arguments' => array(
+      'user' => array(
+        'type' => 'user',
+        'label' => t('The user which the expiration time will be removed'),
+      ), 
+    ),
+    'module' => 'Role expire',    
+  );
+    
+  return $items;
+}
+
+/**
+ * Action: Add expire time to role.
+ */
+function role_expire_rules_action_set_role_expire($account, $settings) {
+  role_expire_write_record($account->uid, $settings['rid'], strtotime($settings['timestamp']));
+}
+
+/**
+ * Action: Add expire time to role form configuration.
+ */
+function role_expire_rules_action_set_role_expire_form($settings = array(), &$form) {
+  $settings += array('rid' => '', 'timestamp' => '');
+
+   
+  // The role ID.
+  $form['settings']['rid'] = _role_expire_get_rids($settings);
+  
+  //The timestamp.
+  $form['settings']['timestamp'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Expiration time'),
+    '#required' => TRUE,
+    '#description' => t('Enter the time the role will expire. Enter date and time in format: <em>dd-mm-yyyy hh:mm:ss</em> or use relative time i.e. now, 1 day, 2 months, 1 year, 3 years.'),
+    '#default_value' => $settings['timestamp'],
+  );
+}
+
+/**
+ * Action: Add expire time to role validation handler.
+ */
+function role_expire_rules_action_set_role_expire_validate($form, $form_state) {
+  $timestamp = strtotime($form_state['values']['settings']['timestamp']);
+  if (!$timestamp) {
+    form_set_error('timestamp', t('Role expiration time is not in correct format.'));
+  }  
+}
+
+/**
+ * Action: Remove expire time from role
+ */
+function role_expire_rules_action_remove_role_expire($account, $settings) {
+  role_expire_delete_record($account->uid, $settings['rid']);
+}
+
+/**
+ * Action: Add expire time to role form configuration.
+ */
+function role_expire_rules_action_remove_role_expire_form($settings = array(), &$form) {
+  $settings += array('rid' => '');
+   
+  // The role ID.
+  $form['settings']['rid'] = _role_expire_get_rids($settings);
+}
+
+
+/**
+ * Helper function; Return a form element with the valid roles.
+ * @return unknown_type
+ */
+function _role_expire_get_rids($settings) {
+  // Get valid roles.
+  $roles = user_roles(TRUE);
+  unset($roles[DRUPAL_AUTHENTICATED_RID]);
+  // The role ID.
+  return array(
+    '#type' => 'select',
+    '#options' => $roles,
+    '#title' => t('Role'),
+    '#required' => TRUE,
+    '#description' => t('Select a role.'),
+    '#default_value' => $settings['rid'],
+  );    
+}
\ No newline at end of file
