Growing Venture Solutions - GVS - content recommendation http://growingventuresolutions.com/taxonomy/term/119/0 en What content is HOT on my site? Drupal's Radioactivity module to the rescue http://growingventuresolutions.com/blog/what-content-hot-my-site-drupal-s-radioactivity-module-rescue <p>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 <a href="http://www.ixda.org/discussion/by-hotness">"hotness"</a>. 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 <a href="http://drupal.org/project/radioactivity">radioactivity module</a>.</p> <h3>Radioactivity Module for Drupal</h3> <p>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:</p> <ul> <li>Posting content adds a lot of energy so that the hotness favors recent items.</li> <li>Commenting on a post adds some energy</li> <li>Voting up adds a bit of energy, voting down subtracts some energy</li> <li>Favoriting a post adds some energy as well</li> </ul> <p>We've got a few other elements that affect energy to help offset any potential gaming.</p> <p>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.</p> <!--break--><!--break--> <h3>Extending the Radioactivity Module</h3> <p>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 <a href="http://drupal.org/node/688556">node is created</a> and then some better <a href="http://drupal.org/node/451024">fivestar integration</a>.</p> <p>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!</p> <p>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.</p> <p>Those of you comfortable with the FormAPI will appreciate the structure of the code - it contains an array of arrays of arrays!</p> <p><div class="codeblock"><code>/**<br /> * Implement hook_radioactivity_info().<br /> */<br />function ixda_radioactivity_flag_radioactivity_info() {<br />&nbsp; return array(&#039;sources&#039; =&gt; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; array(&#039;node&#039; =&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; array(&#039;favorite&#039; =&gt; array(&#039;title_placeholder&#039; =&gt; &#039;Flag post as favorite.&#039;),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039;unfavorite&#039; =&gt; array(&#039;title_placeholder&#039; =&gt; &#039;Unflag a post as favorite.&#039;)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )));<br />}</code></div></p> <p>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.</p> <p>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.</p> <p><div class="codeblock"><code>/**<br /> * Implement hook_flag().<br /> */<br />function ixda_radioactivity_flag_flag($event, $flag, $content_id, $account) {<br />&nbsp; require_once drupal_get_path(&#039;module&#039;, &#039;radioactivity&#039;) .&#039;/radioactivity.inc&#039;;<br /><br />&nbsp; // If this is a favoriting.<br />&nbsp; if ($flag-&gt;name == &#039;favorite&#039;) {<br />&nbsp;&nbsp;&nbsp; $oid = $content_id;<br />&nbsp;&nbsp;&nbsp; $oclass = &#039;node&#039;;<br /><br />&nbsp;&nbsp;&nbsp; // Then either favorite the object or unfavorite it.<br />&nbsp;&nbsp;&nbsp; if ($event == &#039;flag&#039;) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; radioactivity_add_energy($oid, $oclass, &#039;favorite&#039;);<br />&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; else {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; radioactivity_add_energy($oid, $oclass, &#039;unfavorite&#039;);<br />&nbsp;&nbsp;&nbsp; }<br />&nbsp; }<br />}</code></div></p> <p><strong>That's IT!</strong> 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:</p> <p><object width="400" height="300"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=9337149&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=9337149&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="300"></embed></object></p><p><a href="http://www.masteringdrupal.com/screencast/highlight-your-best-content-using-radioactivity-module">Using Drupal's Radioactivity Module to Highlight Your Best Content</a><br /> from <a href="http://www.masteringdrupal.com">Mastering Drupal</a>.</p> <h3>Best content on groups.drupal.org: Radioactivity again</h3> <p>I liked the module so much that I've now also deployed it on <a href="http://groups.drupal.org/hot">groups.drupal.org/hot</a>. It's available as a single page and the top 5 items at any given time are shown in a sidebar block.</p> <p>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.</p> http://growingventuresolutions.com/blog/what-content-hot-my-site-drupal-s-radioactivity-module-rescue#comments Planet Drupal content recommendation development IxDA Tue, 10 Aug 2010 06:00:00 +0000 Greg 911 at http://growingventuresolutions.com