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

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

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