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

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

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