--- loncom/interface/Attic/lonspreadsheet.pm	2003/02/13 19:07:46	1.168
+++ loncom/interface/Attic/lonspreadsheet.pm	2003/05/27 19:57:51	1.180
@@ -1,5 +1,5 @@
 #
-# $Id: lonspreadsheet.pm,v 1.168 2003/02/13 19:07:46 matthew Exp $
+# $Id: lonspreadsheet.pm,v 1.180 2003/05/27 19:57:51 www Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -152,9 +152,9 @@ sub handler {
                                              $ENV{'form.ufn'} eq 'default')) {
         $ENV{'form.ufn'}='default_'.$1;
     }
-    if (!$ENV{'form.ufn'} || $ENV{'form.ufn'} eq 'default') {
-        $ENV{'form.ufn'}='course_default_'.$sheettype;
-    }
+#    if (!$ENV{'form.ufn'} || $ENV{'form.ufn'} eq 'default') {
+#        $ENV{'form.ufn'}='course_default_'.$sheettype;
+#    }
     #
     # Interactive loading of specific sheet?
     #
@@ -183,10 +183,7 @@ sub handler {
     # Header....
     #
     $r->print('<html><head><title>LON-CAPA Spreadsheet</title>');
-    my $nothing = "''";
-    if ($ENV{'browser.type'} eq 'explorer') {
-        $nothing = "'javascript:void(0);'";
-    }
+    my $nothing = &Apache::lonhtmlcommon::javascript_nothing();
 
     if ($ENV{'request.role'} !~ /^st\./) {
         $r->print(<<ENDSCRIPT);
@@ -262,7 +259,6 @@ ENDSCRIPT
     # Global directory configs
     #
     $sheet->includedir($r->dir_config('lonIncludes'));
-    $sheet->tmpdir($r->dir_config('lonDaemons').'/tmp/');
     #
     # Check user permissions
     if (($sheet->{'type'}  eq 'classcalc'       ) || 
@@ -356,6 +352,7 @@ ENDSCRIPT
         $oldsheets{'course'} ne $sheet->{'cid'}) {
         undef %oldsheets;
         undef %loadedcaches;
+        &Apache::lonspreadsheet::Spreadsheet::clear_package_variables();
     }
     $oldsheets{'course'} = $sheet->{'cid'};
     #
@@ -524,18 +521,12 @@ my %numbertimes;
 # Directories
 #
 my $includedir;
-my $tmpdir;
 
 sub includedir {
     my $self = shift;
     $includedir = shift;
 }
 
-sub tmpdir {
-    my $self = shift;
-    $tmpdir = shift;
-}
-
 my %spreadsheets;
 #my %loadedcaches;
 my %courserdatas;
@@ -544,6 +535,25 @@ my %defaultsheets;
 my %rowlabel_cache;
 #my %oldsheets;
 
+sub clear_package_variables {
+    undef %courseopt;
+    undef %useropt;
+    undef %parmhash;
+    undef %Section;
+    undef %expiredates;
+    undef $cachedassess;
+    undef %cachedstores;
+    undef %starttimes;
+    undef %usedtimes;
+    undef %numbertimes;
+    undef $includedir;
+    undef %spreadsheets;
+    undef %courserdatas;
+    undef %userrdatas;
+    undef %defaultsheets;
+    undef %rowlabel_cache;
+}
+
 sub complete_recalc {
     my $self = shift;
     undef %spreadsheets;
@@ -729,8 +739,27 @@ sub parmval {
     # second, check map parms
     my $thisparm = $parmhash{$symbparm};
     return $thisparm if (defined($thisparm));
+
     # first, check default
-    return &Apache::lonnet::metadata($fn,$rwhat.'.default');
+    $thisparm = &Apache::lonnet::metadata($fn,$rwhat.'.default');
+    return $thisparm if (defined($thisparm));
+
+    #Cascade Up
+    my $space=$what;
+    $space=~s/\.\w+$//;
+    if ($space ne '0') {
+	my @parts=split(/_/,$space);
+	my $id=pop(@parts);
+	my $part=join('_',@parts);
+	if ($part eq '') { $part='0'; }
+	my $newwhat=$rwhat;
+	$newwhat=~s/\Q$space\E/$part/;
+	my $partgeneral=&parmval($newwhat,$symb,$uname,$udom,$csec);
+	if (defined($partgeneral)) { return $partgeneral; }
+    }
+
+    #nothing defined
+    return '';
 }
 
 #
@@ -742,6 +771,15 @@ sub new {
     #
     my ($uname,$udom,$stype,$usymb)=@_;
     #
+    if (! exists($Section{$uname.':'.$udom})) {
+        my $classlist = &Apache::loncoursedata::get_classlist();
+        #
+        foreach my $student (keys(%$classlist)) {
+            my ($studentDomain,$studentName,undef,undef,undef,$studentSection,
+                undef,undef) = @{$classlist->{$student}};
+            $Section{$studentName.':'.$studentDomain} = $studentSection;
+        }
+    }
     my $self = {
         uname => $uname,
         udom  => $udom,
@@ -1514,7 +1552,7 @@ sub expandnamed {
 	foreach my $varname ( @vars ) {
             if ($varname=~/\D/) {
                $formula=~s/$varname/'$c{\''.$varname.'\'}'/ge;
-               $varname=~s/$var/\(\\w\+\)/g;
+               $varname=~s/$var/\([\\w:\\- ]\+\)/g;
 	       foreach (keys(%{$self->{'constants'}})) {
 		  if ($_=~/$varname/) {
 		      $values{$1}=1;
@@ -1893,6 +1931,22 @@ sub dump_values_to_log {
     }
     $self->logthis("--------------------------------------------------------");}
 
+##
+## Yet another debugging function
+##
+sub dump_hash_to_log {
+    my $self= shift();
+    my %tmp = @_;
+    if (@_<2) {
+        %tmp = %{$_[0]};
+    }
+    $self->logthis('---------------------------- (entries end with ":"');
+    while (my ($key,$val) = each (%tmp)) {
+        $self->logthis($key.' = '.$val.':');
+    }
+    $self->logthis('---------------------------- (entries end with ":"');
+}
+
 ################################
 ##      Helper functions      ##
 ################################
@@ -2778,7 +2832,7 @@ sub tmpwrite {
         $ENV{'user.domain'}.'_spreadsheet_'.$self->{'usymb'}.'_'.
            $self->{'filename'};
     $fn=~s/\W/\_/g;
-    $fn=$tmpdir.$fn.'.tmp';
+    $fn=$Apache::lonnet::tmpdir.$fn.'.tmp';
     my $fh;
     if ($fh=Apache::File->new('>'.$fn)) {
         my %f = $self->formulas();
@@ -2797,7 +2851,7 @@ sub tmpread {
            $ENV{'user.domain'}.'_spreadsheet_'.$self->{'usymb'}.'_'.
            $self->{'filename'};
     $fn=~s/\W/\_/g;
-    $fn=$tmpdir.$fn.'.tmp';
+    $fn=$Apache::lonnet::tmpdir.$fn.'.tmp';
     my $fh;
     my %fo=();
     my $countrows=0;
@@ -3133,14 +3187,17 @@ sub loadstudent{
     my %formulas  = $self->formulas();
     $cachedassess = $self->{'uname'}.':'.$self->{'udom'};
     # Get ALL the student preformance data
-    my @tmp = &Apache::lonnet::currentdump($self->{'uname'},
-                                           $self->{'udom'},
-                                           $self->{'cid'});
+    my @tmp = &Apache::loncoursedata::get_current_state($self->{'uname'},
+                                                        $self->{'udom'},
+                                                        undef,
+                                                        $self->{'cid'});
     if ((scalar @tmp > 0) && ($tmp[0] !~ /^error:/)) {
         %cachedstores = @tmp;
     }
     undef @tmp;
-    # 
+    # debugging code
+    # $self->dump_hash_to_log(\%cachedstores);
+    #
     my @assessdata=();
     foreach my $row ($self->rows()) {
         my $cell = 'A'.$row;
@@ -3173,7 +3230,7 @@ sub loadstudent{
     $self->constants(\%constants);
 }
 
-# --------------------------------------------------- Load data for one student
+# --------------------------------------------------- Load Course Sheet
 #
 sub loadcourse {
     my $self = shift;
@@ -3186,20 +3243,12 @@ sub loadcourse {
     foreach ($self->rows()) {
         $total++ if ($formulas{'A'.$_} !~ /^[!~-]/);
     }
-    my $now=0;
-    my $since=time;
-    $r->print(<<ENDPOP);
-<script>
-    popwin=open('','popwin','width=400,height=100');
-    popwin.document.writeln('<html><body bgcolor="#FFFFFF">'+
-      '<h3>Spreadsheet Calculation Progress</h3>'+
-      '<form name=popremain>'+
-      '<input type=text size=45 name=remaining value=Starting></form>'+
-      '</body></html>');
-    popwin.document.close();
-</script>
-ENDPOP
-    $r->rflush();
+
+    my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin($r,
+	      'Spreadsheet Status','Spreadsheet Calculation Progress', $total);
+    &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,
+					  'Processing Course Assessment Data');
+
     # It would be nice to load in the classlist and assessment info at this 
     # point, before attacking the student spreadsheets.
     foreach my $row ($self->rows()) {
@@ -3213,12 +3262,8 @@ ENDPOP
         my @studentdata=$self->exportsheet($sname,$sdom,'studentcalc',
                                      undef,undef,$r);
         undef %userrdatas;
-        $now++;
-        $r->print('<script>popwin.document.popremain.remaining.value="'.
-                  $now.'/'.$total.': '.int((time-$since)/$now*($total-$now)).
-                  ' secs remaining '.(time-$started).' last";</script>');
-        $r->rflush(); 
-        #
+	&Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
+						 'last student');
         my $index=0;
         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') {
@@ -3238,8 +3283,7 @@ ENDPOP
     }
     $self->formulas(\%formulas);
     $self->constants(\%constants);
-    $r->print('<script>popwin.close()</script>');
-    $r->rflush(); 
+    &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
 }
 
 # ------------------------------------------------ Load data for one assessment