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

1.1       matthew     1: #
1.20    ! matthew     2: # $Id: studentcalc.pm,v 1.19 2003/09/08 18:25:26 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 Spreadsheet::WriteExcel;
                     61: use Time::HiRes;
                     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
                     92:     @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
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'});
                    122:     push (@title,scalar(localtime(time)));
                    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;
                    144:     my $link .= '<p><a href="/adm/classcalc?'.
                    145:         'sname='.$self->{'name'}.
                    146:             '&sdomain='.$self->{'domain'}.'">'.
                    147:                 'Course level sheet</a></p>'."\n";
                    148:     return $link;
                    149: }
                    150: 
1.14      matthew   151: sub convenience_links {
                    152:     my $self = shift;
                    153:     my ($resource) = @_;
                    154:     my $symb = &Apache::lonnet::escape($resource->{'symb'});
                    155:     my $result = <<"END";
                    156: <a href="/adm/grades?symb=$symb&command=submission" target="LONcatInfo">
                    157:     <img src="/adm/lonMisc/subm_button.gif" border=0 />
                    158:     </a>
                    159: <a href="/adm/grades?symb=$symb&command=gradingmenu" target="LONcatInfo">
                    160:     <img src="/adm/lonMisc/pgrd_button.gif" border=0 />
                    161:     </a>
                    162: <a href="/adm/parmset?symb=$symb" target="LONcatInfo">
                    163:     <img src="/adm/lonMisc/pprm_button.gif" border=0 />
                    164:     </a>
                    165: END
                    166:     return $result;
                    167: }
                    168: 
1.1       matthew   169: sub outsheet_html {
                    170:     my $self = shift;
                    171:     my ($r) = @_;
1.13      matthew   172:     my $importcolor = '#FFFFAA';
1.12      matthew   173:     my $exportcolor = '#88FF88';
1.1       matthew   174:     ####################################
                    175:     # Get the list of assessment files #
                    176:     ####################################
                    177:     my @AssessFileNames = $self->othersheets('assesscalc');
1.2       matthew   178:     my $editing_is_allowed = &Apache::lonnet::allowed('mgr',
                    179:                                                 $ENV{'request.course.id'});
1.1       matthew   180:     ####################################
                    181:     # Determine table structure        #
                    182:     ####################################
                    183:     my $num_uneditable = 26;
                    184:     my $num_left = 52-$num_uneditable;
                    185:     my $tableheader =<<"END";
1.2       matthew   186: <p>
1.1       matthew   187: <table border="2">
                    188: <tr>
                    189:   <th colspan="2" rowspan="2"><font size="+2">Student</font></th>
1.12      matthew   190:   <td bgcolor="$importcolor" colspan="$num_uneditable">
1.1       matthew   191:       <b><font size="+1">Import</font></b></td>
                    192:   <td colspan="$num_left">
                    193:       <b><font size="+1">Calculations</font></b></td>
                    194: </tr><tr>
                    195: END
                    196:     my $label_num = 0;
                    197:     foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
                    198:         if ($label_num<$num_uneditable) { 
1.12      matthew   199:             $tableheader .='<td bgcolor="'.$importcolor.'">';
1.1       matthew   200:         } else {
                    201:             $tableheader .='<td>';
                    202:         }
                    203:         $tableheader .="<b><font size=+1>$_</font></b></td>";
                    204:         $label_num++;
                    205:     }
                    206:     $tableheader .="</tr>\n";
1.4       matthew   207:     if ($self->blackout()) {
                    208:         $r->print('<font color="red" size="+2"><p>'.
                    209:                   'Some computations are not available at this time.<br />'.
                    210:                   'There are problems whose status you are allowed to view.'.
                    211:                   '</font></p>'."\n");
                    212:     } else {
                    213:         $r->print($tableheader);
                    214:         #
                    215:         # Print out template row
                    216:         if (exists($ENV{'request.role.adv'}) && $ENV{'request.role.adv'}) {
                    217:             $r->print('<tr><td>Template</td><td>&nbsp;</td>'.
1.12      matthew   218:                       $self->html_template_row($num_uneditable,
                    219:                                                $importcolor)."</tr>\n");
1.4       matthew   220:         }
                    221:         #
                    222:         # Print out summary/export row
                    223:         $r->print('<tr><td>Summary</td><td>0</td>'.
1.12      matthew   224:                   $self->html_export_row($exportcolor)."</tr>\n");
1.4       matthew   225:     }
1.1       matthew   226:     $r->print("</table>\n");
                    227:     #
                    228:     # Prepare to output rows
