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

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

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