You are here

function mostpopular_refresh in Drupal Most Popular 6

Same name and namespace in other branches
  1. 7 mostpopular.module \mostpopular_refresh()

Refreshes data from each service by invoking hook_mostpopular_service('refresh').

1 call to mostpopular_refresh()
mostpopular_cron in ./mostpopular.api.php
Implements hook_cron().
1 string reference to 'mostpopular_refresh'
mostpopular_menu in ./mostpopular.module
Implements hook_menu().

File

./mostpopular.api.php, line 334
Provides functions for other modules to use to interact with the Most Popular data.

Code

function mostpopular_refresh() {
  $services = MostPopularService::fetchEnabled();
  $intervals = MostPopularInterval::fetchAll();
  $max = variable_get('mostpopular_max', 5);
  $t = '';
  foreach ($services as $service) {
    $count = 0;
    $t .= '<div>';
    $t .= t("Refreshing %title", array(
      '%title' => $service->title,
    ));
    $status = array();
    foreach ($intervals as $interval) {

      // Get the last time this service was run
      $run = MostPopularLastRun::fetch($service->sid, $interval->iid);

      // Apply the throttle if one is defined
      if (!$run
        ->canRun()) {
        $status[] = t('%interval: No need to refresh yet', array(
          '%interval' => $interval->title,
        ));
        continue;
      }

      // Invoke the module
      $out = module_invoke($service->module, 'mostpopular_service', 'refresh', $service->delta, array(
        'sid' => $service->sid,
        'iid' => $interval->iid,
        'ts' => $interval
          ->timestamp(),
        'last_run' => $last_run->last_run,
        'throttle' => $last_run->throttle,
        'max' => $max,
      ));

      // If the module returned any results, save them to the database.
      if ($out !== FALSE) {
        if (count($out) > 0) {

          // Remove the previous results, if there are any
          MostPopularItem::reset($service->sid, $interval->iid);

          // Write the new results to the cache table
          foreach ($out as $value) {
            $value['sid'] = $service->sid;
            $value['iid'] = $interval->iid;
            $item = new MostPopularItem($value, TRUE);
            $item
              ->save();
          }

          // Since there were items returned, the service is ok.
          $service
            ->updateStatus(MostPopularService::STATUS_OK);
        }

        // Record the last time at which this service was run on this interval
        $run->last_run = time();
        $run
          ->save();
        $status[] = t("%interval: Found %count items", array(
          '%count' => count($out),
          '%interval' => $interval->title,
        ));
      }
      else {
        $status[] = t("%interval: No results", array(
          '%interval' => $interval->title,
        ));
      }
    }
    $t .= theme('item_list', $status);
    $t .= '</div><br/>';
  }
  if (empty($t)) {
    $t .= t("You must first enable services.  Go to !link", array(
      '!link' => l(t('the services tab'), 'admin/settings/mostpopular/services'),
    ));
  }
  return $t;
}