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

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

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