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

1.1       matthew     1: #
1.36    ! albertel    2: # $Id: assesscalc.pm,v 1.35 2004/12/08 00:15:18 albertel 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:     # check map parms
                    341:     my $thisparm = $parmhash{$symbparm};
                    342:     return $thisparm if (defined($thisparm));
                    343:     # check default
                    344:     $thisparm = &Apache::lonnet::metadata($fn,$rwhat.'.default');
                    345:     return $thisparm if (defined($thisparm));
1.36    ! albertel  346:     # check more course
        !           347:     return $courseopt{$courselevelm} if (defined($courseopt{$courselevelm}));
        !           348:     return $courseopt{$courselevel}  if (defined($courseopt{$courselevel}));
        !           349: 
1.1       matthew   350:     # Cascade Up
                    351:     my $space=$what;
1.35      albertel  352:     $space=~s/\.[^._]+$//;
1.1       matthew   353:     if ($space ne '0') {
                    354: 	my @parts=split(/_/,$space);
                    355: 	my $id=pop(@parts);
                    356: 	my $part=join('_',@parts);
                    357: 	if ($part eq '') { $part='0'; }
                    358: 	my $newwhat=$rwhat;
                    359: 	$newwhat=~s/\Q$space\E/$part/;
1.17      albertel  360: 	my $partgeneral=$self->parmval($newwhat,$symb,$uname,$udom,$csec,1);
1.1       matthew   361: 	if (defined($partgeneral)) { return $partgeneral; }
                    362:     }
1.17      albertel  363:     if ($recurse) { return undef; }
                    364:     my $pack_def=&Apache::lonnet::packages_tab_default($fn,'resource.'.$what);
                    365:     if (defined($pack_def)) { return $pack_def; }
1.1       matthew   366:     #nothing defined
                    367:     return '';
                    368: }
                    369: 
1.8       matthew   370: sub get_html_title {
                    371:     my $self = shift;
1.29      matthew   372:     my ($assess_title,$name,$time) = $self->get_full_title();
1.8       matthew   373:     my $title = '<h1>'.$assess_title.'</h1>'.
                    374:         '<h2>'.$name.', '.
                    375:         &Apache::loncommon::aboutmewrapper
                    376:                          ($self->{'name'}.'@'.$self->{'domain'},
                    377:                           $self->{'name'},$self->{'domain'});
                    378:     $title .= '<h3>'.$time.'</h3>';
                    379:     return $title;
                    380: }
                    381: 
1.1       matthew   382: sub get_title {
                    383:     my $self = shift;
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/^_//;
1.29      matthew   390:         return $assess_title;
1.1       matthew   391:     } else {
1.29      matthew   392:         return &Apache::lonnet::gettitle($self->{'symb'});
1.1       matthew   393:     }
1.29      matthew   394: }
                    395: 
                    396: sub get_full_title {
                    397:     my $self = shift;
                    398:     my @title = ($self->get_title());
1.2       matthew   399:     # Look up the users identifying information
                    400:     # Get the users information
1.34      albertel  401:     my $name = &Apache::loncommon::plainname($self->{'name'},
                    402: 					     $self->{'domain'});
1.8       matthew   403:     push (@title,$name);
1.26      www       404:     push (@title,&Apache::lonlocal::locallocaltime(time));
1.8       matthew   405:     return @title;
1.1       matthew   406: }
                    407: 
                    408: sub parent_link {
                    409:     my $self = shift;
                    410:     my $link .= '<p><a href="/adm/studentcalc?'.
                    411:         'sname='.$self->{'name'}.
                    412:             '&sdomain='.$self->{'domain'}.'">'.
1.26      www       413:                 &mt('Student level sheet').'</a></p>'."\n";
1.1       matthew   414:     return $link;
                    415: }
                    416: 
                    417: sub outsheet_html {
                    418:     my $self = shift;
                    419:     my ($r) = @_;
1.29      matthew   420:     ####################################
                    421:     # Report any calculation errors    #
                    422:     ####################################
                    423:     $r->print($self->html_report_error());
1.1       matthew   424:     ###################################
                    425:     # Determine table structure
                    426:     ###################################
1.14      matthew   427:     my $importcolor = '#FFFFFF';
1.15      matthew   428:     my $exportcolor = '#FFFFAA';
1.1       matthew   429:     my $num_uneditable = 1;
                    430:     my $num_left = 52-$num_uneditable;
1.26      www       431:     my %lt=&Apache::lonlocal::texthash(
                    432: 				       'as' => 'Assessment',
                    433: 				       'ca' => 'Calculations',
                    434: 				       );
1.1       matthew   435:     my $tableheader =<<"END";
                    436: <table border="2">
                    437: <tr>
1.26      www       438:   <th colspan="2" rowspan="2"><font size="+2">$lt{'as'}</font></th>
1.14      matthew   439:   <td bgcolor="$importcolor" colspan="$num_uneditable">&nbsp;</td>
1.1       matthew   440:   <td colspan="$num_left">
1.26      www       441:       <b><font size="+1">$lt{'ca'}</font></b></td>
1.1       matthew   442: </tr><tr>
                    443: END
                    444:     my $label_num = 0;
                    445:     foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
                    446:         if ($label_num<$num_uneditable) { 
1.14      matthew   447:             $tableheader .= '<td bgcolor="'.$importcolor.'">';
1.1       matthew   448:         } else {
                    449:             $tableheader .= '<td>';
                    450:         }
                    451:         $tableheader .= "<b><font size=+1>$_</font></b></td>";
                    452:         $label_num++;
                    453:     }
                    454:     $tableheader.="</tr>\n";
                    455:     #
                    456:     $r->print($tableheader);
                    457:     #
                    458:     # Print out template row
                    459:     $r->print('<tr><td>Template</td><td>&nbsp;</td>'.
1.14      matthew   460: 	      $self->html_template_row($num_uneditable,$importcolor).
                    461:               "</tr>\n");
