You are here

mostpopular_disqus.module in Drupal Most Popular 7

Uses the Disqus.com API to provide Most Popular data.

File

modules/mostpopular_disqus/mostpopular_disqus.module
View source
<?php

/*
 * Drupal Most Popular - Showcase the most popular content across your Drupal website and engage your audience.
 * Copyright © 2009-2012 New Signature
 * 
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * You can contact New Signature by electronic mail at labs@newsignature.com -or- by U.S. Postal Service at 1100 H St. NW, Suite 940, Washington, DC 20005.
 */

/**
 * @file
 * Uses the Disqus.com API to provide Most Popular data.
 */

/**
 * Implements hook_mostpopular_service_info().
 *
 * @see hook_mostpopular_service_info()
 */
function mostpopular_disqus_mostpopular_service_info() {
  $info = array();
  $info['commented'] = array(
    'name' => t('Disqus Most Commented'),
    'title' => t('Commented'),
    'entity_types' => TRUE,
  );
  return $info;
}

/**
 * Implements the 'refresh_delta' callback for the Disqus service.
 * 
 * @param object $service The service definition.
 * @param object $block The block definition. 
 * @param integer $span The number of seconds over which to search.
 * @param integer $last_run the timestamp of the last time this service was run.
 */
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;
}

/**
 * Implements the 'config_form' callback for the Disqus service.
 * 
 * @param object $service The service definition.
 * @param array $form_state The current state of the form.
 */
function mostpopular_disqus_config_form($service, &$form_state) {
  $form = array();
  $form['auth'] = array(
    '#type' => 'fieldset',
    '#title' => t('Disqus login credentials'),
  );
  $form['auth']['secret_key'] = array(
    '#type' => 'textfield',
    '#title' => t('Secret Key'),
    '#description' => t('Enter your API Secret Key.'),
    '#required' => TRUE,
    '#default_value' => !empty($service->data['auth']['secret_key']) ? $service->data['auth']['secret_key'] : '',
  );
  $form['auth']['forum'] = array(
    '#type' => 'textfield',
    '#title' => t('Forum'),
    '#description' => t('Enter the ID of the forum you wish to retrieve results from.'),
    '#required' => TRUE,
    '#default_value' => !empty($service->data['auth']['forum']) ? $service->data['auth']['forum'] : '',
  );
  return $form;
}

Functions

Namesort descending Description
mostpopular_disqus_config_form Implements the 'config_form' callback for the Disqus service.
mostpopular_disqus_mostpopular_service_info Implements hook_mostpopular_service_info().
mostpopular_disqus_refresh_commented Implements the 'refresh_delta' callback for the Disqus service.