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

1.1       matthew     1: #
1.17.2.3! matthew     2: # $Id: assesscalc.pm,v 1.17.2.2 2003/10/03 15:39:46 albertel 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;
1.17.2.2  albertel   51: use Apache::loncoursedata();
1.1       matthew    52: use HTML::Entities();
                     53: use Spreadsheet::WriteExcel;
                     54: use GDBM_File;
                     55: use Time::HiRes;
                     56: 
                     57: @Apache::assesscalc::ISA = ('Apache::Spreadsheet');
                     58: 
                     59: ########################################################
                     60: ########################################################
                     61: 
                     62: =pod
                     63: 
                     64: =head2 Package Variables
                     65: 
                     66: =over 4
                     67: 
                     68: =item %Exportrows
                     69: 
                     70: =item $current_name
                     71: 
                     72: =item $current_domain
                     73: 
                     74: =item $current_course
                     75: 
                     76: =item %parmhash
                     77: 
                     78: =item %nice_parameter_name
                     79: 
                     80: =item %useropt
                     81: 
                     82: =item %courseopt
                     83: 
                     84: =back 
                     85: 
                     86: =cut
                     87: 
                     88: ########################################################
                     89: ########################################################
                     90: 
                     91: my %Exportrows;
                     92: 
                     93: my $current_name;
                     94: my $current_domain;
                     95: my $current_course;
                     96: 
                     97: my %parmhash;
                     98: my %nice_parameter_name;
                     99: 
                    100: my %useropt;
                    101: my %courseopt;
                    102: 
                    103: ########################################################
                    104: ########################################################
                    105: 
                    106: =pod
                    107: 
                    108: =head2 Package Subroutines
                    109: 
                    110: =item &clear_package()
                    111: 
                    112: Reset all package variables.  
                    113: 
                    114: =cut
                    115: 
                    116: ########################################################
                    117: ########################################################
                    118: sub clear_package {
                    119:     undef(%Exportrows);
                    120:     undef($current_name);
                    121:     undef($current_domain);
                    122:     undef($current_course);
                    123:     undef(%useropt);
                    124:     undef(%courseopt);
                    125: }
                    126: 
