--- loncom/interface/Attic/lonspreadsheet.pm	2002/09/30 18:03:39	1.113
+++ loncom/interface/Attic/lonspreadsheet.pm	2002/10/18 13:46:01	1.118
@@ -1,5 +1,5 @@
 #
-# $Id: lonspreadsheet.pm,v 1.113 2002/09/30 18:03:39 matthew Exp $
+# $Id: lonspreadsheet.pm,v 1.118 2002/10/18 13:46:01 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -61,6 +61,7 @@ use Apache::Constants qw(:common :http);
 use GDBM_File;
 use HTML::TokeParser;
 use Apache::lonhtmlcommon;
+use Apache::loncoursedata;
 #
 # Caches for coursewide information 
 #
@@ -116,60 +117,9 @@ my $tmpdir;
 # =============================================================================
 # ===================================== Implements an instance of a spreadsheet
 
-sub initsheet {
-    my $safeeval = new Safe(shift);
-    my $safehole = new Safe::Hole;
-    $safeeval->permit("entereval");
-    $safeeval->permit(":base_math");
-    $safeeval->permit("sort");
-    $safeeval->deny(":base_io");
-    $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
-    $safeeval->share('$@');
-    my $code=<<'ENDDEFS';
-# ---------------------------------------------------- Inside of the safe space
-
-#
-# f: formulas
-# t: intermediate format (variable references expanded)
-# v: output values
-# c: preloaded constants (A-column)
-# rl: row label
-# os: other spreadsheets (for student spreadsheet only)
-
-undef %sheet_values; 
-undef %t;
-undef %f;
-undef %c;
-undef %rowlabel;
-undef @os;
-
-$maxrow = 0;
-$sheettype = '';
-
-# filename/reference of the sheet
-$filename = '';
-
-# user data
-$uname = '';
-$uhome = '';
-$udom  = '';
-
-# course data
-
-$csec = '';
-$chome= '';
-$cnum = '';
-$cdom = '';
-$cid  = '';
-$coursefilename  = '';
-
-# symb
-
-$usymb = '';
-
-# error messages
-$errormsg = '';
-
+##
+## mask - used to reside in the safe space.  
+##
 sub mask {
     my ($lower,$upper)=@_;
 
@@ -193,7 +143,6 @@ sub mask {
           $alpha='['.$la.'-Za-'.$ua.']';
        }
     }   
-
     if (($ld eq '*') || ($ud eq '*')) {
 	$num='\d+';
     } else {
@@ -213,7 +162,9 @@ sub mask {
        } else {
            my @lda=($ld=~m/\d/g);
            my @uda=($ud=~m/\d/g);
-           my $i; $j=0; $notdone=1;
+           my $i; 
+           my $j=0; 
+           my $notdone=1;
            for ($i=0;($i<=$#lda)&&($notdone);$i++) {
                if ($lda[$i]==$uda[$i]) {
 		   $num.=$lda[$i];
@@ -246,6 +197,65 @@ sub mask {
     return '^'.$alpha.$num."\$";
 }
 
+
+
+sub initsheet {
+    my $safeeval = new Safe(shift);
+    my $safehole = new Safe::Hole;
+    $safeeval->permit("entereval");
+    $safeeval->permit(":base_math");
+    $safeeval->permit("sort");
+    $safeeval->deny(":base_io");
+    $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
+    $safehole->wrap(\&Apache::lonspreadsheet::mask,$safeeval,'&mask');
+    $safehole->wrap(\&Apache::lonspreadsheet::templaterow,$safeeval,'&templaterow');
+    $safeeval->share('$@');
+    my $code=<<'ENDDEFS';
+# ---------------------------------------------------- Inside of the safe space
+
+#
+# f: formulas
+# t: intermediate format (variable references expanded)
+# v: output values
+# c: preloaded constants (A-column)
+# rl: row label
+# os: other spreadsheets (for student spreadsheet only)
+
+undef %sheet_values; 
+undef %t;
+undef %f;
+undef %c;
+undef %rowlabel;
+undef @os;
+
+$maxrow = 0;
+$sheettype = '';
+
+# filename/reference of the sheet
+$filename = '';
+
+# user data
+$uname = '';
+$uhome = '';
+$udom  = '';
+
+# course data
+
+$csec = '';
+$chome= '';
+$cnum = '';
+$cdom = '';
+$cid  = '';
+$coursefilename  = '';
+
+# symb
+
+$usymb = '';
+
+# error messages
+$errormsg = '';
+
+
 #-------------------------------------------------------
 
 =item UWCALC(hashname,modules,units,date) 
@@ -918,20 +928,6 @@ sub calc {
     return '';
 }
 
-sub templaterow {
-    my @cols=();
-    $cols[0]='<b><font size=+1>Template</font></b>';
-    foreach ('A','B','C','D','E','F','G','H','I','J','K','L','M',
-	     'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
-	     'a','b','c','d','e','f','g','h','i','j','k','l','m',
-	     'n','o','p','q','r','s','t','u','v','w','x','y','z') {
-        my $fm=$f{'template_'.$_};
-        $fm=~s/[\'\"]/\&\#34;/g;
-        push(@cols,"'template_$_','$fm'".'___eq___'.$fm);
-    }
-    return @cols;
-}
-
 #
 # This is actually used for the student spreadsheet, not the assessment sheet
 # Do not be fooled by the name!
@@ -999,175 +995,71 @@ sub exportrowa {
     return @exportarray;
 }
 
+sub templaterow {
+    my @cols=();
+    $cols[0]='<b><font size=+1>Template</font></b>';
+    foreach ('A','B','C','D','E','F','G','H','I','J','K','L','M',
+	     'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
+	     'a','b','c','d','e','f','g','h','i','j','k','l','m',
+	     'n','o','p','q','r','s','t','u','v','w','x','y','z') {
+        my $fm=$f{'template_'.$_};
+        $fm=~s/[\'\"]/\&\#34;/g;
+        push(@cols,"'template_$_','$fm'".'___eq___'.$fm);
+    }
+    return @cols;
+}
+
+
 # ------------------------------------------- End of "Inside of the safe space"
 ENDDEFS
     $safeeval->reval($code);
     return $safeeval;
 }
 
-# ------------------------------------------------ Add or change formula values
 
+# ------------------------------------------------ Add or change formula values
 sub setformulas {
     my ($safeeval,%f)=@_;
     %{$safeeval->varglob('f')}=%f;
 }
 
 # ------------------------------------------------ Add or change formula values
-
 sub setconstants {
     my ($safeeval,%c)=@_;
     %{$safeeval->varglob('c')}=%c;
 }
 
 # --------------------------------------------- Set names of other spreadsheets
-
 sub setothersheets {
     my ($safeeval,@os)=@_;
     @{$safeeval->varglob('os')}=@os;
 }
 
 # ------------------------------------------------ Add or change formula values
-
 sub setrowlabels {
     my ($safeeval,%rowlabel)=@_;
     %{$safeeval->varglob('rowlabel')}=%rowlabel;
 }
 
 # ------------------------------------------------------- Calculate spreadsheet
-
 sub calcsheet {
     my $safeeval=shift;
     return $safeeval->reval('&calc();');
 }
 
-# ------------------------------------------------------------------ Get values
-
-sub getvalues {
-    my $safeeval=shift;
-    return $safeeval->reval('%sheet_values');
-}
-
 # ---------------------------------------------------------------- Get formulas
-
 sub getformulas {
     my $safeeval=shift;
     return %{$safeeval->varglob('f')};
 }
 
 # ----------------------------------------------------- Get value of $f{'A'.$n}
-
 sub getfa {
     my ($safeeval,$n)=@_;
     return $safeeval->reval('$f{"A'.$n.'"}');
 }
 
-# -------------------------------------------------------------------- Get type
-
-sub gettype {
-    my $safeeval=shift;
-    return $safeeval->reval('$sheettype');
-}
-
-# ------------------------------------------------------------------ Set maxrow
-
-sub setmaxrow {
-    my ($safeeval,$row)=@_;
-    $safeeval->reval('$maxrow='.$row.';');
-}
-
-# ------------------------------------------------------------------ Get maxrow
-
-sub getmaxrow {
-    my $safeeval=shift;
-    return $safeeval->reval('$maxrow');
-}
-
-# ---------------------------------------------------------------- Set filename
-
-sub setfilename {
-    my ($safeeval,$fn)=@_;
-    $safeeval->reval('$filename="'.$fn.'";');
-}
-
-# ---------------------------------------------------------------- Get filename
-
-sub getfilename {
-    my $safeeval=shift;
-    return $safeeval->reval('$filename');
-}
-
-# --------------------------------------------------------------- Get course ID
-
-sub getcid {
-    my $safeeval=shift;
-    return $safeeval->reval('$cid');
-}
-
-# --------------------------------------------------------- Get course filename
-
-sub getcoursefilename {
-    my $safeeval=shift;
-    return $safeeval->reval('$coursefilename');
-}
-
-# ----------------------------------------------------------- Get course number
-
-sub getcnum {
-    my $safeeval=shift;
-    return $safeeval->reval('$cnum');
-}
-
-# ------------------------------------------------------------- Get course home
-
-sub getchome {
-    my $safeeval=shift;
-    return $safeeval->reval('$chome');
-}
-
-# ----------------------------------------------------------- Get course domain
-
-sub getcdom {
-    my $safeeval=shift;
-    return $safeeval->reval('$cdom');
-}
-
-# ---------------------------------------------------------- Get course section
-
-sub getcsec {
-    my $safeeval=shift;
-    return $safeeval->reval('$csec');
-}
-
-# --------------------------------------------------------------- Get user name
-
-sub getuname {
-    my $safeeval=shift;
-    return $safeeval->reval('$uname');
-}
-
-# ------------------------------------------------------------- Get user domain
-
-sub getudom {
-    my $safeeval=shift;
-    return $safeeval->reval('$udom');
-}
-
-# --------------------------------------------------------------- Get user home
-
-sub getuhome {
-    my $safeeval=shift;
-    return $safeeval->reval('$uhome');
-}
-
-# -------------------------------------------------------------------- Get symb
-
-sub getusymb {
-    my $safeeval=shift;
-    return $safeeval->reval('$usymb');
-}
-
 # ------------------------------------------------------------- Export of A-row
-
 sub exportdata {
     my $safeeval=shift;
     return $safeeval->reval('&exportrowa()');
@@ -1183,7 +1075,7 @@ sub exportdata {
 # --------------------------------------------- Produce output row n from sheet
 
 sub rown {
-    my ($safeeval,$n)=@_;
+    my ($safeeval,$n,$sheetdata)=@_;
     my $defaultbg;
     my $rowdata='';
     my $dataflag=0;
@@ -1200,7 +1092,7 @@ sub rown {
     my $showf=0;
     my $proc;
     my $maxred=1;
-    my $sheettype=&gettype($safeeval);
+    my $sheettype=$sheetdata->{'sheettype'};
     if ($sheettype eq 'studentcalc') {
         $proc='&outrowassess';
         $maxred=26;
@@ -1296,13 +1188,14 @@ sub outsheet {
             }
             $tabledata.="<b><font size=+1>$_</font></b></td>";
         }
-        $tabledata.='</tr>'.&rown($safeeval,'-').&rown($safeeval,0);
+        $tabledata.='</tr>'.&rown($safeeval,'-',$sheetdata).
+            &rown($safeeval,0,$sheetdata);
     }
     $r->print($tabledata);
     #
     # Prepare to output rows
     my $row;
-    my $maxrow=&getmaxrow($safeeval);
+    my $maxrow=$sheetdata->{'maxrow'};
     #
     my @sortby=();
     my @sortidx=();
@@ -1323,7 +1216,7 @@ sub outsheet {
     # Loop through the rows and output them one at a time
     my $n=0;
     for ($row=0;$row<$maxrow;$row++) {
-        my $thisrow=&rown($safeeval,$sortidx[$row]+1);
+        my $thisrow=&rown($safeeval,$sortidx[$row]+1,$sheetdata);
         if ($thisrow) {
             if (($n/25==int($n/25)) && (!$ENV{'form.showcsv'})) {
                 $r->print("</table>\n<br>\n");
@@ -1346,11 +1239,11 @@ sub outsheet {
 # ----------------------------------------------- Read list of available sheets
 # 
 sub othersheets {
-    my ($safeeval,$stype)=@_;
+    my ($safeeval,$stype,$sheetdata)=@_;
     #
-    my $cnum  = &getcnum($safeeval);
-    my $cdom  = &getcdom($safeeval);
-    my $chome = &getchome($safeeval);
+    my $cnum  = $sheetdata->{'cnum'};
+    my $cdom  = $sheetdata->{'cdom'};
+    my $chome = $sheetdata->{'chome'};
     #
     my @alternatives=();
     my %results=&Apache::lonnet::dump($stype.'_spreadsheets',$cdom,$cnum);
@@ -1420,7 +1313,7 @@ sub readsheet {
         }
     }
     # $fn now has a value
-    &setfilename($safeeval,$fn);
+    $sheetdata->{'filename'} = $fn;
     # see if sheet is cached
     my $fstring='';
     if ($fstring=$spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}) {
@@ -1480,6 +1373,8 @@ sub makenewsheet {
     $sheetdata{'uhome'} = &Apache::lonnet::homeserver($uname,$udom);
     
     my $safeeval=initsheet($stype);
+    #
+    # Place all the %sheetdata items into the safe space
     my $initstring = '';
     foreach (keys(%sheetdata)) {
         $initstring.= qq{\$$_="$sheetdata{$_}";};
@@ -1490,15 +1385,15 @@ sub makenewsheet {
 
 # ------------------------------------------------------------ Save spreadsheet
 sub writesheet {
-    my ($safeeval,$makedef)=@_;
-    my $cid=&getcid($safeeval);
+    my ($safeeval,$makedef,$sheetdata)=@_;
+    my $cid=$sheetdata->{'cid'};
     if (&Apache::lonnet::allowed('opa',$cid)) {
         my %f=&getformulas($safeeval);
-        my $stype=&gettype($safeeval);
-        my $cnum=&getcnum($safeeval);
-        my $cdom=&getcdom($safeeval);
-        my $chome=&getchome($safeeval);
-        my $fn=&getfilename($safeeval);
+        my $stype= $sheetdata->{'sheettype'};
+        my $cnum = $sheetdata->{'cnum'};
+        my $cdom = $sheetdata->{'cdom'};
+        my $chome= $sheetdata->{'chome'};
+        my $fn   = $sheetdata->{'filename'};
         # Cache new sheet
         $spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}=join('___;___',%f);
         # Write sheet
@@ -1541,10 +1436,10 @@ sub writesheet {
 # "Modified workcopy" - interactive only
 #
 sub tmpwrite {
-    my $safeeval=shift;
+    my ($safeeval,$sheetdata) = @_;
     my $fn=$ENV{'user.name'}.'_'.
-        $ENV{'user.domain'}.'_spreadsheet_'.&getusymb($safeeval).'_'.
-           &getfilename($safeeval);
+        $ENV{'user.domain'}.'_spreadsheet_'.$sheetdata->{'usymb'}.'_'.
+           $sheetdata->{'filename'};
     $fn=~s/\W/\_/g;
     $fn=$tmpdir.$fn.'.tmp';
     my $fh;
@@ -1555,10 +1450,10 @@ sub tmpwrite {
 
 # ---------------------------------------------------------- Read the temp copy
 sub tmpread {
-    my ($safeeval,$nfield,$nform)=@_;
+    my ($safeeval,$sheetdata,$nfield,$nform)=@_;
     my $fn=$ENV{'user.name'}.'_'.
-           $ENV{'user.domain'}.'_spreadsheet_'.&getusymb($safeeval).'_'.
-           &getfilename($safeeval);
+           $ENV{'user.domain'}.'_spreadsheet_'.$sheetdata->{'usymb'}.'_'.
+           $sheetdata->{'filename'};
     $fn=~s/\W/\_/g;
     $fn=$tmpdir.$fn.'.tmp';
     my $fh;
@@ -1650,24 +1545,24 @@ sub parmval {
     my $courselevelr = $usercourseprefix.'.'.$symbparm;
     my $courselevelm = $usercourseprefix.'.'.$mapparm;
     # fourth, check user
-    if ($uname) { 
-        return $useropt{$courselevelr} if ($useropt{$courselevelr});
-        return $useropt{$courselevelm} if ($useropt{$courselevelm});
-        return $useropt{$courselevel}  if ($useropt{$courselevel});
+    if (defined($uname)) {
+        return $useropt{$courselevelr} if (defined($useropt{$courselevelr}));
+        return $useropt{$courselevelm} if (defined($useropt{$courselevelm}));
+        return $useropt{$courselevel}  if (defined($useropt{$courselevel}));
     }
     # third, check course
-    if ($csec) {
-        return $courseopt{$seclevelr} if ($courseopt{$seclevelr});
-        return $courseopt{$seclevelm} if ($courseopt{$seclevelm});
-        return $courseopt{$seclevel}  if ($courseopt{$seclevel});
+    if (defined($csec)) {
+        return $courseopt{$seclevelr} if (defined($courseopt{$seclevelr}));
+        return $courseopt{$seclevelm} if (defined($courseopt{$seclevelm}));
+        return $courseopt{$seclevel}  if (defined($courseopt{$seclevel}));
     }
     #
-    return $courseopt{$courselevelr} if ($courseopt{$courselevelr});
-    return $courseopt{$courselevelm} if ($courseopt{$courselevelm});
-    return $courseopt{$courselevel}  if ($courseopt{$courselevel});
+    return $courseopt{$courselevelr} if (defined($courseopt{$courselevelr}));
+    return $courseopt{$courselevelm} if (defined($courseopt{$courselevelm}));
+    return $courseopt{$courselevel}  if (defined($courseopt{$courselevel}));
     # second, check map parms
     my $thisparm = $parmhash{$symbparm};
-    return $thisparm if ($thisparm);
+    return $thisparm if (defined($thisparm));
     # first, check default
     return &Apache::lonnet::metadata($fn,$rwhat.'.default');
 }
@@ -1684,66 +1579,29 @@ sub updateclasssheet {
 
     #
     # Read class list and row labels
-    my %classlist;
-    my @tmp = &Apache::lonnet::dump('classlist',$cdom,$cnum);
-    if ($tmp[0] !~ /^error/) {
-        %classlist = @tmp;
-    } else {
-        return 'Could not access course data';
-    }
-    undef @tmp;
+    my $classlist = &Apache::loncoursedata::get_classlist();
+    if (! defined($classlist)) {
+        return 'Could not access course classlist';
+    } 
     #
     my %currentlist=();
-    my $now=time;
-    foreach my $student (keys(%classlist)) {
-        my ($end,$start)=split(/\:/,$classlist{$student});
-        my $active=1;
-        $active = 0 if (($end) && ($now>$end));
-        $active = 1 if ($ENV{'form.Status'} eq 'Any');
-        $active = !$active if ($ENV{'form.Status'} eq 'Expired');
-        if ($active) {
+    foreach my $student (keys(%$classlist)) {
+        my ($studentDomain,$studentName,$end,$start,$id,$studentSection,
+            $fullname,$status)   =   @{$classlist->{$student}};
+        if ($ENV{'form.Status'} eq $status || $ENV{'form.Status'} eq 'Any') {
             my $rowlabel='';
-            my ($studentName,$studentDomain)=split(/\:/,$student);
-            my $studentSection=&Apache::lonnet::usection($studentDomain,
-                                                         $studentName,$cid);
-            $Section{$studentName.':'.$studentDomain} = $studentSection;
-#            if ($studentSection==-1) {
-#                unless ($ENV{'form.showcsv'}) {
-#                    $rowlabel='<font color=red>Data not available: '.
-#                        $studentName.'</font>';
-#                } else {
-#                    $rowlabel='ERROR","'.$studentName.
-#                        '","Data not available","","","';
-#                }
-#            } else {
-                my %reply=&Apache::lonnet::idrget($studentDomain,$studentName);
-                my %studentInformation=&Apache::lonnet::get
-                    ('environment',
-                     ['lastname','generation','firstname','middlename','id'],
-                     $studentDomain,$studentName);
-                if (! $ENV{'form.showcsv'}) {
-                    $rowlabel='<a href="/adm/studentcalc?uname='.$studentName.
-                        '&udom='.$studentDomain.'">'.
-                            $studentSection.'&nbsp;';
-                    foreach ('id','firstname','middlename',
-                             'lastname','generation'){
-                        $rowlabel.=$studentInformation{$_}."&nbsp;";
-                    }
-                    $rowlabel.='</a>';
-                } else {
-                    $rowlabel= '"'.join('","',
-                                        ($studentSection,
-                                         $studentInformation{'id'},
-                                         $studentInformation{'firstname'},
-                                         $studentInformation{'middlename'},
-                                         $studentInformation{'lastname'},
-                                         $studentInformation{'generation'})
-                                        ).'"';
-                }
- #           }
+            if ($ENV{'form.showcsv'}) {
+                $rowlabel= '"'.join('","',($studentName,$studentDomain,
+                                           $fullname,$studentSection,$id).'"');
+            } else {
+                $rowlabel='<a href="/adm/studentcalc?uname='.$studentName.
+                    '&udom='.$studentDomain.'">';
+                $rowlabel.=$studentSection.'&nbsp;'.$id."&nbsp;".$fullname;
+                $rowlabel.='</a>';
+            }
             $currentlist{$student}=$rowlabel;
-        } # end of if ($active)
-    } # end of foreach my $student (keys(%classlist))
+        }
+    }
     #
     # Find discrepancies between the course row table and this
     #
@@ -1776,7 +1634,7 @@ sub updateclasssheet {
     }
     if ($changed) { &setformulas($safeeval,%f); }
     #
-    &setmaxrow($safeeval,$maxrow);
+    $sheetdata->{'maxrow'} = $maxrow;
     &setrowlabels($safeeval,%currentlist);
 }
 
@@ -1897,7 +1755,7 @@ sub updatestudentassesssheet {
         }
     }
     if ($changed) { &setformulas($safeeval,%f); }
-    &setmaxrow($safeeval,$maxrow);
+    $sheetdata->{'maxrow'} = $maxrow;
     &setrowlabels($safeeval,%current);
     #
     undef %current;
@@ -2062,10 +1920,9 @@ sub loadassessment {
     # This is dumb.  It is also necessary :(
     my @oldkeys=keys %returnhash;
     #
-    foreach (@oldkeys) {
-        my $name=$_;
-        my $value=$returnhash{$_};
-        delete $returnhash{$_};
+    foreach my $name (@oldkeys) {
+        my $value=$returnhash{$name};
+        delete $returnhash{$name};
         $name=~s/\_/\./g;
         $returnhash{$name}=$value;
     }
@@ -2074,35 +1931,28 @@ sub loadassessment {
     undef %useropt;
 
     my $userprefix=$uname.'_'.$udom.'_';
-    
+
     unless ($uhome eq 'no_host') { 
         # Get coursedata
         unless ((time-$courserdatas{$cid.'.last_cache'})<240) {
-            my $reply=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.
-                                             ':resourcedata',$chome);
-            if ($reply!~/^error\:/) {
-                $courserdatas{$cid}=$reply;
-                $courserdatas{$cid.'.last_cache'}=time;
-            }
+            my %Tmp = &Apache::lonnet::dump('resourcedata',$cdom,$cnum);
+            $courserdatas{$cid}=\%Tmp;
+            $courserdatas{$cid.'.last_cache'}=time;
         }
-        foreach (split(/\&/,$courserdatas{$cid})) {
-            my ($name,$value)=split(/\=/,$_);
-            $courseopt{$userprefix.&Apache::lonnet::unescape($name)}=
-                &Apache::lonnet::unescape($value);  
+        while (my ($name,$value) = each(%{$courserdatas{$cid}})) {
+            $courseopt{$userprefix.$name}=$value;
         }
         # Get userdata (if present)
-        unless ((time-$userrdatas{$uname.'___'.$udom.'.last_cache'})<240) {
-            my $reply=
-                &Apache::lonnet::reply('dump:'.$udom.':'.$uname.':resourcedata',$uhome);
-            if ($reply!~/^error\:/) {
-                $userrdatas{$uname.'___'.$udom}=$reply;
-                $userrdatas{$uname.'___'.$udom.'.last_cache'}=time;
-            }
+        unless ((time-$userrdatas{$uname.'@'.$udom.'.last_cache'})<240) {
+            my %Tmp = &Apache::lonnet::dump('resourcedata',$udom,$uname);
+            $userrdatas{$cid} = \%Tmp;
+            # Most of the time the user does not have a 'resourcedata.db' 
+            # file.  We need to cache that we got nothing instead of bothering
+            # with requesting it every time.
+            $userrdatas{$uname.'@'.$udom.'.last_cache'}=time;
         }
-        foreach (split(/\&/,$userrdatas{$uname.'___'.$udom})) {
-            my ($name,$value)=split(/\=/,$_);
-            $useropt{$userprefix.&Apache::lonnet::unescape($name)}=
-                &Apache::lonnet::unescape($value);
+        while (my ($name,$value) = each(%{$userrdatas{$cid}})) {
+            $useropt{$userprefix.$name}=$value;
         }
     }
     # now courseopt, useropt initialized for this user and course
@@ -2120,7 +1970,7 @@ sub loadassessment {
     #
     my %c=();
     if (tie(%parmhash,'GDBM_File',
-            &getcoursefilename($safeeval).'_parms.db',&GDBM_READER(),0640)) {
+            $sheetdata->{'coursefilename'}.'_parms.db',&GDBM_READER(),0640)) {
         my %f=&getformulas($safeeval);
         foreach (keys(%f))  {
             next if ($_!~/^A/);
@@ -2289,8 +2139,8 @@ sub exportsheet {
         &readsheet($thissheet,$sheetdata,$fn);
         &updatesheet($thissheet,$sheetdata);
         &loadrows($thissheet,$sheetdata);
-        &calcsheet($thissheet,$sheetdata); 
-        @exportarr=&exportdata($thissheet,$sheetdata);
+        &calcsheet($thissheet); 
+        @exportarr=&exportdata($thissheet);
         #
         # Store now
         #
@@ -2429,12 +2279,17 @@ sub cachedssheets {
 sub handler {
     my $r=shift;
 
+    if (! exists($ENV{'form.Status'})) {
+        $ENV{'form.Status'} = 'Active';
+    }
+    # Check this server
     my $loaderror=&Apache::lonnet::overloaderror($r);
     if ($loaderror) { return $loaderror; }
+    # Check the course homeserver
     $loaderror= &Apache::lonnet::overloaderror($r,
                       $ENV{'course.'.$ENV{'request.course.id'}.'.home'});
     if ($loaderror) { return $loaderror; } 
-
+    
     if ($r->header_only) {
         $r->content_type('text/html');
         $r->send_http_header;
@@ -2531,7 +2386,7 @@ ENDSCRIPT
     }
     # Read new sheet or modified worksheet
     $r->uri=~/\/(\w+)$/;
-    my ($asheet,$asheetdata)=&makenewsheet($aname,$adom,$1,$ENV{'form.usymb'});
+    my ($sheet,$sheetdata)=&makenewsheet($aname,$adom,$1,$ENV{'form.usymb'});
     #
     # If a new formula had been entered, go from work copy
     if ($ENV{'form.unewfield'}) {
@@ -2539,24 +2394,20 @@ ENDSCRIPT
         $ENV{'form.unewformula'}=~s/\'/\"/g;
         $r->print('<p>New formula: '.$ENV{'form.unewfield'}.'='.
                   $ENV{'form.unewformula'}.'<p>');
-        &setfilename($asheet,$ENV{'form.ufn'});
-        &tmpread($asheet,$ENV{'form.unewfield'},$ENV{'form.unewformula'});
+        $sheetdata->{'filename'} = $ENV{'form.ufn'};
+        &tmpread($sheet,$sheetdata,
+                 $ENV{'form.unewfield'},$ENV{'form.unewformula'});
     } elsif ($ENV{'form.saveas'}) {
-        &setfilename($asheet,$ENV{'form.ufn'});
-        &tmpread($asheet);
+        $sheetdata->{'filename'} = $ENV{'form.ufn'};
+        &tmpread($sheet,$sheetdata);
     } else {
-        &readsheet($asheet,$asheetdata,$ENV{'form.ufn'});
+        &readsheet($sheet,$sheetdata,$ENV{'form.ufn'});
     }
     # Print out user information
-    unless ($asheetdata->{'sheettype'} eq 'classcalc') {
-        $r->print('<p><b>User:</b> '.$asheetdata->{'uname'}.
-                  '<br><b>Domain:</b> '.$asheetdata->{'udom'});
-#        if (&getcsec($asheet) eq '-1') {
-#            $r->print('<h3><font color=red>'.
-#                      'Not a student in this course</font></h3>');
-#        } else {
-            $r->print('<br><b>Section/Group:</b> '.$asheetdata->{'csec'});
-#        }
+    unless ($sheetdata->{'sheettype'} eq 'classcalc') {
+        $r->print('<p><b>User:</b> '.$sheetdata->{'uname'}.
+                  '<br><b>Domain:</b> '.$sheetdata->{'udom'});
+        $r->print('<br><b>Section/Group:</b> '.$sheetdata->{'csec'});
         if ($ENV{'form.usymb'}) {
             $r->print('<br><b>Assessment:</b> <tt>'.
                       $ENV{'form.usymb'}.'</tt>');
@@ -2564,10 +2415,10 @@ ENDSCRIPT
     }
     #
     # Check user permissions
-    if (($asheetdata->{'sheettype'} eq 'classcalc'       ) || 
-        ($asheetdata->{'uname'}     ne $ENV{'user.name'} ) ||
-        ($asheetdata->{'udom'}      ne $ENV{'user.domain'})) {
-        unless (&Apache::lonnet::allowed('vgr',$asheetdata->{'cid'})) {
+    if (($sheetdata->{'sheettype'} eq 'classcalc'       ) || 
+        ($sheetdata->{'uname'}     ne $ENV{'user.name'} ) ||
+        ($sheetdata->{'udom'}      ne $ENV{'user.domain'})) {
+        unless (&Apache::lonnet::allowed('vgr',$sheetdata->{'cid'})) {
             $r->print('<h1>Access Permission Denied</h1>'.
                       '</form></body></html>');
             return OK;
@@ -2577,15 +2428,15 @@ ENDSCRIPT
     $r->print('<br />'.
               '<input type="submit" name="forcerecalc" '.
               'value="Completely Recalculate Sheet"><p>');
-    if ($asheetdata->{'sheettype'} eq 'assesscalc') {
+    if ($sheetdata->{'sheettype'} eq 'assesscalc') {
         $r->print('<p><font size=+2>'.
                   '<a href="/adm/studentcalc?'.
-                  'uname='.$asheetdata->{'uname'}.
-                  '&udom='.$asheetdata->{'udom'}.'">'.
+                  'uname='.$sheetdata->{'uname'}.
+                  '&udom='.$sheetdata->{'udom'}.'">'.
                   'Level up: Student Sheet</a></font><p>');
     }
-    if (($asheetdata->{'sheettype'} eq 'studentcalc') && 
-        (&Apache::lonnet::allowed('vgr',$asheetdata->{'cid'}))) {
+    if (($sheetdata->{'sheettype'} eq 'studentcalc') && 
+        (&Apache::lonnet::allowed('vgr',$sheetdata->{'cid'}))) {
         $r->print ('<p><font size=+2><a href="/adm/classcalc">'.
                    'Level up: Course Sheet</a></font><p>');
     }
@@ -2598,13 +2449,13 @@ ENDSCRIPT
                   '<input type=text size=20 name=newfn value="'.$fname.'">'.
                   'make default: <input type=checkbox name="makedefufn"><p>');
     }
-    $r->print(&hiddenfield('ufn',&getfilename($asheet)));
+    $r->print(&hiddenfield('ufn',$sheetdata->{'filename'}));
     # Load dialog
     if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
         $r->print('<p><input type=submit name=load value="Load ...">'.
                   '<select name="loadthissheet">'.
                   '<option name="default">Default</option>');
-        foreach (&othersheets($asheet,$asheetdata->{'sheettype'})) {
+        foreach (&othersheets($sheet,$sheetdata->{'sheettype'},$sheetdata)) {
             $r->print('<option name="'.$_.'"');
             if ($ENV{'form.ufn'} eq $_) {
                 $r->print(' selected');
@@ -2612,37 +2463,38 @@ ENDSCRIPT
             $r->print('>'.$_.'</option>');
         } 
         $r->print('</select><p>');
-        if ($asheetdata->{'sheettype'} eq 'studentcalc') {
-            &setothersheets($asheet,&othersheets($asheet,'assesscalc'));
+        if ($sheetdata->{'sheettype'} eq 'studentcalc') {
+            &setothersheets($sheet,
+                            &othersheets($sheet,'assesscalc',$sheetdata));
         }
     }
     # Cached sheets
     &expirationdates();
     undef %oldsheets;
     undef %loadedcaches;
-    if ($asheetdata->{'sheettype'} eq 'classcalc') {
+    if ($sheetdata->{'sheettype'} eq 'classcalc') {
         $r->print("Loading previously calculated student sheets ...\n");
         $r->rflush();
         &cachedcsheets();
-    } elsif ($asheetdata->{'sheettype'} eq 'studentcalc') {
+    } elsif ($sheetdata->{'sheettype'} eq 'studentcalc') {
         $r->print("Loading previously calculated assessment sheets ...\n");
         $r->rflush();
-        &cachedssheets($asheetdata->{'uname'},$asheetdata->{'udom'},
-                       $asheetdata->{'uhome'});
+        &cachedssheets($sheetdata->{'uname'},$sheetdata->{'udom'},
+                       $sheetdata->{'uhome'});
     }
     # Update sheet, load rows
     $r->print("Loaded sheet(s), updating rows ...<br>\n");
     $r->rflush();
     #
-    &updatesheet($asheet,$asheetdata);
+    &updatesheet($sheet,$sheetdata);
     $r->print("Updated rows, loading row data ...\n");
     $r->rflush();
     #
-    &loadrows($asheet,$asheetdata,$r);
+    &loadrows($sheet,$sheetdata,$r);
     $r->print("Loaded row data, calculating sheet ...<br>\n");
     $r->rflush();
     #
-    my $calcoutput=&calcsheet($asheet);
+    my $calcoutput=&calcsheet($sheet);
     $r->print('<h3><font color=red>'.$calcoutput.'</h3></font>');
     # See if something to save
     if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
@@ -2650,18 +2502,19 @@ ENDSCRIPT
         if ($ENV{'form.saveas'} && ($fname=$ENV{'form.newfn'})) {
             $fname=~s/\W/\_/g;
             if ($fname eq 'default') { $fname='course_default'; }
-            $fname.='_'.$asheetdata->{'sheettype'};
-            &setfilename($asheet,$fname);
+            $fname.='_'.$sheetdata->{'sheettype'};
+            $sheetdata->{'filename'} = $fname;
             $ENV{'form.ufn'}=$fname;
             $r->print('<p>Saving spreadsheet: '.
-                      &writesheet($asheet,$ENV{'form.makedefufn'}).'<p>');
+                      &writesheet($sheet,$ENV{'form.makedefufn'},$sheetdata).
+                      '<p>');
         }
     }
     #
-    #Write the modified worksheet
-    $r->print('<b>Current sheet:</b> '.&getfilename($asheet).'<p>');
-    &tmpwrite($asheet);
-    if ($asheetdata->{'sheettype'} eq 'studentcalc') {
+    # Write the modified worksheet
+    $r->print('<b>Current sheet:</b> '.$sheetdata->{'filename'}.'<p>');
+    &tmpwrite($sheet,$sheetdata);
+    if ($sheetdata->{'sheettype'} eq 'studentcalc') {
         $r->print('<br>Show rows with empty A column: ');
     } else {
         $r->print('<br>Show empty rows: ');
@@ -2684,7 +2537,7 @@ ENDSCRIPT
     $r->print('>');
     #
     # CSV format checkbox (classcalc sheets only)
-    if ($asheetdata->{'sheettype'} eq 'classcalc') {
+    if ($sheetdata->{'sheettype'} eq 'classcalc') {
         $r->print(' Output CSV format: <input type="checkbox" '.
                   'name="showcsv" onClick="submit()"');
         if ($ENV{'form.showcsv'}) { $r->print(' checked'); }
@@ -2703,7 +2556,7 @@ value='Insert Row Top'>
 value='Insert Row Bottom'><br>
 ENDINSERTBUTTONS
     # Print out sheet
-    &outsheet($r,$asheet,$asheetdata);
+    &outsheet($r,$sheet,$sheetdata);
     $r->print('</form></body></html>');
     #  Done
     return OK;