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

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

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