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

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

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