Annotation of loncom/interface/spreadsheet/assesscalc.pm, revision 1.10

1.1       matthew     1: #
1.10    ! matthew     2: # $Id: assesscalc.pm,v 1.9 2003/05/23 21:03:29 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: assesscalc
                     34: 
                     35: =head1 SYNOPSIS
                     36: 
                     37: =head1 DESCRIPTION
                     38: 
                     39: =cut
                     40: 
                     41: ###################################################
                     42: ###                 AssessSheet                 ###
                     43: ###################################################
                     44: package Apache::assesscalc;
                     45: 
                     46: use strict;
                     47: use Apache::Constants qw(:common :http);
                     48: use Apache::lonnet;
1.2       matthew    49: use Apache::loncommon;
1.1       matthew    50: use Apache::Spreadsheet;
                     51: use HTML::Entities();
                     52: use Spreadsheet::WriteExcel;
                     53: use GDBM_File;
                     54: use Time::HiRes;
                     55: 
                     56: @Apache::assesscalc::ISA = ('Apache::Spreadsheet');
                     57: 
                     58: ########################################################
                     59: ########################################################
                     60: 
                     61: =pod
                     62: 
                     63: =head2 Package Variables
                     64: 
                     65: =over 4
                     66: 
                     67: =item %Exportrows
                     68: 
                     69: =item $current_name
                     70: 
                     71: =item $current_domain
                     72: 
                     73: =item $current_course
                     74: 
                     75: =item %parmhash
                     76: 
                     77: =item %nice_parameter_name
                     78: 
                     79: =item %useropt
                     80: 
                     81: =item %courseopt
                     82: 
                     83: =back 
                     84: 
                     85: =cut
                     86: 
                     87: ########################################################
                     88: ########################################################
                     89: 
                     90: my %Exportrows;
                     91: 
                     92: my $current_name;
                     93: my $current_domain;
                     94: my $current_course;
                     95: 
                     96: my %parmhash;
                     97: my %nice_parameter_name;
                     98: 
                     99: my %useropt;
                    100: my %courseopt;
                    101: 
                    102: ########################################################
                    103: ########################################################
                    104: 
                    105: =pod
                    106: 
                    107: =head2 Package Subroutines
                    108: 
                    109: =item &clear_package()
                    110: 
                    111: Reset all package variables.  
                    112: 
                    113: =cut
                    114: 
                    115: ########################################################
                    116: ########################################################
                    117: sub clear_package {
                    118:     undef(%Exportrows);
                    119:     undef($current_name);
                    120:     undef($current_domain);
                    121:     undef($current_course);
                    122:     undef(%useropt);
                    123:     undef(%courseopt);
                    124: }
                    125: 
                    126: ########################################################
                    127: ########################################################
                    128: 
                    129: =pod
                    130: 
                    131: =item &initialize_package()
                    132: 
                    133: =cut
                    134: 
                    135: ########################################################
                    136: ########################################################
                    137: sub initialize_package {
                    138:     my ($sname,$sdomain) = @_;
                    139:     $current_course = $ENV{'request.course.id'};
                    140:     $current_name   = $sname;
                    141:     $current_domain = $sdomain;
                    142:     undef(%courseopt);
                    143:     &load_cached_export_rows();
                    144:     &load_parameter_caches();
                    145: }
                    146: 
                    147: ########################################################
                    148: ########################################################
                    149: 
                    150: =pod
                    151: 
                    152: =item &load_parameter_caches()
                    153: 
                    154: =cut
                    155: 
                    156: ########################################################
                    157: ########################################################
                    158: sub load_parameter_caches {
                    159:     my $userprefix = $current_name.':'.$current_domain.'_';
                    160:     $userprefix =~ s/:/_/g;
                    161:     #
                    162:     # Course Parameters Cache
                    163:     if (! %courseopt) {
                    164:         &Apache::lonnet::logthis("loading course options");
                    165:         $current_course = $ENV{'request.course.id'};
                    166:         undef(%courseopt);
                    167:         if (! defined($current_name) || ! defined($current_domain)) {
                    168:             &Apache::lonnet::logthis('bad call to setup_parameter_caches');
                    169:             return;
                    170:         }
                    171:         my $dom = $ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
                    172:         my $id  = $ENV{'course.'.$ENV{'request.course.id'}.'.num'};
                    173:         my %Tmp = &Apache::lonnet::dump('resourcedata',$dom,$id);
                    174:         while (my ($name,$value) = each(%Tmp)) {
1.3       matthew   175:             $courseopt{$name}=$value;
1.1       matthew   176:         }
                    177:     }
                    178:     if (! %useropt) {
                    179:         my %Tmp = &Apache::lonnet::dump('resourcedata',
                    180:                                         $current_domain,$current_name);
                    181:         while (my ($name,$value) = each(%Tmp)) {
                    182:             if ($name =~ /^error: 2/ || $name =~ /no such file/) {
                    183:                 undef(%useropt);
                    184:                 last;
                    185:             }
                    186:             $useropt{$userprefix.$name}=$value;
                    187:         }
                    188:     }
                    189: }
                    190: 
                    191: ########################################################
                    192: ########################################################
                    193: 
                    194: =pod
                    195: 
                    196: =head2 assesscalc object methods
                    197: 
                    198: =cut
                    199: 
                    200: ########################################################
                    201: ########################################################
                    202: sub ensure_current_parameter_caches {
                    203:     my $self = shift;
                    204:     if (! defined($current_course) || 
                    205:         $current_course ne $ENV{'request.course.id'} ) {
                    206:         $current_course = $ENV{'request.course.id'};
                    207:         undef(%courseopt); 
                    208:     }
                    209:     if (! defined($current_name)   || $current_name ne $self->{'name'} ||
                    210:         ! defined($current_domain) || $current_domain ne $self->{'domain'}) {
                    211:         $current_domain = $self->{'domain'};
                    212:         $current_name   = $self->{'name'};
                    213:         undef(%useropt);
                    214:     }
                    215:     &load_parameter_caches();
                    216: }
                    217: 
                    218: ##################################################
                    219: ##################################################
                    220: 
                    221: =pod
                    222: 
                    223: =item &parmval()
                    224: 
                    225: Determine the value of a parameter.
                    226: 
                    227: Inputs: $what, the parameter needed, $symb, $uname, $udom, $csec 
                    228: 
                    229: Returns: The value of a parameter, or '' if none.
                    230: 
                    231: This function cascades through the possible levels searching for a value for
                    232: a parameter.  The levels are checked in the following order:
                    233: user, course (at section level and course level), map, and lonnet::metadata.
                    234: This function uses %parmhash, which must be tied prior to calling it.
                    235: This function also requires %courseopt and %useropt to be initialized for
                    236: this user and course.
                    237: 
                    238: =cut
                    239: 
                    240: ##################################################
                    241: ##################################################
                    242: sub parmval {
                    243:     my $self = shift;
                    244:     my ($what,$symb,$uname,$udom,$csec)=@_;
                    245:     $uname = $self->{'name'}    if (! defined($uname));
                    246:     $udom  = $self->{'domain'}  if (! defined($udom));
                    247:     $csec  = $self->{'section'} if (! defined($csec));
                    248:     $symb  = $self->{'symb'}    if (! defined($symb));
                    249:     #
                    250:     my $result='';
                    251:     #
                    252:     # This should be a 
                    253:     my ($mapname,$id,$fn)=split(/___/,$symb);
                    254:     # Cascading lookup scheme
                    255:     my $rwhat=$what;
                    256:     $what =~ s/^parameter\_//;
                    257:     $what =~ s/\_([^\_]+)$/\.$1/;
                    258:     #
                    259:     my $symbparm = $symb.'.'.$what;
                    260:     my $mapparm  = $mapname.'___(all).'.$what;
1.3       matthew   261:     my $courseprefix = $self->{'cid'};
1.1       matthew   262:     my $usercourseprefix = $uname.'_'.$udom.'_'.$self->{'cid'};
                    263:     #
1.3       matthew   264:     my $seclevel  = $courseprefix.'.['.$csec.'].'.$what;
                    265:     my $seclevelr = $courseprefix.'.['.$csec.'].'.$symbparm;
                    266:     my $seclevelm = $courseprefix.'.['.$csec.'].'.$mapparm;
                    267:     #
                    268:     my $courselevel  = $courseprefix.'.'.$what;
                    269:     my $courselevelr = $courseprefix.'.'.$symbparm;
                    270:     my $courselevelm = $courseprefix.'.'.$mapparm;
                    271:     #
                    272:     my $ucourselevel  = $usercourseprefix.'.'.$what;
                    273:     my $ucourselevelr = $usercourseprefix.'.'.$symbparm;
                    274:     my $ucourselevelm = $usercourseprefix.'.'.$mapparm;
1.1       matthew   275:    # check user
                    276:     if (defined($uname)) {
1.3       matthew   277:         return $useropt{$ucourselevelr} if (defined($useropt{$ucourselevelr}));
                    278:         return $useropt{$ucourselevelm} if (defined($useropt{$ucourselevelm}));
                    279:         return $useropt{$ucourselevel}  if (defined($useropt{$ucourselevel}));
1.1       matthew   280:     }
                    281:     # check section
                    282:     if (defined($csec)) {
                    283:         return $courseopt{$seclevelr} if (defined($courseopt{$seclevelr}));
                    284:         return $courseopt{$seclevelm} if (defined($courseopt{$seclevelm}));
                    285:         return $courseopt{$seclevel}  if (defined($courseopt{$seclevel}));
                    286:     }
                    287:     #
                    288:     # check course
                    289:     return $courseopt{$courselevelr} if (defined($courseopt{$courselevelr}));
                    290:     return $courseopt{$courselevelm} if (defined($courseopt{$courselevelm}));
                    291:     return $courseopt{$courselevel}  if (defined($courseopt{$courselevel}));
                    292:     # check map parms
                    293:     my $thisparm = $parmhash{$symbparm};
                    294:     return $thisparm if (defined($thisparm));
                    295:     # check default
                    296:     $thisparm = &Apache::lonnet::metadata($fn,$rwhat.'.default');
                    297:     return $thisparm if (defined($thisparm));
                    298:     #
                    299:     # Cascade Up
                    300:     my $space=$what;
                    301:     $space=~s/\.\w+$//;
                    302:     if ($space ne '0') {
                    303: 	my @parts=split(/_/,$space);
                    304: 	my $id=pop(@parts);
                    305: 	my $part=join('_',@parts);
                    306: 	if ($part eq '') { $part='0'; }
                    307: 	my $newwhat=$rwhat;
                    308: 	$newwhat=~s/\Q$space\E/$part/;
                    309: 	my $partgeneral=$self->parmval($newwhat,$symb,$uname,$udom,$csec);
                    310: 	if (defined($partgeneral)) { return $partgeneral; }
                    311:     }
                    312:     #nothing defined
                    313:     return '';
                    314: }
                    315: 