1.4       matthew   229:     if (exists($ENV{'request.role.adv'}) && $ENV{'request.role.adv'}) {
                    230:         $tableheader =<<"END";
1.2       matthew   231: </p><p>
1.1       matthew   232: <table border="2">
1.14      matthew   233: <tr><th>Row</th><th>&nbsp;</th><th>Assessment</th>
1.1       matthew   234: END
1.4       matthew   235:     } else {
                    236:         $tableheader =<<"END";
                    237: </p><p>
                    238: <table border="2">
                    239: <tr><th>&nbsp;</th><th>Assessment</th>
                    240: END
                    241:     }
1.1       matthew   242:     foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
                    243: 	if ($label_num<$num_uneditable) { 
                    244:             $tableheader.='<td bgcolor="#FFDDDD">';
                    245:         } else {
                    246:             $tableheader.='<td>';
                    247:         }
                    248:         $tableheader.="<b><font size=+1>$_</font></b></td>";
                    249:     }
                    250:     $tableheader.="\n";
                    251:     #
                    252:     my $num_output = 1;
                    253:     if (scalar(@Sequences)< 1) {
                    254:         &initialize_sequence_cache();
                    255:     }
                    256:     foreach my $Sequence (@Sequences) {
                    257: 	next if ($Sequence->{'num_assess'} < 1);
                    258: 	$r->print("<h3>".$Sequence->{'title'}."</h3>\n");
                    259:  	$r->print($tableheader);
                    260: 	foreach my $resource (@{$Sequence->{'contents'}}) {
                    261: 	    next if ($resource->{'type'} ne 'assessment');
                    262: 	    my $rownum = $self->get_row_number_from_key($resource->{'symb'});
                    263:             my $assess_filename = $self->{'row_source'}->{$rownum};
1.2       matthew   264:             my $row_output = '<tr>';
                    265:             if ($editing_is_allowed) {
                    266:                 $row_output .= '<td>'.$rownum.'</td>';
1.14      matthew   267:                 $row_output .= '<td>'.$self->convenience_links($resource).'</td>';
1.2       matthew   268:                 $row_output .= '<td>'.
                    269:                     '<a href="/adm/assesscalc?sname='.$self->{'name'}.
                    270:                     '&sdomain='.$self->{'domain'}.
                    271:                     '&filename='.$assess_filename.
1.4       matthew   272:                     '&usymb='.&Apache::lonnet::escape($resource->{'symb'}).
                    273:                     '">'.$resource->{'title'}.'</a><br />';
1.2       matthew   274:                 $row_output .= &assess_file_selector($rownum,
                    275:                                                      $assess_filename,
                    276:                                                      \@AssessFileNames).
                    277:                                                          '</td>';
                    278:             } else {
                    279:                 $row_output .= '<td><a href="'.$resource->{'src'}.'?symb='.
1.4       matthew   280:                     &Apache::lonnet::escape($resource->{'symb'}).
                    281:                     '">Go&nbsp;To</a>';
1.2       matthew   282:                 $row_output .= '</td><td>'.$resource->{'title'}.'</td>';
                    283:             }
1.4       matthew   284:             if ($self->blackout() && $self->{'blackout_rows'}->{$rownum}>0) {
                    285:                 $row_output .= 
                    286:                     '<td colspan="52">Unavailable at this time</td></tr>'."\n";
                    287:             } else {
1.12      matthew   288:                 $row_output .= $self->html_row($num_uneditable,$rownum,
                    289:                                                $exportcolor,$importcolor).
1.4       matthew   290:                     "</tr>\n";
                    291:             }
1.1       matthew   292:             $r->print($row_output);
                    293: 	}
                    294: 	$r->print("</table>\n");
                    295:     }
