function user_variable_set in User variable 7
Same name and namespace in other branches
- 6 user_variable.module \user_variable_set()
Set a variable if it does not exist, then create.
Parameters
$name: Variable name.
$value: Variable value.
$common: If TRUE then the variable does not depend on user ID.
$uid: User ID.
$expired: The lifetime of a variable (in seconds) if equal 0 then the variable is not deleted automatically.
$session: If TRUE then the variable is tied to the session when set, the variable is tied to the value (eg, session ID).
File
- ./user_variable.module, line 28 
- User variable - Creating and working with user variables.
Code
function user_variable_set($name, $value, $common = FALSE, $uid = 0, $expired = 0, $session = FALSE) {
  global $user_variables_array, $user;
  $serialized_value = serialize($value);
  $expired = $expired > 0 ? time() + $expired : 0;
  if ($session === TRUE) {
    $session = session_id();
  }
  elseif ($session === FALSE) {
    $session = '';
  }
  if ($common) {
    $uid = $user->uid;
  }
  else {
    if (!$uid) {
      $uid = $user->uid;
    }
  }
  $query = db_merge('user_variable')
    ->fields(array(
    'sid' => $session,
    'value' => $serialized_value,
    'expired' => $expired,
    'common' => $common ? 1 : 0,
    'name' => $name,
    'uid' => $uid,
  ));
  if ($common) {
    $query
      ->key(array(
      'name' => $name,
      'common' => 1,
      'sid' => $session,
    ))
      ->execute();
    if ($session == '') {
      $user_variables_array['common_session'][$name] = $value;
    }
    else {
      $user_variables_array['common'][$session][$name] = $value;
    }
  }
  else {
    $query
      ->key(array(
      'name' => $name,
      'common' => 0,
      'sid' => $session,
      'uid' => $uid,
    ))
      ->execute();
    if ($session == '') {
      $user_variables_array['uid_session'][$uid][$name] = $value;
    }
    else {
      $user_variables_array['uid'][$uid][$session][$name] = $value;
    }
  }
}