[LON-CAPA-cvs] cvs: loncom /html/adm/help/tex Spreadsheet_Functions.tex /interface/spreadsheet studentcalc.pm

matthew lon-capa-cvs@mail.lon-capa.org
Thu, 18 Sep 2003 20:12:40 -0000


matthew		Thu Sep 18 16:12:40 2003 EDT

  Modified files:              
    /loncom/html/adm/help/tex	Spreadsheet_Functions.tex 
    /loncom/interface/spreadsheet	studentcalc.pm 
  Log:
  Added &SUMSEQ(), a new function available in the student level spreadsheet
  to sum over sequences.
  
  
Index: loncom/html/adm/help/tex/Spreadsheet_Functions.tex
diff -u loncom/html/adm/help/tex/Spreadsheet_Functions.tex:1.4 loncom/html/adm/help/tex/Spreadsheet_Functions.tex:1.5
--- loncom/html/adm/help/tex/Spreadsheet_Functions.tex:1.4	Fri Aug 29 17:08:40 2003
+++ loncom/html/adm/help/tex/Spreadsheet_Functions.tex	Thu Sep 18 16:12:39 2003
@@ -43,6 +43,27 @@
 \item \texttt{\&EXT(expression)} - access to EXT function in lonnet
 \index{EXT}
 
+\item \texttt{\&SUMSEQ(column,sequence1,sequence2,sequence3,...)} -
+sum the given column across the folders or sequences listed.  
+
+When specifying a sequence use the full title.  Instead of a sequence title
+the word ``all'' can be used to sum over all sequences.  Regular expressions
+can be entered as well, if prefixed by ``regexp:''.  See below for examples.
+
+\begin{itemize}
+
+\item \texttt{\&SUMSEQ(``Z'',''Chapter 1'');}
+
+\item \texttt{\&SUMSEQ(``Z'',''Chapter 1'',''Chapter 2'',''Chapter 3'');}
+
+\item \texttt{\&SUMSEQ(``Z'',''all'');} - sum over all sequences.
+
+\item \texttt{\&SUMSEQ(``Z'',''regexp:Large Biomolecules'');} -
+ sum over all sequences which match the regular expression 
+/Large Biomolecules/.
+
+\end{itemize}
+
 \end{itemize}
 
 In addition, most non-IO Perl functions work in cells.
Index: loncom/interface/spreadsheet/studentcalc.pm
diff -u loncom/interface/spreadsheet/studentcalc.pm:1.21 loncom/interface/spreadsheet/studentcalc.pm:1.22
--- loncom/interface/spreadsheet/studentcalc.pm:1.21	Tue Sep 16 11:39:36 2003
+++ loncom/interface/spreadsheet/studentcalc.pm	Thu Sep 18 16:12:40 2003
@@ -1,5 +1,5 @@
 #
-# $Id: studentcalc.pm,v 1.21 2003/09/16 15:39:36 matthew Exp $
+# $Id: studentcalc.pm,v 1.22 2003/09/18 20:12:40 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -57,7 +57,6 @@
 use Apache::Spreadsheet();
 use Apache::assesscalc();
 use HTML::Entities();
-use Spreadsheet::WriteExcel;
 use Time::HiRes;
 
 @Apache::studentcalc::ISA = ('Apache::Spreadsheet');
