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

1.1       matthew     1: #
1.34    ! albertel    2: # $Id: assesscalc.pm,v 1.33 2004/02/24 20:47:14 matthew Exp $
1.1       matthew     3: #
                      4: # Copyright Michigan State University Board of Trustees
                      5: #
                      6: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      7: #
                      8: # LON-CAPA is free software; you can redistribute it and/or modify
                      9: # it under the terms of the GNU General Public License as published by
                     10: # the Free Software Foundation; either version 2 of the License, or
                     11: # (at your option) any later version.
                     12: #
                     13: # LON-CAPA is distributed in the hope that it will be useful,
                     14: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     15: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     16: # GNU General Public License for more details.
                     17: #
                     18: # You should have received a copy of the GNU General Public License
                     19: # along with LON-CAPA; if not, write to the Free Software
                     20: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     21: #
                     22: # /home/httpd/html/adm/gpl.txt
                     23: #
                     24: # http://www.lon-capa.org/
                     25: #
                     26: # The LearningOnline Network with CAPA
                     27: # Spreadsheet/Grades Display Handler
                     28: #
                     29: # POD required stuff:
                     30: 
                     31: =head1 NAME
                     32: 
                     33: assesscalc
                     34: 
                     35: =head1 SYNOPSIS
                     36: 
                     37: =head1 DESCRIPTION
                     38: 
                     39: =cut
                     40: 
                     41: ###################################################
                     42: ###                 AssessSheet                 ###
                     43: ###################################################
                     44: package Apache::assesscalc;
                     45: 
                     46: use strict;
1.18      matthew    47: use warnings FATAL=>'all';
                     48: no warnings 'uninitialized';
1.1       matthew    49: use Apache::Constants qw(:common :http);
                     50: use Apache::lonnet;
1.2       matthew    51: use Apache::loncommon;
1.1       matthew    52: use Apache::Spreadsheet;
1.25      matthew    53: use Apache::loncoursedata();
1.1       matthew    54: use HTML::Entities();
                     55: use Spreadsheet::WriteExcel;
                     56: use GDBM_File;
                     57: use Time::HiRes;
1.26      www        58: use Apache::lonlocal;
1.1       matthew    59: 
                     60: @Apache::assesscalc::ISA = ('Apache::Spreadsheet');
                     61: 
                     62: ########################################################
                     63: ########################################################
                     64: 
                     65: =pod
                     66: 
                     67: =head2 Package Variables
                     68: 
                     69: =over 4
                     70: 
                     71: =item %Exportrows
                     72: 
                     73: =item $current_name
                     74: 
                     75: =item $current_domain
                     76: 
                     77: =item $current_course
                     78: 
                     79: =item %parmhash
                     80: 
                     81: =item %nice_parameter_name
                     82: 
                     83: =item %useropt
                     84: 
                     85: =item %courseopt
                     86: 
                     87: =back 
                     88: 
                     89: =cut
                     90: 
                     91: ########################################################
                     92: ########################################################
                     93: 
                     94: my %Exportrows;
1.22      matthew    95: my %newExportrows;
1.1       matthew    96: 
                     97: my $current_name;
                     98: my $current_domain;
                     99: my $current_course;
                    100: 
                    101: my %parmhash;
                    102: my %nice_parameter_name;
                    103: 
                    104: my %useropt;
1.22      matthew   105: my %userdata;
1.1       matthew   106: my %courseopt;
                    107: 
                    108: ########################################################
                    109: ########################################################
                    110: 
                    111: =pod
                    112: 
                    113: =head2 Package Subroutines
                    114: 
                    115: =item &clear_package()
                    116: 
1.22      matthew   117: Reset all package variables and clean up caches.
1.1       matthew   118: 
                    119: =cut
                    120: 
                    121: ########################################################
                    122: ########################################################
                    123: sub clear_package {
1.22      matthew   124:     if (defined($current_name) &&
                    125:         defined($current_domain) &&
                    126:         defined($current_course) &&
                    127:         $current_course eq $ENV{'request.course.id'} &&
                    128:         %newExportrows) {
                    129:         &save_cached_export_rows($current_name,$current_domain);
                    130:     }
1.1       matthew   131:     undef(%Exportrows);
1.22      matthew   132:     undef(%newExportrows);
1.1       matthew   133:     undef($current_name);
                    134:     undef($current_domain);
                    135:     undef($current_course);
                    136:     undef(%useropt);
1.22      matthew   137:     undef(%userdata);
1.1       matthew   138:     undef(%courseopt);
                    139: }
                    140: 
1.22      matthew   141: sub save_cached_export_rows {
                    142:     my ($sname,$sdomain) = @_;
                    143:     my $start = Time::HiRes::time;
                    144:     my $result = &Apache::lonnet::put
                    145:         ('nohist_calculatedsheets_'.$ENV{'request.course.id'},
                    146:          $newExportrows{$sname.':'.$sdomain},
                    147:          $sdomain,$sname);
                    148:     delete($newExportrows{$sname.':'.$sdomain});
                    149: }
                    150: 
1.11      matthew   151: sub initialize {
                    152:     &clear_package();
1.25      matthew   153:     &Apache::loncoursedata::clear_internal_caches();
1.11      matthew   154: }
                    155: 