1.11      matthew   127: sub initialize {
                    128:     &clear_package();
1.17.2.2  albertel  129:     &Apache::loncoursedata::clear_internal_caches();
1.11      matthew   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();
1.17.2.2  albertel  153:     &Apache::loncoursedata::clear_internal_caches();
1.1       matthew   154: }
                    155: 
                    156: ########################################################
                    157: ########################################################
                    158: 
                    159: =pod
                    160: 
                    161: =item &load_parameter_caches()
                    162: 
                    163: =cut
                    164: 
                    165: ########################################################
                    166: ########################################################
                    167: sub load_parameter_caches {
                    168:     my $userprefix = $current_name.':'.$current_domain.'_';
                    169:     $userprefix =~ s/:/_/g;
                    170:     #
                    171:     # Course Parameters Cache
                    172:     if (! %courseopt) {
                    173:         &Apache::lonnet::logthis("loading course options");
                    174:         $current_course = $ENV{'request.course.id'};
                    175:         undef(%courseopt);
                    176:         if (! defined($current_name) || ! defined($current_domain)) {
                    177:             &Apache::lonnet::logthis('bad call to setup_parameter_caches');
                    178:             return;
                    179:         }
                    180:         my $dom = $ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
                    181:         my $id  = $ENV{'course.'.$ENV{'request.course.id'}.'.num'};
                    182:         my %Tmp = &Apache::lonnet::dump('resourcedata',$dom,$id);
                    183:         while (my ($name,$value) = each(%Tmp)) {
1.3       matthew   184:             $courseopt{$name}=$value;
1.1       matthew   185:         }
                    186:     }
                    187:     if (! %useropt) {
                    188:         my %Tmp = &Apache::lonnet::dump('resourcedata',
                    189:                                         $current_domain,$current_name);
                    190:         while (my ($name,$value) = each(%Tmp)) {
                    191:             if ($name =~ /^error: 2/ || $name =~ /no such file/) {
                    192:                 undef(%useropt);
                    193:                 last;
                    194:             }
                    195:             $useropt{$userprefix.$name}=$value;
                    196:         }
                    197:     }
                    198: }
                    199: 
                    200: ########################################################
                    201: ########################################################
                    202: 
                    203: =pod
                    204: 
                    205: =head2 assesscalc object methods
                    206: 
                    207: =cut
                    208: 
                    209: ########################################################
                    210: ########################################################
                    211: sub ensure_current_parameter_caches {
                    212:     my $self = shift;
                    213:     if (! defined($current_course) || 
                    214:         $current_course ne $ENV{'request.course.id'} ) {
                    215:         $current_course = $ENV{'request.course.id'};
                    216:         undef(%courseopt); 
                    217:     }
                    218:     if (! defined($current_name)   || $current_name ne $self->{'name'} ||
                    219:         ! defined($current_domain) || $current_domain ne $self->{'domain'}) {
                    220:         $current_domain = $self->{'domain'};
                    221:         $current_name   = $self->{'name'};
                    222:         undef(%useropt);
                    223:     }
                    224:     &load_parameter_caches();
                    225: }
                    226: 
                    227: ##################################################
                    228: ##################################################
                    229: 
                    230: =pod
                    231: 
                    232: =item &parmval()
                    233: 
                    234: Determine the value of a parameter.
                    235: 
                    236: Inputs: $what, the parameter needed, $symb, $uname, $udom, $csec 
                    237: 
                    238: Returns: The value of a parameter, or '' if none.
                    239: 
                    240: This function cascades through the possible levels searching for a value for
                    241: a parameter.  The levels are checked in the following order:
                    242: user, course (at section level and course level), map, and lonnet::metadata.
                    243: This function uses %parmhash, which must be tied prior to calling it.
                    244: This function also requires %courseopt and %useropt to be initialized for
                    245: this user and course.
                    246: 
                    247: =cut
                    248: 
                    249: ##################################################
                    250: ##################################################
                    251: sub parmval {
                    252:     my $self = shift;
1.17      albertel  253:     my ($what,$symb,$uname,$udom,$csec,$recurse)=@_;
1.1       matthew   254:     $uname = $self->{'name'}    if (! defined($uname));
                    255:     $udom  = $self->{'domain'}  if (! defined($udom));
                    256:     $csec  = $self->{'section'} if (! defined($csec));
                    257:     $symb  = $self->{'symb'}    if (! defined($symb));
                    258:     #
                    259:     my $result='';
                    260:     #
                    261:     # This should be a 
                    262:     my ($mapname,$id,$fn)=split(/___/,$symb);
                    263:     # Cascading lookup scheme
                    264:     my $rwhat=$what;
                    265:     $what =~ s/^parameter\_//;
                    266:     $what =~ s/\_([^\_]+)$/\.$1/;
                    267:     #
                    268:     my $symbparm = $symb.'.'.$what;
                    269:     my $mapparm  = $mapname.'___(all).'.$what;
1.3       matthew   270:     my $courseprefix = $self->{'cid'};
1.1       matthew   271:     my $usercourseprefix = $uname.'_'.$udom.'_'.$self->{'cid'};
                    272:     #
1.3       matthew   273:     my $seclevel  = $courseprefix.'.['.$csec.'].'.$what;
                    274:     my $seclevelr = $courseprefix.'.['.$csec.'].'.$symbparm;
                    275:     my $seclevelm = $courseprefix.'.['.$csec.'].'.$mapparm;
                    276:     #
                    277:     my $courselevel  = $courseprefix.'.'.$what;
                    278:     my $courselevelr = $courseprefix.'.'.$symbparm;
                    279:     my $courselevelm = $courseprefix.'.'.$mapparm;
                    280:     #
                    281:     my $ucourselevel  = $usercourseprefix.'.'.$what;
                    282:     my $ucourselevelr = $usercourseprefix.'.'.$symbparm;
                    283:     my $ucourselevelm = $usercourseprefix.'.'.$mapparm;
1.1       matthew   284:    # check user
                    285:     if (defined($uname)) {
1.3       matthew   286:         return $useropt{$ucourselevelr} if (defined($useropt{$ucourselevelr}));
                    287:         return $useropt{$ucourselevelm} if (defined($useropt{$ucourselevelm}));
                    288:         return $useropt{$ucourselevel}  if (defined($useropt{$ucourselevel}));
1.1       matthew   289:     }
                    290:     # check section
                    291:     if (defined($csec)) {
                    292:         return $courseopt{$seclevelr} if (defined($courseopt{$seclevelr}));
                    293:         return $courseopt{$seclevelm} if (defined($courseopt{$seclevelm}));
                    294:         return $courseopt{$seclevel}  if (defined($courseopt{$seclevel}));
                    295:     }
                    296:     #
                    297:     # check course
                    298:     return $courseopt{$courselevelr} if (defined($courseopt{$courselevelr}));
                    299:     return $courseopt{$courselevelm} if (defined($courseopt{$courselevelm}));
                    300:     return $courseopt{$courselevel}  if (defined($courseopt{$courselevel}));
                    301:     # check map parms
                    302:     my $thisparm = $parmhash{$symbparm};
                    303:     return $thisparm if (defined($thisparm));
                    304:     # check default
                    305:     $thisparm = &Apache::lonnet::metadata($fn,$rwhat.'.default');
                    306:     return $thisparm if (defined($thisparm));
                    307:     #
                    308:     # Cascade Up
                    309:     my $space=$what;
                    310:     $space=~s/\.\w+$//;
                    311:     if ($space ne '0') {
                    312: 	my @parts=split(/_/,$space);
                    313: 	my $id=pop(@parts);
                    314: 	my $part=join('_',@parts);
                    315: 	if ($part eq '') { $part='0'; }
                    316: 	my $newwhat=$rwhat;
                    317: 	$newwhat=~s/\Q$space\E/$part/;
1.17      albertel  318: 	my $partgeneral=$self->parmval($newwhat,$symb,$uname,$udom,$csec,1);
1.1       matthew   319: 	if (defined($partgeneral)) { return $partgeneral; }
                    320:     }
1.17      albertel  321:     if ($recurse) { return undef; }
                    322:     my $pack_def=&Apache::lonnet::packages_tab_default($fn,'resource.'.$what);
                    323:     if (defined($pack_def)) { return $pack_def; }
1.1       matthew   324:     #nothing defined
                    325:     return '';
                    326: }
                    327: 
