Friday, March 4, 2011

Add total time logged in infos to SMF message posters

Well,
some hours ago I had the need to show the total time logged in info in the poster message area below the number of posts published by the member.
The forum platform is SMF, I 've just started working with it so I can't give a professional opinion about it.
Now what we have to do to achieve our goal is what follows.
  1. Edit the /Sources/Load.php file.
    goto line 838 (inside the loadMemberData method) and add to the fields selected by the query the one we want: mem.totalTimeLoggedIn
  2. Well, now the information we want is selected from db.
  3. Goto line 1006 (inside loadMemberContext method). Here the member context variable to be passed to the template is prepared. We have to add our informations to the array returned by this method, so add the following key=>value pair to the $memberContext[$user]  array:
    'total_time_logged_in' => array(
    'days' => floor($profile['totalTimeLoggedIn'] / 86400),
    'hours' => floor(($profile['totalTimeLoggedIn'] % 86400) / 3600),
    'minutes' => floor(($profile['totalTimeLoggedIn'] % 3600) / 60)
    )
  4. Well done, now our information is present in the context member variable passed to the template we have to edit. Notice that we have divided the amount of time in days, hours and minutes.
  5. Now we have to show this information in the right place, that is the right template, which is /Themes/default/Display.template.php, or the one corresponding to your used theme if present in the theme folder. Open the file and goto line 316. At this line are printed the informations about the number of posts written by the message poster, so below that we insert our code:
    // Show how many posts they have made.
    echo '
    ', $txt[26], ': ', $message['member']['posts'], '

    ';
    // Show total time online.
    if (!empty($message['member']['total_time_logged_in'])) {
    echo '
    ', $txt['totalTimeLogged1'];

    // If days is just zero, don't bother to show it.
    if ($message['member']['total_time_logged_in']['days'] > 0)
    echo $message['member']['total_time_logged_in']['days'], $txt['totalTimeLogged5'];

    // Same with hours - only show it if it's above zero.
    if ($message['member']['total_time_logged_in']['hours'] > 0)
    echo $message['member']['total_time_logged_in']['hours'], $txt['totalTimeLogged6'];

    // But, let's always show minutes - Time wasted here: 0 minutes ;).
    echo $message['member']['total_time_logged_in']['minutes'], $txt['totalTimeLogged7'], '<br /><br />';
    }
    else echo '<br />';
Ok, only some considerations:
I displayed the count of time in this form;
xd xh xm
you can show it as
x days x hours and x minutes
or in the form you want, you only have to edit the language locate files of the languages you're interedsted in and set the text you prefer, you may define new strings and so on. Brutally you may also print the string you want directly into the template without using the locate system.
Hasta la proxima siempre.

No comments: