Who is online at your site - code is here

Who is online at your site - code is here

by Alan Chambers -
Number of replies: 54

Here is basic code for displaying who is currently active on your site within the last half hour (1800 secs). I placed it beneath the main menu on index.php.  I originally wanted to show if a particular user was online so that others would know that they could chat with them ( a VIP who has his chat open when logged into Moodle) at this time.

Now, how can I alert the other user that I would like to chat to them? Or guide them to a particular chat room within Moodle? Any other uses, tangents to pursue?

Average of ratings: Useful (1)
In reply to Alan Chambers

Re: Who is online at your site - code is here

by Gustav W Delius -

What I would find useful would be a People box also on the main Moodle home page. The "Participants" link in this box should lead to a side-wide participants list. This would then also allow you to see who was active on the site recently because by default the participants list is sorted by last access time.

In reply to Gustav W Delius

Re: Who is online at your site - code is here

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
Good idea!  This is realistic now there is proper paging/searching.

It should probably only be available to logged-in users, though.
In reply to Alan Chambers

Re: Who is online at your site - code is here

by Richard Watkins -
Cool, I added a iscreator() check so only course creators and admins can see this and modified the SQL to order the results to show most recent activity first.
In reply to Richard Watkins

Re: Who is online at your site - code is here

by Art Lader -
Will you be posting your version?
In reply to Art Lader

Re: Who is online at your site - code is here

by Richard Watkins -

Yes big grin

if (iscreator()) {   
  print "<b><u><FONT SIZE=\"-3\" COLOR=\"#0099CC\">Users Online</u>&nbsp &nbsp &nbsp &nbsp<u>Last action</u></b><br>";
  $query = "select id,lastaccess,firstname,lastname from mdl_user order by lastaccess desc";
  $result = mysql_db_query ("$CFG->dbname", $query);
  $numOfRows = mysql_num_rows ($result);
  for ($i = 0; $i < $numOfRows; $i++){
   $deano = mysql_result ($result, $i, "id");
   $lastaction = mysql_result ($result, $i, "lastaccess");
   $thetime = time();
   $name = mysql_result ($result, $i, "firstname");
   $last = mysql_result ($result, $i, "lastname"); 
         if ($lastaction > $thetime - 1800){
      global $CFG;
       print "<a href=\"$CFG->wwwroot/user/view.php?id=$deano&course=1\">$name $last</a> &nbsp (".format_time(time() - $lastaction).")&nbsp<br>";
      }else{
        
    }
  }
  print "<br></FONT>";
  
     }

Average of ratings: Useful (1)
In reply to Alan Chambers

Re: Who is online at your site - code is here

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
What's all this hardcoded SQL and mysql_ function stuff?    surprise   wink

$timenow = time();
$cutofftime = time() - 1800;
$users = get_records_select("user", "lastaccess > '$cutofftime'",
"lastaccess DESC", "id,lastaccess,firstname,lastname");

foreach ($users as $user) {
$fullname = fullname($user);
$timeago = format_time($timenow - $user->lastaccess);
echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&course=1\">$fullname</a> ($timeago)<br />";
}
(typed quickly here and untested)

Average of ratings: Useful (2)
In reply to Martin Dougiamas

Re: Who is online at your site - code is here

by Richard Watkins -

What's all this hardcoded SQL and MySQL function stuff?

In my defense, I was only working with what I had been given!!
Your code didn't seem to work in my 1.0.9 install, don't have to time to test it on my 1.1.1 install atm.

In reply to Richard Watkins

Re: Who is online at your site - code is here

by Williams Castillo -
Your 1.0.9 version should be missing only the fullname function... Replace it by

$user->firstname . ' ' . $user->lastname

...and voìla.

Cheers,
Will



In reply to Richard Watkins

Re: Who is online at your site - code is here

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
No worries, I was replying to Alan .. smile I was just taking the opportunity to show a little Moodle coding style with a nice short example, not as a one-upmanship thing - just a little education. smile

