[LON-CAPA-cvs] cvs: loncom / lonsql /lonnet/perl lonnet.pm

www lon-capa-cvs@mail.lon-capa.org
Tue, 18 Jun 2002 15:04:05 -0000


www		Tue Jun 18 11:04:05 2002 EDT

  Modified files:              
    /loncom	lonsql 
    /loncom/lonnet/perl	lonnet.pm 
  Log:
  Toward bug 121.
  lonnet now has routines to query course and user activity logs.
  lonsql userlog already does some stuff.
  Left to do: implement filters!
  BUGFIX: lonsql was not escaping the query result. A ":" in any of the fields
  would truncate the reply. lonsearchcat likely has to be adapted to unescape.
  
  
Index: loncom/lonsql
diff -u loncom/lonsql:1.45 loncom/lonsql:1.46
--- loncom/lonsql:1.45	Mon Jun 17 16:25:51 2002
+++ loncom/lonsql	Tue Jun 18 11:04:05 2002
@@ -3,7 +3,7 @@
 # The LearningOnline Network
 # lonsql - LON TCP-MySQL-Server Daemon for handling database requests.
 #
-# $Id: lonsql,v 1.45 2002/06/17 20:25:51 www Exp $
+# $Id: lonsql,v 1.46 2002/06/18 15:04:05 www Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -190,32 +190,52 @@
     print $fh "$local ($$): $message\n";
 }
 
-
-
-# -------------------------------------------- Return path to profile directory
-
-sub propath {
-    my ($udom,$uname)=@_;
-    $udom=~s/\W//g;
-    $uname=~s/\W//g;
-    my $subdir=$uname.'__';
-    $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
-    my $proname="$perlvar{'lonUsersDir'}/$udom/$subdir/$uname";
-    return $proname;
-} 
-
 # ------------------------------------------------------------------ Course log
 
 sub courselog {
     my ($path,$command)=@_;
-    return 'not_yet_implemented';
+    my %filters=();
+    foreach (split(/\=/,&unescape($command))) {
+	my ($name,$value)=split(/\=/,$_);
+        $filters{$name}=$value;
+    }
+    my @results=();
+    open(IN,$path.'/activity.log') or return ('file_error');
+    while ($line=<IN>) {
+        chomp($line);
+        my ($timestamp,$host,$log)=split(/\:/,$line);
+        foreach (split(/\&/,&unescape($log))) {
+	    my ($res,$uname,$udom,$action,$values)=split(/\:/,$_);
+            my $include=1;
+        }
+    }
+    close IN;
+    return join('&',sort(@results));
 }
 
 # -------------------------------------------------------------------- User log
 
 sub userlog {
     my ($path,$command)=@_;
-    return 'not_yet_implemented';
+    my %filters=();
+    foreach (split(/\=/,&unescape($command))) {
+	my ($name,$value)=split(/\=/,$_);
+        $filters{$name}=$value;
+    }
+    my @results=();
+    open(IN,$path.'/activity.log') or return ('file_error');
+    while ($line=<IN>) {
+        chomp($line);
+        my ($timestamp,$host,$log)=split(/\:/,$line);
+        $log=&unescape($log);
+        my $include=1;
+        if (($filters{'action'} eq 'log') && ($log!~/^Log/)) { $include=0; }
+        if ($include) {
+	   push(@results,$timestamp.':'.$log);
+        }
+    }
+    close IN;
+    return join('&',sort(@results));
 }
 
 
@@ -430,7 +450,12 @@
 		$result.=$customresult;
 	    }
 # ------------------------------------------------------------ end of sql query
-	  }
+	   }
+
+            # result does need to be escaped
+
+            $result=&escape($result);
+
 	    # reply with result, append \n unless already there
 
 	    $result.="\n" unless ($result=~/\n$/);
Index: loncom/lonnet/perl/lonnet.pm
diff -u loncom/lonnet/perl/lonnet.pm:1.239 loncom/lonnet/perl/lonnet.pm:1.240
--- loncom/lonnet/perl/lonnet.pm:1.239	Sat Jun 15 16:06:21 2002
+++ loncom/lonnet/perl/lonnet.pm	Tue Jun 18 11:04:05 2002
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # TCP networking package
 #
-# $Id: lonnet.pm,v 1.239 2002/06/15 20:06:21 www Exp $
+# $Id: lonnet.pm,v 1.240 2002/06/18 15:04:05 www Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1827,6 +1827,47 @@
 	}
     }
     return \%rhash;
+}
+
+# ----------------------------------------- Send log queries and wait for reply
+
+sub log_query {
+    my ($uname,$udom,$query,%filters)=@_;
+    my $uhome=&homeserver($uname,$udom);
+    if ($uhome eq 'no_host') { return 'error: no_host'; }
+    my $uhost=$hostname{$uhome};
+    my $command=&escape(join('&',map{$_.'='.$filters{$_}} keys %filters));
+    my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
+                       $uhome);
+    unless ($queryid=~/^$uhost\_/) { return 'error: '.$queryid; }
+    my $replyfile=$perlvar{'lonDaemons'}.'/tmp/'.$queryid;
+    my $reply='';
+    for (1..100) {
+	sleep 2;
+        &logthis('wait');
+        if (-e $replyfile.'.end') {
+	    if (my $fh=Apache::File->new($replyfile)) {
+               $reply.=<$fh>;
+               $fh->close;
+	   } else { return 'error: reply_file_error'; }
+        }
+        return &unescape($reply);
+    }
+    return 'error: timeout';
+}
+
+sub courselog_query {
+    my (%filters)=@_;
+    unless ($ENV{'request.course.id'}) { return 'no_course'; }
+    my $cname=$ENV{'course.'.$ENV{'request.course.id'}.'.num'};
+    my $cdom=$ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
+    return &log_query($cname,$cdom,'courselog',%filters);
+
+}
+
+sub userlog_query {
+    my ($uname,$udom,%filters)=@_;
+    return &log_query($uname,$udom,'userlog',%filters);
 }
 
 # ------------------------------------------------------------------ Plain Text