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

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

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