--- loncom/interface/spreadsheet/studentcalc.pm	2003/05/19 14:34:54	1.2
+++ loncom/interface/spreadsheet/studentcalc.pm	2003/12/08 19:58:39	1.25
@@ -1,5 +1,5 @@
 #
-# $Id: studentcalc.pm,v 1.2 2003/05/19 14:34:54 matthew Exp $
+# $Id: studentcalc.pm,v 1.25 2003/12/08 19:58:39 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -45,15 +45,20 @@ studentcalc
 ###################################################
 package Apache::studentcalc;
 
+use warnings FATAL=>'all';
+no warnings 'uninitialized';
+
 use strict;
 use Apache::Constants qw(:common :http);
 use Apache::lonnet;
+use Apache::loncommon();
+use Apache::loncoursedata();
 use Apache::lonnavmaps;
-use Apache::Spreadsheet;
-use Apache::assesscalc;
+use Apache::Spreadsheet();
+use Apache::assesscalc();
 use HTML::Entities();
-use Spreadsheet::WriteExcel;
 use Time::HiRes;
+use Apache::lonlocal;
 
 @Apache::studentcalc::ISA = ('Apache::Spreadsheet');
 
@@ -62,16 +67,29 @@ my %Exportrows = ();
 
 my $current_course;
 
+sub initialize {
+    &Apache::assesscalc::initialize();
+    &initialize_sequence_cache();
+}
+
 sub initialize_package {
     $current_course = $ENV{'request.course.id'};
     &initialize_sequence_cache();
     &load_cached_export_rows();
 }
 