1.8       matthew   316: sub get_html_title {
                    317:     my $self = shift;
                    318:     my ($assess_title,$name,$time) = $self->get_title();
                    319:     my $title = '<h1>'.$assess_title.'</h1>'.
                    320:         '<h2>'.$name.', '.
                    321:         &Apache::loncommon::aboutmewrapper
                    322:                          ($self->{'name'}.'@'.$self->{'domain'},
                    323:                           $self->{'name'},$self->{'domain'});
                    324:     $title .= '<h3>'.$time.'</h3>';
                    325:     return $title;
                    326: }
                    327: 
1.1       matthew   328: sub get_title {
                    329:     my $self = shift;
1.8       matthew   330:     my @title = ();
1.1       matthew   331:     if (($self->{'usymb'} eq '_feedback') ||
                    332:         ($self->{'usymb'} eq '_evaluation') ||
                    333:         ($self->{'usymb'} eq '_discussion') ||
                    334:         ($self->{'usymb'} eq '_tutoring')) {
1.8       matthew   335:         my $assess_title = ucfirst($self->{'usymb'});
                    336:         $assess_title =~ s/^_//;
                    337:         push(@title,$assess_title);
1.1       matthew   338:     } else {
1.8       matthew   339:         push(@title,&Apache::lonnet::gettitle($self->{'symb'}));
1.1       matthew   340:     }
1.2       matthew   341:     # Look up the users identifying information
                    342:     # Get the users information
                    343:     my %userenv = &Apache::loncoursedata::GetUserName($self->{'name'},
                    344:                                                       $self->{'domain'});
                    345:     my $name = 
                    346:         join(' ',@userenv{'firstname','middlename','lastname','generation'});
                    347:     $name =~ s/\s+$//;
1.8       matthew   348:     push (@title,$name);
                    349:     push (@title,scalar(localtime(time)));
                    350:     return @title;
1.1       matthew   351: }
                    352: 
                    353: sub parent_link {
                    354:     my $self = shift;
                    355:     my $link .= '<p><a href="/adm/studentcalc?'.
                    356:         'sname='.$self->{'name'}.
                    357:             '&sdomain='.$self->{'domain'}.'">'.
                    358:                 'Student level sheet</a></p>'."\n";
                    359:     return $link;
                    360: }
                    361: 
                    362: sub outsheet_html {
                    363:     my $self = shift;
                    364:     my ($r) = @_;
                    365:     ###################################
                    366:     # Determine table structure
                    367:     ###################################
                    368:     my $num_uneditable = 1;
                    369:     my $num_left = 52-$num_uneditable;
                    370:     my $tableheader =<<"END";
                    371: <table border="2">
                    372: <tr>
                    373:   <th colspan="2" rowspan="2"><font size="+2">Assessment</font></th>
                    374:   <td bgcolor="#FFDDDD" colspan="$num_uneditable">&nbsp;</td>
                    375:   <td colspan="$num_left">
                    376:       <b><font size="+1">Calculations</font></b></td>
                    377: </tr><tr>
                    378: END
                    379:     my $label_num = 0;
                    380:     foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
                    381:         if ($label_num<$num_uneditable) { 
                    382:             $tableheader .= '<td bgcolor="#FFDDDD">';
                    383:         } else {
                    384:             $tableheader .= '<td>';
                    385:         }
                    386:         $tableheader .= "<b><font size=+1>$_</font></b></td>";
                    387:         $label_num++;
                    388:     }
                    389:     $tableheader.="</tr>\n";
                    390:     #
                    391:     $r->print($tableheader);
                    392:     #
                    393:     # Print out template row
                    394:     $r->print('<tr><td>Template</td><td>&nbsp;</td>'.
                    395: 	      $self->html_template_row($num_uneditable)."</tr>\n");
                    396:     #
                    397:     # Print out summary/export row
                    398:     $r->print('<tr><td>Export</td><td>0</td>'.
                    399: 	      $self->html_export_row()."</tr>\n");
                    400:     #
                    401:     # Prepare to output rows
                    402:     $tableheader =<<"END";
                    403: <table border="2">
                    404: <tr><th>row</th><th>Item</th>
                    405: END
                    406:     foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
                    407: 	if ($label_num<$num_uneditable) { 
                    408:             $tableheader.='<th bgcolor="#FFDDDD">';
                    409:         } else {
                    410:             $tableheader.='<th>';
                    411:         }
                    412:         $tableheader.="<b><font size=+1>$_</font></b></th>";
                    413:     }
                    414:     #
                    415:     my $num_output = 0;
