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

1.1       matthew     1: #
1.23    ! www         2: # $Id: studentcalc.pm,v 1.22 2003/09/18 20:12:40 matthew 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 = ();
                     66: my %Exportrows = ();
                     67: 
                     68: my $current_course;
                     69: 
1.8       matthew    70: sub initialize {
1.11      matthew    71:     &Apache::assesscalc::initialize();
1.8       matthew    72:     &initialize_sequence_cache();
                     73: }
                     74: 
1.1       matthew    75: sub initialize_package {
                     76:     $current_course = $ENV{'request.course.id'};
                     77:     &initialize_sequence_cache();
                     78:     &load_cached_export_rows();
1.8       matthew    79: }
                     80: 
                     81: sub ensure_correct_sequence_data {
                     82:     if ($current_course ne $ENV{'request.course.id'}) {
                     83:         &initialize_sequence_cache();
                     84:         $current_course = $ENV{'request.course.id'};
                     85:     }
                     86:     return;
1.1       matthew    87: }
                     88: 
                     89: sub initialize_sequence_cache {
                     90:     #
                     91:     # Set up the sequences and assessments
1.22      matthew    92:     undef(@Sequences);
1.1       matthew    93:     my ($top,$sequences,$assessments) = 
                     94:         &Apache::loncoursedata::get_sequence_assessment_data();
                     95:     if (! defined($top) || ! ref($top)) {
                     96:         # There has been an error, better report it
1.2       matthew    97:         &Apache::lonnet::logthis('top is undefined (studentcalc.pm)');
1.1       matthew    98:         return;
                     99:     }
                    100:     @Sequences = @{$sequences} if (ref($sequences) eq 'ARRAY');
                    101: }
                    102: 
                    103: sub clear_package {
1.17      matthew   104:     undef(@Sequences);
                    105:     undef(%Exportrows);
1.18      matthew   106:     &Apache::assesscalc::clear_package();
1.1       matthew   107: }
                    108: 
                    109: sub get_title {
                    110:     my $self = shift;
1.6       matthew   111:     my @title = ();
                    112:     #
                    113:     # Determine the students name
1.3       matthew   114:     my %userenv = &Apache::loncoursedata::GetUserName($self->{'name'},
                    115:                                                       $self->{'domain'});
1.6       matthew   116:     my $name = join(' ',
                    117:                  @userenv{'firstname','middlename','lastname','generation'});
1.3       matthew   118:     $name =~ s/\s+$//;
1.6       matthew   119: 
                    120:     push (@title,$name);
                    121:     push (@title,$self->{'coursedesc'});
1.23    ! www       122:     push (@title,&Apache::lonlocal::locallocaltime(time));
1.6       matthew   123:     return @title;
                    124: }
                    125: 
                    126: sub get_html_title {
                    127:     my $self = shift;
                    128:     my ($name,$desc,$time) = $self->get_title();
                    129:     my $title = '<h1>'.$name;
1.3       matthew   130:     if ($ENV{'user.name'} ne $self->{'name'} && 
                    131:         $ENV{'user.domain'} ne $self->{'domain'}) {
1.19      matthew   132:         $title .= ' '.&Apache::loncommon::aboutmewrapper
1.3       matthew   133:                                     ($self->{'name'}.'@'.$self->{'domain'},
                    134:                                      $self->{'name'},$self->{'domain'});
                    135:     }
                    136:     $title .= "</h1>\n";
1.6       matthew   137:     $title .= '<h2>'.$desc."</h2>\n";
                    138:     $title .= '<h3>'.$time.'</h3>';
1.1       matthew   139:     return $title;
                    140: }
                    141: 
                    142: sub parent_link {
                    143:     my $self = shift;
1.23    ! www       144:     return '<p><a href="/adm/classcalc">'.&mt('Course level sheet').'</a></p>'."\n";
1.1       matthew   145: }
                    146: 
