Annotation of loncom/interface/spreadsheet/studentcalc.pm, revision 1.38

1.1       matthew     1: #
1.38    ! raeburn     2: # $Id: studentcalc.pm,v 1.37 2006/04/06 16:43:49 albertel Exp $
1.1       matthew     3: #
                      4: # Copyright Michigan State University Board of Trustees
                      5: #
                      6: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      7: #
                      8: # LON-CAPA is free software; you can redistribute it and/or modify
                      9: # it under the terms of the GNU General Public License as published by
                     10: # the Free Software Foundation; either version 2 of the License, or
                     11: # (at your option) any later version.
                     12: #
                     13: # LON-CAPA is distributed in the hope that it will be useful,
                     14: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     15: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     16: # GNU General Public License for more details.
                     17: #
                     18: # You should have received a copy of the GNU General Public License
                     19: # along with LON-CAPA; if not, write to the Free Software
                     20: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     21: #
                     22: # /home/httpd/html/adm/gpl.txt
                     23: #
                     24: # http://www.lon-capa.org/
                     25: #
                     26: # The LearningOnline Network with CAPA
                     27: # Spreadsheet/Grades Display Handler
                     28: #
                     29: # POD required stuff:
                     30: 
                     31: =head1 NAME
                     32: 
                     33: studentcalc
                     34: 
                     35: =head1 SYNOPSIS
                     36: 
                     37: =head1 DESCRIPTION
                     38: 
                     39: =over 4
                     40: 
                     41: =cut
                     42: 
                     43: ###################################################
                     44: ###                 StudentSheet                ###
                     45: ###################################################
                     46: package Apache::studentcalc;
                     47: 
1.17      matthew    48: use warnings FATAL=>'all';
                     49: no warnings 'uninitialized';
                     50: 
1.1       matthew    51: use strict;
                     52: use Apache::Constants qw(:common :http);
                     53: use Apache::lonnet;
1.3       matthew    54: use Apache::loncommon();
                     55: use Apache::loncoursedata();
1.1       matthew    56: use Apache::lonnavmaps;
1.3       matthew    57: use Apache::Spreadsheet();
                     58: use Apache::assesscalc();
1.1       matthew    59: use HTML::Entities();
                     60: use Time::HiRes;
1.23      www        61: use Apache::lonlocal;
1.1       matthew    62: 
                     63: @Apache::studentcalc::ISA = ('Apache::Spreadsheet');
                     64: 
                     65: my @Sequences = ();
1.28      matthew    66: my $navmap;
1.1       matthew    67: my %Exportrows = ();
                     68: 
                     69: my $current_course;
                     70: 
1.8       matthew    71: sub initialize {
                     72:     &initialize_sequence_cache();
1.28      matthew    73:     &Apache::assesscalc::initialize($navmap);
1.8       matthew    74: }
                     75: 
1.1       matthew    76: sub initialize_package {
1.30      albertel   77:     $current_course = $env{'request.course.id'};
1.1       matthew    78:     &initialize_sequence_cache();
                     79:     &load_cached_export_rows();
1.8       matthew    80: }
                     81: 
                     82: sub ensure_correct_sequence_data {
1.30      albertel   83:     if ($current_course ne $env{'request.course.id'}) {
1.8       matthew    84:         &initialize_sequence_cache();
1.30      albertel   85:         $current_course = $env{'request.course.id'};
1.8       matthew    86:     }
                     87:     return;
1.1       matthew    88: }
                     89: 
                     90: sub initialize_sequence_cache {
                     91:     #
                     92:     # Set up the sequences and assessments
1.22      matthew    93:     undef(@Sequences);
1.28      matthew    94:     undef($navmap);
                     95:     $navmap = Apache::lonnavmaps::navmap->new();
                     96:     if (!defined($navmap)) {
                     97:         &Apache::lonnet::logthis('student spreadsheet:Can not open Coursemap');
                     98:     }
                     99:     my @all_sequences = $navmap->retrieveResources(undef,
                    100:                                                sub { shift->is_map(); },1,0,1);
                    101:     for my $sequence ($navmap->getById('0.0'), @all_sequences) {
                    102: 	if ($navmap->hasResource($sequence,sub { shift->is_problem(); }, 0)){
                    103:             push(@Sequences,$sequence);
1.31      albertel  104: 	    &get_resources($sequence);
1.28      matthew   105:         }
1.1       matthew   106:     }
1.28      matthew   107: }
                    108: 
