You are here

function user_stats_cron in User Stats 7

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

Implements hook_cron().

We slowly work through all users without a post count updating them.

File

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

Code

function user_stats_cron() {
  if (variable_get('user_stats_rebuild_stats', TRUE) && (variable_get('user_stats_count_posts', TRUE) || variable_get('user_stats_count_comments', TRUE))) {
    $sql = "SELECT uid FROM {users} WHERE uid NOT IN\n      (SELECT uid FROM {user_stats_values} WHERE name = 'post_count')";

    // Update 25 users per cron run.
    $result = db_query_range($sql, 0, variable_get('user_stats_user_per_cron', '25'));
    $users_updated = FALSE;
    foreach ($result as $update_user) {
      user_stats_post_count_update('reset', $update_user->uid);
      $users_updated = TRUE;
    }

    // If all users have been updated we'll avoid running this expensive
    // query again by setting the following flag.
    if (!$users_updated) {
      variable_set('user_stats_rebuild_stats', FALSE);
    }
  }

  // Fire rules day_older event.
  // This may seem grossly inefficient, but testing showed that, even firing
  // the event for ~100 users, takes less than a second to run when there are
  // no rules using this event. With a rule (that adds a role if the user has
  // been a member for over 1,000 days) cron took an extra ~40 seconds to run.
  // Basically, this has no potential to harm a site's performance, unless a
  // rule is configured.
  // Having said this: if there's a better way, please raise a bug report!
  if (module_exists('rules')) {
    $sql = "SELECT uid FROM {users} u ";

    // ((last cron - created) - (time() - created)) > one day
    $sql .= "WHERE (FLOOR((:request_time-created)/(60*60*24))-FLOOR((:cron_last-created)/(60*60*24)))>0 AND uid>0";
    $result = db_query($sql, array(
      ':request_time' => REQUEST_TIME,
      ':cron_last' => variable_get('cron_last', REQUEST_TIME),
    ));
    $reset_user_count = 0;
    foreach ($result as $update_user) {
      rules_invoke_event('user_stats_day_older', $update_user->uid);
    }
  }
  if (variable_get('user_stats_track_ips', TRUE)) {

    // Delete items from the IP log that are past expiry.
    db_delete('user_stats_ips')
      ->condition('first_seen_timestamp', REQUEST_TIME - variable_get('user_stats_flush_ips_timer', 31536000), '<')
      ->execute();
  }
}