1.14      matthew   147: sub convenience_links {
                    148:     my $self = shift;
                    149:     my ($resource) = @_;
                    150:     my $symb = &Apache::lonnet::escape($resource->{'symb'});
                    151:     my $result = <<"END";
                    152: <a href="/adm/grades?symb=$symb&command=submission" target="LONcatInfo">
                    153:     <img src="/adm/lonMisc/subm_button.gif" border=0 />
                    154:     </a>
                    155: <a href="/adm/grades?symb=$symb&command=gradingmenu" target="LONcatInfo">
                    156:     <img src="/adm/lonMisc/pgrd_button.gif" border=0 />
                    157:     </a>
                    158: <a href="/adm/parmset?symb=$symb" target="LONcatInfo">
                    159:     <img src="/adm/lonMisc/pprm_button.gif" border=0 />
                    160:     </a>
                    161: END
                    162:     return $result;
                    163: }
                    164: 
1.1       matthew   165: sub outsheet_html {
                    166:     my $self = shift;
                    167:     my ($r) = @_;
1.13      matthew   168:     my $importcolor = '#FFFFAA';
1.12      matthew   169:     my $exportcolor = '#88FF88';
1.1       matthew   170:     ####################################
                    171:     # Get the list of assessment files #
                    172:     ####################################
                    173:     my @AssessFileNames = $self->othersheets('assesscalc');
1.2       matthew   174:     my $editing_is_allowed = &Apache::lonnet::allowed('mgr',
                    175:                                                 $ENV{'request.course.id'});
1.1       matthew   176:     ####################################
                    177:     # Determine table structure        #
                    178:     ####################################
                    179:     my $num_uneditable = 26;
                    180:     my $num_left = 52-$num_uneditable;
1.23    ! www       181:     my %lt=&Apache::lonlocal::texthash(
        !           182: 				       'st' => 'Student',
        !           183: 				       'im' => 'Import',
        !           184: 				       'ca' => 'Calculations',
        !           185: 				       'as' => 'Assessment',
        !           186: 				       'ro' => 'Row',
        !           187: 				       );
1.1       matthew   188:     my $tableheader =<<"END";
1.2       matthew   189: <p>
1.1       matthew   190: <table border="2">
                    191: <tr>
1.23    ! www       192:   <th colspan="2" rowspan="2"><font size="+2">$lt{'st'}</font></th>
1.12      matthew   193:   <td bgcolor="$importcolor" colspan="$num_uneditable">
1.23    ! www       194:       <b><font size="+1">$lt{'im'}</font></b></td>
1.1       matthew   195:   <td colspan="$num_left">
1.23    ! www       196:       <b><font size="+1">$lt{'ca'}</font></b></td>
1.1       matthew   197: </tr><tr>
                    198: END
                    199:     my $label_num = 0;
                    200:     foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
                    201:         if ($label_num<$num_uneditable) { 
1.12      matthew   202:             $tableheader .='<td bgcolor="'.$importcolor.'">';
1.1       matthew   203:         } else {
                    204:             $tableheader .='<td>';
                    205:         }
                    206:         $tableheader .="<b><font size=+1>$_</font></b></td>";
                    207:         $label_num++;
                    208:     }
                    209:     $tableheader .="</tr>\n";
1.4       matthew   210:     if ($self->blackout()) {
                    211:         $r->print('<font color="red" size="+2"><p>'.
1.23    ! www       212:                   &mt('Some computations are not available at this time.').'<br />'.
        !           213:                   &mt('There are problems whose status you are not allowed to view.').
1.4       matthew   214:                   '</font></p>'."\n");
                    215:     } else {
                    216:         $r->print($tableheader);
                    217:         #
                    218:         # Print out template row
                    219:         if (exists($ENV{'request.role.adv'}) && $ENV{'request.role.adv'}) {
                    220:             $r->print('<tr><td>Template</td><td>&nbsp;</td>'.
1.12      matthew   221:                       $self->html_template_row($num_uneditable,
                    222:                                                $importcolor)."</tr>\n");
1.4       matthew   223:         }
                    224:         #
                    225:         # Print out summary/export row
1.23    ! www       226:         $r->print('<tr><td>'.&mt('Summary').'</td><td>0</td>'.
1.12      matthew   227:                   $self->html_export_row($exportcolor)."</tr>\n");
1.4       matthew   228:     }
1.1       matthew   229:     $r->print("</table>\n");
                    230:     #
                    231:     # Prepare to output rows