1.31      albertel  109: my %res_memoize;
1.28      matthew   110: sub get_resources {
                    111:     my ($seq) = @_;
1.31      albertel  112:     if (exists($res_memoize{$seq->symb()})) {
                    113: 	return @{$res_memoize{$seq->symb()}};
                    114:     }
1.28      matthew   115:     return () if (! defined($navmap) || ! ref($navmap));
                    116:     my @resources = $navmap->retrieveResources($seq,
                    117:                                                sub { shift->is_problem(); },
                    118:                                                0,0,0);
1.31      albertel  119:     $res_memoize{$seq->symb()}=\@resources;
1.28      matthew   120:     return @resources;
1.1       matthew   121: }
                    122: 
                    123: sub clear_package {
1.17      matthew   124:     undef(@Sequences);
                    125:     undef(%Exportrows);
1.31      albertel  126:     undef(%res_memoize);
1.33      albertel  127:     undef($navmap);
1.18      matthew   128:     &Apache::assesscalc::clear_package();
1.1       matthew   129: }
                    130: 
                    131: sub get_title {
                    132:     my $self = shift;
1.6       matthew   133:     my @title = ();
                    134:     #
                    135:     # Determine the students name
1.26      albertel  136:     my $name = &Apache::loncommon::plainname($self->{'name'},
                    137: 					     $self->{'domain'});
1.6       matthew   138:     push (@title,$name);
                    139:     push (@title,$self->{'coursedesc'});
1.23      www       140:     push (@title,&Apache::lonlocal::locallocaltime(time));
1.6       matthew   141:     return @title;
                    142: }
                    143: 
                    144: sub get_html_title {
                    145:     my $self = shift;
                    146:     my ($name,$desc,$time) = $self->get_title();
                    147:     my $title = '<h1>'.$name;
1.30      albertel  148:     if ($env{'user.name'} ne $self->{'name'} && 
                    149:         $env{'user.domain'} ne $self->{'domain'}) {
1.19      matthew   150:         $title .= ' '.&Apache::loncommon::aboutmewrapper
1.3       matthew   151:                                     ($self->{'name'}.'@'.$self->{'domain'},
                    152:                                      $self->{'name'},$self->{'domain'});
                    153:     }
                    154:     $title .= "</h1>\n";
1.6       matthew   155:     $title .= '<h2>'.$desc."</h2>\n";
                    156:     $title .= '<h3>'.$time.'</h3>';
1.1       matthew   157:     return $title;
                    158: }
                    159: 
                    160: sub parent_link {
                    161:     my $self = shift;
1.23      www       162:     return '<p><a href="/adm/classcalc">'.&mt('Course level sheet').'</a></p>'."\n";
1.1       matthew   163: }
                    164: 
1.14      matthew   165: sub convenience_links {
                    166:     my $self = shift;
                    167:     my ($resource) = @_;
1.28      matthew   168:     my $result=&Apache::loncommon::submlink('<img src="/adm/lonMisc/subm_button.gif" border="0" />',$self->{'name'},$self->{'domain'},$resource->symb,'LONcatInfo');
                    169:     $result .= &Apache::loncommon::pgrdlink('<img src="/adm/lonMisc/pgrd_button.gif" border="0" />',$self->{'name'},$self->{'domain'},$resource->symb,'LONcatInfo');
                    170:     $result .= &Apache::loncommon::pprmlink('<img src="/adm/lonMisc/pprm_button.gif" border="0" />',$self->{'name'},$self->{'domain'},$resource->symb,'LONcatInfo');
1.14      matthew   171:     return $result;
                    172: }
                    173: 
1.1       matthew   174: sub outsheet_html {
                    175:     my $self = shift;
                    176:     my ($r) = @_;
1.13      matthew   177:     my $importcolor = '#FFFFAA';
1.12      matthew   178:     my $exportcolor = '#88FF88';
1.1       matthew   179:     ####################################
                    180:     # Get the list of assessment files #
                    181:     ####################################
                    182:     my @AssessFileNames = $self->othersheets('assesscalc');
1.2       matthew   183:     my $editing_is_allowed = &Apache::lonnet::allowed('mgr',
1.30      albertel  184:                                                 $env{'request.course.id'});
1.1       matthew   185:     ####################################
1.24      matthew   186:     # Report any calculation errors    #
                    187:     ####################################
                    188:     $r->print($self->html_report_error());
                    189:     ####################################
1.1       matthew   190:     # Determine table structure        #
                    191:     ####################################
                    192:     my $num_uneditable = 26;
                    193:     my $num_left = 52-$num_uneditable;
1.23      www       194:     my %lt=&Apache::lonlocal::texthash(
                    195: 				       'st' => 'Student',
                    196: 				       'im' => 'Import',
                    197: 				       'ca' => 'Calculations',
                    198: 				       'as' => 'Assessment',
                    199: 				       'ro' => 'Row',
                    200: 				       );
1.1       matthew   201:     my $tableheader =<<"END";
1.2       matthew   202: <p>
1.1       matthew   203: <table border="2">
                    204: <tr>
1.23      www       205:   <th colspan="2" rowspan="2"><font size="+2">$lt{'st'}</font></th>
1.12      matthew   206:   <td bgcolor="$importcolor" colspan="$num_uneditable">
1.23      www       207:       <b><font size="+1">$lt{'im'}</font></b></td>
1.1       matthew   208:   <td colspan="$num_left">
1.23      www       209:       <b><font size="+1">$lt{'ca'}</font></b></td>
1.1       matthew   210: </tr><tr>
                    211: END
                    212:     my $label_num = 0;
                    213:     foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
                    214:         if ($label_num<$num_uneditable) { 
1.12      matthew   215:             $tableheader .='<td bgcolor="'.$importcolor.'">';
1.1       matthew   216:         } else {
                    217:             $tableheader .='<td>';
                    218:         }
                    219:         $tableheader .="<b><font size=+1>$_</font></b></td>";
                    220:         $label_num++;
                    221:     }
                    222:     $tableheader .="</tr>\n";
