TinyPortal

Development => Block Codes => Latest block snippets => Topic started by: IchBin on January 11, 2010, 09:09:19 PM

Title: [Block] Users Online with Avatar
Post by: IchBin on January 11, 2010, 09:09:19 PM
Name of Snippet: Users Online with Avatar
SMF/TP versions tested:: SMF1 & SMF2/TP1 beta 4
Block Type:: php
Author: IchBin
Link to Discussion: http://www.tinyportal.net/index.php?topic=27231.0
Other Requirements: None
Description:
This php code snippet will only work in a phpbox type block. It has a configurable column setting so it can fit a top/bottom or side block. You can also set a time limit on how far back you want to show users online i.e. 15 minutes, 1hour, 1day. All of the configurable options are put in the top, including any CSS changes you want to make. If your user avatars are larger or smaller than 100x100 pixels wide, adjust the CSS accordingly in the code. A default avatar is set if the user does not have an avatar. This assumes there is a file named noavatar.gif (attached) in your themes images directory.

Code For SMF1.x

global $db_prefix, $scripturl, $modSettings;

/* ###  CONFIGURATION OPTIONS  ### */
// Set time limit using seconds
// 1 hour = 3600
// 1 day = 84600
$timelimit = 3600;

// Set $columns to the amount of columns you need
// You set this depending on whether you want it in a side block or a top/bottom block.
$columns = 5;

// Set the height and width of avatars
// This will resize the avatars to be more uniform
$width = '40px';
$height = '40px';

// Set or change the style of all the elements
echo '<style type="text/css">
.avatar_column{
border: 0;
}
.avatar_column td{
height: 100px;
width: 100px;
overflow: hidden;
text-align: center;
vertical-align: top;
}
.default{
border: 2px solid black;
}
.he{
border: 2px solid blue;
}
.she{
border: 2px solid pink;
}
</style>';

/* ###  END CONFIGURATION OPTIONS  ### */

