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

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

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