online students

online students

by Dominik Kudyba -
Number of replies: 4

Hello !

Anyone knows how can I see who is online in my moodle website ?

Which mysql table contains a flag for this etc.

Is there a ready routine for this ?

Thanx

Average of ratings: -
In reply to Dominik Kudyba

Re: online students

by Przemyslaw Stencel -
AFAIK currently there's no way to tell who's online at the moment. You can have a look at the "Participants" page and check when your students last accessed the course site. If any of these times is in seconds, or just a few minutes, there's a chance (but chance only) that they might still be online.

I kind of vaguely remember someone proposing a paging feature, but I don't know if anyone is working on it.
In reply to Przemyslaw Stencel

Re: online students

by Zbigniew Fiedorowicz -

The requisite information is stored in the PHP session files. By parsing those, you can find all users whose sessions are considered to be active by Moodle (ie. users with the corresponding cookies can reenter Moodle without a new login).

Here is a quick Perl hack which does this:

#!/usr/bin/perl
#Replace /tmp/ in the line below by whatever directory PHP stores
#your session files in - see your php.ini file
my @files = glob("/tmp/sess_[0-9a-f]*");
my @fields;
my $matched;
print "Content-Type: text/plain\n\n";
foreach $file (@files) {
    open(SESSFILE,$file);
    while (<SESSFILE>) {
        $matched = 0;
        if (/username/) {$matched = 1;}
        @fields = split /;/;
        for ($i=0; $i<=$#fields;$i++) {
           if ($fields[$i]=~/username"$/) {
                $fields[$i+1]=~ /([^"]*)"$/;
                print "$1, ";
           }
           if ($fields[$i]=~/firstname"$/) {
                $fields[$i+1]=~ /([^"]*)"$/;
                print "$1 ";
           }
           if ($fields[$i]=~/lastname"$/) {
                $fields[$i+1]=~ /([^"]*)"$/;
                print "$1";
           }
       }
        if ($matched) {print "\n";}
    }
    close(SESSFILE);
}

To see this in action, visit https://webwork.math.ohio-state.edu/cgi-bin/webwork/system/cgi-scripts/listMoodleUsers.pl/

Note that this hack assumes that Moodle is the only PHP application running on the server. One would need to be a little more careful, if this is not the case.

In reply to Zbigniew Fiedorowicz

Here is the hack !

by Dominik Kudyba -

Hello !

 

Thank you very very much for your answers. Finally, I think that the best way is

- to find the more recent activities in the log table,

- to isolate the activities that are 0 to e.g. 10 minutes old and

- to find the authors of these activities

 

In practice : we search all the user.id with (now()-log.time)=600

 

I propose these 2 scripts :

// to put anywhere in lib/datalib.php (oli)

function  get_whosonline($off){
return  get_records_sql("SELECT DISTINCTROW moodle_user.id, moodle_user.firstname, moodle_user.lastname, moodle_user.picture FROM moodle_user LEFT JOIN moodle_log ON moodle_user.id = moodle_log.userid WHERE (1 AND (moodle_log.time > $off)) GROUP BY moodle_user.id LIMIT 0, 12 ");

}

<?php  // to put in /theme/header.html (oli)
$
now=time() ;
$off
=$now-600 ; //
if  ($users=get_whosonline($off)){
echo  "Who's online : <br><br>";
foreach  ($usersas$user){
echo  "<a href="$CFG->wwwroot/user/view.php?id=$user->id&course=1"".
"title="$user->firstname $user->lastname">"
;
echo  "<img border=0 src="$CFG->wwwroot/user/pix.php/$user->id/f2.jpg" ".
"width=35 height=35 alt="$user->firstname $user->lastname">"
;
echo  "</a>\n";
}
}
?>


Sorry for my bad English wink.gif

Average of ratings: Useful (1)
In reply to Zbigniew Fiedorowicz

Re: online students

by Zbigniew Fiedorowicz -

Attached below is an improved version of my Perl script for listing all active Moodle sessions on our server.

#!/usr/bin/perl
#Replace /tmp/ in the line below by whatever directory PHP stores
#your session files in - see your php.ini file
my @files = glob("/tmp/sess_[0-9a-f]*");
my @fields;
print "Content-Type: text/plain\n\n";
foreach $file (@files) {
    #If session last accessed more than 1/2 hour ago skip
    if ((-M $file)*24 > 0.5) {next;}
    open(SESSFILE,$file);
    while (<SESSFILE>) {
        #If session doesn't belong to Moodle skip
        if (!/moodle.*username.*firstname.*lastname/){next;}
        @fields = split /;/;
        for ($i=0; $i<=$#fields;$i++) {
           if ($fields[$i]=~/id\"$/) {
                $fields[$i+1]=~ /([^\"]*)\"$/;
                print "$1, ";
           }
           if ($fields[$i]=~/username\"$/) {
                $fields[$i+1]=~ /([^\"]*)\"$/;
                print "$1, ";
           }
           if ($fields[$i]=~/firstname\"$/) {
                $fields[$i+1]=~ /([^\"]*)\"$/;
                print "$1 ";
           }
           if ($fields[$i]=~/lastname\"$/) {
                $fields[$i+1]=~ /([^\"]*)\"$/;
                print "$1, ";
           }
           if ($fields[$i]=~/lastIP\"$/) {
                $fields[$i+1]=~ /([^\"]*)\"$/;
                print "$1";
           }
       }
       print "\n";
    }
    close(SESSFILE);
}