// Load all users who have logged in within $timelimit
$result = db_query("
SELECT mem.ID_MEMBER, mem.showOnline, mem.lastLogin, mem.realName, mem.avatar, mem.gender, a.ID_ATTACH, a.attachmentType, a.filename
FROM ({$db_prefix}members as mem)
LEFT JOIN {$db_prefix}attachments AS a ON (a.ID_MEMBER = mem.ID_MEMBER)
WHERE mem.lastLogin > (UNIX_TIMESTAMP() - $timelimit)
ORDER BY mem.lastLogin DESC", __FILE__, __LINE__);

$users = array();

// Loop through the results to display the users avatar
while ($row = mysql_fetch_assoc($result)){
    $users[$row['ID_MEMBER']] = array (
    'id' => $row['ID_MEMBER'],
    'href' => $scripturl.'?action=profile;u='.$row['ID_MEMBER'],
    'name' => $row['realName'],
    'show' => $row['showOnline'],
'gender' => $row['gender'],
    'avatar' => array(
    'image' => $row['avatar'] == '' ? ($row['ID_ATTACH'] > 0 ? 'src="' . (empty($row['attachmentType']) ? $scripturl . '?action=dlattach;attach=' . $row['ID_ATTACH'] . ';type=avatar' : $modSettings['custom_avatar_url'] . '/' . $row['filename']) . '" alt="" border="0" width="'.$width.'" height="'.$height.'" title="'.$row['realName'].'" />' : '') : (stristr($row['avatar'], 'http://') ? 'src="' . $row['avatar'] . '" alt="" border="0" width="'.$width.'" height="'.$height.'" title="'.$row['realName'].'" />' : 'src="' . $modSettings['avatar_url'] . '/' . htmlspecialchars($row['avatar']) . '" alt="" border="0" width="'.$width.'" height="'.$height.'" title="'.$row['realName'].'" />'),
    ),
    );
}

mysql_free_result($result);

echo '
<table class="avatar_column">
<tr>';

$counter = 0;

foreach ($users as $user){
if ($user['show'] == 1){
switch ($user['gender']){
case 0:
$css = "default";
break;
case 1:
$css = "he";
break;
case 2:
$css = "she";
break;
}
if ($counter < $columns){
echo '
    <td>',empty($user['avatar']['image']) ? '<a href="'.$user['href'].'"><img class="'.$css.'" src="'.$settings['images_url'].'/noavatar.gif" width="'.$width.'" height="'.$height.'" alt="" title="'.$user['name'].'" /></a><h6>'.$user['name'].'</h6>' : '<a href="'.$user['href'].'"><img class="'.$css.'" '.$user['avatar']['image'].'</a><h6>'.$user['name'].'</h6></td>';
    $counter++;
}
else{
echo '
</tr>
<tr>
<td>',empty($user['avatar']['image']) ? '<a href="'.$user['href'].'"><img class="'.$css.'" src="'.$settings['images_url'].'/noavatar.gif" width="'.$width.'" height="'.$height.'" alt="" /></a><h6>'.$user['name'].'</h6>' : '<a href="'.$user['href'].'"><img class="'.$css.'" '.$user['avatar']['image'].'</a><h6>'.$user['name'].'</h6></td>';
$counter = 1;
}
}
}
echo '
</tr>
</table>';
Title: Re: [Block] Users Online with Avatar
Post by: IchBin on January 16, 2010, 01:30:43 AM
Code for SMF2

global $smcFunc, $db_prefix, $scripturl, $modSettings, $settings;

/* ###  CONFIGURATION OPTIONS  ### */
// Set time limit using seconds
// 1 hour = 3600
// 1 day = 84600
$timelimit = 3600;

// Set $columns to the amount of columns you need
// You set this depending on whether you want it in a side block or a top/bottom block.
$columns = 5;

// Set the height and width of avatars
// This will resize the avatars to be more uniform
$width = '40px';
$height = '40px';

// Set or change the style of all the elements
echo '<style type="text/css">
.avatar_column{
border: 0;
}
.avatar_column td{
height: 100px;
width: 100px;
overflow: hidden;
text-align: center;
vertical-align: top;
}
.default{
border: 2px solid black;
}
.he{
border: 2px solid blue;
}
.she{
border: 2px solid pink;
}
</style>';
/* ###  END OF CONFIGURATION OPTIONS  ### */

$mdate = date("U");

// Load all users who have logged in within $timelimit
$result = $smcFunc['db_query']('', '
SELECT mem.id_member, mem.show_online, mem.last_login, mem.real_name, mem.avatar, mem.gender, a.id_attach, a.attachment_type, a.filename
FROM smf_members AS mem
LEFT JOIN smf_attachments AS a ON (a.id_member = mem.id_member)
WHERE mem.last_login > ({int:date} - '.$timelimit.')
ORDER BY mem.last_login DESC',
array('date' => $mdate)
);

$users = array();

// Loop through the results to display the users avatar
while ($row = $smcFunc['db_fetch_assoc']($result)) {
    $users[$row['id_member']] = array (
    'id' => $row['id_member'],
    'href' => $scripturl.'?action=profile;u='.$row['id_member'],
    'name' => $row['real_mame'],
    'show' => $row['show_online'],
'gender' => $row['gender'],
    'avatar' => array(
    'image' => $row['avatar'] == '' ? ($row['id_attach'] > 0 ? 'src="' . (empty($row['attachment_type']) ? $scripturl . '?action=dlattach;attach=' . $row['id_attach'] . ';type=avatar' : $modSettings['custom_avatar_url'] . '/' . $row['filename']) . '" alt="" border="0" width="'.$width.'" height="'.$height.'" title="'.$row['real_name'].'" />' : '') : (stristr($row['avatar'], 'http://') ? 'src="' . $row['avatar'] . '" alt="" border="0" width="'.$width.'" height="'.$height.'" title="'.$row['real_name'].'" />' : 'src="' . $modSettings['avatar_url'] . '/' . htmlspecialchars($row['avatar']) . '" alt="" border="0" width="'.$width.'" height="'.$height.'" title="'.$row['real_name'].'" />'),
    ),
    );
}

$smcFunc['db_free_result']($result);

echo '
<table class="avatar_column">
<tr>';

$counter = 0;

foreach ($users as $user){
if ($user['show'] == 1){
switch ($user['gender']){
case 0:
$css = "default";
break;
case 1:
$css = "he";
break;
case 2:
$css = "she";
break;
}
if ($counter < $columns){
echo '
    <td>',empty($user['avatar']['image']) ? '<a href="'.$user['href'].'"><img class="'.$css.'" src="'.$settings['images_url'].'/noavatar.gif" width="'.$width.'" height="'.$height.'" alt="" title="'.$user['name'].'" /></a><h6>'.$user['name'].'</h6>' : '<a href="'.$user['href'].'"><img class="'.$css.'" '.$user['avatar']['image'].'</a><h6>'.$user['name'].'</h6></td>';
    $counter++;
}
else{
echo '
</tr>
<tr>
<td>',empty($user['avatar']['image']) ? '<a href="'.$user['href'].'"><img class="'.$css.'" src="'.$settings['images_url'].'/noavatar.gif" width="'.$width.'" height="'.$height.'" alt="" /></a><h6>'.$user['name'].'</h6>' : '<a href="'.$user['href'].'"><img class="'.$css.'" '.$user['avatar']['image'].'</a><h6>'.$user['name'].'</h6></td>';
$counter = 1;
}
}
}
echo '
</tr>
</table>';
Title: Re: [Block] Users Online with Avatar
Post by: Renegd98 on March 06, 2010, 01:39:00 PM
IchBin,

You need to add $settings to the global define line... or .$settings throws and error.
Title: Re: [Block] Users Online with Avatar
Post by: IchBin on March 07, 2010, 02:17:43 AM
Done