PDA

View Full Version : sidebar block


Wisc
12-05-2015, 05:00 AM
I would like to have sidebar block "Visiting Members" That shows the visiting members for X amount of days

What is the code I would put in the content box to accomplish this.

As always Thank you in advance!

MarkFL
12-05-2015, 05:53 AM
Here is how I set it up on my local dev site:

https://vborg.vbsupport.ru/attachment.php?attachmentid=153768&stc=1&d=1449301635

And this is the PHP code I wrote:

global $vbulletin, $db;
$output = '<div class="restore"><ul>';

$users_visited = $vbulletin->db->query_read("
SELECT user.*
FROM " . TABLE_PREFIX . "user AS user
WHERE lastvisit >= " . TIMENOW . " - " . 7 * 86400 . "
ORDER BY lastvisit DESC
");

$vcount = 0;
while ($visitor = $db->fetch_array($users_visited))
{
$output .= '<li>' . visitor_link($visitor, $visitor['lastvisit']) . '</li>';
$vcount++;
}

$output .= '</ul></div>';

$output = '<div style="text-align: center; font-weight: bold; margin-bottom: 1em">' . $vcount . ' Visiting Members</div>' . $output;

return $output;

function visitor_link($user_name, $dateline)
{
global $vbulletin;
$link = 'member.php?do=getinfo&username=' . $user_name['username'];

if ($user_name['displaygroupid'])
{
$groupid = $user_name['displaygroupid'];
}
else
{
$groupid = $user_name['usergroupid'];
}

$open_tag = $vbulletin->usergroupcache[$groupid]['opentag'];
$close_tag = $vbulletin->usergroupcache[$groupid]['closetag'];
$title = 'Last Visited: ' . vbdate($vbulletin->options['dateformat'], $dateline, 1) . ' at ' . vbdate($vbulletin->options['timeformat'], $dateline);
return '<a title="' . $title . '" href="' . $link . '">' . $open_tag . $user_name['username'] . $close_tag . '</a>';
}

This is the result:

https://vborg.vbsupport.ru/attachment.php?attachmentid=153769&stc=1&d=1449301765

Each username is shown with their usergroup HTML markup in a bulleted list, and each username links to their profile, with a tooltip showing their last visit time. :)

Please give this a try, and let me know of any changes you would like.

Wisc
12-07-2015, 03:23 AM
gave me : 406 Not Acceptable
when i tried to save it...

MarkFL
12-07-2015, 03:47 AM
gave me : 406 Not Acceptable
when i tried to save it...

406 happens when the server cannot respond with the accept-header specified in the request.

Unfortunately, I cannot duplicate the error, it works fine on both my local and online dev sites.

Wisc
12-07-2015, 04:05 AM
so id server issue or I should try to
refresh the blocks
clear the cache

and do again?

Thank you for your hard work...
I think will be helpful for a lot of people

MarkFL
12-07-2015, 04:14 AM
so id server issue or I should try to
refresh the blocks
clear the cache

and do again?

Thank you for your hard work...
I think will be helpful for a lot of people

It sounds like a server issue to me, from the brief amount I read about it online. As such, it is outside my relatively diminutive areas of expertise, so I don't know what to tell you to try to resolve the issue. Hopefully someone else here can offer some suggestions.

What I suggest doing is creating a new thread, where the title indicates you are getting a 406 error when trying to save a forum block, and this will be more likely to get you some help rather than to trust that someone who knows how to fix it will stumble across this thread. :D

Wisc
12-07-2015, 05:46 AM
Thank you again MarK...the code is a huge help and others will use too i am sure
Happy Holidays

webmastersun
12-15-2015, 11:04 AM
Cool tips Mark!

Your codes can be edited a bit to make a block on right sidebar that show users online last 24 hours...instead of showing at the bottom of homepage.

setishock
12-30-2015, 03:47 PM
Mark, is there a way to make it so it doesn't show banned members? All but a few of the listed visitors are spammers that got banned. I'd prefer not to show those.

