[LON-CAPA-cvs] cvs: loncom / lond /interface lontrackstudent.pm /localize lonlocal.pm /lonnet/perl lonnet.pm

raeburn raeburn@source.lon-capa.org
Fri, 02 Jan 2009 23:07:55 -0000


This is a MIME encoded message

--raeburn1230937675
Content-Type: text/plain

raeburn		Fri Jan  2 23:07:55 2009 EDT

  Modified files:              
    /loncom	lond 
    /loncom/lonnet/perl	lonnet.pm 
    /loncom/localize	lonlocal.pm 
    /loncom/interface	lontrackstudent.pm 
  Log:
  - When course activity log data are stored in $cnum_$cdom_activity tables, timestamps are stored using lonmysql::sqltime() which employs the server localtime.
  - Student Activity displayed with trackstudent needs to have these timestamps converted to the time zone set for the course.
     - &server_timezone_handler() in lond retrieves server time zone from /etc/sysconfig/clock or /etc/timezone depending on distro.
     - &get_server_timezone() in lonnet.pm gets server time zone from course's homeserver.
     - &gettimezone() in lonlocal.pm can now take a timezone as an argument.
         - will validate it, and return it if valid, 'local' otherwise
     - &convert_timezone() in lontrackstudent.pm converts timestamps prior to display, and includes course time zone.
  
  
--raeburn1230937675
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20090102230755.txt"

Index: loncom/lond
diff -u loncom/lond:1.409 loncom/lond:1.410
--- loncom/lond:1.409	Tue Oct  7 10:08:06 2008
+++ loncom/lond	Fri Jan  2 23:07:45 2009
@@ -2,7 +2,7 @@
 # The LearningOnline Network
 # lond "LON Daemon" Server (port "LOND" 5663)
 #
-# $Id: lond,v 1.409 2008/10/07 10:08:06 foxr Exp $
+# $Id: lond,v 1.410 2009/01/02 23:07:45 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -59,7 +59,7 @@
 my $status='';
 my $lastlog='';
 
-my $VERSION='$Revision: 1.409 $'; #' stupid emacs
+my $VERSION='$Revision: 1.410 $'; #' stupid emacs
 my $remoteVERSION;
 my $currenthostid="default";
 my $currentdomainid;
@@ -1587,6 +1587,38 @@
 }
 &register_handler("ls3", \&ls3_handler, 0, 1, 0);
 
