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

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

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