1.1       matthew   156: ########################################################
                    157: ########################################################
                    158: 
                    159: =pod
                    160: 
                    161: =item &initialize_package()
                    162: 
                    163: =cut
                    164: 
                    165: ########################################################
                    166: ########################################################
                    167: sub initialize_package {
                    168:     my ($sname,$sdomain) = @_;
                    169:     $current_name   = $sname;
                    170:     $current_domain = $sdomain;
1.21      matthew   171:     undef(%useropt);
1.22      matthew   172:     undef(%userdata);
1.13      matthew   173:     if ($current_course ne $ENV{'request.course.id'}) {
                    174:         $current_course = $ENV{'request.course.id'};
                    175:         undef(%courseopt);
                    176:     }
1.1       matthew   177:     &load_cached_export_rows();
                    178:     &load_parameter_caches();
1.25      matthew   179:     &Apache::loncoursedata::clear_internal_caches();
1.1       matthew   180: }
                    181: 
1.22      matthew   182: 
1.1       matthew   183: ########################################################
                    184: ########################################################
                    185: 
                    186: =pod
                    187: 
                    188: =item &load_parameter_caches()
                    189: 
                    190: =cut
                    191: 
                    192: ########################################################
                    193: ########################################################
                    194: sub load_parameter_caches {
                    195:     my $userprefix = $current_name.':'.$current_domain.'_';
                    196:     $userprefix =~ s/:/_/g;
                    197:     #
                    198:     # Course Parameters Cache
                    199:     if (! %courseopt) {
                    200:         $current_course = $ENV{'request.course.id'};
                    201:         undef(%courseopt);
                    202:         if (! defined($current_name) || ! defined($current_domain)) {
                    203:             return;
                    204:         }
                    205:         my $dom = $ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
                    206:         my $id  = $ENV{'course.'.$ENV{'request.course.id'}.'.num'};
                    207:         my %Tmp = &Apache::lonnet::dump('resourcedata',$dom,$id);
                    208:         while (my ($name,$value) = each(%Tmp)) {
1.3       matthew   209:             $courseopt{$name}=$value;
1.1       matthew   210:         }
                    211:     }
                    212:     if (! %useropt) {
                    213:         my %Tmp = &Apache::lonnet::dump('resourcedata',
                    214:                                         $current_domain,$current_name);
                    215:         while (my ($name,$value) = each(%Tmp)) {
                    216:             if ($name =~ /^error: 2/ || $name =~ /no such file/) {
                    217:                 undef(%useropt);
                    218:                 last;
                    219:             }
                    220:             $useropt{$userprefix.$name}=$value;
                    221:         }
1.21      matthew   222:         $useropt{'loadtime'} = time;
1.1       matthew   223:     }
1.22      matthew   224:     if (! %userdata) {
                    225:         %userdata = &Apache::loncoursedata::get_current_state($current_name,
                    226:                                                               $current_domain);
                    227:         $userdata{'loadtime'} = time;
                    228:     }
                    229:     return;
1.1       matthew   230: }
                    231: 
                    232: ########################################################
                    233: ########################################################
                    234: 
                    235: =pod
                    236: 
                    237: =head2 assesscalc object methods
                    238: 
                    239: =cut
                    240: 
                    241: ########################################################
                    242: ########################################################