1.1       matthew   462:     #
                    463:     # Print out summary/export row
                    464:     $r->print('<tr><td>Export</td><td>0</td>'.
1.14      matthew   465: 	      $self->html_export_row($exportcolor)."</tr>\n");
1.1       matthew   466:     #
                    467:     # Prepare to output rows
                    468:     $tableheader =<<"END";
                    469: <table border="2">
                    470: <tr><th>row</th><th>Item</th>
                    471: END
                    472:     foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
                    473: 	if ($label_num<$num_uneditable) { 
1.14      matthew   474:             $tableheader.='<th bgcolor="'.$importcolor.'">';
1.1       matthew   475:         } else {
                    476:             $tableheader.='<th>';
                    477:         }
                    478:         $tableheader.="<b><font size=+1>$_</font></b></th>";
                    479:     }
                    480:     #
                    481:     my $num_output = 0;
1.8       matthew   482:     foreach my $rownum (sort {$a <=> $b} ($self->rows())) {
1.28      matthew   483:         if (! $self->parameter_part_is_valid(
                    484:                                              $self->{'formulas'}->{'A'.$rownum}
                    485:                                              )) {
                    486:             next;
                    487:         }
1.1       matthew   488: 	if ($num_output++ % 50 == 0) {
                    489: 	    $r->print("</table>\n".$tableheader);
                    490: 	}
                    491: 	$r->print('<tr><td>'.$rownum.'</td>'.
1.14      matthew   492:                   $self->assess_html_row($rownum,$importcolor)."</tr>\n");
1.1       matthew   493:     }
                    494:     $r->print("</table>\n");
                    495:     return;
                    496: }
                    497: 
                    498: sub assess_html_row {
                    499:     my $self = shift();
1.14      matthew   500:     my ($row,$importcolor) = @_;
1.1       matthew   501:     my $parameter_name = $self->{'formulas'}->{'A'.$row};
                    502:     my @rowdata = $self->get_row($row);
                    503:     my $num_cols_output = 0;
                    504:     my $row_html;
                    505:     if (exists($nice_parameter_name{$parameter_name})) {
                    506:         my $name = $nice_parameter_name{$parameter_name};
                    507:         $name =~ s/ /\&nbsp;/g;
                    508:         $row_html .= '<td>'.$name.'<br />'.$parameter_name.'</td>';
                    509:     } else {
                    510:         $row_html .= '<td>'.$parameter_name.'</td>';
                    511:     }
                    512:     foreach my $cell (@rowdata) {
1.10      matthew   513:         if ($num_cols_output < 1) {
1.14      matthew   514:             $row_html .= '<td bgcolor="'.$importcolor.'">';
1.10      matthew   515:             $row_html .= &Apache::Spreadsheet::html_uneditable_cell($cell,
                    516:                                                                     '#FFDDDD');
                    517:         } else {
                    518:             $row_html .= '<td bgcolor="#EOFFDD">';
                    519:             $row_html .= &Apache::Spreadsheet::html_editable_cell($cell,
                    520:                                                                   '#E0FFDD',1);
                    521:         }
1.1       matthew   522: 	$row_html .= '</td>';
1.10      matthew   523:         $num_cols_output++;
1.1       matthew   524:     }
                    525:     return $row_html;
                    526: }
                    527: 