1.8       matthew   416:     foreach my $rownum (sort {$a <=> $b} ($self->rows())) {
1.1       matthew   417: 	if ($num_output++ % 50 == 0) {
                    418: 	    $r->print("</table>\n".$tableheader);
                    419: 	}
                    420: 	$r->print('<tr><td>'.$rownum.'</td>'.
1.10    ! matthew   421:                   $self->assess_html_row($rownum)."</tr>\n");
1.1       matthew   422:     }
                    423:     $r->print("</table>\n");
                    424:     return;
                    425: }
                    426: 
                    427: sub assess_html_row {
                    428:     my $self = shift();
1.10    ! matthew   429:     my ($row) = @_;
1.1       matthew   430:     my $parameter_name = $self->{'formulas'}->{'A'.$row};
                    431:     my @rowdata = $self->get_row($row);
                    432:     my $num_cols_output = 0;
                    433:     my $row_html;
                    434:     if (exists($nice_parameter_name{$parameter_name})) {
                    435:         my $name = $nice_parameter_name{$parameter_name};
                    436:         $name =~ s/ /\&nbsp;/g;
                    437:         $row_html .= '<td>'.$name.'<br />'.$parameter_name.'</td>';
                    438:     } else {
                    439:         $row_html .= '<td>'.$parameter_name.'</td>';
                    440:     }
                    441:     foreach my $cell (@rowdata) {
1.10    ! matthew   442:         if ($num_cols_output < 1) {
        !           443:             $row_html .= '<td bgcolor="#FFDDDD">';
        !           444:             $row_html .= &Apache::Spreadsheet::html_uneditable_cell($cell,
        !           445:                                                                     '#FFDDDD');
        !           446:         } else {
        !           447:             $row_html .= '<td bgcolor="#EOFFDD">';
        !           448:             $row_html .= &Apache::Spreadsheet::html_editable_cell($cell,
        !           449:                                                                   '#E0FFDD',1);
        !           450:         }
1.1       matthew   451: 	$row_html .= '</td>';
1.10    ! matthew   452:         $num_cols_output++;
1.1       matthew   453:     }
                    454:     return $row_html;
                    455: }
                    456: 