1.4       matthew   223:     if ($self->blackout()) {
                    224:         $r->print('<font color="red" size="+2"><p>'.
1.23      www       225:                   &mt('Some computations are not available at this time.').'<br />'.
                    226:                   &mt('There are problems whose status you are not allowed to view.').
1.4       matthew   227:                   '</font></p>'."\n");
                    228:     } else {
                    229:         $r->print($tableheader);
                    230:         #
                    231:         # Print out template row
1.30      albertel  232:         if (exists($env{'request.role.adv'}) && $env{'request.role.adv'}) {
1.4       matthew   233:             $r->print('<tr><td>Template</td><td>&nbsp;</td>'.
1.12      matthew   234:                       $self->html_template_row($num_uneditable,
                    235:                                                $importcolor)."</tr>\n");
1.4       matthew   236:         }
                    237:         #
                    238:         # Print out summary/export row
1.23      www       239:         $r->print('<tr><td>'.&mt('Summary').'</td><td>0</td>'.
1.12      matthew   240:                   $self->html_export_row($exportcolor)."</tr>\n");
1.4       matthew   241:     }
1.1       matthew   242:     $r->print("</table>\n");
                    243:     #
                    244:     # Prepare to output rows
1.30      albertel  245:     if (exists($env{'request.role.adv'}) && $env{'request.role.adv'}) {
1.4       matthew   246:         $tableheader =<<"END";
1.2       matthew   247: </p><p>
1.1       matthew   248: <table border="2">
1.23      www       249: <tr><th>$lt{'ro'}</th><th>&nbsp;</th><th>$lt{'as'}</th>
1.1       matthew   250: END
1.4       matthew   251:     } else {
                    252:         $tableheader =<<"END";
                    253: </p><p>
                    254: <table border="2">
1.23      www       255: <tr><th>&nbsp;</th><th>$lt{'as'}</th>
1.4       matthew   256: END
                    257:     }