1.4       matthew   232:     if (exists($ENV{'request.role.adv'}) && $ENV{'request.role.adv'}) {
                    233:         $tableheader =<<"END";
1.2       matthew   234: </p><p>
1.1       matthew   235: <table border="2">
1.23    ! www       236: <tr><th>$lt{'ro'}</th><th>&nbsp;</th><th>$lt{'as'}</th>
1.1       matthew   237: END
1.4       matthew   238:     } else {
                    239:         $tableheader =<<"END";
                    240: </p><p>
                    241: <table border="2">
1.23    ! www       242: <tr><th>&nbsp;</th><th>$lt{'as'}</th>
1.4       matthew   243: END
                    244:     }
1.1       matthew   245:     foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
                    246: 	if ($label_num<$num_uneditable) { 
                    247:             $tableheader.='<td bgcolor="#FFDDDD">';
                    248:         } else {
                    249:             $tableheader.='<td>';
                    250:         }
                    251:         $tableheader.="<b><font size=+1>$_</font></b></td>";
                    252:     }
                    253:     $tableheader.="\n";
                    254:     #
                    255:     my $num_output = 1;
                    256:     if (scalar(@Sequences)< 1) {
                    257:         &initialize_sequence_cache();
                    258:     }
                    259:     foreach my $Sequence (@Sequences) {
                    260: 	next if ($Sequence->{'num_assess'} < 1);
                    261: 	$r->print("<h3>".$Sequence->{'title'}."</h3>\n");
                    262:  	$r->print($tableheader);
                    263: 	foreach my $resource (@{$Sequence->{'contents'}}) {
                    264: 	    next if ($resource->{'type'} ne 'assessment');
                    265: 	    my $rownum = $self->get_row_number_from_key($resource->{'symb'});
                    266:             my $assess_filename = $self->{'row_source'}->{$rownum};
1.2       matthew   267:             my $row_output = '<tr>';
                    268:             if ($editing_is_allowed) {
                    269:                 $row_output .= '<td>'.$rownum.'</td>';
1.14      matthew   270:                 $row_output .= '<td>'.$self->convenience_links($resource).'</td>';
1.2       matthew   271:                 $row_output .= '<td>'.
                    272:                     '<a href="/adm/assesscalc?sname='.$self->{'name'}.
                    273:                     '&sdomain='.$self->{'domain'}.
                    274:                     '&filename='.$assess_filename.
1.4       matthew   275:                     '&usymb='.&Apache::lonnet::escape($resource->{'symb'}).
                    276:                     '">'.$resource->{'title'}.'</a><br />';
1.2       matthew   277:                 $row_output .= &assess_file_selector($rownum,
                    278:                                                      $assess_filename,
                    279:                                                      \@AssessFileNames).
                    280:                                                          '</td>';
                    281:             } else {
                    282:                 $row_output .= '<td><a href="'.$resource->{'src'}.'?symb='.
1.4       matthew   283:                     &Apache::lonnet::escape($resource->{'symb'}).
                    284:                     '">Go&nbsp;To</a>';
1.2       matthew   285:                 $row_output .= '</td><td>'.$resource->{'title'}.'</td>';
                    286:             }
1.4       matthew   287:             if ($self->blackout() && $self->{'blackout_rows'}->{$rownum}>0) {
                    288:                 $row_output .= 
1.23    ! www       289:                     '<td colspan="52">'.&mt('Unavailable at this time').'</td></tr>'."\n";
1.4       matthew   290:             } else {
1.12      matthew   291:                 $row_output .= $self->html_row($num_uneditable,$rownum,
                    292:                                                $exportcolor,$importcolor).
1.4       matthew   293:                     "</tr>\n";
                    294:             }
1.1       matthew   295:             $r->print($row_output);
                    296: 	}
                    297: 	$r->print("</table>\n");
                    298:     }
