Itsnotme!

You might also like:

The 'Likes' on Facebook

Most popular article for last n days under Joomla 1.6
Interactions - troubleshoot

logo_joomla16_cuadradoHow 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:


<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>


Add the following new Field Block:

<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>


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.

Popular story last n days joomla 1.6

Step#2: We will ask Joomla to register our nDays variable

Open [joomla_ROOT]/modules/mod_articles_popular/helper.php


After:

// Ordering 
$model->setState('list.ordering', 'a.hits');
$model->setState('list.direction', 'DESC');


Add the following code-block:

//custom date condition
$nDays = $params->get('nDays');
if($nDays>0) {
    $model->setState('filter.nDays', $nDays);
}


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:

// 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)');
}


Add the following code block:

// 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') . " )");
}


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!

 

Comments  

 
#3 CsRo 2011-12-19 23:52
excellent works perfect thanks
Quote
 
 
#2 David Aubry 2011-12-12 05:41
Thank you 1000 times !
I was searching for a replacement of "Popular of Late for Joomla 1.5" that can work on Joomla 1.7. Nothing except your code that took 5 minutes to implement.
What if there is an upgrade of articles or most read module of Joomla? Will I need to reenter the lines of codes?
Quote
 
 
#1 Sjaaakie 2011-07-23 21:37
Thanks for the script I use it on my site and it works fine
Quote
 

Add comment


Security code
Refresh