Store
Community Documentation

v3 Knowledgebase

Add activity point for login

The task this time is to award a member an activity point for a daily login. Someone asked if this was possible and I think it may be. I took this to mean that they would get a point if they logged in today, but not another if they logged in again on the same day.

So the logic should be to get the user_id upon login and, if the last_login column for the user is a different day than today, then send a request to the user activity service to update the activity points. Since the points must go somewhere, I choose to put them under 'activity_comment', but if you wanted to get fancy you could create a new column in the user_activity table called 'activity_login' or something similar and send them to that column.

The hook that I chose was in the user auth service and named user.service_auth_login__cookie_start. This will place it after the user data has been collected but before the process changes the last_login date.

Initially I thought that I would just subtract one day in unix time from the current time and compare it with the last_login time to see if a day had passed, but I realized that a person could Login at 9:00 the first day and 8:30 the second day and not get their points. I finally settled on comparing a month/day integer like '0422' for the two times and if they are different then award the points.

A further complication occured when I realized that at this point the user object was not yet created, so some of the useful functions like Phpfox::getUserBy() will not work here. Consequently I couldn't call the userOffset from the date library and had to borrow the code, change it a little, and use it in the plugin.

So I made a plugin in the admincp like:

*Product: core
*Module: user
Title: PointsOnLogin
Hook: user.service_auth_login__cookie_start
Active: yes
phpCode:
PHP:
// I need two extra fields from the user table
$aLast $this->database()->select('last_login, time_zone')
    ->
from($this->_sTable)
    ->
where('user_id = ' $aRow['user_id'])
    ->
execute('getRow');

// This section gets the time offset for the user
$sOffset=0;
$sUserOffSet null;
if (
PHPFOX_USE_DATE_TIME)
{
    
$bDst false;
}
        
if (
$bDst === false)
{
    
$sUserOffSet $aLast['time_zone'];        
            
    if (empty(
$sUserOffSet))
    {
        
$sUserOffSet Phpfox::getParam('core.default_time_zone_offset');
    }            
            
    if (
substr($sUserOffSet,0,1) == 'z' && PHPFOX_USE_DATE_TIME)
    {
        
$aTZ Phpfox::getService('core')->getTimeZones();

        
$oGmt = new DateTimeZone('GMT');
        
$mTimeNow = new DateTime(null$oGmt);
        
$oTZ = new DateTimeZone($aTZ[$sUserOffSet]);
        
$oDateTime = new DateTime(null$oTZ);            

        
$sOffSet = ($oTZ->getOffset($mTimeNow));
    }
}

// Then we define the two times that we are comparing        
$iLast=(int)date('md',($aLast['last_login'] + $sOffSet));
$iThis=(int)date('md',(PHPFOX_TIME $sOffSet));

// award the point if the dates are different
if($iLast != $iThis)
{
  
Phpfox::getService('user.activity')->update($aRow['user_id'], 'comment','+','1');


If you have more than one point set up for comments, then that many points would go for logging in. I very quickly tested this so perform your own tests if you use any of this code.