1.1       matthew   258:     foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
                    259: 	if ($label_num<$num_uneditable) { 
                    260:             $tableheader.='<td bgcolor="#FFDDDD">';
                    261:         } else {
                    262:             $tableheader.='<td>';
                    263:         }
                    264:         $tableheader.="<b><font size=+1>$_</font></b></td>";
                    265:     }
                    266:     $tableheader.="\n";
                    267:     #
                    268:     my $num_output = 1;
                    269:     if (scalar(@Sequences)< 1) {
                    270:         &initialize_sequence_cache();
                    271:     }
                    272:     foreach my $Sequence (@Sequences) {
1.28      matthew   273: 	$r->print("<h3>".$Sequence->compTitle."</h3>\n");
1.36      bowersj2  274: 
                    275:         my @resources = &get_resources($Sequence);
                    276:         my $first_rownum =
                    277:             $self->get_row_number_from_key($resources[0]->symb);
                    278:         my $last_rownum = 
                    279:             $self->get_row_number_from_key($resources[-1]->symb);
                    280:         $r->print(&assess_file_selector([$first_rownum, $last_rownum],
                    281:                                         undef, \@AssessFileNames));
                    282: 
1.1       matthew   283:  	$r->print($tableheader);
1.36      bowersj2  284: 	foreach my $resource (@resources) {
1.28      matthew   285: 	    my $rownum = $self->get_row_number_from_key($resource->symb);
1.1       matthew   286:             my $assess_filename = $self->{'row_source'}->{$rownum};
1.2       matthew   287:             my $row_output = '<tr>';
                    288:             if ($editing_is_allowed) {
                    289:                 $row_output .= '<td>'.$rownum.'</td>';
1.14      matthew   290:                 $row_output .= '<td>'.$self->convenience_links($resource).'</td>';
1.2       matthew   291:                 $row_output .= '<td>'.
                    292:                     '<a href="/adm/assesscalc?sname='.$self->{'name'}.
                    293:                     '&sdomain='.$self->{'domain'}.
                    294:                     '&filename='.$assess_filename.
1.28      matthew   295:                     '&usymb='.&Apache::lonnet::escape($resource->symb).
                    296:                     '">'.$resource->compTitle.'</a><br />';
1.2       matthew   297:                 $row_output .= &assess_file_selector($rownum,
                    298:                                                      $assess_filename,
                    299:                                                      \@AssessFileNames).
                    300:                                                          '</td>';
                    301:             } else {
1.28      matthew   302:                 $row_output .= '<td><a href="'.$resource->src.'?symb='.
                    303:                     &Apache::lonnet::escape($resource->symb).
1.4       matthew   304:                     '">Go&nbsp;To</a>';
1.28      matthew   305:                 $row_output .= '</td><td>'.$resource->compTitle.'</td>';
1.2       matthew   306:             }
1.4       matthew   307:             if ($self->blackout() && $self->{'blackout_rows'}->{$rownum}>0) {
                    308:                 $row_output .= 
1.23      www       309:                     '<td colspan="52">'.&mt('Unavailable at this time').'</td></tr>'."\n";
1.4       matthew   310:             } else {
1.12      matthew   311:                 $row_output .= $self->html_row($num_uneditable,$rownum,
                    312:                                                $exportcolor,$importcolor).
1.4       matthew   313:                     "</tr>\n";
                    314:             }
1.1       matthew   315:             $r->print($row_output);
                    316: 	}
                    317: 	$r->print("</table>\n");
                    318:     }
1.2       matthew   319:     $r->print("</p>\n");
1.1       matthew   320:     return;
                    321: }
                    322: 
                    323: ########################################################
                    324: ########################################################
                    325: 
                    326: =pod
                    327: 
                    328: =item &assess_file_selector()
                    329: 
                    330: =cut
                    331: 
                    332: ########################################################
                    333: ########################################################
                    334: sub assess_file_selector {
                    335:     my ($row,$default,$AssessFiles)=@_;
1.2       matthew   336:     if (!defined($AssessFiles) || ! @$AssessFiles) {
                    337:         return '';
                    338:     }
1.30      albertel  339:     return '' if (! &Apache::lonnet::allowed('mgr',$env{'request.course.id'}));
1.36      bowersj2  340:     my $element_name;
                    341:     my $source_row = $row;
                    342:     if (ref($row)) {
                    343:         my ($first_rownum, $last_rownum) = @$row;
                    344:         $element_name = "FileSelect_${first_rownum}_${last_rownum}";
                    345:         $source_row = "${first_rownum}:${last_rownum}";
                    346:     } else {
                    347:         $element_name = 'FileSelect_'.$row;
                    348:     }
1.1       matthew   349:     my $load_dialog = '<select size="1" name="'.$element_name.'" '.
                    350:         'onchange="'.
1.36      bowersj2  351:         "document.sheet.cell.value='source_${source_row}';".
1.1       matthew   352:         "document.sheet.newformula.value=document.sheet.$element_name\.value;".
                    353:         'document.sheet.submit()" '.'>'."\n";
1.36      bowersj2  354:     if (ref($row)) {
                    355:         $load_dialog .= '    <option name="" value="">' .
                    356:             &mt("Select spreadsheet for entire sequence")
                    357:             . "</option>\n";
                    358:     }
1.1       matthew   359:     foreach my $file (@{$AssessFiles}) {
                    360:         $load_dialog .= '    <option name="'.$file.'"';
                    361:         $load_dialog .= ' selected' if ($default eq $file);
                    362:         $load_dialog .= '>'.$file."</option>\n";
                    363:     }
                    364:     $load_dialog .= "</select>\n";
                    365:     return $load_dialog;
                    366: }
                    367: 
                    368: sub modify_cell {
                    369:     my $self = shift;
                    370:     my ($cell,$formula) = @_;
1.36      bowersj2  371: 
                    372:     my $set_row = sub {
                    373:         my $row = shift;
                    374:         my $formula = shift;
                    375:         my $cell = 'A' . $row;
1.1       matthew   376:         $self->{'row_source'}->{$row} = $formula;
                    377:         my $original_source = $self->formula($cell);
                    378:         if ($original_source =~ /__&&&__/) {
                    379:             ($original_source,undef) = split('__&&&__',$original_source);
                    380:         }
                    381:         $formula = $original_source.'__&&&__'.$formula;
1.36      bowersj2  382:         $self->set_formula($cell,$formula);
                    383:     };
                    384: 
                    385:     if ($cell =~ /^source_(\d+):(\d+)$/) {
                    386:         my $first_row = $1;
                    387:         my $last_row = $2;
                    388:         for my $row ($first_row..$last_row) {
                    389:             $set_row->($row, $formula);
                    390:         }
                    391:     } elsif ($cell =~ /^source_(\d+)$/) {
                    392:         # Need to make sure $formula is a valid filename....
                    393:         my $row = $1;
                    394:         $set_row->($row, $formula);
1.1       matthew   395:     } elsif ($cell =~ /([A-z])\-/) {
                    396:         $cell = 'template_'.$1;
1.36      bowersj2  397:         $self->set_formula($cell,$formula);
1.1       matthew   398:     } elsif ($cell !~ /^([A-z](\d+)|template_[A-z])$/) {
                    399:         return;
                    400:     }
                    401:     $self->rebuild_stats();
                    402:     return;
                    403: }
                    404: 
