Annotation of loncom/interface/lonspreadsheet.pm, revision 1.92

1.79      matthew     1: #
1.92    ! www         2: # $Id: lonspreadsheet.pm,v 1.91 2002/06/08 15:27:06 matthew Exp $
1.79      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: #
1.1       www        26: # The LearningOnline Network with CAPA
                     27: # Spreadsheet/Grades Display Handler
                     28: #
1.15      www        29: # 11/11,11/15,11/27,12/04,12/05,12/06,12/07,
1.23      www        30: # 12/08,12/09,12/11,12/12,12/15,12/16,12/18,12/19,12/30,
1.39      www        31: # 01/01/01,02/01,03/01,19/01,20/01,22/01,
1.47      www        32: # 03/05,03/08,03/10,03/12,03/13,03/15,03/17,
1.55      www        33: # 03/19,03/20,03/21,03/27,04/05,04/09,
1.69      www        34: # 07/09,07/14,07/21,09/01,09/10,9/11,9/12,9/13,9/14,9/17,
1.77      www        35: # 10/16,10/17,10/20,11/05,11/28,12/27 Gerd Kortemeyer
1.78      matthew    36: # 01/14/02 Matthew
1.80      matthew    37: # 02/04/02 Matthew
                     38: 
                     39: # POD required stuff:
                     40: 
                     41: =head1 NAME
                     42: 
                     43: lonspreadsheet
                     44: 
                     45: =head1 SYNOPSIS
                     46: 
                     47: Spreadsheet interface to internal LON-CAPA data
                     48: 
                     49: =head1 DESCRIPTION
                     50: 
                     51: Lonspreadsheet provides course coordinators the ability to manage their
                     52: students grades online.  The students are able to view their own grades, but
                     53: not the grades of their peers.  The spreadsheet is highly customizable,
                     54: offering the ability to use Perl code to manipulate data, as well as many
                     55: built-in functions.
                     56: 
                     57: 
                     58: =head2 Functions available to user of lonspreadsheet
                     59: 
                     60: =over 4
                     61: 
                     62: =cut
1.1       www        63: 
                     64: package Apache::lonspreadsheet;
1.36      www        65:             
1.1       www        66: use strict;
                     67: use Safe;
1.3       www        68: use Safe::Hole;
1.1       www        69: use Opcode;
                     70: use Apache::lonnet;
1.7       www        71: use Apache::Constants qw(:common :http);
1.19      www        72: use GDBM_File;
1.3       www        73: use HTML::TokeParser;
                     74: 
1.11      www        75: #
1.44      www        76: # Caches for previously calculated spreadsheets
                     77: #
                     78: 
                     79: my %oldsheets;
1.46      www        80: my %loadedcaches;
1.47      www        81: my %expiredates;
1.44      www        82: 
                     83: #
1.39      www        84: # Cache for stores of an individual user
                     85: #
                     86: 
                     87: my $cachedassess;
                     88: my %cachedstores;
                     89: 
                     90: #
1.11      www        91: # These cache hashes need to be independent of user, resource and course
1.27      www        92: # (user and course can/should be in the keys)
1.11      www        93: #
1.33      www        94: 
                     95: my %spreadsheets;
                     96: my %courserdatas;
                     97: my %userrdatas;
                     98: my %defaultsheets;
1.35      www        99: my %updatedata;
1.27      www       100: 
1.11      www       101: #
                    102: # These global hashes are dependent on user, course and resource, 
                    103: # and need to be initialized every time when a sheet is calculated
                    104: #
                    105: my %courseopt;
                    106: my %useropt;
                    107: my %parmhash;
                    108: 
1.28      www       109: # Stuff that only the screen handler can know
                    110: 
                    111: my $includedir;
                    112: my $tmpdir;
                    113: 
1.5       www       114: # =============================================================================
                    115: # ===================================== Implements an instance of a spreadsheet
