<info>
TinyPortal BlockCode file.
format: Boardmod
</info>

<name>
Most Time Online
</name>

<author>
@rjen
</author>

<version>
1.1
</version>

<date>
24.April.2025
</date>

<code>
global $scripturl, $smcFunc, $txt, $modSettings;

// Configuration
// Specify number of users to show. Maximum is 20 
	$max = '10';
	$showtitle = 'Y';
// End Config

	loadLanguage('Stats');

	// Most Time online.
	$temp = cache_get_data('stats_total_time_members', 600);
	$members_result = $smcFunc['db_query']('', '
		SELECT id_member, real_name, total_time_logged_in
		FROM {db_prefix}members
		WHERE is_activated = {int:is_activated}' .
		(!empty($temp) ? ' AND id_member IN ({array_int:member_list_cached})' : '') . '
		ORDER BY total_time_logged_in DESC
		LIMIT {int:max}',
		array(
			'member_list_cached' => $temp,
			'is_activated' => 1,
			'max' => $max,
		)
	);
	$context['stats_blocks']['time_online'] = array();
	$temp2 = array();
	$max_time_online = 1;
	while ($row_members = $smcFunc['db_fetch_assoc']($members_result))
	{
		$temp2[] = (int) $row_members['id_member'];

		// Figure out the days, hours and minutes.
		$timeDays = floor($row_members['total_time_logged_in'] / 86400);
		$timeHours = floor(($row_members['total_time_logged_in'] % 86400) / 3600);

		// Figure out which things to show... (days, hours, minutes, etc.)
		$timelogged = '';
		if ($timeDays > 0)
			$timelogged .= $timeDays . $txt['total_time_logged_d'];
		if ($timeHours > 0)
			$timelogged .= $timeHours . $txt['total_time_logged_h'];
		$timelogged .= floor(($row_members['total_time_logged_in'] % 3600) / 60) . $txt['total_time_logged_m'];

		$context['stats_blocks']['time_online'][] = array(
			'id' => $row_members['id_member'],
			'name' => $row_members['real_name'],
			'num' => $timelogged,
			'seconds_online' => $row_members['total_time_logged_in'],
			'href' => $scripturl . '?action=profile;u=' . $row_members['id_member'],
			'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row_members['id_member'] . '">' . $row_members['real_name'] . '</a>'
		);

		if ($max_time_online < $row_members['total_time_logged_in'])
			$max_time_online = $row_members['total_time_logged_in'];
	}
	$smcFunc['db_free_result']($members_result);

	foreach ($context['stats_blocks']['time_online'] as $i => $member)
		$context['stats_blocks']['time_online'][$i]['percent'] = round(($member['seconds_online'] * 100) / $max_time_online);

// output the most online
	echo '
		<div class="content">';
	
	if ($showtitle == 'Y') 
	echo '
			<div class="title_bar">
				<h4 class="titlebg">
					<span class="main_icons posters"></span> ',  $txt['top_time_online'], '
				</h4>
			</div>';
	echo '
			<dl class="stats">';

	foreach ($context['stats_blocks']['time_online'] as $item)
	{
		echo '
				<dt>
					', $item['link'], '
				</dt>
				<dd class="statsbar generic_bar righttext">';

		if (!empty($item['percent']))
			echo '
					<div class="bar" style="width: ', $item['percent'], '%;"></div>';
		else
			echo '
					<div class="bar empty"></div>';

		echo '
					<span>', $item['num'], '</span>
				</dd>';
	}

	echo '
			</dl>
		</div><!-- .content -->';

</code>

<description>
This block will show the top X users with most time online in the forum. The layout is similar to the one used in the Stats section.<br>
Edit the php code in // Configuration in the block code to change the number of users to show and to show the title in the block itself. <br>
Default configuration values are:<br>
- 10 users<br>
- $showtitle=Y.<br>
</description>