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

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

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