I've been planning a little display similar to this for every course, but the links will go to a little popup window containing an ICQ-like instant messenger. In there it will work a little like Ray's Dialogue module, and a little like the Chat module, with the added advantage that new messages will cause the same popup to come up on the recipient's window (through javascript in the header of every page). This is why I'm not very keen on making the Dialogue module part of the standard distribution in its current form (good as it is, Ray!).
In reply to Martin Dougiamas

Re: Who is online at your site - code is here

by Ger Tielemans -
Can't you rework Dialogue so it can fit? I think it is one of the best - after workshop - extensions for people who want to go to scaffolding, student self control, managing constructiv...wink
In reply to Martin Dougiamas

Re: Who is online at your site - code is here

by Alan Chambers -
I am not a programmer, but ideas person so thanks martin for the coding style and the instant popup on the recipients machine when hyperlink is clicked ("i want to communicate with you now") is what I was heading towards. Hope this is integrated asap with ability to include /or not as a feature. Two choices on how it is included - all online are listed - or only teacher/admin gets to view online users list . This helps with others privacy issues in schools etc.
In reply to Martin Dougiamas

Re: Who is online at your site - code is here

by Alan Chambers -

Martin

I have a real need for a popup to appear on the other users machine when a hyperlink (of their name) is clicked on in my browser. I only need at minimum an alert to say "i see you are online at the moment, go to (prearranged) chat link".

Best outcome, click on online user and a "private"chat is opened up in their browser, and yours, with you already entered into the chat.

If code for alert only is not toooo complex you might let me play with it.

regards Alan

In reply to Alan Chambers

Re: Who is online at your site - code is here

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
Sorry, Alan, the code for this doesn't exist yet ... it's just a plan.
In reply to Martin Dougiamas

Re: Who is online at your site - code is here

by Alan Chambers -
Code exists inside chat code but needs massaging. Have a friend working on a code with innovative approach.
In reply to Martin Dougiamas

Re: Who is online at your site - code is here

by Joseph Vargas -
Where would this code be placed so that it works?
In reply to Martin Dougiamas

Re: Who is online at your site - code is here

by Boyd Mitchell -

Hi, I am getting this error message with this ;GREAT script.  What does it mean? and How should I fix it?

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/html/moodlelci/index.php on line 73

Thanks

Boyd

In reply to Boyd Mitchell

Re: Who is online at your site - code is here

by Alan Chambers -

See post re: fullname function and version of moodle

Have noticed that if there has not been any activity for last 1800 secs then error appears where names are to be listed. Db has no valid entries at this point - corrects as soon as an activity is noted. Is your path correct as quoted?

In reply to Alan Chambers

Re: Who is online at your site - code is here

by Boyd Mitchell -

We have a lot of activity here... so I'm pretty sure the 1800 seconds isn't the issue.  But even so, if I would like to change it to 5 minutes (300) then I might see this problem.  Is there a way to correct that.

Yes my path is correct.

Thanks so much for responding.  I truely appreciate it.

Boyd

In reply to Martin Dougiamas

Re: Who is online at your site - code is here

by Nicolas Martignoni -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators
I've hacked this code, using the many suggestions of this thread:
/// Print whoisonline
    $moddata = array();
    $modicon = array();
    $timenow = time();
    $cutofftime = time() - 1800;
    $users = get_records_select("user", "lastaccess > '$cutofftime'", 
                                "lastaccess DESC", "id,lastaccess,firstname,lastname");
                                
    foreach ($users as $user) {
        $fullname = fullname($user);
        $timeago = format_time($timenow - $user->lastaccess);
        $moddata[]="<a title=""."Who's online..."."" href="../user/view.php?id=$user->id&course=1">$fullname ($timeago)";
        $modicon[]="<img src="$CFG->pixpath/i/user.gif" height=16 width=16 alt="">";
    }
    print_side_block("Who's online...", "", $moddata, $modicon);
I'll be great to have it added to the main Moodle source wink
In reply to Nicolas Martignoni