1.22      matthew   243: sub ensure_current_caches {
1.1       matthew   244:     my $self = shift;
1.19      matthew   245:     ##
                    246:     ## Check for a modified parameters
                    247:     ##
1.1       matthew   248:     if (! defined($current_course) || 
                    249:         $current_course ne $ENV{'request.course.id'} ) {
                    250:         $current_course = $ENV{'request.course.id'};
                    251:         undef(%courseopt); 
1.22      matthew   252:         undef(%useropt);
                    253:         undef(%userdata);
1.1       matthew   254:     }
1.19      matthew   255:     ##
                    256:     ## Check for new user
                    257:     ##
1.1       matthew   258:     if (! defined($current_name)   || $current_name ne $self->{'name'} ||
                    259:         ! defined($current_domain) || $current_domain ne $self->{'domain'}) {
                    260:         $current_domain = $self->{'domain'};
                    261:         $current_name   = $self->{'name'};
                    262:         undef(%useropt);
1.22      matthew   263:         undef(%userdata);
1.1       matthew   264:     }
                    265:     &load_parameter_caches();
                    266: }
                    267: 
                    268: ##################################################
                    269: ##################################################
                    270: 
                    271: =pod
                    272: 
                    273: =item &parmval()
                    274: 
                    275: Determine the value of a parameter.
                    276: 
                    277: Inputs: $what, the parameter needed, $symb, $uname, $udom, $csec 
                    278: 
                    279: Returns: The value of a parameter, or '' if none.
                    280: 
                    281: This function cascades through the possible levels searching for a value for
                    282: a parameter.  The levels are checked in the following order:
                    283: user, course (at section level and course level), map, and lonnet::metadata.
                    284: This function uses %parmhash, which must be tied prior to calling it.
                    285: This function also requires %courseopt and %useropt to be initialized for
                    286: this user and course.
                    287: 
                    288: =cut
                    289: 
                    290: ##################################################
                    291: ##################################################
                    292: sub parmval {
                    293:     my $self = shift;
1.17      albertel  294:     my ($what,$symb,$uname,$udom,$csec,$recurse)=@_;
1.1       matthew   295:     $uname = $self->{'name'}    if (! defined($uname));
                    296:     $udom  = $self->{'domain'}  if (! defined($udom));
                    297:     $csec  = $self->{'section'} if (! defined($csec));
                    298:     $symb  = $self->{'symb'}    if (! defined($symb));
                    299:     #
                    300:     my $result='';
                    301:     #
                    302:     # This should be a 
1.20      www       303:     my ($mapname,$id,$fn)=&Apache::lonnet::decode_symb($symb);
1.1       matthew   304:     # Cascading lookup scheme
                    305:     my $rwhat=$what;
                    306:     $what =~ s/^parameter\_//;
                    307:     $what =~ s/\_([^\_]+)$/\.$1/;
                    308:     #
                    309:     my $symbparm = $symb.'.'.$what;
                    310:     my $mapparm  = $mapname.'___(all).'.$what;
1.3       matthew   311:     my $courseprefix = $self->{'cid'};
1.1       matthew   312:     my $usercourseprefix = $uname.'_'.$udom.'_'.$self->{'cid'};
                    313:     #
1.3       matthew   314:     my $seclevel  = $courseprefix.'.['.$csec.'].'.$what;
                    315:     my $seclevelr = $courseprefix.'.['.$csec.'].'.$symbparm;
                    316:     my $seclevelm = $courseprefix.'.['.$csec.'].'.$mapparm;
                    317:     #
                    318:     my $courselevel  = $courseprefix.'.'.$what;
                    319:     my $courselevelr = $courseprefix.'.'.$symbparm;
                    320:     my $courselevelm = $courseprefix.'.'.$mapparm;
                    321:     #
                    322:     my $ucourselevel  = $usercourseprefix.'.'.$what;
                    323:     my $ucourselevelr = $usercourseprefix.'.'.$symbparm;
                    324:     my $ucourselevelm = $usercourseprefix.'.'.$mapparm;
1.1       matthew   325:    # check user
                    326:     if (defined($uname)) {
1.3       matthew   327:         return $useropt{$ucourselevelr} if (defined($useropt{$ucourselevelr}));
                    328:         return $useropt{$ucourselevelm} if (defined($useropt{$ucourselevelm}));
                    329:         return $useropt{$ucourselevel}  if (defined($useropt{$ucourselevel}));
1.1       matthew   330:     }
                    331:     # check section
                    332:     if (defined($csec)) {
                    333:         return $courseopt{$seclevelr} if (defined($courseopt{$seclevelr}));
                    334:         return $courseopt{$seclevelm} if (defined($courseopt{$seclevelm}));
                    335:         return $courseopt{$seclevel}  if (defined($courseopt{$seclevel}));
                    336:     }
                    337:     #
                    338:     # check course
                    339:     return $courseopt{$courselevelr} if (defined($courseopt{$courselevelr}));
                    340:     return $courseopt{$courselevelm} if (defined($courseopt{$courselevelm}));
                    341:     return $courseopt{$courselevel}  if (defined($courseopt{$courselevel}));
                    342:     # check map parms
                    343:     my $thisparm = $parmhash{$symbparm};
                    344:     return $thisparm if (defined($thisparm));
                    345:     # check default
                    346:     $thisparm = &Apache::lonnet::metadata($fn,$rwhat.'.default');
                    347:     return $thisparm if (defined($thisparm));
                    348:     #
                    349:     # Cascade Up
                    350:     my $space=$what;
                    351:     $space=~s/\.\w+$//;
                    352:     if ($space ne '0') {
                    353: 	my @parts=split(/_/,$space);
                    354: 	my $id=pop(@parts);
                    355: 	my $part=join('_',@parts);
                    356: 	if ($part eq '') { $part='0'; }
                    357: 	my $newwhat=$rwhat;
                    358: 	$newwhat=~s/\Q$space\E/$part/;
1.17      albertel  359: 	my $partgeneral=$self->parmval($newwhat,$symb,$uname,$udom,$csec,1);
1.1       matthew   360: 	if (defined($partgeneral)) { return $partgeneral; }
                    361:     }
1.17      albertel  362:     if ($recurse) { return undef; }
                    363:     my $pack_def=&Apache::lonnet::packages_tab_default($fn,'resource.'.$what);
                    364:     if (defined($pack_def)) { return $pack_def; }
1.1       matthew   365:     #nothing defined
                    366:     return '';
                    367: }
                    368: 
1.8       matthew   369: sub get_html_title {
                    370:     my $self = shift;
1.29      matthew   371:     my ($assess_title,$name,$time) = $self->get_full_title();
1.8       matthew   372:     my $title = '<h1>'.$assess_title.'</h1>'.
                    373:         '<h2>'.$name.', '.
                    374:         &Apache::loncommon::aboutmewrapper
                    375:                          ($self->{'name'}.'@'.$self->{'domain'},
                    376:                           $self->{'name'},$self->{'domain'});
                    377:     $title .= '<h3>'.$time.'</h3>';
                    378:     return $title;
                    379: }
                    380: 