1.9       matthew   528: sub csv_rows {
                    529:     # writes the meat of the spreadsheet to an excel worksheet.  Called
                    530:     # by Spreadsheet::outsheet_excel;
1.1       matthew   531:     my $self = shift;
1.30      matthew   532:     my ($connection,$filehandle) = @_;
1.9       matthew   533:     #
                    534:     # Write a header row
                    535:     $self->csv_output_row($filehandle,undef,
1.26      www       536:                           (&mt('Parameter'),&mt('Description'),&mt('Value')));
1.9       matthew   537:     #
                    538:     # Write each row
                    539:     foreach my $rownum (sort {$a <=> $b} ($self->rows())) {
                    540:         my $parameter_name = $self->{'formulas'}->{'A'.$rownum};
                    541:         my $description = '';
                    542:         if (exists($nice_parameter_name{$parameter_name})) {
                    543:             $description = $nice_parameter_name{$parameter_name};
                    544:         }
                    545:         $self->csv_output_row($filehandle,$rownum,
                    546:                               $parameter_name,$description);
                    547:     }
                    548:     return;
1.1       matthew   549: }
                    550: 
1.8       matthew   551: sub excel_rows {
                    552:     # writes the meat of the spreadsheet to an excel worksheet.  Called
                    553:     # by Spreadsheet::outsheet_excel;
1.1       matthew   554:     my $self = shift;
1.30      matthew   555:     my ($connection,$worksheet,$cols_output,$rows_output) = @_;
                    556:     return if (! ref($worksheet));
1.8       matthew   557:     #
                    558:     # Write a header row
                    559:     $cols_output = 0;
                    560:     foreach my $value ('Parameter','Description','Value') {
1.30      matthew   561:         $worksheet->write($rows_output,$cols_output++,$value);
1.8       matthew   562:     }
                    563:     $rows_output++;    
                    564:     #
                    565:     # Write each row
                    566:     foreach my $rownum (sort {$a <=> $b} ($self->rows())) {
                    567:         my $parameter_name = $self->{'formulas'}->{'A'.$rownum};
                    568:         my $description = '';
                    569:         if (exists($nice_parameter_name{$parameter_name})) {
                    570:             $description = $nice_parameter_name{$parameter_name};
                    571:         }
                    572:         $self->excel_output_row($worksheet,$rownum,$rows_output++,
                    573:                                 $parameter_name,$description);
                    574:     }
                    575:     return;
1.1       matthew   576: }
                    577: 
