You are here

function _invite_escalate_role in Invite 5.2

Escalates an invited user's role, based on the role(s) of the inviter.

Parameters

$account: The user object of the invitee.

1 call to _invite_escalate_role()
invite_accept in ./invite.module
Set an invitation's status to accepted.

File

./invite.module, line 443
Allows your users to send and track invitations to join your site.

Code

function _invite_escalate_role($account) {

  // Default target role.
  $roles = array(
    'default',
  );

  // Add roles of inviter.
  $inviter_uid = db_result(db_query("SELECT uid FROM {invite} WHERE invitee = %d", $account->uid));
  if ($inviter_uid && ($inviter = user_load(array(
    'uid' => $inviter_uid,
  )))) {
    $roles = array_merge($roles, array_intersect($inviter->roles, user_roles(0, 'send invitations')));
  }

  // Map to configured target roles.
  $targets = array();
  foreach ($roles as $role) {
    $role_no_space = str_replace(' ', '_', $role);
    $target = variable_get('invite_target_role_' . $role_no_space, DRUPAL_AUTHENTICATED_RID);
    if ($target != DRUPAL_AUTHENTICATED_RID) {
      $targets[$target] = $target;
    }
  }

  // Notify other modules of changed user.
  $edit = array(
    'roles' => $targets,
  );
  user_module_invoke('update', $edit, $account);

  // Save new user role(s).
  foreach ($targets as $target) {
    db_query("DELETE FROM {users_roles} WHERE uid = %d AND rid = %d", $account->uid, $target);
    db_query("INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)", $account->uid, $target);
  }

  // Notify other modules of role escalation.
  $args = array(
    'invitee' => $account,
    'inviter' => $inviter,
    'roles' => $targets,
  );
  module_invoke_all('invite', 'escalate', $args);
}