File:  [LON-CAPA] / loncom / interface / spreadsheet / assesscalc.pm
Revision 1.18: download - view: text, annotated - select for diffs
Fri Sep 5 01:06:45 2003 UTC (20 years, 10 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Minor modifications brought about due to making warnings fatal.

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

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