1.21      matthew   578: ##
                    579: ## Routines to support assesscalc::compute
                    580: ##
                    581: sub get_parm_names {
                    582:     my $self = shift;
                    583:     my @Mandatory_parameters = @_;
                    584:     my %parameters_and_names;
                    585:     #
1.23      albertel  586:     my ($symap,$syid,$srcf) = &Apache::lonnet::decode_symb($self->{'symb'});
1.21      matthew   587:     my @Metadata = split(/\,/,&Apache::lonnet::metadata($srcf,'keys'));
                    588:     foreach my $parm (@Mandatory_parameters,@Metadata) {
                    589:         next if ($parm !~ /^(resource\.|stores|parameter)_/);
                    590:         my $cleaned_name = $parm;
                    591:         $cleaned_name =~ s/^resource\./stores_/;
                    592:         $cleaned_name =~ s/\./_/g;
                    593:         my $display = &Apache::lonnet::metadata($srcf,
                    594:                                                 $cleaned_name.'.display');
                    595:         if (! $display) {
                    596:             $display .= &Apache::lonnet::metadata($srcf,$cleaned_name.'.name');
                    597:         }
                    598:         $parameters_and_names{$cleaned_name}=$display;
                    599:     }
                    600:     return (%parameters_and_names);
                    601: }
                    602: 
                    603: sub get_parameter_values {
                    604:     my $self = shift();
                    605:     my @Parameters;
                    606:     my ($parameters) = @_;
                    607:     if (!ref($parameters)) {
                    608:         @Parameters = @_;
                    609:     } elsif (ref($parameters) eq 'ARRAY') {
                    610:         @Parameters = @$parameters;
                    611:     } elsif (ref($parameters) eq 'HASH') {
                    612:         @Parameters = keys(%$parameters);
                    613:     }
                    614:     #
                    615:     my %parameters;
                    616:     #
                    617:     my $filename = $self->{'coursefilename'}.'_parms.db';
                    618:     if (tie(%parmhash,'GDBM_File',
                    619:             $self->{'coursefilename'}.'_parms.db',&GDBM_READER(),0640)) {
                    620:         foreach my $parmname (@Parameters) {
                    621:             my $value = $self->parmval($parmname);
                    622:             $parameters{$parmname} =$value;
                    623:         }
                    624:         untie(%parmhash);
                    625:     } else {
                    626:         $self->logthis('unable to tie '.$filename);
                    627:     }
                    628:     return %parameters;
                    629: }
                    630: 
                    631: sub deal_with_export_row {
                    632:     my $self = shift();
                    633:     my @exportarray = @_;
                    634:     $Exportrows{$self->{'symb'}}->{'time'} = time;
                    635:     $Exportrows{$self->{'symb'}}->{$self->{'filename'}} = \@exportarray;
                    636:     #
                    637:     # Save the export data
                    638:     $self->save_export_data();
                    639:     return;
                    640: }
                    641: 
1.22      matthew   642: sub get_problem_state {
                    643:     my $self = shift;
                    644:     my %student_parameters;
                    645:     if (exists($userdata{$self->{'symb'}}) && 
                    646:         ref($userdata{$self->{'symb'}}) eq 'HASH') {
                    647:         %student_parameters = %{$userdata{$self->{'symb'}}};
                    648:     }
                    649:     return %student_parameters;
                    650: }
1.21      matthew   651: 
1.28      matthew   652: sub determine_parts {
                    653:     my $self = shift;
                    654:     if (exists($self->{'Parts'}) && ref($self->{'Parts'}) eq 'HASH') {
                    655:         return;
                    656:     }
                    657:     my (undef,undef,$url) = &Apache::lonnet::decode_symb($self->{'symb'});
                    658:     my $src = &Apache::lonnet::clutter($url);
                    659:     return if (! defined($src));
                    660:     my %Parts;
                    661:     my $metadata = &Apache::lonnet::metadata($src,'packages');
                    662:     foreach (split(',',$metadata)) {
                    663:         my ($part) = (/^part_(.*)$/);
                    664:         if (defined($part) && 
                    665:             ! &Apache::loncommon::check_if_partid_hidden
                    666:                 ($part,$self->{'symb'},$self->{'name'},$self->{'domain'})
                    667:             ) {
                    668:             $Parts{$part}++;
                    669:         }
                    670:     }
                    671:     # Make sure part 0 is defined.
                    672:     $Parts{'0'}++;
                    673:     $self->{'Parts'} = \%Parts;
                    674:     return;
                    675: }
                    676: 
                    677: sub parameter_part_is_valid {
                    678:     my $self = shift;
                    679:     my ($parameter) = @_;
                    680:     return 1 if ($parameter eq 'timestamp');
                    681:     if (! defined($self->{'Parts'}) || 
                    682:         ! ref ($self->{'Parts'})    ||
                    683:         ref($self->{'Parts'}) ne 'HASH') {
                    684:         return 1;
                    685:     }
                    686:     #
                    687:     my (undef,$part) = 
                    688:         ($parameter =~ m/^(resource|stores|parameter)_([^_]+)_.*/);
                    689:     if (exists($self->{'Parts'})          && 
                    690:         exists($self->{'Parts'}->{$part}) &&
                    691:         $self->{'Parts'}->{$part} ) {
                    692:         return 1;
                    693:     } else {
                    694:         return 0;
                    695:     }
                    696: }
                    697: 