1.1       matthew   381: sub get_title {
                    382:     my $self = shift;
1.16      matthew   383:     if (($self->{'symb'} eq '_feedback') ||
                    384:         ($self->{'symb'} eq '_evaluation') ||
                    385:         ($self->{'symb'} eq '_discussion') ||
                    386:         ($self->{'symb'} eq '_tutoring')) {
                    387:         my $assess_title = ucfirst($self->{'symb'});
1.8       matthew   388:         $assess_title =~ s/^_//;
1.29      matthew   389:         return $assess_title;
1.1       matthew   390:     } else {
1.29      matthew   391:         return &Apache::lonnet::gettitle($self->{'symb'});
1.1       matthew   392:     }
1.29      matthew   393: }
                    394: 
                    395: sub get_full_title {
                    396:     my $self = shift;
                    397:     my @title = ($self->get_title());
1.2       matthew   398:     # Look up the users identifying information
                    399:     # Get the users information
1.34    ! albertel  400:     my $name = &Apache::loncommon::plainname($self->{'name'},
        !           401: 					     $self->{'domain'});
1.8       matthew   402:     push (@title,$name);
1.26      www       403:     push (@title,&Apache::lonlocal::locallocaltime(time));
1.8       matthew   404:     return @title;
1.1       matthew   405: }
                    406: 
                    407: sub parent_link {
                    408:     my $self = shift;
                    409:     my $link .= '<p><a href="/adm/studentcalc?'.
                    410:         'sname='.$self->{'name'}.
                    411:             '&sdomain='.$self->{'domain'}.'">'.
1.26      www       412:                 &mt('Student level sheet').'</a></p>'."\n";
1.1       matthew   413:     return $link;
                    414: }
                    415: 
                    416: sub outsheet_html {
                    417:     my $self = shift;
                    418:     my ($r) = @_;
1.29      matthew   419:     ####################################
                    420:     # Report any calculation errors    #
                    421:     ####################################
                    422:     $r->print($self->html_report_error());
1.1       matthew   423:     ###################################
                    424:     # Determine table structure
                    425:     ###################################
1.14      matthew   426:     my $importcolor = '#FFFFFF';
1.15      matthew   427:     my $exportcolor = '#FFFFAA';
1.1       matthew   428:     my $num_uneditable = 1;
                    429:     my $num_left = 52-$num_uneditable;
1.26      www       430:     my %lt=&Apache::lonlocal::texthash(
                    431: 				       'as' => 'Assessment',
                    432: 				       'ca' => 'Calculations',
                    433: 				       );
1.1       matthew   434:     my $tableheader =<<"END";
                    435: <table border="2">
                    436: <tr>
1.26      www       437:   <th colspan="2" rowspan="2"><font size="+2">$lt{'as'}</font></th>
1.14      matthew   438:   <td bgcolor="$importcolor" colspan="$num_uneditable">&nbsp;</td>
1.1       matthew   439:   <td colspan="$num_left">
1.26      www       440:       <b><font size="+1">$lt{'ca'}</font></b></td>
1.1       matthew   441: </tr><tr>
                    442: END
                    443:     my $label_num = 0;
                    444:     foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
                    445:         if ($label_num<$num_uneditable) { 
1.14      matthew   446:             $tableheader .= '<td bgcolor="'.$importcolor.'">';
1.1       matthew   447:         } else {
                    448:             $tableheader .= '<td>';
                    449:         }
                    450:         $tableheader .= "<b><font size=+1>$_</font></b></td>";
                    451:         $label_num++;
                    452:     }
                    453:     $tableheader.="</tr>\n";
                    454:     #
                    455:     $r->print($tableheader);
                    456:     #
                    457:     # Print out template row
                    458:     $r->print('<tr><td>Template</td><td>&nbsp;</td>'.
1.14      matthew   459: 	      $self->html_template_row($num_uneditable,$importcolor).
                    460:               "</tr>\n");
1.1       matthew   461:     #
                    462:     # Print out summary/export row
                    463:     $r->print('<tr><td>Export</td><td>0</td>'.
1.14      matthew   464: 	      $self->html_export_row($exportcolor)."</tr>\n");
1.1       matthew   465:     #
                    466:     # Prepare to output rows
                    467:     $tableheader =<<"END";
                    468: <table border="2">
                    469: <tr><th>row</th><th>Item</th>
                    470: END
                    471:     foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
                    472: 	if ($label_num<$num_uneditable) { 
1.14      matthew   473:             $tableheader.='<th bgcolor="'.$importcolor.'">';
1.1       matthew   474:         } else {
                    475:             $tableheader.='<th>';
                    476:         }
                    477:         $tableheader.="<b><font size=+1>$_</font></b></th>";
                    478:     }
                    479:     #
                    480:     my $num_output = 0;
1.8       matthew   481:     foreach my $rownum (sort {$a <=> $b} ($self->rows())) {
1.28      matthew   482:         if (! $self->parameter_part_is_valid(
                    483:                                              $self->{'formulas'}->{'A'.$rownum}
                    484:                                              )) {
                    485:             next;
                    486:         }
1.1       matthew   487: 	if ($num_output++ % 50 == 0) {
                    488: 	    $r->print("</table>\n".$tableheader);
                    489: 	}
                    490: 	$r->print('<tr><td>'.$rownum.'</td>'.
1.14      matthew   491:                   $self->assess_html_row($rownum,$importcolor)."</tr>\n");
1.1       matthew   492:     }
                    493:     $r->print("</table>\n");
                    494:     return;
                    495: }
                    496: 
                    497: sub assess_html_row {
                    498:     my $self = shift();
1.14      matthew   499:     my ($row,$importcolor) = @_;
1.1       matthew   500:     my $parameter_name = $self->{'formulas'}->{'A'.$row};
                    501:     my @rowdata = $self->get_row($row);
                    502:     my $num_cols_output = 0;
                    503:     my $row_html;
                    504:     if (exists($nice_parameter_name{$parameter_name})) {
                    505:         my $name = $nice_parameter_name{$parameter_name};
                    506:         $name =~ s/ /\&nbsp;/g;
                    507:         $row_html .= '<td>'.$name.'<br />'.$parameter_name.'</td>';
                    508:     } else {
                    509:         $row_html .= '<td>'.$parameter_name.'</td>';
                    510:     }
                    511:     foreach my $cell (@rowdata) {
1.10      matthew   512:         if ($num_cols_output < 1) {
1.14      matthew   513:             $row_html .= '<td bgcolor="'.$importcolor.'">';
1.10      matthew   514:             $row_html .= &Apache::Spreadsheet::html_uneditable_cell($cell,
                    515:                                                                     '#FFDDDD');
                    516:         } else {
                    517:             $row_html .= '<td bgcolor="#EOFFDD">';
                    518:             $row_html .= &Apache::Spreadsheet::html_editable_cell($cell,
                    519:                                                                   '#E0FFDD',1);
                    520:         }
1.1       matthew   521: 	$row_html .= '</td>';
1.10      matthew   522:         $num_cols_output++;
1.1       matthew   523:     }
                    524:     return $row_html;
                    525: }
                    526: 
