--- loncom/interface/Attic/lonspreadsheet.pm 2001/03/12 18:27:46 1.43
+++ loncom/interface/Attic/lonspreadsheet.pm 2001/03/20 20:11:27 1.47
@@ -4,7 +4,8 @@
# 11/11,11/15,11/27,12/04,12/05,12/06,12/07,
# 12/08,12/09,12/11,12/12,12/15,12/16,12/18,12/19,12/30,
# 01/01/01,02/01,03/01,19/01,20/01,22/01,
-# 03/05,03/08,03/10,03/12 Gerd Kortemeyer
+# 03/05,03/08,03/10,03/12,03/13,03/15,03/17,
+# 03/19,03/20 Gerd Kortemeyer
package Apache::lonspreadsheet;
@@ -18,6 +19,14 @@ use GDBM_File;
use HTML::TokeParser;
#
+# Caches for previously calculated spreadsheets
+#
+
+my %oldsheets;
+my %loadedcaches;
+my %expiredates;
+
+#
# Cache for stores of an individual user
#
@@ -1019,10 +1028,13 @@ sub updateclasssheet {
my $reply=&Apache::lonnet::reply('get:'.$sdom.':'.$sname.
':environment:firstname&middlename&lastname&generation',
&Apache::lonnet::homeserver($sname,$sdom));
- $rowlabel=$ssec.' '.$reply{$sname}.'
';
+ $rowlabel=''.
+ $ssec.' '.$reply{$sname}.'
';
map {
$rowlabel.=&Apache::lonnet::unescape($_).' ';
} split(/\&/,$reply);
+ $rowlabel.='';
}
$currentlist{&Apache::lonnet::unescape($name)}=$rowlabel;
}
@@ -1476,22 +1488,206 @@ sub loadrows {
}
}
+# ======================================================= Forced recalculation?
+
+sub checkthis {
+ my ($keyname,$time)=@_;
+ return ($time<$expiredates{$keyname});
+}
+sub forcedrecalc {
+ my ($uname,$udom,$stype,$usymb)=@_;
+ my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
+ my $time=$oldsheets{$key.'.time'};
+ unless ($time) { return 1; }
+ if ($stype eq 'assesscalc') {
+ my $map=(split(/\_\_\_/,$usymb))[0];
+ if (&checkthis('::assesscalc:',$time) ||
+ &checkthis('::assesscalc:'.$map,$time) ||
+ &checkthis('::assesscalc:'.$usymb,$time) ||
+ &checkthis($uname.':'.$udom.':assesscalc:',$time)) {
+ return 1;
+ }
+ } else {
+ if (&checkthis('::studentcalc:',$time) ||
+ &checkthis($uname.':'.$udom.':studencalc:',$time)) {
+ return 1;
+ }
+ }
+ return 0;
+}
+
# ============================================================== Export handler
#
# Non-interactive call from with program
#
sub exportsheet {
+ my ($uname,$udom,$stype,$usymb,$fn)=@_;
+ my @exportarr=();
+#
+# Check if cached
+#
+
+ my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
+ my $found='';
+
+ if ($oldsheets{$key}) {
+ map {
+ my ($name,$value)=split(/\_\_\_\=\_\_\_/,$_);
+ if ($name eq $fn) {
+ $found=$value;
+ }
+ } split(/\_\_\_\&\_\_\_/,$oldsheets{$key});
+ }
+
+ unless ($found) {
+ &cachedssheets($uname,$udom,&Apache::lonnet::homeserver($uname,$udom));
+ if ($oldsheets{$key}) {
+ map {
+ my ($name,$value)=split(/\_\_\_\=\_\_\_/,$_);
+ if ($name eq $fn) {
+ $found=$value;
+ }
+ } split(/\_\_\_\&\_\_\_/,$oldsheets{$key});
+ }
+ }
+#
+# Check if still valid
+#
+ if ($found) {
+ if (&forcedrecalc($uname,$udom,$stype,$usymb)) {
+ $found='';
+ }
+ }
- my ($uname,$udom,$stype,$usymb,$fn)=@_;
+ if ($found) {
+#
+# Return what was cached
+#
+ @exportarr=split(/\_\_\_\;\_\_\_/,$found);
+
+ } else {
+#
+# Not cached
+#
+
my $thissheet=&makenewsheet($uname,$udom,$stype,$usymb);
&readsheet($thissheet,$fn);
&updatesheet($thissheet);
&loadrows($thissheet);
- &calcsheet($thissheet);
- return &exportdata($thissheet);
+ &calcsheet($thissheet);
+ @exportarr=&exportdata($thissheet);
+#
+# Store now
+#
+ my $cid=$ENV{'request.course.id'};
+ my $current='';
+ if ($stype eq 'studentcalc') {
+ $current=&Apache::lonnet::reply('get:'.
+ $ENV{'course.'.$cid.'.domain'}.':'.
+ $ENV{'course.'.$cid.'.num'}.
+ ':nohist_calculatedsheets:'.
+ &Apache::lonnet::escape($key),
+ $ENV{'course.'.$cid.'.home'});
+ } else {
+ $current=&Apache::lonnet::reply('get:'.
+ &getudom($thissheet).':'.
+ &getuname($thissheet).
+ ':nohist_calculatedsheets_'.
+ $ENV{'request.course.id'}.':'.
+ &Apache::lonnet::escape($key),
+ &getuhome($thissheet));
+
+ }
+ my %currentlystored=();
+ unless ($current=~/^error\:/) {
+ map {
+ my ($name,$value)=split(/\_\_\_\=\_\_\_/,$_);
+ $currentlystored{$name}=$value;
+ } split(/\_\_\_\&\_\_\_/,&Apache::lonnet::unescape($current));
+ }
+ $currentlystored{$fn}=join('___;___',@exportarr);
+
+ my $newstore='';
+ map {
+ if ($newstore) { $newstore.='___&___'; }
+ $newstore.=$_.'___=___'.$currentlystored{$_};
+ } keys %currentlystored;
+ my $now=time;
+ if ($stype eq 'studentcalc') {
+ &Apache::lonnet::reply('put:'.
+ $ENV{'course.'.$cid.'.domain'}.':'.
+ $ENV{'course.'.$cid.'.num'}.
+ ':nohist_calculatedsheets:'.
+ &Apache::lonnet::escape($key).'='.
+ &Apache::lonnet::escape($newstore).'&'.
+ &Apache::lonnet::escape($key).'.time='.$now,
+ $ENV{'course.'.$cid.'.home'});
+ } else {
+ &Apache::lonnet::reply('put:'.
+ &getudom($thissheet).':'.
+ &getuname($thissheet).
+ ':nohist_calculatedsheets_'.
+ $ENV{'request.course.id'}.':'.
+ &Apache::lonnet::escape($key).'='.
+ &Apache::lonnet::escape($newstore).'&'.
+ &Apache::lonnet::escape($key).'.time='.$now,
+ &getuhome($thissheet));
+ }
+ }
+ return @exportarr;
+}
+
+# ===================================================== Calculated sheets cache
+#
+# Load previously cached student spreadsheets for this course
+#
+
+sub cachedcsheets {
+ my $cid=$ENV{'request.course.id'};
+ my $reply=&Apache::lonnet::reply('dump:'.
+ $ENV{'course.'.$cid.'.domain'}.':'.
+ $ENV{'course.'.$cid.'.num'}.
+ ':nohist_calculatedsheets',
+ $ENV{'course.'.$cid.'.home'});
+ unless ($reply=~/^error\:/) {
+ map {
+ my ($name,$value)=split(/\=/,$_);
+ $oldsheets{&Apache::lonnet::unescape($name)}
+ =&Apache::lonnet::unescape($value);
+ } split(/\&/,$reply);
+ }
}
+# ===================================================== Calculated sheets cache
+#
+# Load previously cached assessment spreadsheets for this student
+#
+
+sub cachedssheets {
+ my ($sname,$sdom,$shome)=@_;
+ unless (($loadedcaches{$sname.'_'.$sdom}) || ($shome eq 'no_host')) {
+ my $cid=$ENV{'request.course.id'};
+ my $reply=&Apache::lonnet::reply('dump:'.$sdom.':'.$sname.
+ ':nohist_calculatedsheets_'.
+ $ENV{'request.course.id'},
+ $shome);
+ unless ($reply=~/^error\:/) {
+ map {
+ my ($name,$value)=split(/\=/,$_);
+ $oldsheets{&Apache::lonnet::unescape($name)}
+ =&Apache::lonnet::unescape($value);
+ } split(/\&/,$reply);
+ }
+ $loadedcaches{$sname.'_'.$sdom}=1;
+ }
+}
+
+# ===================================================== Calculated sheets cache
+#
+# Load previously cached assessment spreadsheets for this student
+#
+
# ================================================================ Main handler
#
# Interactive call to screen
@@ -1660,10 +1856,25 @@ ENDSCRIPT
$r->print(&hiddenfield('ufn',&getfilename($asheet)));
+# --------------------------------------------------------------- Cached sheets
+
+ undef %oldsheets;
+ undef %loadedcaches;
+
+ if (&gettype($asheet) eq 'classcalc') {
+ $r->print("Loading previously calculated student sheets ...
\n");
+ $r->rflush();
+ &cachedcsheets();
+ } elsif (&gettype($asheet) eq 'studentcalc') {
+ $r->print("Loading previously calculated assessment sheets ...
\n");
+ $r->rflush();
+ &cachedssheets(&getuname($asheet),&getudom($asheet),
+ &getuhome($asheet));
+ }
# ----------------------------------------------------- Update sheet, load rows
- $r->print("Loaded sheet, updating rows ...
\n");
+ $r->print("Loaded sheet(s), updating rows ...
\n");
$r->rflush();
&updatesheet($asheet);
@@ -1695,19 +1906,3 @@ ENDSCRIPT
1;
__END__
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-