--- loncom/interface/spreadsheet/studentcalc.pm 2003/09/16 15:39:36 1.21 +++ loncom/interface/spreadsheet/studentcalc.pm 2003/12/08 19:58:39 1.25 @@ -1,5 +1,5 @@ # -# $Id: studentcalc.pm,v 1.21 2003/09/16 15:39:36 matthew Exp $ +# $Id: studentcalc.pm,v 1.25 2003/12/08 19:58:39 matthew Exp $ # # Copyright Michigan State University Board of Trustees # @@ -57,8 +57,8 @@ use Apache::lonnavmaps; use Apache::Spreadsheet(); use Apache::assesscalc(); use HTML::Entities(); -use Spreadsheet::WriteExcel; use Time::HiRes; +use Apache::lonlocal; @Apache::studentcalc::ISA = ('Apache::Spreadsheet'); @@ -89,7 +89,7 @@ sub ensure_correct_sequence_data { 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)) { @@ -119,7 +119,7 @@ sub get_title { push (@title,$name); push (@title,$self->{'coursedesc'}); - push (@title,scalar(localtime(time))); + push (@title,&Apache::lonlocal::locallocaltime(time)); return @title; } @@ -141,7 +141,7 @@ sub get_html_title { sub parent_link { my $self = shift; - return '

Course level sheet

'."\n"; + return '

'.&mt('Course level sheet').'

'."\n"; } sub convenience_links { @@ -174,19 +174,30 @@ 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";

- + + $lt{'im'} + $lt{'ca'} END my $label_num = 0; @@ -202,8 +213,8 @@ END $tableheader .="\n"; if ($self->blackout()) { $r->print('

'. - 'Some computations are not available at this time.
'. - 'There are problems whose status you are allowed to view.'. + &mt('Some computations are not available at this time.').'
'. + &mt('There are problems whose status you are not allowed to view.'). '

'."\n"); } else { $r->print($tableheader); @@ -216,7 +227,7 @@ END } # # Print out summary/export row - $r->print(''. + $r->print(''. $self->html_export_row($exportcolor)."\n"); } $r->print("
Student$lt{'st'} - Import - Calculations
Summary0
'.&mt('Summary').'0
\n"); @@ -226,13 +237,13 @@ END $tableheader =<<"END";

- + END } else { $tableheader =<<"END";

Row Assessment
$lt{'ro'} $lt{'as'}
- + END } foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){ @@ -279,7 +290,7 @@ END } if ($self->blackout() && $self->{'blackout_rows'}->{$rownum}>0) { $row_output .= - ''."\n"; + ''."\n"; } else { $row_output .= $self->html_row($num_uneditable,$rownum, $exportcolor,$importcolor). @@ -352,11 +363,11 @@ sub csv_rows { # writes the meat of the spreadsheet to an excel worksheet. Called # by Spreadsheet::outsheet_excel; my $self = shift; - my ($filehandle) = @_; + my ($connection,$filehandle) = @_; # # Write a header row $self->csv_output_row($filehandle,undef, - ('Sequence or Folder','Assessment title')); + (&mt('Sequence or Folder'),&mt('Assessment title'))); # # Write each assessments row if (scalar(@Sequences)< 1) { @@ -378,12 +389,12 @@ sub excel_rows { # writes the meat of the spreadsheet to an excel worksheet. Called # by Spreadsheet::outsheet_excel; my $self = shift; - my ($worksheet,$cols_output,$rows_output) = @_; + 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++,$value); + $worksheet->write($rows_output,$cols_output++,&mt($value)); } $rows_output++; # @@ -409,35 +420,103 @@ sub outsheet_recursive_excel { 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; } @@ -452,11 +531,19 @@ sub compute { } $f{$cell} = $resource->{'symb'}.'__&&&__'.$assess_filename; if ($connection->aborted()) { $self->cleanup(); return; } - my $assessSheet = Apache::assesscalc->new($self->{'name'}, - $self->{'domain'}, - $assess_filename, - $resource->{'symb'}); + 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); @@ -482,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(); @@ -585,11 +674,16 @@ Writes the export data for this student ############################################# sub save_export_data { my $self = shift; - &Apache::assesscalc::save_cached_export_rows($self->{'name'}, - $self->{'domain'}); - return if ($self->temporary()); 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'; @@ -633,9 +727,16 @@ sub export_data { $self->compute($r); } if ($connection->aborted()) { $self->cleanup(); return; } - my @Data = @{$Exportrows{$student}->{'data'}}; - for (my $i=0; $i<=$#Data;$i++) { - $Data[$i]="'".$Data[$i]."'" if ($Data[$i]=~/\D/ && defined($Data[$i])); + 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; }
 Assessment
 $lt{'as'}Unavailable at this time
'.&mt('Unavailable at this time').'