MarkFL
12-30-2015, 06:35 PM
Mark, is there a way to make it so it doesn't show banned members? All but a few of the listed visitors are spammers that got banned. I'd prefer not to show those.

Yes, change the query near the top of the PHP content to read:

$users_visited = $vbulletin->db->query_read("
SELECT user.*
FROM " . TABLE_PREFIX . "user AS user
WHERE lastvisit >= " . TIMENOW . " - " . 7 * 86400 . "
AND usergroupid != 8
ORDER BY lastvisit DESC
");

CAG CheechDogg
12-30-2015, 08:00 PM
Mark sorry to highjack this thread ...but how about to show only 1 usergroup even if they belong to a secondary usergroup?

Dragonsys
12-30-2015, 09:11 PM
Mark sorry to highjack this thread ...but how about to show only 1 usergroup even if they belong to a secondary usergroup?

it doesn't display usergroups. Are you wanting to show all member of only 1 specific group? If so, change the query to this:
$users_visited = $vbulletin->db->query_read("
SELECT user.*
FROM " . TABLE_PREFIX . "user AS user
WHERE lastvisit >= " . TIMENOW . " - " . 7 * 86400 . "
AND usergroupid = _YOURUSERGROUP_
ORDER BY lastvisit DESC
");

change _YOURUSERGROUP_ to the group ID you want to show

CAG CheechDogg
12-30-2015, 09:43 PM
it doesn't display usergroups. Are you wanting to show all member of only 1 specific group? If so, change the query to this:
$users_visited = $vbulletin->db->query_read("
SELECT user.*
FROM " . TABLE_PREFIX . "user AS user
WHERE lastvisit >= " . TIMENOW . " - " . 7 * 86400 . "
AND usergroupid = _YOURUSERGROUP_
ORDER BY lastvisit DESC
");

change _YOURUSERGROUP_ to the group ID you want to show

What I was really looking for is to show members of a specific usergroup who are online now. I have members who belong to usergroup "Officers" and would like to show them online now if they are online now kind of like they are staff who are online so that members can contact them if they have questions... know what I mean?

MarkFL
12-30-2015, 10:10 PM
What I was really looking for is to show members of a specific usergroup who are online now. I have members who belong to usergroup "Officers" and would like to show them online now if they are online now kind of like they are staff who are online so that members can contact them if they have questions... know what I mean?

When I am in for the night, I will post the code to display members of a certain usergroup who are online at the moment. :)

CAG CheechDogg
12-30-2015, 11:45 PM
When I am in for the night, I will post the code to display members of a certain usergroup who are online at the moment. :)

Good stuff Mark thank you so much !!!!!!

:up::up::up:

setishock
12-31-2015, 12:06 AM
I wasn't paying attention and just put in the change you made to weed out banned members. It gave me a server error. Had one of those DOH moments. When I replaced the old with the new in its entirety, it worked as advertised. Now all that shows is me and the supermod and the folks that just signed up for S&G.

Thanks again. If I could program like you my Arduino wouldn't be sitting on my desk gathering dust.

CAG CheechDogg
12-31-2015, 05:33 AM
When I am in for the night, I will post the code to display members of a certain usergroup who are online at the moment. :)

Any progress on this Senor Mark?

MarkFL
12-31-2015, 05:42 AM
Any progress on this Senor Mark?

I had some buddies show up unexpectedly tonight, and many beers were quaffed, and I decided to put off anything serious until tomorrow. :o

--------------- Added 1451590749 at 1451590749 ---------------

Any progress on this Senor Mark?

You will likely want to set "Cache Time (in minutes)" to 0 so that this information is always current. This is the PHP content I used on my dev site to show all Math Helpers currently online:

global $vbulletin, $db;
$groupid = 13;
$groupname = 'Math Helpers';
$output = '<div class="restore"><ul>';

