You are here

function uc_roles_renew in Ubercart 7.3

Same name and namespace in other branches
  1. 6.2 uc_roles/uc_roles.module \uc_roles_renew()

Renews a given role on a user.

This function updates expiration time on a role already granted to a user. First the function checks the new expiration. If it never expires, the function deletes the past expiration record and returns, leaving management up to Drupal. Otherwise, the record is updated with the new expiration time, and the user is notified of the change.

Parameters

$account: A Drupal user object.

int $rid: A Drupal role ID.

int $timestamp: When this role will expire.

bool $silent: When set to TRUE will suppress any Drupal messages from this function.

2 calls to uc_roles_renew()
uc_roles_action_order_renew in uc_roles/uc_roles.rules.inc
Renews an order's product roles.
uc_roles_user_presave in uc_roles/uc_roles.module
Implements hook_user_presave().

File

uc_roles/uc_roles.module, line 1159
Grants roles upon accepted payment of products.

Code

function uc_roles_renew($account, $rid, $timestamp, $silent = FALSE) {
  global $user;

  // If it doesn't expire, we'll remove our data associated with it.
  // After that, Drupal will take care of it.
  if (is_null($timestamp)) {
    uc_roles_delete($account, $rid);
    return;
  }

  // Update the expiration date and reset the notified flag.
  db_update('uc_roles_expirations')
    ->fields(array(
    'expiration' => $timestamp,
    'notified' => NULL,
  ))
    ->condition('uid', $account->uid)
    ->condition('rid', $rid)
    ->execute();
  if (!$silent) {
    $role_name = db_query("SELECT name FROM {role} WHERE rid = :rid", array(
      ':rid' => $rid,
    ))
      ->fetchField();
    if ($user->uid == $account->uid) {
      $message = t('Your %role role has been renewed. It will expire on %date.', array(
        '%role' => $role_name,
        '%date' => format_date($timestamp, 'short'),
      ));
    }
    else {
      $message = t("!user's %role role has been renewed. It will expire on %date.", array(
        '!user' => theme('username', array(
          'account' => $account,
          'name' => check_plain(format_username($account)),
          'link_path' => 'user/' . $account->uid,
        )),
        '%role' => $role_name,
        '%date' => format_date($timestamp, 'short'),
      ));
    }
    drupal_set_message($message);
  }
}