Gossamer Forum
Home : Products : Gossamer Forum : Discussion :

Who's Online Global

Quote Reply
Who's Online Global
I'm trying to figure out how to make the Who's Online global display with each username listed, linked to their profile view. I have the following:

Code:
sub {
my @found = ();
my $table = $DB->table('Online','User');
my $sth = $table->select( { 'Online.online_invisible' => 0, 'Online.guest_id_fk' => 0 }, ['User.user_username'] );

while (my $userid = $sth->fetchrow_hashref) {
push @found, "<A Href=\"http://www.mydomain.com/cgi-bin/forums/gforum.cgi?username=$userid->{user_username}\">$userid->{user_username}</A>";
}
return (@found ? join(', ', @found) : 'None');
}

This displays the HTML, but it converts the HTML code once it is displayed in a template. I see the actual code, because it converts the <>=" characters to ascii codes. Any advice on how to get this to work would be greatly appreciated!

Sean
Quote Reply
Re: [SeanP] Who's Online Global In reply to
Hi Sean,

In order to have GForum globals return HTML that won't be HTML-escaped, you need to return the value as a Perl scalar reference - to do this, change the last line:

return (@found ? join(', ', @found) : 'None');

to:

return (@found ? \join(', ', @found) : 'None');

Without the \, Gossamer Forum will HTML-escape any return values from globals.

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com
Quote Reply
Re: [Jagerman] Who's Online Global In reply to
That works... Thanks. How do you sort the usernames in alphabetical order?

Sean
Quote Reply
Re: [SeanP] Who's Online Global In reply to
Before this line:

my $sth = $table->select(...

add:

$table->select_options("ORDER BY user_username");

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com
Quote Reply
Re: [Jagerman] Who's Online Global In reply to
Works great! Thanks!
Quote Reply
Re: [Jagerman] Who's Online Global In reply to
My final global now looks like:

Code:
sub {

my @found;
my $table = $DB->table('Online','User');
$table->select_options ('ORDER BY user_username ASC');
my $sth = $table->select( { 'Online.online_invisible' => 0, 'Online.guest_id_fk' => 0 }, ['User.user_username']);

while (my $userid = $sth->fetchrow_hashref) {
push @found, "<font size=1><A Href=\"/cgi-bin/forums/gforum.cgi?username=$userid->{user_username}\">$userid->{user_username}</A></font>";
}
return (@found ? \join(', ', @found) : 'None');
}


I'm having a slight problem with the Online table not updating itself properly until someone accesses the Who's Online page. So, if someone logs off and nobody accesses the Who's Online for a while, it still lists them as online. I tried adding the following line to the beginning of the global in order to run the whos_online sub routine:

Code:

&GForum::Online::whos_online;

It seems to work some of the time, but other times I get an error that brings the site down. Any help would be greatly appreciated.

Sean
Quote Reply
Re: [SeanP] Who's Online Global In reply to
Try adding this near the beginning of your global:

$DB->table('Online')->delete(GT::SQL::Condition->new(online_time => '<' => (time - $CFG->{online_timeout} * 60)));

That'll purge the Online table of any expired entries.

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com
Quote Reply
Re: [Jagerman] Who's Online Global In reply to
That works great! Thanks!