File:  [LON-CAPA] / loncom / interface / spreadsheet / studentcalc.pm
Revision 1.26: download - view: text, annotated - select for diffs
Tue Nov 2 20:48:02 2004 UTC (19 years, 7 months ago) by albertel
Branches: MAIN
CVS tags: version_1_2_99_0, HEAD
consolidate some of the multitude of functions out there that try to format and display the student's 'fullname'

    1: #
    2: # $Id: studentcalc.pm,v 1.26 2004/11/02 20:48:02 albertel Exp $
    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: 
   48: use warnings FATAL=>'all';
   49: no warnings 'uninitialized';
   50: 
   51: use strict;
   52: use Apache::Constants qw(:common :http);
   53: use Apache::lonnet;
   54: use Apache::loncommon();
   55: use Apache::loncoursedata();
   56: use Apache::lonnavmaps;
   57: use Apache::Spreadsheet();
   58: use Apache::assesscalc();
   59: use HTML::Entities();
   60: use Time::HiRes;
   61: use Apache::lonlocal;
   62: 
   63: @Apache::studentcalc::ISA = ('Apache::Spreadsheet');
   64: 
   65: my @Sequences = ();
   66: my %Exportrows = ();
   67: 
   68: my $current_course;
   69: 
   70: sub initialize {
   71:     &Apache::assesscalc::initialize();
   72:     &initialize_sequence_cache();
   73: }
   74: 
   75: sub initialize_package {
   76:     $current_course = $ENV{'request.course.id'};
   77:     &initialize_sequence_cache();
   78:     &load_cached_export_rows();
   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;
   87: }
   88: 
   89: sub initialize_sequence_cache {
   90:     #
   91:     # Set up the sequences and assessments
   92:     undef(@Sequences);
   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
   97:         &Apache::lonnet::logthis('top is undefined (studentcalc.pm)');
   98:         return;
   99:     }
  100:     @Sequences = @{$sequences} if (ref($sequences) eq 'ARRAY');
  101: }
  102: 
  103: sub clear_package {
  104:     undef(@Sequences);
  105:     undef(%Exportrows);
  106:     &Apache::assesscalc::clear_package();
  107: }
  108: 
  109: sub get_title {
  110:     my $self = shift;
  111:     my @title = ();
  112:     #
  113:     # Determine the students name
  114:     my $name = &Apache::loncommon::plainname($self->{'name'},
  115: 					     $self->{'domain'});
  116:     push (@title,$name);
  117:     push (@title,$self->{'coursedesc'});
  118:     push (@title,&Apache::lonlocal::locallocaltime(time));
  119:     return @title;
  120: }
  121: 
  122: sub get_html_title {
  123:     my $self = shift;
  124:     my ($name,$desc,$time) = $self->get_title();
  125:     my $title = '<h1>'.$name;
  126:     if ($ENV{'user.name'} ne $self->{'name'} && 
  127:         $ENV{'user.domain'} ne $self->{'domain'}) {
  128:         $title .= ' '.&Apache::loncommon::aboutmewrapper
  129:                                     ($self->{'name'}.'@'.$self->{'domain'},
  130:                                      $self->{'name'},$self->{'domain'});
  131:     }
  132:     $title .= "</h1>\n";
  133:     $title .= '<h2>'.$desc."</h2>\n";
  134:     $title .= '<h3>'.$time.'</h3>';
  135:     return $title;
  136: }
  137: 
  138: sub parent_link {
  139:     my $self = shift;
  140:     return '<p><a href="/adm/classcalc">'.&mt('Course level sheet').'</a></p>'."\n";
  141: }
  142: 
  143: sub convenience_links {
  144:     my $self = shift;
  145:     my ($resource) = @_;
  146:     my $symb = &Apache::lonnet::escape($resource->{'symb'});
  147:     my $result = <<"END";
  148: <a href="/adm/grades?symb=$symb&command=submission" target="LONcatInfo">
  149:     <img src="/adm/lonMisc/subm_button.gif" border=0 />
  150:     </a>
  151: <a href="/adm/grades?symb=$symb&command=gradingmenu" target="LONcatInfo">
  152:     <img src="/adm/lonMisc/pgrd_button.gif" border=0 />
  153:     </a>
  154: <a href="/adm/parmset?symb=$symb" target="LONcatInfo">
  155:     <img src="/adm/lonMisc/pprm_button.gif" border=0 />
  156:     </a>
  157: END
  158:     return $result;
  159: }
  160: 
  161: sub outsheet_html {
  162:     my $self = shift;
  163:     my ($r) = @_;
  164:     my $importcolor = '#FFFFAA';
  165:     my $exportcolor = '#88FF88';
  166:     ####################################
  167:     # Get the list of assessment files #
  168:     ####################################
  169:     my @AssessFileNames = $self->othersheets('assesscalc');
  170:     my $editing_is_allowed = &Apache::lonnet::allowed('mgr',
  171:                                                 $ENV{'request.course.id'});
  172:     ####################################
  173:     # Report any calculation errors    #
  174:     ####################################
  175:     $r->print($self->html_report_error());
  176:     ####################################
  177:     # Determine table structure        #
  178:     ####################################
  179:     my $num_uneditable = 26;
  180:     my $num_left = 52-$num_uneditable;
  181:     my %lt=&Apache::lonlocal::texthash(
  182: 				       'st' => 'Student',
  183: 				       'im' => 'Import',
  184: 				       'ca' => 'Calculations',
  185: 				       'as' => 'Assessment',
  186: 				       'ro' => 'Row',
  187: 				       );
  188:     my $tableheader =<<"END";
  189: <p>
  190: <table border="2">
  191: <tr>
  192:   <th colspan="2" rowspan="2"><font size="+2">$lt{'st'}</font></th>
  193:   <td bgcolor="$importcolor" colspan="$num_uneditable">
  194:       <b><font size="+1">$lt{'im'}</font></b></td>
  195:   <td colspan="$num_left">
  196:       <b><font size="+1">$lt{'ca'}</font></b></td>
  197: </tr><tr>
  198: END
  199:     my $label_num = 0;
  200:     foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
  201:         if ($label_num<$num_uneditable) { 
  202:             $tableheader .='<td bgcolor="'.$importcolor.'">';
  203:         } else {
  204:             $tableheader .='<td>';
  205:         }
  206:         $tableheader .="<b><font size=+1>$_</font></b></td>";
  207:         $label_num++;
  208:     }
  209:     $tableheader .="</tr>\n";
  210:     if ($self->blackout()) {
  211:         $r->print('<font color="red" size="+2"><p>'.
  212:                   &mt('Some computations are not available at this time.').'<br />'.
  213:                   &mt('There are problems whose status you are not allowed to view.').
  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>'.
  221:                       $self->html_template_row($num_uneditable,
  222:                                                $importcolor)."</tr>\n");
  223:         }
  224:         #
  225:         # Print out summary/export row
  226:         $r->print('<tr><td>'.&mt('Summary').'</td><td>0</td>'.
  227:                   $self->html_export_row($exportcolor)."</tr>\n");
  228:     }
  229:     $r->print("</table>\n");
  230:     #
  231:     # Prepare to output rows
  232:     if (exists($ENV{'request.role.adv'}) && $ENV{'request.role.adv'}) {
  233:         $tableheader =<<"END";
  234: </p><p>
  235: <table border="2">
  236: <tr><th>$lt{'ro'}</th><th>&nbsp;</th><th>$lt{'as'}</th>
  237: END
  238:     } else {
  239:         $tableheader =<<"END";
  240: </p><p>
  241: <table border="2">
  242: <tr><th>&nbsp;</th><th>$lt{'as'}</th>
  243: END
  244:     }
  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};
  267:             my $row_output = '<tr>';
  268:             if ($editing_is_allowed) {
  269:                 $row_output .= '<td>'.$rownum.'</td>';
  270:                 $row_output .= '<td>'.$self->convenience_links($resource).'</td>';
  271:                 $row_output .= '<td>'.
  272:                     '<a href="/adm/assesscalc?sname='.$self->{'name'}.
  273:                     '&sdomain='.$self->{'domain'}.
  274:                     '&filename='.$assess_filename.
  275:                     '&usymb='.&Apache::lonnet::escape($resource->{'symb'}).
  276:                     '">'.$resource->{'title'}.'</a><br />';
  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='.
  283:                     &Apache::lonnet::escape($resource->{'symb'}).
  284:                     '">Go&nbsp;To</a>';
  285:                 $row_output .= '</td><td>'.$resource->{'title'}.'</td>';
  286:             }
  287:             if ($self->blackout() && $self->{'blackout_rows'}->{$rownum}>0) {
  288:                 $row_output .= 
  289:                     '<td colspan="52">'.&mt('Unavailable at this time').'</td></tr>'."\n";
  290:             } else {
  291:                 $row_output .= $self->html_row($num_uneditable,$rownum,
  292:                                                $exportcolor,$importcolor).
  293:                     "</tr>\n";
  294:             }
  295:             $r->print($row_output);
  296: 	}
  297: 	$r->print("</table>\n");
  298:     }
  299:     $r->print("</p>\n");
  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)=@_;
  316:     if (!defined($AssessFiles) || ! @$AssessFiles) {
  317:         return '';
  318:     }
  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: 
  358: sub csv_rows {
  359:     # writes the meat of the spreadsheet to an excel worksheet.  Called
  360:     # by Spreadsheet::outsheet_excel;
  361:     my $self = shift;
  362:     my ($connection,$filehandle) = @_;
  363:     #
  364:     # Write a header row
  365:     $self->csv_output_row($filehandle,undef,
  366:                           (&mt('Sequence or Folder'),&mt('Assessment title')));
  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;
  382: }
  383: 
  384: sub excel_rows {
  385:     # writes the meat of the spreadsheet to an excel worksheet.  Called
  386:     # by Spreadsheet::outsheet_excel;
  387:     my $self = shift;
  388:     my ($connection,$worksheet,$cols_output,$rows_output) = @_;
  389:     #
  390:     # Write a header row
  391:     $cols_output = 0;
  392:     foreach my $value ('Container','Assessment title') {
  393:         $worksheet->write($rows_output,$cols_output++,&mt($value));
  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;
  412: }
  413: 
  414: sub outsheet_recursive_excel {
  415:     my $self = shift;
  416:     my ($r) = @_;
  417: } 
  418: 
  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: ##
  496: sub compute {
  497:     my $self = shift;
  498:     my ($r) = @_;
  499:     my $connection = $r->connection();
  500:     if ($connection->aborted()) { $self->cleanup; return; }
  501:     if (! defined($current_course) ||
  502:         $current_course ne $ENV{'request.course.id'} ||
  503:         ! @Sequences ) {
  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();
  515:     foreach my $seq (@Sequences) {
  516:         next if ($seq->{'num_assess'}<1);
  517:         foreach my $resource (@{$seq->{'contents'}}) {
  518:             if ($connection->aborted()) { $self->cleanup(); return; }
  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;
  529:             if ($connection->aborted()) { $self->cleanup(); return; }
  530:             my $assessSheet;
  531:                 $assessSheet = Apache::assesscalc->new($self->{'name'},
  532:                                                        $self->{'domain'},
  533:                                                        $assess_filename,
  534:                                                        $resource->{'symb'});
  535:             my @exportdata = $assessSheet->export_data($r);
  536:             #
  537:             if ($assessSheet->badcalc()) {
  538:                 $self->set_calcerror(
  539:             &mt('Error computing row for assessment "[_1]" (row [_2]):[_3]',
  540:                 $assessSheet->get_title(),$rownum,$assessSheet->calcerror()));
  541:             }
  542:             #
  543:             if ($connection->aborted()) { $self->cleanup(); return; }
  544:             if ($assessSheet->blackout()) {
  545:                 $self->blackout(1);
  546:                 $self->{'blackout_rows'}->{$rownum} = 1;
  547:             }
  548:             #
  549:             # Be sure not to disturb the formulas in the 'A' column
  550:             my $data = shift(@exportdata);
  551:             $c{$cell} = $data if (defined($data));
  552:             #
  553:             # Deal with the remaining columns
  554:             my $i=0;
  555:             foreach (split(//,'BCDEFGHIJKLMNOPQRSTUVWXYZ')) {
  556:                 my $cell = $_.$rownum;
  557:                 my $data = shift(@exportdata);
  558:                 if (defined($data)) {
  559:                     $f{$cell} = 'import';
  560:                     $c{$cell} = $data;
  561:                 }
  562:                 $i++;
  563:             }
  564:         }
  565:     }
  566:     $self->constants(\%c);
  567:     $self->formulas(\%f);
  568:     $self->put_sequence_data_in_safe_space();
  569:     $self->calcsheet();
  570:     $self->remove_sequence_data_from_safe_space();
  571:     #
  572:     # Store export row in cache
  573:     my @exportarray=$self->exportrow();
  574:     my $student = $self->{'name'}.':'.$self->{'domain'};
  575:     $Exportrows{$student}->{'time'} = time;
  576:     $Exportrows{$student}->{'data'} = \@exportarray;
  577:     # save export row
  578:     $self->save_export_data();
  579:     #
  580:     $self->save() if ($self->need_to_save());
  581:     return;
  582: }
  583: 
  584: sub set_row_sources {
  585:     my $self = shift;
  586:     while (my ($cell,$value) = each(%{$self->{'formulas'}})) {
  587:         next if ($cell !~ /^A(\d+)$/ || $1 < 1);
  588:         my $row = $1;
  589:         (undef,$value) = split('__&&&__',$value);
  590:         $value = 'Default' if (! defined($value));
  591:         $self->{'row_source'}->{$row} = $value;
  592:     }
  593:     return;
  594: }
  595: 
  596: sub set_row_numbers {
  597:     my $self = shift;
  598:     while (my ($cell,$formula) = each(%{$self->{'formulas'}})) {
  599:         next if ($cell !~ /^A(\d+)/);
  600:         my $row = $1;
  601:         next if ($row == 0);
  602:         my ($symb,undef) = split('__&&&__',$formula);
  603:         $self->{'row_numbers'}->{$symb} = $row;
  604:         $self->{'maxrow'} = $row if ($row > $self->{'maxrow'});
  605:     }
  606: }
  607: 
  608: sub get_row_number_from_symb {
  609:     my $self = shift;
  610:     my ($key) = @_;
  611:     ($key,undef) = split('__&&&__',$key) if ($key =~ /__&&&__/);
  612:     return $self->get_row_number_from_key($key);
  613: }
  614: 
  615: #############################################
  616: #############################################
  617: 
  618: =pod
  619: 
  620: =item &load_cached_export_rows
  621: 
  622: Retrieves and parsers the export rows of the student spreadsheets.
  623: These rows are saved in the courses directory in the format:
  624: 
  625:  sname:sdom:studentcalc:.time => time
  626: 
  627:  sname:sdom:studentcalc => ___=___Adata___;___Bdata___;___Cdata___;___ .....
  628: 
  629: =cut
  630: 
  631: #############################################
  632: #############################################
  633: sub load_cached_export_rows {
  634:     undef(%Exportrows);
  635:     my @tmp = &Apache::lonnet::dump('nohist_calculatedsheets',
  636: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  637: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'},undef);
  638:     my %Selected_Assess_Sheet;
  639:     if ($tmp[0] =~ /^error/) {
  640:         &Apache::lonnet::logthis('unable to read cached student export rows '.
  641:                                  'for course '.$ENV{'request.course.id'});
  642:         return;
  643:     }
  644:     my %tmp = @tmp;
  645:     while (my ($key,$sheetdata) = each(%tmp)) {
  646:         my ($sname,$sdom,$sheettype,$remainder) = split(':',$key);
  647:         my $student = $sname.':'.$sdom;
  648:         if ($remainder =~ /\.time/) {
  649:             $Exportrows{$student}->{'time'} = $sheetdata;
  650:         } else {
  651:             $sheetdata =~ s/^___=___//;
  652:             my @Data = split('___;___',$sheetdata);
  653:             $Exportrows{$student}->{'data'} = \@Data;
  654:         }
  655:     }
  656: }
  657: 
  658: #############################################
  659: #############################################
  660: 
  661: =pod
  662: 
  663: =item &save_export_data()
  664: 
  665: Writes the export data for this student to the course cache.
  666: 
  667: =cut
  668: 
  669: #############################################
  670: #############################################
  671: sub save_export_data {
  672:     my $self = shift;
  673:     my $student = $self->{'name'}.':'.$self->{'domain'};
  674:     return if ($self->temporary());
  675:     if ($self->badcalc()){
  676:         # do not save data away when calculations have not been done properly.
  677:         delete($Exportrows{$student});
  678:         return;
  679:     }
  680:     return if (! exists($Exportrows{$student}));
  681:     &Apache::assesscalc::save_cached_export_rows($self->{'name'},
  682:                                                  $self->{'domain'});
  683:     return if (! $self->is_default());
  684:     my $key = join(':',($self->{'name'},$self->{'domain'},'studentcalc')).':';
  685:     my $timekey = $key.'.time';
  686:     my $newstore = join('___;___',
  687:                         @{$Exportrows{$student}->{'data'}});
  688:     $newstore = '___=___'.$newstore;
  689:     my $result= &Apache::lonnet::put('nohist_calculatedsheets',
  690:                          { $key     => $newstore,
  691:                            $timekey => $Exportrows{$student}->{'time'} },
  692:                          $self->{'cdom'},
  693:                          $self->{'cnum'});
  694:     return;
  695: }
  696: 
  697: #############################################
  698: #############################################
  699: 
  700: =pod
  701: 
  702: =item &export_data()
  703: 
  704: Returns the export data associated with the spreadsheet.  Computes the
  705: spreadsheet only if necessary.
  706: 
  707: =cut
  708: 
  709: #############################################
  710: #############################################
  711: sub export_data {
  712:     my $self = shift;
  713:     my ($r) = @_;
  714:     my $connection = $r->connection();
  715:     my $student = $self->{'name'}.':'.$self->{'domain'};
  716:     if (! exists($Exportrows{$student}) ||
  717:         ! defined($Exportrows{$student}) ||
  718:         ! exists($Exportrows{$student}->{'data'}) ||
  719:         ! defined($Exportrows{$student}->{'data'}) ||
  720:         ! exists($Exportrows{$student}->{'time'}) ||
  721:         ! defined($Exportrows{$student}->{'time'}) ||
  722:         ! $self->check_expiration_time($Exportrows{$student}->{'time'})) {
  723:         $self->compute($r);
  724:     }
  725:     if ($connection->aborted()) { $self->cleanup(); return; }
  726:     my @Data;
  727:     if ($self->badcalc()) {
  728:         @Data = ();
  729:     } else {
  730:         @Data = @{$Exportrows{$student}->{'data'}};
  731:         for (my $i=0; $i<=$#Data;$i++) {
  732:             if ($Data[$i]=~/\D/ && defined($Data[$i])) {
  733:                 $Data[$i]="'".$Data[$i]."'";
  734:             }
  735:         }
  736:     }
  737:     return @Data;
  738: }
  739: 
  740: 1;
  741: 
  742: __END__

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