Most popular article for last n days under Joomla 1.6
How to fetch the most popular story of last few days, last week, last month (or simply last N days) for Joomla 1.6
In Joomla 1.6, mod_articles_popular fetches the most popular articles for all time and shows the result accordingly. You have control over the result almost like the one with articles_category module; however, what if you want to show the most popular articles of last N days (say the past week)?
With a few and small tweaks, you can add this feature to the core mod_articles_popular and make it accept last N days to show the result based on this settings.
Heres how we begin:
[But before we begin, ‘At your own risk’ -> make sure that you make a backup of files that you are editing. Just in case you need to revert it to original state]
Step#1: Lets configure the CMS parameters
Open [joomla_ROOT]/modules/mod_articles_popular/mod_articles_popular.xml
And after the following Field Block:
{codecitation class=”brush: xml;”}<field name=”show_front” type=”radio” default=”1″ label=”MOD_POPULAR_FIELD_FEATURED_LABEL” description=”MOD_POPULAR_FIELD_FEATURED_DESC”>
<option value=”1″>JSHOW</option>
<option value=”0″>JHIDE</option>
</field>{/codecitation}
Add the following new Field Block:
{codecitation class=”brush: xml;”}<field name=”nDays” type=”text” default=”0″ label=”N Days” description=”Show the most read n last N days (0 is for all-time)”>
<option value=”1″>JSHOW</option>
<option value=”0″>JHIDE</option>
</field>{/codecitation}
And remember that everything is within the <fieldset name=”basic”> block before its close tag. Save this document and check your CMS for Most Popular Module, a new parameter namely N Days will appear under Basic panel. The default value is set to be 0 day which indicates no day limitation.
Step#2: We will ask Joomla to register our nDays variable
Open [joomla_ROOT]/modules/mod_articles_popular/helper.php
After:
{codecitation class=”brush: php;”}// Ordering
$model->setState(‘list.ordering’, ‘a.hits’);
$model->setState(‘list.direction’, ‘DESC’);{/codecitation}
Add the following code-block:
{codecitation class=”brush: php;”}//custom date condition
$nDays = $params->get(‘nDays’);
if($nDays>0) {
$model->setState(‘filter.nDays’, $nDays);
}{/codecitation}
What we’ve done is asked Joomla to accept our nDays variable and made it available to Module for query stuff. And finally the last step…
Step#3: Integrate the ‘where’ condition
Open [joomla_ROOT]/components/com_content/models/articles.php
After:
{codecitation class=”brush: php;”}// Filter by language
if ($this->getState(‘filter.language’)) {
$query->where(‘a.language in (‘.$db->quote(JFactory::getLanguage()->getTag()).’,’.$db->quote(‘*’).’)’);
$query->where(‘(contact.language in (‘.$db->quote(JFactory::getLanguage()->getTag()).’,’.$db->quote(‘*’).’) OR contact.language IS NULL)’);
}{/codecitation}
Add the following code block:
{codecitation class=”brush: php;”}// Filter by Date Limit
if($this->getState(‘filter.nDays’) && $this->getState(‘filter.nDays’)>0) {
$query->where(“(TO_DAYS(NOW()) – TO_DAYS( a.created ) <= ” . $this->getState(‘filter.nDays’) . ” )”);
}{/codecitation}
What we just did is built a where condition and calculated the date so as to make limitation of latest N days of most popular articles.
Don’t forget to save all your files and time to test our tweaked module. Test your module by setting N Days = 7 for week and so on.
That’s all folks, I hope it helps you as it did for me.
Happy Coding!