1.4       www       116: 
                    117: sub initsheet {
1.36      www       118:     my $safeeval = new Safe(shift);
1.4       www       119:     my $safehole = new Safe::Hole;
                    120:     $safeeval->permit("entereval");
                    121:     $safeeval->permit(":base_math");
                    122:     $safeeval->permit("sort");
                    123:     $safeeval->deny(":base_io");
                    124:     $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
                    125:     my $code=<<'ENDDEFS';
                    126: # ---------------------------------------------------- Inside of the safe space
                    127: 
1.3       www       128: #
                    129: # f: formulas
1.4       www       130: # t: intermediate format (variable references expanded)
                    131: # v: output values
1.6       www       132: # c: preloaded constants (A-column)
                    133: # rl: row label
1.55      www       134: # os: other spreadsheets (for student spreadsheet only)
1.3       www       135: 
1.36      www       136: undef %v; 
                    137: undef %t;
                    138: undef %f;
                    139: undef %c;
                    140: undef %rl;
1.55      www       141: undef @os;
                    142: 
1.6       www       143: $maxrow=0;
1.5       www       144: $sheettype='';
1.27      www       145: 
                    146: # filename/reference of the sheet
                    147: 
1.5       www       148: $filename='';
1.1       www       149: 
1.27      www       150: # user data
                    151: $uname='';
                    152: $uhome='';
                    153: $udom='';
                    154: 
                    155: # course data
                    156: 
                    157: $csec='';
                    158: $chome='';
                    159: $cnum='';
                    160: $cdom='';
1.28      www       161: $cid='';
1.29      www       162: $cfn='';
1.27      www       163: 
                    164: # symb
                    165: 
                    166: $usymb='';
                    167: 
1.1       www       168: sub mask {
                    169:     my ($lower,$upper)=@_;
                    170: 
1.7       www       171:     $lower=~/([A-Za-z]|\*)(\d+|\*)/;
1.1       www       172:     my $la=$1;
                    173:     my $ld=$2;
                    174: 
1.7       www       175:     $upper=~/([A-Za-z]|\*)(\d+|\*)/;
1.1       www       176:     my $ua=$1;
                    177:     my $ud=$2;
                    178:     my $alpha='';
                    179:     my $num='';
                    180: 
                    181:     if (($la eq '*') || ($ua eq '*')) {
1.7       www       182:        $alpha='[A-Za-z]';
1.1       www       183:     } else {
1.7       www       184:        if (($la=~/[A-Z]/) && ($ua=~/[A-Z]/) ||
                    185:            ($la=~/[a-z]/) && ($ua=~/[a-z]/)) {
                    186:           $alpha='['.$la.'-'.$ua.']';
                    187:        } else {
                    188:           $alpha='['.$la.'-Za-'.$ua.']';
                    189:        }
1.1       www       190:     }   
                    191: 
                    192:     if (($ld eq '*') || ($ud eq '*')) {
                    193: 	$num='\d+';
                    194:     } else {
                    195:         if (length($ld)!=length($ud)) {
                    196:            $num.='(';
1.78      matthew   197: 	   foreach ($ld=~m/\d/g) {
1.1       www       198:               $num.='['.$_.'-9]';
1.78      matthew   199: 	   }
1.1       www       200:            if (length($ud)-length($ld)>1) {
                    201:               $num.='|\d{'.(length($ld)+1).','.(length($ud)-1).'}';
                    202: 	   }
                    203:            $num.='|';
1.78      matthew   204:            foreach ($ud=~m/\d/g) {
1.1       www       205:                $num.='[0-'.$_.']';
1.78      matthew   206:            }
1.1       www       207:            $num.=')';
                    208:        } else {
                    209:            my @lda=($ld=~m/\d/g);
                    210:            my @uda=($ud=~m/\d/g);
1.7       www       211:            my $i; $j=0; $notdone=1;
                    212:            for ($i=0;($i<=$#lda)&&($notdone);$i++) {
1.1       www       213:                if ($lda[$i]==$uda[$i]) {
                    214: 		   $num.=$lda[$i];
                    215:                    $j=$i;
1.7       www       216:                } else {
                    217:                    $notdone=0;
1.1       www       218:                }
                    219:            }
                    220:            if ($j<$#lda-1) {
                    221: 	       $num.='('.$lda[$j+1];
                    222:                for ($i=$j+2;$i<=$#lda;$i++) {
                    223:                    $num.='['.$lda[$i].'-9]';
                    224:                }
                    225:                if ($uda[$j+1]-$lda[$j+1]>1) {
                    226: 		   $num.='|['.($lda[$j+1]+1).'-'.($uda[$j+1]-1).']\d{'.
                    227:                    ($#lda-$j-1).'}';
                    228:                }
                    229: 	       $num.='|'.$uda[$j+1];
                    230:                for ($i=$j+2;$i<=$#uda;$i++) {
                    231:                    $num.='[0-'.$uda[$i].']';
                    232:                }
                    233:                $num.=')';
                    234:            } else {
1.7       www       235:                if ($lda[$#lda]!=$uda[$#uda]) {
                    236:                   $num.='['.$lda[$#lda].'-'.$uda[$#uda].']';
                    237: 	       }
1.1       www       238:            }
                    239:        }
                    240:     }
1.4       www       241:     return '^'.$alpha.$num."\$";
1.80      matthew   242: }
                    243: 
                    244: #-------------------------------------------------------
                    245: 
                    246: =item UWCALC(hashname,modules,units,date) 
                    247: 
                    248: returns the proportion of the module 
                    249: weights not previously completed by the student.
                    250: 
                    251: =over 4
                    252: 
                    253: =item hashname 
                    254: 
                    255: name of the hash the module dates have been inserted into
                    256: 
                    257: =item modules 
                    258: 
                    259: reference to a cell which contains a comma deliminated list of modules 
                    260: covered by the assignment.
                    261: 
                    262: =item units 
                    263: 
                    264: reference to a cell which contains a comma deliminated list of module 
                    265: weights with respect to the assignment
                    266: 
                    267: =item date 
                    268: 
                    269: reference to a cell which contains the date the assignment was completed.
                    270: 
                    271: =back 
                    272: 
                    273: =cut
                    274: 
                    275: #-------------------------------------------------------
                    276: sub UWCALC {
                    277:     my ($hashname,$modules,$units,$date) = @_;
                    278:     my @Modules = split(/,/,$modules);
                    279:     my @Units   = split(/,/,$units);
                    280:     my $total_weight;
                    281:     foreach (@Units) {
                    282: 	$total_weight += $_;
                    283:     }
                    284:     my $usum=0;
                    285:     for (my $i=0; $i<=$#Modules; $i++) {
                    286: 	if (&HASH($hashname,$Modules[$i]) eq $date) {
                    287: 	    $usum += $Units[$i];
                    288: 	}
                    289:     }
                    290:     return $usum/$total_weight;
                    291: }
                    292: 
                    293: #-------------------------------------------------------
                    294: 
                    295: =item CDLSUM(list) 
                    296: 
                    297: returns the sum of the elements in a cell which contains
                    298: a Comma Deliminate List of numerical values.
                    299: 'list' is a reference to a cell which contains a comma deliminated list.
                    300: 
                    301: =cut
                    302: 
                    303: #-------------------------------------------------------
                    304: sub CDLSUM {
                    305:     my ($list)=@_;
                    306:     my $sum;
                    307:     foreach (split/,/,$list) {
                    308: 	$sum += $_;
                    309:     }
                    310:     return $sum;
                    311: }
                    312: 
                    313: #-------------------------------------------------------
                    314: 
                    315: =item CDLITEM(list,index) 
                    316: 
                    317: returns the item at 'index' in a Comma Deliminated List.
                    318: 
                    319: =over 4
                    320: 
                    321: =item list
                    322: 
                    323: reference to a cell which contains a comma deliminated list.
                    324: 
                    325: =item index 
                    326: 
                    327: the Perl index of the item requested (first element in list has
                    328: an index of 0) 
                    329: 
                    330: =back
                    331: 
                    332: =cut
                    333: 
                    334: #-------------------------------------------------------
                    335: sub CDLITEM {
                    336:     my ($list,$index)=@_;
                    337:     my @Temp = split/,/,$list;
                    338:     return $Temp[$index];
                    339: }
                    340: 
                    341: #-------------------------------------------------------
                    342: 
                    343: =item CDLHASH(name,key,value) 
                    344: 
                    345: loads a comma deliminated list of keys into
                    346: the hash 'name', all with a value of 'value'.
                    347: 
                    348: =over 4
                    349: 
                    350: =item name  
                    351: 
                    352: name of the hash.
                    353: 
                    354: =item key
                    355: 
                    356: (a pointer to) a comma deliminated list of keys.
                    357: 
                    358: =item value
                    359: 
                    360: a single value to be entered for each key.
                    361: 
                    362: =back
                    363: 
                    364: =cut
                    365: 
                    366: #-------------------------------------------------------
                    367: sub CDLHASH {
                    368:     my ($name,$key,$value)=@_;
                    369:     my @Keys;
                    370:     my @Values;
                    371:     # Check to see if we have multiple $key values
                    372:     if ($key =~ /[A-z](\-[A-z])?\d+(\-\d+)?/) {
                    373: 	my $keymask = &mask($key);
                    374: 	# Assume the keys are addresses
                    375: 	my @Temp = grep /$keymask/,keys(%v);
                    376: 	@Keys = $v{@Temp};
                    377:     } else {
                    378: 	$Keys[0]= $key;
                    379:     }
                    380:     my @Temp;
                    381:     foreach $key (@Keys) {
                    382: 	@Temp = (@Temp, split/,/,$key);
                    383:     }
                    384:     @Keys = @Temp;
                    385:     if ($value =~ /[A-z](\-[A-z])?\d+(\-\d+)?/) {
                    386: 	my $valmask = &mask($value);
                    387: 	my @Temp = grep /$valmask/,keys(%v);
                    388: 	@Values =$v{@Temp};
                    389:     } else {
                    390: 	$Values[0]= $value;
                    391:     }
                    392:     $value = $Values[0];
                    393:     # Add values to hash
                    394:     for (my $i = 0; $i<=$#Keys; $i++) {
                    395: 	my $key   = $Keys[$i];
                    396: 	if (! exists ($hashes{$name}->{$key})) {
                    397: 	    $hashes{$name}->{$key}->[0]=$value;
                    398: 	} else {
                    399: 	    my @Temp = sort(@{$hashes{$name}->{$key}},$value);
                    400: 	    $hashes{$name}->{$key} = \@Temp;
                    401: 	}
                    402:     }
                    403:     return "hash '$name' updated";
                    404: }
                    405: 
                    406: #-------------------------------------------------------
                    407: 
                    408: =item GETHASH(name,key,index) 
                    409: 
                    410: returns the element in hash 'name' 
                    411: reference by the key 'key', at index 'index' in the values list.
                    412: 
                    413: =cut
                    414: 
                    415: #-------------------------------------------------------
                    416: sub GETHASH {
                    417:     my ($name,$key,$index)=@_;
                    418:     if (! defined($index)) {
                    419: 	$index = 0;
                    420:     }
                    421:     if ($key =~ /^[A-z]\d+$/) {
                    422: 	$key = $v{$key};
                    423:     }
                    424:     return $hashes{$name}->{$key}->[$index];
                    425: }
                    426: 
                    427: #-------------------------------------------------------
                    428: 
                    429: =item CLEARHASH(name) 
                    430: 
                    431: clears all the values from the hash 'name'
                    432: 
                    433: =item CLEARHASH(name,key) 
                    434: 
                    435: clears all the values from the hash 'name' associated with the given key.
                    436: 
                    437: =cut
                    438: 
                    439: #-------------------------------------------------------
                    440: sub CLEARHASH {
                    441:     my ($name,$key)=@_;
                    442:     if (defined($key)) {
                    443: 	if (exists($hashes{$name}->{$key})) {
                    444: 	    $hashes{$name}->{$key}=undef;
                    445: 	    return "hash '$name' key '$key' cleared";
                    446: 	}
                    447:     } else {
                    448: 	if (exists($hashes{$name})) {
                    449: 	    $hashes{$name}=undef;
                    450: 	    return "hash '$name' cleared";
                    451: 	}
                    452:     }
                    453:     return "Error in clearing hash";
                    454: }
                    455: 
                    456: #-------------------------------------------------------
                    457: 
                    458: =item HASH(name,key,value) 
                    459: 
                    460: loads values into an internal hash.  If a key 
                    461: already has a value associated with it, the values are sorted numerically.  
                    462: 
                    463: =item HASH(name,key) 
                    464: 
                    465: returns the 0th value in the hash 'name' associated with 'key'.
                    466: 
                    467: =cut
                    468: 
                    469: #-------------------------------------------------------
                    470: sub HASH {
                    471:     my ($name,$key,$value)=@_;
                    472:     my @Keys;
                    473:     undef @Keys;
                    474:     my @Values;
                    475:     # Check to see if we have multiple $key values
                    476:     if ($key =~ /[A-z](\-[A-z])?\d+(\-\d+)?/) {
                    477: 	my $keymask = &mask($key);
                    478: 	# Assume the keys are addresses
                    479: 	my @Temp = grep /$keymask/,keys(%v);
                    480: 	@Keys = $v{@Temp};
                    481:     } else {
                    482: 	$Keys[0]= $key;
                    483:     }
                    484:     # If $value is empty, return the first value associated 
                    485:     # with the first key.
                    486:     if (! $value) {
                    487: 	return $hashes{$name}->{$Keys[0]}->[0];
                    488:     }
                    489:     # Check to see if we have multiple $value(s) 
                    490:     if ($value =~ /[A-z](\-[A-z])?\d+(\-\d+)?/) {
                    491: 	my $valmask = &mask($value);
                    492: 	my @Temp = grep /$valmask/,keys(%v);
                    493: 	@Values =$v{@Temp};
                    494:     } else {
                    495: 	$Values[0]= $value;
                    496:     }
                    497:     # Add values to hash
                    498:     for (my $i = 0; $i<=$#Keys; $i++) {
                    499: 	my $key   = $Keys[$i];
                    500: 	my $value = ($i<=$#Values ? $Values[$i] : $Values[0]);
                    501: 	if (! exists ($hashes{$name}->{$key})) {
                    502: 	    $hashes{$name}->{$key}->[0]=$value;
                    503: 	} else {
                    504: 	    my @Temp = sort(@{$hashes{$name}->{$key}},$value);
                    505: 	    $hashes{$name}->{$key} = \@Temp;
                    506: 	}
                    507:     }
                    508:     return $Values[-1];
1.1       www       509: }
                    510: 
1.84      matthew   511: #-------------------------------------------------------
                    512: 
                    513: =item NUM(range)
                    514: 
                    515: returns the number of items in the range.
                    516: 
                    517: =cut
                    518: 
                    519: #-------------------------------------------------------
1.1       www       520: sub NUM {
                    521:     my $mask=mask(@_);
1.78      matthew   522:     my $num= $#{@{grep(/$mask/,keys(%v))}}+1;
1.1       www       523:     return $num;   
                    524: }
                    525: 
                    526: sub BIN {
                    527:     my ($low,$high,$lower,$upper)=@_;
                    528:     my $mask=mask($lower,$upper);
                    529:     my $num=0;
1.78      matthew   530:     foreach (grep /$mask/,keys(%v)) {
1.1       www       531:         if (($v{$_}>=$low) && ($v{$_}<=$high)) {
                    532:             $num++;
                    533:         }
1.78      matthew   534:     }
1.1       www       535:     return $num;   
                    536: }
                    537: 
                    538: 
1.84      matthew   539: #-------------------------------------------------------
                    540: 
                    541: =item SUM(range)
                    542: 
                    543: returns the sum of items in the range.
                    544: 
                    545: =cut
                    546: 
                    547: #-------------------------------------------------------
1.1       www       548: sub SUM {
                    549:     my $mask=mask(@_);
                    550:     my $sum=0;
1.78      matthew   551:     foreach (grep /$mask/,keys(%v)) {
1.1       www       552:         $sum+=$v{$_};
1.78      matthew   553:     }
1.1       www       554:     return $sum;   
                    555: }
                    556: 
1.84      matthew   557: #-------------------------------------------------------
                    558: 
                    559: =item MEAN(range)
                    560: 
                    561: compute the average of the items in the range.
                    562: 
                    563: =cut
                    564: 
                    565: #-------------------------------------------------------
1.1       www       566: sub MEAN {
                    567:     my $mask=mask(@_);
                    568:     my $sum=0; my $num=0;
1.78      matthew   569:     foreach (grep /$mask/,keys(%v)) {
1.1       www       570:         $sum+=$v{$_};
                    571:         $num++;
1.78      matthew   572:     }
1.1       www       573:     if ($num) {
                    574:        return $sum/$num;
                    575:     } else {
                    576:        return undef;
                    577:     }   
                    578: }
                    579: 
1.84      matthew   580: #-------------------------------------------------------
                    581: 
                    582: =item STDDEV(range)
                    583: 
                    584: compute the standard deviation of the items in the range.
                    585: 
                    586: =cut
                    587: 
                    588: #-------------------------------------------------------
1.1       www       589: sub STDDEV {
                    590:     my $mask=mask(@_);
                    591:     my $sum=0; my $num=0;
1.78      matthew   592:     foreach (grep /$mask/,keys(%v)) {
1.1       www       593:         $sum+=$v{$_};
                    594:         $num++;
1.78      matthew   595:     }
1.1       www       596:     unless ($num>1) { return undef; }
                    597:     my $mean=$sum/$num;
                    598:     $sum=0;
1.78      matthew   599:     foreach (grep /$mask/,keys(%v)) {
1.1       www       600:         $sum+=($v{$_}-$mean)**2;
1.78      matthew   601:     }
1.1       www       602:     return sqrt($sum/($num-1));    
                    603: }
                    604: 
1.84      matthew   605: #-------------------------------------------------------
                    606: 
                    607: =item PROD(range)
                    608: 
                    609: compute the product of the items in the range.
                    610: 
                    611: =cut
                    612: 
                    613: #-------------------------------------------------------
1.1       www       614: sub PROD {
                    615:     my $mask=mask(@_);
                    616:     my $prod=1;
1.78      matthew   617:     foreach (grep /$mask/,keys(%v)) {
1.1       www       618:         $prod*=$v{$_};
1.78      matthew   619:     }
1.1       www       620:     return $prod;   
                    621: }
                    622: 
1.84      matthew   623: #-------------------------------------------------------
                    624: 
                    625: =item MAX(range)
                    626: 
                    627: compute the maximum of the items in the range.
                    628: 
                    629: =cut
                    630: 
                    631: #-------------------------------------------------------
1.1       www       632: sub MAX {
                    633:     my $mask=mask(@_);
                    634:     my $max='-';
1.78      matthew   635:     foreach (grep /$mask/,keys(%v)) {
1.1       www       636:         unless ($max) { $max=$v{$_}; }
                    637:         if (($v{$_}>$max) || ($max eq '-')) { $max=$v{$_}; }
1.78      matthew   638:     } 
1.1       www       639:     return $max;   
                    640: }
                    641: 
1.84      matthew   642: #-------------------------------------------------------
                    643: 
                    644: =item MIN(range)
                    645: 
                    646: compute the minimum of the items in the range.
                    647: 
                    648: =cut
                    649: 
                    650: #-------------------------------------------------------
1.1       www       651: sub MIN {
                    652:     my $mask=mask(@_);
                    653:     my $min='-';
1.78      matthew   654:     foreach (grep /$mask/,keys(%v)) {
1.1       www       655:         unless ($max) { $max=$v{$_}; }
                    656:         if (($v{$_}<$min) || ($min eq '-')) { $min=$v{$_}; }
1.78      matthew   657:     }
1.1       www       658:     return $min;   
                    659: }
                    660: 
1.84      matthew   661: #-------------------------------------------------------
                    662: 
                    663: =item SUMMAX(num,lower,upper)
                    664: 
                    665: compute the sum of the largest 'num' items in the range from
                    666: 'lower' to 'upper'
                    667: 
                    668: =cut
                    669: 
                    670: #-------------------------------------------------------
1.1       www       671: sub SUMMAX {
                    672:     my ($num,$lower,$upper)=@_;
                    673:     my $mask=mask($lower,$upper);
                    674:     my @inside=();
1.78      matthew   675:     foreach (grep /$mask/,keys(%v)) {
1.84      matthew   676: 	push (@inside,$v{$_});
1.78      matthew   677:     }
1.1       www       678:     @inside=sort(@inside);
                    679:     my $sum=0; my $i;
                    680:     for ($i=$#inside;(($i>$#inside-$num) && ($i>=0));$i--) { 
                    681:         $sum+=$inside[$i];
                    682:     }
                    683:     return $sum;   
                    684: }
                    685: 
1.84      matthew   686: #-------------------------------------------------------
                    687: 
                    688: =item SUMMIN(num,lower,upper)
                    689: 
                    690: compute the sum of the smallest 'num' items in the range from
                    691: 'lower' to 'upper'
                    692: 
                    693: =cut
                    694: 
                    695: #-------------------------------------------------------
1.1       www       696: sub SUMMIN {
                    697:     my ($num,$lower,$upper)=@_;
                    698:     my $mask=mask($lower,$upper);
                    699:     my @inside=();
1.78      matthew   700:     foreach (grep /$mask/,keys(%v)) {
1.1       www       701: 	$inside[$#inside+1]=$v{$_};
1.78      matthew   702:     }
1.1       www       703:     @inside=sort(@inside);
                    704:     my $sum=0; my $i;
                    705:     for ($i=0;(($i<$num) && ($i<=$#inside));$i++) { 
                    706:         $sum+=$inside[$i];
                    707:     }
                    708:     return $sum;   
                    709: }
                    710: 
1.59      www       711: sub expandnamed {
                    712:     my $expression=shift;
                    713:     if ($expression=~/^\&/) {
                    714: 	my ($func,$var,$formula)=($expression=~/^\&(\w+)\(([^\;]+)\;(.*)\)/);
                    715: 	my @vars=split(/\W+/,$formula);
                    716:         my %values=();
                    717:         undef %values;
1.78      matthew   718: 	foreach ( @vars ) {
1.59      www       719:             my $varname=$_;
                    720:             if ($varname=~/\D/) {
                    721:                $formula=~s/$varname/'$c{\''.$varname.'\'}'/ge;
                    722:                $varname=~s/$var/\(\\w\+\)/g;
1.78      matthew   723: 	       foreach (keys(%c)) {
1.59      www       724: 		  if ($_=~/$varname/) {
                    725: 		      $values{$1}=1;
                    726:                   }
1.78      matthew   727:                }
1.59      www       728: 	    }
1.78      matthew   729:         }
1.59      www       730:         if ($func eq 'EXPANDSUM') {
                    731:             my $result='';
1.78      matthew   732: 	    foreach (keys(%values)) {
1.59      www       733:                 my $thissum=$formula;
                    734:                 $thissum=~s/$var/$_/g;
                    735:                 $result.=$thissum.'+';
1.78      matthew   736:             } 
1.59      www       737:             $result=~s/\+$//;
                    738:             return $result;
                    739:         } else {
                    740: 	    return 0;
                    741:         }
                    742:     } else {
1.88      matthew   743:         # it is not a function, so it is a parameter name
                    744:         # We should do the following:
                    745:         #    1. Take the list of parameter names
                    746:         #    2. look through the list for ones that match the parameter we want
                    747:         #    3. If there are no collisions, return the one that matches
                    748:         #    4. If there is a collision, return 'bad parameter name error'
                    749:         my $returnvalue = '';
                    750:         my @matches = ();
                    751:         $#matches = -1;
                    752:         study $expression;
                    753:         foreach $parameter (keys(%c)) {
                    754:             push @matches,$parameter if ($parameter =~ /$expression/);
                    755:         }
                    756:         if ($#matches == 0) {
                    757:             $returnvalue = '$c{\''.$matches[0].'\'}';
                    758:         } else {
                    759:             $returnvalue =  "'bad parameter name : $expression'";
                    760:         }
                    761:         return $returnvalue;
1.59      www       762:     }
                    763: }
                    764: 
1.1       www       765: sub sett {
                    766:     %t=();
1.16      www       767:     my $pattern='';
                    768:     if ($sheettype eq 'assesscalc') {
                    769: 	$pattern='A';
                    770:     } else {
                    771:         $pattern='[A-Z]';
                    772:     }
1.78      matthew   773:     foreach (keys(%f)) {
1.20      www       774: 	if ($_=~/template\_(\w)/) {
                    775: 	  my $col=$1;
                    776:           unless ($col=~/^$pattern/) {
1.78      matthew   777: 	    foreach (keys(%f)) {
1.20      www       778: 	      if ($_=~/A(\d+)/) {
                    779: 		my $trow=$1;
                    780:                 if ($trow) {
1.88      matthew   781:                     # Get the name of this cell
1.20      www       782: 		    my $lb=$col.$trow;
1.88      matthew   783:                     # Grab the template declaration
1.20      www       784:                     $t{$lb}=$f{'template_'.$col};
1.88      matthew   785:                     # Replace '#' with the row number
1.20      www       786:                     $t{$lb}=~s/\#/$trow/g;
1.88      matthew   787:                     # Replace '....' with ','
1.20      www       788:                     $t{$lb}=~s/\.\.+/\,/g;
1.88      matthew   789:                     # Replace 'A0' with the value from 'A0'
1.20      www       790:                     $t{$lb}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$v\{\'$2\'\}/g;
1.88      matthew   791:                     # Replace parameters
1.59      www       792:                     $t{$lb}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.&expandnamed($2)/ge;
1.20      www       793:                 }
                    794: 	      }
1.78      matthew   795: 	    }
1.20      www       796: 	  }
                    797:       }
1.78      matthew   798:     }
                    799:     foreach (keys(%f)) {
1.20      www       800: 	if (($f{$_}) && ($_!~/template\_/)) {
1.42      www       801:             my $matches=($_=~/^$pattern(\d+)/);
                    802:             if  (($matches) && ($1)) {
1.6       www       803: 	        unless ($f{$_}=~/^\!/) {
                    804: 		    $t{$_}=$c{$_};
                    805:                 }
                    806:             } else {
                    807: 	       $t{$_}=$f{$_};
1.7       www       808:                $t{$_}=~s/\.\.+/\,/g;
                    809:                $t{$_}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$v\{\'$2\'\}/g;
1.59      www       810:                $t{$_}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.&expandnamed($2)/ge;
1.6       www       811:             }
1.1       www       812:         }
1.78      matthew   813:     }
1.88      matthew   814:     # For some reason 'A0' gets special treatment...  This seems superfluous
                    815:     # but I imagine it is here for a reason.
1.17      www       816:     $t{'A0'}=$f{'A0'};
                    817:     $t{'A0'}=~s/\.\.+/\,/g;
                    818:     $t{'A0'}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$v\{\'$2\'\}/g;
1.59      www       819:     $t{'A0'}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.&expandnamed($2)/ge;
1.1       www       820: }
                    821: 
1.4       www       822: sub calc {
1.1       www       823:     %v=();
1.4       www       824:     &sett();
1.1       www       825:     my $notfinished=1;
                    826:     my $depth=0;
                    827:     while ($notfinished) {
                    828: 	$notfinished=0;
1.78      matthew   829:         foreach (keys(%t)) {
1.1       www       830:             my $old=$v{$_};
1.4       www       831:             $v{$_}=eval($t{$_});
1.1       www       832: 	    if ($@) {
                    833: 		%v=();
                    834:                 return $@;
                    835:             }
                    836: 	    if ($v{$_} ne $old) { $notfinished=1; }
1.78      matthew   837:         }
1.1       www       838:         $depth++;
                    839:         if ($depth>100) {
                    840: 	    %v=();
                    841:             return 'Maximum calculation depth exceeded';
                    842:         }
                    843:     }
                    844:     return '';
                    845: }
                    846: 
1.21      www       847: sub templaterow {
                    848:     my @cols=();
                    849:     $cols[0]='<b><font size=+1>Template</font></b>';
1.78      matthew   850:     foreach ('A','B','C','D','E','F','G','H','I','J','K','L','M',
                    851: 	     'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
                    852: 	     'a','b','c','d','e','f','g','h','i','j','k','l','m',
                    853: 	     'n','o','p','q','r','s','t','u','v','w','x','y','z') {
1.21      www       854:         my $fm=$f{'template_'.$_};
                    855:         $fm=~s/[\'\"]/\&\#34;/g;
                    856:         $cols[$#cols+1]="'template_$_','$fm'".'___eq___'.$fm;
1.78      matthew   857:     }
1.21      www       858:     return @cols;
                    859: }
                    860: 
1.16      www       861: sub outrowassess {
1.6       www       862:     my $n=shift;
                    863:     my @cols=();
                    864:     if ($n) {
1.55      www       865:        my ($usy,$ufn)=split(/\_\_\&\&\&\_\_/,$f{'A'.$n});
1.92    ! www       866:       if ($rl{$usy}) {
1.58      www       867:        $cols[0]=$rl{$usy}.'<br>'.
1.55      www       868:                 '<select name="sel_'.$n.'" onChange="changesheet('.$n.
                    869:                 ')"><option name="default">Default</option>';
1.92    ! www       870:       } else { $cols[0]=''; }
1.78      matthew   871:        foreach (@os) {
1.55      www       872:            $cols[0].='<option name="'.$_.'"';
                    873:             if ($ufn eq $_) {
                    874:                $cols[0].=' selected';
                    875:             }
                    876:             $cols[0].='>'.$_.'</option>';
1.78      matthew   877:        }
1.55      www       878:        $cols[0].='</select>';
1.6       www       879:     } else {
                    880:        $cols[0]='<b><font size=+1>Export</font></b>';
                    881:     }
1.78      matthew   882:     foreach ('A','B','C','D','E','F','G','H','I','J','K','L','M',
                    883: 	     'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
                    884: 	     'a','b','c','d','e','f','g','h','i','j','k','l','m',
                    885: 	     'n','o','p','q','r','s','t','u','v','w','x','y','z') {
1.6       www       886:         my $fm=$f{$_.$n};
                    887:         $fm=~s/[\'\"]/\&\#34;/g;
1.83      matthew   888:         push(@cols,"'$_$n','$fm'".'___eq___'.$v{$_.$n});
1.78      matthew   889:     }
1.6       www       890:     return @cols;
                    891: }
                    892: 
1.18      www       893: sub outrow {
                    894:     my $n=shift;
                    895:     my @cols=();
                    896:     if ($n) {
1.21      www       897:        $cols[0]=$rl{$f{'A'.$n}};
1.18      www       898:     } else {
                    899:        $cols[0]='<b><font size=+1>Export</font></b>';
                    900:     }
1.78      matthew   901:     foreach ('A','B','C','D','E','F','G','H','I','J','K','L','M',
                    902: 	     'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
                    903: 	     'a','b','c','d','e','f','g','h','i','j','k','l','m',
                    904: 	     'n','o','p','q','r','s','t','u','v','w','x','y','z') {
1.18      www       905:         my $fm=$f{$_.$n};
                    906:         $fm=~s/[\'\"]/\&\#34;/g;
                    907:         $cols[$#cols+1]="'$_$n','$fm'".'___eq___'.$v{$_.$n};
1.78      matthew   908:     }
1.18      www       909:     return @cols;
                    910: }
                    911: 
1.14      www       912: sub exportrowa {
1.28      www       913:     my @exportarray=();
1.78      matthew   914:     foreach ('A','B','C','D','E','F','G','H','I','J','K','L','M',
                    915: 	     'N','O','P','Q','R','S','T','U','V','W','X','Y','Z') {
1.28      www       916: 	$exportarray[$#exportarray+1]=$v{$_.'0'};
1.78      matthew   917:     } 
1.28      www       918:     return @exportarray;
1.14      www       919: }
                    920: 
1.4       www       921: # ------------------------------------------- End of "Inside of the safe space"
                    922: ENDDEFS
                    923:     $safeeval->reval($code);
                    924:     return $safeeval;
                    925: }
                    926: 
                    927: # ------------------------------------------------ Add or change formula values
                    928: 
                    929: sub setformulas {
1.34      www       930:     my ($safeeval,%f)=@_;
                    931:     %{$safeeval->varglob('f')}=%f;
1.6       www       932: }
                    933: 
                    934: # ------------------------------------------------ Add or change formula values
                    935: 
                    936: sub setconstants {
1.34      www       937:     my ($safeeval,%c)=@_;
                    938:     %{$safeeval->varglob('c')}=%c;
1.6       www       939: }
                    940: 
1.55      www       941: # --------------------------------------------- Set names of other spreadsheets
                    942: 
                    943: sub setothersheets {
                    944:     my ($safeeval,@os)=@_;
                    945:     @{$safeeval->varglob('os')}=@os;
                    946: }
                    947: 
1.6       www       948: # ------------------------------------------------ Add or change formula values
                    949: 
                    950: sub setrowlabels {
1.34      www       951:     my ($safeeval,%rl)=@_;
                    952:     %{$safeeval->varglob('rl')}=%rl;
1.4       www       953: }
                    954: 
                    955: # ------------------------------------------------------- Calculate spreadsheet
                    956: 
                    957: sub calcsheet {
                    958:     my $safeeval=shift;
                    959:     $safeeval->reval('&calc();');
                    960: }
                    961: 
                    962: # ------------------------------------------------------------------ Get values
                    963: 
                    964: sub getvalues {
                    965:     my $safeeval=shift;
                    966:     return $safeeval->reval('%v');
                    967: }
                    968: 
                    969: # ---------------------------------------------------------------- Get formulas
                    970: 
                    971: sub getformulas {
                    972:     my $safeeval=shift;
1.34      www       973:     return %{$safeeval->varglob('f')};
1.4       www       974: }
                    975: 
1.5       www       976: # -------------------------------------------------------------------- Get type
                    977: 
                    978: sub gettype {
                    979:     my $safeeval=shift;
                    980:     return $safeeval->reval('$sheettype');
                    981: }
1.27      www       982: 
1.6       www       983: # ------------------------------------------------------------------ Set maxrow
                    984: 
                    985: sub setmaxrow {
                    986:     my ($safeeval,$row)=@_;
                    987:     $safeeval->reval('$maxrow='.$row.';');
                    988: }
                    989: 
                    990: # ------------------------------------------------------------------ Get maxrow
                    991: 
                    992: sub getmaxrow {
                    993:     my $safeeval=shift;
                    994:     return $safeeval->reval('$maxrow');
                    995: }
1.5       www       996: 
1.6       www       997: # ---------------------------------------------------------------- Set filename
1.5       www       998: 
                    999: sub setfilename {
                   1000:     my ($safeeval,$fn)=@_;
1.11      www      1001:     $safeeval->reval('$filename="'.$fn.'";');
1.5       www      1002: }
                   1003: 
1.6       www      1004: # ---------------------------------------------------------------- Get filename
1.5       www      1005: 
                   1006: sub getfilename {
                   1007:     my $safeeval=shift;
                   1008:     return $safeeval->reval('$filename');
                   1009: }
1.29      www      1010: 
1.28      www      1011: # --------------------------------------------------------------- Get course ID
                   1012: 
                   1013: sub getcid {
                   1014:     my $safeeval=shift;
                   1015:     return $safeeval->reval('$cid');
                   1016: }
1.14      www      1017: 
1.29      www      1018: # --------------------------------------------------------- Get course filename
                   1019: 
                   1020: sub getcfn {
                   1021:     my $safeeval=shift;
                   1022:     return $safeeval->reval('$cfn');
                   1023: }
                   1024: 
1.27      www      1025: # ----------------------------------------------------------- Get course number
                   1026: 
                   1027: sub getcnum {
                   1028:     my $safeeval=shift;
                   1029:     return $safeeval->reval('$cnum');
                   1030: }
                   1031: 
                   1032: # ------------------------------------------------------------- Get course home
                   1033: 
                   1034: sub getchome {
                   1035:     my $safeeval=shift;
                   1036:     return $safeeval->reval('$chome');
                   1037: }
                   1038: 
                   1039: # ----------------------------------------------------------- Get course domain
                   1040: 
                   1041: sub getcdom {
                   1042:     my $safeeval=shift;
                   1043:     return $safeeval->reval('$cdom');
                   1044: }
                   1045: 
                   1046: # ---------------------------------------------------------- Get course section
                   1047: 
                   1048: sub getcsec {
                   1049:     my $safeeval=shift;
                   1050:     return $safeeval->reval('$csec');
                   1051: }
                   1052: 
                   1053: # --------------------------------------------------------------- Get user name
                   1054: 
                   1055: sub getuname {
                   1056:     my $safeeval=shift;
                   1057:     return $safeeval->reval('$uname');
                   1058: }
                   1059: 
                   1060: # ------------------------------------------------------------- Get user domain
                   1061: 
                   1062: sub getudom {
                   1063:     my $safeeval=shift;
                   1064:     return $safeeval->reval('$udom');
                   1065: }
                   1066: 
                   1067: # --------------------------------------------------------------- Get user home
                   1068: 
                   1069: sub getuhome {
                   1070:     my $safeeval=shift;
                   1071:     return $safeeval->reval('$uhome');
                   1072: }
                   1073: 
                   1074: # -------------------------------------------------------------------- Get symb
                   1075: 
                   1076: sub getusymb {
                   1077:     my $safeeval=shift;
                   1078:     return $safeeval->reval('$usymb');
                   1079: }
                   1080: 
1.14      www      1081: # ------------------------------------------------------------- Export of A-row
                   1082: 
1.28      www      1083: sub exportdata {
1.14      www      1084:     my $safeeval=shift;
                   1085:     return $safeeval->reval('&exportrowa()');
                   1086: }
                   1087: 
1.55      www      1088: 
1.5       www      1089: # ========================================================== End of Spreadsheet
                   1090: # =============================================================================
                   1091: 
1.27      www      1092: #
                   1093: # Procedures for screen output
                   1094: #
1.6       www      1095: # --------------------------------------------- Produce output row n from sheet
                   1096: 
                   1097: sub rown {
                   1098:     my ($safeeval,$n)=@_;
1.21      www      1099:     my $defaultbg;
1.24      www      1100:     my $rowdata='';
1.61      www      1101:     my $dataflag=0;
1.21      www      1102:     unless ($n eq '-') {
                   1103:        $defaultbg=((($n-1)/5)==int(($n-1)/5))?'#E0E0':'#FFFF';
                   1104:     } else {
                   1105:        $defaultbg='#E0FF';
                   1106:     }
1.71      www      1107:     unless ($ENV{'form.showcsv'}) {
                   1108:        $rowdata.="\n<tr><td><b><font size=+1>$n</font></b></td>";
                   1109:     } else {
                   1110:        $rowdata.="\n".'"'.$n.'"';
                   1111:     }
1.6       www      1112:     my $showf=0;
1.16      www      1113:     my $proc;
1.18      www      1114:     my $maxred;
1.62      www      1115:     my $sheettype=&gettype($safeeval);
                   1116:     if ($sheettype eq 'studentcalc') {
1.55      www      1117:         $proc='&outrowassess';
                   1118:         $maxred=26;
                   1119:     } else {
                   1120:         $proc='&outrow';
                   1121:     }
1.62      www      1122:     if ($sheettype eq 'assesscalc') {
1.18      www      1123:         $maxred=1;
1.16      www      1124:     } else {
1.18      www      1125:         $maxred=26;
1.16      www      1126:     }
1.61      www      1127:     if ($n eq '-') { $proc='&templaterow'; $n=-1; $dataflag=1; }
1.78      matthew  1128:     foreach ($safeeval->reval($proc.'('.$n.')')) {
1.9       www      1129:        my $bgcolor=$defaultbg.((($showf-1)/5==int(($showf-1)/5))?'99':'DD');
1.6       www      1130:        my ($fm,$vl)=split(/\_\_\_eq\_\_\_/,$_);
1.62      www      1131:        if ((($vl ne '') || ($vl eq '0')) &&
                   1132:            (($showf==1) || ($sheettype ne 'studentcalc'))) { $dataflag=1; }
1.6       www      1133:        if ($showf==0) { $vl=$_; }
1.71      www      1134:       unless ($ENV{'form.showcsv'}) {
1.18      www      1135:        if ($showf<=$maxred) { $bgcolor='#FFDDDD'; }
1.9       www      1136:        if (($n==0) && ($showf<=26)) { $bgcolor='#CCCCFF'; } 
1.18      www      1137:        if (($showf>$maxred) || ((!$n) && ($showf>0))) {
1.6       www      1138: 	   if ($vl eq '') {
1.9       www      1139: 	       $vl='<font size=+2 color='.$bgcolor.'>&#35;</font>';
1.6       www      1140:            }
                   1141:            $rowdata.=
1.10      www      1142:        '<td bgcolor='.$bgcolor.'><a href="javascript:celledit('.$fm.');">'.$vl.
1.6       www      1143: 	       '</a></td>';
                   1144:        } else {
1.9       www      1145:            $rowdata.='<td bgcolor='.$bgcolor.'>&nbsp;'.$vl.'&nbsp;</td>';
1.6       www      1146:        }
1.71      www      1147:       } else {
                   1148: 	  $rowdata.=',"'.$vl.'"';
                   1149:       }
1.6       www      1150:        $showf++;
1.78      matthew  1151:     }  # End of foreach($safeval...)
1.61      www      1152:     if ($ENV{'form.showall'} || ($dataflag)) {
1.71      www      1153:        return $rowdata.($ENV{'form.showcsv'}?'':'</tr>');
1.61      www      1154:     } else {
1.63      www      1155:        return '';
1.61      www      1156:     }
1.6       www      1157: }
                   1158: 
                   1159: # ------------------------------------------------------------- Print out sheet
                   1160: 
                   1161: sub outsheet {
1.24      www      1162:     my ($r,$safeeval)=@_;
1.18      www      1163:     my $maxred;
                   1164:     my $realm;
                   1165:     if (&gettype($safeeval) eq 'assesscalc') {
                   1166:         $maxred=1;
                   1167:         $realm='Assessment';
                   1168:     } elsif (&gettype($safeeval) eq 'studentcalc') {
                   1169:         $maxred=26;
                   1170:         $realm='User';
                   1171:     } else {
                   1172:         $maxred=26;
                   1173:         $realm='Course';
                   1174:     }
                   1175:     my $maxyellow=52-$maxred;
1.71      www      1176:     my $tabledata;
                   1177:     unless ($ENV{'form.showcsv'}) {
                   1178:        $tabledata=
1.24      www      1179:         '<table border=2><tr><th colspan=2 rowspan=2><font size=+2>'.
1.18      www      1180:                   $realm.'</font></th>'.
                   1181:                   '<td bgcolor=#FFDDDD colspan='.$maxred.
                   1182:                   '><b><font size=+1>Import</font></b></td>'.
                   1183:                   '<td colspan='.$maxyellow.
                   1184: 		  '><b><font size=+1>Calculations</font></b></td></tr><tr>';
                   1185:     my $showf=0;
1.78      matthew  1186:     foreach ('A','B','C','D','E','F','G','H','I','J','K','L','M',
                   1187: 	     'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
                   1188: 	     'a','b','c','d','e','f','g','h','i','j','k','l','m',
                   1189: 	     'n','o','p','q','r','s','t','u','v','w','x','y','z') {
1.18      www      1190:         $showf++;
                   1191:         if ($showf<=$maxred) { 
                   1192:            $tabledata.='<td bgcolor="#FFDDDD">'; 
                   1193:         } else {
                   1194:            $tabledata.='<td>';
                   1195:         }
                   1196:         $tabledata.="<b><font size=+1>$_</font></b></td>";
1.78      matthew  1197:     }
1.71      www      1198:     $tabledata.='</tr>'.&rown($safeeval,'-').&rown($safeeval,0);
                   1199:    } else { $tabledata='<pre>'; }
                   1200: 
                   1201:     $r->print($tabledata);
                   1202: 
1.6       www      1203:     my $row;
                   1204:     my $maxrow=&getmaxrow($safeeval);
1.64      www      1205: 
1.65      www      1206:     my @sortby=();
                   1207:     my @sortidx=();
                   1208:     for ($row=1;$row<=$maxrow;$row++) {
                   1209:        $sortby[$row-1]=$safeeval->reval('$f{"A'.$row.'"}');
                   1210:        $sortidx[$row-1]=$row-1;
                   1211:     }
                   1212:     @sortidx=sort { $sortby[$a] cmp $sortby[$b]; } @sortidx;
1.64      www      1213: 
1.63      www      1214:         my $what='Student';
                   1215:         if (&gettype($safeeval) eq 'assesscalc') {
                   1216: 	    $what='Item';
                   1217: 	} elsif (&gettype($safeeval) eq 'studentcalc') {
                   1218:             $what='Assessment';
                   1219:         }
1.65      www      1220: 
                   1221:     my $n=0;
                   1222:     for ($row=0;$row<$maxrow;$row++) {
                   1223:      my $thisrow=&rown($safeeval,$sortidx[$row]+1);
                   1224:      if ($thisrow) {
1.71      www      1225:        if (($n/25==int($n/25)) && (!$ENV{'form.showcsv'})) {
1.65      www      1226: 	$r->print("</table>\n<br>\n");
                   1227:         $r->rflush();
                   1228:         $r->print('<table border=2><tr><td>&nbsp;<td>'.$what.'</td>');
1.78      matthew  1229:         foreach ('A','B','C','D','E','F','G','H','I','J','K','L','M',
                   1230: 		 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
                   1231: 		 'a','b','c','d','e','f','g','h','i','j','k','l','m',
                   1232: 		 'n','o','p','q','r','s','t','u','v','w','x','y','z') {
1.63      www      1233:            $r->print('<td>'.$_.'</td>');
1.78      matthew  1234:         }
1.63      www      1235:         $r->print('</tr>');
                   1236:        }
1.64      www      1237:        $n++;
                   1238:        $r->print($thisrow);
1.63      www      1239:       }
1.6       www      1240:     }
1.71      www      1241:     $r->print($ENV{'form.showcsv'}?'</pre>':'</table>');
1.6       www      1242: }
                   1243: 
1.27      www      1244: #
1.55      www      1245: # ----------------------------------------------- Read list of available sheets
                   1246: # 
                   1247: sub othersheets {
                   1248:     my ($safeeval,$stype)=@_;
1.81      matthew  1249:     #
1.55      www      1250:     my $cnum=&getcnum($safeeval);
                   1251:     my $cdom=&getcdom($safeeval);
                   1252:     my $chome=&getchome($safeeval);
1.81      matthew  1253:     #
1.55      www      1254:     my @alternatives=();
1.81      matthew  1255:     my %results=&Apache::lonnet::dump($stype.'_spreadsheets',$cdom,$cnum);
                   1256:     my ($tmp) = keys(%results);
                   1257:     unless ($tmp =~ /^(con_lost|error|no_such_host)/i) {
                   1258:         @alternatives = sort (keys(%results));
                   1259:     }
1.55      www      1260:     return @alternatives; 
                   1261: }
                   1262: 
1.82      matthew  1263: 
                   1264: #
                   1265: # -------------------------------------- Parse a spreadsheet
                   1266: # 
                   1267: sub parse_sheet {
                   1268:     # $sheetxml is a scalar reference or a scalar
                   1269:     my ($sheetxml) = @_;
                   1270:     if (! ref($sheetxml)) {
                   1271:         my $tmp = $sheetxml;
                   1272:         $sheetxml = \$tmp;
                   1273:     }
                   1274:     my %f;
                   1275:     my $parser=HTML::TokeParser->new($sheetxml);
                   1276:     my $token;
                   1277:     while ($token=$parser->get_token) {
                   1278:         if ($token->[0] eq 'S') {
                   1279:             if ($token->[1] eq 'field') {
                   1280:                 $f{$token->[2]->{'col'}.$token->[2]->{'row'}}=
                   1281:                     $parser->get_text('/field');
                   1282:             }
                   1283:             if ($token->[1] eq 'template') {
                   1284:                 $f{'template_'.$token->[2]->{'col'}}=
                   1285:                     $parser->get_text('/template');
                   1286:             }
                   1287:         }
                   1288:     }
                   1289:     return \%f;
                   1290: }
                   1291: 
1.55      www      1292: #
1.27      www      1293: # -------------------------------------- Read spreadsheet formulas for a course
                   1294: #
                   1295: 
                   1296: sub readsheet {
                   1297:   my ($safeeval,$fn)=@_;
                   1298:   my $stype=&gettype($safeeval);
                   1299:   my $cnum=&getcnum($safeeval);
1.28      www      1300:   my $cdom=&getcdom($safeeval);
                   1301:   my $chome=&getchome($safeeval);
1.27      www      1302: 
1.83      matthew  1303:   if (! defined($fn)) {
1.82      matthew  1304:       # There is no filename. Look for defaults in course and global, cache
1.28      www      1305:       unless ($fn=$defaultsheets{$cnum.'_'.$cdom.'_'.$stype}) {
1.82      matthew  1306:           my %tmphash = &Apache::lonnet::get('environment',
                   1307:                                              ['spreadsheet_default_'.$stype],
                   1308:                                              $cdom,$cnum);
                   1309:           my ($tmp) = keys(%tmphash);
                   1310:           if ($tmp =~ /^(con_lost|error|no_such_host)/i) {
                   1311:               $fn = 'default_'.$stype;
                   1312:           } else {
                   1313:               $fn = $tmphash{'spreadsheet_default_'.$stype};
                   1314:           } 
1.90      www      1315:           unless (($fn) && ($fn!~/^error\:/)) {
                   1316:  	     $fn='default_'.$stype;
                   1317:           }
1.82      matthew  1318:           $defaultsheets{$cnum.'_'.$cdom.'_'.$stype}=$fn; 
                   1319:       }
1.27      www      1320:   }
                   1321: 
                   1322: # ---------------------------------------------------------- fn now has a value
                   1323: 
                   1324:   &setfilename($safeeval,$fn);
                   1325: 
                   1326: # ------------------------------------------------------ see if sheet is cached
                   1327:   my $fstring='';
1.28      www      1328:   if ($fstring=$spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}) {
                   1329:       &setformulas($safeeval,split(/\_\_\_\;\_\_\_/,$fstring));
1.27      www      1330:   } else {
1.6       www      1331: 
1.27      www      1332: # ---------------------------------------------------- Not cached, need to read
1.5       www      1333: 
1.27      www      1334:      my %f=();
1.3       www      1335: 
1.27      www      1336:      if ($fn=~/^default\_/) {
1.82      matthew  1337:          my $sheetxml='';
1.10      www      1338:          my $fh;
1.73      www      1339:          my $dfn=$fn;
                   1340:          $dfn=~s/\_/\./g;
                   1341:          if ($fh=Apache::File->new($includedir.'/'.$dfn)) {
1.82      matthew  1342:              $sheetxml=join('',<$fh>);
                   1343:          } else {
1.73      www      1344:              $sheetxml='<field row="0" col="A">"Error"</field>';
1.82      matthew  1345:          }
1.83      matthew  1346:          %f=%{&parse_sheet(\$sheetxml)};
1.82      matthew  1347:      } elsif($fn=~/\/*\.spreadsheet$/) {
1.83      matthew  1348:          my $sheetxml=&Apache::lonnet::getfile
                   1349:              (&Apache::lonnet::filelocation('',$fn));
                   1350:          if ($sheetxml == -1) {
1.82      matthew  1351:              $sheetxml='<field row="0" col="A">"Error loading spreadsheet '
                   1352:                  .$fn.'"</field>';
                   1353:          }
1.83      matthew  1354:          %f=%{&parse_sheet(\$sheetxml)};
1.82      matthew  1355:      } else {
                   1356:          my $sheet='';
                   1357:          my %tmphash = &Apache::lonnet::dump($fn,$cdom,$cnum);
                   1358:          my ($tmp) = keys(%tmphash);
                   1359:          unless ($tmp =~ /^(con_lost|error|no_such_host)/i) {
                   1360:              foreach (keys(%tmphash)) {
                   1361:                  $f{$_}=$tmphash{$_};
1.20      www      1362:              }
1.82      matthew  1363:          }
                   1364:      }
1.27      www      1365: # --------------------------------------------------------------- Cache and set
1.28      www      1366:        $spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}=join('___;___',%f);  
1.27      www      1367:        &setformulas($safeeval,%f);
1.3       www      1368:     }
                   1369: }
                   1370: 
1.28      www      1371: # -------------------------------------------------------- Make new spreadsheet
                   1372: 
                   1373: sub makenewsheet {
                   1374:     my ($uname,$udom,$stype,$usymb)=@_;
1.36      www      1375:     my $safeeval=initsheet($stype);
1.28      www      1376:     $safeeval->reval(
1.29      www      1377:        '$uname="'.$uname.
                   1378:       '";$udom="'.$udom.
                   1379:       '";$uhome="'.&Apache::lonnet::homeserver($uname,$udom).
                   1380:       '";$sheettype="'.$stype.
                   1381:       '";$usymb="'.$usymb.
1.30      www      1382:       '";$csec="'.&Apache::lonnet::usection($udom,$uname,
                   1383:                                             $ENV{'request.course.id'}).
1.29      www      1384:       '";$cid="'.$ENV{'request.course.id'}.
                   1385:       '";$cfn="'.$ENV{'request.course.fn'}.
                   1386:       '";$cnum="'.$ENV{'course.'.$ENV{'request.course.id'}.'.num'}.
                   1387:       '";$cdom="'.$ENV{'course.'.$ENV{'request.course.id'}.'.domain'}.
                   1388:       '";$chome="'.$ENV{'course.'.$ENV{'request.course.id'}.'.home'}.'";');
1.28      www      1389:     return $safeeval;
                   1390: }
                   1391: 
1.19      www      1392: # ------------------------------------------------------------ Save spreadsheet
                   1393: 
                   1394: sub writesheet {
1.28      www      1395:   my ($safeeval,$makedef)=@_;
                   1396:   my $cid=&getcid($safeeval);
                   1397:   if (&Apache::lonnet::allowed('opa',$cid)) {
1.19      www      1398:     my %f=&getformulas($safeeval);
1.28      www      1399:     my $stype=&gettype($safeeval);
                   1400:     my $cnum=&getcnum($safeeval);
                   1401:     my $cdom=&getcdom($safeeval);
                   1402:     my $chome=&getchome($safeeval);
                   1403:     my $fn=&getfilename($safeeval);
                   1404: 
                   1405: # ------------------------------------------------------------- Cache new sheet
                   1406:     $spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}=join('___;___',%f);    
                   1407: # ----------------------------------------------------------------- Write sheet
1.19      www      1408:     my $sheetdata='';
1.78      matthew  1409:     foreach (keys(%f)) {
1.56      www      1410:      unless ($f{$_} eq 'import') {
1.19      www      1411:        $sheetdata.=&Apache::lonnet::escape($_).'='.
                   1412: 	   &Apache::lonnet::escape($f{$_}).'&';
1.56      www      1413:      }
1.78      matthew  1414:     }
1.19      www      1415:     $sheetdata=~s/\&$//;
1.28      www      1416:     my $reply=&Apache::lonnet::reply('put:'.$cdom.':'.$cnum.':'.$fn.':'.
                   1417:               $sheetdata,$chome);
1.22      www      1418:     if ($reply eq 'ok') {
1.28      www      1419:           $reply=&Apache::lonnet::reply('put:'.$cdom.':'.$cnum.':'.
1.30      www      1420:               $stype.'_spreadsheets:'.
1.55      www      1421:               &Apache::lonnet::escape($fn).'='.$ENV{'user.name'}.'@'.
                   1422:                                                $ENV{'user.domain'},
1.28      www      1423:               $chome);
                   1424:           if ($reply eq 'ok') {
                   1425:               if ($makedef) { 
                   1426:                 return &Apache::lonnet::reply('put:'.$cdom.':'.$cnum.
                   1427:                                 ':environment:spreadsheet_default_'.$stype.'='.
                   1428:                                 &Apache::lonnet::escape($fn),
                   1429:                                 $chome);
                   1430: 	      } else {
                   1431: 		  return $reply;
                   1432:     	      }
                   1433: 	   } else {
                   1434: 	       return $reply;
                   1435:            }
1.22      www      1436:       } else {
                   1437: 	  return $reply;
                   1438:       }
                   1439:   }
                   1440:   return 'unauthorized';
1.19      www      1441: }
                   1442: 
1.10      www      1443: # ----------------------------------------------- Make a temp copy of the sheet
1.28      www      1444: # "Modified workcopy" - interactive only
                   1445: #
1.10      www      1446: 
                   1447: sub tmpwrite {
1.28      www      1448:     my $safeeval=shift;
                   1449:     my $fn=$ENV{'user.name'}.'_'.
                   1450:            $ENV{'user.domain'}.'_spreadsheet_'.&getusymb($safeeval).'_'.
                   1451:            &getfilename($safeeval);
1.10      www      1452:     $fn=~s/\W/\_/g;
                   1453:     $fn=$tmpdir.$fn.'.tmp';
                   1454:     my $fh;
                   1455:     if ($fh=Apache::File->new('>'.$fn)) {
                   1456: 	print $fh join("\n",&getformulas($safeeval));
                   1457:     }
                   1458: }
                   1459: 
                   1460: # ---------------------------------------------------------- Read the temp copy
                   1461: 
                   1462: sub tmpread {
1.28      www      1463:     my ($safeeval,$nfield,$nform)=@_;
                   1464:     my $fn=$ENV{'user.name'}.'_'.
                   1465:            $ENV{'user.domain'}.'_spreadsheet_'.&getusymb($safeeval).'_'.
                   1466:            &getfilename($safeeval);
1.10      www      1467:     $fn=~s/\W/\_/g;
                   1468:     $fn=$tmpdir.$fn.'.tmp';
                   1469:     my $fh;
                   1470:     my %fo=();
1.92    ! www      1471:     my $countrows=0;
1.10      www      1472:     if ($fh=Apache::File->new($fn)) {
                   1473:         my $name;
                   1474:         while ($name=<$fh>) {
                   1475: 	    chomp($name);
                   1476:             my $value=<$fh>;
                   1477:             chomp($value);
                   1478:             $fo{$name}=$value;
1.92    ! www      1479:             $countrows++;
1.10      www      1480:         }
                   1481:     }
1.55      www      1482:     if ($nform eq 'changesheet') {
1.57      www      1483:         $fo{'A'.$nfield}=(split(/\_\_\&\&\&\_\_/,$fo{'A'.$nfield}))[0];
1.55      www      1484:         unless ($ENV{'form.sel_'.$nfield} eq 'Default') {
1.57      www      1485: 	    $fo{'A'.$nfield}.='__&&&__'.$ENV{'form.sel_'.$nfield};
1.55      www      1486:         }
1.92    ! www      1487:     } elsif ($nfield eq 'insertrow') {
        !          1488:         if ($nform eq 'top') {
        !          1489: 	    $fo{'A'.$countrows}='AAAAA_'.$countrows;
        !          1490:         } else {
        !          1491:             $fo{'A'.$countrows}='zzzzz_'.$countrows;
        !          1492:         }
1.55      www      1493:     } else {
                   1494:        if ($nfield) { $fo{$nfield}=$nform; }
                   1495:     }
1.10      www      1496:     &setformulas($safeeval,%fo);
                   1497: }
                   1498: 
1.11      www      1499: # ================================================================== Parameters
                   1500: # -------------------------------------------- Figure out a cascading parameter
1.28      www      1501: #
1.29      www      1502: # For this function to work
                   1503: #
                   1504: # * parmhash needs to be tied
                   1505: # * courseopt and useropt need to be initialized for this user and course
                   1506: #
1.11      www      1507: 
                   1508: sub parmval {
1.28      www      1509:     my ($what,$safeeval)=@_;
                   1510:     my $cid=&getcid($safeeval);
                   1511:     my $csec=&getcsec($safeeval);
                   1512:     my $uname=&getuname($safeeval);
                   1513:     my $udom=&getudom($safeeval);
                   1514:     my $symb=&getusymb($safeeval);
1.11      www      1515: 
                   1516:     unless ($symb) { return ''; }
                   1517:     my $result='';
                   1518: 
                   1519:     my ($mapname,$id,$fn)=split(/\_\_\_/,$symb);
                   1520: # ----------------------------------------------------- Cascading lookup scheme
1.12      www      1521:        my $rwhat=$what;
                   1522:        $what=~s/^parameter\_//;
1.68      www      1523:        $what=~s/\_([^\_]+)$/\.$1/;
1.11      www      1524: 
                   1525:        my $symbparm=$symb.'.'.$what;
                   1526:        my $mapparm=$mapname.'___(all).'.$what;
1.29      www      1527:        my $usercourseprefix=$uname.'_'.$udom.'_'.$cid;
1.11      www      1528: 
                   1529:        my $seclevel=
1.28      www      1530:             $usercourseprefix.'.['.
1.11      www      1531: 		$csec.'].'.$what;
                   1532:        my $seclevelr=
1.28      www      1533:             $usercourseprefix.'.['.
1.11      www      1534: 		$csec.'].'.$symbparm;
                   1535:        my $seclevelm=
1.28      www      1536:             $usercourseprefix.'.['.
1.11      www      1537: 		$csec.'].'.$mapparm;
                   1538: 
                   1539:        my $courselevel=
1.28      www      1540:             $usercourseprefix.'.'.$what;
1.11      www      1541:        my $courselevelr=
1.28      www      1542:             $usercourseprefix.'.'.$symbparm;
1.11      www      1543:        my $courselevelm=
1.28      www      1544:             $usercourseprefix.'.'.$mapparm;
1.12      www      1545: 
1.11      www      1546: # ---------------------------------------------------------- fourth, check user
                   1547:       
                   1548:       if ($uname) { 
                   1549: 
                   1550:        if ($useropt{$courselevelr}) { return $useropt{$courselevelr}; }
                   1551: 
                   1552:        if ($useropt{$courselevelm}) { return $useropt{$courselevelm}; }
                   1553: 
                   1554:        if ($useropt{$courselevel}) { return $useropt{$courselevel}; }
                   1555: 
                   1556:       }
                   1557: 
                   1558: # --------------------------------------------------------- third, check course
                   1559:      
                   1560:        if ($csec) {
                   1561:  
                   1562:         if ($courseopt{$seclevelr}) { return $courseopt{$seclevelr}; }
                   1563: 
                   1564:         if ($courseopt{$seclevelm}) { return $courseopt{$seclevelm}; }  
                   1565: 
                   1566:         if ($courseopt{$seclevel}) { return $courseopt{$seclevel}; }
                   1567:   
                   1568:       }
                   1569: 
                   1570:        if ($courseopt{$courselevelr}) { return $courseopt{$courselevelr}; }
                   1571: 
                   1572:        if ($courseopt{$courselevelm}) { return $courseopt{$courselevelm}; }
                   1573: 
                   1574:        if ($courseopt{$courselevel}) { return $courseopt{$courselevel}; }
                   1575: 
                   1576: # ----------------------------------------------------- second, check map parms
                   1577: 
                   1578:        my $thisparm=$parmhash{$symbparm};
                   1579:        if ($thisparm) { return $thisparm; }
                   1580: 
                   1581: # -------------------------------------------------------- first, check default
                   1582: 
1.12      www      1583:        return &Apache::lonnet::metadata($fn,$rwhat.'.default');
1.11      www      1584:         
                   1585: }
                   1586: 
1.23      www      1587: # ---------------------------------------------- Update rows for course listing
1.11      www      1588: 
1.28      www      1589: sub updateclasssheet {
1.23      www      1590:     my $safeeval=shift;
1.28      www      1591:     my $cnum=&getcnum($safeeval);
                   1592:     my $cdom=&getcdom($safeeval);
                   1593:     my $cid=&getcid($safeeval);
                   1594:     my $chome=&getchome($safeeval);
                   1595: 
                   1596: # ---------------------------------------------- Read class list and row labels
                   1597: 
1.23      www      1598:     my $classlst=&Apache::lonnet::reply
1.28      www      1599:                                  ('dump:'.$cdom.':'.$cnum.':classlist',$chome);
1.23      www      1600:     my %currentlist=();
                   1601:     my $now=time;
                   1602:     unless ($classlst=~/^error\:/) {
1.78      matthew  1603:         foreach (split(/\&/,$classlst)) {
1.23      www      1604:             my ($name,$value)=split(/\=/,$_);
1.24      www      1605:             my ($end,$start)=split(/\:/,&Apache::lonnet::unescape($value));
1.23      www      1606:             my $active=1;
                   1607:             if (($end) && ($now>$end)) { $active=0; }
                   1608:             if ($active) {
1.24      www      1609:                 my $rowlabel='';
                   1610:                 $name=&Apache::lonnet::unescape($name);
1.28      www      1611:                 my ($sname,$sdom)=split(/\:/,$name);
                   1612:                 my $ssec=&Apache::lonnet::usection($sdom,$sname,$cid);
                   1613:                 if ($ssec==-1) {
1.71      www      1614: 		   unless ($ENV{'form.showcsv'}) {
1.24      www      1615:                     $rowlabel='<font color=red>Data not available: '.$name.
                   1616: 			      '</font>';
1.71      www      1617: 		   } else {
                   1618: 		       $rowlabel='ERROR","'.$name.
                   1619:                                  '","Data not available","","","';
                   1620:                    }
1.24      www      1621:                 } else {
1.28      www      1622:                     my %reply=&Apache::lonnet::idrget($sdom,$sname);
                   1623:                     my $reply=&Apache::lonnet::reply('get:'.$sdom.':'.$sname.
1.24      www      1624: 		      ':environment:firstname&middlename&lastname&generation',
1.28      www      1625:                       &Apache::lonnet::homeserver($sname,$sdom));
1.71      www      1626: 		   unless ($ENV{'form.showcsv'}) {
1.45      www      1627:                     $rowlabel='<a href="/adm/studentcalc?uname='.$sname.
                   1628:                               '&udom='.$sdom.'">'.
                   1629:                               $ssec.'&nbsp;'.$reply{$sname}.'<br>';
1.78      matthew  1630:                     foreach ( split(/\&/,$reply)) {
1.24      www      1631:                         $rowlabel.=&Apache::lonnet::unescape($_).' ';
1.78      matthew  1632:                     }
1.45      www      1633:                     $rowlabel.='</a>';
1.71      www      1634: 		   } else {
                   1635: 		    $rowlabel=$ssec.'","'.$reply{$sname}.'"';
                   1636:                     my $ncount=0;
1.78      matthew  1637:                     foreach (split(/\&/,$reply)) {
1.71      www      1638:                         $rowlabel.=',"'.&Apache::lonnet::unescape($_).'"';
                   1639:                         $ncount++;
1.78      matthew  1640:                     }
1.71      www      1641:                     unless ($ncount==4) { $rowlabel.=',""'; }
                   1642:                     $rowlabel=~s/\"$//;
                   1643: 		   }
1.24      www      1644:                 }
                   1645: 		$currentlist{&Apache::lonnet::unescape($name)}=$rowlabel;
1.23      www      1646:             }
1.78      matthew  1647:         } # end of foreach (split(/\&/,$classlst))
1.23      www      1648: #
                   1649: # -------------------- Find discrepancies between the course row table and this
                   1650: #
                   1651:         my %f=&getformulas($safeeval);
                   1652:         my $changed=0;
                   1653: 
                   1654:         my $maxrow=0;
                   1655:         my %existing=();
                   1656: 
                   1657: # ----------------------------------------------------------- Now obsolete rows
1.78      matthew  1658: 	foreach (keys(%f)) {
1.23      www      1659: 	    if ($_=~/^A(\d+)/) {
                   1660:                 $maxrow=($1>$maxrow)?$1:$maxrow;
                   1661:                 $existing{$f{$_}}=1;
                   1662: 		unless ((defined($currentlist{$f{$_}})) || (!$1)) {
                   1663: 		   $f{$_}='!!! Obsolete';
                   1664:                    $changed=1;
                   1665:                 }
                   1666:             }
1.78      matthew  1667:         }
1.23      www      1668: 
                   1669: # -------------------------------------------------------- New and unknown keys
                   1670:      
1.78      matthew  1671:         foreach (sort keys(%currentlist)) {
1.23      www      1672:             unless ($existing{$_}) {
                   1673: 		$changed=1;
                   1674:                 $maxrow++;
                   1675:                 $f{'A'.$maxrow}=$_;
                   1676:             }
1.78      matthew  1677:         }
1.23      www      1678:      
                   1679:         if ($changed) { &setformulas($safeeval,%f); }
                   1680: 
                   1681:         &setmaxrow($safeeval,$maxrow);
                   1682:         &setrowlabels($safeeval,%currentlist);
                   1683: 
                   1684:     } else {
                   1685:         return 'Could not access course data';
                   1686:     }
                   1687: }
1.5       www      1688: 
1.28      www      1689: # ----------------------------------- Update rows for student and assess sheets
                   1690: 
                   1691: sub updatestudentassesssheet {
1.5       www      1692:     my $safeeval=shift;
                   1693:     my %bighash;
1.35      www      1694:     my $stype=&gettype($safeeval);
                   1695:     my %current=();
                   1696:     unless ($updatedata{$ENV{'request.course.fn'}.'_'.$stype}) {
1.5       www      1697: # -------------------------------------------------------------------- Tie hash
                   1698:       if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
                   1699:                        &GDBM_READER,0640)) {
                   1700: # --------------------------------------------------------- Get all assessments
                   1701: 
1.52      www      1702: 	my %allkeys=('timestamp' => 
1.75      www      1703:                      'Timestamp of Last Transaction<br>timestamp',
                   1704:                      'subnumber' =>
                   1705:                      'Number of Submissions<br>subnumber',
                   1706:                      'tutornumber' =>
                   1707:                      'Number of Tutor Responses<br>tutornumber',
                   1708:                      'totalpoints' =>
                   1709:                      'Total Points Granted<br>totalpoints');
1.5       www      1710: 
1.50      www      1711:         my $adduserstr='';
                   1712:         if ((&getuname($safeeval) ne $ENV{'user.name'}) ||
                   1713:             (&getudom($safeeval) ne $ENV{'user.domain'})) {
                   1714:             $adduserstr='&uname='.&getuname($safeeval).
                   1715: 		'&udom='.&getudom($safeeval);
                   1716:         }
                   1717: 
1.72      www      1718:         my %allassess=('_feedback' =>
                   1719: 	              '<a href="/adm/assesscalc?usymb=_feedback'.$adduserstr.
                   1720:                        '">Feedback</a>',
                   1721:                        '_evaluation' =>
                   1722: 	              '<a href="/adm/assesscalc?usymb=_evaluation'.$adduserstr.
                   1723:                        '">Evaluation</a>',
                   1724:                        '_tutoring' =>
                   1725: 	              '<a href="/adm/assesscalc?usymb=_tutoring'.$adduserstr.
1.74      www      1726:                        '">Tutoring</a>',
                   1727:                        '_discussion' =>
                   1728: 	              '<a href="/adm/assesscalc?usymb=_discussion'.$adduserstr.
                   1729:                        '">Discussion</a>'
1.72      www      1730:         );
                   1731: 
1.78      matthew  1732:         foreach (keys(%bighash)) {
1.5       www      1733: 	    if ($_=~/^src\_(\d+)\.(\d+)$/) {
                   1734: 	       my $mapid=$1;
                   1735:                my $resid=$2;
                   1736:                my $id=$mapid.'.'.$resid;
                   1737:                my $srcf=$bighash{$_};
                   1738:                if ($srcf=~/\.(problem|exam|quiz|assess|survey|form)$/) {
                   1739:                  my $symb=
                   1740:                      &Apache::lonnet::declutter($bighash{'map_id_'.$mapid}).
                   1741: 			    '___'.$resid.'___'.
                   1742: 			    &Apache::lonnet::declutter($srcf);
1.38      www      1743: 		 $allassess{$symb}=
1.50      www      1744: 	            '<a href="/adm/assesscalc?usymb='.$symb.$adduserstr.'">'.
                   1745:                      $bighash{'title_'.$id}.'</a>';
1.6       www      1746:                  if ($stype eq 'assesscalc') {
1.78      matthew  1747: 		     foreach (split(/\,/,
                   1748: 				    &Apache::lonnet::metadata($srcf,'keys'))) {
1.11      www      1749:                        if (($_=~/^stores\_(.*)/) || ($_=~/^parameter\_(.*)/)) {
1.5       www      1750: 			  my $key=$_;
                   1751:                           my $display=
                   1752: 			      &Apache::lonnet::metadata($srcf,$key.'.display');
                   1753:                           unless ($display) {
1.40      www      1754:                               $display.=
1.5       www      1755: 			         &Apache::lonnet::metadata($srcf,$key.'.name');
                   1756:                           }
1.40      www      1757:                           $display.='<br>'.$key;
1.5       www      1758:                           $allkeys{$key}=$display;
                   1759: 		       }
1.78      matthew  1760:                    } # end of foreach
1.5       www      1761: 	         }
                   1762: 	      }
                   1763: 	   }
1.78      matthew  1764:         } # end of foreach (keys(%bighash))
1.5       www      1765:         untie(%bighash);
                   1766:     
                   1767: #
1.11      www      1768: # %allkeys has a list of storage and parameter displays by unikey
1.5       www      1769: # %allassess has a list of all resource displays by symb
                   1770: #
1.6       www      1771: 
                   1772:         if ($stype eq 'assesscalc') {
                   1773: 	    %current=%allkeys;
                   1774:         } elsif ($stype eq 'studentcalc') {
                   1775:             %current=%allassess;
                   1776:         }
1.35      www      1777:         $updatedata{$ENV{'request.course.fn'}.'_'.$stype}=
                   1778: 	    join('___;___',%current);
                   1779:     } else {
                   1780:         return 'Could not access course data';
                   1781:     }
                   1782: # ------------------------------------------------------ Get current from cache
                   1783:     } else {
                   1784:         %current=split(/\_\_\_\;\_\_\_/,
                   1785: 		       $updatedata{$ENV{'request.course.fn'}.'_'.$stype});
                   1786:     }
                   1787: # -------------------- Find discrepancies between the course row table and this
                   1788: #
                   1789:         my %f=&getformulas($safeeval);
                   1790:         my $changed=0;
1.6       www      1791: 
                   1792:         my $maxrow=0;
                   1793:         my %existing=();
                   1794: 
                   1795: # ----------------------------------------------------------- Now obsolete rows
1.78      matthew  1796: 	foreach (keys(%f)) {
1.6       www      1797: 	    if ($_=~/^A(\d+)/) {
                   1798:                 $maxrow=($1>$maxrow)?$1:$maxrow;
1.57      www      1799:                 my ($usy,$ufn)=split(/\_\_\&\&\&\_\_/,$f{$_});
                   1800:                 $existing{$usy}=1;
                   1801: 		unless ((defined($current{$usy})) || (!$1)) {
1.6       www      1802: 		   $f{$_}='!!! Obsolete';
                   1803:                    $changed=1;
1.57      www      1804: 	        } elsif ($ufn) {
                   1805: 		    $current{$usy}
                   1806:                        =~s/assesscalc\?usymb\=/assesscalc\?ufn\=$ufn\&usymb\=/;
1.5       www      1807:                 }
                   1808:             }
1.78      matthew  1809:         }
1.6       www      1810: 
                   1811: # -------------------------------------------------------- New and unknown keys
                   1812:      
1.78      matthew  1813:         foreach (keys(%current)) {
1.6       www      1814:             unless ($existing{$_}) {
                   1815: 		$changed=1;
                   1816:                 $maxrow++;
                   1817:                 $f{'A'.$maxrow}=$_;
                   1818:             }
1.78      matthew  1819:         }
1.35      www      1820:     
1.6       www      1821:         if ($changed) { &setformulas($safeeval,%f); }
                   1822: 
                   1823:         &setmaxrow($safeeval,$maxrow);
                   1824:         &setrowlabels($safeeval,%current);
1.35      www      1825:  
                   1826:         undef %current;
                   1827:         undef %existing;
1.5       www      1828: }
1.3       www      1829: 
1.24      www      1830: # ------------------------------------------------ Load data for one assessment
1.16      www      1831: 
1.29      www      1832: sub loadstudent {
1.16      www      1833:     my $safeeval=shift;
                   1834:     my %c=();
                   1835:     my %f=&getformulas($safeeval);
1.39      www      1836:     $cachedassess=&getuname($safeeval).':'.&getudom($safeeval);
                   1837:     %cachedstores=();
                   1838:     {
                   1839:       my $reply=&Apache::lonnet::reply('dump:'.&getudom($safeeval).':'.
                   1840:                                                &getuname($safeeval).':'.
                   1841:                                                &getcid($safeeval),
                   1842:                                                &getuhome($safeeval));
                   1843:       unless ($reply=~/^error\:/) {
1.78      matthew  1844: 	 foreach ( split(/\&/,$reply)) {
1.39      www      1845:             my ($name,$value)=split(/\=/,$_);
                   1846:             $cachedstores{&Apache::lonnet::unescape($name)}=
                   1847: 	                  &Apache::lonnet::unescape($value);
1.78      matthew  1848: 	}
1.39      www      1849:       }
                   1850:     }
1.36      www      1851:     my @assessdata=();
1.78      matthew  1852:     foreach (keys(%f)) {
1.17      www      1853: 	if ($_=~/^A(\d+)/) {
                   1854: 	   my $row=$1;
1.42      www      1855:            unless (($f{$_}=~/^\!/) || ($row==0)) {
1.55      www      1856: 	      my ($usy,$ufn)=split(/\_\_\&\&\&\_\_/,$f{$_});
1.36      www      1857: 	      @assessdata=&exportsheet(&getuname($safeeval),
                   1858:                                        &getudom($safeeval),
1.55      www      1859:                                        'assesscalc',$usy,$ufn);
1.17      www      1860:               my $index=0;
1.78      matthew  1861:               foreach ('A','B','C','D','E','F','G','H','I','J','K','L','M',
                   1862: 	               'N','O','P','Q','R','S','T','U','V','W','X','Y','Z') {
1.30      www      1863:                   if ($assessdata[$index]) {
1.41      www      1864: 		     my $col=$_;
                   1865: 		     if ($assessdata[$index]=~/\D/) {
                   1866:                          $c{$col.$row}="'".$assessdata[$index]."'";
                   1867:  		     } else {
                   1868: 		         $c{$col.$row}=$assessdata[$index];
                   1869: 		     }
                   1870:                      unless ($col eq 'A') { 
                   1871: 			 $f{$col.$row}='import';
1.30      www      1872:                      }
                   1873: 		  }
                   1874:                   $index++;
1.78      matthew  1875:               }
1.16      www      1876: 	   }
                   1877:         }
1.78      matthew  1878:     }
1.39      www      1879:     $cachedassess='';
                   1880:     undef %cachedstores;
1.18      www      1881:     &setformulas($safeeval,%f);
1.16      www      1882:     &setconstants($safeeval,%c);
                   1883: }
                   1884: 
1.24      www      1885: # --------------------------------------------------- Load data for one student
                   1886: 
1.30      www      1887: sub loadcourse {
1.37      www      1888:     my ($safeeval,$r)=@_;
1.24      www      1889:     my %c=();
                   1890:     my %f=&getformulas($safeeval);
1.37      www      1891:     my $total=0;
1.78      matthew  1892:     foreach (keys(%f)) {
1.37      www      1893: 	if ($_=~/^A(\d+)/) {
                   1894: 	    unless ($f{$_}=~/^\!/) { $total++; }
                   1895:         }
1.78      matthew  1896:     }
1.37      www      1897:     my $now=0;
                   1898:     my $since=time;
1.39      www      1899:     $r->print(<<ENDPOP);
                   1900: <script>
                   1901:     popwin=open('','popwin','width=400,height=100');
                   1902:     popwin.document.writeln('<html><body bgcolor="#FFFFFF">'+
1.50      www      1903:       '<h3>Spreadsheet Calculation Progress</h3>'+
1.39      www      1904:       '<form name=popremain>'+
                   1905:       '<input type=text size=35 name=remaining value=Starting></form>'+
                   1906:       '</body></html>');
1.42      www      1907:     popwin.document.close();
1.39      www      1908: </script>
                   1909: ENDPOP
1.37      www      1910:     $r->rflush();
1.78      matthew  1911:     foreach (keys(%f)) {
1.24      www      1912: 	if ($_=~/^A(\d+)/) {
                   1913: 	   my $row=$1;
1.42      www      1914:            unless (($f{$_}=~/^\!/)  || ($row==0)) {
1.37      www      1915: 	      my @studentdata=&exportsheet(split(/\:/,$f{$_}),
1.30      www      1916:                                            'studentcalc');
1.37      www      1917:               undef %userrdatas;
                   1918:               $now++;
1.39      www      1919:               $r->print('<script>popwin.document.popremain.remaining.value="'.
1.37      www      1920:                   $now.'/'.$total.': '.int((time-$since)/$now*($total-$now)).
1.39      www      1921:                         ' secs remaining";</script>');
1.37      www      1922:               $r->rflush(); 
                   1923: 
1.24      www      1924:               my $index=0;
1.78      matthew  1925:              foreach ('A','B','C','D','E','F','G','H','I','J','K','L','M',
                   1926: 	              'N','O','P','Q','R','S','T','U','V','W','X','Y','Z') {
1.30      www      1927:                   if ($studentdata[$index]) {
1.41      www      1928: 		     my $col=$_;
                   1929: 		     if ($studentdata[$index]=~/\D/) {
                   1930:                          $c{$col.$row}="'".$studentdata[$index]."'";
                   1931:  		     } else {
                   1932: 		         $c{$col.$row}=$studentdata[$index];
                   1933: 		     }
                   1934:                      unless ($col eq 'A') { 
                   1935: 			 $f{$col.$row}='import';
1.30      www      1936:                      }
                   1937: 		  }
                   1938:                   $index++;
1.78      matthew  1939:               }
1.24      www      1940: 	   }
                   1941:         }
1.78      matthew  1942:     }
1.24      www      1943:     &setformulas($safeeval,%f);
                   1944:     &setconstants($safeeval,%c);
1.43      www      1945:     $r->print('<script>popwin.close()</script>');
1.37      www      1946:     $r->rflush(); 
1.24      www      1947: }
                   1948: 
1.6       www      1949: # ------------------------------------------------ Load data for one assessment
                   1950: 
1.29      www      1951: sub loadassessment {
                   1952:     my $safeeval=shift;
                   1953: 
                   1954:     my $uhome=&getuhome($safeeval);
                   1955:     my $uname=&getuname($safeeval);
                   1956:     my $udom=&getudom($safeeval);
                   1957:     my $symb=&getusymb($safeeval);
                   1958:     my $cid=&getcid($safeeval);
                   1959:     my $cnum=&getcnum($safeeval);
                   1960:     my $cdom=&getcdom($safeeval);
                   1961:     my $chome=&getchome($safeeval);
                   1962: 
1.6       www      1963:     my $namespace;
1.29      www      1964:     unless ($namespace=$cid) { return ''; }
1.11      www      1965: 
                   1966: # ----------------------------------------------------------- Get stored values
1.39      www      1967: 
                   1968:    my %returnhash=();
                   1969: 
                   1970:    if ($cachedassess eq $uname.':'.$udom) {
                   1971: #
                   1972: # get data out of the dumped stores
                   1973: # 
                   1974: 
                   1975:        my $version=$cachedstores{'version:'.$symb};
                   1976:        my $scope;
                   1977:        for ($scope=1;$scope<=$version;$scope++) {
1.78      matthew  1978:            foreach (split(/\:/,$cachedstores{$scope.':keys:'.$symb})) {
1.39      www      1979:                $returnhash{$_}=$cachedstores{$scope.':'.$symb.':'.$_};
1.78      matthew  1980:            } 
1.39      www      1981:        }
                   1982: 
                   1983:    } else {
                   1984: #
                   1985: # restore individual
                   1986: #
                   1987: 
1.11      www      1988:     my $answer=&Apache::lonnet::reply(
1.15      www      1989:        "restore:$udom:$uname:".
                   1990:        &Apache::lonnet::escape($namespace).":".
                   1991:        &Apache::lonnet::escape($symb),$uhome);
1.78      matthew  1992:     foreach (split(/\&/,$answer)) {
1.6       www      1993: 	my ($name,$value)=split(/\=/,$_);
1.11      www      1994:         $returnhash{&Apache::lonnet::unescape($name)}=
                   1995:                     &Apache::lonnet::unescape($value);
1.78      matthew  1996:     }
1.6       www      1997:     my $version;
                   1998:     for ($version=1;$version<=$returnhash{'version'};$version++) {
1.78      matthew  1999:        foreach (split(/\:/,$returnhash{$version.':keys'})) {
1.6       www      2000:           $returnhash{$_}=$returnhash{$version.':'.$_};
1.78      matthew  2001:        } 
1.6       www      2002:     }
1.39      www      2003:    }
1.11      www      2004: # ----------------------------- returnhash now has all stores for this resource
1.76      www      2005: 
                   2006: # --------- convert all "_" to "." to be able to use libraries, multiparts, etc
                   2007: 
                   2008:     my @oldkeys=keys %returnhash;
                   2009: 
1.78      matthew  2010:     foreach (@oldkeys) {
1.76      www      2011:         my $name=$_;
                   2012:         my $value=$returnhash{$_};
                   2013:         delete $returnhash{$_};
                   2014:         $name=~s/\_/\./g;
                   2015:         $returnhash{$name}=$value;
1.78      matthew  2016:     }
1.11      www      2017: 
                   2018: # ---------------------------- initialize coursedata and userdata for this user
1.31      www      2019:     undef %courseopt;
                   2020:     undef %useropt;
1.29      www      2021: 
                   2022:     my $userprefix=$uname.'_'.$udom.'_';
                   2023: 
1.11      www      2024:     unless ($uhome eq 'no_host') { 
                   2025: # -------------------------------------------------------------- Get coursedata
1.13      www      2026:       unless
1.32      www      2027:         ((time-$courserdatas{$cid.'.last_cache'})<240) {
1.29      www      2028:          my $reply=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.
                   2029:               ':resourcedata',$chome);
1.11      www      2030:          if ($reply!~/^error\:/) {
1.29      www      2031:             $courserdatas{$cid}=$reply;
                   2032:             $courserdatas{$cid.'.last_cache'}=time;
1.11      www      2033:          }
                   2034:       }
1.78      matthew  2035:       foreach (split(/\&/,$courserdatas{$cid})) {
1.11      www      2036:          my ($name,$value)=split(/\=/,$_);
1.29      www      2037:          $courseopt{$userprefix.&Apache::lonnet::unescape($name)}=
1.11      www      2038:                     &Apache::lonnet::unescape($value);  
1.78      matthew  2039:       }
1.11      www      2040: # --------------------------------------------------- Get userdata (if present)
1.13      www      2041:       unless
1.32      www      2042:         ((time-$userrdatas{$uname.'___'.$udom.'.last_cache'})<240) {
1.11      www      2043:          my $reply=
                   2044:        &Apache::lonnet::reply('dump:'.$udom.':'.$uname.':resourcedata',$uhome);
                   2045:          if ($reply!~/^error\:/) {
                   2046: 	     $userrdatas{$uname.'___'.$udom}=$reply;
1.13      www      2047: 	     $userrdatas{$uname.'___'.$udom.'.last_cache'}=time;
1.11      www      2048:          }
                   2049:       }
1.78      matthew  2050:       foreach (split(/\&/,$userrdatas{$uname.'___'.$udom})) {
1.11      www      2051:          my ($name,$value)=split(/\=/,$_);
1.29      www      2052:          $useropt{$userprefix.&Apache::lonnet::unescape($name)}=
1.15      www      2053: 	          &Apache::lonnet::unescape($value);
1.78      matthew  2054:       }
1.29      www      2055:     }
                   2056: # ----------------- now courseopt, useropt initialized for this user and course
                   2057: # (used by parmval)
                   2058: 
1.60      www      2059: #
                   2060: # Load keys for this assessment only
                   2061: #
                   2062:     my %thisassess=();
                   2063:     my ($symap,$syid,$srcf)=split(/\_\_\_/,$symb);
                   2064:     
1.78      matthew  2065:     foreach (split(/\,/,&Apache::lonnet::metadata($srcf,'keys'))) {
1.60      www      2066:         $thisassess{$_}=1;
1.78      matthew  2067:     } 
1.60      www      2068: #
                   2069: # Load parameters
                   2070: #
1.29      www      2071:    my %c=();
1.6       www      2072: 
1.29      www      2073:    if (tie(%parmhash,'GDBM_File',
                   2074:            &getcfn($safeeval).'_parms.db',&GDBM_READER,0640)) {
1.6       www      2075:     my %f=&getformulas($safeeval);
1.78      matthew  2076:     foreach (keys(%f))  {
1.6       www      2077: 	if ($_=~/^A/) {
                   2078:             unless ($f{$_}=~/^\!/) {
1.11      www      2079:   	       if ($f{$_}=~/^parameter/) {
1.60      www      2080: 		if ($thisassess{$f{$_}}) {
1.40      www      2081:                   my $val=&parmval($f{$_},$safeeval);
                   2082:                   $c{$_}=$val;
                   2083:                   $c{$f{$_}}=$val;
1.60      www      2084: 	        }
1.11      www      2085: 	       } else {
1.15      www      2086: 		  my $key=$f{$_};
1.40      www      2087:                   my $ckey=$key;
1.15      www      2088:                   $key=~s/^stores\_/resource\./;
1.70      www      2089:                   $key=~s/\_/\./g;
1.15      www      2090:  	          $c{$_}=$returnhash{$key};
1.40      www      2091:                   $c{$ckey}=$returnhash{$key};
1.11      www      2092: 	       }
                   2093: 	   }
1.6       www      2094:         }
1.78      matthew  2095:     }
1.29      www      2096:     untie(%parmhash);
                   2097:    }
                   2098:    &setconstants($safeeval,%c);
1.6       www      2099: }
                   2100: 
1.10      www      2101: # --------------------------------------------------------- Various form fields
                   2102: 
                   2103: sub textfield {
                   2104:     my ($title,$name,$value)=@_;
                   2105:     return "\n<p><b>$title:</b><br>".
                   2106:            '<input type=text name="'.$name.'" size=80 value="'.$value.'">';
                   2107: }
                   2108: 
                   2109: sub hiddenfield {
                   2110:     my ($name,$value)=@_;
                   2111:     return "\n".'<input type=hidden name="'.$name.'" value="'.$value.'">';
                   2112: }
                   2113: 
                   2114: sub selectbox {
                   2115:     my ($title,$name,$value,%options)=@_;
                   2116:     my $selout="\n<p><b>$title:</b><br>".'<select name="'.$name.'">';
1.78      matthew  2117:     foreach (sort keys(%options)) {
1.10      www      2118:         $selout.='<option value="'.$_.'"';
                   2119:         if ($_ eq $value) { $selout.=' selected'; }
                   2120:         $selout.='>'.$options{$_}.'</option>';
1.78      matthew  2121:     }
1.10      www      2122:     return $selout.'</select>';
                   2123: }
                   2124: 
1.28      www      2125: # =============================================== Update information in a sheet
                   2126: #
                   2127: # Add new users or assessments, etc.
                   2128: #
                   2129: 
                   2130: sub updatesheet {
                   2131:     my $safeeval=shift;
                   2132:     my $stype=&gettype($safeeval);
                   2133:     if ($stype eq 'classcalc') {
                   2134: 	return &updateclasssheet($safeeval);
                   2135:     } else {
                   2136:         return &updatestudentassesssheet($safeeval);
                   2137:     }
                   2138: }
                   2139: 
                   2140: # =================================================== Load the rows for a sheet
                   2141: #
                   2142: # Import the data for rows
                   2143: #
                   2144: 
1.37      www      2145: sub loadrows {
                   2146:     my ($safeeval,$r)=@_;
1.28      www      2147:     my $stype=&gettype($safeeval);
                   2148:     if ($stype eq 'classcalc') {
1.37      www      2149: 	&loadcourse($safeeval,$r);
1.28      www      2150:     } elsif ($stype eq 'studentcalc') {
1.29      www      2151:         &loadstudent($safeeval);
1.28      www      2152:     } else {
1.29      www      2153:         &loadassessment($safeeval);
1.28      www      2154:     }
                   2155: }
                   2156: 
1.47      www      2157: # ======================================================= Forced recalculation?
                   2158: 
                   2159: sub checkthis {
                   2160:     my ($keyname,$time)=@_;
                   2161:     return ($time<$expiredates{$keyname});
                   2162: }
                   2163: sub forcedrecalc {
                   2164:     my ($uname,$udom,$stype,$usymb)=@_;
                   2165:     my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
                   2166:     my $time=$oldsheets{$key.'.time'};
1.53      www      2167:     if ($ENV{'form.forcerecalc'}) { return 1; }
1.47      www      2168:     unless ($time) { return 1; }
                   2169:     if ($stype eq 'assesscalc') {
                   2170:         my $map=(split(/\_\_\_/,$usymb))[0];
                   2171:         if (&checkthis('::assesscalc:',$time) ||
                   2172:             &checkthis('::assesscalc:'.$map,$time) ||
                   2173:             &checkthis('::assesscalc:'.$usymb,$time) ||
1.49      www      2174:             &checkthis($uname.':'.$udom.':assesscalc:',$time) ||
                   2175:             &checkthis($uname.':'.$udom.':assesscalc:'.$map,$time) ||
                   2176:             &checkthis($uname.':'.$udom.':assesscalc:'.$usymb,$time)) {
1.47      www      2177:             return 1;
                   2178:         } 
                   2179:     } else {
                   2180:         if (&checkthis('::studentcalc:',$time) || 
1.51      www      2181:             &checkthis($uname.':'.$udom.':studentcalc:',$time)) {
1.47      www      2182: 	    return 1;
                   2183:         }
                   2184:     }
                   2185:     return 0; 
                   2186: }
                   2187: 
1.28      www      2188: # ============================================================== Export handler
                   2189: #
                   2190: # Non-interactive call from with program
                   2191: #
                   2192: 
                   2193: sub exportsheet {
1.44      www      2194:  my ($uname,$udom,$stype,$usymb,$fn)=@_;
                   2195:  my @exportarr=();
1.74      www      2196: 
                   2197:  if (($usymb=~/^\_(\w+)/) && (!$fn)) {
                   2198:     $fn='default_'.$1;
                   2199:  }
                   2200: 
1.44      www      2201: #
                   2202: # Check if cached
                   2203: #
1.46      www      2204: 
1.44      www      2205:  my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
                   2206:  my $found='';
                   2207: 
                   2208:  if ($oldsheets{$key}) {
1.78      matthew  2209:      foreach (split(/\_\_\_\&\_\_\_/,$oldsheets{$key})) {
1.44      www      2210:          my ($name,$value)=split(/\_\_\_\=\_\_\_/,$_);
                   2211:          if ($name eq $fn) {
                   2212: 	     $found=$value;
                   2213:          }
1.78      matthew  2214:      }
1.44      www      2215:  }
                   2216: 
1.46      www      2217:  unless ($found) {
                   2218:      &cachedssheets($uname,$udom,&Apache::lonnet::homeserver($uname,$udom));
                   2219:      if ($oldsheets{$key}) {
1.78      matthew  2220: 	foreach (split(/\_\_\_\&\_\_\_/,$oldsheets{$key})) {
1.46      www      2221:             my ($name,$value)=split(/\_\_\_\=\_\_\_/,$_);
                   2222:             if ($name eq $fn) {
                   2223: 	        $found=$value;
                   2224:             }
1.78      matthew  2225:         } 
1.46      www      2226:      }
                   2227:  }
1.47      www      2228: #
                   2229: # Check if still valid
                   2230: #
                   2231:  if ($found) {
                   2232:      if (&forcedrecalc($uname,$udom,$stype,$usymb)) {
                   2233: 	 $found='';
                   2234:      }
                   2235:  }
                   2236:  
1.44      www      2237:  if ($found) {
                   2238: #
                   2239: # Return what was cached
                   2240: #
                   2241:      @exportarr=split(/\_\_\_\;\_\_\_/,$found);
                   2242: 
                   2243:  } else {
                   2244: #
                   2245: # Not cached
1.46      www      2246: #        
                   2247: 
1.29      www      2248:     my $thissheet=&makenewsheet($uname,$udom,$stype,$usymb);
1.28      www      2249:     &readsheet($thissheet,$fn);
                   2250:     &updatesheet($thissheet);
                   2251:     &loadrows($thissheet);
1.44      www      2252:     &calcsheet($thissheet); 
                   2253:     @exportarr=&exportdata($thissheet);
                   2254: #
                   2255: # Store now
                   2256: #
                   2257:     my $cid=$ENV{'request.course.id'}; 
1.47      www      2258:     my $current='';
1.46      www      2259:     if ($stype eq 'studentcalc') {
                   2260:        $current=&Apache::lonnet::reply('get:'.
1.44      www      2261:                                      $ENV{'course.'.$cid.'.domain'}.':'.
                   2262:                                      $ENV{'course.'.$cid.'.num'}.
                   2263: 				     ':nohist_calculatedsheets:'.
                   2264:                                      &Apache::lonnet::escape($key),
                   2265:                                      $ENV{'course.'.$cid.'.home'});
1.46      www      2266:     } else {
                   2267:        $current=&Apache::lonnet::reply('get:'.
                   2268:                                      &getudom($thissheet).':'.
                   2269:                                      &getuname($thissheet).
                   2270: 				     ':nohist_calculatedsheets_'.
                   2271:                                      $ENV{'request.course.id'}.':'.
                   2272:                                      &Apache::lonnet::escape($key),
                   2273:                                      &getuhome($thissheet));
                   2274: 
                   2275:     }
1.44      www      2276:     my %currentlystored=();
                   2277:     unless ($current=~/^error\:/) {
1.78      matthew  2278:        foreach (split(/\_\_\_\&\_\_\_/,&Apache::lonnet::unescape($current))) {
1.44      www      2279:            my ($name,$value)=split(/\_\_\_\=\_\_\_/,$_);
                   2280:            $currentlystored{$name}=$value;
1.78      matthew  2281:        }
1.44      www      2282:     }
                   2283:     $currentlystored{$fn}=join('___;___',@exportarr);
                   2284: 
                   2285:     my $newstore='';
1.78      matthew  2286:     foreach (keys(%currentlystored)) {
1.44      www      2287:         if ($newstore) { $newstore.='___&___'; }
                   2288:         $newstore.=$_.'___=___'.$currentlystored{$_};
1.78      matthew  2289:     }
1.47      www      2290:     my $now=time;
1.46      www      2291:     if ($stype eq 'studentcalc') {
                   2292:        &Apache::lonnet::reply('put:'.
1.44      www      2293:                          $ENV{'course.'.$cid.'.domain'}.':'.
                   2294:                          $ENV{'course.'.$cid.'.num'}.
                   2295: 			 ':nohist_calculatedsheets:'.
                   2296:                          &Apache::lonnet::escape($key).'='.
1.47      www      2297: 			 &Apache::lonnet::escape($newstore).'&'.
                   2298:                          &Apache::lonnet::escape($key).'.time='.$now,
1.44      www      2299:                          $ENV{'course.'.$cid.'.home'});
1.46      www      2300:    } else {
                   2301:        &Apache::lonnet::reply('put:'.
                   2302:                          &getudom($thissheet).':'.
                   2303:                          &getuname($thissheet).
                   2304: 			 ':nohist_calculatedsheets_'.
                   2305:                          $ENV{'request.course.id'}.':'.
                   2306:                          &Apache::lonnet::escape($key).'='.
1.47      www      2307: 			 &Apache::lonnet::escape($newstore).'&'.
                   2308:                          &Apache::lonnet::escape($key).'.time='.$now,
1.46      www      2309:                          &getuhome($thissheet));
                   2310:    }
1.44      www      2311:  }
                   2312:  return @exportarr;
                   2313: }
1.48      www      2314: # ============================================================ Expiration Dates
                   2315: #
                   2316: # Load previously cached student spreadsheets for this course
                   2317: #
                   2318: 
                   2319: sub expirationdates {
                   2320:     undef %expiredates;
                   2321:     my $cid=$ENV{'request.course.id'};
                   2322:     my $reply=&Apache::lonnet::reply('dump:'.
                   2323: 				     $ENV{'course.'.$cid.'.domain'}.':'.
                   2324:                                      $ENV{'course.'.$cid.'.num'}.
                   2325: 				     ':nohist_expirationdates',
                   2326:                                      $ENV{'course.'.$cid.'.home'});
                   2327:     unless ($reply=~/^error\:/) {
1.78      matthew  2328: 	foreach (split(/\&/,$reply)) {
1.48      www      2329:             my ($name,$value)=split(/\=/,$_);
                   2330:             $expiredates{&Apache::lonnet::unescape($name)}
                   2331:                         =&Apache::lonnet::unescape($value);
1.78      matthew  2332:         }
1.48      www      2333:     }
                   2334: }
1.44      www      2335: 
                   2336: # ===================================================== Calculated sheets cache
                   2337: #
1.46      www      2338: # Load previously cached student spreadsheets for this course
1.44      www      2339: #
                   2340: 
1.46      www      2341: sub cachedcsheets {
1.44      www      2342:     my $cid=$ENV{'request.course.id'};
                   2343:     my $reply=&Apache::lonnet::reply('dump:'.
                   2344: 				     $ENV{'course.'.$cid.'.domain'}.':'.
                   2345:                                      $ENV{'course.'.$cid.'.num'}.
                   2346: 				     ':nohist_calculatedsheets',
                   2347:                                      $ENV{'course.'.$cid.'.home'});
                   2348:     unless ($reply=~/^error\:/) {
1.78      matthew  2349: 	foreach ( split(/\&/,$reply)) {
1.44      www      2350:             my ($name,$value)=split(/\=/,$_);
                   2351:             $oldsheets{&Apache::lonnet::unescape($name)}
                   2352:                       =&Apache::lonnet::unescape($value);
1.78      matthew  2353:         }
1.44      www      2354:     }
1.28      www      2355: }
                   2356: 
1.46      www      2357: # ===================================================== Calculated sheets cache
                   2358: #
                   2359: # Load previously cached assessment spreadsheets for this student
                   2360: #
                   2361: 
                   2362: sub cachedssheets {
                   2363:   my ($sname,$sdom,$shome)=@_;
                   2364:   unless (($loadedcaches{$sname.'_'.$sdom}) || ($shome eq 'no_host')) {
                   2365:     my $cid=$ENV{'request.course.id'};
                   2366:     my $reply=&Apache::lonnet::reply('dump:'.$sdom.':'.$sname.
                   2367: 			             ':nohist_calculatedsheets_'.
                   2368:                                       $ENV{'request.course.id'},
                   2369:                                      $shome);
                   2370:     unless ($reply=~/^error\:/) {
1.78      matthew  2371: 	foreach ( split(/\&/,$reply)) {
1.46      www      2372:             my ($name,$value)=split(/\=/,$_);
                   2373:             $oldsheets{&Apache::lonnet::unescape($name)}
                   2374:                       =&Apache::lonnet::unescape($value);
1.78      matthew  2375:         }
1.46      www      2376:     }
                   2377:     $loadedcaches{$sname.'_'.$sdom}=1;
                   2378:   }
                   2379: }
                   2380: 
                   2381: # ===================================================== Calculated sheets cache
                   2382: #
                   2383: # Load previously cached assessment spreadsheets for this student
                   2384: #
                   2385: 
1.12      www      2386: # ================================================================ Main handler
1.28      www      2387: #
                   2388: # Interactive call to screen
                   2389: #
                   2390: #
                   2391: 
1.3       www      2392: 
                   2393: sub handler {
1.7       www      2394:     my $r=shift;
                   2395: 
1.28      www      2396:     if ($r->header_only) {
1.7       www      2397:       $r->content_type('text/html');
                   2398:       $r->send_http_header;
                   2399:       return OK;
1.28      www      2400:     }
                   2401: 
                   2402: # ---------------------------------------------------- Global directory configs
                   2403: 
1.29      www      2404: $includedir=$r->dir_config('lonIncludes');
1.28      www      2405: $tmpdir=$r->dir_config('lonDaemons').'/tmp/';
1.3       www      2406: 
1.7       www      2407: # ----------------------------------------------------- Needs to be in a course
1.3       www      2408: 
1.29      www      2409:   if ($ENV{'request.course.fn'}) { 
1.10      www      2410: 
                   2411: # --------------------------- Get query string for limited number of parameters
1.17      www      2412: 
1.89      matthew  2413:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   2414:                                             ['uname','udom','usymb','ufn']);
1.72      www      2415: 
                   2416:     if (($ENV{'form.usymb'}=~/^\_(\w+)/) && (!$ENV{'form.ufn'})) {
                   2417: 	$ENV{'form.ufn'}='default_'.$1;
                   2418:     }
1.10      www      2419: 
1.55      www      2420: # -------------------------------------- Interactive loading of specific sheet?
                   2421:     if (($ENV{'form.load'}) && ($ENV{'form.loadthissheet'} ne 'Default')) {
                   2422: 	$ENV{'form.ufn'}=$ENV{'form.loadthissheet'};
                   2423:     }
1.10      www      2424: # ------------------------------------------- Nothing there? Must be login user
1.29      www      2425: 
                   2426:     my $aname;
                   2427:     my $adom;
                   2428: 
1.10      www      2429:     unless ($ENV{'form.uname'}) {
1.29      www      2430: 	$aname=$ENV{'user.name'};
                   2431:         $adom=$ENV{'user.domain'};
1.11      www      2432:     } else {
1.29      www      2433:         $aname=$ENV{'form.uname'};
                   2434:         $adom=$ENV{'form.udom'};
1.10      www      2435:     }
1.14      www      2436: 
1.10      www      2437: # ------------------------------------------------------------------- Open page
                   2438: 
1.7       www      2439:     $r->content_type('text/html');
1.11      www      2440:     $r->header_out('Cache-control','no-cache');
                   2441:     $r->header_out('Pragma','no-cache');
1.7       www      2442:     $r->send_http_header;
1.3       www      2443: 
1.14      www      2444: # --------------------------------------------------------------- Screen output
                   2445: 
1.10      www      2446:     $r->print('<html><head><title>LON-CAPA Spreadsheet</title>');
                   2447:     $r->print(<<ENDSCRIPT);
                   2448: <script language="JavaScript">
                   2449: 
                   2450:     function celledit(cn,cf) {
                   2451:         var cnf=prompt(cn,cf);
1.86      matthew  2452:         if (cnf!=null) {
                   2453:             document.sheet.unewfield.value=cn;
1.10      www      2454:             document.sheet.unewformula.value=cnf;
                   2455:             document.sheet.submit();
                   2456:         }
                   2457:     }
                   2458: 
1.55      www      2459:     function changesheet(cn) {
                   2460: 	document.sheet.unewfield.value=cn;
                   2461:         document.sheet.unewformula.value='changesheet';
                   2462:         document.sheet.submit();
                   2463:     }
                   2464: 
1.92    ! www      2465:     function insertrow(cn) {
        !          2466: 	document.sheet.unewfield.value='insertrow';
        !          2467:         document.sheet.unewformula.value=cn;
        !          2468:         document.sheet.submit();
        !          2469:     }
        !          2470: 
1.10      www      2471: </script>
                   2472: ENDSCRIPT
                   2473:     $r->print('</head><body bgcolor="#FFFFFF">'.
1.21      www      2474:        '<img align=right src=/adm/lonIcons/lonlogos.gif>'.
                   2475:        '<h1>LON-CAPA Spreadsheet</h1>'.
1.10      www      2476:        '<form action="'.$r->uri.'" name=sheet method=post>'.
                   2477:        &hiddenfield('uname',$ENV{'form.uname'}).
                   2478:        &hiddenfield('udom',$ENV{'form.udom'}).
                   2479:        &hiddenfield('usymb',$ENV{'form.usymb'}).
                   2480:        &hiddenfield('unewfield','').
                   2481:        &hiddenfield('unewformula',''));
1.29      www      2482: 
                   2483: # ---------------------- Make sure that this gets out, even if user hits "stop"
                   2484: 
1.24      www      2485:     $r->rflush();
1.29      www      2486: 
1.54      www      2487: # ---------------------------------------------------------------- Full recalc?
                   2488: 
                   2489: 
                   2490:     if ($ENV{'form.forcerecalc'}) {
                   2491: 	$r->print('<h4>Completely Recalculating Sheet ...</h4>');
                   2492:         undef %spreadsheets;
                   2493:         undef %courserdatas;
                   2494:         undef %userrdatas;
                   2495:         undef %defaultsheets;
                   2496:         undef %updatedata;
                   2497:    }
                   2498:  
1.14      www      2499: # ---------------------------------------- Read new sheet or modified worksheet
                   2500: 
1.19      www      2501:     $r->uri=~/\/(\w+)$/;
1.14      www      2502: 
1.29      www      2503:     my $asheet=&makenewsheet($aname,$adom,$1,$ENV{'form.usymb'});
                   2504: 
1.30      www      2505: # ------------------------ If a new formula had been entered, go from work copy
                   2506: 
                   2507:     if ($ENV{'form.unewfield'}) {
                   2508:         $r->print('<h2>Modified Workcopy</h2>');
                   2509:         $ENV{'form.unewformula'}=~s/\'/\"/g;
                   2510:         $r->print('<p>New formula: '.$ENV{'form.unewfield'}.'='.
                   2511:                   $ENV{'form.unewformula'}.'<p>');
                   2512:         &setfilename($asheet,$ENV{'form.ufn'});
                   2513: 	&tmpread($asheet,
                   2514:                  $ENV{'form.unewfield'},$ENV{'form.unewformula'});
                   2515: 
                   2516:      } elsif ($ENV{'form.saveas'}) {
                   2517:         &setfilename($asheet,$ENV{'form.ufn'});
                   2518: 	&tmpread($asheet);
                   2519:     } else {
                   2520:         &readsheet($asheet,$ENV{'form.ufn'});
                   2521:     }
                   2522: 
                   2523: # -------------------------------------------------- Print out user information
                   2524: 
                   2525:     unless (&gettype($asheet) eq 'classcalc') {
                   2526:         $r->print('<p><b>User:</b> '.&getuname($asheet).
                   2527:                   '<br><b>Domain:</b> '.&getudom($asheet));
                   2528:         if (&getcsec($asheet) eq '-1') {
                   2529:            $r->print('<h3><font color=red>'.
                   2530:                      'Not a student in this course</font></h3>');
                   2531:         } else {
                   2532:            $r->print('<br><b>Section/Group:</b> '.&getcsec($asheet));
                   2533:         }
1.77      www      2534:         if ($ENV{'form.usymb'}) {
                   2535:            $r->print('<br><b>Assessment:</b> <tt>'.$ENV{'form.usymb'}.'</tt>');
                   2536:         }
1.30      www      2537:     }
                   2538: 
                   2539: # ---------------------------------------------------------------- Course title
                   2540: 
                   2541:     $r->print('<h1>'.
1.61      www      2542:             $ENV{'course.'.$ENV{'request.course.id'}.'.description'}.
                   2543:              '</h1><h3>'.localtime().'</h3>');
1.30      www      2544: 
1.53      www      2545: # ---------------------------------------------------- See if user can see this
                   2546: 
                   2547:     if ((&gettype($asheet) eq 'classcalc') || 
                   2548:         (&getuname($asheet) ne $ENV{'user.name'}) ||
                   2549:         (&getudom($asheet) ne $ENV{'user.domain'})) {
                   2550:         unless (&Apache::lonnet::allowed('vgr',&getcid($asheet))) {
                   2551: 	    $r->print(
                   2552:            '<h1>Access Permission Denied</h1></form></body></html>');
                   2553:             return OK;
                   2554:         }
                   2555:     }
1.30      www      2556: 
1.53      www      2557: # ---------------------------------------------------------- Additional options
                   2558: 
                   2559:     $r->print(
                   2560:  '<input type=submit name=forcerecalc value="Completely Recalculate Sheet"><p>'
                   2561: 		 );
                   2562:     if (&gettype($asheet) eq 'assesscalc') {
                   2563:        $r->print ('<p><font size=+2><a href="/adm/studentcalc?uname='.
                   2564:                                                &getuname($asheet).
                   2565:                                                '&udom='.&getudom($asheet).
                   2566:                   '">Level up: Student Sheet</a></font><p>');
                   2567:     }
                   2568:     
                   2569:     if ((&gettype($asheet) eq 'studentcalc') && 
                   2570:         (&Apache::lonnet::allowed('vgr',&getcid($asheet)))) {
                   2571:        $r->print (
                   2572:                    '<p><font size=+2><a href="/adm/classcalc">'.
                   2573:                    'Level up: Course Sheet</a></font><p>');
                   2574:     }
                   2575:     
1.30      www      2576: 
                   2577: # ----------------------------------------------------------------- Save dialog
                   2578: 
                   2579: 
                   2580:     if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
                   2581:         my $fname=$ENV{'form.ufn'};
                   2582:         $fname=~s/\_[^\_]+$//;
                   2583:         if ($fname eq 'default') { $fname='course_default'; }
                   2584:         $r->print('<input type=submit name=saveas value="Save as ...">'.
                   2585:               '<input type=text size=20 name=newfn value="'.$fname.
                   2586:               '"> (make default: <input type=checkbox name="makedefufn">)<p>');
                   2587:     }
                   2588: 
                   2589:     $r->print(&hiddenfield('ufn',&getfilename($asheet)));
1.55      www      2590: 
                   2591: # ----------------------------------------------------------------- Load dialog
                   2592:     if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
                   2593: 	$r->print('<p><input type=submit name=load value="Load ...">'.
                   2594:                   '<select name="loadthissheet">'.
                   2595:                   '<option name="default">Default</option>');
1.78      matthew  2596:         foreach (&othersheets($asheet,&gettype($asheet))) {
1.55      www      2597: 	    $r->print('<option name="'.$_.'"');
                   2598:             if ($ENV{'form.ufn'} eq $_) {
                   2599:                $r->print(' selected');
                   2600:             }
                   2601:             $r->print('>'.$_.'</option>');
1.78      matthew  2602:         } 
1.55      www      2603:         $r->print('</select><p>');
                   2604:         if (&gettype($asheet) eq 'studentcalc') {
                   2605: 	    &setothersheets($asheet,&othersheets($asheet,'assesscalc'));
                   2606:         }
                   2607:     }
1.30      www      2608: 
1.46      www      2609: # --------------------------------------------------------------- Cached sheets
1.48      www      2610: 
                   2611:     &expirationdates();
1.46      www      2612: 
                   2613:     undef %oldsheets;
                   2614:     undef %loadedcaches;
1.44      www      2615: 
1.46      www      2616:     if (&gettype($asheet) eq 'classcalc') {
                   2617:         $r->print("Loading previously calculated student sheets ...<br>\n");
                   2618:         $r->rflush();
                   2619:         &cachedcsheets();
                   2620:     } elsif (&gettype($asheet) eq 'studentcalc') {
                   2621:         $r->print("Loading previously calculated assessment sheets ...<br>\n");
                   2622:         $r->rflush();
                   2623:         &cachedssheets(&getuname($asheet),&getudom($asheet),
                   2624:                        &getuhome($asheet));
                   2625:     }
1.30      www      2626: 
                   2627: # ----------------------------------------------------- Update sheet, load rows
1.14      www      2628: 
1.44      www      2629:     $r->print("Loaded sheet(s), updating rows ...<br>\n");
1.36      www      2630:     $r->rflush();
                   2631: 
1.29      www      2632:     &updatesheet($asheet);
1.36      www      2633: 
                   2634:     $r->print("Updated rows, loading row data ...<br>\n");
                   2635:     $r->rflush();
                   2636: 
1.37      www      2637:     &loadrows($asheet,$r);
1.14      www      2638: 
1.36      www      2639:     $r->print("Loaded row data, calculating sheet ...<br>\n");
                   2640:     $r->rflush();
1.14      www      2641: 
1.29      www      2642:     my $calcoutput=&calcsheet($asheet);
                   2643:     $r->print('<h3><font color=red>'.$calcoutput.'</h3></font>');
1.56      www      2644: 
                   2645: # ---------------------------------------------------- See if something to save
                   2646: 
                   2647:     if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
                   2648:         my $fname='';
                   2649: 	if ($ENV{'form.saveas'} && ($fname=$ENV{'form.newfn'})) {
                   2650:             $fname=~s/\W/\_/g;
                   2651:             if ($fname eq 'default') { $fname='course_default'; }
                   2652:             $fname.='_'.&gettype($asheet);
                   2653:             &setfilename($asheet,$fname);
                   2654:             $ENV{'form.ufn'}=$fname;
                   2655: 	    $r->print('<p>Saving spreadsheet: '.
                   2656:                          &writesheet($asheet,$ENV{'form.makedefufn'}).'<p>');
                   2657: 	}
                   2658:     }
                   2659: 
                   2660: # ------------------------------------------------ Write the modified worksheet
                   2661: 
                   2662:    $r->print('<b>Current sheet:</b> '.&getfilename($asheet).'<p>');
                   2663: 
                   2664:    &tmpwrite($asheet);
1.61      www      2665: 
1.62      www      2666:     if (&gettype($asheet) eq 'studentcalc') {
                   2667: 	$r->print('<br>Show rows with empty A column: ');
                   2668:     } else {
                   2669:         $r->print('<br>Show empty rows: ');
                   2670:     } 
1.77      www      2671: 
                   2672:     $r->print(&hiddenfield('userselhidden','true').
                   2673:              '<input type=checkbox name=showall onClick="submit()"');
                   2674: 
                   2675:     if ($ENV{'form.showall'}) { 
                   2676:        $r->print(' checked'); 
                   2677:     } else {
                   2678: 	unless ($ENV{'form.userselhidden'}) {
                   2679:            unless 
                   2680: 	($ENV{'course.'.$ENV{'request.course.id'}.'.hideemptyrows'} eq 'yes') {
                   2681:           $r->print(' checked');
                   2682:           $ENV{'form.showall'}=1;
                   2683:            }
                   2684:        }
                   2685:     }
1.61      www      2686:     $r->print('>');
1.87      matthew  2687: 
1.69      www      2688:     if (&gettype($asheet) eq 'classcalc') {
                   2689:        $r->print(
                   2690:    ' Output CSV format: <input type=checkbox name=showcsv onClick="submit()"');
                   2691:        if ($ENV{'form.showcsv'}) { $r->print(' checked'); }
                   2692:        $r->print('>');
                   2693:     }
1.92    ! www      2694: 
        !          2695: # ------------------------------------------------------------------ Insertrows
        !          2696: 
        !          2697:    $r->print(<<ENDINSERTBUTTONS);
        !          2698: <br>
        !          2699: <input type='button' onClick='insertrow("top");' 
        !          2700: value='Insert Row Top'>
        !          2701: <input type='button' onClick='insertrow("bottom");' 
        !          2702: value='Insert Row Bottom'><br>
        !          2703: ENDINSERTBUTTONS
        !          2704: 
1.56      www      2705: # ------------------------------------------------------------- Print out sheet
1.8       www      2706: 
1.29      www      2707:     &outsheet($r,$asheet);
1.10      www      2708:     $r->print('</form></body></html>');
1.29      www      2709: 
1.14      www      2710: # ------------------------------------------------------------------------ Done
1.7       www      2711:   } else {
                   2712: # ----------------------------- Not in a course, or not allowed to modify parms
                   2713:       $ENV{'user.error.msg'}=
                   2714:         $r->uri.":opa:0:0:Cannot modify spreadsheet";
                   2715:       return HTTP_NOT_ACCEPTABLE; 
                   2716:   }
1.3       www      2717:     return OK;
1.28      www      2718: 
1.1       www      2719: }
                   2720: 
                   2721: 1;
                   2722: __END__
1.77      www      2723: 
                   2724: 
                   2725: 
                   2726: 

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