You are here

function mostpopular_disqus_refresh_commented in Drupal Most Popular 7

Implements the 'refresh_delta' callback for the Disqus service.

Parameters

object $service The service definition.:

object $block The block definition. :

integer $span The number of seconds over which to search.:

integer $last_run the timestamp of the last time this service was run.:

File

modules/mostpopular_disqus/mostpopular_disqus.module, line 40
Uses the Disqus.com API to provide Most Popular data.

Code

function mostpopular_disqus_refresh_commented($service, $block, $span, $last_run) {
  $interval = '';
  $forum = $service->data['auth']['forum'];

  // Disqus only allows the following intervals: 1h, 6h, 12h, 1d, 7d, 30d, 90d
  if ($span <= 60 * 60) {
    $interval = '1h';
  }
  elseif ($span <= 60 * 60 * 6) {
    $interval = '6h';
  }
  elseif ($span <= 60 * 60 * 12) {
    $interval = '12h';
  }
  elseif ($span <= 60 * 60 * 24) {
    $interval = '1d';
  }
  elseif ($span <= 60 * 60 * 24 * 7) {
    $interval = '7d';
  }
  elseif ($span <= time() - strtotime('-1 month')) {
    $interval = '30d';
  }
  else {
    $interval = '90d';
  }
  $limit = $block->count;
  $out = array();
  module_load_include('php', 'mostpopular_disqus', 'disqusapi/disqusapi');
  $disqus = new DisqusAPI($service->data['auth']['secret_key']);
  try {
    $data = $disqus->threads
      ->listPopular(array(
      'forum' => $forum,
      'interval' => $interval,
    ));
  } catch (Exception $e) {
    watchdog('mostpopular_disqus', nl2br(check_plain($e
      ->getMessage())), NULL, WATCHDOG_ERROR);
    return FALSE;
  }
  if (variable_get('mostpopular_debug', FALSE)) {
    watchdog('mostpopular_disqus', 'Response for %interval: <pre>!response</pre>', array(
      '%interval' => $interval,
      '!response' => print_r($data, TRUE),
    ), WATCHDOG_DEBUG);
  }
  $status = '';
  foreach ($data as $v) {
    $count = $v->postsInInterval;
    $url = $v->link;

    // Match the URL to a node
    $obj = mostpopular_match_result_nodes($url, $count, $service->data);
    if (isset($obj)) {
      if (empty($obj->title)) {
        $obj->title = $v->title;
      }
      if (empty($obj->url)) {
        $obj->url = $url;
      }
      $out[] = $obj;
      $status .= t('@url (@count)', array(
        '@url' => $url,
        '@count' => $count,
      ));
      if (isset($obj->entity_type)) {
        $status .= t(' is %entity: %id', array(
          '%entity' => $obj->entity_type,
          '%id' => $obj->entity_id,
        ));
      }
      $status .= '<br>';
    }
    if (count($out) >= $limit) {
      break;
    }
  }
  watchdog('mostpopular_disqus', 'Found %num items (of %count results)<br/>!status', array(
    '%num' => count($out),
    '%count' => count($data),
    '!status' => $status,
  ), WATCHDOG_DEBUG);
  return $out;
}