@@ -89,7 +88,7 @@
 sub initialize_sequence_cache {
     #
     # Set up the sequences and assessments
-    @Sequences = ();
+    undef(@Sequences);
     my ($top,$sequences,$assessments) = 
         &Apache::loncoursedata::get_sequence_assessment_data();
     if (! defined($top) || ! ref($top)) {
@@ -409,35 +408,103 @@
     my ($r) = @_;
 } 
 
+##
+## Routines to deal with sequences in the safe space
+##
+sub get_rows_in_sequence {
+    my $self = shift();
+    my ($sequence) = @_;
+    my @Rows;
+    foreach my $resource (@{$sequence->{'contents'}}) {
+        if ($resource->{'type'} eq 'assessment') {
+            my $rownum = $self->get_row_number_from_key($resource->{'symb'});
+            push (@Rows,$rownum);
+        }
+    }
+    return @Rows;
+}
+
+sub remove_sequence_data_from_safe_space {
+    my $self = shift();
+    my $command = 'undef(%Sequence_Rows);';
+    $self->{'safe'}->reval($command);
+}
+
+sub put_sequence_data_in_safe_space {
+    my $self = shift();
+    my $data = 'undef(%Sequence_Rows);';
+    # Build up the %Sequence_Rows hash - each sequence title is associated with
+    # an array pointer, which holds the rows in the sequence.
+    foreach my $seq (@Sequences) {
+        my @Rows = $self->get_rows_in_sequence($seq);
+        # 
+        # Potential problems with sequence titles:
+        # 1. duplicate titles - they get the total for the titles
+        # 2. control characters in titles - use q{} around the string to
+        #    deal with it.  
+        my $title = &HTML::Entities::decode($seq->{'title'});
+        $title =~ s/&\#058;/:/g;
+        if (@Rows) {
+            $data .= 'push(@{$Sequence_Rows{"'.quotemeta($title).'"}},'.
+                '('.join(',',@Rows).'));'."\n";;
+        }
+    }
+    my $new_code = $data.<<'END';
+sub SUMSEQ {
+    my ($col,@titles) = @_;
+    return 'bad column: '.$col if ($col !~ /^[A-z]$/);
+    my $sum = 0;
+    foreach my $title (@titles) {
+        while (my ($seq_title,$rows) = each(%Sequence_Rows)) {
+            my $regexp;
+            if ($title =~ /^regexp:(.*)$/) {
+                $regexp = $1;
+            } elsif (lc($title) eq 'all') {
+                $regexp = '.';
+            }
+            if (defined($regexp)) {
+                next if ($seq_title !~ /$regexp/);
+            } else {
+                next if ($seq_title ne $title);
+            }
+            foreach my $rownum (@{$rows}) {
+                my $cell = $col.$rownum;
+                if (exists($sheet_values{$cell})) {
+                    $sum += $sheet_values{$cell};
+                }
+            }
+        }
+    }
+    return $sum;
+}
+END
+    $self->{'safe'}->reval($new_code);
+    return;
+}
+
+##
+## Main computation method
+##
 sub compute {
     my $self = shift;
     my ($r) = @_;
     my $connection = $r->connection();
     if ($connection->aborted()) { $self->cleanup; return; }
     if (! defined($current_course) ||
-        $current_course ne $ENV{'request.course.id'}) {
+        $current_course ne $ENV{'request.course.id'} ||
+        ! @Sequences ) {
         $current_course = $ENV{'request.course.id'};
         &clear_package();
         &initialize_sequence_cache();
     }
     $self->initialize_safe_space();
-    my @sequences = @Sequences;
-    if (@sequences < 1) {
-        my ($top,$sequences,$assessments) = 
-            &Apache::loncoursedata::get_sequence_assessment_data();
-        if (! defined($top) || ! ref($top)) {
-            &Apache::lonnet::logthis('top is undefined');
-            return;
-        }
-        @sequences = @{$sequences} if (ref($sequences) eq 'ARRAY');
-    }
     &Apache::assesscalc::initialize_package($self->{'name'},$self->{'domain'});
     my %f = $self->formulas();
     #
     # Process the formulas list - 
     #   the formula for the A column of a row is symb__&&__filename
     my %c = $self->constants();
-    foreach my $seq (@sequences) {
+    foreach my $seq (@Sequences) {
         next if ($seq->{'num_assess'}<1);
         foreach my $resource (@{$seq->{'contents'}}) {
             if ($connection->aborted()) { $self->cleanup(); return; }
@@ -482,7 +549,9 @@
     }
     $self->constants(\%c);
     $self->formulas(\%f);
+    $self->put_sequence_data_in_safe_space();
     $self->calcsheet();
+    $self->remove_sequence_data_from_safe_space();
     #
     # Store export row in cache
     my @exportarray=$self->exportrow();