You are here

function radioactivity_cron in Radioactivity 7

Same name and namespace in other branches
  1. 8.3 radioactivity.module \radioactivity_cron()
  2. 8.2 radioactivity.module \radioactivity_cron()
  3. 5 radioactivity.module \radioactivity_cron()
  4. 6 radioactivity.module \radioactivity_cron()
  5. 7.2 radioactivity.module \radioactivity_cron()
  6. 4.0.x radioactivity.module \radioactivity_cron()

Implementation of hook_cron

File

./radioactivity.module, line 170

Code

function radioactivity_cron() {
  $fields = db_select("field_config", "fc")
    ->fields("fc", array(
    "id",
  ))
    ->condition("fc.type", RADIOACTIVITY_FIELD_TYPE)
    ->condition("fc.deleted", "0")
    ->execute();
  $time_now = time();

  // @TODO: add deferred processing
  while ($row = $fields
    ->fetchObject()) {
    $field_info = field_info_field_by_id($row->id);
    $storage = radiactivity_get_incident_storage($row->id);

    // Process incidents
    $storage
      ->process_incidents();

    // Update field database
    $half_life = $field_info['settings']['half_life'];
    $cut_off = $field_info['settings']['cut_off'];
    $field_name = $field_info['field_name'];
    $table_name = 'field_data_' . $field_name;
    $energy = $field_name . '_' . RADIOACTIVITY_FIELD_ENERGY;
    $timestamp = $field_name . '_' . RADIOACTIVITY_FIELD_TIMESTAMP;

    // grab update value from deferred values table
    // and update it to the fields table if it is used
    $updated = db_update($table_name)
      ->expression($energy, $energy . ' * pow(2, (' . $timestamp . ' * 1.0 - ' . $time_now . ') / ' . $half_life . ')')
      ->fields(array(
      $timestamp => $time_now,
    ))
      ->condition($timestamp, $time_now, '<')
      ->condition("deleted", "0")
      ->execute();

    /* Brute deletion of the field might not be ok but the field api can handle this */
    $cuts = db_delete($table_name)
      ->condition($energy, $cut_off, '<')
      ->condition("deleted", "0")
      ->execute();
    drupal_set_message(t("Updated @num fields and culled @culled fields of @table", array(
      "@num" => $updated,
      "@culled" => $cuts,
      "@table" => $table_name,
    )));
  }
  variable_set('radioactivity_last_cron_timestamp', $timestamp);
}