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

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

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