You are here

function user_stats_get_stats in User Stats 6

Same name and namespace in other branches
  1. 5 user_stats.module \user_stats_get_stats()
  2. 7 user_stats.module \user_stats_get_stats()

Returns user stats.

Parameters

$type: The statistic to return. Possible values are:

  • "ip_address"
  • "join_date"
  • "login_count"
  • "login_days"
  • "post_count"
  • "post_days"
  • "reg_days"
  • "online"
  • "profile"

$uid: The user id who's stats should be retrieved.

Return value

The statistic requested. Every statistic except join_date, online and IP address is a numeric. Join date is a string, whilst online is a boolean and IP Address a string. Note: if $type = "post_days" and the user hasn't posted any content (of the counted types) then 'n/a' is returned.

5 calls to user_stats_get_stats()
user_stats_events_argument_day_older in ./user_stats.rules.inc
Handler to load number of days user has been registered on event.
user_stats_login_count_update in ./user_stats.module
Manage the login count of a given user.
user_stats_post_count_update in ./user_stats.module
Manage the post count of a given user.
user_stats_preprocess_author_pane in ./user_stats.author-pane.inc
Implementation of hook_preprocess_author_pane().
user_stats_token_values in ./user_stats.module
Implementation of hook_token_values().

File

./user_stats.module, line 84
User Stats provides commonly requested user statistics for themers. These are:

Code

function user_stats_get_stats($type, $uid) {

  // Sometimes $uid can be NULL (comment previews for example).
  if (!is_numeric($uid)) {
    return;
  }

  // IP address is really a bit of feature creep.
  // At some point in the future, this could be split off into its own module.
  if ($type == 'ip_address') {
    if (!user_access('View IP addresses')) {
      return FALSE;
    }

    // Check cache.
    if (user_stats_cache_get($type, $uid) === FALSE) {
      $query = db_query("SELECT ip_address\n        FROM {user_stats_ips} WHERE uid = %d\n        ORDER BY first_seen_timestamp LIMIT 1", $uid);
      user_stats_cache_set($type, $uid, db_result($query));
    }
    return user_stats_cache_get($type, $uid);
  }

  // Everything else is under the 'View statistics' permission.
  if (!user_access('View statistics')) {
    return FALSE;
  }

  // Check cache first.
  if (user_stats_cache_get($type, $uid) !== FALSE) {
    return user_stats_cache_get($type, $uid);
  }
  switch ($type) {
    case 'join_date':
      $query = db_query("SELECT created FROM {users} WHERE uid = %d", $uid);
      $data = db_result($query);
      break;
    case 'login_count':
      if (!variable_get('user_stats_count_logins', TRUE)) {
        $data = 'n/a';
      }
      else {
        if (user_stats_isset($type, $uid)) {
          $query = db_query("SELECT value FROM {user_stats_values}\n          WHERE name = 'login_count' AND uid = %d", $uid);
          $data = db_result($query);
        }
        else {
          return 0;
        }
      }
      break;
    case 'login_days':
      $user_access = db_result(db_query("SELECT access FROM {users} WHERE uid = %d", $uid));
      $data = floor((time() - $user_access) / 86400);
      break;
    case 'post_count':
      if (!variable_get('user_stats_count_posts', TRUE) && !variable_get('user_stats_count_comments', TRUE)) {
        $data = 'n/a';
      }
      else {
        if (!user_stats_isset('post_count', $uid)) {
          user_stats_post_count_update('reset', $uid);
        }
      }
      $query = db_query("SELECT value FROM {user_stats_values}\n        WHERE name = 'post_count' AND uid = %d", $uid);
      $data = db_result($query);
      break;
    case 'post_days':
      $last_post = _user_stats_last_post($uid);
      if ($last_post !== FALSE) {
        $data = floor((time() - $last_post) / 86400);
      }
      else {
        $data = 'n/a';
      }
      break;
    case 'reg_days':
      $query = db_query("SELECT value FROM {user_stats_values}\n        WHERE name = 'last_known_age' AND uid = %d", $uid);
      $data = db_result($query);
      if (!$data) {
        $data = 0;
      }
      break;
    case 'reg_days_onthefly':
      $user_created = db_result(db_query("SELECT created FROM {users} WHERE uid = %d", $uid));
      $data = floor((time() - $user_created) / 86400);
      break;
    case 'online':
      $user_access = db_result(db_query("SELECT timestamp FROM {sessions} WHERE uid = %d", $uid));
      $data = time() - $user_access < variable_get('user_block_seconds_online', 900) ? TRUE : FALSE;
      break;
    default:

      // Check for custom statistics
      $custom_stats = array();
      $module_list = module_implements('default_user_stats');
      foreach ($module_list as $module) {
        $custom_stats = array_merge($custom_stats, module_invoke($module, 'default_user_stats'));
      }
      if (array_key_exists($type, $custom_stats)) {
        module_load_include('module', 'qna');
        $data = call_user_func($custom_stats[$type], $uid);
        break;
      }
      else {

        // Raise an error if the statistic doesn't exist.
        $err_message = 'Statistic "' . check_plain($type) . '" does not exist.';
        trigger_error($err_message, E_USER_WARNING);
        return;
      }
  }
  user_stats_cache_set($type, $uid, $data);
  return user_stats_cache_get($type, $uid);
}