1.9       matthew   457: sub csv_rows {
                    458:     # writes the meat of the spreadsheet to an excel worksheet.  Called
                    459:     # by Spreadsheet::outsheet_excel;
1.1       matthew   460:     my $self = shift;
1.9       matthew   461:     my ($filehandle) = @_;
                    462:     #
                    463:     # Write a header row
                    464:     $self->csv_output_row($filehandle,undef,
                    465:                           ('Parameter','Description','Value'));
                    466:     #
                    467:     # Write each row
                    468:     foreach my $rownum (sort {$a <=> $b} ($self->rows())) {
                    469:         my $parameter_name = $self->{'formulas'}->{'A'.$rownum};
                    470:         my $description = '';
                    471:         if (exists($nice_parameter_name{$parameter_name})) {
                    472:             $description = $nice_parameter_name{$parameter_name};
                    473:         }
                    474:         $self->csv_output_row($filehandle,$rownum,
                    475:                               $parameter_name,$description);
                    476:     }
                    477:     return;
1.1       matthew   478: }
                    479: 
1.8       matthew   480: sub excel_rows {
                    481:     # writes the meat of the spreadsheet to an excel worksheet.  Called
                    482:     # by Spreadsheet::outsheet_excel;
1.1       matthew   483:     my $self = shift;
1.8       matthew   484:     my ($worksheet,$cols_output,$rows_output) = @_;
                    485:     #
                    486:     # Write a header row
                    487:     $cols_output = 0;
                    488:     foreach my $value ('Parameter','Description','Value') {
                    489:         $worksheet->write($rows_output,$cols_output++,$value);
                    490:     }
                    491:     $rows_output++;    
                    492:     #
                    493:     # Write each row
                    494:     foreach my $rownum (sort {$a <=> $b} ($self->rows())) {
                    495:         my $parameter_name = $self->{'formulas'}->{'A'.$rownum};
                    496:         my $description = '';
                    497:         if (exists($nice_parameter_name{$parameter_name})) {
                    498:             $description = $nice_parameter_name{$parameter_name};
                    499:         }
                    500:         $self->excel_output_row($worksheet,$rownum,$rows_output++,
                    501:                                 $parameter_name,$description);
                    502:     }
                    503:     return;
1.1       matthew   504: }
                    505: 
                    506: sub compute {
                    507:     my $self = shift;
                    508:     $self->logthis('computing');
                    509:     $self->initialize_safe_space();
                    510:     #
                    511:     # Definitions
                    512:     undef(%nice_parameter_name);
                    513:     my %parameters;   # holds underscored parameters by name
                    514:     #
                    515:     # Get the metadata fields and determine their proper names
                    516:     my ($symap,$syid,$srcf)=split(/___/,$self->{'symb'});
                    517:     my @Metadata = split(/\,/,&Apache::lonnet::metadata($srcf,'keys'));
                    518:     foreach my $parm (@Metadata) {
                    519:         next if ($parm !~ /^(resource\.|stores|parameter)_/);
                    520:         my $cleaned_name = $parm;
                    521:         $cleaned_name =~ s/^resource\./stores_/;
                    522:         $cleaned_name =~ s/\./_/g;
                    523:         my $display = &Apache::lonnet::metadata($srcf,
                    524:                                                 $cleaned_name.'.display');
                    525:         if (! $display) {
                    526:             $display .= &Apache::lonnet::metadata($srcf,$cleaned_name.'.name');
                    527:         }
                    528:         $parameters{$cleaned_name}++;
                    529:         $nice_parameter_name{$cleaned_name} = $display;
                    530:     }
                    531:     #
                    532:     # Get the values of the metadata fields
                    533:     $self->ensure_current_parameter_caches();
                    534:     my $filename = $self->{'coursefilename'}.'_parms.db';
                    535:     if (tie(%parmhash,'GDBM_File',
                    536:             $self->{'coursefilename'}.'_parms.db',&GDBM_READER(),0640)) {
                    537:         foreach my $parmname (keys(%parameters)) {
1.3       matthew   538:             my $value = $self->parmval($parmname);
1.1       matthew   539:             $parameters{$parmname} =$value;
                    540:         }
                    541:         untie(%parmhash);
                    542:     } else {
                    543:         $self->logthis('unable to tie '.$filename);
                    544:     }
                    545:     #
                    546:     # Clean out unnecessary parameters
                    547:     foreach (keys(%parameters)) {
                    548:         delete($parameters{$_}) if (! /(resource\.|stores_|parameter_)/);
                    549:     }
                    550:     #
                    551:     # Get the students performance data
                    552:     my %student_parameters = 
                    553:         &Apache::loncoursedata::get_current_state($self->{'name'},
                    554:                                                   $self->{'domain'},
                    555:                                                   $self->{'symb'},
                    556:                                                   $self->{'cid'});
                    557:     while (my ($parm,$value) = each(%student_parameters)) {
                    558:         $parm =~ s/^resource\./stores_/;
                    559:         $parm =~ s/\./_/g;
                    560:         $parameters{$parm} = $value;
                    561:     }
                    562:     #
                    563:     # Set up the formulas and parameter values
                    564:     my %f=$self->formulas();
                    565:     my %c;
                    566:     #
1.5       matthew   567:     # Check for blackout requirements
                    568:     if ((!exists($ENV{'request.role.adv'}) || !$ENV{'request.role.adv'})) {
                    569:         while (my ($parm,$value) = each(%parameters)) {
                    570:             last if ($self->blackout());
                    571:             next if ($parm !~ /^(parameter_.*)_problemstatus$/);
                    572:             next if ($parameters{$1.'_answerdate'}<time);
                    573:             if (lc($value) eq 'no') {
                    574:                 # We must blackout this sheet
                    575:                 $self->blackout(1);
                    576:             }
                    577:         }
                    578:     }
                    579:     #
                    580:     # Move the parameters into the spreadsheet
1.1       matthew   581:     while (my ($parm,$value) = each(%parameters)) {
                    582:         my $cell = 'A'.$self->get_row_number_from_key($parm);
                    583:         $f{$cell} = $parm;
1.6       matthew   584:         $value = '"'.$value.'"' if ($value =~/[^0-9.]/);
                    585:         $c{$parm} = $value;
1.1       matthew   586:     }
1.6       matthew   587:     $self->formulas(\%f);
                    588:     $self->constants(\%c);
1.1       matthew   589:     $self->calcsheet();
                    590:     #
                    591:     # Store export row in cache
                    592:     my @exportarray = $self->exportrow();
                    593:     $Exportrows{$self->{'symb'}}->{'time'} = time;
                    594:     $Exportrows{$self->{'symb'}}->{$self->{'filename'}} = \@exportarray;
                    595:     #
                    596:     # Save the export data
                    597:     $self->save_export_data();
                    598:     return;
                    599: }
                    600: 
                    601: ##
                    602: ## sett overrides Spreadsheet::sett
                    603: ##
                    604: sub sett {
                    605:     my $self = shift;
                    606:     my %t=();
                    607:     #
                    608:     # Deal with the template row by copying the template formulas into each
                    609:     # row.
                    610:     foreach my $col ($self->template_cells()) {
                    611:         next if ($col=~/^A/);
                    612:         foreach my $row ($self->rows()) {
                    613:             # Get the name of this cell
                    614:             my $cell=$col.$row;
                    615:             # Grab the template declaration
                    616:             $t{$cell}=$self->formula('template_'.$col);
                    617:             # Replace '#' with the row number
                    618:             $t{$cell}=~s/\#/$row/g;
                    619:             # Replace '....' with ','
                    620:             $t{$cell}=~s/\.\.+/\,/g;
                    621:             # Replace 'A0' with the value from 'A0'
                    622:             $t{$cell}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
                    623:             # Replace parameters
                    624:             $t{$cell}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.$self->expandnamed($2)/ge;
                    625:         }
                    626:     }
                    627:     #
                    628:     # Deal with the cells which have formulas
                    629:     while (my ($cell,$formula) = each(%{$self->{'formulas'}})) {
                    630: 	next if ($cell =~ /template_/);
                    631:         if ($cell =~ /^A/ && $cell ne 'A0') {
                    632:             if ($formula !~ /^\!/) {
                    633:                 $t{$cell}=$self->{'constants'}->{$formula};
                    634:             }
                    635:         } else {
                    636:             $t{$cell}=$formula;
                    637:             $t{$cell}=~s/\.\.+/\,/g;
                    638:             $t{$cell}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
                    639:             $t{$cell}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.$self->expandnamed($2)/ge;
                    640:         }
                    641:     }
                    642:     # Put %t into the safe space
                    643:     %{$self->{'safe'}->varglob('t')}=%t;
                    644: }
                    645: 
                    646: 
                    647: ########################################################
                    648: ########################################################
                    649: 
                    650: =pod
                    651: 
                    652: =item &load_cached_export_rows()
                    653: 
                    654: Retrieves and parsers the export rows of the assessment spreadsheets.
                    655: These rows are saved in the students directory in the format:
                    656: 
                    657:  sname:sdom:assesscalc:symb.time => time
                    658: 
                    659:  sname:sdom:assesscalc:symb => filename___=___Adata___;___Bdata___;___ ...
                    660: 
                    661: =cut
                    662: 
                    663: ########################################################
                    664: ########################################################
                    665: sub load_cached_export_rows {
                    666:     %Exportrows = undef;
                    667:     my @tmp = &Apache::lonnet::dump('nohist_calculatedsheets_'.
                    668:                                     $ENV{'request.course.id'},
                    669:                                     $current_domain,$current_name,undef);
                    670:     if ($tmp[0]!~/^error/) {
                    671:         my %tmp = @tmp;
                    672:         my $default_filename =  $ENV{'course.'.$ENV{'request.course.id'}.
                    673:                                          '.spreadsheet_default_assesscalc'};
                    674:         # We only got one key, so we will access it directly.
                    675:         while (my ($key,$sheetdata) = each(%tmp)) {
                    676:             my ($sname,$sdom,$sheettype,$symb) = split(':',$key);
                    677:             if ($symb =~ /\.time$/) {
                    678:                 $symb =~ s/\.time$//;
                    679:                 $Exportrows{$symb}->{'time'} = $sheetdata;
                    680:             } else {
                    681:                 $sheetdata =~ s/^(.*)___=___//;
                    682:                 my $filename = $1;
                    683:                 $filename = $default_filename if (! defined($filename));
                    684:                 my @Data = split('___;___',$sheetdata);
                    685:                 $Exportrows{$symb}->{$filename} = \@Data;
                    686:             }
                    687:         }
                    688:     }
                    689: }
                    690: 
                    691: #############################################
                    692: #############################################
                    693: 
                    694: =pod
                    695: 
                    696: =item &export_data
                    697: 
                    698: Returns the export data associated with the spreadsheet.  Computes the
                    699: spreadsheet only if necessary.
                    700: 
                    701: =cut
                    702: 
                    703: #############################################
                    704: #############################################
                    705: sub export_data {
                    706:     my $self = shift;
                    707:     my $symb = $self->{'symb'};
1.5       matthew   708:     if (! exists($ENV{'request.role.adv'}) || ! $ENV{'request.role.adv'} ||
                    709:         ! exists($Exportrows{$symb}) || ! defined($Exportrows{$symb})  ||
1.1       matthew   710:         ! $self->check_expiration_time($Exportrows{$symb}->{'time'}) ||
                    711:         ! exists($Exportrows{$symb}->{$self->{'filename'}}) ||
                    712:         ! defined($Exportrows{$symb}->{$self->{'filename'}})) {
                    713:         $self->compute();
                    714:     }
                    715:     my @Data = @{$Exportrows{$symb}->{$self->{'filename'}}};
                    716:     if ($Data[0] =~ /^(.*)___=___/) {
                    717:         $self->{'sheetname'} = $1;
                    718:         $Data[0] =~ s/^(.*)___=___//;
                    719:     }
                    720:     for (my $i=0;$i<$#Data;$i++) {
                    721:         $Data[$i]="'".$Data[$i]."'" if ($Data[$i]=~/\D/ && defined($Data[$i]));
                    722:     }
                    723:     return @Data;
                    724: }
                    725: 
                    726: #############################################
                    727: #############################################
                    728: 
                    729: =pod
                    730: 
                    731: =item &save_export_data()
                    732: 
                    733: Writes the export data for this spreadsheet to the students cache.
                    734: 
                    735: =cut
                    736: 
                    737: #############################################
                    738: #############################################
                    739: sub save_export_data {
                    740:     my $self = shift;
1.7       matthew   741:     return if ($self->temporary());
1.1       matthew   742:     my $student = $self->{'name'}.':'.$self->{'domain'};
                    743:     my $symb    = $self->{'symb'};
                    744:     if (! exists($Exportrows{$symb}) || 
                    745:         ! exists($Exportrows{$symb}->{$self->{'filename'}})) {
                    746:         return;
                    747:     }
                    748:     my $key = join(':',($self->{'name'},$self->{'domain'},'assesscalc',$symb));
                    749:     my $timekey = $key.'.time';
                    750:     my $newstore= join('___;___',@{$Exportrows{$symb}->{$self->{'filename'}}});
                    751:     $newstore = $self->{'filename'}.'___=___'.$newstore;
                    752:     my $result = &Apache::lonnet::put
                    753:         ('nohist_calculatedsheets_'.$ENV{'request.course.id'},
                    754:          { $key     => $newstore,
                    755:            $timekey => $Exportrows{$symb}->{'time'} },
                    756:          $self->{'domain'},
                    757:          $self->{'name'});
                    758: 
                    759:     return;
                    760: }
                    761: 
                    762: 1;
                    763: 
                    764: __END__

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