File:  [LON-CAPA] / loncom / interface / spreadsheet / Spreadsheet.pm
Revision 1.24: download - view: text, annotated - select for diffs
Mon Sep 8 20:32:22 2003 UTC (20 years, 9 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
There are downsides to making warnings fatal:
my %tmphash = &Apache::lonnet::dump(blah) crashes if the file does not
exist.  Not good.  I don't feel like detecting this right now, so I am
commenting out the use warnings stuff...

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

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