Localized "Who's Online"-type functionality in header

Started by James Gryphon, December 18, 2017, 10:09:45 PM

Previous topic - Next topic

James Gryphon

As you all know there are different "Who's Online"-type sections on several different pages on the board. The feature proper, on the board index, and the different "who's viewing this board/page" ones as well. I was looking into trying to make them appear, when they appear (I'm not talking about putting the one on the BoardIndex on every page! They would appear on the pages they do now), into the header, above where the link tree is now.
I'm trying to figure out the most efficient way to do it. It'd be nice if there was some way of calling a function and having the other PHP file that's pulled up provide it, but I don't know if it works that way. If anyone has any advice for a solid (and secure) way to perform this process, I'd greatly appreciate it.

James Gryphon

I hacked something together.

In index.template.php, the linktree is modified slightly.


echo '
<div class="navigate_section">';
if (function_exists('look_around')) {
look_around(); };
echo '
<ul>';


There's a new "look_around" function added in three of the templates: BoardIndex, MessageIndex and Display. These contain basically the same code that was used in their original respective areas (users online and "who viewing".)

That done, I thought it might be helpful to check in here so that y'all can tell me what's wrong with it.

Arantor

The actual logic for working out who is online isn't in the template. It's physically contained in the board index code, the message index code and the display code and is different in all three cases, because they do different things to work out what's going on and a one step solution isn't really going to get you there.

James Gryphon

Well, I'm concerned you might have misunderstood me slightly, if only because my implementation of it does seem to be working so far. Let me try to explain what I did a little more clearly than I did before, since the previous post is rather sparse on details.

index.template.php is modified as above. Inside BoardIndex.template.php, I take the segment starting from "// "Users online" up until where the next segment starts, cut it out of where it is at present, and insert it into a new function like so:
function look_around()
{
global $context, $settings, $options, $txt, $scripturl, $modSettings;
// "Users online" - in order of activity.
echo '
<div class="title_barIC">
<h4 class="titlebg">
<span class="ie6_header floatleft">
', $context['show_who'] ? '<a href="' . $scripturl . '?action=who' . '">' : '', '<img class="icon" src="', $settings['images_url'], '/icons/online.gif', '" alt="', $txt['online_users'], '" />', $context['show_who'] ? '</a>' : '', '
', $txt['online_users'], '
</span>
</h4>
</div>
<p class="inline stats">
', $context['show_who'] ? '<a href="' . $scripturl . '?action=who">' : '', comma_format($context['num_guests']), ' ', $context['num_guests'] == 1 ? $txt['guest'] : $txt['guests'], ', ' . comma_format($context['num_users_online']), ' ', $context['num_users_online'] == 1 ? $txt['user'] : $txt['users'];

// Handle hidden users and buddies.
$bracketList = array();
if ($context['show_buddies'])
$bracketList[] = comma_format($context['num_buddies']) . ' ' . ($context['num_buddies'] == 1 ? $txt['buddy'] : $txt['buddies']);
if (!empty($context['num_spiders']))
$bracketList[] = comma_format($context['num_spiders']) . ' ' . ($context['num_spiders'] == 1 ? $txt['spider'] : $txt['spiders']);
if (!empty($context['num_users_hidden']))
$bracketList[] = comma_format($context['num_users_hidden']) . ' ' . $txt['hidden'];

if (!empty($bracketList))
echo ' (' . implode(', ', $bracketList) . ')';

echo $context['show_who'] ? '</a>' : '', '
</p>
<p class="inline smalltext">';

// Assuming there ARE users online... each user in users_online has an id, username, name, group, href, and link.
if (!empty($context['users_online']))
{
echo '
', sprintf($txt['users_active'], $modSettings['lastActive']), ':<br />', implode(', ', $context['list_users_online']);

// Showing membergroups?
if (!empty($settings['show_group_key']) && !empty($context['membergroups']))
echo '
<br />[' . implode(']&nbsp;&nbsp;[', $context['membergroups']) . ']';
}

echo '
</p>
<p class="last smalltext">
', $txt['most_online_today'], ': <strong>', comma_format($modSettings['mostOnlineToday']), '</strong>.
', $txt['most_online_ever'], ': ', comma_format($modSettings['mostOnline']), ' (', timeformat($modSettings['mostDate']), ')
</p>';
}


The other two files, MessageIndex.template.php and Display.template.php, are modified in analogous fashion. This produces the result shown in the attachments, and can be demonstrated over at my test forum.

Arantor

Oh, I see what you've done. Yes, I did misunderstand both your purpose and your solution.

Advertisement: