GVS is now part of Acquia. Acquia logo

What content is HOT on my site? Drupal's Radioactivity module to the rescue

Greg's picture

Earlier this year we supported the IxDA in launching a new version of their IxDA.org site. One of the many interesting new features of this site is the ability to sort content by "hotness". The goal of this tool is to create a list of interesting content on the site. Their analytics show them that most people who are involved in the site visit it at least twice a month. So, they wanted a system to highlight content over the last two to three weeks. Enter the radioactivity module.

Radioactivity Module for Drupal

The Radioactivity module works on the concept of adding energy to a piece of content which then "decays" (or diminishes) with a particular half-life. The exact behavior is up to the site administrator, but on IxDA.org we originally set it up with values roughly similar to:

  • Posting content adds a lot of energy so that the hotness favors recent items.
  • Commenting on a post adds some energy
  • Voting up adds a bit of energy, voting down subtracts some energy
  • Favoriting a post adds some energy as well

We've got a few other elements that affect energy to help offset any potential gaming.

We set the half-life for decay to 15 days. So, if a piece of content gets posted and 3 comments and 2 vote ups and 1 favorite with 100 views on the first day it will have about 500 units of energy. If it gets no new energy, it would decay down to 250 units of energy after 15 days, and then down to 125 after 30 days and so on. Eventually the energy and decay are really small and for efficiency the module simply deletes all records with less than 2 units of energy.

Extending the Radioactivity Module

We needed some features which simply were not available in the Radioactivity module. The first two were to create events for adding energy when a node is created and then some better fivestar integration.

But I want to talk the most about the custom flag integration. The nature of the flag module is that it is so flexible and specific on each site that general flag integration for the Radioactivity module is probably not reasonable to add. So, instead I created a site-specific module for IxDA to do flag integration and have provided it here as an attachment so that you can download it and see how it works. If you look at the code, it is amazingly simple to build integration with the Radioactivity module!

Creating a new Radioactivity integration module is amazingly simple. You really only need two hooks. The first is hook_radioactivity_info which declares some information about your integration module.

Those of you comfortable with the FormAPI will appreciate the structure of the code - it contains an array of arrays of arrays!

/**
* Implement hook_radioactivity_info().
*/
function ixda_radioactivity_flag_radioactivity_info() {
  return array('sources' =>
           array('node' =>
             array('favorite' => array('title_placeholder' => 'Flag post as favorite.'),
               'unfavorite' => array('title_placeholder' => 'Unflag a post as favorite.')
         )));
}

The sources can be about different kinds of objects, this particular flag happens to be a flag on nodes called "Favorite" and people can either mark it as a Favorite or remove it as a favorite and I wanted the administrator to be able to assign points for either of those events.

The next hook is not specifically radioactivity but is from the Flag module. This hook is called whenever an item is flagged (or un-flagged). This flag then calls functions from the radioactivity module to add energy to content.

/**
* Implement hook_flag().
*/
function ixda_radioactivity_flag_flag($event, $flag, $content_id, $account) {
  require_once drupal_get_path('module', 'radioactivity') .'/radioactivity.inc';

  // If this is a favoriting.
  if ($flag->name == 'favorite') {
    $oid = $content_id;
    $oclass = 'node';

    // Then either favorite the object or unfavorite it.
    if ($event == 'flag') {
      radioactivity_add_energy($oid, $oclass, 'favorite');
    }
    else {
      radioactivity_add_energy($oid, $oclass, 'unfavorite');
    }
  }
}

That's IT! Two simple functions and you've done it. Everything else is configuration. Check out this video of setting up a new radioactivity profile and sorting content by that profile:

Using Drupal's Radioactivity Module to Highlight Your Best Content
from Mastering Drupal.

Best content on groups.drupal.org: Radioactivity again

I liked the module so much that I've now also deployed it on groups.drupal.org/hot. It's available as a single page and the top 5 items at any given time are shown in a sidebar block.

One of the features I would love to see is an rss feed of hot content. The problem there is that a piece of content could become hot twice. So, we would want to create a threshold value of energy for content that is included in the feed and then sort it descending by the day it broke that threshold.

Comments

Good to read

I'm happy to have come across this article. I was looking at the radioactivity module a while back, but it didn't quite fit my needs. I want to affect the 'hotness' of a piece of content based on the number of nodes that refer (node reference) to it. I'll have to look at extending the module and see what I can do.

Thanks!

I was just looking for something like this for a project I'm working on which uses fivestar and flags - this will be perfect for that 'popular' block!

The benefit of using

The benefit of using Radioactivity for a "most popular" block is that you can then disable the Statistics module; which always is a pain-in-the-butt for sites where you want to do more advanced caching of anonymous users (Varnish, Boost, aggressive, etc.).

GVS projects

CertifiedToRock.com was created to allow community members and employers to get a sense of someone's involvement with the Drupal project.

GVS is now part of Acquia.

Acquia logo

Contact Acquia if you are interested in a Drupal Support or help with any products GVS offered such as the Conference Organizing Distribution (COD).

We Wrote the Book On Drupal Security:

Cracking Drupal Book Cover