+sub server_timezone_handler {
+    my ($cmd,$tail,$client) = @_;
+    my $userinput = "$cmd:$tail";
+    my $timezone;
+    my $clockfile = '/etc/sysconfig/clock'; # Fedora/CentOS/SuSE
+    my $tzfile = '/etc/timezone'; # Debian/Ubuntu
+    if (-e $clockfile) {
+        if (open(my $fh,"<$clockfile")) {
+            while (<$fh>) {
+                next if (/^[\#\s]/);
+                if (/^(?:TIME)?ZONE\s*=\s*['"]?\s*([\w\/]+)/) {
+                    $timezone = $1;
+                    last;
+                }
+            }
+            close($fh);
+        }
+    } elsif (-e $tzfile) {
+        if (open(my $fh,"<$tzfile")) {
+            $timezone = <$fh>;
+            close($fh);
+            chomp($timezone);
+            if ($timezone =~ m{^Etc/(\w+)$}) {
+                $timezone = $1;
+            }
+        }
+    }
+    &Reply($client,\$timezone,$userinput); # This supports debug logging.
+    return 1;
+}
+&register_handler("servertimezone", \&server_timezone_handler, 0, 1, 0);
+
 #   Process a reinit request.  Reinit requests that either
 #   lonc or lond be reinitialized so that an updated 
 #   host.tab or domain.tab can be processed.
Index: loncom/lonnet/perl/lonnet.pm
diff -u loncom/lonnet/perl/lonnet.pm:1.982 loncom/lonnet/perl/lonnet.pm:1.983
--- loncom/lonnet/perl/lonnet.pm:1.982	Fri Jan  2 22:45:43 2009
+++ loncom/lonnet/perl/lonnet.pm	Fri Jan  2 23:07:49 2009
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # TCP networking package
 #
-# $Id: lonnet.pm,v 1.982 2009/01/02 22:45:43 raeburn Exp $
+# $Id: lonnet.pm,v 1.983 2009/01/02 23:07:49 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -181,6 +181,20 @@
     return 0;
 }
 
+sub get_server_timezone {
+    my ($cnum,$cdom) = @_;
+    my $home=&homeserver($cnum,$cdom);
+    if ($home ne 'no_host') {
+        my $cachetime = 24*3600;
+        my ($timezone,$cached)=&is_cached_new('servertimezone',$home);
+        if (defined($cached)) {
+            return $timezone;
+        } else {
+            my $timezone = &reply('servertimezone',$home);
+            return &do_cache_new('servertimezone',$home,$timezone,$cachetime);
+        }
+    }
+}
 
 # -------------------------------------------------- Non-critical communication
 sub subreply {
Index: loncom/localize/lonlocal.pm
diff -u loncom/localize/lonlocal.pm:1.52 loncom/localize/lonlocal.pm:1.53
--- loncom/localize/lonlocal.pm:1.52	Fri Nov 28 14:36:55 2008
+++ loncom/localize/lonlocal.pm	Fri Jan  2 23:07:52 2009
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Localization routines
 #
-# $Id: lonlocal.pm,v 1.52 2008/11/28 14:36:55 raeburn Exp $
+# $Id: lonlocal.pm,v 1.53 2009/01/02 23:07:52 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -340,15 +340,24 @@
 
 # ========================================================== Localize localtime
 sub gettimezone {
-    my $timezone; 
-    if ($Apache::lonnet::env{'course.'.$Apache::lonnet::env{'request.course.id'}.'.timezone'}) {
-        $timezone = $Apache::lonnet::env{'course.'.$Apache::lonnet::env{'request.course.id'}.'.timezone'};    
-    } elsif ($Apache::lonnet::env{'request.course.id'} ne '') {
-        my $cdom = $Apache::lonnet::env{'course.'.$Apache::lonnet::env{'request.course.id'}.'.domain'};
-        if ($cdom ne '') {
-            my %domdefaults = &Apache::lonnet::get_domain_defaults($cdom);
-            if ($domdefaults{'timezone_def'} ne '') {
-                $timezone = $domdefaults{'timezone_def'};
+    my ($timezone) = @_;
+    if ($timezone ne '') {
+        if (!DateTime::TimeZone->is_valid_name($timezone)) {
+            $timezone = 'local';
+        }
+        return $timezone;
+    }
+    my $cid = $Apache::lonnet::env{'request.course.id'};  
+    if ($cid ne '') {
+        if ($Apache::lonnet::env{'course.'.$cid.'.timezone'}) {
+            $timezone = $Apache::lonnet::env{'course.'.$cid.'.timezone'};    
+        } else {
+            my $cdom = $Apache::lonnet::env{'course.'.$cid.'.domain'};
+            if ($cdom ne '') {
+                my %domdefaults = &Apache::lonnet::get_domain_defaults($cdom);
+                if ($domdefaults{'timezone_def'} ne '') {
+                    $timezone = $domdefaults{'timezone_def'};
+                }
             }
         }
     } elsif ($Apache::lonnet::env{'request.role.domain'} ne '') {
@@ -373,7 +382,7 @@
 }
 
 sub locallocaltime {
-    my $thistime=shift;
+    my ($thistime,$timezone) = @_;
     if (!defined($thistime) || $thistime eq '') {
 	return &mt('Never');
     }
@@ -387,7 +396,7 @@
     }
 
     my $dt = DateTime->from_epoch(epoch => $thistime)
-                     ->set_time_zone(&gettimezone());
+                     ->set_time_zone(&gettimezone($timezone));
     if ((&current_language=~/^en/) || (!$lh)) {
 
 	return $dt->strftime("%a %b %e %I:%M:%S %P %Y (%Z)");
Index: loncom/interface/lontrackstudent.pm
diff -u loncom/interface/lontrackstudent.pm:1.25 loncom/interface/lontrackstudent.pm:1.26
--- loncom/interface/lontrackstudent.pm:1.25	Sat Dec 20 04:31:55 2008
+++ loncom/interface/lontrackstudent.pm	Fri Jan  2 23:07:55 2009
@@ -1,6 +1,6 @@
 # The LearningOnline Network with CAPA
 #
-# $Id: lontrackstudent.pm,v 1.25 2008/12/20 04:31:55 raeburn Exp $
+# $Id: lontrackstudent.pm,v 1.26 2009/01/02 23:07:55 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -48,6 +48,7 @@
 use Apache::lonnet;
 use Apache::lonlocal;
 use Time::HiRes;
+use DateTime();
 use lib '/home/httpd/lib/perl/';
 use LONCAPA;
 
@@ -265,6 +266,7 @@
     if ($mode eq 'full_class') { 
         $tableheader = 
             '<table><tr>'.
+            '<th>&nbsp;</th>'.
             '<th>'.&mt('Resource').'</th>'.
             '<th>'.&mt('Time').'</th>'.
             '<th>'.&mt('Student').'</th>'.
@@ -275,6 +277,7 @@
     } elsif ($mode =~ /^student:(.*):(.*)$/) {
         $tableheader = 
             '<table><tr>'.
+            '<th>&nbsp;</th>'.
             '<th>'.&mt('Resource').'</th>'.
             '<th>'.&mt('Time').'</th>'.
             '<th>'.&mt('Action').'</th>'.
@@ -286,6 +289,17 @@
     $r->rflush();
     ##
     ##
+
+    my $cid = $env{'request.course.id'};
+    my $cnum = $env{'course.'.$cid.'.num'};
+    my $cdom = $env{'course.'.$cid.'.domain'};   
+    my $server_timezone = &Apache::lonnet::get_server_timezone($cnum,$cdom);
+    if ($server_timezone ne '') {
+        if (&Apache::lonlocal::gettimezone($server_timezone) eq 'local') {
+            $server_timezone = '';
+        }
+    }
+
     while (my $line = <ACTIVITYDATA>) {
         # FIXME: does not pass symbs along :(
         chomp($line);
@@ -367,6 +381,9 @@
                 '<a href="'.$src.'">'.$title.'</a>'.
                 '</span></td>';
         }
+        if ($server_timezone ne '') {
+            $timestamp = &convert_timezone($server_timezone,$timestamp);
+        }
         $tablerow .= '<td valign="top"><span class="LC_nobreak">'.$timestamp.'</span></td>';
         if ($mode eq 'full_class') {
             $tablerow.='<td valign="top">'.$student.'</td>';
@@ -383,6 +400,30 @@
     return $count;
 }
 
+sub convert_timezone {
+    my ($server_timezone,$timestamp) = @_;
+    if ($server_timezone && $timestamp) {
+        my ($date,$time) = split(/\s+/,$timestamp);
+        my ($year,$month,$day) = split(/\-/,$date);
+        my ($hour,$minute,$sec) = split(/:/,$time);
+        foreach ($month,$day,$hour,$minute,$sec) {
+            return $timestamp if $_ eq '';
+            $_ =~ s/^0//;
+        }
+        my $dt = DateTime->new(year      => $year,
+                               month     => $month,
+                               day       => $day,
+                               hour      => $hour,
+                               minute    => $minute,
+                               second    => $sec,
+                               time_zone => $server_timezone,
+                              );
+        my $unixtime = $dt->epoch;
+        $timestamp = &Apache::lonlocal::locallocaltime($unixtime);
+    }
+    return $timestamp;
+}
+
 ###################################################################
 ###################################################################
 sub display_values {

--raeburn1230937675--