You are here

function regcode_roles_regcode_used in Registration codes 6.2

Same name and namespace in other branches
  1. 6 regcode_roles/regcode_roles.module \regcode_roles_regcode_used()
  2. 7.2 regcode_roles/regcode_roles.module \regcode_roles_regcode_used()
  3. 7 regcode_roles/regcode_roles.module \regcode_roles_regcode_used()

Implements hook_regcode_used()

Add the new role to the user

File

regcode_roles/regcode_roles.module, line 261

Code

function regcode_roles_regcode_used(&$edit, &$account, $regcode) {

  // Do nothing if the regcode is not valid
  if (!is_object($regcode)) {
    return;
  }

  // Grab applicable roles for category used
  $rules = count($regcode->tags) ? regcode_roles_get_rules($regcode->tags) : array();
  if (count($rules)) {

    // If there's no roles fieldset, then simulate the fieldset with an empty array
    if (!is_array($edit['roles'])) {
      $edit['roles'] = array();
    }

    // Merge in any roles the user has already
    foreach ($account->roles as $rid => $role) {
      if (!$rid) {

        // During registration the $account->roles variable contains bogus data
        // which looks like array(0 => authenticated user). Because this is
        // keyed inproperly we need a specific check to avoid creating a dummy role
        // @see http://drupal.org/node/884962
        continue;
      }
      $edit['roles'][$rid] = $role;
    }

    // Add the new roles to the user
    foreach ($rules as $rule) {
      $edit['roles'][$rule['role_id']] = $rule['role'];
    }
  }

  // Apply role_expire rules
  if (module_exists('role_expire')) {
    foreach ($rules as $rule) {
      if ($rule['expire_date']) {
        $expiry_timestamp = $rule['expire_date'];
      }
      elseif ($rule['expire_duration']) {
        $expiry_timestamp = time() + $rule['expire_duration'] * 60 * 60 * 24;
      }
      role_expire_write_record($account->uid, $rule['role_id'], $expiry_timestamp);
    }
  }
}