+sub ensure_correct_sequence_data {
+    if ($current_course ne $ENV{'request.course.id'}) {
+        &initialize_sequence_cache();
+        $current_course = $ENV{'request.course.id'};
+    }
+    return;
+}
+
 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)) {
@@ -83,30 +101,72 @@ sub initialize_sequence_cache {
 }
 
 sub clear_package {
-    @Sequences = undef;
-    %Exportrows = undef;
+    undef(@Sequences);
+    undef(%Exportrows);
+    &Apache::assesscalc::clear_package();
 }
 
 sub get_title {
     my $self = shift;
-    my $title = '';
-    $title .= '<h2>Spreadsheet for '.
-        $self->{'name'}.'@'.$self->{'domain'}.'</h2>';
+    my @title = ();
+    #
+    # Determine the students name
+    my %userenv = &Apache::loncoursedata::GetUserName($self->{'name'},
+                                                      $self->{'domain'});
+    my $name = join(' ',
+                 @userenv{'firstname','middlename','lastname','generation'});
+    $name =~ s/\s+$//;
+
+    push (@title,$name);
+    push (@title,$self->{'coursedesc'});
+    push (@title,&Apache::lonlocal::locallocaltime(time));
+    return @title;
+}
+
+sub get_html_title {
+    my $self = shift;
+    my ($name,$desc,$time) = $self->get_title();
+    my $title = '<h1>'.$name;
+    if ($ENV{'user.name'} ne $self->{'name'} && 
+        $ENV{'user.domain'} ne $self->{'domain'}) {
+        $title .= ' '.&Apache::loncommon::aboutmewrapper
+                                    ($self->{'name'}.'@'.$self->{'domain'},
+                                     $self->{'name'},$self->{'domain'});
+    }
+    $title .= "</h1>\n";
+    $title .= '<h2>'.$desc."</h2>\n";
+    $title .= '<h3>'.$time.'</h3>';
     return $title;
 }
 
 sub parent_link {
     my $self = shift;
-    my $link .= '<p><a href="/adm/classcalc?'.
-        'sname='.$self->{'name'}.
-            '&sdomain='.$self->{'domain'}.'">'.
-                'Course level sheet</a></p>'."\n";
-    return $link;
+    return '<p><a href="/adm/classcalc">'.&mt('Course level sheet').'</a></p>'."\n";
+}
+
+sub convenience_links {
+    my $self = shift;
+    my ($resource) = @_;
+    my $symb = &Apache::lonnet::escape($resource->{'symb'});
+    my $result = <<"END";
+<a href="/adm/grades?symb=$symb&command=submission" target="LONcatInfo">
+    <img src="/adm/lonMisc/subm_button.gif" border=0 />
+    </a>
+<a href="/adm/grades?symb=$symb&command=gradingmenu" target="LONcatInfo">
+    <img src="/adm/lonMisc/pgrd_button.gif" border=0 />
+    </a>
+<a href="/adm/parmset?symb=$symb" target="LONcatInfo">
+    <img src="/adm/lonMisc/pprm_button.gif" border=0 />
+    </a>
+END
+    return $result;
 }
 
 sub outsheet_html {
     my $self = shift;
     my ($r) = @_;
+    my $importcolor = '#FFFFAA';
+    my $exportcolor = '#88FF88';
     ####################################
     # Get the list of assessment files #
     ####################################
@@ -114,25 +174,36 @@ sub outsheet_html {
     my $editing_is_allowed = &Apache::lonnet::allowed('mgr',
                                                 $ENV{'request.course.id'});
     ####################################
+    # Report any calculation errors    #
+    ####################################
+    $r->print($self->html_report_error());
+    ####################################
     # Determine table structure        #
     ####################################
     my $num_uneditable = 26;
     my $num_left = 52-$num_uneditable;
+    my %lt=&Apache::lonlocal::texthash(
+				       'st' => 'Student',
+				       'im' => 'Import',
+				       'ca' => 'Calculations',
+				       'as' => 'Assessment',
+				       'ro' => 'Row',
+				       );
     my $tableheader =<<"END";
 <p>
 <table border="2">
 <tr>
-  <th colspan="2" rowspan="2"><font size="+2">Student</font></th>
-  <td bgcolor="#FFDDDD" colspan="$num_uneditable">
-      <b><font size="+1">Import</font></b></td>
+  <th colspan="2" rowspan="2"><font size="+2">$lt{'st'}</font></th>
+  <td bgcolor="$importcolor" colspan="$num_uneditable">
+      <b><font size="+1">$lt{'im'}</font></b></td>
   <td colspan="$num_left">
-      <b><font size="+1">Calculations</font></b></td>
+      <b><font size="+1">$lt{'ca'}</font></b></td>
 </tr><tr>
 END
     my $label_num = 0;
     foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
         if ($label_num<$num_uneditable) { 
-            $tableheader .='<td bgcolor="#FFDDDD">';
+            $tableheader .='<td bgcolor="'.$importcolor.'">';
         } else {
             $tableheader .='<td>';
         }
@@ -140,24 +211,41 @@ END
         $label_num++;
     }
     $tableheader .="</tr>\n";
-    #
-    $r->print($tableheader);
-    #
-    # Print out template row
-    $r->print('<tr><td>Template</td><td>&nbsp;</td>'.
-	      $self->html_template_row($num_uneditable)."</tr>\n");
-    #
-    # Print out summary/export row
-    $r->print('<tr><td>Export</td><td>0</td>'.
-	      $self->html_export_row()."</tr>\n");
+    if ($self->blackout()) {
+        $r->print('<font color="red" size="+2"><p>'.
+                  &mt('Some computations are not available at this time.').'<br />'.
+                  &mt('There are problems whose status you are not allowed to view.').
+                  '</font></p>'."\n");
+    } else {
+        $r->print($tableheader);
+        #
+        # Print out template row
+        if (exists($ENV{'request.role.adv'}) && $ENV{'request.role.adv'}) {
+            $r->print('<tr><td>Template</td><td>&nbsp;</td>'.
+                      $self->html_template_row($num_uneditable,
+                                               $importcolor)."</tr>\n");
+        }
+        #
+        # Print out summary/export row
+        $r->print('<tr><td>'.&mt('Summary').'</td><td>0</td>'.
+                  $self->html_export_row($exportcolor)."</tr>\n");
+    }
     $r->print("</table>\n");
     #
     # Prepare to output rows
-    $tableheader =<<"END";
+    if (exists($ENV{'request.role.adv'}) && $ENV{'request.role.adv'}) {
+        $tableheader =<<"END";
+</p><p>
+<table border="2">
+<tr><th>$lt{'ro'}</th><th>&nbsp;</th><th>$lt{'as'}</th>
+END
+    } else {
+        $tableheader =<<"END";
 </p><p>
 <table border="2">
-<tr><th>Row</th><th>Assessment</th>
+<tr><th>&nbsp;</th><th>$lt{'as'}</th>
 END
+    }
     foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
 	if ($label_num<$num_uneditable) { 
             $tableheader.='<td bgcolor="#FFDDDD">';
@@ -183,23 +271,31 @@ END
             my $row_output = '<tr>';
             if ($editing_is_allowed) {
                 $row_output .= '<td>'.$rownum.'</td>';
+                $row_output .= '<td>'.$self->convenience_links($resource).'</td>';
                 $row_output .= '<td>'.
                     '<a href="/adm/assesscalc?sname='.$self->{'name'}.
                     '&sdomain='.$self->{'domain'}.
                     '&filename='.$assess_filename.
-                    '&usymb='.&Apache::lonnet::escape($resource->{'symb'}).'">'.
-                    $resource->{'title'}.'</a><br />';
+                    '&usymb='.&Apache::lonnet::escape($resource->{'symb'}).
+                    '">'.$resource->{'title'}.'</a><br />';
                 $row_output .= &assess_file_selector($rownum,
                                                      $assess_filename,
                                                      \@AssessFileNames).
                                                          '</td>';
             } else {
                 $row_output .= '<td><a href="'.$resource->{'src'}.'?symb='.
-                    &Apache::lonnet::escape($resource->{'symb'}).'>Go To</a>';
+                    &Apache::lonnet::escape($resource->{'symb'}).
+                    '">Go&nbsp;To</a>';
                 $row_output .= '</td><td>'.$resource->{'title'}.'</td>';
             }
-            $row_output .= $self->html_row($num_uneditable,$rownum).
-                "</tr>\n";
+            if ($self->blackout() && $self->{'blackout_rows'}->{$rownum}>0) {
+                $row_output .= 
+                    '<td colspan="52">'.&mt('Unavailable at this time').'</td></tr>'."\n";
+            } else {
+                $row_output .= $self->html_row($num_uneditable,$rownum,
+                                               $exportcolor,$importcolor).
+                    "</tr>\n";
+            }
             $r->print($row_output);
 	}
 	$r->print("</table>\n");
@@ -263,68 +359,167 @@ sub modify_cell {
     return;
 }
 
-sub outsheet_csv {
+sub csv_rows {
+    # writes the meat of the spreadsheet to an excel worksheet.  Called
+    # by Spreadsheet::outsheet_excel;
     my $self = shift;
-    my ($r) = @_;
+    my ($connection,$filehandle) = @_;
+    #
+    # Write a header row
+    $self->csv_output_row($filehandle,undef,
+                          (&mt('Sequence or Folder'),&mt('Assessment title')));
+    #
+    # Write each assessments row
+    if (scalar(@Sequences)< 1) {
+        &initialize_sequence_cache();
+    }
+    foreach my $Sequence (@Sequences) {
+	next if ($Sequence->{'num_assess'} < 1);
+	foreach my $resource (@{$Sequence->{'contents'}}) {
+	    my $rownum = $self->get_row_number_from_key($resource->{'symb'});
+            my @assessdata = ($Sequence->{'title'},
+                              $resource->{'title'});
+            $self->csv_output_row($filehandle,$rownum,@assessdata);
+        }
+    }
+    return;
 }
-sub outsheet_excel {
+
+sub excel_rows {
+    # writes the meat of the spreadsheet to an excel worksheet.  Called
+    # by Spreadsheet::outsheet_excel;
     my $self = shift;
-    my ($r) = @_;
+    my ($connection,$worksheet,$cols_output,$rows_output) = @_;
+    #
+    # Write a header row
+    $cols_output = 0;
+    foreach my $value ('Container','Assessment title') {
+        $worksheet->write($rows_output,$cols_output++,&mt($value));
+    }
+    $rows_output++;    
+    #
+    # Write each assessments row
+    if (scalar(@Sequences)< 1) {
+        &initialize_sequence_cache();
+    }
+    foreach my $Sequence (@Sequences) {
+	next if ($Sequence->{'num_assess'} < 1);
+	foreach my $resource (@{$Sequence->{'contents'}}) {
+	    my $rownum = $self->get_row_number_from_key($resource->{'symb'});
+            my @assessdata = ($Sequence->{'title'},
+                              $resource->{'title'});
+            $self->excel_output_row($worksheet,$rownum,$rows_output++,
+                                    @assessdata);
+        }
+    }
+    return;
 }
+
 sub outsheet_recursive_excel {
     my $self = shift;
     my ($r) = @_;
 } 
 
-sub display {
-    my $self = shift;
-    my ($r) = @_;
-    $self->compute();
-    $self->outsheet_html($r);
-    return;
+##
+## 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 set_row_sources {
-    my $self = shift;
-    while (my ($cell,$value) = each(%{$self->{'formulas'}})) {
-        next if ($cell !~ /^A(\d+)/ && $1 > 0);
-        my $row = $1;
-        (undef,$value) = split('__&&&__',$value);
-        $value = 'Default' if (! defined($value));
-        $self->{'row_source'}->{$row} = $value;
+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;
-    $self->logthis('computing');
+    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; }
             next if ($resource->{'type'} ne 'assessment');
             my $rownum = $self->get_row_number_from_key($resource->{'symb'});
             my $cell = 'A'.$rownum;
@@ -335,11 +530,25 @@ sub compute {
                 $self->{'row_source'}->{$rownum} = $assess_filename;
             }
             $f{$cell} = $resource->{'symb'}.'__&&&__'.$assess_filename;
-            my $assessSheet = Apache::assesscalc->new($self->{'name'},
-                                                      $self->{'domain'},
-                                                      $assess_filename,
-                                                      $resource->{'symb'});
-            my @exportdata = $assessSheet->export_data();
+            if ($connection->aborted()) { $self->cleanup(); return; }
+            my $assessSheet;
+                $assessSheet = Apache::assesscalc->new($self->{'name'},
+                                                       $self->{'domain'},
+                                                       $assess_filename,
+                                                       $resource->{'symb'});
+            my @exportdata = $assessSheet->export_data($r);
+            #
+            if ($assessSheet->badcalc()) {
+                $self->set_calcerror(
+            &mt('Error computing row for assessment "[_1]" (row [_2]):[_3]',
+                $assessSheet->get_title(),$rownum,$assessSheet->calcerror()));
+            }
+            #
+            if ($connection->aborted()) { $self->cleanup(); return; }
+            if ($assessSheet->blackout()) {
+                $self->blackout(1);
+                $self->{'blackout_rows'}->{$rownum} = 1;
+            }
             #
             # Be sure not to disturb the formulas in the 'A' column
             my $data = shift(@exportdata);
@@ -360,7 +569,9 @@ sub compute {
     }
     $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();
@@ -369,6 +580,20 @@ sub compute {
     $Exportrows{$student}->{'data'} = \@exportarray;
     # save export row
     $self->save_export_data();
+    #
+    $self->save() if ($self->need_to_save());
+    return;
+}
+
+sub set_row_sources {
+    my $self = shift;
+    while (my ($cell,$value) = each(%{$self->{'formulas'}})) {
+        next if ($cell !~ /^A(\d+)$/ || $1 < 1);
+        my $row = $1;
+        (undef,$value) = split('__&&&__',$value);
+        $value = 'Default' if (! defined($value));
+        $self->{'row_source'}->{$row} = $value;
+    }
     return;
 }
 
@@ -380,6 +605,7 @@ sub set_row_numbers {
         next if ($row == 0);
         my ($symb,undef) = split('__&&&__',$formula);
         $self->{'row_numbers'}->{$symb} = $row;
+        $self->{'maxrow'} = $row if ($row > $self->{'maxrow'});
     }
 }
 
@@ -409,7 +635,7 @@ These rows are saved in the courses dire
 #############################################
 #############################################
 sub load_cached_export_rows {
-    %Exportrows = undef;
+    undef(%Exportrows);
     my @tmp = &Apache::lonnet::dump('nohist_calculatedsheets',
 		     $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'},undef);
@@ -449,14 +675,22 @@ Writes the export data for this student
 sub save_export_data {
     my $self = shift;
     my $student = $self->{'name'}.':'.$self->{'domain'};
+    return if ($self->temporary());
+    if ($self->badcalc()){
+        # do not save data away when calculations have not been done properly.
+        delete($Exportrows{$student});
+        return;
+    }
     return if (! exists($Exportrows{$student}));
+    &Apache::assesscalc::save_cached_export_rows($self->{'name'},
+                                                 $self->{'domain'});
     return if (! $self->is_default());
     my $key = join(':',($self->{'name'},$self->{'domain'},'studentcalc')).':';
     my $timekey = $key.'.time';
     my $newstore = join('___;___',
                         @{$Exportrows{$student}->{'data'}});
     $newstore = '___=___'.$newstore;
-    &Apache::lonnet::put('nohist_calculatedsheets',
+    my $result= &Apache::lonnet::put('nohist_calculatedsheets',
                          { $key     => $newstore,
                            $timekey => $Exportrows{$student}->{'time'} },
                          $self->{'cdom'},
@@ -480,14 +714,29 @@ spreadsheet only if necessary.
 #############################################
 sub export_data {
     my $self = shift;
+    my ($r) = @_;
+    my $connection = $r->connection();
     my $student = $self->{'name'}.':'.$self->{'domain'};
     if (! exists($Exportrows{$student}) ||
+        ! defined($Exportrows{$student}) ||
+        ! exists($Exportrows{$student}->{'data'}) ||
+        ! defined($Exportrows{$student}->{'data'}) ||
+        ! exists($Exportrows{$student}->{'time'}) ||
+        ! defined($Exportrows{$student}->{'time'}) ||
         ! $self->check_expiration_time($Exportrows{$student}->{'time'})) {
-        $self->compute();
+        $self->compute($r);
     }
-    my @Data = @{$Exportrows{$student}->{'data'}};
-    for (my $i=0; $i<=$#Data;$i++) {
-        $Data[$i]="'".$Data[$i]."'" if ($Data[$i]=~/\D/ && defined($Data[$i]));
+    if ($connection->aborted()) { $self->cleanup(); return; }
+    my @Data;
+    if ($self->badcalc()) {
+        @Data = ();
+    } else {
+        @Data = @{$Exportrows{$student}->{'data'}};
+        for (my $i=0; $i<=$#Data;$i++) {
+            if ($Data[$i]=~/\D/ && defined($Data[$i])) {
+                $Data[$i]="'".$Data[$i]."'";
+            }
+        }
     }
     return @Data;
 }