1.2       matthew   299:     $r->print("</p>\n");
1.1       matthew   300:     return;
                    301: }
                    302: 
                    303: ########################################################
                    304: ########################################################
                    305: 
                    306: =pod
                    307: 
                    308: =item &assess_file_selector()
                    309: 
                    310: =cut
                    311: 
                    312: ########################################################
                    313: ########################################################
                    314: sub assess_file_selector {
                    315:     my ($row,$default,$AssessFiles)=@_;
1.2       matthew   316:     if (!defined($AssessFiles) || ! @$AssessFiles) {
                    317:         return '';
                    318:     }
1.1       matthew   319:     return '' if (! &Apache::lonnet::allowed('mgr',$ENV{'request.course.id'}));
                    320:     my $element_name = 'FileSelect_'.$row;
                    321:     my $load_dialog = '<select size="1" name="'.$element_name.'" '.
                    322:         'onchange="'.
                    323:         "document.sheet.cell.value='source_$row';".
                    324:         "document.sheet.newformula.value=document.sheet.$element_name\.value;".
                    325:         'document.sheet.submit()" '.'>'."\n";
                    326:     foreach my $file (@{$AssessFiles}) {
                    327:         $load_dialog .= '    <option name="'.$file.'"';
                    328:         $load_dialog .= ' selected' if ($default eq $file);
                    329:         $load_dialog .= '>'.$file."</option>\n";
                    330:     }
                    331:     $load_dialog .= "</select>\n";
                    332:     return $load_dialog;
                    333: }
                    334: 
                    335: sub modify_cell {
                    336:     my $self = shift;
                    337:     my ($cell,$formula) = @_;
                    338:     if ($cell =~ /^source_(\d+)$/) {
                    339:         # Need to make sure $formula is a valid filename....
                    340:         my $row = $1;
                    341:         $cell = 'A'.$row;
                    342:         $self->{'row_source'}->{$row} = $formula;
                    343:         my $original_source = $self->formula($cell);
                    344:         if ($original_source =~ /__&&&__/) {
                    345:             ($original_source,undef) = split('__&&&__',$original_source);
                    346:         }
                    347:         $formula = $original_source.'__&&&__'.$formula;
                    348:     } elsif ($cell =~ /([A-z])\-/) {
                    349:         $cell = 'template_'.$1;
                    350:     } elsif ($cell !~ /^([A-z](\d+)|template_[A-z])$/) {
                    351:         return;
                    352:     }
                    353:     $self->set_formula($cell,$formula);
                    354:     $self->rebuild_stats();
                    355:     return;
                    356: }
                    357: 