1.9       matthew   527: sub csv_rows {
                    528:     # writes the meat of the spreadsheet to an excel worksheet.  Called
                    529:     # by Spreadsheet::outsheet_excel;
1.1       matthew   530:     my $self = shift;
1.30      matthew   531:     my ($connection,$filehandle) = @_;
1.9       matthew   532:     #
                    533:     # Write a header row
                    534:     $self->csv_output_row($filehandle,undef,
1.26      www       535:                           (&mt('Parameter'),&mt('Description'),&mt('Value')));
1.9       matthew   536:     #
                    537:     # Write each row
                    538:     foreach my $rownum (sort {$a <=> $b} ($self->rows())) {
                    539:         my $parameter_name = $self->{'formulas'}->{'A'.$rownum};
                    540:         my $description = '';
                    541:         if (exists($nice_parameter_name{$parameter_name})) {
                    542:             $description = $nice_parameter_name{$parameter_name};
                    543:         }
                    544:         $self->csv_output_row($filehandle,$rownum,
                    545:                               $parameter_name,$description);
                    546:     }
                    547:     return;
1.1       matthew   548: }
                    549: 
1.8       matthew   550: sub excel_rows {
                    551:     # writes the meat of the spreadsheet to an excel worksheet.  Called
                    552:     # by Spreadsheet::outsheet_excel;
1.1       matthew   553:     my $self = shift;
1.30      matthew   554:     my ($connection,$worksheet,$cols_output,$rows_output) = @_;
                    555:     return if (! ref($worksheet));
1.8       matthew   556:     #
                    557:     # Write a header row
                    558:     $cols_output = 0;
                    559:     foreach my $value ('Parameter','Description','Value') {
1.30      matthew   560:         $worksheet->write($rows_output,$cols_output++,$value);
1.8       matthew   561:     }
                    562:     $rows_output++;    
                    563:     #
                    564:     # Write each row
                    565:     foreach my $rownum (sort {$a <=> $b} ($self->rows())) {
                    566:         my $parameter_name = $self->{'formulas'}->{'A'.$rownum};
                    567:         my $description = '';
                    568:         if (exists($nice_parameter_name{$parameter_name})) {
                    569:             $description = $nice_parameter_name{$parameter_name};
                    570:         }
                    571:         $self->excel_output_row($worksheet,$rownum,$rows_output++,
                    572:                                 $parameter_name,$description);
                    573:     }
                    574:     return;
1.1       matthew   575: }
                    576: 
1.21      matthew   577: ##
                    578: ## Routines to support assesscalc::compute
                    579: ##
                    580: sub get_parm_names {
                    581:     my $self = shift;
                    582:     my @Mandatory_parameters = @_;
                    583:     my %parameters_and_names;
                    584:     #
1.23      albertel  585:     my ($symap,$syid,$srcf) = &Apache::lonnet::decode_symb($self->{'symb'});
1.21      matthew   586:     my @Metadata = split(/\,/,&Apache::lonnet::metadata($srcf,'keys'));
                    587:     foreach my $parm (@Mandatory_parameters,@Metadata) {
                    588:         next if ($parm !~ /^(resource\.|stores|parameter)_/);
                    589:         my $cleaned_name = $parm;
                    590:         $cleaned_name =~ s/^resource\./stores_/;
                    591:         $cleaned_name =~ s/\./_/g;
                    592:         my $display = &Apache::lonnet::metadata($srcf,
                    593:                                                 $cleaned_name.'.display');
                    594:         if (! $display) {
                    595:             $display .= &Apache::lonnet::metadata($srcf,$cleaned_name.'.name');
                    596:         }
                    597:         $parameters_and_names{$cleaned_name}=$display;
                    598:     }
                    599:     return (%parameters_and_names);
                    600: }
                    601: 
                    602: sub get_parameter_values {
                    603:     my $self = shift();
                    604:     my @Parameters;
                    605:     my ($parameters) = @_;
                    606:     if (!ref($parameters)) {
                    607:         @Parameters = @_;
                    608:     } elsif (ref($parameters) eq 'ARRAY') {
                    609:         @Parameters = @$parameters;
                    610:     } elsif (ref($parameters) eq 'HASH') {
                    611:         @Parameters = keys(%$parameters);
                    612:     }
                    613:     #
                    614:     my %parameters;
                    615:     #
                    616:     my $filename = $self->{'coursefilename'}.'_parms.db';
                    617:     if (tie(%parmhash,'GDBM_File',
                    618:             $self->{'coursefilename'}.'_parms.db',&GDBM_READER(),0640)) {
                    619:         foreach my $parmname (@Parameters) {
                    620:             my $value = $self->parmval($parmname);
                    621:             $parameters{$parmname} =$value;
                    622:         }
                    623:         untie(%parmhash);
                    624:     } else {
                    625:         $self->logthis('unable to tie '.$filename);
                    626:     }
                    627:     return %parameters;
                    628: }
                    629: 
                    630: sub deal_with_export_row {
                    631:     my $self = shift();
                    632:     my @exportarray = @_;
                    633:     $Exportrows{$self->{'symb'}}->{'time'} = time;
                    634:     $Exportrows{$self->{'symb'}}->{$self->{'filename'}} = \@exportarray;
                    635:     #
                    636:     # Save the export data
                    637:     $self->save_export_data();
                    638:     return;
                    639: }
                    640: 
