You are here

function simplenews_statistics_cron in Simplenews Statistics 7

Same name and namespace in other branches
  1. 6.3 simplenews_statistics.module \simplenews_statistics_cron()
  2. 7.2 simplenews_statistics.module \simplenews_statistics_cron()

Implements hook_cron().

File

./simplenews_statistics.module, line 129
Main simplenews statistics file.

Code

function simplenews_statistics_cron() {

  // Delete open and click records after a specified period of time.
  $days = variable_get('simplenews_statistics_archive_days', 0);
  if (is_numeric($days) && $days > 0) {
    $timestamp = strtotime("-{$days} days");

    // Only archive one per cron run.
    $query = db_select('simplenews_statistics', 'ss')
      ->fields('ss')
      ->condition('ss.archived', 0)
      ->condition('ss.send_end_timestamp', 0, '>')
      ->condition('ss.send_end_timestamp', $timestamp, '<');
    $record = $query
      ->execute()
      ->fetchObject();
    if (empty($record)) {
      return;

      // Nothing to archive.
    }
    $nid = $record->nid;

    // Update simplenews_statistics record.
    $record->archived = 1;
    $record->unique_opens = simplenews_statistics_count_opens($nid, TRUE);
    $record->total_opens = simplenews_statistics_delete_opens($nid);
    $record->unique_clicks = simplenews_statistics_count_clicks($nid, TRUE);
    $record->total_clicks = simplenews_statistics_delete_clicks($nid);
    drupal_write_record('simplenews_statistics', $record, 'nid');
    watchdog('simplenews_statistics', 'Newsletter %nid archived. Deleted %total_opens open records and %total_clicks click records.', array(
      '%nid' => $nid,
      '%total_opens' => $record->total_opens,
      '%total_clicks' => $record->total_clicks,
    ));
  }
}