1.8       matthew   328: sub get_html_title {
                    329:     my $self = shift;
                    330:     my ($assess_title,$name,$time) = $self->get_title();
                    331:     my $title = '<h1>'.$assess_title.'</h1>'.
                    332:         '<h2>'.$name.', '.
                    333:         &Apache::loncommon::aboutmewrapper
                    334:                          ($self->{'name'}.'@'.$self->{'domain'},
                    335:                           $self->{'name'},$self->{'domain'});
                    336:     $title .= '<h3>'.$time.'</h3>';
                    337:     return $title;
                    338: }
                    339: 
1.1       matthew   340: sub get_title {
                    341:     my $self = shift;
1.8       matthew   342:     my @title = ();
1.16      matthew   343:     if (($self->{'symb'} eq '_feedback') ||
                    344:         ($self->{'symb'} eq '_evaluation') ||
                    345:         ($self->{'symb'} eq '_discussion') ||
                    346:         ($self->{'symb'} eq '_tutoring')) {
                    347:         my $assess_title = ucfirst($self->{'symb'});
1.8       matthew   348:         $assess_title =~ s/^_//;
                    349:         push(@title,$assess_title);
1.1       matthew   350:     } else {
1.8       matthew   351:         push(@title,&Apache::lonnet::gettitle($self->{'symb'}));
1.1       matthew   352:     }
1.2       matthew   353:     # Look up the users identifying information
                    354:     # Get the users information
                    355:     my %userenv = &Apache::loncoursedata::GetUserName($self->{'name'},
                    356:                                                       $self->{'domain'});
                    357:     my $name = 
                    358:         join(' ',@userenv{'firstname','middlename','lastname','generation'});
                    359:     $name =~ s/\s+$//;
1.8       matthew   360:     push (@title,$name);
                    361:     push (@title,scalar(localtime(time)));
                    362:     return @title;
1.1       matthew   363: }
                    364: 
                    365: sub parent_link {
                    366:     my $self = shift;
                    367:     my $link .= '<p><a href="/adm/studentcalc?'.
                    368:         'sname='.$self->{'name'}.
                    369:             '&sdomain='.$self->{'domain'}.'">'.
                    370:                 'Student level sheet</a></p>'."\n";
                    371:     return $link;
                    372: }
                    373: 
                    374: sub outsheet_html {
                    375:     my $self = shift;
                    376:     my ($r) = @_;
1.17.2.3! matthew   377:     ####################################
        !           378:     # Report any calculation errors    #
        !           379:     ####################################
        !           380:     $r->print($self->html_report_error());
1.1       matthew   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.13      matthew   527: #    $self->logthis('computing');
1.1       matthew   528:     $self->initialize_safe_space();
1.11      matthew   529:     #########################################
                    530:     #########################################
                    531:     ###                                   ###
                    532:     ###  Retrieve the problem parameters  ###
                    533:     ###                                   ###
                    534:     #########################################
                    535:     #########################################
                    536:     my @Mandatory_parameters = ("stores_0_solved",
                    537:                                 "stores_0_awarddetail",
                    538:                                 "stores_0_awarded",
                    539:                                 "timestamp",
                    540:                                 "stores_0_tries",
                    541:                                 "stores_0_award");
1.1       matthew   542:     #
                    543:     # Definitions
                    544:     undef(%nice_parameter_name);
                    545:     my %parameters;   # holds underscored parameters by name
                    546:     #
                    547:     # Get the metadata fields and determine their proper names
                    548:     my ($symap,$syid,$srcf)=split(/___/,$self->{'symb'});
                    549:     my @Metadata = split(/\,/,&Apache::lonnet::metadata($srcf,'keys'));
1.11      matthew   550:     foreach my $parm (@Mandatory_parameters,@Metadata) {
1.1       matthew   551:         next if ($parm !~ /^(resource\.|stores|parameter)_/);
                    552:         my $cleaned_name = $parm;
                    553:         $cleaned_name =~ s/^resource\./stores_/;
                    554:         $cleaned_name =~ s/\./_/g;
                    555:         my $display = &Apache::lonnet::metadata($srcf,
                    556:                                                 $cleaned_name.'.display');
                    557:         if (! $display) {
                    558:             $display .= &Apache::lonnet::metadata($srcf,$cleaned_name.'.name');
                    559:         }
                    560:         $parameters{$cleaned_name}++;
                    561:         $nice_parameter_name{$cleaned_name} = $display;
                    562:     }
                    563:     #
                    564:     # Get the values of the metadata fields
                    565:     $self->ensure_current_parameter_caches();
                    566:     my $filename = $self->{'coursefilename'}.'_parms.db';
                    567:     if (tie(%parmhash,'GDBM_File',
                    568:             $self->{'coursefilename'}.'_parms.db',&GDBM_READER(),0640)) {
                    569:         foreach my $parmname (keys(%parameters)) {
1.3       matthew   570:             my $value = $self->parmval($parmname);
1.1       matthew   571:             $parameters{$parmname} =$value;
                    572:         }
                    573:         untie(%parmhash);
                    574:     } else {
                    575:         $self->logthis('unable to tie '.$filename);
                    576:     }
                    577:     #
                    578:     # Clean out unnecessary parameters
                    579:     foreach (keys(%parameters)) {
                    580:         delete($parameters{$_}) if (! /(resource\.|stores_|parameter_)/);
                    581:     }
                    582:     #
                    583:     # Get the students performance data
                    584:     my %student_parameters = 
                    585:         &Apache::loncoursedata::get_current_state($self->{'name'},
                    586:                                                   $self->{'domain'},
                    587:                                                   $self->{'symb'},
                    588:                                                   $self->{'cid'});
                    589:     while (my ($parm,$value) = each(%student_parameters)) {
                    590:         $parm =~ s/^resource\./stores_/;
                    591:         $parm =~ s/\./_/g;
                    592:         $parameters{$parm} = $value;
                    593:     }
                    594:     #
                    595:     # Set up the formulas and parameter values
                    596:     my %f=$self->formulas();
                    597:     my %c;
                    598:     #
1.5       matthew   599:     # Check for blackout requirements
                    600:     if ((!exists($ENV{'request.role.adv'}) || !$ENV{'request.role.adv'})) {
                    601:         while (my ($parm,$value) = each(%parameters)) {
                    602:             last if ($self->blackout());
                    603:             next if ($parm !~ /^(parameter_.*)_problemstatus$/);
1.17.2.1  albertel  604: 	    if ($parameters{$1.'_answerdate'} ne '' &&
                    605: 		$parameters{$1.'_answerdate'} < time) {
                    606: 		next;
                    607: 	    }
1.5       matthew   608:             if (lc($value) eq 'no') {
                    609:                 # We must blackout this sheet
                    610:                 $self->blackout(1);
                    611:             }
                    612:         }
                    613:     }
                    614:     #
                    615:     # Move the parameters into the spreadsheet
1.1       matthew   616:     while (my ($parm,$value) = each(%parameters)) {
                    617:         my $cell = 'A'.$self->get_row_number_from_key($parm);
                    618:         $f{$cell} = $parm;
1.6       matthew   619:         $value = '"'.$value.'"' if ($value =~/[^0-9.]/);
                    620:         $c{$parm} = $value;
1.1       matthew   621:     }
1.6       matthew   622:     $self->formulas(\%f);
                    623:     $self->constants(\%c);
1.1       matthew   624:     $self->calcsheet();
                    625:     #
                    626:     # Store export row in cache
                    627:     my @exportarray = $self->exportrow();
                    628:     $Exportrows{$self->{'symb'}}->{'time'} = time;
                    629:     $Exportrows{$self->{'symb'}}->{$self->{'filename'}} = \@exportarray;
                    630:     #
                    631:     # Save the export data
                    632:     $self->save_export_data();
1.12      matthew   633:     $self->save() if ($self->need_to_save());
1.1       matthew   634:     return;
                    635: }
                    636: 
                    637: ##
                    638: ## sett overrides Spreadsheet::sett
                    639: ##
                    640: sub sett {
                    641:     my $self = shift;
                    642:     my %t=();
                    643:     #
                    644:     # Deal with the template row by copying the template formulas into each
                    645:     # row.
                    646:     foreach my $col ($self->template_cells()) {
                    647:         next if ($col=~/^A/);
                    648:         foreach my $row ($self->rows()) {
                    649:             # Get the name of this cell
                    650:             my $cell=$col.$row;
                    651:             # Grab the template declaration
                    652:             $t{$cell}=$self->formula('template_'.$col);
                    653:             # Replace '#' with the row number
                    654:             $t{$cell}=~s/\#/$row/g;
                    655:             # Replace '....' with ','
                    656:             $t{$cell}=~s/\.\.+/\,/g;
                    657:             # Replace 'A0' with the value from 'A0'
                    658:             $t{$cell}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
                    659:             # Replace parameters
                    660:             $t{$cell}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.$self->expandnamed($2)/ge;
                    661:         }
                    662:     }
                    663:     #
                    664:     # Deal with the cells which have formulas
                    665:     while (my ($cell,$formula) = each(%{$self->{'formulas'}})) {
                    666: 	next if ($cell =~ /template_/);
                    667:         if ($cell =~ /^A/ && $cell ne 'A0') {
                    668:             if ($formula !~ /^\!/) {
                    669:                 $t{$cell}=$self->{'constants'}->{$formula};
                    670:             }
                    671:         } else {
                    672:             $t{$cell}=$formula;
                    673:             $t{$cell}=~s/\.\.+/\,/g;
                    674:             $t{$cell}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
                    675:             $t{$cell}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.$self->expandnamed($2)/ge;
                    676:         }
                    677:     }
                    678:     # Put %t into the safe space
                    679:     %{$self->{'safe'}->varglob('t')}=%t;
                    680: }
                    681: 
                    682: 
                    683: ########################################################
                    684: ########################################################
                    685: 
                    686: =pod
                    687: 
                    688: =item &load_cached_export_rows()
                    689: 
                    690: Retrieves and parsers the export rows of the assessment spreadsheets.
                    691: These rows are saved in the students directory in the format:
                    692: 
                    693:  sname:sdom:assesscalc:symb.time => time
                    694: 
                    695:  sname:sdom:assesscalc:symb => filename___=___Adata___;___Bdata___;___ ...
                    696: 
                    697: =cut
                    698: 
                    699: ########################################################
                    700: ########################################################
                    701: sub load_cached_export_rows {
                    702:     %Exportrows = undef;
                    703:     my @tmp = &Apache::lonnet::dump('nohist_calculatedsheets_'.
                    704:                                     $ENV{'request.course.id'},
                    705:                                     $current_domain,$current_name,undef);
                    706:     if ($tmp[0]!~/^error/) {
                    707:         my %tmp = @tmp;
                    708:         my $default_filename =  $ENV{'course.'.$ENV{'request.course.id'}.
                    709:                                          '.spreadsheet_default_assesscalc'};
                    710:         # We only got one key, so we will access it directly.
                    711:         while (my ($key,$sheetdata) = each(%tmp)) {
                    712:             my ($sname,$sdom,$sheettype,$symb) = split(':',$key);
                    713:             if ($symb =~ /\.time$/) {
                    714:                 $symb =~ s/\.time$//;
                    715:                 $Exportrows{$symb}->{'time'} = $sheetdata;
                    716:             } else {
                    717:                 $sheetdata =~ s/^(.*)___=___//;
                    718:                 my $filename = $1;
                    719:                 $filename = $default_filename if (! defined($filename));
                    720:                 my @Data = split('___;___',$sheetdata);
                    721:                 $Exportrows{$symb}->{$filename} = \@Data;
                    722:             }
                    723:         }
                    724:     }
                    725: }
                    726: 
                    727: #############################################
                    728: #############################################
                    729: 
                    730: =pod
                    731: 
                    732: =item &export_data
                    733: 
                    734: Returns the export data associated with the spreadsheet.  Computes the
                    735: spreadsheet only if necessary.
                    736: 
                    737: =cut
                    738: 
                    739: #############################################
                    740: #############################################
                    741: sub export_data {
                    742:     my $self = shift;
                    743:     my $symb = $self->{'symb'};
1.5       matthew   744:     if (! exists($ENV{'request.role.adv'}) || ! $ENV{'request.role.adv'} ||
                    745:         ! exists($Exportrows{$symb}) || ! defined($Exportrows{$symb})  ||
1.1       matthew   746:         ! $self->check_expiration_time($Exportrows{$symb}->{'time'}) ||
                    747:         ! exists($Exportrows{$symb}->{$self->{'filename'}}) ||
                    748:         ! defined($Exportrows{$symb}->{$self->{'filename'}})) {
                    749:         $self->compute();
                    750:     }
1.17.2.3! matthew   751:     my @Data;
        !           752:     if ($self->badcalc()) {
        !           753:         @Data = ();
        !           754:     } else {
        !           755:         @Data = @{$Exportrows{$symb}->{$self->{'filename'}}};
        !           756:         if ($Data[0] =~ /^(.*)___=___/) {
        !           757:             $self->{'sheetname'} = $1;
        !           758:             $Data[0] =~ s/^(.*)___=___//;
        !           759:         }
        !           760:         for (my $i=0;$i<$#Data;$i++) {
        !           761:             if ($Data[$i]=~/\D/ && defined($Data[$i])) {
        !           762:                 $Data[$i]="'".$Data[$i]."'";
        !           763:             }
        !           764:         }
1.1       matthew   765:     }
                    766:     return @Data;
                    767: }
                    768: 
                    769: #############################################
                    770: #############################################
                    771: 
                    772: =pod
                    773: 
                    774: =item &save_export_data()
                    775: 
                    776: Writes the export data for this spreadsheet to the students cache.
                    777: 
                    778: =cut
                    779: 
                    780: #############################################
                    781: #############################################
                    782: sub save_export_data {
                    783:     my $self = shift;
1.7       matthew   784:     return if ($self->temporary());
1.1       matthew   785:     my $student = $self->{'name'}.':'.$self->{'domain'};
                    786:     my $symb    = $self->{'symb'};
1.17.2.3! matthew   787:     if ($self->badcalc()){
        !           788:         # do not save data away when calculations have not been done properly.
        !           789:         delete($Exportrows{$symb});
        !           790:         return;
        !           791:     }
1.1       matthew   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>