Annotation of loncom/interface/spreadsheet/Spreadsheet.pm, revision 1.49

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

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