Re: Who is online at your site - code is here

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
I'm still skeptical of how useful this is to non-Admin users, but I will put it in Moodle 1.3, I promise.  Don't let me forget.  wink

There needs to be two new "Site settings" option to control it:

  Show "Recent users" to everyone
  Show "Recent users" to logged-in users
  Don't show Recent Users (default)

and

  How far back to show recent users:  1 hour / 30 minutes / 20 minutes / 10 minutes / 5 minutes / 1 minute
Average of ratings: Useful (1)
In reply to Martin Dougiamas

Re: Who is online at your site - code is here

by Jon Bolton -
Picture of Particularly helpful Moodlers Picture of Testers
It certainly helps teachers on my courses - quick way to find out if anyone is around when you're online without having to go to the logs. It would be useful ... please smile
In reply to Jon Bolton

Re: Who is online at your site - code is here

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
You don't need to go to the logs ... look at the participants pagesmile
In reply to Martin Dougiamas

Re: Who is online at your site - code is here

by Wendi Dunlap -
For one of my Moodle "courses" (I use quotes because it's not really a course!) this would be extremely useful for non-Admins. I'm using Moodle in a somewhat non-standard way, for a community that isn't a formal course (see http://slumberland.org/moodle/course/view.php?id=5 to see what I mean -- guests are welcome), and I find that most people don't seem to figure out that they can use the Participants page that way. Many of them haven't even got the hang of the Recent Activities menu, either... so having something to show people that there are others on the site would be good for us.
In reply to Martin Dougiamas

Re: Who is online at your site - code is here

by W Page -

Hi Martin,

Thank you for including this feature in the next Moodle release.

This feature will allow folks to connect on line especially when it comes to communication in the Forums, Chat and Dialogue.  It encourages spontaneous interaction as well.  And well, it is just fun.

There should also be a part of

  • the admin which allows the administrator to
    • customise this feature for their site along the line of what you mentioned above and
    • determine if a site members and/or teachers and/or other admins will be allowed  to opt to be visible online or not when on the site.
  • the users profile page which allows the site member, admin, teacher to opt to be visible when on line.

But, shouldn't you be resting.  I am glad you are feeling better.  But please take it slow and heal completely.  The folks have kept Moodle rolling, while awaiting for your return.

WP1

In reply to Martin Dougiamas

Re: Who is online at your site - code is here

by Ger Tielemans -
If you could limit the view to the members of the group or course you are working on on that moment, it could be useful in a S.C. way?
In reply to Martin Dougiamas

Re: Who is online at your site - code is here

by Junaid Haroon -
>> ... but I will put it in Moodle 1.3, I promise. Don't let
>> me forget.

Just reminding smile
In reply to Nicolas Martignoni

Re: Who is online at your site - code is here

by Bruno Vernier -
I modified it slightly to show avatars, only firstnames, and to use display blocks (bubbles) for time since last activity (less clutter)


///    print whoisonline
      $modicon = array();
      $moddata = array();
      $timenow = time();
      $cutofftime = time() - 36000; // last 10 hours
      $users = get_records_select("user", "lastaccess > '$cutofftime'","lastaccess DESC", "id,lastaccess,firstname,lastname,picture");
      foreach ($users as $user) {
              $fullname = $user->firstname ; //fullname($user);
              $timeago = format_time($timenow - $user->lastaccess);
              $moddata[]="<a title=\"$timeago\" href='$CFG->wwwroot/user/view.php?id=$user->id&course=1'>$fullname ";
              if ($user->picture==0) {
                  $modicon[]="<img src='$CFG->pixpath/i/user.gif' height=16 width=16 alt='NP'>";
              }   else{
                  $modicon[]="<img src='$CFG->wwwroot/user/pix.php/$user->id/f2.jpg' height=16 width=16 alt='NP'>";
              }
      }
      print_side_block("Who's online...", "", $moddata, $modicon);

Average of ratings: Useful (1)
In reply to Bruno Vernier

Re: Who is online at your site - code is here

by Jon Bolton -
Picture of Particularly helpful Moodlers Picture of Testers
Nice job Bruno - a lot tidier than it was. Like the idea of the avatars as well.

Erm blush ... how do I alter it to show firstname and lastname?
I have too many students called John and Ann to know which it is (and they haven't all updated their photos). I altered the line

$fullname = $user->firstname ; //fullname($user);

to read

$fullname = $user->firstname,lastname ; //fullname($user);

but got a parse error. My PHP coding is very rudimentary I'm afraid blush.

And also, couldn't find any display blocks (bubbles) for time since last activity - just got the avatar and name. Any clues?
In reply to Jon Bolton

Re: Who is online at your site - code is here

by Crafton Williams -
Hi Jon:


To display the fullname, try: $user->firstname $user->lastname.

Hope that helps.

Crafton

In reply to Crafton Williams

Re: Who is online at your site - code is here

by Wendi Dunlap -
It appears to be:

$fullname = fullname($user);

...it's actually commented out at the end of that same line.

In reply to Bruno Vernier

Re: Who is online at your site - code is here

by W Page -

Hi Bruno!

Thank you for "tweaking" the script.

The script appears to be working as far as placement on the site however all I get is an empty box if the user's image has been changed from the default.  The image is displayed in the users profile.

How can I correct this.

WP1

In reply to W Page

Re: Who is online at your site - code is here

by Bruno Vernier -
WP1, re: avatar showing up as empty box

try changing f2.jpg in the code to f1.jpg (that's all i can think of right now)


Jon: re: display block (bubbles)

i put the variable $timeago in the title attribute which (depending on CSS and browser version) should trigger a bubble when the mouse hovers for a second or two over a participant's name

we can also put a bit of javascript like
on_Mouse_Out='alert(\"$fullname was here $timeago ago\")'
(without the underscores) as an attribute of the Anchor element (for example right after <a in the source code

or  on_Click='alert(\"$fullname was here $timeago ago\")'
as an attribute of the <img element

In reply to Bruno Vernier

Re: Who is online at your site - code is here

by W Page -

Hi Bruno,

I made the change you suggested but the result was the same.  I am going to wait a few hours and then refresh the page again and see what happens.  The image in the profile and the profile page are present.  This only happen if the icon is changed from the default "smily face".  I just cannot figure this one out.

Image is below.

WP1

Attachment whoisonline_iconmissing.gif
In reply to Bruno Vernier

Re: Who is online at your site - code is here

by W Page -
Hello Bruno,

I have been thinking thoughtful thoughtful about this problem I have been having with the user pic in the "WhoIsOnline" script.

In the Site Configuration Page, I have the following settings initially.
GD - GD 1.x is installed (automatically determined by script)
Slash Argument - file.php/pic.jpg

Now when I upload an icon or pic, the icon (originally the yellow smiley) in the Edit Profile and User Profile pages disappears. The new uploaded icons do not display. I then have to revisit the Site Configuration Page and change to the following,
Slash Argument - file.php?=/pic.jpg

The new uploaded icons then appear on the Edit Profile and User Profile pages.

The icon appears in the "WhoIsOnline" script only if I do not change the icon from the original yellow smiley. Then I see the "user.gif" icon displayed. If I change the original smiley icon then the new uploaded icon does not display in the "WhoIsOnline" script. There is only an empty box.

When I check to see where the icons are uploading to, this is the path,
/moodledata/users/1/f1.jpg
/moodledata/users/1/f2.jpg

  • Is there something wrong with where the uploading icons are being uploaded to?
  • Is there something else I could do or something I am doing wrong?

Thanks in advance for any and all help you can give me with this.

WP1
In reply to Bruno Vernier

Re: Who is online at your site - code is here

by Wendi Dunlap -
Here's my tweaked version, which only shows up to non-Guests. Also, it has first names and surnames, and shows the last 5 minutes.



/// Tell logged in users who is online

if (!isguest()) {
$modicon = array();
$moddata = array();
$timenow = time();
$cutofftime = time() - 300; // last 5 minutes
$users = get_records_select("user", "lastaccess > '$cutofftime'","lastaccess DESC", "id,lastaccess,firstname,lastname,picture");
foreach ($users as $user) {
$fullname = fullname($user); //$user->firstname if you want the first name only
$timeago = format_time($timenow - $user->lastaccess);
$moddata[]="$fullname ";
if ($user->picture==0) {
$modicon[]="<img src='$CFG->pixpath/i/user.gif' height=16 width=16 alt='NP'>";
} else{
$modicon[]="<img src='$CFG->wwwroot/user/pix.php/$user->id/f2.jpg' height=16 width=16 alt='NP'>";
}
}
print_side_block("Who's here?", "", $moddata, $modicon);

}

/// end print whoisonline



I don't know php so even this slight alteration is an achievement. ;)
In reply to Bruno Vernier

Re: Who is online at your site - code is here

by Paul Norrod -
Partly for the feature and partly to learn more about Moodle/PHP, I want to modify this code to show only those currently online students who are enrolled in that particular course.  I know I need to modify this line:

$users = get_records_select("user", "lastaccess > '$cutofftime'","lastaccess DESC", "id,lastaccess,firstname,lastname,picture");

But I don't know how to include the "AND the student is in this course" condition in the "lastaccess > '$cutofftime'" selection criteria.

Also, where is the get_records_select function located in moodle?

Thanks in advance for any info!
In reply to Paul Norrod

Re: Who is online at your site - code is here

by Bruno Vernier -
Paul,

all of the moodle functions like get_records_select and get_records_sql are found in lib/datalib.php

extracting from my Cheatsheet:

function get_record($table, $field1, $value1, $field2="", $value2="", $field3="", $value3="")
function get_record_sql($sql)
function get_record_select($table, $select="", $fields="*")
function get_records($table, $field="", $value="", $sort="", $fields="*", $limitfrom="", $limitnum="")
function get_records_select($table, $select="", $sort="", $fields="*", $limitfrom="", $limitnum="")
function get_records_list($table, $field="", $values="", $sort="", $fields="*")
function get_records_sql($sql)
function get_records_menu($table, $field="", $value="", $sort="", $fields="*")
function get_records_select_menu($table, $select="", $sort="", $fields="*")
function get_records_sql_menu($sql)

In reply to Bruno Vernier

Re: Who is online at your site - code is here

by Paul Norrod -
Thanks Bruno!  The "cheatsheet" was just what I was looking for.  I can figure out how to do what I want to do now.
In reply to Bruno Vernier

How and where do I add this code? Do I need to change anything else?

by Norbert Berger -

Hi

I am very impressed. This is exactly what I have been looking for. In the past I got a grant for a year's subscription to LivePerson to offer on-demand chat or alternative email when I am not online (see http://bspr09.kfunigraz.ac.at) for virtual teacher support. On my trial site, http://bslso107.kfunigraz.ac.at/moodle/ I'd love to allow visitors to see who is online so chats or dialogues can be initiated.

However, I am having problems implementing it: When I copy the text/scripts here to format.php's, the pages show an error. Same in index.php. Do I need to change other settings, too?

As I am new to php and moodle, I apologise: it's probably something straightforward, and I am too ignorant to be able to sort this out myself. Help would be much appreciated.

Yours gratefully

Norbert

In reply to Alan Chambers

Re: Who is online at your site - code is here

by Tony Ruggiero -

Great idea!!!

Is it possible to do this class by class on the class (format) page?

Tony

In reply to Tony Ruggiero

Re: Who is online at your site - code is here

by Dave Ray -

My 2 cents worth;

This is a feature which perhaps deserves a pilot project

during which this feature is accessed only by selecting given

administrative  options.

------dave

In reply to Alan Chambers

Re: Who is online at your site - code is here

by Nicolas Martignoni -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators
I cannot manage to download this attachment. Can someone help me?
In reply to Nicolas Martignoni

Re: Who is online at your site - code is here

by Jon Bolton -
Picture of Particularly helpful Moodlers Picture of Testers
You need to add some code in your course/format/*/format.php file.

Mine reads as follows:

// Print whoisonline
print_side_block_start( "Who's online now...", 210,"");
$query = "select id,lastaccess,firstname,lastname from mdl_user";
$result = mysql_db_query ("$CFG->dbname", $query);
$numOfRows = mysql_num_rows ($result);
for ($i = 0; $i < $numOfRows; $i++){
$deano = mysql_result ($result, $i, "id");
$lastaction = mysql_result ($result, $i, "lastaccess");
$thetime = time();
$name = mysql_result ($result, $i, "firstname");
$last = mysql_result ($result, $i, "lastname");
      if ($lastaction > $thetime - 300){
global $CFG;
print "<img src=\"$CFG->pixpath/i/user.gif\" height=16 width=16 alt=\"\"><font size=\"-1\"><a href=\"$CFG->wwwroot/user/view.php?id=$deano&course=$course->id\">$name $last</a><br>";
}else{

}
}
print_side_block_end();

For me, this is added immediately before the line
/// Links to all activity modules by type

The resulting display is attached.
Hope this helps.
Attachment dundee.jpg
Average of ratings: Useful (2)
In reply to Jon Bolton

Re: Who is online at your site - code is here

by Wendi Dunlap -
Thank you! I just added this and it seems to work perfectly.
In reply to Wendi Dunlap

Re: Who is online at your site - code is here

by Wendi Dunlap -
But... now I wonder, how do I make it only visible to logged-in users?

Thanks in advance!
In reply to Jon Bolton

Re: Who is online at your site - code is here

by Fermí Cueva -

He aprovechado tu código para ampliarlo.

Ahora se puede avisar a otro usuario, pero aún falta mucho.

Fermin Cueva

In reply to Fermí Cueva

Re: Who is online at your site - code is here

by Eloy Lafuente (stronk7) -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Peer reviewers Picture of Plugin developers Picture of Testers
Hi moodlers,

after reading this discussion and talking with Fermin Cueva, I've create a new "block" to show online users. It is slightly different from your code posted above, because it works INSIDE courses (not at site level).

It's a very basic implementation (Fermin's code has some options to implement an "advise" system), but seems to work fine. To see more info about blocks and test the "Online Users" one, please, take a look to this: http://moodle.org/mod/forum/discuss.php?d=6740

Feedback appreciated, TIA and ciao smile
In reply to Eloy Lafuente (stronk7)

Re: Who is online at your site - code is here

by Tozé Loureiro -
I have checked your site and it is great! The installation of the "blocks" is very simple and this new "block concept" opens new "windows" sorriso for Moodle...

Just one (very) little detail, I can't find the code of the "online user block" mentioned above...adormecido
In reply to Tozé Loureiro

Re: Who is online at your site - code is here

by Bruno Vernier -
I could not find it either, so I modified my original code to fit in the new blocks format

this one shows all users across all courses


don't forget to insert into your BLOCKS table: (change the PREFIX to yours)

INSERT INTO `PREFIX_blocks` ( `id` , `name` , `valid` )
VALUES (
'10', 'whoisonline', '1'
);
In reply to Tozé Loureiro

Re: Who is online at your site - code is here

by Eloy Lafuente (stronk7) -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Peer reviewers Picture of Plugin developers Picture of Testers
Hi,

all the blocks code (being developed and NO FINAL!!) is under CVS in directory contrib/blocks.

Although I suppose you know it, I must say it isn't fully functional and important things are being decided yet. BE CAREFUL about installing (patching currently) all that stuff in your servers!!

Ciao smile
In reply to Eloy Lafuente (stronk7)

"Online Users": BUG OR MY ERROR?

by daniel ginerman -
I'm having the following problem (i'm in 1.43)
People who are online don't appear really in the "online users" block!
Any idea what can be happening with this?
Besides, I'd like to give my users the possibility of somehow sending instant messages to users that appear indeed as currently online. It will be ok even if it goes through the "dialogue" feature that works really very fine.
Does anybody have the solution?
Thanx to all!,

daniEl