#!/usr/bin/perl
$|=1;
# The LearningOnline Network with CAPA
# User Status
# (Versions
# (Running loncron
# 09/06/01 Gerd Kortemeyer)
# 02/18/02,02/19/02 Gerd Kortemeyer)

use lib '/home/httpd/lib/perl/';
use LONCAPA::Configuration;

use HTTP::Headers;
use IO::File;

 
print "Content-type: text/html\n\n";
      
# -------------------- Read loncapa.conf (and by default, loncapa_apache.conf).
my $perlvarref=LONCAPA::Configuration::read_conf('loncapa.conf');
my %perlvar=%{$perlvarref};
undef $perlvarref; # remove since sensitive and not needed
delete $perlvar{'lonReceipt'}; # remove since sensitive and not needed
delete $perlvar{'lonSqlAccess'}; # remove since sensitive and not needed

my $oneline=($ENV{'QUERY_STRING'} eq 'simple');
unless ($oneline) { print "<html><body bgcolor=#FFFFFF>\n<h1>User Status ".localtime()."</h1>"; }

my $filename;
opendir(DIR,$perlvar{'lonIDsDir'});
%usercounts=();
while ($filename=readdir(DIR)) {
    unless ($filename=~/^\./) {
        my ($dev,$ino,$mode,$nlink,
            $uid,$gid,$rdev,$size,
            $atime,$mtime,$ctime,
            $blksize,$blocks)=stat($perlvar{'lonIDsDir'}.'/'.$filename);
        $now=time;
        $since=$now-$mtime;
        $sinceacc=$now-$atime;
	unless ($oneline) { print ("\n\n<hr />"); }
        my %userinfo=();
        undef $userinfo;
        my $fh=IO::File->new($perlvar{'lonIDsDir'}.'/'.$filename);
        while ($line=<$fh>) {
            chomp($line);
            my ($name,$value)=split(/\=/,$line);
            $userinfo{$name}=$value;
        }
        $fh->close();
        $color="#000000";
        $userclass="Active";
        if ($since>300) { $color="#222222"; }
        if ($since>600) { $color="#444444"; }
        if ($since>3600) { $color="#666666"; $userclass="Moderately Active"; }
        if ($since>7200) { $color="#888888"; }
        if ($since>21600) { $color="#AAAAAA"; $userclass="Inactive"; }
        $usercount{$userclass}++;
        $usercount{'in Domain '.$userinfo{'user.domain'}}++;
      unless ($oneline) {
        print '<font color="'.$color.'">';
        print '<h3>'.$userinfo{'environment.lastname'}.', '.
	       $userinfo{'environment.firstname'}.' '.
	       $userinfo{'environment.middlename'}.' '.
	       $userinfo{'environment.generation'}." (".
               $userinfo{'user.name'}."\@".$userinfo{'user.domain'}.
               ")</h3>\n<b>Login time:</b> ".
               localtime($userinfo{'user.login.time'}).
              ' <b>Browser</b>: '.$userinfo{'browser.type'}." <b>Client:</b> ".
               $userinfo{'request.host'}."<br />\n<b>Role: </b>".
               $userinfo{'request.role'}." ";
	if ($userinfo{'request.course.id'}) {
            print "<b>Course:</b> ".
          $userinfo{'course.'.$userinfo{'request.course.id'}.'.description'}.
                ' ('.$userinfo{'request.course.id'}.')';
            $usercount{'in Course '.
   $userinfo{'course.'.$userinfo{'request.course.id'}.'.description'}.
			   ' ('.$userinfo{'request.course.id'}.')'}++;
        } else {
	    print "Not in a course.";
        }
        print "<br /><b>Last Transaction:</b> ".localtime($mtime).
       " (".$since." secs ago) <br /><b>Last Access:</b> ".localtime($atime).
       " (".$sinceacc." secs ago)";
       print ("</font>"); 
     }      
    }
}
closedir(DIR);
unless ($oneline) { 
print "<hr /><h2>User Count</h2>";
foreach (sort keys %usercount) {
    print "<b>".$_.":</b> ".$usercount{$_}."<br />";
}
print "</body></html>";
} else {
foreach (sort keys %usercount) {
    print $_.'='.$usercount{$_}.'&';
}
}
