File:  [LON-CAPA] / loncom / interface / spreadsheet / Spreadsheet.pm
Revision 1.55: download - view: text, annotated - select for diffs
Thu Sep 1 21:47:14 2005 UTC (18 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- when changing the course env, need to also change the local users session environment

    1: #
    2: # $Id: Spreadsheet.pm,v 1.55 2005/09/01 21:47:14 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: Spreadsheet
   34: 
   35: =head1 SYNOPSIS
   36: 
   37: =head1 DESCRIPTION
   38: 
   39: =over 4
   40: 
   41: =cut
   42: 
   43: ###################################################
   44: ###################################################
   45: ###                 Spreadsheet                 ###
   46: ###################################################
   47: ###################################################
   48: package Apache::Spreadsheet;
   49: 
   50: use strict;
   51: #use warnings FATAL=>'all';
   52: #no warnings 'uninitialized';
   53: use Apache::Constants qw(:common :http);
   54: use Apache::lonnet;
   55: use Safe;
   56: use Safe::Hole;
   57: use Opcode;
   58: use HTML::Entities();
   59: use HTML::TokeParser;
   60: use Spreadsheet::WriteExcel;
   61: use Time::HiRes;
   62: use Apache::lonlocal;
   63: 
   64: ##
   65: ## Package Variables
   66: ##
   67: my %expiredates;
   68: 
   69: my @UC_Columns = split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
   70: my @LC_Columns = split(//,'abcdefghijklmnopqrstuvwxyz');
   71: 
   72: ######################################################
   73: 
   74: =pod
   75: 
   76: =item &new
   77: 
   78: Returns a new spreadsheet object.
   79: 
   80: =cut
   81: 
   82: ######################################################
   83: sub new {
   84:     my $this = shift;
   85:     my $class = ref($this) || $this;
   86:     my ($stype) = ($class =~ /Apache::(.*)$/);
   87:     #
   88:     my ($name,$domain,$filename,$usymb)=@_;
   89:     if (defined($usymb) && ref($usymb)) {
   90:         $usymb = $usymb->symb;
   91:     }
   92:     if (! defined($name) || $name eq '') {
   93:         $name = $env{'user.name'};
   94:     }
   95:     if (! defined($domain) || $domain eq '') {
   96:         $domain = $env{'user.domain'};
   97:     }
   98:     #
   99:     my $self = {
  100:         name     => $name,
  101:         domain   => $domain,
  102:         type     => $stype,
  103:         symb     => $usymb,
  104:         errorlog => '',
  105:         maxrow   => 0,
  106:         cid      => $env{'request.course.id'},
  107:         cnum     => $env{'course.'.$env{'request.course.id'}.'.num'},
  108:         cdom     => $env{'course.'.$env{'request.course.id'}.'.domain'},
  109:         coursedesc => $env{'course.'.$env{'request.course.id'}.'.description'},
  110:         coursefilename => $env{'request.course.fn'},
  111:         #
  112:         # Flags
  113:         temporary => 0,  # true if this sheet has been modified but not saved
  114:         new_rows  => 0,  # true if this sheet has new rows
  115: 	loaded    => 0,  # true if the formulas have been loaded
  116:         #
  117:         # blackout is used to determine if any data needs to be hidden from the
  118:         # student.
  119:         blackout => 0,
  120:         #
  121:         # Data storage
  122:         formulas    => {},
  123:         constants   => {},
  124:         rows        => [],
  125:         row_source  => {}, 
  126:         othersheets => [],
  127:     };
  128:     #
  129:     bless($self,$class);
  130:     $self->filename($filename);
  131:     #
  132:     return $self;
  133: }
  134: 
  135: ######################################################
  136: 
  137: =pod
  138: 
  139: =item &filename
  140: 
  141: get or set the filename for a spreadsheet.
  142: 
  143: =cut
  144: 
  145: ######################################################
  146: sub filename {
  147:     my $self = shift();
  148:     if (@_) {
  149:         my ($newfilename) = @_;
  150:         if (! defined($newfilename) || $newfilename eq 'Default' ||
  151:             $newfilename !~ /\w/ || $newfilename eq '') {
  152:             my $key = 'course.'.$self->{'cid'}.'.spreadsheet_default_'.
  153:                 $self->{'type'};
  154:             if (exists($env{$key}) && $env{$key} ne '') {
  155:                 $newfilename = $env{$key};
  156:             } else {
  157:                 $newfilename = 'default_'.$self->{'type'};
  158:             }
  159:         }
  160:         if ($newfilename !~ /\w/ || $newfilename =~ /^\W*$/) {
  161:             $newfilename = 'default_'.$self->{'type'};
  162:         }
  163:         if ($newfilename !~ /^default\.$self->{'type'}$/ &&
  164:             $newfilename !~ /^\/res\/(.*)spreadsheet$/) {
  165:             if ($newfilename !~ /_$self->{'type'}$/) {
  166:                 $newfilename =~ s/[\s_]*$//;
  167:                 $newfilename .= '_'.$self->{'type'};
  168:             }
  169:         }
  170:         $self->{'filename'} = $newfilename;
  171:         return;
  172:     }
  173:     return $self->{'filename'};
  174: }
  175: 
  176: ######################################################
  177: 
  178: =pod
  179: 
  180: =item &make_default()
  181: 
  182: Make the current spreadsheet file the default for the course.  Expires all the
  183: default spreadsheets.......!
  184: 
  185: =cut
  186: 
  187: ######################################################
  188: sub make_default {
  189:     my $self = shift();
  190:     my $result = &Apache::lonnet::put('environment',
  191:             {'spreadsheet_default_'.$self->{'type'} => $self->filename()},
  192:                                      $self->{'cdom'},$self->{'cnum'});
  193:     return $result if ($result ne 'ok');
  194:     &Apache::lonnet::appenv('course.'.$self->{'cid'}.'.spreadsheet_default_'.
  195: 			    $self->{'type'} => $self->filename());
  196:     my $symb = $self->{'symb'};
  197:     $symb = '' if (! defined($symb));
  198:     &Apache::lonnet::expirespread('','',$self->{'type'},$symb);    
  199: }
  200: 
  201: ######################################################
  202: 
  203: =pod
  204: 
  205: =item &is_default()
  206: 
  207: Returns 1 if the current spreadsheet is the default as specified in the
  208: course environment.  Returns 0 otherwise.
  209: 
  210: =cut
  211: 
  212: ######################################################
  213: sub is_default {
  214:     my $self = shift;
  215:     # Check to find out if we are the default spreadsheet (filenames match)
  216:     my $default_filename = '';
  217:     my %tmphash = &Apache::lonnet::get('environment',
  218:                                        ['spreadsheet_default_'.
  219:                                         $self->{'type'}],
  220:                                        $self->{'cdom'},
  221:                                        $self->{'cnum'});
  222:     my ($tmp) = keys(%tmphash);
  223:     if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
  224:         $default_filename = $tmphash{'spreadsheet_default_'.$self->{'type'}};
  225:     }
  226:     if ($default_filename =~ /^\s*$/) {
  227:         $default_filename = 'default_'.$self->{'type'};
  228:     }
  229:     return 1 if ($self->filename() eq $default_filename);
  230:     return 0;
  231: }
  232: 
  233: sub initialize {
  234:     # This method is here to remind you that it will be overridden by
  235:     # the descendents of the spreadsheet class.
  236: }
  237: 
  238: sub clear_package {
  239:     # This method is here to remind you that it will be overridden by
  240:     # the descendents of the spreadsheet class.
  241: }
  242: 
  243: sub cleanup {
  244:     my $self = shift();
  245:     $self->clear_package();
  246: }
  247: 
  248: sub initialize_spreadsheet_package {
  249:     &load_spreadsheet_expirationdates();
  250:     &clear_spreadsheet_definition_cache();
  251: }
  252: 
  253: sub load_spreadsheet_expirationdates {
  254:     undef %expiredates;
  255:     my $cid=$env{'request.course.id'};
  256:     my @tmp = &Apache::lonnet::dump('nohist_expirationdates',
  257:                                     $env{'course.'.$cid.'.domain'},
  258:                                     $env{'course.'.$cid.'.num'});
  259:     if (lc($tmp[0]) !~ /^error/){
  260:         %expiredates = @tmp;
  261:     }
  262: }
  263: 
  264: sub check_expiration_time {
  265:     my $self = shift;
  266:     my ($time)=@_;
  267:     return 0 if (! defined($time));
  268:     my ($key1,$key2,$key3,$key4,$key5);
  269:     # Description of keys
  270:     #
  271:     # key1: all sheets of this type have expired
  272:     # key2: all sheets of this type for this student
  273:     # key3: all sheets of this type in this map for this student
  274:     # key4: this assessment sheet for this student
  275:     # key5: this assessment sheet for all students
  276:     $key1 = '::'.$self->{'type'}.':';
  277:     $key2 = $self->{'name'}.':'.$self->{'domain'}.':'.$self->{'type'}.':';
  278:     $key3 = $key2.$self->{'container'} if (defined($self->{'container'}));
  279:     $key4 = $key2.$self->{'symb'} if (defined($self->{'symb'}));
  280:     $key5 = $key1.$self->{'symb'} if (defined($self->{'symb'}));
  281:     my $returnvalue = 1; # default to okay
  282:     foreach my $key ($key1,$key2,$key3,$key4,$key5) {
  283:         next if (! defined($key));
  284:         if (exists($expiredates{$key}) && $expiredates{$key} > $time) {
  285:             $returnvalue = 0; # need to recompute
  286:         }
  287:     }
  288:     return $returnvalue;
  289: }
  290: 
  291: ######################################################
  292: 
  293: =pod
  294: 
  295: =item &initialize_safe_space
  296: 
  297: Returns the safe space required by a Spreadsheet object.
  298: 
  299: =head 2 Safe Space Functions
  300: 
  301: =over 4
  302: 
  303: =cut
  304: 
  305: ######################################################
  306: { 
  307: 
  308:     my $safeeval;
  309: 
  310: sub initialize_safe_space {
  311:   my $self = shift;
  312:   my $usection = &Apache::lonnet::getsection($self->{'domain'},
  313:                                              $self->{'name'},
  314:                                              $env{'request.course.id'});
  315:   if (! defined($safeeval)) {
  316:       $safeeval = new Safe(shift);
  317:       my $safehole = new Safe::Hole;
  318:       $safeeval->permit("entereval");
  319:       $safeeval->permit(":base_math");
  320:       $safeeval->permit("sort");
  321:       $safeeval->deny(":base_io");
  322:       $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&Apache::lonnet::EXT');
  323:       $safehole->wrap(\&mask,$safeeval,'&mask');
  324:       $safehole->wrap(\&Apache::lonnet::logthis,$safeeval,'&logthis');
  325:       $safeeval->share('$@');
  326:     # Holds the (computed, final) values for the sheet
  327:     # This is only written to by &calc, the spreadsheet computation routine.
  328:     # It is read by many functions
  329:       $safeeval->share('%sheet_values');
  330:       my $code=<<'ENDDEFS';
  331: # ---------------------------------------------------- Inside of the safe space
  332: #
  333: # f: formulas
  334: # t: intermediate format (variable references expanded)
  335: # v: output values
  336: # c: preloaded constants (A-column)
  337: # rl: row label
  338: # os: other spreadsheets (for student spreadsheet only)
  339: undef %t; # Holds the forumlas of the spreadsheet to be computed. Set in
  340:     # &sett, which does the translation of strings like C5 into the value
  341:     # in C5. Used in &calc - %t holds the values that are actually eval'd.
  342: undef %f;    # Holds the formulas for each cell.  This is the users
  343:     # (spreadsheet authors) data for each cell.
  344: undef %c; # Holds the constants for a sheet.  In the assessment
  345:     # sheets, this is the A column.  Used in &MINPARM, &MAXPARM, &expandnamed,
  346:     # &sett, and &constants.  There is no &getconstants.
  347:     # &constants is called by &loadstudent, &loadcourse, &load assessment,
  348: undef @os;  # Holds the names of other spreadsheets - this is used to specify
  349:     # the spreadsheets that are available for the assessment sheet.
  350:     # Set by &setothersheets.  &setothersheets is called by &handler.  A
  351:     # related subroutine is &othersheets.
  352: $errorlog = '';
  353: #
  354: $maxrow = 0;
  355: $type = '';
  356: #
  357: # filename/reference of the sheet
  358: $filename = '';
  359: #
  360: # user data
  361: $name = '';
  362: $domain  = '';
  363: #
  364: # course data
  365: $csec = '';
  366: $cnum = '';
  367: $cdom = '';
  368: $cid  = '';
  369: $coursefilename  = '';
  370: #
  371: # symb
  372: $usymb = '';
  373: #
  374: # error messages
  375: $errormsg = '';
  376: #
  377: #-------------------------------------------------------
  378: 
  379: =pod
  380: 
  381: =item EXT(parameter)
  382: 
  383: Calls the system EXT function to determine the value of the given parameter.
  384: 
  385: =cut
  386: 
  387: #-------------------------------------------------------
  388: sub EXT {
  389:     my ($parameter) = @_;
  390:     return '' if (! defined($parameter) || $parameter eq '');
  391:     $parameter =~ s/^parameter\./resource\./;
  392:     my $value = &Apache::lonnet::EXT($parameter,$symb,$domain,$name,$usection);
  393:     return $value;
  394: }
  395: 
  396: #-------------------------------------------------------
  397: 
  398: =pod
  399: 
  400: =item NUM(range)
  401: 
  402: returns the number of items in the range.
  403: 
  404: =cut
  405: 
  406: #-------------------------------------------------------
  407: sub NUM {
  408:     my $values=&get_values(@_);
  409:     my $num= scalar(@$values);
  410:     return $num;   
  411: }
  412: 
  413: #-------------------------------------------------------
  414: 
  415: =pod
  416: 
  417: =item BIN(low,high,lower,upper)
  418: 
  419: =cut
  420: 
  421: #-------------------------------------------------------
  422: sub BIN {
  423:     my ($low,$high,$lower,$upper)=@_;
  424:     my $values=&get_values($lower,$upper);
  425:     my $num=0;
  426:     foreach (@$values) {
  427:         if (($_>=$low) && ($_<=$high)) {
  428:             $num++;
  429:         }
  430:     }
  431:     return $num;   
  432: }
  433: 
  434: #-------------------------------------------------------
  435: 
  436: =pod
  437: 
  438: =item SUM(range)
  439: 
  440: returns the sum of items in the range.
  441: 
  442: =cut
  443: 
  444: #-------------------------------------------------------
  445: sub SUM {
  446:     my $values=&get_values(@_);
  447:     my $sum=0;
  448:     foreach (@$values) {
  449:         $sum+=$_;
  450:     }
  451:     return $sum;   
  452: }
  453: 
  454: #-------------------------------------------------------
  455: 
  456: =pod
  457: 
  458: =item MEAN(range)
  459: 
  460: compute the average of the items in the range.
  461: 
  462: =cut
  463: 
  464: #-------------------------------------------------------
  465: sub MEAN {
  466:     my $values=&get_values(@_);
  467:     my $sum=0; 
  468:     my $num=0;
  469:     foreach (@$values) {
  470:         $sum+=$_;
  471:         $num++;
  472:     }
  473:     if ($num) {
  474:        return $sum/$num;
  475:     } else {
  476:        return undef;
  477:     }   
  478: }
  479: 
  480: #-------------------------------------------------------
  481: 
  482: =pod
  483: 
  484: =item STDDEV(range)
  485: 
  486: compute the standard deviation of the items in the range.
  487: 
  488: =cut
  489: 
  490: #-------------------------------------------------------
  491: sub STDDEV {
  492:     my $values=&get_values(@_);
  493:     my $sum=0; my $num=0;
  494:     foreach (@$values) {
  495:         $sum+=$_;
  496:         $num++;
  497:     }
  498:     unless ($num>1) { return undef; }
  499:     my $mean=$sum/$num;
  500:     $sum=0;
  501:     foreach (@$values) {
  502:         $sum+=($_-$mean)**2;
  503:     }
  504:     return sqrt($sum/($num-1));    
  505: }
  506: 
  507: #-------------------------------------------------------
  508: 
  509: =pod
  510: 
  511: =item PROD(range)
  512: 
  513: compute the product of the items in the range.
  514: 
  515: =cut
  516: 
  517: #-------------------------------------------------------
  518: sub PROD {
  519:     my $values=&get_values(@_);
  520:     my $prod=1;
  521:     foreach (@$values) {
  522:         $prod*=$_;
  523:     }
  524:     return $prod;   
  525: }
  526: 
  527: #-------------------------------------------------------
  528: 
  529: =pod
  530: 
  531: =item MAX(range)
  532: 
  533: compute the maximum of the items in the range.
  534: 
  535: =cut
  536: 
  537: #-------------------------------------------------------
  538: sub MAX {
  539:     my $values=&get_values(@_);
  540:     my $max='-';
  541:     foreach (@$values) {
  542:         if (($_>$max) || ($max eq '-')) { 
  543:             $max=$_; 
  544:         }
  545:     } 
  546:     return $max;   
  547: }
  548: 
  549: #-------------------------------------------------------
  550: 
  551: =pod
  552: 
  553: =item MIN(range)
  554: 
  555: compute the minimum of the items in the range.
  556: 
  557: =cut
  558: 
  559: #-------------------------------------------------------
  560: sub MIN {
  561:     my $values=&get_values(@_);
  562:     my $min='-';
  563:     foreach (@$values) {
  564:         if (($_<$min) || ($min eq '-')) { 
  565:             $min=$_; 
  566:         }
  567:     }
  568:     return $min;   
  569: }
  570: 
  571: #-------------------------------------------------------
  572: 
  573: =pod
  574: 
  575: =item SUMMAX(num,lower,upper)
  576: 
  577: compute the sum of the largest 'num' items in the range from
  578: 'lower' to 'upper'
  579: 
  580: =cut
  581: 
  582: #-------------------------------------------------------
  583: sub SUMMAX {
  584:     my ($num,$lower,$upper)=@_;
  585:     my $values=&get_values($lower,$upper);
  586:     my @inside=sort {$a <=> $b} (@$values);
  587:     my $sum=0; my $i;
  588:     for ($i=$#inside;(($i>$#inside-$num) && ($i>=0));$i--) { 
  589:         $sum+=$inside[$i];
  590:     }
  591:     return $sum;   
  592: }
  593: 
  594: #-------------------------------------------------------
  595: 
  596: =pod
  597: 
  598: =item SUMMIN(num,lower,upper)
  599: 
  600: compute the sum of the smallest 'num' items in the range from
  601: 'lower' to 'upper'
  602: 
  603: =cut
  604: 
  605: #-------------------------------------------------------
  606: sub SUMMIN {
  607:     my ($num,$lower,$upper)=@_;
  608:     my $values=&get_values($lower,$upper);
  609:     my @inside=sort {$a <=> $b} (@$values);
  610:     my $sum=0; my $i;
  611:     for ($i=0;(($i<$num) && ($i<=$#inside));$i++) { 
  612:         $sum+=$inside[$i];
  613:     }
  614:     return $sum;   
  615: }
  616: 
  617: #-------------------------------------------------------
  618: 
  619: =pod
  620: 
  621: =item MINPARM(parametername)
  622: 
  623: Returns the minimum value of the parameters matching the parametername.
  624: parametername should be a string such as 'duedate'.
  625: 
  626: =cut
  627: 
  628: #-------------------------------------------------------
  629: sub MINPARM {
  630:     my ($expression) = @_;
  631:     my $min = undef;
  632:     foreach $parameter (keys(%c)) {
  633:         next if ($parameter !~ /$expression/);
  634:         if ((! defined($min)) || ($min > $c{$parameter})) {
  635:             $min = $c{$parameter} 
  636:         }
  637:     }
  638:     return $min;
  639: }
  640: 
  641: #-------------------------------------------------------
  642: 
  643: =pod
  644: 
  645: =item MAXPARM(parametername)
  646: 
  647: Returns the maximum value of the parameters matching the input parameter name.
  648: parametername should be a string such as 'duedate'.
  649: 
  650: =cut
  651: 
  652: #-------------------------------------------------------
  653: sub MAXPARM {
  654:     my ($expression) = @_;
  655:     my $max = undef;
  656:     foreach $parameter (keys(%c)) {
  657:         next if ($parameter !~ /$expression/);
  658:         if ((! defined($min)) || ($max < $c{$parameter})) {
  659:             $max = $c{$parameter} 
  660:         }
  661:     }
  662:     return $max;
  663: }
  664: 
  665: #-------------------------------------------------------
  666: 
  667: =pod
  668: 
  669: =item  &get_values($lower,$upper)
  670: 
  671: Inputs: $lower and $upper, cell names ("X12" or "a150") or globs ("X*").
  672: 
  673: Returns: an array ref of the values of the cells that exist in the 
  674:          speced range
  675: 
  676: =cut
  677: 
  678: #-------------------------------------------------------
  679: sub get_values {
  680:     my ($lower,$upper)=@_;
  681:     $upper = $lower if (! defined($upper));
  682:     my @values;
  683:     my ($la,$ld) = ($lower=~/([A-z]|\*)(\d+|\*)/);
  684:     my ($ua,$ud) = ($upper=~/([A-z]|\*)(\d+|\*)/);
  685:     my ($alpha,$num);
  686:     if ($ld ne '*' && $ud ne '*') {
  687: 	my @alpha;
  688: 	if (($la eq '*') || ($ua eq '*')) {
  689: 	    @alpha=('A'..'z');
  690: 	} else {
  691: 	    if ($la gt $ua) { ($la,$ua)=($ua,$la); }
  692: 	    if ((lc($la) ne $la) && (lc($ua) eq $ua)) {
  693: 		@alpha=($la..'Z','a'..$ua);
  694: 	    } else {
  695: 		@alpha=($la..$ua);
  696:             }
  697: 	}
  698: 	my @num=($ld..$ud);
  699: 	foreach my $a (@alpha) {
  700: 	    foreach my $n (@num) {
  701: 		if (exists($sheet_values{$a.$n})) {
  702: 		    push(@values,$sheet_values{$a.$n});
  703: 		}
  704: 	    }
  705: 	}
  706: 	return \@values;
  707:     } else {
  708: 	$num = '(\d+)';
  709:     }
  710:     if (($la eq '*') || ($ua eq '*')) {
  711:         $alpha='[A-z]';
  712:     } else {
  713: 	if ($la gt $ua) { ($la,$ua)=($ua,$la); }
  714:         $alpha=qq/[$la-$ua]/;
  715:     }
  716:     my $expression = '^'.$alpha.$num.'$';
  717:     foreach (grep /$expression/,keys(%sheet_values)) {
  718: 	push(@values,$sheet_values{$_});
  719:     }
  720:     return \@values;
  721: }
  722: 
  723: sub calc {
  724:     my $notfinished = 1;
  725:     my $lastcalc = '';
  726:     my $depth = 0;
  727:     while ($notfinished) {
  728: 	$notfinished=0;
  729:         while (my ($cell,$value) = each(%t)) {
  730:             my $old=$sheet_values{$cell};
  731:             $sheet_values{$cell}=eval $value;
  732: #            $errorlog .= $cell.' = '.$old.'->'.$sheet_values{$cell}."\n";
  733: 	    if ($@) {
  734: 		undef %sheet_values;
  735:                 return $cell.': '.$@;
  736:             }
  737: 	    if ($sheet_values{$cell} ne $old) { 
  738:                 $notfinished=1; 
  739:                 $lastcalc=$cell; 
  740:             }
  741:         }
  742: #        $errorlog.="------------------------------------------------";
  743: 
  744:         $depth++;
  745:         if ($depth>100) {
  746: 	    undef %sheet_values;
  747:             return $lastcalc.': Maximum calculation depth exceeded';
  748:         }
  749:     }
  750:     return 'okay';
  751: }
  752: 
  753: # ------------------------------------------- End of "Inside of the safe space"
  754: ENDDEFS
  755:         $safeeval->reval($code);
  756:     }
  757:     $self->{'safe'} = $safeeval;
  758:     $self->{'root'} = $self->{'safe'}->root();
  759:     #
  760:     # Place some of the %$self  items into the safe space except the safe space
  761:     # itself
  762:     my $initstring = '';
  763:     foreach (qw/name domain type symb cid csec coursefilename
  764:              cnum cdom/) {
  765:         $initstring.= qq{\$$_="$self->{$_}";};
  766:     }
  767:     $initstring.=qq{\$usection="$usection";};
  768:     $self->{'safe'}->reval($initstring);
  769:     return $self;
  770: }
  771: 
  772: }
  773: 
  774: ######################################################
  775: 
  776: =pod
  777: 
  778: =back
  779: 
  780: =cut
  781: 
  782: ######################################################
  783: 
  784: ##
  785: ## sub add_hash_to_safe {} # spreadsheet, would like to destroy
  786: ##
  787: 
  788: #
  789: # expandnamed used to reside in the safe space
  790: #
  791: sub expandnamed {
  792:     my $self = shift;
  793:     my $expression=shift;
  794:     if ($expression=~/^\&/) {
  795: 	my ($func,$var,$formula)=($expression=~/^\&(\w+)\(([^\;]+)\;(.*)\)/);
  796: 	my @vars=split(/\W+/,$formula);
  797:         my %values=();
  798: 	foreach my $varname ( @vars ) {
  799:             if ($varname=~/^(parameter|stores|timestamp)/) {
  800:                 $formula=~s/$varname/'$c{\''.$varname.'\'}'/ge;
  801:                $varname=~s/$var/\([\\w:\\- ]\+\)/g;
  802: 	       foreach (keys(%{$self->{'constants'}})) {
  803: 		  if ($_=~/$varname/) {
  804: 		      $values{$1}=1;
  805:                   }
  806:                }
  807: 	    }
  808:         }
  809:         if ($func eq 'EXPANDSUM') {
  810:             my $result='';
  811: 	    foreach (keys(%values)) {
  812:                 my $thissum=$formula;
  813:                 $thissum=~s/$var/$_/g;
  814:                 $result.=$thissum.'+';
  815:             } 
  816:             $result=~s/\+$//;
  817:             return $result;
  818:         } else {
  819: 	    return 0;
  820:         }
  821:     } else {
  822:         # it is not a function, so it is a parameter name
  823:         # We should do the following:
  824:         #    1. Take the list of parameter names
  825:         #    2. look through the list for ones that match the parameter we want
  826:         #    3. If there are no collisions, return the one that matches
  827:         #    4. If there is a collision, return 'bad parameter name error'
  828:         my $returnvalue = '';
  829:         my @matches = ();
  830:         my @values = ();
  831:         $#matches = -1;
  832:         while (my($parameter,$value) = each(%{$self->{'constants'}})) {
  833:             next if ($parameter !~ /$expression/);
  834:             push(@matches,$parameter);
  835:             push(@values,$value);
  836:         }
  837:         if (scalar(@matches) == 0) {
  838:             $returnvalue = '""';#'"unmatched parameter: '.$parameter.'"';
  839:         } elsif (scalar(@matches) == 1) {
  840:             # why do we not do this lookup here, instead of delaying it?
  841:             $returnvalue = $values[0];
  842:         } elsif (scalar(@matches) > 0) {
  843:             # more than one match.  Look for a concise one
  844:             $returnvalue =  "'non-unique parameter name : $expression'";
  845:             for (my $i=0; $i<=$#matches;$i++) {
  846:                 if ($matches[$i] =~ /^$expression$/) {
  847:                     # why do we not do this lookup here?
  848:                     $returnvalue = $values[$i];
  849:                 }
  850:             }
  851:         } else {
  852:             # There was a negative number of matches, which indicates 
  853:             # something is wrong with reality.  Better warn the user.
  854:             $returnvalue = '"bizzare parameter: '.$expression.'"';
  855:         }
  856:         return $returnvalue;
  857:     }
  858: }
  859: 
  860: sub sett {
  861:     my $self = shift;
  862:     my %t=();
  863:     undef(%Apache::Spreadsheet::sheet_values);
  864:     #
  865:     # Deal with the template row
  866:     foreach my $col ($self->template_cells()) {
  867:         next if ($col=~/^[A-Z]/);
  868:         foreach my $row ($self->rows()) {
  869:             # Get the name of this cell
  870:             my $cell=$col.$row;
  871:             # Grab the template declaration
  872:             $t{$cell}=$self->formula('template_'.$col);
  873:             # Replace '#' with the row number
  874:             $t{$cell}=~s/\#/$row/g;
  875:             # Replace '....' with ','
  876:             $t{$cell}=~s/\.\.+/\,/g;
  877:             # Replace 'A0' with the value from 'A0'
  878:             $t{$cell}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
  879:             # Replace parameters
  880:             $t{$cell}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.$self->expandnamed($2)/ge;
  881:         }
  882:     }
  883:     #
  884:     # Deal with the normal cells
  885:     while (my($cell,$formula) = each(%{$self->{'formulas'}})) {
  886: 	next if ($_=~/^template\_/);
  887:         my ($col,$row) = ($cell =~ /^([A-z])(\d+)$/);
  888:         if ($row eq '0') {
  889:             $t{$cell}=$formula;
  890:             $t{$cell}=~s/\.\.+/\,/g;
  891:             $t{$cell}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
  892:             $t{$cell}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.$self->expandnamed($2)/ge;
  893:         } elsif  ( $col  =~ /^[A-Z]$/  ) {
  894:             if ($formula !~ /^\!/ && exists($self->{'constants'}->{$cell})
  895: 		&& $self->{'constants'}->{$cell} ne '') {
  896: 		$Apache::Spreadsheet::sheet_values{$cell}=
  897: 		    eval($self->{'constants'}->{$cell});
  898:             }
  899:         } else { # $row > 1 and $col =~ /[a-z]
  900:             $t{$cell}=$formula;
  901:             $t{$cell}=~s/\.\.+/\,/g;
  902:             $t{$cell}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
  903:             $t{$cell}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.$self->expandnamed($2)/ge;
  904:         }
  905:     }
  906:     %{$self->{'safe'}->varglob('t')}=%t;
  907: }
  908: 
  909: ##
  910: ## sync_safe_space:  Called by calcsheet to make sure all the data we 
  911: #  need to calculate is placed into the safe space
  912: ##
  913: sub sync_safe_space {
  914:     my $self = shift;
  915:     # Inside the safe space 'formulas' has a diabolical alter-ego named 'f'.
  916:     #%{$self->{'safe'}->varglob('f')}=%{$self->{'formulas'}};
  917:     # 'constants' leads a peaceful hidden life of 'c'.
  918:     %{$self->{'safe'}->varglob('c')}=%{$self->{'constants'}};
  919:     # 'othersheets' hides as 'os', a disguise few can penetrate.
  920:     #@{$self->{'safe'}->varglob('os')}=@{$self->{'othersheets'}};
  921: }
  922: 
  923: ##
  924: ## Retrieve the error log from the safe space (used for debugging)
  925: ##
  926: sub get_errorlog {
  927:     my $self = shift;
  928:     $self->{'errorlog'} = $ { $self->{'safe'}->varglob('errorlog') };
  929:     return $self->{'errorlog'};
  930: }
  931: 
  932: ##
  933: ## Clear the error log inside the safe space
  934: ##
  935: sub clear_errorlog {
  936:     my $self = shift;
  937:     $ {$self->{'safe'}->varglob('errorlog')} = '';
  938:     $self->{'errorlog'} = '';
  939: }
  940: 
  941: ##
  942: ## constants:  either set or get the constants
  943: ##
  944: sub constants {
  945:     my $self=shift;
  946:     my ($constants) = @_;
  947:     if (defined($constants)) {
  948:         if (! ref($constants)) {
  949:             my %tmp = @_;
  950:             $constants = \%tmp;
  951:         }
  952:         $self->{'constants'} = $constants;
  953:         return;
  954:     } else {
  955:         return %{$self->{'constants'}};
  956:     }
  957: }
  958: 
  959: ##
  960: ## formulas: either set or get the formulas
  961: ##
  962: sub formulas {
  963:     my $self=shift;
  964:     my ($formulas) = @_;
  965:     if (defined($formulas)) {
  966:         if (! ref($formulas)) {
  967:             my %tmp = @_;
  968:             $formulas = \%tmp;
  969:         }
  970:         $self->{'formulas'} = $formulas;
  971:         $self->{'rows'} = [];
  972:         $self->{'template_cells'} = [];
  973: 	$self->{'loaded'} = 1;
  974:         return;
  975:     } else {
  976: 	$self->check_formulas_loaded();
  977:         return %{$self->{'formulas'}};
  978:     }
  979: }
  980: 
  981: sub check_formulas_loaded {
  982:     my $self=shift;
  983:     if (!$self->{'loaded'}) {
  984: 	$self->{'loaded'}=1;
  985: 	# Load in the spreadsheet definition
  986: 	if (exists($env{'form.workcopy'}) && 
  987: 	    $self->{'type'} eq $env{'form.workcopy'}) {
  988: 	    $self->load_tmp();
  989: 	} else {
  990: 	    $self->load();
  991: 	}
  992:     }
  993: }
  994: 
  995: sub set_formula {
  996:     my $self = shift;
  997:     my ($cell,$formula) = @_;
  998:     $self->check_formulas_loaded();
  999:     $self->{'formulas'}->{$cell}=$formula;
 1000:     return;
 1001: }
 1002: 
 1003: ##
 1004: ## formulas_keys:  Return the keys to the formulas hash.
 1005: ##
 1006: sub formulas_keys {
 1007:     my $self = shift;
 1008:     $self->check_formulas_loaded();
 1009:     return keys(%{$self->{'formulas'}});
 1010: }
 1011: 
 1012: ##
 1013: ## formula:  Return the formula for a given cell in the spreadsheet
 1014: ## returns '' if the cell does not have a formula or does not exist
 1015: ##
 1016: sub formula {
 1017:     my $self = shift;
 1018:     my $cell = shift;
 1019:     $self->check_formulas_loaded();
 1020:     if (defined($cell) && exists($self->{'formulas'}->{$cell})) {
 1021:         return $self->{'formulas'}->{$cell};
 1022:     }
 1023:     return '';
 1024: }
 1025: 
 1026: ##
 1027: ## logthis: write the input to lonnet.log
 1028: ##
 1029: sub logthis {
 1030:     my $self = shift;
 1031:     my $message = shift;
 1032:     &Apache::lonnet::logthis($self->{'type'}.':'.
 1033:                              $self->{'name'}.':'.$self->{'domain'}.':'.
 1034:                              $message);
 1035:     return;
 1036: }
 1037: 
 1038: ##
 1039: ## dump_formulas_to_log: makes lonnet.log huge...
 1040: ##
 1041: sub dump_formulas_to_log {
 1042:     my $self =shift;
 1043:     $self->logthis("Spreadsheet formulas");
 1044:     $self->logthis("--------------------------------------------------------");
 1045:     while (my ($cell, $formula) = each(%{$self->{'formulas'}})) {
 1046:         $self->logthis('    '.$cell.' = '.$formula);
 1047:     }
 1048:     $self->logthis("--------------------------------------------------------");}
 1049: 
 1050: ##
 1051: ## value: returns the computed value of a particular cell
 1052: ##
 1053: sub value {
 1054:     my $self = shift;
 1055:     my $cell = shift;
 1056:     if (defined($cell) && exists($self->{'values'}->{$cell})) {
 1057:         return $self->{'values'}->{$cell};
 1058:     }
 1059:     return '';
 1060: }
 1061: 
 1062: ##
 1063: ## dump_values_to_log: makes lonnet.log huge...
 1064: ##
 1065: sub dump_values_to_log {
 1066:     my $self =shift;
 1067:     $self->logthis("Spreadsheet Values");
 1068:     $self->logthis("------------------------------------------------------");
 1069:     while (my ($cell, $value) = each(%{$self->{'values'}})) {
 1070:         $self->logthis('    '.$cell.' = '.$value);
 1071:     }
 1072:     $self->logthis("------------------------------------------------------");
 1073: }
 1074: 
 1075: ##
 1076: ## Yet another debugging function
 1077: ##
 1078: sub dump_hash_to_log {
 1079:     my $self= shift();
 1080:     my %tmp = @_;
 1081:     if (@_<2) {
 1082:         %tmp = %{$_[0]};
 1083:     }
 1084:     $self->logthis('---------------------------- (begin hash dump)');
 1085:     while (my ($key,$val) = each (%tmp)) {
 1086:         $self->logthis(' '.$key.' = '.$val.':');
 1087:     }
 1088:     $self->logthis('---------------------------- (finished hash dump)');
 1089: }
 1090: 
 1091: ##
 1092: ## rebuild_stats: rebuilds the rows and template_cells arrays
 1093: ##
 1094: sub rebuild_stats {
 1095:     my $self = shift;
 1096:     $self->{'rows'}=[];
 1097:     $self->{'template_cells'}=[];
 1098:     $self->check_formulas_loaded();
 1099:     while (my ($cell,$formula) = each(%{$self->{'formulas'}})) {
 1100:         push(@{$self->{'rows'}},$1) if ($cell =~ /^A(\d+)/ && $1 != 0);
 1101:         push(@{$self->{'template_cells'}},$1) if ($cell =~ /^template_(\w+)/);
 1102:     }
 1103:     return;
 1104: }
 1105: 
 1106: ##
 1107: ## template_cells returns a list of the cells defined in the template row
 1108: ##
 1109: sub template_cells {
 1110:     my $self = shift;
 1111:     $self->rebuild_stats() if (! defined($self->{'template_cells'}) ||
 1112:                                ! @{$self->{'template_cells'}});
 1113:     return @{$self->{'template_cells'}};
 1114: }
 1115: 
 1116: ##
 1117: ## Sigh.... 
 1118: ##
 1119: sub setothersheets {
 1120:     my $self = shift;
 1121:     my @othersheets = @_;
 1122:     $self->{'othersheets'} = \@othersheets;
 1123: }
 1124: 
 1125: ##
 1126: ## rows returns a list of the names of cells defined in the A column
 1127: ##
 1128: sub rows {
 1129:     my $self = shift;
 1130:     $self->rebuild_stats() if (!@{$self->{'rows'}});
 1131:     return @{$self->{'rows'}};
 1132: }
 1133: 
 1134: #
 1135: # calcsheet: makes all the calls to compute the spreadsheet.
 1136: #
 1137: sub calcsheet {
 1138:     my $self = shift;
 1139:     $self->sync_safe_space();
 1140:     $self->clear_errorlog();
 1141:     $self->sett();
 1142:     my $result =  $self->{'safe'}->reval('&calc();');
 1143: #    $self->logthis($self->get_errorlog());
 1144:     %{$self->{'values'}} = %{$self->{'safe'}->varglob('sheet_values')};
 1145: #    $self->logthis($self->get_errorlog());
 1146:     if ($result ne 'okay') {
 1147:         $self->set_calcerror($result);
 1148:     }
 1149:     return $result;
 1150: }
 1151: 
 1152: sub set_badcalc {
 1153:     my $self = shift();
 1154:     $self->{'badcalc'} =1;
 1155:     return;
 1156: }
 1157: 
 1158: sub badcalc {
 1159:     my $self = shift;
 1160:     if (exists($self->{'badcalc'}) && $self->{'badcalc'}) {
 1161:         return 1;
 1162:     } else {
 1163:         return 0;
 1164:     }
 1165: }
 1166: 
 1167: sub set_calcerror {
 1168:     my $self = shift;
 1169:     if (@_) {
 1170:         $self->set_badcalc();
 1171:         if (exists($self->{'calcerror'})) {
 1172:             $self->{'calcerror'}.="\n".$_[0];
 1173:         } else {
 1174:             $self->{'calcerror'}.=$_[0];
 1175:         }
 1176:     }
 1177: }
 1178: 
 1179: sub calcerror {
 1180:     my $self = shift;
 1181:     if ($self->badcalc()) {
 1182:         if (exists($self->{'calcerror'})) {
 1183:             return $self->{'calcerror'};
 1184:         }
 1185:     }
 1186:     return;
 1187: }
 1188: 
 1189: ###########################################################
 1190: ##
 1191: ## Output Helpers
 1192: ##
 1193: ###########################################################
 1194: sub display {
 1195:     my $self = shift;
 1196:     my ($r) = @_;
 1197:     my $outputmode = 'html';
 1198:     foreach ($self->output_options()) {
 1199:         if ($env{'form.output_format'} eq $_->{'value'}) {
 1200:             $outputmode = $_->{'value'};
 1201:             last;
 1202:         }
 1203:     }
 1204:     if ($outputmode eq 'html') {
 1205:         $self->compute($r);
 1206:         $self->outsheet_html($r);
 1207:     } elsif ($outputmode eq 'htmlclasslist') {
 1208:         # No computation neccessary...  This is kludgy
 1209:         $self->outsheet_htmlclasslist($r);
 1210:     } elsif ($outputmode eq 'excel') {
 1211:         $self->compute($r);
 1212:         $self->outsheet_excel($r);
 1213:     } elsif ($outputmode eq 'csv') {
 1214:         $self->compute($r);
 1215:         $self->outsheet_csv($r);
 1216:     } elsif ($outputmode eq 'xml') {
 1217: #        $self->compute($r);
 1218:         $self->outsheet_xml($r);
 1219:     }
 1220:     $self->cleanup();
 1221:     return;
 1222: }
 1223: 
 1224: ############################################
 1225: ##         HTML output routines           ##
 1226: ############################################
 1227: sub html_report_error {
 1228:     my $self = shift();
 1229:     my $Str = '';
 1230:     if ($self->badcalc()) {
 1231:         $Str = '<h3 style="color:red">'.
 1232:             &mt('An error occurred while calculating this spreadsheet').
 1233:             "</h3>\n".
 1234:             '<pre>'.$self->calcerror()."</pre>\n";
 1235:     }
 1236:     return $Str;
 1237: }
 1238: 
 1239: sub html_export_row {
 1240:     my $self = shift();
 1241:     my ($color) = @_;
 1242:     $color = '#CCCCFF' if (! defined($color));
 1243:     my $allowed = &Apache::lonnet::allowed('mgr',$env{'request.course.id'});
 1244:     my $row_html;
 1245:     my @rowdata = $self->get_row(0);
 1246:     foreach my $cell (@rowdata) {
 1247:         if ($cell->{'name'} =~ /^[A-Z]/) {
 1248: 	    $row_html .= '<td bgcolor="'.$color.'">'.
 1249:                 &html_editable_cell($cell,$color,$allowed).'</td>';
 1250:         } else {
 1251: 	    $row_html .= '<td bgcolor="#DDCCFF">'.
 1252:                 &html_editable_cell($cell,'#DDCCFF',$allowed).'</td>';
 1253:         }
 1254:     }
 1255:     return $row_html;
 1256: }
 1257: 
 1258: sub html_template_row {
 1259:     my $self = shift();
 1260:     my $allowed = &Apache::lonnet::allowed('mgr',$env{'request.course.id'});
 1261:     my ($num_uneditable,$importcolor) = @_;
 1262:     my $row_html;
 1263:     my @rowdata = $self->get_template_row();
 1264:     my $count = 0;
 1265:     for (my $i = 0; $i<=$#rowdata; $i++) {
 1266:         my $cell = $rowdata[$i];
 1267:         if ($i < $num_uneditable) {
 1268: 	    $row_html .= '<td bgcolor="'.$importcolor.'">'.
 1269:                 &html_uneditable_cell($cell,'#FFDDDD',$allowed).'</td>';
 1270:         } else {
 1271: 	    $row_html .= '<td bgcolor="#EOFFDD">'.
 1272:                 &html_editable_cell($cell,'#EOFFDD',$allowed).'</td>';
 1273:         }
 1274:     }
 1275:     return $row_html;
 1276: }
 1277: 
 1278: sub html_editable_cell {
 1279:     my ($cell,$bgcolor,$allowed) = @_;
 1280:     my $result;
 1281:     my ($name,$formula,$value);
 1282:     if (defined($cell)) {
 1283:         $name    = $cell->{'name'};
 1284:         $formula = $cell->{'formula'};
 1285:         $value   = $cell->{'value'};
 1286:     }
 1287:     $name    = '' if (! defined($name));
 1288:     $formula = '' if (! defined($formula));
 1289:     if (! defined($value)) {
 1290:         $value = '<font color="'.$bgcolor.'">#</font>';
 1291:         if ($formula ne '') {
 1292:             $value = '<i>undefined value</i>';
 1293:         }
 1294:     } elsif ($value =~ /^\s*$/ ) {
 1295:         $value = '<font color="'.$bgcolor.'">#</font>';
 1296:     } else {
 1297:         $value = &HTML::Entities::encode($value,'<>&"') if ($value !~/&nbsp;/);
 1298:     }
 1299:     return $value if (! $allowed);
 1300:     #
 1301:     # The formula will be parsed by the browser twice before being 
 1302:     # displayed to the user for editing. 
 1303:     #
 1304:     # The encoding string "^A-blah" is placed in []'s inside a regexp, so 
 1305:     # we specify the characters we want left alone by putting a '^' in front.
 1306:     $formula = &HTML::Entities::encode($formula,'^A-z0-9 !#$%-;=?~');
 1307:     # HTML::Entities::encode does not catch everything - we need '\' encoded
 1308:     $formula =~ s/\\/&\#092/g;
 1309:     # Escape it again - this time the only encodable character is '&'
 1310:     $formula =~ s/\&/\&amp;/g;
 1311:     # Glue everything together
 1312:     $result .= "<a href=\"javascript:celledit(\'".
 1313:         $name."','".$formula."');\">".$value."</a>";
 1314:     return $result;
 1315: }
 1316: 
 1317: sub html_uneditable_cell {
 1318:     my ($cell,$bgcolor) = @_;
 1319:     my $value = (defined($cell) ? $cell->{'value'} : '');
 1320:     $value = &HTML::Entities::encode($value,'<>&"') if ($value !~/&nbsp;/);
 1321:     return '&nbsp;'.$value.'&nbsp;';
 1322: }
 1323: 
 1324: sub html_row {
 1325:     my $self = shift();
 1326:     my ($num_uneditable,$row,$exportcolor,$importcolor) = @_;
 1327:     my $allowed = &Apache::lonnet::allowed('mgr',$env{'request.course.id'});
 1328:     my @rowdata = $self->get_row($row);
 1329:     my $num_cols_output = 0;
 1330:     my $row_html;
 1331:     my $color = $importcolor;
 1332:     if ($row == 0) {
 1333:         $color = $exportcolor;
 1334:     }
 1335:     $color = '#FFDDDD' if (! defined($color));
 1336:     foreach my $cell (@rowdata) {
 1337: 	if ($num_cols_output++ < $num_uneditable) {
 1338: 	    $row_html .= '<td bgcolor="'.$color.'">';
 1339: 	    $row_html .= &html_uneditable_cell($cell,'#FFDDDD');
 1340: 	} else {
 1341: 	    $row_html .= '<td bgcolor="#EOFFDD">';
 1342: 	    $row_html .= &html_editable_cell($cell,'#E0FFDD',$allowed);
 1343: 	}
 1344: 	$row_html .= '</td>';
 1345:     }
 1346:     return $row_html;
 1347: }
 1348: 
 1349: sub html_header {
 1350:     my $self = shift;
 1351:     return '' if (! $env{'request.role.adv'});
 1352:     return "<table>\n".
 1353:         '<tr><th align="center">'.&mt('Output Format').'</th></tr>'."\n".
 1354:         '<tr><td>'.$self->output_selector()."</td></tr>\n".
 1355:         "</table>\n";
 1356: }
 1357: 
 1358: ##
 1359: ## Default output types are HTML, Excel, and CSV
 1360: sub output_options {
 1361:     my $self = shift();
 1362:     return  ({value       => 'html',
 1363:               description => 'HTML'},
 1364:              {value       => 'excel',
 1365:               description => 'Excel'},
 1366: #             {value       => 'xml',
 1367: #              description => 'XML'},
 1368:              {value       => 'csv',
 1369:               description => 'Comma Separated Values'},);
 1370: }
 1371: 
 1372: sub output_selector {
 1373:     my $self = shift();
 1374:     my $output_selector = '<select name="output_format" size="3">'."\n";
 1375:     my $default = 'html';
 1376:     if (exists($env{'form.output_format'})) {
 1377:         $default = $env{'form.output_format'} 
 1378:     } else {
 1379:         $env{'form.output_format'} = $default;
 1380:     }
 1381:     foreach  ($self->output_options()) {
 1382:         $output_selector.='<option value="'.$_->{'value'}.'"';
 1383:         if ($_->{'value'} eq $default) {
 1384:             $output_selector .= ' selected';
 1385:         }
 1386:         $output_selector .= ">".&mt($_->{'description'})."</option>\n";
 1387:     }
 1388:     $output_selector .= "</select>\n";
 1389:     return $output_selector;
 1390: }
 1391: 
 1392: ################################################
 1393: ##          Excel output routines             ##
 1394: ################################################
 1395: sub excel_output_row {
 1396:     my $self = shift;
 1397:     my ($worksheet,$rownum,$rows_output,@prepend) = @_;
 1398:     my $cols_output = 0;
 1399:     #
 1400:     my @rowdata = $self->get_row($rownum);
 1401:     foreach my $cell (@prepend,@rowdata) {
 1402:         my $value = $cell;
 1403:         $value = $cell->{'value'} if (ref($value));
 1404:         $value =~ s/\&nbsp;/ /gi;
 1405:         $worksheet->write($rows_output,$cols_output++,$value);
 1406:     }
 1407:     return;
 1408: }
 1409: 
 1410: #
 1411: # This routine is just a stub 
 1412: sub outsheet_htmlclasslist {
 1413:     my $self = shift;
 1414:     my ($r) = @_;
 1415:     $r->print('<h2>'.&mt("This output is not supported").'</h2>');
 1416:     $r->rflush();
 1417:     return;
 1418: }
 1419: 
 1420: sub outsheet_excel {
 1421:     my $self = shift;
 1422:     my ($r) = @_;
 1423:     my $connection = $r->connection();
 1424:     #
 1425:     $r->print($self->html_report_error());
 1426:     $r->rflush();
 1427:     #
 1428:     $r->print("<h2>".&mt('Preparing Excel Spreadsheet')."</h2>");
 1429:     #
 1430:     # Create excel workbook
 1431:     my ($workbook,$filename,$format)=&Apache::loncommon::create_workbook($r);
 1432:     return if (! defined($workbook));
 1433:     #
 1434:     # Create main worksheet
 1435:     my $worksheet = $workbook->addworksheet('main');
 1436:     my $rows_output = 0;
 1437:     my $cols_output = 0;
 1438:     #
 1439:     # Write excel header
 1440:     foreach my $value ($self->get_title()) {
 1441:         $cols_output = 0;
 1442:         $worksheet->write($rows_output++,$cols_output,$value,$format->{'h1'});
 1443:     }
 1444:     $rows_output++;    # skip a line
 1445:     #
 1446:     # Write summary/export row
 1447:     $cols_output = 0;
 1448:     $self->excel_output_row($worksheet,0,$rows_output++,'Summary',
 1449:                             $format->{'b'});
 1450:     $rows_output++;    # skip a line
 1451:     #
 1452:     $self->excel_rows($connection,$worksheet,$cols_output,$rows_output,
 1453:                       $format);
 1454:     #
 1455:     #
 1456:     # Close the excel file
 1457:     $workbook->close();
 1458:     #
 1459:     # Write a link to allow them to download it
 1460:     $r->print('<br />'.
 1461:               '<a href="'.$filename.'">Your Excel spreadsheet.</a>'."\n");
 1462:     return;
 1463: }
 1464: 
 1465: #################################
 1466: ## CSV output routines         ##
 1467: #################################
 1468: sub outsheet_csv   {
 1469:     my $self = shift;
 1470:     my ($r) = @_;
 1471:     my $connection = $r->connection();
 1472:     #
 1473:     $r->print($self->html_report_error());
 1474:     $r->rflush();
 1475:     #
 1476:     my $csvdata = '';
 1477:     my @Values;
 1478:     #
 1479:     # Open the csv file
 1480:     my $filename = '/prtspool/'.
 1481:         $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
 1482:         time.'_'.rand(1000000000).'.csv';
 1483:     my $file;
 1484:     unless ($file = Apache::File->new('>'.'/home/httpd'.$filename)) {
 1485:         $r->log_error("Couldn't open $filename for output $!");
 1486:         $r->print(&mt("Problems occured in writing the csv file.  ".
 1487:                   "This error has been logged.  ".
 1488:                   "Please alert your LON-CAPA administrator."));
 1489:         $r->print("<pre>\n".$csvdata."</pre>\n");
 1490:         return 0;
 1491:     }
 1492:     #
 1493:     # Output the title information
 1494:     foreach my $value ($self->get_title()) {
 1495:         print $file "'".&Apache::loncommon::csv_translate($value)."'\n";
 1496:     }
 1497:     #
 1498:     # Output the body of the spreadsheet
 1499:     $self->csv_rows($connection,$file);
 1500:     #
 1501:     # Close the csv file
 1502:     close($file);
 1503:     $r->print('<br /><br />'.
 1504:               '<a href="'.$filename.'">'.&mt('Your CSV spreadsheet.').'</a>'."\n");
 1505:     #
 1506:     return 1;
 1507: }
 1508: 
 1509: sub csv_output_row {
 1510:     my $self = shift;
 1511:     my ($filehandle,$rownum,@prepend) = @_;
 1512:     #
 1513:     my @rowdata = ();
 1514:     if (defined($rownum)) {
 1515:         @rowdata = $self->get_row($rownum);
 1516:     }
 1517:     my @output = ();
 1518:     foreach my $cell (@prepend,@rowdata) {
 1519:         my $value = $cell;
 1520:         $value = $cell->{'value'} if (ref($value));
 1521:         $value =~ s/\&nbsp;/ /gi;
 1522:         $value = "'".$value."'";
 1523:         push (@output,$value);
 1524:     }
 1525:     print $filehandle join(',',@output )."\n";
 1526:     return;
 1527: }
 1528: 
 1529: ############################################
 1530: ##          XML output routines           ##
 1531: ############################################
 1532: sub outsheet_xml   {
 1533:     my $self = shift;
 1534:     my ($r) = @_;
 1535:     ## Someday XML
 1536:     ## Will be rendered for the user
 1537:     ## But not on this day
 1538:     my $Str = '<spreadsheet type="'.$self->{'type'}.'">'."\n";
 1539:     $self->check_formulas_loaded();
 1540:     while (my ($cell,$formula) = each(%{$self->{'formulas'}})) {
 1541:         if ($cell =~ /^template_(\w+)/) {
 1542:             my $col = $1;
 1543:             $Str .= '<template col="'.$col.'">'.$formula.'</template>'."\n";
 1544:         } else {
 1545:             my ($col,$row) = ($cell =~ /^([A-z])(\d+)/);
 1546:             next if (! defined($row) || ! defined($col));
 1547:             next if ($row != 0);
 1548:             $Str .= 
 1549:                 '<field row="'.$row.'" col="'.$col.'" >'.$formula.'</field>'
 1550:                 ."\n";
 1551:         }
 1552:     }
 1553:     $Str.="</spreadsheet>";
 1554:     $r->print("<pre>\n\n\n".$Str."\n\n\n</pre>");
 1555:     return $Str;
 1556: }
 1557: 
 1558: ############################################
 1559: ###        Filesystem routines           ###
 1560: ############################################
 1561: sub parse_sheet {
 1562:     # $sheetxml is a scalar reference or a scalar
 1563:     my ($sheetxml) = @_;
 1564:     if (! ref($sheetxml)) {
 1565:         my $tmp = $sheetxml;
 1566:         $sheetxml = \$tmp;
 1567:     }
 1568:     my %formulas;
 1569:     my %sources;
 1570:     my $parser=HTML::TokeParser->new($sheetxml);
 1571:     my $token;
 1572:     while ($token=$parser->get_token) {
 1573:         if ($token->[0] eq 'S') {
 1574:             if ($token->[1] eq 'field') {
 1575:                 my $cell = $token->[2]->{'col'}.$token->[2]->{'row'};
 1576:                 my $source = $token->[2]->{'source'};
 1577:                 my $formula = $parser->get_text('/field');
 1578:                 $formulas{$cell} = $formula;
 1579:                 $sources{$cell}  = $source if (defined($source));
 1580:                 $parser->get_text('/field');
 1581:             } elsif ($token->[1] eq 'template') {
 1582:                 $formulas{'template_'.$token->[2]->{'col'}}=
 1583:                     $parser->get_text('/template');
 1584:             }
 1585:         }
 1586:     }
 1587:     return (\%formulas,\%sources);
 1588: }
 1589: 
 1590: {
 1591: 
 1592: my %spreadsheets;
 1593: 
 1594: sub clear_spreadsheet_definition_cache {
 1595:     undef(%spreadsheets);
 1596: }
 1597: 
 1598: sub load_system_default_sheet {
 1599:     my $self = shift;
 1600:     my $includedir = $Apache::lonnet::perlvar{'lonIncludes'};
 1601:     # load in the default defined spreadsheet
 1602:     my $sheetxml='';
 1603:     my $fh;
 1604:     if ($fh=Apache::File->new($includedir.'/default_'.$self->{'type'})) {
 1605:         $sheetxml=join('',<$fh>);
 1606:         $fh->close();
 1607:     } else {
 1608:         # $sheetxml='<field row="0" col="A">"Error"</field>';
 1609:         $sheetxml='<field row="0" col="A"></field>';
 1610:     }
 1611:     $self->filename('default_');
 1612:     my ($formulas,undef) = &parse_sheet(\$sheetxml);
 1613:     return $formulas;
 1614: }
 1615: 
 1616: sub load {
 1617:     my $self = shift;
 1618:     #
 1619:     my $stype = $self->{'type'};
 1620:     my $cnum  = $self->{'cnum'};
 1621:     my $cdom  = $self->{'cdom'};
 1622:     #
 1623:     my $filename = $self->filename();
 1624:     my $cachekey = join('_',($cnum,$cdom,$stype,$filename));
 1625:     #
 1626:     # see if sheet is cached
 1627:     my ($formulas);
 1628:     if (exists($spreadsheets{$cachekey})) {
 1629:         $formulas = $spreadsheets{$cachekey}->{'formulas'};
 1630: 	$self->formulas($formulas);
 1631:         $self->{'row_source'}=$spreadsheets{$cachekey}->{'row_source'};
 1632:         $self->{'row_numbers'}=$spreadsheets{$cachekey}->{'row_numbers'};
 1633:         $self->{'maxrow'}=$spreadsheets{$cachekey}->{'maxrow'};
 1634:    } else {
 1635:         # Not cached, need to read
 1636:         if (! defined($filename)) {
 1637:             $formulas = $self->load_system_default_sheet();
 1638:         } elsif($filename =~ /^\/res\/.*\.spreadsheet$/) {
 1639:             # Load a spreadsheet definition file
 1640:             my $sheetxml=&Apache::lonnet::getfile
 1641:                 (&Apache::lonnet::filelocation('',$filename));
 1642:             if ($sheetxml == -1) {
 1643:                 $sheetxml='<field row="0" col="A">"Error loading spreadsheet '
 1644:                     .$self->filename().'"</field>';
 1645:             }
 1646:             ($formulas,undef) = &parse_sheet(\$sheetxml);
 1647:             # Get just the filename and set the sheets filename
 1648:             my ($newfilename) = ($filename =~ /\/([^\/]*)\.spreadsheet$/);
 1649:             if ($self->is_default()) {
 1650:                 $self->filename($newfilename);
 1651:                 $self->make_default();
 1652:             } else {
 1653:                 $self->filename($newfilename);
 1654:             }
 1655:         } else {
 1656:             # Load the spreadsheet definition file from the save file
 1657:             my %tmphash = &Apache::lonnet::dump($filename,$cdom,$cnum);
 1658:             my ($tmp) = keys(%tmphash);
 1659:             if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
 1660:                 while (my ($cell,$formula) = each(%tmphash)) {
 1661:                     $formulas->{$cell}=$formula;
 1662:                 }
 1663:             } else {
 1664:                 $formulas = $self->load_system_default_sheet();
 1665:             }
 1666:         }
 1667:         $filename=$self->filename(); # filename may have changed
 1668: 	$cachekey = join('_',($cnum,$cdom,$stype,$filename));
 1669: 	if (ref($formulas) eq 'HASH') {
 1670: 	    %{$spreadsheets{$cachekey}->{'formulas'}} = %{$formulas};
 1671: 	}
 1672: 	$self->formulas($formulas);
 1673: 	$self->set_row_sources();
 1674: 	$self->set_row_numbers();
 1675: 	if (ref($self->{'row_source'})) {
 1676: 	    %{$spreadsheets{$cachekey}->{'row_source'}} = 
 1677: 		%{$self->{'row_source'}};
 1678: 	}
 1679: 	if (ref($self->{'row_numbers'})) {
 1680: 	    %{$spreadsheets{$cachekey}->{'row_numbers'}} = 
 1681: 		%{$self->{'row_numbers'}};
 1682: 	}
 1683:         $spreadsheets{$cachekey}->{'maxrow'} = $self->{'maxrow'};
 1684:     }
 1685: }
 1686: 
 1687: sub set_row_sources {
 1688:     my $self = shift;
 1689:     $self->check_formulas_loaded();
 1690:     while (my ($cell,$value) = each(%{$self->{'formulas'}})) {
 1691:         next if ($cell !~ /^A(\d+)/ || $1 < 1);
 1692:         my $row = $1;
 1693:         $self->{'row_source'}->{$row} = $value;
 1694:     }
 1695:     return;
 1696: }
 1697: 
 1698: sub set_row_numbers {
 1699:     my $self = shift;
 1700:     $self->check_formulas_loaded();
 1701:     while (my ($cell,$value) = each(%{$self->{'formulas'}})) {
 1702: 	next if ($cell !~ /^A(\d+)$/);
 1703:         next if (! defined($value));
 1704: 	$self->{'row_numbers'}->{$value} = $1;
 1705:         $self->{'maxrow'} = $1 if ($1 > $self->{'maxrow'});
 1706:     }
 1707: }
 1708: 
 1709: ##
 1710: ## exportrow is *not* used to get the export row from a computed sub-sheet.
 1711: ##
 1712: sub exportrow {
 1713:     my $self = shift;
 1714:     if (exists($self->{'badcalc'}) && $self->{'badcalc'}) {
 1715:         return ();
 1716:     }
 1717:     my @exportarray;
 1718:     foreach my $column (@UC_Columns) {
 1719:         push(@exportarray,$self->value($column.'0'));
 1720:     }
 1721:     return @exportarray;
 1722: }
 1723: 
 1724: sub save {
 1725:     my $self = shift;
 1726:     my ($makedef)=@_;
 1727:     my $cid=$self->{'cid'};
 1728:     # If we are saving it, it must not be temporary
 1729:     $self->temporary(0);
 1730:     if (&Apache::lonnet::allowed('opa',$cid)) {
 1731:         my %f=$self->formulas();
 1732:         my $stype = $self->{'type'};
 1733:         my $cnum  = $self->{'cnum'};
 1734:         my $cdom  = $self->{'cdom'};
 1735:         my $filename    = $self->{'filename'};
 1736:         my $cachekey = join('_',($cnum,$cdom,$stype,$filename));
 1737:         # Cache new sheet
 1738:         %{$spreadsheets{$cachekey}->{'formulas'}}=%f;
 1739:         # Write sheet
 1740:         foreach (keys(%f)) {
 1741:             delete($f{$_}) if ($f{$_} eq 'import');
 1742:         }
 1743:         my $reply = &Apache::lonnet::put($filename,\%f,$cdom,$cnum);
 1744:         return $reply if ($reply ne 'ok');
 1745:         $reply = &Apache::lonnet::put($stype.'_spreadsheets',
 1746:                      {$filename => $env{'user.name'}.'@'.$env{'user.domain'}},
 1747:                                       $cdom,$cnum);
 1748:         return $reply if ($reply ne 'ok');
 1749:         if ($makedef) { 
 1750:             $reply = &Apache::lonnet::put('environment',
 1751:                                 {'spreadsheet_default_'.$stype => $filename },
 1752:                                           $cdom,$cnum);
 1753:             return $reply if ($reply ne 'ok');
 1754: 	    &Apache::lonnet::appenv('course.'.$self->{'cid'}.'.spreadsheet_default_'.
 1755: 				    $self->{'type'} => $self->filename());
 1756:         } 
 1757:         if ($self->is_default()) {
 1758:             if ($self->{'type'} eq 'studentcalc') {
 1759:                 &Apache::lonnet::expirespread('','','studentcalc','');
 1760:             } elsif ($self->{'type'} eq 'assesscalc') {
 1761:                 &Apache::lonnet::expirespread('','','assesscalc','');
 1762:                 &Apache::lonnet::expirespread('','','studentcalc','');
 1763:             }
 1764:         }
 1765:         return $reply;
 1766:     }
 1767:     return 'unauthorized';
 1768: }
 1769: 
 1770: } # end of scope for %spreadsheets
 1771: 
 1772: sub save_tmp {
 1773:     my $self = shift;
 1774:     my $filename=$env{'user.name'}.'_'.
 1775:         $env{'user.domain'}.'_spreadsheet_'.$self->{'symb'}.'_'.
 1776:            $self->{'filename'};
 1777:     $filename=~s/\W/\_/g;
 1778:     $filename=$Apache::lonnet::tmpdir.$filename.'.tmp';
 1779:     $self->temporary(1);
 1780:     my $fh;
 1781:     if ($fh=Apache::File->new('>'.$filename)) {
 1782:         my %f = $self->formulas();
 1783:         while( my ($cell,$formula) = each(%f)) {
 1784:             next if ($formula eq 'import');
 1785:             print $fh &Apache::lonnet::escape($cell)."=".
 1786:                 &Apache::lonnet::escape($formula)."\n";
 1787:         }
 1788:         $fh->close();
 1789:     }
 1790: }
 1791: 
 1792: sub load_tmp {
 1793:     my $self = shift;
 1794:     my $filename=$env{'user.name'}.'_'.
 1795:         $env{'user.domain'}.'_spreadsheet_'.$self->{'symb'}.'_'.
 1796:             $self->{'filename'};
 1797:     $filename=~s/\W/\_/g;
 1798:     $filename=$Apache::lonnet::tmpdir.$filename.'.tmp';
 1799:     my %formulas = ();
 1800:     if (my $spreadsheet_file = Apache::File->new($filename)) {
 1801:         while (<$spreadsheet_file>) {
 1802: 	    chomp;
 1803:             my ($cell,$formula) = split(/=/);
 1804:             $cell    = &Apache::lonnet::unescape($cell);
 1805:             $formula = &Apache::lonnet::unescape($formula);
 1806:             $formulas{$cell} = $formula;
 1807:         }
 1808:         $spreadsheet_file->close();
 1809:     }
 1810:     # flag the sheet as temporary
 1811:     $self->temporary(1);
 1812:     $self->formulas(\%formulas);
 1813:     $self->set_row_sources();
 1814:     $self->set_row_numbers();
 1815:     return;
 1816: }
 1817: 
 1818: sub temporary {
 1819:     my $self=shift;
 1820:     if (@_) {
 1821:         ($self->{'temporary'})= @_;
 1822:     }
 1823:     return $self->{'temporary'};
 1824: }
 1825: 
 1826: sub modify_cell {
 1827:     # studentcalc overrides this
 1828:     my $self = shift;
 1829:     my ($cell,$formula) = @_;
 1830:     if ($cell =~ /([A-z])\-/) {
 1831:         $cell = 'template_'.$1;
 1832:     } elsif ($cell !~ /^([A-z](\d+)|template_[A-z])$/) {
 1833:         return;
 1834:     }
 1835:     $self->set_formula($cell,$formula);
 1836:     $self->rebuild_stats();
 1837:     return;
 1838: }
 1839: 
 1840: ###########################################
 1841: # othersheets: Returns the list of other spreadsheets available 
 1842: ###########################################
 1843: sub othersheets {
 1844:     my $self = shift(); 
 1845:     my ($stype) = @_;
 1846:     $stype = $self->{'type'} if (! defined($stype) || $stype !~ /calc$/);
 1847:     #
 1848:     my @alternatives=();
 1849:     my %results=&Apache::lonnet::dump($stype.'_spreadsheets',
 1850:                                       $self->{'cdom'}, $self->{'cnum'});
 1851:     my ($tmp) = keys(%results);
 1852:     if ($tmp =~ /^(con_lost|error|no_such_host)/i ) {
 1853:         @alternatives = (&mt('Default'));
 1854:     } else {
 1855:         @alternatives = (&mt('Default'), sort (keys(%results)));
 1856:     }
 1857:     return @alternatives; 
 1858: }
 1859: 
 1860: sub blackout {
 1861:     my $self = shift;
 1862:     $self->{'blackout'} = $_[0] if (@_);
 1863:     return $self->{'blackout'};
 1864: }
 1865: 
 1866: sub get_row {
 1867:     my $self = shift;
 1868:     my ($n)=@_;
 1869:     my @cols=();
 1870:     foreach my $col (@UC_Columns,@LC_Columns) {
 1871:         my $cell = $col.$n;
 1872:         push(@cols,{ name    => $cell,
 1873:                      formula => $self->formula($cell),
 1874:                      value   => $self->value($cell)});
 1875:     }
 1876:     return @cols;
 1877: }
 1878: 
 1879: sub get_template_row {
 1880:     my $self = shift;
 1881:     my @cols=();
 1882:     foreach my $col (@UC_Columns,@LC_Columns) {
 1883:         my $cell = 'template_'.$col;
 1884:         push(@cols,{ name    => $cell,
 1885:                      formula => $self->formula($cell),
 1886:                      value   => $self->formula($cell) });
 1887:     }
 1888:     return @cols;
 1889: }
 1890: 
 1891: sub need_to_save {
 1892:     my $self = shift;
 1893:     if ($self->{'new_rows'} && ! $self->temporary()) {
 1894:         return 1;
 1895:     }
 1896:     return 0;
 1897: }
 1898: 
 1899: sub get_row_number_from_key {
 1900:     my $self = shift;
 1901:     my ($key) = @_;
 1902:     if (! exists($self->{'row_numbers'}->{$key}) ||
 1903:         ! defined($self->{'row_numbers'}->{$key})) {
 1904:         # I used to set $f here to the new value, but the key passed for lookup
 1905:         # may not be the key we need to save
 1906: 	$self->{'maxrow'}++;
 1907: 	$self->{'row_numbers'}->{$key} = $self->{'maxrow'};
 1908: #        $self->logthis('added row '.$self->{'row_numbers'}->{$key}.
 1909: #                       ' for '.$key);
 1910:         $self->{'new_rows'} = 1;
 1911:     }
 1912:     return $self->{'row_numbers'}->{$key};
 1913: }
 1914: 
 1915: 1;
 1916: 
 1917: __END__

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