1.1       matthew   698: sub compute {
                    699:     my $self = shift;
1.19      matthew   700:     my ($r) = @_;
                    701:     my $connection = $r->connection();
                    702:     if ($connection->aborted()) { $self->cleanup(); return; }
1.1       matthew   703:     $self->initialize_safe_space();
1.11      matthew   704:     #########################################
                    705:     #########################################
                    706:     ###                                   ###
                    707:     ###  Retrieve the problem parameters  ###
                    708:     ###                                   ###
                    709:     #########################################
                    710:     #########################################
                    711:     my @Mandatory_parameters = ("stores_0_solved",
                    712:                                 "stores_0_awarddetail",
                    713:                                 "stores_0_awarded",
                    714:                                 "timestamp",
                    715:                                 "stores_0_tries",
                    716:                                 "stores_0_award");
1.1       matthew   717:     #
                    718:     # Definitions
                    719:     undef(%nice_parameter_name);
                    720:     my %parameters;   # holds underscored parameters by name
                    721:     #
                    722:     # Get the metadata fields and determine their proper names
1.21      matthew   723:     my %nice_parm_names = $self->get_parm_names(@Mandatory_parameters);
                    724:     while (my($cleaned_name,$display) = each(%nice_parm_names)) {
1.1       matthew   725:         $parameters{$cleaned_name}++;
                    726:         $nice_parameter_name{$cleaned_name} = $display;
                    727:     }
                    728:     #
                    729:     # Get the values of the metadata fields
1.19      matthew   730:     if ($connection->aborted()) { $self->cleanup(); return; }
1.22      matthew   731:     $self->ensure_current_caches();
1.19      matthew   732:     if ($connection->aborted()) { $self->cleanup(); return; }
1.21      matthew   733:     %parameters = $self->get_parameter_values(keys(%parameters));
1.19      matthew   734:     if ($connection->aborted()) { $self->cleanup(); return; }
1.1       matthew   735:     #
                    736:     # Clean out unnecessary parameters
                    737:     foreach (keys(%parameters)) {
                    738:         delete($parameters{$_}) if (! /(resource\.|stores_|parameter_)/);
                    739:     }
                    740:     #
                    741:     # Get the students performance data
1.22      matthew   742:     my %student_parameters = $self->get_problem_state();
1.1       matthew   743:     while (my ($parm,$value) = each(%student_parameters)) {
                    744:         $parm =~ s/^resource\./stores_/;
                    745:         $parm =~ s/\./_/g;
                    746:         $parameters{$parm} = $value;
1.28      matthew   747:     }
                    748:     #
                    749:     # Clean out any bad parameters
                    750:     $self->determine_parts();
                    751:     foreach my $param (keys(%parameters)) {
                    752:         if (! $self->parameter_part_is_valid($param)) {
                    753:             delete ($parameters{$param});
                    754:         }
1.1       matthew   755:     }
                    756:     #
                    757:     # Set up the formulas and parameter values
                    758:     my %f=$self->formulas();
                    759:     my %c;
                    760:     #
1.5       matthew   761:     # Check for blackout requirements
                    762:     if ((!exists($ENV{'request.role.adv'}) || !$ENV{'request.role.adv'})) {
                    763:         while (my ($parm,$value) = each(%parameters)) {
                    764:             last if ($self->blackout());
                    765:             next if ($parm !~ /^(parameter_.*)_problemstatus$/);
1.24      matthew   766:             if ($parameters{$1.'_answerdate'} ne '' &&
1.21      matthew   767:                 $parameters{$1.'_answerdate'} < time) {
                    768:                 next;
                    769:             }
1.5       matthew   770:             if (lc($value) eq 'no') {
                    771:                 # We must blackout this sheet
                    772:                 $self->blackout(1);
                    773:             }
                    774:         }
                    775:     }
1.19      matthew   776:     if ($connection->aborted()) { $self->cleanup(); return; }
1.5       matthew   777:     #
                    778:     # Move the parameters into the spreadsheet
1.1       matthew   779:     while (my ($parm,$value) = each(%parameters)) {
                    780:         my $cell = 'A'.$self->get_row_number_from_key($parm);
                    781:         $f{$cell} = $parm;
1.31      matthew   782:         if ($parm =~ /_submission$/ && $value =~ /(\{|\})/) {
                    783:             $value = 'witheld';
                    784:         }
1.32      matthew   785:         $value = 'q{'.$value.'}' if ($value =~/([^\d\.]|\.\.)/);
1.6       matthew   786:         $c{$parm} = $value;
1.1       matthew   787:     }
1.6       matthew   788:     $self->formulas(\%f);
                    789:     $self->constants(\%c);
1.19      matthew   790:     if ($connection->aborted()) { $self->cleanup(); return; }
1.1       matthew   791:     $self->calcsheet();
                    792:     #
                    793:     # Store export row in cache
                    794:     my @exportarray = $self->exportrow();
1.21      matthew   795:     $self->deal_with_export_row(@exportarray);
1.12      matthew   796:     $self->save() if ($self->need_to_save());
1.19      matthew   797:     if ($connection->aborted()) { $self->cleanup(); return; }
1.1       matthew   798:     return;
                    799: }
                    800: 
                    801: ##
                    802: ## sett overrides Spreadsheet::sett
                    803: ##
                    804: sub sett {
                    805:     my $self = shift;
                    806:     my %t=();
                    807:     #
                    808:     # Deal with the template row by copying the template formulas into each
                    809:     # row.
                    810:     foreach my $col ($self->template_cells()) {
                    811:         next if ($col=~/^A/);
                    812:         foreach my $row ($self->rows()) {
                    813:             # Get the name of this cell
                    814:             my $cell=$col.$row;
                    815:             # Grab the template declaration
                    816:             $t{$cell}=$self->formula('template_'.$col);
                    817:             # Replace '#' with the row number
                    818:             $t{$cell}=~s/\#/$row/g;
                    819:             # Replace '....' with ','
                    820:             $t{$cell}=~s/\.\.+/\,/g;
                    821:             # Replace 'A0' with the value from 'A0'
                    822:             $t{$cell}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
                    823:             # Replace parameters
                    824:             $t{$cell}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.$self->expandnamed($2)/ge;
                    825:         }
                    826:     }
                    827:     #
                    828:     # Deal with the cells which have formulas
                    829:     while (my ($cell,$formula) = each(%{$self->{'formulas'}})) {
                    830: 	next if ($cell =~ /template_/);
                    831:         if ($cell =~ /^A/ && $cell ne 'A0') {
                    832:             if ($formula !~ /^\!/) {
                    833:                 $t{$cell}=$self->{'constants'}->{$formula};
                    834:             }
                    835:         } else {
                    836:             $t{$cell}=$formula;
                    837:             $t{$cell}=~s/\.\.+/\,/g;
                    838:             $t{$cell}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
                    839:             $t{$cell}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.$self->expandnamed($2)/ge;
                    840:         }
                    841:     }
                    842:     # Put %t into the safe space
                    843:     %{$self->{'safe'}->varglob('t')}=%t;
                    844: }
                    845: 
                    846: 
                    847: ########################################################
                    848: ########################################################
                    849: 
                    850: =pod
                    851: 
                    852: =item &load_cached_export_rows()
                    853: 
                    854: Retrieves and parsers the export rows of the assessment spreadsheets.
                    855: These rows are saved in the students directory in the format:
                    856: 
                    857:  sname:sdom:assesscalc:symb.time => time
                    858: 
                    859:  sname:sdom:assesscalc:symb => filename___=___Adata___;___Bdata___;___ ...
                    860: 
                    861: =cut
                    862: 
                    863: ########################################################
                    864: ########################################################
                    865: sub load_cached_export_rows {
1.18      matthew   866:     undef(%Exportrows);
1.1       matthew   867:     my @tmp = &Apache::lonnet::dump('nohist_calculatedsheets_'.
                    868:                                     $ENV{'request.course.id'},
                    869:                                     $current_domain,$current_name,undef);
                    870:     if ($tmp[0]!~/^error/) {
                    871:         my %tmp = @tmp;
                    872:         my $default_filename =  $ENV{'course.'.$ENV{'request.course.id'}.
                    873:                                          '.spreadsheet_default_assesscalc'};
                    874:         # We only got one key, so we will access it directly.
                    875:         while (my ($key,$sheetdata) = each(%tmp)) {
                    876:             my ($sname,$sdom,$sheettype,$symb) = split(':',$key);
1.33      matthew   877:             if (! defined($sname) || $sname eq '' ||
                    878:                 ! defined($sdom)  || $sdom eq '' ) {
                    879:                 next;
                    880:             }
1.1       matthew   881:             if ($symb =~ /\.time$/) {
                    882:                 $symb =~ s/\.time$//;
                    883:                 $Exportrows{$symb}->{'time'} = $sheetdata;
                    884:             } else {
                    885:                 $sheetdata =~ s/^(.*)___=___//;
                    886:                 my $filename = $1;
                    887:                 $filename = $default_filename if (! defined($filename));
                    888:                 my @Data = split('___;___',$sheetdata);
                    889:                 $Exportrows{$symb}->{$filename} = \@Data;
                    890:             }
                    891:         }
                    892:     }
                    893: }
                    894: 
                    895: #############################################
                    896: #############################################
                    897: 
                    898: =pod
                    899: 
                    900: =item &export_data
                    901: 
                    902: Returns the export data associated with the spreadsheet.  Computes the
                    903: spreadsheet only if necessary.
                    904: 
                    905: =cut
                    906: 
                    907: #############################################
                    908: #############################################
                    909: sub export_data {
                    910:     my $self = shift;
1.19      matthew   911:     my ($r) = @_;
                    912:     my $connection = $r->connection();
1.1       matthew   913:     my $symb = $self->{'symb'};
1.5       matthew   914:     if (! exists($ENV{'request.role.adv'}) || ! $ENV{'request.role.adv'} ||
                    915:         ! exists($Exportrows{$symb}) || ! defined($Exportrows{$symb})  ||
1.1       matthew   916:         ! $self->check_expiration_time($Exportrows{$symb}->{'time'}) ||
                    917:         ! exists($Exportrows{$symb}->{$self->{'filename'}}) ||
1.18      matthew   918:         ! defined($Exportrows{$symb}->{$self->{'filename'}}) ||
                    919:         ! ref($Exportrows{$symb}->{$self->{'filename'}}) 
                    920:         ) {
1.19      matthew   921:         $self->compute($r);
1.1       matthew   922:     }
1.19      matthew   923:     if ($connection->aborted()) { $self->cleanup(); return; }
1.29      matthew   924:     my @Data;
                    925:     if ($self->badcalc()) {
                    926:         @Data = ();
                    927:     } else {
                    928:         @Data = @{$Exportrows{$symb}->{$self->{'filename'}}};
                    929:         if ($Data[0] =~ /^(.*)___=___/) {
                    930:             $self->{'sheetname'} = $1;
                    931:             $Data[0] =~ s/^(.*)___=___//;
                    932:         }
                    933:         for (my $i=0;$i<$#Data;$i++) {
                    934:             if ($Data[$i]=~/\D/ && defined($Data[$i])) {
                    935:                 $Data[$i]="'".$Data[$i]."'";
                    936:             }
                    937:         }
1.1       matthew   938:     }
                    939:     return @Data;
                    940: }
                    941: 
                    942: #############################################
                    943: #############################################
                    944: 
                    945: =pod
                    946: 
                    947: =item &save_export_data()
                    948: 
                    949: Writes the export data for this spreadsheet to the students cache.
                    950: 
                    951: =cut
                    952: 
                    953: #############################################
                    954: #############################################
                    955: sub save_export_data {
                    956:     my $self = shift;
1.7       matthew   957:     return if ($self->temporary());
1.1       matthew   958:     my $student = $self->{'name'}.':'.$self->{'domain'};
                    959:     my $symb    = $self->{'symb'};
1.29      matthew   960:     if ($self->badcalc()){
                    961:         # do not save data away when calculations have not been done properly.
                    962:         delete($Exportrows{$symb});
                    963:         return;
                    964:     }
1.1       matthew   965:     if (! exists($Exportrows{$symb}) || 
                    966:         ! exists($Exportrows{$symb}->{$self->{'filename'}})) {
                    967:         return;
                    968:     }
                    969:     my $key = join(':',($self->{'name'},$self->{'domain'},'assesscalc',$symb));
                    970:     my $timekey = $key.'.time';
1.22      matthew   971:     my $newstore= join('___;___',
                    972:                        map {s/[^[:print:]]//g;$_;} # strip out unprintable
                    973:                                 @{$Exportrows{$symb}->{$self->{'filename'}}});
1.1       matthew   974:     $newstore = $self->{'filename'}.'___=___'.$newstore;
1.22      matthew   975:     $newExportrows{$student}->{$key} = $newstore;
                    976:     $newExportrows{$student}->{$timekey} = $Exportrows{$symb}->{'time'};
1.1       matthew   977:     return;
                    978: }
                    979: 
                    980: 1;
                    981: 
                    982: __END__

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