1.7       matthew   405: sub csv_rows {
                    406:     # writes the meat of the spreadsheet to an excel worksheet.  Called
                    407:     # by Spreadsheet::outsheet_excel;
1.1       matthew   408:     my $self = shift;
1.25      matthew   409:     my ($connection,$filehandle) = @_;
1.7       matthew   410:     #
                    411:     # Write a header row
                    412:     $self->csv_output_row($filehandle,undef,
1.23      www       413:                           (&mt('Sequence or Folder'),&mt('Assessment title')));
1.7       matthew   414:     #
                    415:     # Write each assessments row
                    416:     if (scalar(@Sequences)< 1) {
                    417:         &initialize_sequence_cache();
                    418:     }
                    419:     foreach my $Sequence (@Sequences) {
1.28      matthew   420: 	foreach my $resource (&get_resources($Sequence)) {
                    421: 	    my $rownum = $self->get_row_number_from_key($resource->symb);
                    422:             my @assessdata = ($Sequence->compTitle,
                    423:                               $resource->compTitle);
1.7       matthew   424:             $self->csv_output_row($filehandle,$rownum,@assessdata);
                    425:         }
                    426:     }
                    427:     return;
1.1       matthew   428: }
1.6       matthew   429: 
                    430: sub excel_rows {
                    431:     # writes the meat of the spreadsheet to an excel worksheet.  Called
                    432:     # by Spreadsheet::outsheet_excel;
1.1       matthew   433:     my $self = shift;
1.29      matthew   434:     my ($connection,$worksheet,$cols_output,$rows_output,$format) = @_;
1.6       matthew   435:     #
                    436:     # Write a header row
                    437:     $cols_output = 0;
                    438:     foreach my $value ('Container','Assessment title') {
1.29      matthew   439:         $worksheet->write($rows_output,$cols_output++,&mt($value),$format->{'h4'});
1.6       matthew   440:     }
                    441:     $rows_output++;    
                    442:     #
                    443:     # Write each assessments row
                    444:     if (scalar(@Sequences)< 1) {
                    445:         &initialize_sequence_cache();
                    446:     }
                    447:     foreach my $Sequence (@Sequences) {
1.28      matthew   448: 	foreach my $resource (&get_resources($Sequence)) {
                    449: 	    my $rownum = $self->get_row_number_from_key($resource->symb);
                    450:             my @assessdata = ($Sequence->compTitle,
                    451:                               $resource->compTitle);
1.6       matthew   452:             $self->excel_output_row($worksheet,$rownum,$rows_output++,
                    453:                                     @assessdata);
                    454:         }
                    455:     }
                    456:     return;
1.1       matthew   457: }
1.6       matthew   458: 
1.1       matthew   459: sub outsheet_recursive_excel {
                    460:     my $self = shift;
                    461:     my ($r) = @_;
                    462: } 
                    463: 