1.22      matthew   641: sub get_problem_state {
                    642:     my $self = shift;
                    643:     my %student_parameters;
                    644:     if (exists($userdata{$self->{'symb'}}) && 
                    645:         ref($userdata{$self->{'symb'}}) eq 'HASH') {
                    646:         %student_parameters = %{$userdata{$self->{'symb'}}};
                    647:     }
                    648:     return %student_parameters;
                    649: }
1.21      matthew   650: 
1.28      matthew   651: sub determine_parts {
                    652:     my $self = shift;
                    653:     if (exists($self->{'Parts'}) && ref($self->{'Parts'}) eq 'HASH') {
                    654:         return;
                    655:     }
                    656:     my (undef,undef,$url) = &Apache::lonnet::decode_symb($self->{'symb'});
                    657:     my $src = &Apache::lonnet::clutter($url);
                    658:     return if (! defined($src));
                    659:     my %Parts;
                    660:     my $metadata = &Apache::lonnet::metadata($src,'packages');
                    661:     foreach (split(',',$metadata)) {
                    662:         my ($part) = (/^part_(.*)$/);
                    663:         if (defined($part) && 
                    664:             ! &Apache::loncommon::check_if_partid_hidden
                    665:                 ($part,$self->{'symb'},$self->{'name'},$self->{'domain'})
                    666:             ) {
                    667:             $Parts{$part}++;
                    668:         }
                    669:     }
                    670:     # Make sure part 0 is defined.
                    671:     $Parts{'0'}++;
                    672:     $self->{'Parts'} = \%Parts;
                    673:     return;
                    674: }
                    675: 
                    676: sub parameter_part_is_valid {
                    677:     my $self = shift;
                    678:     my ($parameter) = @_;
                    679:     return 1 if ($parameter eq 'timestamp');
                    680:     if (! defined($self->{'Parts'}) || 
                    681:         ! ref ($self->{'Parts'})    ||
                    682:         ref($self->{'Parts'}) ne 'HASH') {
                    683:         return 1;
                    684:     }
                    685:     #
                    686:     my (undef,$part) = 
                    687:         ($parameter =~ m/^(resource|stores|parameter)_([^_]+)_.*/);
                    688:     if (exists($self->{'Parts'})          && 
                    689:         exists($self->{'Parts'}->{$part}) &&
                    690:         $self->{'Parts'}->{$part} ) {
                    691:         return 1;
                    692:     } else {
                    693:         return 0;
                    694:     }
                    695: }
                    696: 
