You are here

function user_variable_get in User variable 7

Same name and namespace in other branches
  1. 6 user_variable.module \user_variable_get()

Get variable.

Parameters

$name: Variable name.

$common: If TRUE then the variable does not depend on user ID.

$uid: User ID.

$session: Session ID.

Return value

Variable value.

File

./user_variable.module, line 103
User variable - Creating and working with user variables.

Code

function user_variable_get($name, $common = FALSE, $uid = 0, $session = '') {
  global $user_variables_array, $user;
  if ($session === TRUE) {
    $session = session_id();
  }
  elseif ($session === FALSE) {
    $session = '';
  }
  if ($common) {
    if ($session == '') {
      if (isset($user_variables_array['common_session'][$name])) {
        return $user_variables_array['common_session'][$name];
      }
    }
    else {
      if (isset($user_variables_array['common'][$session][$name])) {
        return $user_variables_array['common'][$session][$name];
      }
    }

    //Get from DB
    $result = db_query_range("SELECT value FROM {user_variable}\n      WHERE common = 1 AND name = :name AND sid = :sid", 0, 1, array(
      ':name' => $name,
      ':sid' => $session,
    ))
      ->fetchObject();
    if ($session == '') {
      return $user_variables_array['common_session'][$name] = isset($result->value) && $result->value ? unserialize($result->value) : FALSE;
    }
    else {
      return $user_variables_array['common'][$session][$name] = isset($result->value) && $result->value ? unserialize($result->value) : FALSE;
    }
  }
  else {

    //$common = FALSE
    if (!$uid) {
      $uid = $user->uid;
    }
    if ($session == '') {
      if (isset($user_variables_array['uid_session'][$uid][$name])) {
        return $user_variables_array['uid_session'][$uid][$name];
      }
    }
    else {
      if (isset($user_variables_array['uid'][$uid][$session][$name])) {
        return $user_variables_array['uid'][$uid][$session][$name];
      }
    }

    //Get from DB
    $result = db_query_range("SELECT value FROM {user_variable}\n      WHERE name = :name AND common = 0 AND uid = :uid AND sid = :sid", 0, 1, array(
      ':name' => $name,
      ':uid' => $uid,
      ':sid' => $session,
    ))
      ->fetchObject();
    if ($session == '') {
      return $user_variables_array['uid_session'][$uid][$name] = isset($result->value) && $result->value ? unserialize($result->value) : FALSE;
    }
    else {
      return $user_variables_array['uid'][$uid][$session][$name] = isset($result->value) && $result->value ? unserialize($result->value) : FALSE;
    }
  }
}