1.22      matthew   464: ##
                    465: ## Routines to deal with sequences in the safe space
                    466: ##
                    467: sub get_rows_in_sequence {
                    468:     my $self = shift();
                    469:     my ($sequence) = @_;
                    470:     my @Rows;
1.28      matthew   471:     my @resources = &get_resources($sequence);
                    472:     foreach my $resource (@resources) {
                    473:         my $rownum = $self->get_row_number_from_key($resource->symb);
                    474:         push (@Rows,$rownum);
1.22      matthew   475:     }
                    476:     return @Rows;
                    477: }
                    478: 
                    479: sub remove_sequence_data_from_safe_space {
                    480:     my $self = shift();
                    481:     my $command = 'undef(%Sequence_Rows);';
                    482:     $self->{'safe'}->reval($command);
                    483: }
                    484: 
                    485: sub put_sequence_data_in_safe_space {
                    486:     my $self = shift();
                    487:     my $data = 'undef(%Sequence_Rows);';
                    488:     # Build up the %Sequence_Rows hash - each sequence title is associated with
                    489:     # an array pointer, which holds the rows in the sequence.
                    490:     foreach my $seq (@Sequences) {
                    491:         my @Rows = $self->get_rows_in_sequence($seq);
                    492:         # 
                    493:         # Potential problems with sequence titles:
                    494:         # 1. duplicate titles - they get the total for the titles
                    495:         # 2. control characters in titles - use q{} around the string to
                    496:         #    deal with it.  
1.34      albertel  497:         my $title = &HTML::Entities::decode($seq->title());
1.22      matthew   498:         $title =~ s/&\#058;/:/g;
                    499:         if (@Rows) {
                    500:             $data .= 'push(@{$Sequence_Rows{"'.quotemeta($title).'"}},'.
                    501:                 '('.join(',',@Rows).'));'."\n";;
                    502:         }
                    503:     }
                    504:     my $new_code = $data.<<'END';
                    505: sub SUMSEQ {
                    506:     my ($col,@titles) = @_;
                    507:     return 'bad column: '.$col if ($col !~ /^[A-z]$/);
                    508:     my $sum = 0;
                    509:     foreach my $title (@titles) {
                    510:         while (my ($seq_title,$rows) = each(%Sequence_Rows)) {
                    511:             my $regexp;
                    512:             if ($title =~ /^regexp:(.*)$/) {
                    513:                 $regexp = $1;
                    514:             } elsif (lc($title) eq 'all') {
                    515:                 $regexp = '.';
                    516:             }
                    517:             if (defined($regexp)) {
                    518:                 next if ($seq_title !~ /$regexp/);
                    519:             } else {
                    520:                 next if ($seq_title ne $title);
                    521:             }
                    522:             foreach my $rownum (@{$rows}) {
                    523:                 my $cell = $col.$rownum;
                    524:                 if (exists($sheet_values{$cell})) {
                    525:                     $sum += $sheet_values{$cell};
                    526:                 }
                    527:             }
                    528:         }
                    529:     }
                    530:     return $sum;
                    531: }
                    532: END
                    533:     $self->{'safe'}->reval($new_code);
                    534:     return;
                    535: }
                    536: 
                    537: ##
                    538: ## Main computation method
                    539: ##