1.2       matthew   296:     $r->print("</p>\n");
1.1       matthew   297:     return;
                    298: }
                    299: 
                    300: ########################################################
                    301: ########################################################
                    302: 
                    303: =pod
                    304: 
                    305: =item &assess_file_selector()
                    306: 
                    307: =cut
                    308: 
                    309: ########################################################
                    310: ########################################################
                    311: sub assess_file_selector {
                    312:     my ($row,$default,$AssessFiles)=@_;
1.2       matthew   313:     if (!defined($AssessFiles) || ! @$AssessFiles) {
                    314:         return '';
                    315:     }
1.1       matthew   316:     return '' if (! &Apache::lonnet::allowed('mgr',$ENV{'request.course.id'}));
                    317:     my $element_name = 'FileSelect_'.$row;
                    318:     my $load_dialog = '<select size="1" name="'.$element_name.'" '.
                    319:         'onchange="'.
                    320:         "document.sheet.cell.value='source_$row';".
                    321:         "document.sheet.newformula.value=document.sheet.$element_name\.value;".
                    322:         'document.sheet.submit()" '.'>'."\n";
                    323:     foreach my $file (@{$AssessFiles}) {
                    324:         $load_dialog .= '    <option name="'.$file.'"';
                    325:         $load_dialog .= ' selected' if ($default eq $file);
                    326:         $load_dialog .= '>'.$file."</option>\n";
                    327:     }
                    328:     $load_dialog .= "</select>\n";
                    329:     return $load_dialog;
                    330: }
                    331: 
                    332: sub modify_cell {
                    333:     my $self = shift;
                    334:     my ($cell,$formula) = @_;
                    335:     if ($cell =~ /^source_(\d+)$/) {
                    336:         # Need to make sure $formula is a valid filename....
                    337:         my $row = $1;
                    338:         $cell = 'A'.$row;
                    339:         $self->{'row_source'}->{$row} = $formula;
                    340:         my $original_source = $self->formula($cell);
                    341:         if ($original_source =~ /__&&&__/) {
                    342:             ($original_source,undef) = split('__&&&__',$original_source);
                    343:         }
                    344:         $formula = $original_source.'__&&&__'.$formula;
                    345:     } elsif ($cell =~ /([A-z])\-/) {
                    346:         $cell = 'template_'.$1;
                    347:     } elsif ($cell !~ /^([A-z](\d+)|template_[A-z])$/) {
                    348:         return;
                    349:     }
                    350:     $self->set_formula($cell,$formula);
                    351:     $self->rebuild_stats();
                    352:     return;
                    353: }
                    354: 
