You are here

function user_stats_get_stats in User Stats 7

Same name and namespace in other branches
  1. 5 user_stats.module \user_stats_get_stats()
  2. 6 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
Implements hook_preprocess_author_pane().
user_stats_token_values in ./user_stats.module
Implements hook_token_values().

File

./user_stats.module, line 97
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 = :uid\n        ORDER BY first_seen_timestamp LIMIT 1", array(
        ':uid' => $uid,
      ));
      user_stats_cache_set($type, $uid, $query
        ->fetchField());
    }
    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':
      $data = db_query("SELECT created FROM {users} WHERE uid = :uid", array(
        ':uid' => $uid,
      ))
        ->fetchField();
      break;
    case 'login_count':
      if (!variable_get('user_stats_count_logins', TRUE)) {
        $data = 'n/a';
      }
      else {
        if (user_stats_isset($type, $uid)) {
          $data = db_query("SELECT value FROM {user_stats_values} WHERE name = :name AND uid = :uid", array(
            ':name' => 'login_count',
            ':uid' => $uid,
          ))
            ->fetchField();
        }
        else {
          return 0;
        }
      }
      break;
    case 'login_days':
      $user_access = db_query("SELECT access FROM {users} WHERE uid = :uid", array(
        ':uid' => $uid,
      ))
        ->fetchField();
      $data = floor((REQUEST_TIME - $user_access) / 86400);
      break;
    case 'login_date':
      $data = db_query("SELECT access FROM {users} WHERE uid = :uid", array(
        ':uid' => $uid,
      ))
        ->fetchField();
      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 = :name AND uid = :uid", array(
        ':name' => 'post_count',
        ':uid' => $uid,
      ));
      $posts = $query
        ->fetchField();

      //@TODO Figure out why adding comments here wasn't in the D6 version
      if (variable_get('user_stats_count_comments', TRUE)) {
      }
      $data = $posts;
      break;
    case 'post_days':
      $last_post = _user_stats_last_post($uid);
      if ($last_post !== FALSE) {
        $data = floor((REQUEST_TIME - $last_post) / 86400);
      }
      else {
        $data = 'n/a';
      }
      break;
    case 'reg_days':
      $user_created = db_query("SELECT created FROM {users} WHERE uid = :uid", array(
        ':uid' => $uid,
      ))
        ->fetchField();
      $data = floor((REQUEST_TIME - $user_created) / 86400);
      break;
    case 'online':
      $user_access = db_query("SELECT timestamp FROM {sessions} WHERE uid = :uid", array(
        ':uid' => $uid,
      ))
        ->fetchField();
      $data = REQUEST_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);
}