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

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

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