1.7       matthew   358: sub csv_rows {
                    359:     # writes the meat of the spreadsheet to an excel worksheet.  Called
                    360:     # by Spreadsheet::outsheet_excel;
1.1       matthew   361:     my $self = shift;
1.7       matthew   362:     my ($filehandle) = @_;
                    363:     #
                    364:     # Write a header row
                    365:     $self->csv_output_row($filehandle,undef,
1.23    ! www       366:                           (&mt('Sequence or Folder'),&mt('Assessment title')));
1.7       matthew   367:     #
                    368:     # Write each assessments row
                    369:     if (scalar(@Sequences)< 1) {
                    370:         &initialize_sequence_cache();
                    371:     }
                    372:     foreach my $Sequence (@Sequences) {
                    373: 	next if ($Sequence->{'num_assess'} < 1);
                    374: 	foreach my $resource (@{$Sequence->{'contents'}}) {
                    375: 	    my $rownum = $self->get_row_number_from_key($resource->{'symb'});
                    376:             my @assessdata = ($Sequence->{'title'},
                    377:                               $resource->{'title'});
                    378:             $self->csv_output_row($filehandle,$rownum,@assessdata);
                    379:         }
                    380:     }
                    381:     return;
1.1       matthew   382: }
1.6       matthew   383: 
                    384: sub excel_rows {
                    385:     # writes the meat of the spreadsheet to an excel worksheet.  Called
                    386:     # by Spreadsheet::outsheet_excel;
1.1       matthew   387:     my $self = shift;
1.6       matthew   388:     my ($worksheet,$cols_output,$rows_output) = @_;
                    389:     #
                    390:     # Write a header row
                    391:     $cols_output = 0;
                    392:     foreach my $value ('Container','Assessment title') {
1.23    ! www       393:         $worksheet->write($rows_output,$cols_output++,&mt($value));
1.6       matthew   394:     }
                    395:     $rows_output++;    
                    396:     #
                    397:     # Write each assessments row
                    398:     if (scalar(@Sequences)< 1) {
                    399:         &initialize_sequence_cache();
                    400:     }
                    401:     foreach my $Sequence (@Sequences) {
                    402: 	next if ($Sequence->{'num_assess'} < 1);
                    403: 	foreach my $resource (@{$Sequence->{'contents'}}) {
                    404: 	    my $rownum = $self->get_row_number_from_key($resource->{'symb'});
                    405:             my @assessdata = ($Sequence->{'title'},
                    406:                               $resource->{'title'});
                    407:             $self->excel_output_row($worksheet,$rownum,$rows_output++,
                    408:                                     @assessdata);
                    409:         }
                    410:     }
                    411:     return;
1.1       matthew   412: }
1.6       matthew   413: 
1.1       matthew   414: sub outsheet_recursive_excel {
                    415:     my $self = shift;
                    416:     my ($r) = @_;
                    417: } 
                    418: 
1.22      matthew   419: ##
                    420: ## Routines to deal with sequences in the safe space
                    421: ##
                    422: sub get_rows_in_sequence {
                    423:     my $self = shift();
                    424:     my ($sequence) = @_;
                    425:     my @Rows;
                    426:     foreach my $resource (@{$sequence->{'contents'}}) {
                    427:         if ($resource->{'type'} eq 'assessment') {
                    428:             my $rownum = $self->get_row_number_from_key($resource->{'symb'});
                    429:             push (@Rows,$rownum);
                    430:         }
                    431:     }
                    432:     return @Rows;
                    433: }
                    434: 
                    435: sub remove_sequence_data_from_safe_space {
                    436:     my $self = shift();
                    437:     my $command = 'undef(%Sequence_Rows);';
                    438:     $self->{'safe'}->reval($command);
                    439: }
                    440: 
                    441: sub put_sequence_data_in_safe_space {
                    442:     my $self = shift();
                    443:     my $data = 'undef(%Sequence_Rows);';
                    444:     # Build up the %Sequence_Rows hash - each sequence title is associated with
                    445:     # an array pointer, which holds the rows in the sequence.
                    446:     foreach my $seq (@Sequences) {
                    447:         my @Rows = $self->get_rows_in_sequence($seq);
                    448:         # 
                    449:         # Potential problems with sequence titles:
                    450:         # 1. duplicate titles - they get the total for the titles
                    451:         # 2. control characters in titles - use q{} around the string to
                    452:         #    deal with it.  
                    453:         my $title = &HTML::Entities::decode($seq->{'title'});
                    454:         $title =~ s/&\#058;/:/g;
                    455:         if (@Rows) {
                    456:             $data .= 'push(@{$Sequence_Rows{"'.quotemeta($title).'"}},'.
                    457:                 '('.join(',',@Rows).'));'."\n";;
                    458:         }
                    459:     }
                    460:     my $new_code = $data.<<'END';
                    461: sub SUMSEQ {
                    462:     my ($col,@titles) = @_;
                    463:     return 'bad column: '.$col if ($col !~ /^[A-z]$/);
                    464:     my $sum = 0;
                    465:     foreach my $title (@titles) {
                    466:         while (my ($seq_title,$rows) = each(%Sequence_Rows)) {
                    467:             my $regexp;
                    468:             if ($title =~ /^regexp:(.*)$/) {
                    469:                 $regexp = $1;
                    470:             } elsif (lc($title) eq 'all') {
                    471:                 $regexp = '.';
                    472:             }
                    473:             if (defined($regexp)) {
                    474:                 next if ($seq_title !~ /$regexp/);
                    475:             } else {
                    476:                 next if ($seq_title ne $title);
                    477:             }
                    478:             foreach my $rownum (@{$rows}) {
                    479:                 my $cell = $col.$rownum;
                    480:                 if (exists($sheet_values{$cell})) {
                    481:                     $sum += $sheet_values{$cell};
                    482:                 }
                    483:             }
                    484:         }
                    485:     }
                    486:     return $sum;
                    487: }
                    488: END
                    489:     $self->{'safe'}->reval($new_code);
                    490:     return;
                    491: }
                    492: 
                    493: ##
                    494: ## Main computation method
                    495: ##