1.7       matthew   355: sub csv_rows {
                    356:     # writes the meat of the spreadsheet to an excel worksheet.  Called
                    357:     # by Spreadsheet::outsheet_excel;
1.1       matthew   358:     my $self = shift;
1.7       matthew   359:     my ($filehandle) = @_;
                    360:     #
                    361:     # Write a header row
                    362:     $self->csv_output_row($filehandle,undef,
1.10      matthew   363:                           ('Sequence or Folder','Assessment title'));
1.7       matthew   364:     #
                    365:     # Write each assessments row
                    366:     if (scalar(@Sequences)< 1) {
                    367:         &initialize_sequence_cache();
                    368:     }
                    369:     foreach my $Sequence (@Sequences) {
                    370: 	next if ($Sequence->{'num_assess'} < 1);
                    371: 	foreach my $resource (@{$Sequence->{'contents'}}) {
                    372: 	    my $rownum = $self->get_row_number_from_key($resource->{'symb'});
                    373:             my @assessdata = ($Sequence->{'title'},
                    374:                               $resource->{'title'});
                    375:             $self->csv_output_row($filehandle,$rownum,@assessdata);
                    376:         }
                    377:     }
                    378:     return;
1.1       matthew   379: }
1.6       matthew   380: 
                    381: sub excel_rows {
                    382:     # writes the meat of the spreadsheet to an excel worksheet.  Called
                    383:     # by Spreadsheet::outsheet_excel;
1.1       matthew   384:     my $self = shift;
1.6       matthew   385:     my ($worksheet,$cols_output,$rows_output) = @_;
                    386:     #
                    387:     # Write a header row
                    388:     $cols_output = 0;
                    389:     foreach my $value ('Container','Assessment title') {
                    390:         $worksheet->write($rows_output,$cols_output++,$value);
                    391:     }
                    392:     $rows_output++;    
                    393:     #
                    394:     # Write each assessments row
                    395:     if (scalar(@Sequences)< 1) {
                    396:         &initialize_sequence_cache();
                    397:     }
                    398:     foreach my $Sequence (@Sequences) {
                    399: 	next if ($Sequence->{'num_assess'} < 1);
                    400: 	foreach my $resource (@{$Sequence->{'contents'}}) {
                    401: 	    my $rownum = $self->get_row_number_from_key($resource->{'symb'});
                    402:             my @assessdata = ($Sequence->{'title'},
                    403:                               $resource->{'title'});
                    404:             $self->excel_output_row($worksheet,$rownum,$rows_output++,
                    405:                                     @assessdata);
                    406:         }
                    407:     }
                    408:     return;
1.1       matthew   409: }
1.6       matthew   410: 
1.1       matthew   411: sub outsheet_recursive_excel {
                    412:     my $self = shift;
                    413:     my ($r) = @_;
                    414: } 
                    415: 
                    416: sub compute {
                    417:     my $self = shift;
1.18      matthew   418:     my ($r) = @_;
                    419:     my $connection = $r->connection();
                    420:     if ($connection->aborted()) { $self->cleanup; return; }
1.1       matthew   421:     if (! defined($current_course) ||
                    422:         $current_course ne $ENV{'request.course.id'}) {
                    423:         $current_course = $ENV{'request.course.id'};
                    424:         &clear_package();
                    425:         &initialize_sequence_cache();
                    426:     }
                    427:     $self->initialize_safe_space();
                    428:     my @sequences = @Sequences;
                    429:     if (@sequences < 1) {
                    430:         my ($top,$sequences,$assessments) = 
                    431:             &Apache::loncoursedata::get_sequence_assessment_data();
                    432:         if (! defined($top) || ! ref($top)) {
                    433:             &Apache::lonnet::logthis('top is undefined');
                    434:             return;
                    435:         }
                    436:         @sequences = @{$sequences} if (ref($sequences) eq 'ARRAY');
                    437:     }
                    438:     &Apache::assesscalc::initialize_package($self->{'name'},$self->{'domain'});
                    439:     my %f = $self->formulas();
                    440:     #
                    441:     # Process the formulas list - 
                    442:     #   the formula for the A column of a row is symb__&&__filename
                    443:     my %c = $self->constants();
                    444:     foreach my $seq (@sequences) {
                    445:         next if ($seq->{'num_assess'}<1);
                    446:         foreach my $resource (@{$seq->{'contents'}}) {
1.18      matthew   447:             if ($connection->aborted()) { $self->cleanup(); return; }
1.1       matthew   448:             next if ($resource->{'type'} ne 'assessment');
                    449:             my $rownum = $self->get_row_number_from_key($resource->{'symb'});
                    450:             my $cell = 'A'.$rownum;
                    451:             my $assess_filename = 'Default';
                    452:             if (exists($self->{'row_source'}->{$rownum})) {
                    453:                 $assess_filename = $self->{'row_source'}->{$rownum};
                    454:             } else {
                    455:                 $self->{'row_source'}->{$rownum} = $assess_filename;
                    456:             }
                    457:             $f{$cell} = $resource->{'symb'}.'__&&&__'.$assess_filename;
1.18      matthew   458:             if ($connection->aborted()) { $self->cleanup(); return; }
1.1       matthew   459:             my $assessSheet = Apache::assesscalc->new($self->{'name'},
                    460:                                                       $self->{'domain'},
                    461:                                                       $assess_filename,
                    462:                                                       $resource->{'symb'});
1.18      matthew   463:             my @exportdata = $assessSheet->export_data($r);
                    464:             if ($connection->aborted()) { $self->cleanup(); return; }
1.4       matthew   465:             if ($assessSheet->blackout()) {
                    466:                 $self->blackout(1);
                    467:                 $self->{'blackout_rows'}->{$rownum} = 1;
                    468:             }
1.1       matthew   469:             #
                    470:             # Be sure not to disturb the formulas in the 'A' column
                    471:             my $data = shift(@exportdata);
                    472:             $c{$cell} = $data if (defined($data));
                    473:             #
                    474:             # Deal with the remaining columns
                    475:             my $i=0;
                    476:             foreach (split(//,'BCDEFGHIJKLMNOPQRSTUVWXYZ')) {
                    477:                 my $cell = $_.$rownum;
                    478:                 my $data = shift(@exportdata);
                    479:                 if (defined($data)) {
                    480:                     $f{$cell} = 'import';
                    481:                     $c{$cell} = $data;
                    482:                 }
                    483:                 $i++;
                    484:             }
                    485:         }
                    486:     }
                    487:     $self->constants(\%c);
                    488:     $self->formulas(\%f);
                    489:     $self->calcsheet();
                    490:     #
                    491:     # Store export row in cache
                    492:     my @exportarray=$self->exportrow();
                    493:     my $student = $self->{'name'}.':'.$self->{'domain'};
                    494:     $Exportrows{$student}->{'time'} = time;
                    495:     $Exportrows{$student}->{'data'} = \@exportarray;
                    496:     # save export row
                    497:     $self->save_export_data();
1.9       matthew   498:     #
                    499:     $self->save() if ($self->need_to_save());
                    500:     return;
                    501: }
                    502: 
                    503: sub set_row_sources {
                    504:     my $self = shift;
                    505:     while (my ($cell,$value) = each(%{$self->{'formulas'}})) {
1.17      matthew   506:         next if ($cell !~ /^A(\d+)$/ || $1 < 1);
1.9       matthew   507:         my $row = $1;
                    508:         (undef,$value) = split('__&&&__',$value);
                    509:         $value = 'Default' if (! defined($value));
                    510:         $self->{'row_source'}->{$row} = $value;
                    511:     }
1.1       matthew   512:     return;
                    513: }
                    514: 
                    515: sub set_row_numbers {
                    516:     my $self = shift;
                    517:     while (my ($cell,$formula) = each(%{$self->{'formulas'}})) {
                    518:         next if ($cell !~ /^A(\d+)/);
                    519:         my $row = $1;
                    520:         next if ($row == 0);
                    521:         my ($symb,undef) = split('__&&&__',$formula);
                    522:         $self->{'row_numbers'}->{$symb} = $row;
1.17      matthew   523:         $self->{'maxrow'} = $row if ($row > $self->{'maxrow'});
1.1       matthew   524:     }
                    525: }
                    526: 
                    527: sub get_row_number_from_symb {
                    528:     my $self = shift;
                    529:     my ($key) = @_;
                    530:     ($key,undef) = split('__&&&__',$key) if ($key =~ /__&&&__/);
                    531:     return $self->get_row_number_from_key($key);
                    532: }
                    533: 
                    534: #############################################
                    535: #############################################
                    536: 
                    537: =pod
                    538: 
                    539: =item &load_cached_export_rows
                    540: 
                    541: Retrieves and parsers the export rows of the student spreadsheets.
                    542: These rows are saved in the courses directory in the format:
                    543: 
                    544:  sname:sdom:studentcalc:.time => time
                    545: 
                    546:  sname:sdom:studentcalc => ___=___Adata___;___Bdata___;___Cdata___;___ .....
                    547: 
                    548: =cut
                    549: 
                    550: #############################################
                    551: #############################################
                    552: sub load_cached_export_rows {
1.17      matthew   553:     undef(%Exportrows);
1.1       matthew   554:     my @tmp = &Apache::lonnet::dump('nohist_calculatedsheets',
                    555: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    556: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'},undef);
                    557:     my %Selected_Assess_Sheet;
                    558:     if ($tmp[0] =~ /^error/) {
                    559:         &Apache::lonnet::logthis('unable to read cached student export rows '.
                    560:                                  'for course '.$ENV{'request.course.id'});
                    561:         return;
                    562:     }
                    563:     my %tmp = @tmp;
                    564:     while (my ($key,$sheetdata) = each(%tmp)) {
                    565:         my ($sname,$sdom,$sheettype,$remainder) = split(':',$key);
                    566:         my $student = $sname.':'.$sdom;
                    567:         if ($remainder =~ /\.time/) {
                    568:             $Exportrows{$student}->{'time'} = $sheetdata;
                    569:         } else {
                    570:             $sheetdata =~ s/^___=___//;
                    571:             my @Data = split('___;___',$sheetdata);
                    572:             $Exportrows{$student}->{'data'} = \@Data;
                    573:         }
                    574:     }
                    575: }
                    576: 
                    577: #############################################
                    578: #############################################
                    579: 
                    580: =pod
                    581: 
                    582: =item &save_export_data()
                    583: 
                    584: Writes the export data for this student to the course cache.
                    585: 
                    586: =cut
                    587: 
                    588: #############################################
                    589: #############################################
                    590: sub save_export_data {
                    591:     my $self = shift;
1.20    ! matthew   592:     &Apache::assesscalc::save_cached_export_rows($self->{'name'},
        !           593:                                                  $self->{'domain'});
1.5       matthew   594:     return if ($self->temporary());
1.1       matthew   595:     my $student = $self->{'name'}.':'.$self->{'domain'};
                    596:     return if (! exists($Exportrows{$student}));
                    597:     return if (! $self->is_default());
                    598:     my $key = join(':',($self->{'name'},$self->{'domain'},'studentcalc')).':';
                    599:     my $timekey = $key.'.time';
                    600:     my $newstore = join('___;___',
                    601:                         @{$Exportrows{$student}->{'data'}});
                    602:     $newstore = '___=___'.$newstore;
1.16      matthew   603:     my $result= &Apache::lonnet::put('nohist_calculatedsheets',
1.1       matthew   604:                          { $key     => $newstore,
                    605:                            $timekey => $Exportrows{$student}->{'time'} },
                    606:                          $self->{'cdom'},
                    607:                          $self->{'cnum'});
                    608:     return;
                    609: }
                    610: 
                    611: #############################################
                    612: #############################################
                    613: 
                    614: =pod
                    615: 
                    616: =item &export_data()
                    617: 
                    618: Returns the export data associated with the spreadsheet.  Computes the
                    619: spreadsheet only if necessary.
                    620: 
                    621: =cut
                    622: 
                    623: #############################################
                    624: #############################################
                    625: sub export_data {
                    626:     my $self = shift;
1.18      matthew   627:     my ($r) = @_;
                    628:     my $connection = $r->connection();
1.1       matthew   629:     my $student = $self->{'name'}.':'.$self->{'domain'};
                    630:     if (! exists($Exportrows{$student}) ||
1.15      matthew   631:         ! defined($Exportrows{$student}) ||
1.16      matthew   632:         ! exists($Exportrows{$student}->{'data'}) ||
1.15      matthew   633:         ! defined($Exportrows{$student}->{'data'}) ||
1.16      matthew   634:         ! exists($Exportrows{$student}->{'time'}) ||
                    635:         ! defined($Exportrows{$student}->{'time'}) ||
1.1       matthew   636:         ! $self->check_expiration_time($Exportrows{$student}->{'time'})) {
1.18      matthew   637:         $self->compute($r);
1.1       matthew   638:     }
1.18      matthew   639:     if ($connection->aborted()) { $self->cleanup(); return; }
1.1       matthew   640:     my @Data = @{$Exportrows{$student}->{'data'}};
                    641:     for (my $i=0; $i<=$#Data;$i++) {
                    642:         $Data[$i]="'".$Data[$i]."'" if ($Data[$i]=~/\D/ && defined($Data[$i]));
                    643:     }
                    644:     return @Data;
                    645: }
                    646: 
                    647: 1;
                    648: 
                    649: __END__

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