You are here

function user_variable_set in User variable 6

Same name and namespace in other branches
  1. 7 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;
    }
  }
  $o_variable = new stdClass();
  $o_variable->sid = $session;
  $o_variable->value = $serialized_value;
  $o_variable->expired = $expired;
  $o_variable->common = $common ? 1 : 0;
  $o_variable->name = $name;
  $o_variable->uid = $uid;
  if ($common) {
    drupal_write_record('user_variable', $o_variable, array(
      'name',
      'common',
      'sid',
    ));
    if ($session == '') {
      $user_variables_array['common_session'][$name] = $value;
    }
    else {
      $user_variables_array['common'][$session][$name] = $value;
    }
  }
  else {
    drupal_write_record('user_variable', $o_variable, array(
      'name',
      'common',
      'uid',
      'sid',
    ));
    if ($session == '') {
      $user_variables_array['uid_session'][$uid][$name] = $value;
    }
    else {
      $user_variables_array['uid'][$uid][$session][$name] = $value;
    }
  }
  if (!db_affected_rows()) {
    drupal_write_record('user_variable', $o_variable);
  }
}