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

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

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