1.1       matthew   697: sub compute {
                    698:     my $self = shift;
1.19      matthew   699:     my ($r) = @_;
                    700:     my $connection = $r->connection();
                    701:     if ($connection->aborted()) { $self->cleanup(); return; }
1.1       matthew   702:     $self->initialize_safe_space();
1.11      matthew   703:     #########################################
                    704:     #########################################
                    705:     ###                                   ###
                    706:     ###  Retrieve the problem parameters  ###
                    707:     ###                                   ###
                    708:     #########################################
                    709:     #########################################
                    710:     my @Mandatory_parameters = ("stores_0_solved",
                    711:                                 "stores_0_awarddetail",
                    712:                                 "stores_0_awarded",
                    713:                                 "timestamp",
                    714:                                 "stores_0_tries",
                    715:                                 "stores_0_award");
1.1       matthew   716:     #
                    717:     # Definitions
                    718:     undef(%nice_parameter_name);
                    719:     my %parameters;   # holds underscored parameters by name
                    720:     #
                    721:     # Get the metadata fields and determine their proper names
1.21      matthew   722:     my %nice_parm_names = $self->get_parm_names(@Mandatory_parameters);
                    723:     while (my($cleaned_name,$display) = each(%nice_parm_names)) {
1.1       matthew   724:         $parameters{$cleaned_name}++;
                    725:         $nice_parameter_name{$cleaned_name} = $display;
                    726:     }
                    727:     #
                    728:     # Get the values of the metadata fields
1.19      matthew   729:     if ($connection->aborted()) { $self->cleanup(); return; }
1.22      matthew   730:     $self->ensure_current_caches();
1.19      matthew   731:     if ($connection->aborted()) { $self->cleanup(); return; }
1.21      matthew   732:     %parameters = $self->get_parameter_values(keys(%parameters));
1.19      matthew   733:     if ($connection->aborted()) { $self->cleanup(); return; }
1.1       matthew   734:     #
                    735:     # Clean out unnecessary parameters
                    736:     foreach (keys(%parameters)) {
                    737:         delete($parameters{$_}) if (! /(resource\.|stores_|parameter_)/);
                    738:     }
                    739:     #
                    740:     # Get the students performance data
1.22      matthew   741:     my %student_parameters = $self->get_problem_state();
1.1       matthew   742:     while (my ($parm,$value) = each(%student_parameters)) {
                    743:         $parm =~ s/^resource\./stores_/;
                    744:         $parm =~ s/\./_/g;
                    745:         $parameters{$parm} = $value;
1.28      matthew   746:     }
                    747:     #
                    748:     # Clean out any bad parameters
                    749:     $self->determine_parts();
                    750:     foreach my $param (keys(%parameters)) {
                    751:         if (! $self->parameter_part_is_valid($param)) {
                    752:             delete ($parameters{$param});
                    753:         }
1.1       matthew   754:     }
                    755:     #
                    756:     # Set up the formulas and parameter values
                    757:     my %f=$self->formulas();
                    758:     my %c;
                    759:     #
1.5       matthew   760:     # Check for blackout requirements
                    761:     if ((!exists($ENV{'request.role.adv'}) || !$ENV{'request.role.adv'})) {
                    762:         while (my ($parm,$value) = each(%parameters)) {
                    763:             last if ($self->blackout());
                    764:             next if ($parm !~ /^(parameter_.*)_problemstatus$/);
1.24      matthew   765:             if ($parameters{$1.'_answerdate'} ne '' &&
1.21      matthew   766:                 $parameters{$1.'_answerdate'} < time) {
                    767:                 next;
                    768:             }
1.5       matthew   769:             if (lc($value) eq 'no') {
                    770:                 # We must blackout this sheet
                    771:                 $self->blackout(1);
                    772:             }
                    773:         }
                    774:     }
1.19      matthew   775:     if ($connection->aborted()) { $self->cleanup(); return; }
1.5       matthew   776:     #
                    777:     # Move the parameters into the spreadsheet
1.1       matthew   778:     while (my ($parm,$value) = each(%parameters)) {
                    779:         my $cell = 'A'.$self->get_row_number_from_key($parm);
                    780:         $f{$cell} = $parm;
1.31      matthew   781:         if ($parm =~ /_submission$/ && $value =~ /(\{|\})/) {
                    782:             $value = 'witheld';
                    783:         }
1.32      matthew   784:         $value = 'q{'.$value.'}' if ($value =~/([^\d\.]|\.\.)/);
1.6       matthew   785:         $c{$parm} = $value;
1.1       matthew   786:     }
1.6       matthew   787:     $self->formulas(\%f);
                    788:     $self->constants(\%c);
1.19      matthew   789:     if ($connection->aborted()) { $self->cleanup(); return; }
1.1       matthew   790:     $self->calcsheet();
                    791:     #
                    792:     # Store export row in cache
                    793:     my @exportarray = $self->exportrow();
1.21      matthew   794:     $self->deal_with_export_row(@exportarray);
1.12      matthew   795:     $self->save() if ($self->need_to_save());
1.19      matthew   796:     if ($connection->aborted()) { $self->cleanup(); return; }
1.1       matthew   797:     return;
                    798: }
                    799: 
                    800: ##
                    801: ## sett overrides Spreadsheet::sett
                    802: ##
                    803: sub sett {
                    804:     my $self = shift;
                    805:     my %t=();
                    806:     #
                    807:     # Deal with the template row by copying the template formulas into each
                    808:     # row.
                    809:     foreach my $col ($self->template_cells()) {
                    810:         next if ($col=~/^A/);
                    811:         foreach my $row ($self->rows()) {
                    812:             # Get the name of this cell
                    813:             my $cell=$col.$row;
                    814:             # Grab the template declaration
                    815:             $t{$cell}=$self->formula('template_'.$col);
                    816:             # Replace '#' with the row number
                    817:             $t{$cell}=~s/\#/$row/g;
                    818:             # Replace '....' with ','
                    819:             $t{$cell}=~s/\.\.+/\,/g;
                    820:             # Replace 'A0' with the value from 'A0'
                    821:             $t{$cell}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
                    822:             # Replace parameters
                    823:             $t{$cell}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.$self->expandnamed($2)/ge;
                    824:         }
                    825:     }
                    826:     #
                    827:     # Deal with the cells which have formulas
                    828:     while (my ($cell,$formula) = each(%{$self->{'formulas'}})) {
                    829: 	next if ($cell =~ /template_/);
                    830:         if ($cell =~ /^A/ && $cell ne 'A0') {
                    831:             if ($formula !~ /^\!/) {
                    832:                 $t{$cell}=$self->{'constants'}->{$formula};
                    833:             }
                    834:         } else {
                    835:             $t{$cell}=$formula;
                    836:             $t{$cell}=~s/\.\.+/\,/g;
                    837:             $t{$cell}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
                    838:             $t{$cell}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.$self->expandnamed($2)/ge;
                    839:         }
                    840:     }
                    841:     # Put %t into the safe space
                    842:     %{$self->{'safe'}->varglob('t')}=%t;
                    843: }
                    844: 
                    845: 
                    846: ########################################################
                    847: ########################################################
                    848: 
                    849: =pod
                    850: 
                    851: =item &load_cached_export_rows()
                    852: 
                    853: Retrieves and parsers the export rows of the assessment spreadsheets.
                    854: These rows are saved in the students directory in the format:
                    855: 
                    856:  sname:sdom:assesscalc:symb.time => time
                    857: 
                    858:  sname:sdom:assesscalc:symb => filename___=___Adata___;___Bdata___;___ ...
                    859: 
                    860: =cut
                    861: 
                    862: ########################################################
                    863: ########################################################
                    864: sub load_cached_export_rows {
1.18      matthew   865:     undef(%Exportrows);
1.1       matthew   866:     my @tmp = &Apache::lonnet::dump('nohist_calculatedsheets_'.
                    867:                                     $ENV{'request.course.id'},
                    868:                                     $current_domain,$current_name,undef);
                    869:     if ($tmp[0]!~/^error/) {
                    870:         my %tmp = @tmp;
                    871:         my $default_filename =  $ENV{'course.'.$ENV{'request.course.id'}.
                    872:                                          '.spreadsheet_default_assesscalc'};
                    873:         # We only got one key, so we will access it directly.
                    874:         while (my ($key,$sheetdata) = each(%tmp)) {
                    875:             my ($sname,$sdom,$sheettype,$symb) = split(':',$key);
1.33      matthew   876:             if (! defined($sname) || $sname eq '' ||
                    877:                 ! defined($sdom)  || $sdom eq '' ) {
                    878:                 next;
                    879:             }
1.1       matthew   880:             if ($symb =~ /\.time$/) {
                    881:                 $symb =~ s/\.time$//;
                    882:                 $Exportrows{$symb}->{'time'} = $sheetdata;
                    883:             } else {
                    884:                 $sheetdata =~ s/^(.*)___=___//;
                    885:                 my $filename = $1;
                    886:                 $filename = $default_filename if (! defined($filename));
                    887:                 my @Data = split('___;___',$sheetdata);
                    888:                 $Exportrows{$symb}->{$filename} = \@Data;
                    889:             }
                    890:         }
                    891:     }
                    892: }
                    893: 
                    894: #############################################
                    895: #############################################
                    896: 
                    897: =pod
                    898: 
                    899: =item &export_data
                    900: 
                    901: Returns the export data associated with the spreadsheet.  Computes the
                    902: spreadsheet only if necessary.
                    903: 
                    904: =cut
                    905: 
                    906: #############################################
                    907: #############################################
                    908: sub export_data {
                    909:     my $self = shift;
1.19      matthew   910:     my ($r) = @_;
                    911:     my $connection = $r->connection();
1.1       matthew   912:     my $symb = $self->{'symb'};
1.5       matthew   913:     if (! exists($ENV{'request.role.adv'}) || ! $ENV{'request.role.adv'} ||
                    914:         ! exists($Exportrows{$symb}) || ! defined($Exportrows{$symb})  ||
1.1       matthew   915:         ! $self->check_expiration_time($Exportrows{$symb}->{'time'}) ||
                    916:         ! exists($Exportrows{$symb}->{$self->{'filename'}}) ||
1.18      matthew   917:         ! defined($Exportrows{$symb}->{$self->{'filename'}}) ||
                    918:         ! ref($Exportrows{$symb}->{$self->{'filename'}}) 
                    919:         ) {
1.19      matthew   920:         $self->compute($r);
1.1       matthew   921:     }
1.19      matthew   922:     if ($connection->aborted()) { $self->cleanup(); return; }
1.29      matthew   923:     my @Data;
                    924:     if ($self->badcalc()) {
                    925:         @Data = ();
                    926:     } else {
                    927:         @Data = @{$Exportrows{$symb}->{$self->{'filename'}}};
                    928:         if ($Data[0] =~ /^(.*)___=___/) {
                    929:             $self->{'sheetname'} = $1;
                    930:             $Data[0] =~ s/^(.*)___=___//;
                    931:         }
                    932:         for (my $i=0;$i<$#Data;$i++) {
                    933:             if ($Data[$i]=~/\D/ && defined($Data[$i])) {
                    934:                 $Data[$i]="'".$Data[$i]."'";
                    935:             }
                    936:         }
1.1       matthew   937:     }
                    938:     return @Data;
                    939: }
                    940: 
                    941: #############################################
                    942: #############################################
                    943: 
                    944: =pod
                    945: 
                    946: =item &save_export_data()
                    947: 
                    948: Writes the export data for this spreadsheet to the students cache.
                    949: 
                    950: =cut
                    951: 
                    952: #############################################
                    953: #############################################
                    954: sub save_export_data {
                    955:     my $self = shift;
1.7       matthew   956:     return if ($self->temporary());
1.1       matthew   957:     my $student = $self->{'name'}.':'.$self->{'domain'};
                    958:     my $symb    = $self->{'symb'};
1.29      matthew   959:     if ($self->badcalc()){
                    960:         # do not save data away when calculations have not been done properly.
                    961:         delete($Exportrows{$symb});
                    962:         return;
                    963:     }
1.1       matthew   964:     if (! exists($Exportrows{$symb}) || 
                    965:         ! exists($Exportrows{$symb}->{$self->{'filename'}})) {
                    966:         return;
                    967:     }
                    968:     my $key = join(':',($self->{'name'},$self->{'domain'},'assesscalc',$symb));
                    969:     my $timekey = $key.'.time';
1.22      matthew   970:     my $newstore= join('___;___',
                    971:                        map {s/[^[:print:]]//g;$_;} # strip out unprintable
                    972:                                 @{$Exportrows{$symb}->{$self->{'filename'}}});
1.1       matthew   973:     $newstore = $self->{'filename'}.'___=___'.$newstore;
1.22      matthew   974:     $newExportrows{$student}->{$key} = $newstore;
                    975:     $newExportrows{$student}->{$timekey} = $Exportrows{$symb}->{'time'};
1.1       matthew   976:     return;
                    977: }
                    978: 
                    979: 1;
                    980: 
                    981: __END__

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