File:  [LON-CAPA] / loncom / interface / spreadsheet / assesscalc.pm
Revision 1.17.2.2: download - view: text, annotated - select for diffs
Fri Oct 3 15:39:46 2003 UTC (20 years, 9 months ago) by albertel
Branches: version_1_0_2
CVS tags: version_1_0_3
Diff to branchpoint 1.17: preferred, unified
- backport 1.25

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

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