1.1       matthew   540: sub compute {
                    541:     my $self = shift;
1.18      matthew   542:     my ($r) = @_;
1.1       matthew   543:     if (! defined($current_course) ||
1.30      albertel  544:         $current_course ne $env{'request.course.id'} ||
1.22      matthew   545:         ! @Sequences ) {
1.30      albertel  546:         $current_course = $env{'request.course.id'};
1.1       matthew   547:         &clear_package();
                    548:         &initialize_sequence_cache();
                    549:     }
                    550:     $self->initialize_safe_space();
1.28      matthew   551:     &Apache::assesscalc::initialize_package($self->{'name'},$self->{'domain'},
                    552:                                             $navmap);
1.1       matthew   553:     my %f = $self->formulas();
                    554:     #
                    555:     # Process the formulas list - 
                    556:     #   the formula for the A column of a row is symb__&&__filename
                    557:     my %c = $self->constants();
1.22      matthew   558:     foreach my $seq (@Sequences) {
1.28      matthew   559:         foreach my $resource (&get_resources($seq)) {
                    560:             my $rownum = $self->get_row_number_from_key($resource->symb);
1.1       matthew   561:             my $cell = 'A'.$rownum;
                    562:             my $assess_filename = 'Default';
                    563:             if (exists($self->{'row_source'}->{$rownum})) {
                    564:                 $assess_filename = $self->{'row_source'}->{$rownum};
                    565:             } else {
                    566:                 $self->{'row_source'}->{$rownum} = $assess_filename;
                    567:             }
1.28      matthew   568:             $f{$cell} = $resource->symb.'__&&&__'.$assess_filename;
1.24      matthew   569:             my $assessSheet;
                    570:                 $assessSheet = Apache::assesscalc->new($self->{'name'},
                    571:                                                        $self->{'domain'},
                    572:                                                        $assess_filename,
1.37      albertel  573:                                                        $resource->symb,
1.38    ! raeburn   574: 						       $self->{'section'},
        !           575:                                                        $self->{'group'});
1.18      matthew   576:             my @exportdata = $assessSheet->export_data($r);
1.24      matthew   577:             #
                    578:             if ($assessSheet->badcalc()) {
                    579:                 $self->set_calcerror(
                    580:             &mt('Error computing row for assessment "[_1]" (row [_2]):[_3]',
                    581:                 $assessSheet->get_title(),$rownum,$assessSheet->calcerror()));
                    582:             }
                    583:             #
1.4       matthew   584:             if ($assessSheet->blackout()) {
                    585:                 $self->blackout(1);
                    586:                 $self->{'blackout_rows'}->{$rownum} = 1;
                    587:             }
1.1       matthew   588:             #
                    589:             # Be sure not to disturb the formulas in the 'A' column
                    590:             my $data = shift(@exportdata);
                    591:             $c{$cell} = $data if (defined($data));
                    592:             #
                    593:             # Deal with the remaining columns
                    594:             my $i=0;
                    595:             foreach (split(//,'BCDEFGHIJKLMNOPQRSTUVWXYZ')) {
                    596:                 my $cell = $_.$rownum;
                    597:                 my $data = shift(@exportdata);
                    598:                 if (defined($data)) {
                    599:                     $f{$cell} = 'import';
                    600:                     $c{$cell} = $data;
                    601:                 }
                    602:                 $i++;
                    603:             }
                    604:         }
                    605:     }
                    606:     $self->constants(\%c);
                    607:     $self->formulas(\%f);
1.22      matthew   608:     $self->put_sequence_data_in_safe_space();
1.1       matthew   609:     $self->calcsheet();
1.22      matthew   610:     $self->remove_sequence_data_from_safe_space();
1.1       matthew   611:     #
                    612:     # Store export row in cache
                    613:     my @exportarray=$self->exportrow();
                    614:     my $student = $self->{'name'}.':'.$self->{'domain'};
                    615:     $Exportrows{$student}->{'time'} = time;
                    616:     $Exportrows{$student}->{'data'} = \@exportarray;
                    617:     # save export row
                    618:     $self->save_export_data();
1.9       matthew   619:     #
                    620:     $self->save() if ($self->need_to_save());
                    621:     return;
                    622: }
                    623: 
                    624: sub set_row_sources {
                    625:     my $self = shift;
1.35      albertel  626:     $self->check_formulas_loaded();
1.9       matthew   627:     while (my ($cell,$value) = each(%{$self->{'formulas'}})) {
1.17      matthew   628:         next if ($cell !~ /^A(\d+)$/ || $1 < 1);
1.9       matthew   629:         my $row = $1;
                    630:         (undef,$value) = split('__&&&__',$value);
                    631:         $value = 'Default' if (! defined($value));
                    632:         $self->{'row_source'}->{$row} = $value;
                    633:     }
1.1       matthew   634:     return;
                    635: }
                    636: 
                    637: sub set_row_numbers {
                    638:     my $self = shift;
1.35      albertel  639:     $self->check_formulas_loaded();
1.1       matthew   640:     while (my ($cell,$formula) = each(%{$self->{'formulas'}})) {
                    641:         next if ($cell !~ /^A(\d+)/);
                    642:         my $row = $1;
                    643:         next if ($row == 0);
                    644:         my ($symb,undef) = split('__&&&__',$formula);
                    645:         $self->{'row_numbers'}->{$symb} = $row;
1.17      matthew   646:         $self->{'maxrow'} = $row if ($row > $self->{'maxrow'});
1.1       matthew   647:     }
                    648: }
                    649: 
                    650: sub get_row_number_from_symb {
                    651:     my $self = shift;
                    652:     my ($key) = @_;
                    653:     ($key,undef) = split('__&&&__',$key) if ($key =~ /__&&&__/);
                    654:     return $self->get_row_number_from_key($key);
                    655: }
                    656: 
                    657: #############################################
                    658: #############################################
                    659: 
                    660: =pod
                    661: 
                    662: =item &load_cached_export_rows
                    663: 
                    664: Retrieves and parsers the export rows of the student spreadsheets.
                    665: These rows are saved in the courses directory in the format:
                    666: 
                    667:  sname:sdom:studentcalc:.time => time
                    668: 
                    669:  sname:sdom:studentcalc => ___=___Adata___;___Bdata___;___Cdata___;___ .....
                    670: 
                    671: =cut
                    672: 
                    673: #############################################
                    674: #############################################
                    675: sub load_cached_export_rows {
1.17      matthew   676:     undef(%Exportrows);
1.1       matthew   677:     my @tmp = &Apache::lonnet::dump('nohist_calculatedsheets',
1.30      albertel  678: 		     $env{'course.'.$env{'request.course.id'}.'.domain'},
                    679: 		     $env{'course.'.$env{'request.course.id'}.'.num'},undef);
1.1       matthew   680:     my %Selected_Assess_Sheet;
                    681:     if ($tmp[0] =~ /^error/) {
                    682:         &Apache::lonnet::logthis('unable to read cached student export rows '.
1.30      albertel  683:                                  'for course '.$env{'request.course.id'});
1.1       matthew   684:         return;
                    685:     }
                    686:     my %tmp = @tmp;
                    687:     while (my ($key,$sheetdata) = each(%tmp)) {
                    688:         my ($sname,$sdom,$sheettype,$remainder) = split(':',$key);
                    689:         my $student = $sname.':'.$sdom;
                    690:         if ($remainder =~ /\.time/) {
                    691:             $Exportrows{$student}->{'time'} = $sheetdata;
                    692:         } else {
                    693:             $sheetdata =~ s/^___=___//;
                    694:             my @Data = split('___;___',$sheetdata);
                    695:             $Exportrows{$student}->{'data'} = \@Data;
                    696:         }
                    697:     }
                    698: }
                    699: 
                    700: #############################################
                    701: #############################################
                    702: 
                    703: =pod
                    704: 
                    705: =item &save_export_data()
                    706: 
                    707: Writes the export data for this student to the course cache.
                    708: 
                    709: =cut
                    710: 
                    711: #############################################
                    712: #############################################
                    713: sub save_export_data {
                    714:     my $self = shift;
1.24      matthew   715:     my $student = $self->{'name'}.':'.$self->{'domain'};
                    716:     return if ($self->temporary());
                    717:     if ($self->badcalc()){
                    718:         # do not save data away when calculations have not been done properly.
                    719:         delete($Exportrows{$student});
                    720:         return;
                    721:     }
                    722:     return if (! exists($Exportrows{$student}));
1.20      matthew   723:     &Apache::assesscalc::save_cached_export_rows($self->{'name'},
                    724:                                                  $self->{'domain'});
1.1       matthew   725:     return if (! $self->is_default());
                    726:     my $key = join(':',($self->{'name'},$self->{'domain'},'studentcalc')).':';
                    727:     my $timekey = $key.'.time';
                    728:     my $newstore = join('___;___',
                    729:                         @{$Exportrows{$student}->{'data'}});
                    730:     $newstore = '___=___'.$newstore;
1.16      matthew   731:     my $result= &Apache::lonnet::put('nohist_calculatedsheets',
1.1       matthew   732:                          { $key     => $newstore,
                    733:                            $timekey => $Exportrows{$student}->{'time'} },
                    734:                          $self->{'cdom'},
                    735:                          $self->{'cnum'});
                    736:     return;
                    737: }
                    738: 
                    739: #############################################
                    740: #############################################
                    741: 
                    742: =pod
                    743: 
                    744: =item &export_data()
                    745: 
                    746: Returns the export data associated with the spreadsheet.  Computes the
                    747: spreadsheet only if necessary.
                    748: 
                    749: =cut
                    750: 
                    751: #############################################
                    752: #############################################
                    753: sub export_data {
                    754:     my $self = shift;
1.18      matthew   755:     my ($r) = @_;
                    756:     my $connection = $r->connection();
1.1       matthew   757:     my $student = $self->{'name'}.':'.$self->{'domain'};
                    758:     if (! exists($Exportrows{$student}) ||
1.15      matthew   759:         ! defined($Exportrows{$student}) ||
1.16      matthew   760:         ! exists($Exportrows{$student}->{'data'}) ||
1.15      matthew   761:         ! defined($Exportrows{$student}->{'data'}) ||
1.16      matthew   762:         ! exists($Exportrows{$student}->{'time'}) ||
                    763:         ! defined($Exportrows{$student}->{'time'}) ||
1.1       matthew   764:         ! $self->check_expiration_time($Exportrows{$student}->{'time'})) {
1.18      matthew   765:         $self->compute($r);
1.1       matthew   766:     }
1.18      matthew   767:     if ($connection->aborted()) { $self->cleanup(); return; }
1.24      matthew   768:     my @Data;
                    769:     if ($self->badcalc()) {
                    770:         @Data = ();
                    771:     } else {
                    772:         @Data = @{$Exportrows{$student}->{'data'}};
                    773:         for (my $i=0; $i<=$#Data;$i++) {
                    774:             if ($Data[$i]=~/\D/ && defined($Data[$i])) {
                    775:                 $Data[$i]="'".$Data[$i]."'";
                    776:             }
                    777:         }
1.1       matthew   778:     }
                    779:     return @Data;
                    780: }
                    781: 
                    782: 1;
                    783: 
                    784: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>