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

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

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