You are here

function download_count_statistics_cron in Download Count 6.2

Implementation of hook_cron().

File

./download_count_statistics.module, line 73
Provides statistics for download_count module data.

Code

function download_count_statistics_cron() {
  $today = format_date(time(), 'custom', 'Y-m-d');
  $last_processed_day = variable_get('download_count_statistics_last_processed_day', '0');
  if ($today > $last_processed_day) {
    $files = db_query("SELECT fid, DATE(FROM_UNIXTIME(timestamp)) as date, COUNT(dcid) as count FROM {download_count} WHERE DATE(FROM_UNIXTIME(timestamp)) >= '%s' AND DATE(FROM_UNIXTIME(timestamp)) < '%s' GROUP BY DATE(FROM_UNIXTIME(timestamp)), fid ORDER BY DATE(FROM_UNIXTIME(timestamp)) ASC", $last_processed_day, $today);
    while ($row = db_fetch_object($files)) {
      $result = db_query("INSERT INTO {download_count_statistics} VALUES (%d, UNIX_TIMESTAMP('%s'), %d)", $row->fid, $row->date, $row->count);
      if (!$result) {
        watchdog('download_count', 'Download count for fid %fid was unable to be included in the download count statisitcs for %today.', array(
          '%fid' => $row->fid,
          '%today' => $today,
        ), WATCHDOG_ERROR);
      }
    }
    watchdog('download_count', 'Download count statistics processed for %day.', array(
      '%day' => $today,
    ), WATCHDOG_NOTICE);
    variable_set('download_count_statistics_last_processed_day', $today);
  }
}