$users_online = $vbulletin->db->query_read("
SELECT user.*, session.loggedin
FROM " . TABLE_PREFIX . "user AS user
INNER JOIN " . TABLE_PREFIX . "session AS session
ON session.userid = user.userid
WHERE (user.usergroupid = " . $groupid . "
OR " . $groupid . " IN (user.membergroupids))
AND session.loggedin > 0
ORDER BY user.lastactivity DESC
");

$count = 0;
while ($member = $db->fetch_array($users_online))
{
$output .= '<li>' . helper_link($member, $member['lastactivity']) . '</li>';
$count++;
}

$output .= '</ul></div>';

$output = '<div style="text-align: center; font-weight: bold; margin-bottom: 1em">' . $count . ' ' . $groupname . ' Online</div>' . $output;

return $output;

function helper_link($user_name, $dateline)
{
global $vbulletin;
$link = 'member.php?do=getinfo&username=' . $user_name['username'];

if ($user_name['displaygroupid'])
{
$groupid = $user_name['displaygroupid'];
}
else
{
$groupid = $user_name['usergroupid'];
}

$open_tag = $vbulletin->usergroupcache[$groupid]['opentag'];
$close_tag = $vbulletin->usergroupcache[$groupid]['closetag'];
$title = 'Last Activity: ' . vbdate($vbulletin->options['dateformat'], $dateline, 1) . ' at ' . vbdate($vbulletin->options['timeformat'], $dateline);
return '<a title="' . $title . '" href="' . $link . '">' . $open_tag . $user_name['username'] . $close_tag . '</a>';
}

You will want to edit these two lines near the top:

$groupid = 13;
$groupname = 'Math Helpers';


to use the groupid and groupname of your choice.

This will list all users currently online who are a member of the selected usergroupid, and list them in descending order by the time of their last activity.

I should also add, that if you wish to use multiple groupids (and have user avatars displayed), I recommend checking out Joe's (BirdOPrey5) product here:

Current Staff Online Forum Sideblock and CMS Widget by BOP5 (https://vborg.vbsupport.ru/showthread.php?t=284617)

CAG CheechDogg
01-01-2016, 04:35 PM
I had some buddies show up unexpectedly tonight, and many beers were quaffed, and I decided to put off anything serious until tomorrow. :o

--------------- Added 1451590749 at 1451590749 ---------------



You will likely want to set "Cache Time (in minutes)" to 0 so that this information is always current. This is the PHP content I used on my dev site to show all Math Helpers currently online:

global $vbulletin, $db;
$groupid = 13;
$groupname = 'Math Helpers';
$output = '<div class="restore"><ul>';

$users_online = $vbulletin->db->query_read("
SELECT user.*, session.loggedin
FROM " . TABLE_PREFIX . "user AS user
INNER JOIN " . TABLE_PREFIX . "session AS session
ON session.userid = user.userid
WHERE (user.usergroupid = " . $groupid . "
OR " . $groupid . " IN (user.membergroupids))
AND session.loggedin > 0
ORDER BY user.lastactivity DESC
");

$count = 0;
while ($member = $db->fetch_array($users_online))
{
$output .= '<li>' . helper_link($member, $member['lastactivity']) . '</li>';
$count++;
}

$output .= '</ul></div>';

$output = '<div style="text-align: center; font-weight: bold; margin-bottom: 1em">' . $count . ' ' . $groupname . ' Online</div>' . $output;

return $output;

function helper_link($user_name, $dateline)
{
global $vbulletin;
$link = 'member.php?do=getinfo&username=' . $user_name['username'];

if ($user_name['displaygroupid'])
{
$groupid = $user_name['displaygroupid'];
}
else
{
$groupid = $user_name['usergroupid'];
}

$open_tag = $vbulletin->usergroupcache[$groupid]['opentag'];
$close_tag = $vbulletin->usergroupcache[$groupid]['closetag'];
$title = 'Last Activity: ' . vbdate($vbulletin->options['dateformat'], $dateline, 1) . ' at ' . vbdate($vbulletin->options['timeformat'], $dateline);
return '<a title="' . $title . '" href="' . $link . '">' . $open_tag . $user_name['username'] . $close_tag . '</a>';
}

You will want to edit these two lines near the top:

$groupid = 13;
$groupname = 'Math Helpers';


to use the groupid and groupname of your choice.

This will list all users currently online who are a member of the selected usergroupid, and list them in descending order by the time of their last activity.

I should also add, that if you wish to use multiple groupids (and have user avatars displayed), I recommend checking out Joe's (BirdOPrey5) product here:

Current Staff Online Forum Sideblock and CMS Widget by BOP5 (https://vborg.vbsupport.ru/showthread.php?t=284617)

TY Mark ... I tried using BOP5's CSOFS mod on my site but for some reason it doesn't work ....

--------------- Added 1451683913 at 1451683913 ---------------

Mark ..is it possible to edit this so that when there is only one member of said group it says "1 Officer Online" when there is only one member (Officer) instead of "1 Officers Online"

--------------- Added 1451685439 at 1451685439 ---------------

Ooops ...nevermind Mark ...this doesn't work with for additional usergroups .... it will only display members of the primary usergroup .... can this be modified to show members if they also have the groupid as the additional usergroup?

--------------- Added 1451685565 at 1451685565 ---------------

Or allow to do an array of group ids?

--------------- Added 1451692168 at 1451692168 ---------------

Bummer !!! This works but for some reason it works for some of the members and not others ... at one point it listed one member 10 times ...

--------------- Added 1451692371 at 1451692371 ---------------

This is what is happening:

https://vborg.vbsupport.ru/external/2016/01/24.jpg

MarkFL
01-02-2016, 01:20 AM
I did not realize there were new posts in this thread because of doublepost merges. Sorry about that. To allow multiple usergroups and to prevent repetition, try this (of course edit the $groupid/$groupname definitions):

global $vbulletin, $db;
$groupid = '6,13';
$groupname = 'Math Helpers/Administrators';
$output = '<div class="restore"><ul>';

$groupid_arr = explode(',', $groupid);
$qwhere = '';

foreach ($groupid_arr AS $id)
{
$qwhere .= 'OR ' . $id . ' IN (user.membergroupids) ';
}

$users_online = $vbulletin->db->query_read("
SELECT user.*, session.loggedin
FROM " . TABLE_PREFIX . "user AS user
INNER JOIN " . TABLE_PREFIX . "session AS session
ON session.userid = user.userid
WHERE user.usergroupid IN (" . $groupid . ")
" . $qwhere . "
AND session.loggedin > 0
GROUP BY user.userid
ORDER BY user.lastactivity DESC
");

$count = 0;
while ($member = $db->fetch_array($users_online))
{
$output .= '<li>' . helper_link($member, $member['lastactivity']) . '</li>';
$count++;
}

$output .= '</ul></div>';

$output = '<div style="text-align: center; font-weight: bold; margin-bottom: 1em">' . $count . ' ' . $groupname . ' Online</div>' . $output;

return $output;

function helper_link($user_name, $dateline)
{
global $vbulletin;
$link = 'member.php?do=getinfo&username=' . $user_name['username'];

if ($user_name['displaygroupid'])
{
$groupid = $user_name['displaygroupid'];
}
else
{
$groupid = $user_name['usergroupid'];
}

$open_tag = $vbulletin->usergroupcache[$groupid]['opentag'];
$close_tag = $vbulletin->usergroupcache[$groupid]['closetag'];
$title = 'Last Activity: ' . vbdate($vbulletin->options['dateformat'], $dateline, 1) . ' at ' . vbdate($vbulletin->options['timeformat'], $dateline);
return '<a title="' . $title . '" href="' . $link . '">' . $open_tag . $user_name['username'] . $close_tag . '</a>';
}

CAG CheechDogg
01-02-2016, 03:36 AM
Ok good this works, just one small bug .... it still shows one member active from 5 hours ago who is a test account ....

--------------- Added 1451768165 at 1451768165 ---------------

Any update on this Mark as to why it will still show someone logged in from 5,10 and 20 hours ago?

I did notice though that in order for this to consider someone active they have to actually execute something like post a comment, like a comment not just be logged on ....

What other options are there for this, can this display members who are just logged on period? And does it also detect logged in users from Tapatalk? I see that there are members who are online logged in but through tapatalk but are not being displayed on this sidebar block ....

Thank you ahead of time Mark !!!

MarkFL
01-02-2016, 06:59 PM
Ok good this works, just one small bug .... it still shows one member active from 5 hours ago who is a test account ....

--------------- Added 1451768165 at 1451768165 ---------------

Any update on this Mark as to why it will still show someone logged in from 5,10 and 20 hours ago?

I did notice though that in order for this to consider someone active they have to actually execute something like post a comment, like a comment not just be logged on ....

What other options are there for this, can this display members who are just logged on period? And does it also detect logged in users from Tapatalk? I see that there are members who are online logged in but through tapatalk but are not being displayed on this sidebar block ....

Thank you ahead of time Mark !!!

I will have to take a look at a better way to determine which users are actually online. :)

CAG CheechDogg
01-02-2016, 07:05 PM
Ok good stuff my Man .... thank again !!!!

MarkFL
01-03-2016, 01:19 AM
Okay, add the following line before the query:

$timecutoff = TIMENOW - $vbulletin->options['cookietimeout'];

And then change the db query to the following:

$users_online = $vbulletin->db->query_read("
SELECT user.*
FROM " . TABLE_PREFIX . "user AS user
INNER JOIN " . TABLE_PREFIX . "session AS session
ON session.userid = user.userid
WHERE user.usergroupid IN (" . $groupid . ")
" . $qwhere . "
AND session.loggedin > 0
AND session.lastactivity >= " . $timecutoff . "
GROUP BY user.userid
ORDER BY user.lastactivity DESC
");

CAG CheechDogg
01-03-2016, 02:15 AM
Where exactly do I add

$timecutoff = TIMENOW - $vbulletin->options['cookietimeout'];

MarkFL
01-03-2016, 02:30 AM
Where exactly do I add

$timecutoff = TIMENOW - $vbulletin->options['cookietimeout'];

Anywhere before the db query will be fine. :)

This is what I now have:

global $vbulletin, $db;
$groupid = '6,13';
$groupname = 'Math Helpers/Administrators';
$timecutoff = TIMENOW - $vbulletin->options['cookietimeout'];
$output = '<div class="restore"><ul>';

$groupid_arr = explode(',', $groupid);
$qwhere = '';

foreach ($groupid_arr AS $id)
{
$qwhere .= 'OR ' . $id . ' IN (user.membergroupids) ';
}

$users_online = $vbulletin->db->query_read("
SELECT user.*
FROM " . TABLE_PREFIX . "user AS user
INNER JOIN " . TABLE_PREFIX . "session AS session
ON session.userid = user.userid
WHERE user.usergroupid IN (" . $groupid . ")
" . $qwhere . "
AND session.loggedin > 0
AND session.lastactivity >= " . $timecutoff . "
GROUP BY user.userid
ORDER BY user.lastactivity DESC
");

$count = 0;
while ($member = $db->fetch_array($users_online))
{
$output .= '<li>' . helper_link($member, $member['lastactivity']) . '</li>';
$count++;
}

$output .= '</ul></div>';

$output = '<div style="text-align: center; font-weight: bold; margin-bottom: 1em">' . $count . ' ' . $groupname . ' Online</div>' . $output;

return $output;

function helper_link($user_name, $dateline)
{
global $vbulletin;
$link = 'member.php?do=getinfo&username=' . $user_name['username'];

if ($user_name['displaygroupid'])
{
$groupid = $user_name['displaygroupid'];
}
else
{
$groupid = $user_name['usergroupid'];
}

$open_tag = $vbulletin->usergroupcache[$groupid]['opentag'];
$close_tag = $vbulletin->usergroupcache[$groupid]['closetag'];
$title = 'Last Activity: ' . vbdate($vbulletin->options['dateformat'], $dateline, 1) . ' at ' . vbdate($vbulletin->options['timeformat'], $dateline);
return '<a title="' . $title . '" href="' . $link . '">' . $open_tag . $user_name['username'] . $close_tag . '</a>';
}