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

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

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