You are here

function user_stats_cron in User Stats 5

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

Implementation of hook_cron().

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

File

./user_stats.module, line 431
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 fid FROM {profile_fields} WHERE name='%s'";
    $fid = db_result(db_query($sql, variable_get('user_stats_post_count_profile_field', 'user_post_count')));

    // Unfortunately this cannot be done with a JOIN because of the need to match on fid.
    $sql = "SELECT uid FROM {users} WHERE uid NOT IN\n      (SELECT uid FROM {profile_values} WHERE fid=%d)";

    // Update 25 users per cron run.
    $result = db_query_range($sql, $fid, 0, variable_get('user_stats_user_per_cron', array(
      '25',
    )));

    // If all users have been updated we'll avoid running this expensive
    // query again by setting the following flag!
    if (db_num_rows($result) == 0) {
      variable_set('user_stats_rebuild_stats', FALSE);
    }
    while ($update_user = db_fetch_object($result)) {
      $user = user_stats_user_load($update_user->uid);
      user_stats_post_count_update($user, 'reset');
    }
  }

  // Fire workflow_ng daily anniversary event.
  if (module_exists('workflow_ng')) {
    $sql = "SELECT uid FROM {users} u ";

    // ((last cron - created) - (time() - created)) > one day
    $sql .= "WHERE (FLOOR((%d-created)/(60*60*24))-FLOOR((%d-created)/(60*60*24)))>0\n      AND uid>0";
    $result = db_query($sql, time(), variable_get('cron_last', time()));
    $reset_user_count = 0;
    while ($update_user = db_fetch_object($result)) {
      $user = user_stats_user_load($update_user->uid);

      // We stop at 50 reset users (they'll just have to wait) so cron doesn't get timed out.
      if (!isset($user->user_post_count) && $reset_user_count < 50) {
        user_stats_post_count_update($user, 'reset');
        $reset_user_count++;
      }
      workflow_ng_invoke_event('user_stats_day_older', $user);
    }
  }
}