--- loncom/interface/spreadsheet/Spreadsheet.pm	2005/10/12 21:48:32	1.57
+++ loncom/interface/spreadsheet/Spreadsheet.pm	2006/05/01 06:17:16	1.68
@@ -1,5 +1,5 @@
 #
-# $Id: Spreadsheet.pm,v 1.57 2005/10/12 21:48:32 albertel Exp $
+# $Id: Spreadsheet.pm,v 1.68 2006/05/01 06:17:16 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -85,7 +85,7 @@ sub new {
     my $class = ref($this) || $this;
     my ($stype) = ($class =~ /Apache::(.*)$/);
     #
-    my ($name,$domain,$filename,$usymb)=@_;
+    my ($name,$domain,$filename,$usymb,$section,$groups)=@_;
     if (defined($usymb) && ref($usymb)) {
         $usymb = $usymb->symb;
     }
@@ -95,10 +95,22 @@ sub new {
     if (! defined($domain) || $domain eq '') {
         $domain = $env{'user.domain'};
     }
+    if (! defined($section) || $section eq '') {
+        $section = &Apache::lonnet::getsection($domain,$name,
+					       $env{'request.course.id'});
+    }
+    if (! defined($groups)) {
+
+        my @usersgroups = &Apache::lonnet::get_users_groups($domain,$name,
+                                                    $env{'request.course.id'});
+        $groups = \@usersgroups;
+    }
     #
     my $self = {
         name     => $name,
         domain   => $domain,
+        section  => $section,
+        groups   => $groups, 
         type     => $stype,
         symb     => $usymb,
         errorlog => '',
@@ -378,10 +390,12 @@ Calls the system EXT function to determi
 
 #-------------------------------------------------------
 sub EXT {
-    my ($parameter) = @_;
+    my ($parameter,$specific_symb) = @_;
     return '' if (! defined($parameter) || $parameter eq '');
     $parameter =~ s/^parameter\./resource\./;
-    my $value = &Apache::lonnet::EXT($parameter,$symb,$domain,$name,$usection);
+    if ($specific_symb eq '') { $specific_symb = $symb; }
+    my $value = &Apache::lonnet::EXT($parameter,$specific_symb,$domain,$name,
+				     $usection);
     return $value;
 }
 
@@ -697,7 +711,7 @@ sub get_values {
 	}
 	return \@values;
     } else {
-	$num = '(\d+)';
+	$num = '([1-9]\d*)';
     }
     if (($la eq '*') || ($ua eq '*')) {
         $alpha='[A-z]';
@@ -806,7 +820,7 @@ sub expandnamed {
                 $result.=$thissum.'+';
             } 
             $result=~s/\+$//;
-            return $result;
+            return '('.$result.')';
         } else {
 	    return 0;
         }
@@ -1193,12 +1207,19 @@ sub display {
             last;
         }
     }
+    $self->{outputmode} = $outputmode;
     if ($outputmode eq 'html') {
         $self->compute($r);
         $self->outsheet_html($r);
     } elsif ($outputmode eq 'htmlclasslist') {
         # No computation neccessary...  This is kludgy
         $self->outsheet_htmlclasslist($r);
+    } elsif ($outputmode eq 'source') {
+        # No computation necessary. Rumor has it that this is some
+        # sort of kludge w.r.t. not "computing". It's also
+        # a bit of of a kludge that we call "outsheet_html" and 
+        # let the 'outputmode' cause the outputting of source.
+        $self->outsheet_html($r);
     } elsif ($outputmode eq 'excel') {
         $self->compute($r);
         $self->outsheet_excel($r);
@@ -1238,10 +1259,12 @@ sub html_export_row {
     foreach my $cell (@rowdata) {
         if ($cell->{'name'} =~ /^[A-Z]/) {
 	    $row_html .= '<td bgcolor="'.$color.'">'.
-                &html_editable_cell($cell,$color,$allowed).'</td>';
+                &html_editable_cell($cell,$color,$allowed,
+                                    $self->{outputmode} eq 'source').'</td>';
         } else {
 	    $row_html .= '<td bgcolor="#DDCCFF">'.
-                &html_editable_cell($cell,'#DDCCFF',$allowed).'</td>';
+                &html_editable_cell($cell,'#DDCCFF',$allowed,
+                                    $self->{outputmode} eq 'source').'</td>';
         }
     }
     return $row_html;
@@ -1261,14 +1284,15 @@ sub html_template_row {
                 &html_uneditable_cell($cell,'#FFDDDD',$allowed).'</td>';
         } else {
 	    $row_html .= '<td bgcolor="#EOFFDD">'.
-                &html_editable_cell($cell,'#EOFFDD',$allowed).'</td>';
+                &html_editable_cell($cell,'#EOFFDD',$allowed,
+                                    $self->{outputmode} eq 'source').'</td>';
         }
     }
     return $row_html;
 }
 
 sub html_editable_cell {
-    my ($cell,$bgcolor,$allowed) = @_;
+    my ($cell,$bgcolor,$allowed,$showsource) = @_;
     my $result;
     my ($name,$formula,$value);
     if (defined($cell)) {
@@ -1278,7 +1302,13 @@ sub html_editable_cell {
     }
     $name    = '' if (! defined($name));
     $formula = '' if (! defined($formula));
-    if (! defined($value)) {
+    if ($showsource) {
+        if (!defined($formula) || $formula =~ /^\s*$/) {
+            $value = '<font color="'.$bgcolor.'">#</font>';
+        } else {
+            $value = &HTML::Entities::encode($formula, '<>&"');
+        }
+    } elsif (! defined($value)) {
         $value = '<font color="'.$bgcolor.'">#</font>';
         if ($formula ne '') {
             $value = '<i>undefined value</i>';
@@ -1331,7 +1361,8 @@ sub html_row {
 	    $row_html .= &html_uneditable_cell($cell,'#FFDDDD');
 	} else {
 	    $row_html .= '<td bgcolor="#EOFFDD">';
-	    $row_html .= &html_editable_cell($cell,'#E0FFDD',$allowed);
+	    $row_html .= &html_editable_cell($cell,'#E0FFDD',$allowed,
+                                             $self->{outputmode} eq 'source');
 	}
 	$row_html .= '</td>';
     }
@@ -1355,6 +1386,8 @@ sub output_options {
               description => 'HTML'},
              {value       => 'excel',
               description => 'Excel'},
+             {value       => 'source',
+              description => 'Show Source'},
 #             {value       => 'xml',
 #              description => 'XML'},
              {value       => 'csv',
@@ -1665,7 +1698,7 @@ sub load {
 
 sub cache_sheet {
     my $self = shift;
-    my $formulas=(@_);
+    my ($formulas) = @_;
     my $stype = $self->{'type'};
     my $cnum  = $self->{'cnum'};
     my $cdom  = $self->{'cdom'};
@@ -1673,7 +1706,7 @@ sub cache_sheet {
     my $filename = $self->filename();
     my $cachekey = join('_',($cnum,$cdom,$stype,$filename));
 
-    if (ref($self->{'formulas'}) eq 'HASH') {
+    if (ref($formulas) eq 'HASH') {
 	%{$spreadsheets{$cachekey}->{'formulas'}} = %{$formulas};
     }
     if (ref($self->{'row_source'})) {