--- loncom/interface/Attic/lonspreadsheet.pm	2002/09/27 20:41:25	1.100.4.2
+++ loncom/interface/Attic/lonspreadsheet.pm	2002/08/29 15:35:01	1.103
@@ -1,5 +1,5 @@
 #
-# $Id: lonspreadsheet.pm,v 1.100.4.2 2002/09/27 20:41:25 matthew Exp $
+# $Id: lonspreadsheet.pm,v 1.103 2002/08/29 15:35:01 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -146,7 +146,7 @@ undef %v;
 undef %t;
 undef %f;
 undef %c;
-undef %rl;
+undef %rowlabel;
 undef @os;
 
 $maxrow=0;
@@ -721,6 +721,53 @@ sub SUMMIN {
     return $sum;   
 }
 
+#-------------------------------------------------------
+
+=item MINPARM(parametername)
+
+Returns the minimum value of the parameters matching the parametername.
+parametername should be a string such as 'duedate'.
+
+=cut
+
+#-------------------------------------------------------
+sub MINPARM {
+    my ($expression) = @_;
+    my $min = undef;
+    study($expression);
+    foreach $parameter (keys(%c)) {
+        next if ($parameter !~ /$expression/);
+        if ((! defined($min)) || ($min > $c{$parameter})) {
+            $min = $c{$parameter} 
+        }
+    }
+    return $min;
+}
+
+#-------------------------------------------------------
+
+=item MAXPARM(parametername)
+
+Returns the maximum value of the parameters matching the input parameter name.
+parametername should be a string such as 'duedate'.
+
+=cut
+
+#-------------------------------------------------------
+sub MAXPARM {
+    my ($expression) = @_;
+    my $max = undef;
+    study($expression);
+    foreach $parameter (keys(%c)) {
+        next if ($parameter !~ /$expression/);
+        if ((! defined($min)) || ($max < $c{$parameter})) {
+            $max = $c{$parameter} 
+        }
+    }
+    return $max;
+}
+
+#--------------------------------------------------------
 sub expandnamed {
     my $expression=shift;
     if ($expression=~/^\&/) {
@@ -822,7 +869,7 @@ sub sett {
 
 # Deal with the normal cells
     foreach (keys(%f)) {
-	if (exists($f{$_}) && ($_!~/template\_/)) {
+	if (($f{$_}) && ($_!~/template\_/)) {
             my $matches=($_=~/^$pattern(\d+)/);
             if  (($matches) && ($1)) {
 	        unless ($f{$_}=~/^\!/) {
@@ -904,8 +951,8 @@ sub outrowassess {
     my @cols=();
     if ($n) {
        my ($usy,$ufn)=split(/\_\_\&\&\&\_\_/,$f{'A'.$n});
-      if ($rl{$usy}) {
-       $cols[0]=$rl{$usy}.'<br>'.
+      if ($rowlabel{$usy}) {
+       $cols[0]=$rowlabel{$usy}.'<br>'.
                 '<select name="sel_'.$n.'" onChange="changesheet('.$n.
                 ')"><option name="default">Default</option>';
       } else { $cols[0]=''; }
@@ -935,7 +982,7 @@ sub outrow {
     my $n=shift;
     my @cols=();
     if ($n) {
-       $cols[0]=$rl{$f{'A'.$n}};
+       $cols[0]=$rowlabel{$f{'A'.$n}};
     } else {
        $cols[0]='<b><font size=+1>Export</font></b>';
     }
@@ -989,8 +1036,8 @@ sub setothersheets {
 # ------------------------------------------------ Add or change formula values
 
 sub setrowlabels {
-    my ($safeeval,%rl)=@_;
-    %{$safeeval->varglob('rl')}=%rl;
+    my ($safeeval,%rowlabel)=@_;
+    %{$safeeval->varglob('rowlabel')}=%rowlabel;
 }
 
 # ------------------------------------------------------- Calculate spreadsheet
@@ -1187,14 +1234,9 @@ sub rown {
 	   if ($vl eq '') {
 	       $vl='<font size=+2 color='.$bgcolor.'>&#35;</font>';
            }
-           $rowdata.='<td bgcolor='.$bgcolor.'>';
-           if ($ENV{'request.role'} =~ /^st\./) {
-               $rowdata.=$vl;
-           } else {
-               $rowdata.='<a href="javascript:celledit('.$fm.');">'.
-                   $vl.'</a>';
-           }
-           $rowdata.='</td>';
+           $rowdata.=
+       '<td bgcolor='.$bgcolor.'><a href="javascript:celledit('.$fm.');">'.$vl.
+	       '</a></td>';
        } else {
            $rowdata.='<td bgcolor='.$bgcolor.'>&nbsp;'.$vl.'&nbsp;</td>';
        }
@@ -1236,20 +1278,20 @@ sub outsheet {
                   '><b><font size=+1>Import</font></b></td>'.
                   '<td colspan='.$maxyellow.
 		  '><b><font size=+1>Calculations</font></b></td></tr><tr>';
-    my $showf=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',
-	     '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') {
-        $showf++;
-        if ($showf<=$maxred) { 
-           $tabledata.='<td bgcolor="#FFDDDD">'; 
-        } else {
-           $tabledata.='<td>';
-        }
-        $tabledata.="<b><font size=+1>$_</font></b></td>";
-    }
-    $tabledata.='</tr>'.&rown($safeeval,'-').&rown($safeeval,0);
+       my $showf=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',
+                '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') {
+           $showf++;
+           if ($showf<=$maxred) { 
+               $tabledata.='<td bgcolor="#FFDDDD">'; 
+           } else {
+               $tabledata.='<td>';
+           }
+           $tabledata.="<b><font size=+1>$_</font></b></td>";
+       }
+       $tabledata.='</tr>'.&rown($safeeval,'-').&rown($safeeval,0);
    } else { $tabledata='<pre>'; }
 
     $r->print($tabledata);
@@ -1264,7 +1306,6 @@ sub outsheet {
        $sortidx[$row-1]=$row-1;
     }
     @sortidx=sort { $sortby[$a] cmp $sortby[$b]; } @sortidx;
-
         my $what='Student';
         if (&gettype($safeeval) eq 'assesscalc') {
 	    $what='Item';
@@ -1274,23 +1315,20 @@ sub outsheet {
 
     my $n=0;
     for ($row=0;$row<$maxrow;$row++) {
-     my $thisrow=&rown($safeeval,$sortidx[$row]+1);
-     if ($thisrow) {
-       if (($n/25==int($n/25)) && (!$ENV{'form.showcsv'})) {
-	$r->print("</table>\n<br>\n");
-        $r->rflush();
-        $r->print('<table border=2><tr><td>&nbsp;<td>'.$what.'</td>');
-        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') {
-           $r->print('<td>'.$_.'</td>');
+        my $thisrow=&rown($safeeval,$sortidx[$row]+1);
+        if ($thisrow) {
+            if (($n/25==int($n/25)) && (!$ENV{'form.showcsv'})) {
+                $r->print("</table>\n<br>\n");
+                $r->rflush();
+                $r->print('<table border=2><tr><td>&nbsp;<td>'.$what.'</td>');
+                $r->print('<td>'.join('</td><td>',
+                                      (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.
+                                            'abcdefghijklmnopqrstuvwxyz'))).
+                          "</td></tr>\n");
+            }
+            $n++;
+            $r->print($thisrow);
         }
-        $r->print('</tr>');
-       }
-       $n++;
-       $r->print($thisrow);
-      }
     }
     $r->print($ENV{'form.showcsv'}?'</pre>':'</table>');
 }
@@ -1652,101 +1690,101 @@ sub updateclasssheet {
     my $cdom=&getcdom($safeeval);
     my $cid=&getcid($safeeval);
     my $chome=&getchome($safeeval);
-
-# ---------------------------------------------- Read class list and row labels
-
-    my $classlst=&Apache::lonnet::reply
-                                 ('dump:'.$cdom.':'.$cnum.':classlist',$chome);
+    #
+    # 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 %currentlist=();
     my $now=time;
-    unless ($classlst=~/^error\:/) {
-        foreach (split(/\&/,$classlst)) {
-            my ($name,$value)=split(/\=/,$_);
-            my ($end,$start)=split(/\:/,&Apache::lonnet::unescape($value));
-            my $active=1;
-            if (($end) && ($now>$end)) { $active=0; }
-            $active = 1 if ($ENV{'form.Status'} eq 'Any');
-            $active = !$active if ($ENV{'form.Status'} eq 'Expired');
-            if ($active) {
-                my $rowlabel='';
-                $name=&Apache::lonnet::unescape($name);
-                my ($sname,$sdom)=split(/\:/,$name);
-                my $ssec=&Apache::lonnet::usection($sdom,$sname,$cid);
-#                if ($ssec==-1) {
-#		   unless ($ENV{'form.showcsv'}) {
-#                    $rowlabel='<font color=red>Data not available: '.$name.
-#			      '</font>';
-#		   } else {
-#		       $rowlabel='ERROR","'.$name.
-#                                 '","Data not available","","","';
-#                   }
-#                } else {
-                    my %reply=&Apache::lonnet::idrget($sdom,$sname);
-                    my $reply=&Apache::lonnet::reply('get:'.$sdom.':'.$sname.
-		      ':environment:firstname&middlename&lastname&generation',
-                      &Apache::lonnet::homeserver($sname,$sdom));
-		   unless ($ENV{'form.showcsv'}) {
-                    $rowlabel='<a href="/adm/studentcalc?uname='.$sname.
-                              '&udom='.$sdom.'">'.
-                              $ssec.'&nbsp;'.$reply{$sname}.'<br>';
-                    foreach ( split(/\&/,$reply)) {
-                        $rowlabel.=&Apache::lonnet::unescape($_).' ';
+    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) {
+            my $rowlabel='';
+            my ($studentName,$studentDomain)=split(/\:/,$student);
+            my $studentSection=&Apache::lonnet::usection($studentDomain,
+                                                         $studentName,$cid);
+            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=$ssec.'","'.$reply{$sname}.'"';
-                    my $ncount=0;
-                    foreach (split(/\&/,$reply)) {
-                        $rowlabel.=',"'.&Apache::lonnet::unescape($_).'"';
-                        $ncount++;
-                    }
-                    unless ($ncount==4) { $rowlabel.=',""'; }
-                    $rowlabel=~s/\"$//;
-		   }
-#                }
-		$currentlist{&Apache::lonnet::unescape($name)}=$rowlabel;
-            }
-        } # end of foreach (split(/\&/,$classlst))
-#
-# -------------------- Find discrepancies between the course row table and this
-#
-        my %f=&getformulas($safeeval);
-        my $changed=0;
-
-        my $maxrow=0;
-        my %existing=();
-
-# ----------------------------------------------------------- Now obsolete rows
-	foreach (keys(%f)) {
-	    if ($_=~/^A(\d+)/) {
-                $maxrow=($1>$maxrow)?$1:$maxrow;
-                $existing{$f{$_}}=1;
-		unless ((defined($currentlist{$f{$_}})) || (!$1) ||
-                        ($f{$_}=~/^(\~\~\~|\-\-\-)/)) {
-		   $f{$_}='!!! Obsolete';
-                   $changed=1;
+                } else {
+                    $rowlabel= '"'.join('","',
+                                        ($studentSection,
+                                         $studentInformation{'id'},
+                                         $studentInformation{'firstname'},
+                                         $studentInformation{'middlename'},
+                                         $studentInformation{'lastname'},
+                                         $studentInformation{'generation'})
+                                        ).'"';
                 }
             }
-        }
-
-# -------------------------------------------------------- New and unknown keys
-     
-        foreach (sort keys(%currentlist)) {
-            unless ($existing{$_}) {
-		$changed=1;
-                $maxrow++;
-                $f{'A'.$maxrow}=$_;
+            $currentlist{$student}=$rowlabel;
+        } # end of if ($active)
+    } # end of foreach my $student (keys(%classlist))
+    #
+    # Find discrepancies between the course row table and this
+    #
+    my %f=&getformulas($safeeval);
+    my $changed=0;
+    #
+    my $maxrow=0;
+    my %existing=();
+    #
+    # Now obsolete rows
+    foreach (keys(%f)) {
+        if ($_=~/^A(\d+)/) {
+            $maxrow=($1>$maxrow)?$1:$maxrow;
+            $existing{$f{$_}}=1;
+            unless ((defined($currentlist{$f{$_}})) || (!$1) ||
+                    ($f{$_}=~/^(\~\~\~|\-\-\-)/)) {
+                $f{$_}='!!! Obsolete';
+                $changed=1;
             }
         }
-     
-        if ($changed) { &setformulas($safeeval,%f); }
-
-        &setmaxrow($safeeval,$maxrow);
-        &setrowlabels($safeeval,%currentlist);
-
-    } else {
-        return 'Could not access course data';
     }
+    #
+    # New and unknown keys
+    foreach (sort keys(%currentlist)) {
+        unless ($existing{$_}) {
+            $changed=1;
+            $maxrow++;
+            $f{'A'.$maxrow}=$_;
+        }
+    }
+    if ($changed) { &setformulas($safeeval,%f); }
+    #
+    &setmaxrow($safeeval,$maxrow);
+    &setrowlabels($safeeval,%currentlist);
 }
 
 # ----------------------------------- Update rows for student and assess sheets
@@ -1755,11 +1793,8 @@ sub updatestudentassesssheet {
     my $safeeval=shift;
     my %bighash;
     my $stype=&gettype($safeeval);
-    my $uname=&getuname($safeeval);
-    my $udom =&getudom($safeeval);
     my %current=();
-    unless ($updatedata{
-        $ENV{'request.course.fn'}.'_'.$stype.'_'.$uname.'_'.$udom}) {
+    unless ($updatedata{$ENV{'request.course.fn'}.'_'.$stype}) {
 # -------------------------------------------------------------------- Tie hash
       if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
                        &GDBM_READER(),0640)) {
@@ -1840,7 +1875,7 @@ sub updatestudentassesssheet {
         } elsif ($stype eq 'studentcalc') {
             %current=%allassess;
         }
-        $updatedata{$ENV{'request.course.fn'}.'_'.$stype.'_'.$uname.'_'.$udom}=
+        $updatedata{$ENV{'request.course.fn'}.'_'.$stype}=
 	    join('___;___',%current);
     } else {
         return 'Could not access course data';
@@ -1848,7 +1883,7 @@ sub updatestudentassesssheet {
 # ------------------------------------------------------ Get current from cache
     } else {
         %current=split(/\_\_\_\;\_\_\_/,
-	     $updatedata{$ENV{'request.course.fn'}.'_'.$stype.'_'.$uname.'_'.$udom});
+		       $updatedata{$ENV{'request.course.fn'}.'_'.$stype});
     }
 # -------------------- Find discrepancies between the course row table and this
 #
@@ -1901,26 +1936,22 @@ sub loadstudent {
     my %c=();
     my %f=&getformulas($safeeval);
     $cachedassess=&getuname($safeeval).':'.&getudom($safeeval);
-    %cachedstores=();
-    {
-      my $reply=&Apache::lonnet::reply('dump:'.&getudom($safeeval).':'.
-                                               &getuname($safeeval).':'.
-                                               &getcid($safeeval),
-                                               &getuhome($safeeval));
-      unless ($reply=~/^error\:/) {
-	 foreach ( split(/\&/,$reply)) {
-            my ($name,$value)=split(/\=/,$_);
-            $cachedstores{&Apache::lonnet::unescape($name)}=
-	                  &Apache::lonnet::unescape($value);
-	}
-      }
+    # Get ALL the student preformance data
+    my @tmp = &Apache::lonnet::dump(&getcid($safeeval),
+                                    &getudom($safeeval),
+                                    &getuname($safeeval),
+                                    undef);
+    if ($tmp[0] !~ /^error:/) {
+        %cachedstores = @tmp;
     }
+    undef @tmp;
+    # 
     my @assessdata=();
     foreach (keys(%f)) {
 	if ($_=~/^A(\d+)/) {
 	   my $row=$1;
            unless (($f{$_}=~/^[\!\~\-]/) || ($row==0)) {
-	      my ($usy,$ufn)=split(/\_\_\&\&\&\_\_/,$f{$_});
+	      my ($usy,$ufn)=split(/__&&&\__/,$f{$_});
 	      @assessdata=&exportsheet(&getuname($safeeval),
                                        &getudom($safeeval),
                                        'assesscalc',$usy,$ufn);
@@ -1989,18 +2020,18 @@ ENDPOP
               $r->rflush(); 
 
               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') {
+              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') {
                   if ($studentdata[$index]) {
-		     my $col=$_;
-		     if ($studentdata[$index]=~/\D/) {
-                         $c{$col.$row}="'".$studentdata[$index]."'";
- 		     } else {
-		         $c{$col.$row}=$studentdata[$index];
-		     }
-                     unless ($col eq 'A') { 
-			 $f{$col.$row}='import';
-                     }
+                      my $col=$_;
+                      if ($studentdata[$index]=~/\D/) {
+                          $c{$col.$row}="'".$studentdata[$index]."'";
+                      } else {
+                          $c{$col.$row}=$studentdata[$index];
+                      }
+                      unless ($col eq 'A') { 
+                          $f{$col.$row}='import';
+                      }
 		  }
                   $index++;
               }
@@ -2466,11 +2497,6 @@ sub handler {
       return OK;
     }
 
-    if ($ENV{'request.role'} =~ /^st\./) {
-        delete $ENV{'form.unewfield'}   if (exists($ENV{'form.unewfield'}));
-        delete $ENV{'form.unewformula'} if (exists($ENV{'form.unewformula'}));
-    }
-
 # ---------------------------------------------------- Global directory configs
 
 $includedir=$r->dir_config('lonIncludes');
@@ -2516,8 +2542,7 @@ $tmpdir=$r->dir_config('lonDaemons').'/t
 # --------------------------------------------------------------- Screen output
 
     $r->print('<html><head><title>LON-CAPA Spreadsheet</title>');
-    if ($ENV{'request.role'} !~ /^st\./) {
-        $r->print(<<ENDSCRIPT);
+    $r->print(<<ENDSCRIPT);
 <script language="JavaScript">
 
     function celledit(cn,cf) {
@@ -2543,10 +2568,7 @@ $tmpdir=$r->dir_config('lonDaemons').'/t
 
 </script>
 ENDSCRIPT
-    }
-    $r->print('</head><body bgcolor="#FFFFFF">'.
-       '<img align=right src=/adm/lonIcons/lonlogos.gif>'.
-       '<h1>LON-CAPA Spreadsheet</h1>'.
+   $r->print('</head>'.&Apache::loncommon::bodytag('Grades Spreadsheet').
        '<form action="'.$r->uri.'" name=sheet method=post>'.
        &hiddenfield('uname',$ENV{'form.uname'}).
        &hiddenfield('udom',$ENV{'form.udom'}).
@@ -2599,23 +2621,17 @@ ENDSCRIPT
     unless (&gettype($asheet) eq 'classcalc') {
         $r->print('<p><b>User:</b> '.&getuname($asheet).
                   '<br><b>Domain:</b> '.&getudom($asheet));
-#        if (&getcsec($asheet) eq '-1') {
-#           $r->print('<h3><font color=red>'.
-#                     'Not a student in this course</font></h3>');
-#        } else {
+        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> '.&getcsec($asheet));
-#        }
+        }
         if ($ENV{'form.usymb'}) {
            $r->print('<br><b>Assessment:</b> <tt>'.$ENV{'form.usymb'}.'</tt>');
         }
     }
 
-# ---------------------------------------------------------------- Course title
-
-    $r->print('<h1>'.
-            $ENV{'course.'.$ENV{'request.course.id'}.'.description'}.
-             '</h1><h3>'.localtime().'</h3>');
-
 # ---------------------------------------------------- See if user can see this
 
     if ((&gettype($asheet) eq 'classcalc') ||