1.1       matthew   496: sub compute {
                    497:     my $self = shift;
1.18      matthew   498:     my ($r) = @_;
                    499:     my $connection = $r->connection();
                    500:     if ($connection->aborted()) { $self->cleanup; return; }
1.1       matthew   501:     if (! defined($current_course) ||
1.22      matthew   502:         $current_course ne $ENV{'request.course.id'} ||
                    503:         ! @Sequences ) {
1.1       matthew   504:         $current_course = $ENV{'request.course.id'};
                    505:         &clear_package();
                    506:         &initialize_sequence_cache();
                    507:     }
                    508:     $self->initialize_safe_space();
                    509:     &Apache::assesscalc::initialize_package($self->{'name'},$self->{'domain'});
                    510:     my %f = $self->formulas();
                    511:     #
                    512:     # Process the formulas list - 
                    513:     #   the formula for the A column of a row is symb__&&__filename
                    514:     my %c = $self->constants();
1.22      matthew   515:     foreach my $seq (@Sequences) {
1.1       matthew   516:         next if ($seq->{'num_assess'}<1);
                    517:         foreach my $resource (@{$seq->{'contents'}}) {
1.18      matthew   518:             if ($connection->aborted()) { $self->cleanup(); return; }
1.1       matthew   519:             next if ($resource->{'type'} ne 'assessment');
                    520:             my $rownum = $self->get_row_number_from_key($resource->{'symb'});
                    521:             my $cell = 'A'.$rownum;
                    522:             my $assess_filename = 'Default';
                    523:             if (exists($self->{'row_source'}->{$rownum})) {
                    524:                 $assess_filename = $self->{'row_source'}->{$rownum};
                    525:             } else {
                    526:                 $self->{'row_source'}->{$rownum} = $assess_filename;
                    527:             }
                    528:             $f{$cell} = $resource->{'symb'}.'__&&&__'.$assess_filename;
1.18      matthew   529:             if ($connection->aborted()) { $self->cleanup(); return; }
1.1       matthew   530:             my $assessSheet = Apache::assesscalc->new($self->{'name'},
                    531:                                                       $self->{'domain'},
                    532:                                                       $assess_filename,
                    533:                                                       $resource->{'symb'});
1.18      matthew   534:             my @exportdata = $assessSheet->export_data($r);
                    535:             if ($connection->aborted()) { $self->cleanup(); return; }
1.4       matthew   536:             if ($assessSheet->blackout()) {
                    537:                 $self->blackout(1);
                    538:                 $self->{'blackout_rows'}->{$rownum} = 1;
                    539:             }
1.1       matthew   540:             #
                    541:             # Be sure not to disturb the formulas in the 'A' column
                    542:             my $data = shift(@exportdata);
                    543:             $c{$cell} = $data if (defined($data));
                    544:             #
                    545:             # Deal with the remaining columns
                    546:             my $i=0;
                    547:             foreach (split(//,'BCDEFGHIJKLMNOPQRSTUVWXYZ')) {
                    548:                 my $cell = $_.$rownum;
                    549:                 my $data = shift(@exportdata);
                    550:                 if (defined($data)) {
                    551:                     $f{$cell} = 'import';
                    552:                     $c{$cell} = $data;
                    553:                 }
                    554:                 $i++;
                    555:             }
                    556:         }
                    557:     }
                    558:     $self->constants(\%c);
                    559:     $self->formulas(\%f);
1.22      matthew   560:     $self->put_sequence_data_in_safe_space();
1.1       matthew   561:     $self->calcsheet();
1.22      matthew   562:     $self->remove_sequence_data_from_safe_space();
1.1       matthew   563:     #
                    564:     # Store export row in cache
                    565:     my @exportarray=$self->exportrow();
                    566:     my $student = $self->{'name'}.':'.$self->{'domain'};
                    567:     $Exportrows{$student}->{'time'} = time;
                    568:     $Exportrows{$student}->{'data'} = \@exportarray;
                    569:     # save export row
                    570:     $self->save_export_data();
1.9       matthew   571:     #
                    572:     $self->save() if ($self->need_to_save());
                    573:     return;
                    574: }
                    575: 
                    576: sub set_row_sources {
                    577:     my $self = shift;
                    578:     while (my ($cell,$value) = each(%{$self->{'formulas'}})) {
1.17      matthew   579:         next if ($cell !~ /^A(\d+)$/ || $1 < 1);
1.9       matthew   580:         my $row = $1;
                    581:         (undef,$value) = split('__&&&__',$value);
                    582:         $value = 'Default' if (! defined($value));
                    583:         $self->{'row_source'}->{$row} = $value;
                    584:     }
1.1       matthew   585:     return;
                    586: }
                    587: 
                    588: sub set_row_numbers {
                    589:     my $self = shift;
                    590:     while (my ($cell,$formula) = each(%{$self->{'formulas'}})) {
                    591:         next if ($cell !~ /^A(\d+)/);
                    592:         my $row = $1;
                    593:         next if ($row == 0);
                    594:         my ($symb,undef) = split('__&&&__',$formula);
                    595:         $self->{'row_numbers'}->{$symb} = $row;
1.17      matthew   596:         $self->{'maxrow'} = $row if ($row > $self->{'maxrow'});
1.1       matthew   597:     }
                    598: }
                    599: 
                    600: sub get_row_number_from_symb {
                    601:     my $self = shift;
                    602:     my ($key) = @_;
                    603:     ($key,undef) = split('__&&&__',$key) if ($key =~ /__&&&__/);
                    604:     return $self->get_row_number_from_key($key);
                    605: }
                    606: 
                    607: #############################################
                    608: #############################################
                    609: 
                    610: =pod
                    611: 
                    612: =item &load_cached_export_rows
                    613: 
                    614: Retrieves and parsers the export rows of the student spreadsheets.
                    615: These rows are saved in the courses directory in the format:
                    616: 
                    617:  sname:sdom:studentcalc:.time => time
                    618: 
                    619:  sname:sdom:studentcalc => ___=___Adata___;___Bdata___;___Cdata___;___ .....
                    620: 
                    621: =cut
                    622: 
                    623: #############################################
                    624: #############################################
                    625: sub load_cached_export_rows {
1.17      matthew   626:     undef(%Exportrows);
1.1       matthew   627:     my @tmp = &Apache::lonnet::dump('nohist_calculatedsheets',
                    628: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    629: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'},undef);
                    630:     my %Selected_Assess_Sheet;
                    631:     if ($tmp[0] =~ /^error/) {
                    632:         &Apache::lonnet::logthis('unable to read cached student export rows '.
                    633:                                  'for course '.$ENV{'request.course.id'});
                    634:         return;
                    635:     }
                    636:     my %tmp = @tmp;
                    637:     while (my ($key,$sheetdata) = each(%tmp)) {
                    638:         my ($sname,$sdom,$sheettype,$remainder) = split(':',$key);
                    639:         my $student = $sname.':'.$sdom;
                    640:         if ($remainder =~ /\.time/) {
                    641:             $Exportrows{$student}->{'time'} = $sheetdata;
                    642:         } else {
                    643:             $sheetdata =~ s/^___=___//;
                    644:             my @Data = split('___;___',$sheetdata);
                    645:             $Exportrows{$student}->{'data'} = \@Data;
                    646:         }
                    647:     }
                    648: }
                    649: 
                    650: #############################################
                    651: #############################################
                    652: 
                    653: =pod
                    654: 
                    655: =item &save_export_data()
                    656: 
                    657: Writes the export data for this student to the course cache.
                    658: 
                    659: =cut
                    660: 
                    661: #############################################
                    662: #############################################
                    663: sub save_export_data {
                    664:     my $self = shift;
1.20      matthew   665:     &Apache::assesscalc::save_cached_export_rows($self->{'name'},
                    666:                                                  $self->{'domain'});
1.5       matthew   667:     return if ($self->temporary());
1.1       matthew   668:     my $student = $self->{'name'}.':'.$self->{'domain'};
                    669:     return if (! exists($Exportrows{$student}));
                    670:     return if (! $self->is_default());
                    671:     my $key = join(':',($self->{'name'},$self->{'domain'},'studentcalc')).':';
                    672:     my $timekey = $key.'.time';
                    673:     my $newstore = join('___;___',
                    674:                         @{$Exportrows{$student}->{'data'}});
                    675:     $newstore = '___=___'.$newstore;
1.16      matthew   676:     my $result= &Apache::lonnet::put('nohist_calculatedsheets',
1.1       matthew   677:                          { $key     => $newstore,
                    678:                            $timekey => $Exportrows{$student}->{'time'} },
                    679:                          $self->{'cdom'},
                    680:                          $self->{'cnum'});
                    681:     return;
                    682: }
                    683: 
                    684: #############################################
                    685: #############################################
                    686: 
                    687: =pod
                    688: 
                    689: =item &export_data()
                    690: 
                    691: Returns the export data associated with the spreadsheet.  Computes the
                    692: spreadsheet only if necessary.
                    693: 
                    694: =cut
                    695: 
                    696: #############################################
                    697: #############################################
                    698: sub export_data {
                    699:     my $self = shift;
1.18      matthew   700:     my ($r) = @_;
                    701:     my $connection = $r->connection();
1.1       matthew   702:     my $student = $self->{'name'}.':'.$self->{'domain'};
                    703:     if (! exists($Exportrows{$student}) ||
1.15      matthew   704:         ! defined($Exportrows{$student}) ||
1.16      matthew   705:         ! exists($Exportrows{$student}->{'data'}) ||
1.15      matthew   706:         ! defined($Exportrows{$student}->{'data'}) ||
1.16      matthew   707:         ! exists($Exportrows{$student}->{'time'}) ||
                    708:         ! defined($Exportrows{$student}->{'time'}) ||
1.1       matthew   709:         ! $self->check_expiration_time($Exportrows{$student}->{'time'})) {
1.18      matthew   710:         $self->compute($r);
1.1       matthew   711:     }
1.18      matthew   712:     if ($connection->aborted()) { $self->cleanup(); return; }
1.1       matthew   713:     my @Data = @{$Exportrows{$student}->{'data'}};
                    714:     for (my $i=0; $i<=$#Data;$i++) {
                    715:         $Data[$i]="'".$Data[$i]."'" if ($Data[$i]=~/\D/ && defined($Data[$i]));
                    716:     }
                    717:     return @Data;
                    718: }
                    719: 
                    720: 1;
                    721: 
                    722: __END__

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