File:  [LON-CAPA] / loncom / interface / Attic / lonspreadsheet.pm
Revision 1.71: download - view: text, annotated - select for diffs
Wed Oct 17 22:09:53 2001 UTC (22 years, 8 months ago) by www
Branches: MAIN
CVS tags: stable_2001_fall, HEAD
CSV output for course list

    1: # The LearningOnline Network with CAPA
    2: # Spreadsheet/Grades Display Handler
    3: #
    4: # 11/11,11/15,11/27,12/04,12/05,12/06,12/07,
    5: # 12/08,12/09,12/11,12/12,12/15,12/16,12/18,12/19,12/30,
    6: # 01/01/01,02/01,03/01,19/01,20/01,22/01,
    7: # 03/05,03/08,03/10,03/12,03/13,03/15,03/17,
    8: # 03/19,03/20,03/21,03/27,04/05,04/09,
    9: # 07/09,07/14,07/21,09/01,09/10,9/11,9/12,9/13,9/14,9/17,
   10: # 10/16,10/17 Gerd Kortemeyer
   11: 
   12: package Apache::lonspreadsheet;
   13:             
   14: use strict;
   15: use Safe;
   16: use Safe::Hole;
   17: use Opcode;
   18: use Apache::lonnet;
   19: use Apache::Constants qw(:common :http);
   20: use GDBM_File;
   21: use HTML::TokeParser;
   22: 
   23: #
   24: # Caches for previously calculated spreadsheets
   25: #
   26: 
   27: my %oldsheets;
   28: my %loadedcaches;
   29: my %expiredates;
   30: 
   31: #
   32: # Cache for stores of an individual user
   33: #
   34: 
   35: my $cachedassess;
   36: my %cachedstores;
   37: 
   38: #
   39: # These cache hashes need to be independent of user, resource and course
   40: # (user and course can/should be in the keys)
   41: #
   42: 
   43: my %spreadsheets;
   44: my %courserdatas;
   45: my %userrdatas;
   46: my %defaultsheets;
   47: my %updatedata;
   48: 
   49: #
   50: # These global hashes are dependent on user, course and resource, 
   51: # and need to be initialized every time when a sheet is calculated
   52: #
   53: my %courseopt;
   54: my %useropt;
   55: my %parmhash;
   56: 
   57: # Stuff that only the screen handler can know
   58: 
   59: my $includedir;
   60: my $tmpdir;
   61: 
   62: # =============================================================================
   63: # ===================================== Implements an instance of a spreadsheet
   64: 
   65: sub initsheet {
   66:     my $safeeval = new Safe(shift);
   67:     my $safehole = new Safe::Hole;
   68:     $safeeval->permit("entereval");
   69:     $safeeval->permit(":base_math");
   70:     $safeeval->permit("sort");
   71:     $safeeval->deny(":base_io");
   72:     $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
   73:     my $code=<<'ENDDEFS';
   74: # ---------------------------------------------------- Inside of the safe space
   75: 
   76: #
   77: # f: formulas
   78: # t: intermediate format (variable references expanded)
   79: # v: output values
   80: # c: preloaded constants (A-column)
   81: # rl: row label
   82: # os: other spreadsheets (for student spreadsheet only)
   83: 
   84: undef %v; 
   85: undef %t;
   86: undef %f;
   87: undef %c;
   88: undef %rl;
   89: undef @os;
   90: 
   91: $maxrow=0;
   92: $sheettype='';
   93: 
   94: # filename/reference of the sheet
   95: 
   96: $filename='';
   97: 
   98: # user data
   99: $uname='';
  100: $uhome='';
  101: $udom='';
  102: 
  103: # course data
  104: 
  105: $csec='';
  106: $chome='';
  107: $cnum='';
  108: $cdom='';
  109: $cid='';
  110: $cfn='';
  111: 
  112: # symb
  113: 
  114: $usymb='';
  115: 
  116: sub mask {
  117:     my ($lower,$upper)=@_;
  118: 
  119:     $lower=~/([A-Za-z]|\*)(\d+|\*)/;
  120:     my $la=$1;
  121:     my $ld=$2;
  122: 
  123:     $upper=~/([A-Za-z]|\*)(\d+|\*)/;
  124:     my $ua=$1;
  125:     my $ud=$2;
  126:     my $alpha='';
  127:     my $num='';
  128: 
  129:     if (($la eq '*') || ($ua eq '*')) {
  130:        $alpha='[A-Za-z]';
  131:     } else {
  132:        if (($la=~/[A-Z]/) && ($ua=~/[A-Z]/) ||
  133:            ($la=~/[a-z]/) && ($ua=~/[a-z]/)) {
  134:           $alpha='['.$la.'-'.$ua.']';
  135:        } else {
  136:           $alpha='['.$la.'-Za-'.$ua.']';
  137:        }
  138:     }   
  139: 
  140:     if (($ld eq '*') || ($ud eq '*')) {
  141: 	$num='\d+';
  142:     } else {
  143:         if (length($ld)!=length($ud)) {
  144:            $num.='(';
  145: 	   map {
  146:               $num.='['.$_.'-9]';
  147:            } ($ld=~m/\d/g);
  148:            if (length($ud)-length($ld)>1) {
  149:               $num.='|\d{'.(length($ld)+1).','.(length($ud)-1).'}';
  150: 	   }
  151:            $num.='|';
  152:            map {
  153:                $num.='[0-'.$_.']';
  154:            } ($ud=~m/\d/g);
  155:            $num.=')';
  156:        } else {
  157:            my @lda=($ld=~m/\d/g);
  158:            my @uda=($ud=~m/\d/g);
  159:            my $i; $j=0; $notdone=1;
  160:            for ($i=0;($i<=$#lda)&&($notdone);$i++) {
  161:                if ($lda[$i]==$uda[$i]) {
  162: 		   $num.=$lda[$i];
  163:                    $j=$i;
  164:                } else {
  165:                    $notdone=0;
  166:                }
  167:            }
  168:            if ($j<$#lda-1) {
  169: 	       $num.='('.$lda[$j+1];
  170:                for ($i=$j+2;$i<=$#lda;$i++) {
  171:                    $num.='['.$lda[$i].'-9]';
  172:                }
  173:                if ($uda[$j+1]-$lda[$j+1]>1) {
  174: 		   $num.='|['.($lda[$j+1]+1).'-'.($uda[$j+1]-1).']\d{'.
  175:                    ($#lda-$j-1).'}';
  176:                }
  177: 	       $num.='|'.$uda[$j+1];
  178:                for ($i=$j+2;$i<=$#uda;$i++) {
  179:                    $num.='[0-'.$uda[$i].']';
  180:                }
  181:                $num.=')';
  182:            } else {
  183:                if ($lda[$#lda]!=$uda[$#uda]) {
  184:                   $num.='['.$lda[$#lda].'-'.$uda[$#uda].']';
  185: 	       }
  186:            }
  187:        }
  188:     }
  189:     return '^'.$alpha.$num."\$";
  190: }
  191: 
  192: sub NUM {
  193:     my $mask=mask(@_);
  194:     my $num=0;
  195:     map {
  196:         $num++;
  197:     } grep /$mask/,keys %v;
  198:     return $num;   
  199: }
  200: 
  201: sub BIN {
  202:     my ($low,$high,$lower,$upper)=@_;
  203:     my $mask=mask($lower,$upper);
  204:     my $num=0;
  205:     map {
  206:         if (($v{$_}>=$low) && ($v{$_}<=$high)) {
  207:             $num++;
  208:         }
  209:     } grep /$mask/,keys %v;
  210:     return $num;   
  211: }
  212: 
  213: 
  214: sub SUM {
  215:     my $mask=mask(@_);
  216:     my $sum=0;
  217:     map {
  218:         $sum+=$v{$_};
  219:     } grep /$mask/,keys %v;
  220:     return $sum;   
  221: }
  222: 
  223: sub MEAN {
  224:     my $mask=mask(@_);
  225:     my $sum=0; my $num=0;
  226:     map {
  227:         $sum+=$v{$_};
  228:         $num++;
  229:     } grep /$mask/,keys %v;
  230:     if ($num) {
  231:        return $sum/$num;
  232:     } else {
  233:        return undef;
  234:     }   
  235: }
  236: 
  237: sub STDDEV {
  238:     my $mask=mask(@_);
  239:     my $sum=0; my $num=0;
  240:     map {
  241:         $sum+=$v{$_};
  242:         $num++;
  243:     } grep /$mask/,keys %v;
  244:     unless ($num>1) { return undef; }
  245:     my $mean=$sum/$num;
  246:     $sum=0;
  247:     map {
  248:         $sum+=($v{$_}-$mean)**2;
  249:     } grep /$mask/,keys %v;
  250:     return sqrt($sum/($num-1));    
  251: }
  252: 
  253: sub PROD {
  254:     my $mask=mask(@_);
  255:     my $prod=1;
  256:     map {
  257:         $prod*=$v{$_};
  258:     } grep /$mask/,keys %v;
  259:     return $prod;   
  260: }
  261: 
  262: sub MAX {
  263:     my $mask=mask(@_);
  264:     my $max='-';
  265:     map {
  266:         unless ($max) { $max=$v{$_}; }
  267:         if (($v{$_}>$max) || ($max eq '-')) { $max=$v{$_}; }
  268:     } grep /$mask/,keys %v;
  269:     return $max;   
  270: }
  271: 
  272: sub MIN {
  273:     my $mask=mask(@_);
  274:     my $min='-';
  275:     map {
  276:         unless ($max) { $max=$v{$_}; }
  277:         if (($v{$_}<$min) || ($min eq '-')) { $min=$v{$_}; }
  278:     } grep /$mask/,keys %v;
  279:     return $min;   
  280: }
  281: 
  282: sub SUMMAX {
  283:     my ($num,$lower,$upper)=@_;
  284:     my $mask=mask($lower,$upper);
  285:     my @inside=();
  286:     map {
  287: 	$inside[$#inside+1]=$v{$_};
  288:     } grep /$mask/,keys %v;
  289:     @inside=sort(@inside);
  290:     my $sum=0; my $i;
  291:     for ($i=$#inside;(($i>$#inside-$num) && ($i>=0));$i--) { 
  292:         $sum+=$inside[$i];
  293:     }
  294:     return $sum;   
  295: }
  296: 
  297: sub SUMMIN {
  298:     my ($num,$lower,$upper)=@_;
  299:     my $mask=mask($lower,$upper);
  300:     my @inside=();
  301:     map {
  302: 	$inside[$#inside+1]=$v{$_};
  303:     } grep /$mask/,keys %v;
  304:     @inside=sort(@inside);
  305:     my $sum=0; my $i;
  306:     for ($i=0;(($i<$num) && ($i<=$#inside));$i++) { 
  307:         $sum+=$inside[$i];
  308:     }
  309:     return $sum;   
  310: }
  311: 
  312: sub expandnamed {
  313:     my $expression=shift;
  314:     if ($expression=~/^\&/) {
  315: 	my ($func,$var,$formula)=($expression=~/^\&(\w+)\(([^\;]+)\;(.*)\)/);
  316: 	my @vars=split(/\W+/,$formula);
  317:         my %values=();
  318:         undef %values;
  319:         map {
  320:             my $varname=$_;
  321:             if ($varname=~/\D/) {
  322:                $formula=~s/$varname/'$c{\''.$varname.'\'}'/ge;
  323:                $varname=~s/$var/\(\\w\+\)/g;
  324: 	       map {
  325: 		  if ($_=~/$varname/) {
  326: 		      $values{$1}=1;
  327:                   }
  328:                } keys %c;
  329: 	    }
  330:         } @vars;
  331:         if ($func eq 'EXPANDSUM') {
  332:             my $result='';
  333: 	    map {
  334:                 my $thissum=$formula;
  335:                 $thissum=~s/$var/$_/g;
  336:                 $result.=$thissum.'+';
  337:             } keys %values;
  338:             $result=~s/\+$//;
  339:             return $result;
  340:         } else {
  341: 	    return 0;
  342:         }
  343:     } else {
  344:         return '$c{\''.$expression.'\'}';
  345:     }
  346: }
  347: 
  348: sub sett {
  349:     %t=();
  350:     my $pattern='';
  351:     if ($sheettype eq 'assesscalc') {
  352: 	$pattern='A';
  353:     } else {
  354:         $pattern='[A-Z]';
  355:     }
  356:     map {
  357: 	if ($_=~/template\_(\w)/) {
  358: 	  my $col=$1;
  359:           unless ($col=~/^$pattern/) {
  360:             map {
  361: 	      if ($_=~/A(\d+)/) {
  362: 		my $trow=$1;
  363:                 if ($trow) {
  364: 		    my $lb=$col.$trow;
  365:                     $t{$lb}=$f{'template_'.$col};
  366:                     $t{$lb}=~s/\#/$trow/g;
  367:                     $t{$lb}=~s/\.\.+/\,/g;
  368:                     $t{$lb}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$v\{\'$2\'\}/g;
  369:                     $t{$lb}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.&expandnamed($2)/ge;
  370:                 }
  371: 	      }
  372:             } keys %f;
  373: 	  }
  374:       }
  375:     } keys %f;
  376:     map {
  377: 	if (($f{$_}) && ($_!~/template\_/)) {
  378:             my $matches=($_=~/^$pattern(\d+)/);
  379:             if  (($matches) && ($1)) {
  380: 	        unless ($f{$_}=~/^\!/) {
  381: 		    $t{$_}=$c{$_};
  382:                 }
  383:             } else {
  384: 	       $t{$_}=$f{$_};
  385:                $t{$_}=~s/\.\.+/\,/g;
  386:                $t{$_}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$v\{\'$2\'\}/g;
  387:                $t{$_}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.&expandnamed($2)/ge;
  388:             }
  389:         }
  390:     } keys %f;
  391:     $t{'A0'}=$f{'A0'};
  392:     $t{'A0'}=~s/\.\.+/\,/g;
  393:     $t{'A0'}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$v\{\'$2\'\}/g;
  394:     $t{'A0'}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.&expandnamed($2)/ge;
  395: }
  396: 
  397: sub calc {
  398:     %v=();
  399:     &sett();
  400:     my $notfinished=1;
  401:     my $depth=0;
  402:     while ($notfinished) {
  403: 	$notfinished=0;
  404:         map {
  405:             my $old=$v{$_};
  406:             $v{$_}=eval($t{$_});
  407: 	    if ($@) {
  408: 		%v=();
  409:                 return $@;
  410:             }
  411: 	    if ($v{$_} ne $old) { $notfinished=1; }
  412:         } keys %t;
  413:         $depth++;
  414:         if ($depth>100) {
  415: 	    %v=();
  416:             return 'Maximum calculation depth exceeded';
  417:         }
  418:     }
  419:     return '';
  420: }
  421: 
  422: sub templaterow {
  423:     my @cols=();
  424:     $cols[0]='<b><font size=+1>Template</font></b>';
  425:     map {
  426:         my $fm=$f{'template_'.$_};
  427:         $fm=~s/[\'\"]/\&\#34;/g;
  428:         $cols[$#cols+1]="'template_$_','$fm'".'___eq___'.$fm;
  429:     } ('A','B','C','D','E','F','G','H','I','J','K','L','M',
  430:        'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
  431:        'a','b','c','d','e','f','g','h','i','j','k','l','m',
  432:        'n','o','p','q','r','s','t','u','v','w','x','y','z');
  433:     return @cols;
  434: }
  435: 
  436: sub outrowassess {
  437:     my $n=shift;
  438:     my @cols=();
  439:     if ($n) {
  440:        my ($usy,$ufn)=split(/\_\_\&\&\&\_\_/,$f{'A'.$n});
  441:        $cols[0]=$rl{$usy}.'<br>'.
  442:                 '<select name="sel_'.$n.'" onChange="changesheet('.$n.
  443:                 ')"><option name="default">Default</option>';
  444:        map {
  445:            $cols[0].='<option name="'.$_.'"';
  446:             if ($ufn eq $_) {
  447:                $cols[0].=' selected';
  448:             }
  449:             $cols[0].='>'.$_.'</option>';
  450:        } @os;
  451:        $cols[0].='</select>';
  452:     } else {
  453:        $cols[0]='<b><font size=+1>Export</font></b>';
  454:     }
  455:     map {
  456:         my $fm=$f{$_.$n};
  457:         $fm=~s/[\'\"]/\&\#34;/g;
  458:         $cols[$#cols+1]="'$_$n','$fm'".'___eq___'.$v{$_.$n};
  459:     } ('A','B','C','D','E','F','G','H','I','J','K','L','M',
  460:        'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
  461:        'a','b','c','d','e','f','g','h','i','j','k','l','m',
  462:        'n','o','p','q','r','s','t','u','v','w','x','y','z');
  463:     return @cols;
  464: }
  465: 
  466: sub outrow {
  467:     my $n=shift;
  468:     my @cols=();
  469:     if ($n) {
  470:        $cols[0]=$rl{$f{'A'.$n}};
  471:     } else {
  472:        $cols[0]='<b><font size=+1>Export</font></b>';
  473:     }
  474:     map {
  475:         my $fm=$f{$_.$n};
  476:         $fm=~s/[\'\"]/\&\#34;/g;
  477:         $cols[$#cols+1]="'$_$n','$fm'".'___eq___'.$v{$_.$n};
  478:     } ('A','B','C','D','E','F','G','H','I','J','K','L','M',
  479:        'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
  480:        'a','b','c','d','e','f','g','h','i','j','k','l','m',
  481:        'n','o','p','q','r','s','t','u','v','w','x','y','z');
  482:     return @cols;
  483: }
  484: 
  485: sub exportrowa {
  486:     my @exportarray=();
  487:     map {
  488: 	$exportarray[$#exportarray+1]=$v{$_.'0'};
  489:     } ('A','B','C','D','E','F','G','H','I','J','K','L','M',
  490:        'N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
  491:     return @exportarray;
  492: }
  493: 
  494: # ------------------------------------------- End of "Inside of the safe space"
  495: ENDDEFS
  496:     $safeeval->reval($code);
  497:     return $safeeval;
  498: }
  499: 
  500: # ------------------------------------------------ Add or change formula values
  501: 
  502: sub setformulas {
  503:     my ($safeeval,%f)=@_;
  504:     %{$safeeval->varglob('f')}=%f;
  505: }
  506: 
  507: # ------------------------------------------------ Add or change formula values
  508: 
  509: sub setconstants {
  510:     my ($safeeval,%c)=@_;
  511:     %{$safeeval->varglob('c')}=%c;
  512: }
  513: 
  514: # --------------------------------------------- Set names of other spreadsheets
  515: 
  516: sub setothersheets {
  517:     my ($safeeval,@os)=@_;
  518:     @{$safeeval->varglob('os')}=@os;
  519: }
  520: 
  521: # ------------------------------------------------ Add or change formula values
  522: 
  523: sub setrowlabels {
  524:     my ($safeeval,%rl)=@_;
  525:     %{$safeeval->varglob('rl')}=%rl;
  526: }
  527: 
  528: # ------------------------------------------------------- Calculate spreadsheet
  529: 
  530: sub calcsheet {
  531:     my $safeeval=shift;
  532:     $safeeval->reval('&calc();');
  533: }
  534: 
  535: # ------------------------------------------------------------------ Get values
  536: 
  537: sub getvalues {
  538:     my $safeeval=shift;
  539:     return $safeeval->reval('%v');
  540: }
  541: 
  542: # ---------------------------------------------------------------- Get formulas
  543: 
  544: sub getformulas {
  545:     my $safeeval=shift;
  546:     return %{$safeeval->varglob('f')};
  547: }
  548: 
  549: # -------------------------------------------------------------------- Get type
  550: 
  551: sub gettype {
  552:     my $safeeval=shift;
  553:     return $safeeval->reval('$sheettype');
  554: }
  555: 
  556: # ------------------------------------------------------------------ Set maxrow
  557: 
  558: sub setmaxrow {
  559:     my ($safeeval,$row)=@_;
  560:     $safeeval->reval('$maxrow='.$row.';');
  561: }
  562: 
  563: # ------------------------------------------------------------------ Get maxrow
  564: 
  565: sub getmaxrow {
  566:     my $safeeval=shift;
  567:     return $safeeval->reval('$maxrow');
  568: }
  569: 
  570: # ---------------------------------------------------------------- Set filename
  571: 
  572: sub setfilename {
  573:     my ($safeeval,$fn)=@_;
  574:     $safeeval->reval('$filename="'.$fn.'";');
  575: }
  576: 
  577: # ---------------------------------------------------------------- Get filename
  578: 
  579: sub getfilename {
  580:     my $safeeval=shift;
  581:     return $safeeval->reval('$filename');
  582: }
  583: 
  584: # --------------------------------------------------------------- Get course ID
  585: 
  586: sub getcid {
  587:     my $safeeval=shift;
  588:     return $safeeval->reval('$cid');
  589: }
  590: 
  591: # --------------------------------------------------------- Get course filename
  592: 
  593: sub getcfn {
  594:     my $safeeval=shift;
  595:     return $safeeval->reval('$cfn');
  596: }
  597: 
  598: # ----------------------------------------------------------- Get course number
  599: 
  600: sub getcnum {
  601:     my $safeeval=shift;
  602:     return $safeeval->reval('$cnum');
  603: }
  604: 
  605: # ------------------------------------------------------------- Get course home
  606: 
  607: sub getchome {
  608:     my $safeeval=shift;
  609:     return $safeeval->reval('$chome');
  610: }
  611: 
  612: # ----------------------------------------------------------- Get course domain
  613: 
  614: sub getcdom {
  615:     my $safeeval=shift;
  616:     return $safeeval->reval('$cdom');
  617: }
  618: 
  619: # ---------------------------------------------------------- Get course section
  620: 
  621: sub getcsec {
  622:     my $safeeval=shift;
  623:     return $safeeval->reval('$csec');
  624: }
  625: 
  626: # --------------------------------------------------------------- Get user name
  627: 
  628: sub getuname {
  629:     my $safeeval=shift;
  630:     return $safeeval->reval('$uname');
  631: }
  632: 
  633: # ------------------------------------------------------------- Get user domain
  634: 
  635: sub getudom {
  636:     my $safeeval=shift;
  637:     return $safeeval->reval('$udom');
  638: }
  639: 
  640: # --------------------------------------------------------------- Get user home
  641: 
  642: sub getuhome {
  643:     my $safeeval=shift;
  644:     return $safeeval->reval('$uhome');
  645: }
  646: 
  647: # -------------------------------------------------------------------- Get symb
  648: 
  649: sub getusymb {
  650:     my $safeeval=shift;
  651:     return $safeeval->reval('$usymb');
  652: }
  653: 
  654: # ------------------------------------------------------------- Export of A-row
  655: 
  656: sub exportdata {
  657:     my $safeeval=shift;
  658:     return $safeeval->reval('&exportrowa()');
  659: }
  660: 
  661: 
  662: # ========================================================== End of Spreadsheet
  663: # =============================================================================
  664: 
  665: #
  666: # Procedures for screen output
  667: #
  668: # --------------------------------------------- Produce output row n from sheet
  669: 
  670: sub rown {
  671:     my ($safeeval,$n)=@_;
  672:     my $defaultbg;
  673:     my $rowdata='';
  674:     my $dataflag=0;
  675:     unless ($n eq '-') {
  676:        $defaultbg=((($n-1)/5)==int(($n-1)/5))?'#E0E0':'#FFFF';
  677:     } else {
  678:        $defaultbg='#E0FF';
  679:     }
  680:     unless ($ENV{'form.showcsv'}) {
  681:        $rowdata.="\n<tr><td><b><font size=+1>$n</font></b></td>";
  682:     } else {
  683:        $rowdata.="\n".'"'.$n.'"';
  684:     }
  685:     my $showf=0;
  686:     my $proc;
  687:     my $maxred;
  688:     my $sheettype=&gettype($safeeval);
  689:     if ($sheettype eq 'studentcalc') {
  690:         $proc='&outrowassess';
  691:         $maxred=26;
  692:     } else {
  693:         $proc='&outrow';
  694:     }
  695:     if ($sheettype eq 'assesscalc') {
  696:         $maxred=1;
  697:     } else {
  698:         $maxred=26;
  699:     }
  700:     if ($n eq '-') { $proc='&templaterow'; $n=-1; $dataflag=1; }
  701:     map {
  702:        my $bgcolor=$defaultbg.((($showf-1)/5==int(($showf-1)/5))?'99':'DD');
  703:        my ($fm,$vl)=split(/\_\_\_eq\_\_\_/,$_);
  704:        if ((($vl ne '') || ($vl eq '0')) &&
  705:            (($showf==1) || ($sheettype ne 'studentcalc'))) { $dataflag=1; }
  706:        if ($showf==0) { $vl=$_; }
  707:       unless ($ENV{'form.showcsv'}) {
  708:        if ($showf<=$maxred) { $bgcolor='#FFDDDD'; }
  709:        if (($n==0) && ($showf<=26)) { $bgcolor='#CCCCFF'; } 
  710:        if (($showf>$maxred) || ((!$n) && ($showf>0))) {
  711: 	   if ($vl eq '') {
  712: 	       $vl='<font size=+2 color='.$bgcolor.'>&#35;</font>';
  713:            }
  714:            $rowdata.=
  715:        '<td bgcolor='.$bgcolor.'><a href="javascript:celledit('.$fm.');">'.$vl.
  716: 	       '</a></td>';
  717:        } else {
  718:            $rowdata.='<td bgcolor='.$bgcolor.'>&nbsp;'.$vl.'&nbsp;</td>';
  719:        }
  720:       } else {
  721: 	  $rowdata.=',"'.$vl.'"';
  722:       }
  723:        $showf++;
  724:     } $safeeval->reval($proc.'('.$n.')');
  725:     if ($ENV{'form.showall'} || ($dataflag)) {
  726:        return $rowdata.($ENV{'form.showcsv'}?'':'</tr>');
  727:     } else {
  728:        return '';
  729:     }
  730: }
  731: 
  732: # ------------------------------------------------------------- Print out sheet
  733: 
  734: sub outsheet {
  735:     my ($r,$safeeval)=@_;
  736:     my $maxred;
  737:     my $realm;
  738:     if (&gettype($safeeval) eq 'assesscalc') {
  739:         $maxred=1;
  740:         $realm='Assessment';
  741:     } elsif (&gettype($safeeval) eq 'studentcalc') {
  742:         $maxred=26;
  743:         $realm='User';
  744:     } else {
  745:         $maxred=26;
  746:         $realm='Course';
  747:     }
  748:     my $maxyellow=52-$maxred;
  749:     my $tabledata;
  750:     unless ($ENV{'form.showcsv'}) {
  751:        $tabledata=
  752:         '<table border=2><tr><th colspan=2 rowspan=2><font size=+2>'.
  753:                   $realm.'</font></th>'.
  754:                   '<td bgcolor=#FFDDDD colspan='.$maxred.
  755:                   '><b><font size=+1>Import</font></b></td>'.
  756:                   '<td colspan='.$maxyellow.
  757: 		  '><b><font size=+1>Calculations</font></b></td></tr><tr>';
  758:     my $showf=0;
  759:     map {
  760:         $showf++;
  761:         if ($showf<=$maxred) { 
  762:            $tabledata.='<td bgcolor="#FFDDDD">'; 
  763:         } else {
  764:            $tabledata.='<td>';
  765:         }
  766:         $tabledata.="<b><font size=+1>$_</font></b></td>";
  767:     } ('A','B','C','D','E','F','G','H','I','J','K','L','M',
  768:        'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
  769:        'a','b','c','d','e','f','g','h','i','j','k','l','m',
  770:        'n','o','p','q','r','s','t','u','v','w','x','y','z');
  771:     $tabledata.='</tr>'.&rown($safeeval,'-').&rown($safeeval,0);
  772:    } else { $tabledata='<pre>'; }
  773: 
  774:     $r->print($tabledata);
  775: 
  776:     my $row;
  777:     my $maxrow=&getmaxrow($safeeval);
  778: 
  779:     my @sortby=();
  780:     my @sortidx=();
  781:     for ($row=1;$row<=$maxrow;$row++) {
  782:        $sortby[$row-1]=$safeeval->reval('$f{"A'.$row.'"}');
  783:        $sortidx[$row-1]=$row-1;
  784:     }
  785:     @sortidx=sort { $sortby[$a] cmp $sortby[$b]; } @sortidx;
  786: 
  787:         my $what='Student';
  788:         if (&gettype($safeeval) eq 'assesscalc') {
  789: 	    $what='Item';
  790: 	} elsif (&gettype($safeeval) eq 'studentcalc') {
  791:             $what='Assessment';
  792:         }
  793: 
  794:     my $n=0;
  795:     for ($row=0;$row<$maxrow;$row++) {
  796:      my $thisrow=&rown($safeeval,$sortidx[$row]+1);
  797:      if ($thisrow) {
  798:        if (($n/25==int($n/25)) && (!$ENV{'form.showcsv'})) {
  799: 	$r->print("</table>\n<br>\n");
  800:         $r->rflush();
  801:         $r->print('<table border=2><tr><td>&nbsp;<td>'.$what.'</td>');
  802:         map {
  803:            $r->print('<td>'.$_.'</td>');
  804:         } ('A','B','C','D','E','F','G','H','I','J','K','L','M',
  805:            'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
  806:            'a','b','c','d','e','f','g','h','i','j','k','l','m',
  807:            'n','o','p','q','r','s','t','u','v','w','x','y','z');
  808:         $r->print('</tr>');
  809:        }
  810:        $n++;
  811:        $r->print($thisrow);
  812:       }
  813:     }
  814:     $r->print($ENV{'form.showcsv'}?'</pre>':'</table>');
  815: }
  816: 
  817: #
  818: # ----------------------------------------------- Read list of available sheets
  819: # 
  820: 
  821: sub othersheets {
  822:     my ($safeeval,$stype)=@_;
  823: 
  824:     my $cnum=&getcnum($safeeval);
  825:     my $cdom=&getcdom($safeeval);
  826:     my $chome=&getchome($safeeval);
  827: 
  828:     my @alternatives=();
  829:     my $result=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.':'.
  830:                                       $stype.'_spreadsheets',$chome);
  831:     if ($result!~/^error\:/) {
  832: 	map {
  833:             $alternatives[$#alternatives+1]=
  834:             &Apache::lonnet::unescape((split(/\=/,$_))[0]);
  835:         } split(/\&/,$result);
  836:     } 
  837:     return @alternatives; 
  838: }
  839: 
  840: #
  841: # -------------------------------------- Read spreadsheet formulas for a course
  842: #
  843: 
  844: sub readsheet {
  845:   my ($safeeval,$fn)=@_;
  846:   my $stype=&gettype($safeeval);
  847:   my $cnum=&getcnum($safeeval);
  848:   my $cdom=&getcdom($safeeval);
  849:   my $chome=&getchome($safeeval);
  850: 
  851: # --------- There is no filename. Look for defaults in course and global, cache
  852: 
  853:   unless($fn) {
  854:       unless ($fn=$defaultsheets{$cnum.'_'.$cdom.'_'.$stype}) {
  855:          $fn=&Apache::lonnet::reply('get:'.$cdom.':'.$cnum.
  856:                                     ':environment:spreadsheet_default_'.$stype,
  857:                                     $chome);
  858:          unless (($fn) && ($fn!~/^error\:/)) {
  859: 	     $fn='default_'.$stype;
  860:          }
  861:          $defaultsheets{$cnum.'_'.$cdom.'_'.$stype}=$fn; 
  862:       }
  863:   }
  864: 
  865: # ---------------------------------------------------------- fn now has a value
  866: 
  867:   &setfilename($safeeval,$fn);
  868: 
  869: # ------------------------------------------------------ see if sheet is cached
  870:   my $fstring='';
  871:   if ($fstring=$spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}) {
  872:       &setformulas($safeeval,split(/\_\_\_\;\_\_\_/,$fstring));
  873:   } else {
  874: 
  875: # ---------------------------------------------------- Not cached, need to read
  876: 
  877:      my %f=();
  878: 
  879:      if ($fn=~/^default\_/) {
  880: 	my $sheetxml='';
  881:        {
  882:          my $fh;
  883:          if ($fh=Apache::File->new($includedir.
  884:                          '/default.'.&gettype($safeeval))) {
  885:                $sheetxml=join('',<$fh>);
  886:           }
  887:        }
  888:         my $parser=HTML::TokeParser->new(\$sheetxml);
  889:         my $token;
  890:         while ($token=$parser->get_token) {
  891:           if ($token->[0] eq 'S') {
  892:  	     if ($token->[1] eq 'field') {
  893:  		 $f{$token->[2]->{'col'}.$token->[2]->{'row'}}=
  894:  		     $parser->get_text('/field');
  895:  	     }
  896:              if ($token->[1] eq 'template') {
  897:                  $f{'template_'.$token->[2]->{'col'}}=
  898:                      $parser->get_text('/template');
  899:              }
  900:           }
  901:         }
  902:       } else {
  903:           my $sheet='';
  904:           my $reply=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.':'.$fn,
  905:                                          $chome);
  906:           unless ($reply=~/^error\:/) {
  907:              $sheet=$reply;
  908: 	  }
  909:           map {
  910:              my ($name,$value)=split(/\=/,$_);
  911:              $f{&Apache::lonnet::unescape($name)}=
  912: 	        &Apache::lonnet::unescape($value);
  913:           } split(/\&/,$sheet);
  914:        }
  915: # --------------------------------------------------------------- Cache and set
  916:        $spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}=join('___;___',%f);  
  917:        &setformulas($safeeval,%f);
  918:     }
  919: }
  920: 
  921: # -------------------------------------------------------- Make new spreadsheet
  922: 
  923: sub makenewsheet {
  924:     my ($uname,$udom,$stype,$usymb)=@_;
  925:     my $safeeval=initsheet($stype);
  926:     $safeeval->reval(
  927:        '$uname="'.$uname.
  928:       '";$udom="'.$udom.
  929:       '";$uhome="'.&Apache::lonnet::homeserver($uname,$udom).
  930:       '";$sheettype="'.$stype.
  931:       '";$usymb="'.$usymb.
  932:       '";$csec="'.&Apache::lonnet::usection($udom,$uname,
  933:                                             $ENV{'request.course.id'}).
  934:       '";$cid="'.$ENV{'request.course.id'}.
  935:       '";$cfn="'.$ENV{'request.course.fn'}.
  936:       '";$cnum="'.$ENV{'course.'.$ENV{'request.course.id'}.'.num'}.
  937:       '";$cdom="'.$ENV{'course.'.$ENV{'request.course.id'}.'.domain'}.
  938:       '";$chome="'.$ENV{'course.'.$ENV{'request.course.id'}.'.home'}.'";');
  939:     return $safeeval;
  940: }
  941: 
  942: # ------------------------------------------------------------ Save spreadsheet
  943: 
  944: sub writesheet {
  945:   my ($safeeval,$makedef)=@_;
  946:   my $cid=&getcid($safeeval);
  947:   if (&Apache::lonnet::allowed('opa',$cid)) {
  948:     my %f=&getformulas($safeeval);
  949:     my $stype=&gettype($safeeval);
  950:     my $cnum=&getcnum($safeeval);
  951:     my $cdom=&getcdom($safeeval);
  952:     my $chome=&getchome($safeeval);
  953:     my $fn=&getfilename($safeeval);
  954: 
  955: # ------------------------------------------------------------- Cache new sheet
  956:     $spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}=join('___;___',%f);    
  957: # ----------------------------------------------------------------- Write sheet
  958:     my $sheetdata='';
  959:     map {
  960:      unless ($f{$_} eq 'import') {
  961:        $sheetdata.=&Apache::lonnet::escape($_).'='.
  962: 	   &Apache::lonnet::escape($f{$_}).'&';
  963:      }
  964:     } keys %f;
  965:     $sheetdata=~s/\&$//;
  966:     my $reply=&Apache::lonnet::reply('put:'.$cdom.':'.$cnum.':'.$fn.':'.
  967:               $sheetdata,$chome);
  968:     if ($reply eq 'ok') {
  969:           $reply=&Apache::lonnet::reply('put:'.$cdom.':'.$cnum.':'.
  970:               $stype.'_spreadsheets:'.
  971:               &Apache::lonnet::escape($fn).'='.$ENV{'user.name'}.'@'.
  972:                                                $ENV{'user.domain'},
  973:               $chome);
  974:           if ($reply eq 'ok') {
  975:               if ($makedef) { 
  976:                 return &Apache::lonnet::reply('put:'.$cdom.':'.$cnum.
  977:                                 ':environment:spreadsheet_default_'.$stype.'='.
  978:                                 &Apache::lonnet::escape($fn),
  979:                                 $chome);
  980: 	      } else {
  981: 		  return $reply;
  982:     	      }
  983: 	   } else {
  984: 	       return $reply;
  985:            }
  986:       } else {
  987: 	  return $reply;
  988:       }
  989:   }
  990:   return 'unauthorized';
  991: }
  992: 
  993: # ----------------------------------------------- Make a temp copy of the sheet
  994: # "Modified workcopy" - interactive only
  995: #
  996: 
  997: sub tmpwrite {
  998:     my $safeeval=shift;
  999:     my $fn=$ENV{'user.name'}.'_'.
 1000:            $ENV{'user.domain'}.'_spreadsheet_'.&getusymb($safeeval).'_'.
 1001:            &getfilename($safeeval);
 1002:     $fn=~s/\W/\_/g;
 1003:     $fn=$tmpdir.$fn.'.tmp';
 1004:     my $fh;
 1005:     if ($fh=Apache::File->new('>'.$fn)) {
 1006: 	print $fh join("\n",&getformulas($safeeval));
 1007:     }
 1008: }
 1009: 
 1010: # ---------------------------------------------------------- Read the temp copy
 1011: 
 1012: sub tmpread {
 1013:     my ($safeeval,$nfield,$nform)=@_;
 1014:     my $fn=$ENV{'user.name'}.'_'.
 1015:            $ENV{'user.domain'}.'_spreadsheet_'.&getusymb($safeeval).'_'.
 1016:            &getfilename($safeeval);
 1017:     $fn=~s/\W/\_/g;
 1018:     $fn=$tmpdir.$fn.'.tmp';
 1019:     my $fh;
 1020:     my %fo=();
 1021:     if ($fh=Apache::File->new($fn)) {
 1022:         my $name;
 1023:         while ($name=<$fh>) {
 1024: 	    chomp($name);
 1025:             my $value=<$fh>;
 1026:             chomp($value);
 1027:             $fo{$name}=$value;
 1028:         }
 1029:     }
 1030:     if ($nform eq 'changesheet') {
 1031:         $fo{'A'.$nfield}=(split(/\_\_\&\&\&\_\_/,$fo{'A'.$nfield}))[0];
 1032:         unless ($ENV{'form.sel_'.$nfield} eq 'Default') {
 1033: 	    $fo{'A'.$nfield}.='__&&&__'.$ENV{'form.sel_'.$nfield};
 1034:         }
 1035:     } else {
 1036:        if ($nfield) { $fo{$nfield}=$nform; }
 1037:     }
 1038:     &setformulas($safeeval,%fo);
 1039: }
 1040: 
 1041: # ================================================================== Parameters
 1042: # -------------------------------------------- Figure out a cascading parameter
 1043: #
 1044: # For this function to work
 1045: #
 1046: # * parmhash needs to be tied
 1047: # * courseopt and useropt need to be initialized for this user and course
 1048: #
 1049: 
 1050: sub parmval {
 1051:     my ($what,$safeeval)=@_;
 1052:     my $cid=&getcid($safeeval);
 1053:     my $csec=&getcsec($safeeval);
 1054:     my $uname=&getuname($safeeval);
 1055:     my $udom=&getudom($safeeval);
 1056:     my $symb=&getusymb($safeeval);
 1057: 
 1058:     unless ($symb) { return ''; }
 1059:     my $result='';
 1060: 
 1061:     my ($mapname,$id,$fn)=split(/\_\_\_/,$symb);
 1062: # ----------------------------------------------------- Cascading lookup scheme
 1063:        my $rwhat=$what;
 1064:        $what=~s/^parameter\_//;
 1065:        $what=~s/\_([^\_]+)$/\.$1/;
 1066: 
 1067:        my $symbparm=$symb.'.'.$what;
 1068:        my $mapparm=$mapname.'___(all).'.$what;
 1069:        my $usercourseprefix=$uname.'_'.$udom.'_'.$cid;
 1070: 
 1071:        my $seclevel=
 1072:             $usercourseprefix.'.['.
 1073: 		$csec.'].'.$what;
 1074:        my $seclevelr=
 1075:             $usercourseprefix.'.['.
 1076: 		$csec.'].'.$symbparm;
 1077:        my $seclevelm=
 1078:             $usercourseprefix.'.['.
 1079: 		$csec.'].'.$mapparm;
 1080: 
 1081:        my $courselevel=
 1082:             $usercourseprefix.'.'.$what;
 1083:        my $courselevelr=
 1084:             $usercourseprefix.'.'.$symbparm;
 1085:        my $courselevelm=
 1086:             $usercourseprefix.'.'.$mapparm;
 1087: 
 1088: # ---------------------------------------------------------- fourth, check user
 1089:       
 1090:       if ($uname) { 
 1091: 
 1092:        if ($useropt{$courselevelr}) { return $useropt{$courselevelr}; }
 1093: 
 1094:        if ($useropt{$courselevelm}) { return $useropt{$courselevelm}; }
 1095: 
 1096:        if ($useropt{$courselevel}) { return $useropt{$courselevel}; }
 1097: 
 1098:       }
 1099: 
 1100: # --------------------------------------------------------- third, check course
 1101:      
 1102:        if ($csec) {
 1103:  
 1104:         if ($courseopt{$seclevelr}) { return $courseopt{$seclevelr}; }
 1105: 
 1106:         if ($courseopt{$seclevelm}) { return $courseopt{$seclevelm}; }  
 1107: 
 1108:         if ($courseopt{$seclevel}) { return $courseopt{$seclevel}; }
 1109:   
 1110:       }
 1111: 
 1112:        if ($courseopt{$courselevelr}) { return $courseopt{$courselevelr}; }
 1113: 
 1114:        if ($courseopt{$courselevelm}) { return $courseopt{$courselevelm}; }
 1115: 
 1116:        if ($courseopt{$courselevel}) { return $courseopt{$courselevel}; }
 1117: 
 1118: # ----------------------------------------------------- second, check map parms
 1119: 
 1120:        my $thisparm=$parmhash{$symbparm};
 1121:        if ($thisparm) { return $thisparm; }
 1122: 
 1123: # -------------------------------------------------------- first, check default
 1124: 
 1125:        return &Apache::lonnet::metadata($fn,$rwhat.'.default');
 1126:         
 1127: }
 1128: 
 1129: # ---------------------------------------------- Update rows for course listing
 1130: 
 1131: sub updateclasssheet {
 1132:     my $safeeval=shift;
 1133:     my $cnum=&getcnum($safeeval);
 1134:     my $cdom=&getcdom($safeeval);
 1135:     my $cid=&getcid($safeeval);
 1136:     my $chome=&getchome($safeeval);
 1137: 
 1138: # ---------------------------------------------- Read class list and row labels
 1139: 
 1140:     my $classlst=&Apache::lonnet::reply
 1141:                                  ('dump:'.$cdom.':'.$cnum.':classlist',$chome);
 1142:     my %currentlist=();
 1143:     my $now=time;
 1144:     unless ($classlst=~/^error\:/) {
 1145:         map {
 1146:             my ($name,$value)=split(/\=/,$_);
 1147:             my ($end,$start)=split(/\:/,&Apache::lonnet::unescape($value));
 1148:             my $active=1;
 1149:             if (($end) && ($now>$end)) { $active=0; }
 1150:             if ($active) {
 1151:                 my $rowlabel='';
 1152:                 $name=&Apache::lonnet::unescape($name);
 1153:                 my ($sname,$sdom)=split(/\:/,$name);
 1154:                 my $ssec=&Apache::lonnet::usection($sdom,$sname,$cid);
 1155:                 if ($ssec==-1) {
 1156: 		   unless ($ENV{'form.showcsv'}) {
 1157:                     $rowlabel='<font color=red>Data not available: '.$name.
 1158: 			      '</font>';
 1159: 		   } else {
 1160: 		       $rowlabel='ERROR","'.$name.
 1161:                                  '","Data not available","","","';
 1162:                    }
 1163:                 } else {
 1164:                     my %reply=&Apache::lonnet::idrget($sdom,$sname);
 1165:                     my $reply=&Apache::lonnet::reply('get:'.$sdom.':'.$sname.
 1166: 		      ':environment:firstname&middlename&lastname&generation',
 1167:                       &Apache::lonnet::homeserver($sname,$sdom));
 1168: 		   unless ($ENV{'form.showcsv'}) {
 1169:                     $rowlabel='<a href="/adm/studentcalc?uname='.$sname.
 1170:                               '&udom='.$sdom.'">'.
 1171:                               $ssec.'&nbsp;'.$reply{$sname}.'<br>';
 1172:                     map {
 1173:                         $rowlabel.=&Apache::lonnet::unescape($_).' ';
 1174:                     } split(/\&/,$reply);
 1175:                     $rowlabel.='</a>';
 1176: 		   } else {
 1177: 		    $rowlabel=$ssec.'","'.$reply{$sname}.'"';
 1178:                     my $ncount=0;
 1179:                     map {
 1180:                         $rowlabel.=',"'.&Apache::lonnet::unescape($_).'"';
 1181:                         $ncount++;
 1182:                     } split(/\&/,$reply);
 1183:                     unless ($ncount==4) { $rowlabel.=',""'; }
 1184:                     $rowlabel=~s/\"$//;
 1185: 		   }
 1186:                 }
 1187: 		$currentlist{&Apache::lonnet::unescape($name)}=$rowlabel;
 1188:             }
 1189:         } split(/\&/,$classlst);
 1190: #
 1191: # -------------------- Find discrepancies between the course row table and this
 1192: #
 1193:         my %f=&getformulas($safeeval);
 1194:         my $changed=0;
 1195: 
 1196:         my $maxrow=0;
 1197:         my %existing=();
 1198: 
 1199: # ----------------------------------------------------------- Now obsolete rows
 1200: 	map {
 1201: 	    if ($_=~/^A(\d+)/) {
 1202:                 $maxrow=($1>$maxrow)?$1:$maxrow;
 1203:                 $existing{$f{$_}}=1;
 1204: 		unless ((defined($currentlist{$f{$_}})) || (!$1)) {
 1205: 		   $f{$_}='!!! Obsolete';
 1206:                    $changed=1;
 1207:                 }
 1208:             }
 1209:         } keys %f;
 1210: 
 1211: # -------------------------------------------------------- New and unknown keys
 1212:      
 1213:         map {
 1214:             unless ($existing{$_}) {
 1215: 		$changed=1;
 1216:                 $maxrow++;
 1217:                 $f{'A'.$maxrow}=$_;
 1218:             }
 1219:         } sort keys %currentlist;        
 1220:      
 1221:         if ($changed) { &setformulas($safeeval,%f); }
 1222: 
 1223:         &setmaxrow($safeeval,$maxrow);
 1224:         &setrowlabels($safeeval,%currentlist);
 1225: 
 1226:     } else {
 1227:         return 'Could not access course data';
 1228:     }
 1229: }
 1230: 
 1231: # ----------------------------------- Update rows for student and assess sheets
 1232: 
 1233: sub updatestudentassesssheet {
 1234:     my $safeeval=shift;
 1235:     my %bighash;
 1236:     my $stype=&gettype($safeeval);
 1237:     my %current=();
 1238:     unless ($updatedata{$ENV{'request.course.fn'}.'_'.$stype}) {
 1239: # -------------------------------------------------------------------- Tie hash
 1240:       if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
 1241:                        &GDBM_READER,0640)) {
 1242: # --------------------------------------------------------- Get all assessments
 1243: 
 1244: 	my %allkeys=('timestamp' => 
 1245:                      'Timestamp of Last Transaction<br>timestamp');
 1246:         my %allassess=();
 1247: 
 1248:         my $adduserstr='';
 1249:         if ((&getuname($safeeval) ne $ENV{'user.name'}) ||
 1250:             (&getudom($safeeval) ne $ENV{'user.domain'})) {
 1251:             $adduserstr='&uname='.&getuname($safeeval).
 1252: 		'&udom='.&getudom($safeeval);
 1253:         }
 1254: 
 1255:         map {
 1256: 	    if ($_=~/^src\_(\d+)\.(\d+)$/) {
 1257: 	       my $mapid=$1;
 1258:                my $resid=$2;
 1259:                my $id=$mapid.'.'.$resid;
 1260:                my $srcf=$bighash{$_};
 1261:                if ($srcf=~/\.(problem|exam|quiz|assess|survey|form)$/) {
 1262:                  my $symb=
 1263:                      &Apache::lonnet::declutter($bighash{'map_id_'.$mapid}).
 1264: 			    '___'.$resid.'___'.
 1265: 			    &Apache::lonnet::declutter($srcf);
 1266: 		 $allassess{$symb}=
 1267: 	            '<a href="/adm/assesscalc?usymb='.$symb.$adduserstr.'">'.
 1268:                      $bighash{'title_'.$id}.'</a>';
 1269:                  if ($stype eq 'assesscalc') {
 1270:                    map {
 1271:                        if (($_=~/^stores\_(.*)/) || ($_=~/^parameter\_(.*)/)) {
 1272: 			  my $key=$_;
 1273:                           my $display=
 1274: 			      &Apache::lonnet::metadata($srcf,$key.'.display');
 1275:                           unless ($display) {
 1276:                               $display.=
 1277: 			         &Apache::lonnet::metadata($srcf,$key.'.name');
 1278:                           }
 1279:                           $display.='<br>'.$key;
 1280:                           $allkeys{$key}=$display;
 1281: 		       }
 1282:                    } split(/\,/,&Apache::lonnet::metadata($srcf,'keys'));
 1283: 	         }
 1284: 	      }
 1285: 	   }
 1286:         } keys %bighash;
 1287:         untie(%bighash);
 1288:     
 1289: #
 1290: # %allkeys has a list of storage and parameter displays by unikey
 1291: # %allassess has a list of all resource displays by symb
 1292: #
 1293: 
 1294:         if ($stype eq 'assesscalc') {
 1295: 	    %current=%allkeys;
 1296:         } elsif ($stype eq 'studentcalc') {
 1297:             %current=%allassess;
 1298:         }
 1299:         $updatedata{$ENV{'request.course.fn'}.'_'.$stype}=
 1300: 	    join('___;___',%current);
 1301:     } else {
 1302:         return 'Could not access course data';
 1303:     }
 1304: # ------------------------------------------------------ Get current from cache
 1305:     } else {
 1306:         %current=split(/\_\_\_\;\_\_\_/,
 1307: 		       $updatedata{$ENV{'request.course.fn'}.'_'.$stype});
 1308:     }
 1309: # -------------------- Find discrepancies between the course row table and this
 1310: #
 1311:         my %f=&getformulas($safeeval);
 1312:         my $changed=0;
 1313: 
 1314:         my $maxrow=0;
 1315:         my %existing=();
 1316: 
 1317: # ----------------------------------------------------------- Now obsolete rows
 1318: 	map {
 1319: 	    if ($_=~/^A(\d+)/) {
 1320:                 $maxrow=($1>$maxrow)?$1:$maxrow;
 1321:                 my ($usy,$ufn)=split(/\_\_\&\&\&\_\_/,$f{$_});
 1322:                 $existing{$usy}=1;
 1323: 		unless ((defined($current{$usy})) || (!$1)) {
 1324: 		   $f{$_}='!!! Obsolete';
 1325:                    $changed=1;
 1326: 	        } elsif ($ufn) {
 1327: 		    $current{$usy}
 1328:                        =~s/assesscalc\?usymb\=/assesscalc\?ufn\=$ufn\&usymb\=/;
 1329:                 }
 1330:             }
 1331:         } keys %f;
 1332: 
 1333: # -------------------------------------------------------- New and unknown keys
 1334:      
 1335:         map {
 1336:             unless ($existing{$_}) {
 1337: 		$changed=1;
 1338:                 $maxrow++;
 1339:                 $f{'A'.$maxrow}=$_;
 1340:             }
 1341:         } keys %current;        
 1342:     
 1343:         if ($changed) { &setformulas($safeeval,%f); }
 1344: 
 1345:         &setmaxrow($safeeval,$maxrow);
 1346:         &setrowlabels($safeeval,%current);
 1347:  
 1348:         undef %current;
 1349:         undef %existing;
 1350: }
 1351: 
 1352: # ------------------------------------------------ Load data for one assessment
 1353: 
 1354: sub loadstudent {
 1355:     my $safeeval=shift;
 1356:     my %c=();
 1357:     my %f=&getformulas($safeeval);
 1358:     $cachedassess=&getuname($safeeval).':'.&getudom($safeeval);
 1359:     %cachedstores=();
 1360:     {
 1361:       my $reply=&Apache::lonnet::reply('dump:'.&getudom($safeeval).':'.
 1362:                                                &getuname($safeeval).':'.
 1363:                                                &getcid($safeeval),
 1364:                                                &getuhome($safeeval));
 1365:       unless ($reply=~/^error\:/) {
 1366:          map {
 1367:             my ($name,$value)=split(/\=/,$_);
 1368:             $cachedstores{&Apache::lonnet::unescape($name)}=
 1369: 	                  &Apache::lonnet::unescape($value);
 1370:          } split(/\&/,$reply);
 1371:       }
 1372:     }
 1373:     my @assessdata=();
 1374:     map {
 1375: 	if ($_=~/^A(\d+)/) {
 1376: 	   my $row=$1;
 1377:            unless (($f{$_}=~/^\!/) || ($row==0)) {
 1378: 	      my ($usy,$ufn)=split(/\_\_\&\&\&\_\_/,$f{$_});
 1379: 	      @assessdata=&exportsheet(&getuname($safeeval),
 1380:                                        &getudom($safeeval),
 1381:                                        'assesscalc',$usy,$ufn);
 1382:               my $index=0;
 1383:               map {
 1384:                   if ($assessdata[$index]) {
 1385: 		     my $col=$_;
 1386: 		     if ($assessdata[$index]=~/\D/) {
 1387:                          $c{$col.$row}="'".$assessdata[$index]."'";
 1388:  		     } else {
 1389: 		         $c{$col.$row}=$assessdata[$index];
 1390: 		     }
 1391:                      unless ($col eq 'A') { 
 1392: 			 $f{$col.$row}='import';
 1393:                      }
 1394: 		  }
 1395:                   $index++;
 1396:               } ('A','B','C','D','E','F','G','H','I','J','K','L','M',
 1397:                  'N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
 1398: 	   }
 1399:         }
 1400:     } keys %f;
 1401:     $cachedassess='';
 1402:     undef %cachedstores;
 1403:     &setformulas($safeeval,%f);
 1404:     &setconstants($safeeval,%c);
 1405: }
 1406: 
 1407: # --------------------------------------------------- Load data for one student
 1408: 
 1409: sub loadcourse {
 1410:     my ($safeeval,$r)=@_;
 1411:     my %c=();
 1412:     my %f=&getformulas($safeeval);
 1413:     my $total=0;
 1414:     map {
 1415: 	if ($_=~/^A(\d+)/) {
 1416: 	    unless ($f{$_}=~/^\!/) { $total++; }
 1417:         }
 1418:     } keys %f;
 1419:     my $now=0;
 1420:     my $since=time;
 1421:     $r->print(<<ENDPOP);
 1422: <script>
 1423:     popwin=open('','popwin','width=400,height=100');
 1424:     popwin.document.writeln('<html><body bgcolor="#FFFFFF">'+
 1425:       '<h3>Spreadsheet Calculation Progress</h3>'+
 1426:       '<form name=popremain>'+
 1427:       '<input type=text size=35 name=remaining value=Starting></form>'+
 1428:       '</body></html>');
 1429:     popwin.document.close();
 1430: </script>
 1431: ENDPOP
 1432:     $r->rflush();
 1433:     map {
 1434: 	if ($_=~/^A(\d+)/) {
 1435: 	   my $row=$1;
 1436:            unless (($f{$_}=~/^\!/)  || ($row==0)) {
 1437: 	      my @studentdata=&exportsheet(split(/\:/,$f{$_}),
 1438:                                            'studentcalc');
 1439:               undef %userrdatas;
 1440:               $now++;
 1441:               $r->print('<script>popwin.document.popremain.remaining.value="'.
 1442:                   $now.'/'.$total.': '.int((time-$since)/$now*($total-$now)).
 1443:                         ' secs remaining";</script>');
 1444:               $r->rflush(); 
 1445: 
 1446:               my $index=0;
 1447:               map {
 1448:                   if ($studentdata[$index]) {
 1449: 		     my $col=$_;
 1450: 		     if ($studentdata[$index]=~/\D/) {
 1451:                          $c{$col.$row}="'".$studentdata[$index]."'";
 1452:  		     } else {
 1453: 		         $c{$col.$row}=$studentdata[$index];
 1454: 		     }
 1455:                      unless ($col eq 'A') { 
 1456: 			 $f{$col.$row}='import';
 1457:                      }
 1458: 		  }
 1459:                   $index++;
 1460:               } ('A','B','C','D','E','F','G','H','I','J','K','L','M',
 1461:                  'N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
 1462: 	   }
 1463:         }
 1464:     } keys %f;
 1465:     &setformulas($safeeval,%f);
 1466:     &setconstants($safeeval,%c);
 1467:     $r->print('<script>popwin.close()</script>');
 1468:     $r->rflush(); 
 1469: }
 1470: 
 1471: # ------------------------------------------------ Load data for one assessment
 1472: 
 1473: sub loadassessment {
 1474:     my $safeeval=shift;
 1475: 
 1476:     my $uhome=&getuhome($safeeval);
 1477:     my $uname=&getuname($safeeval);
 1478:     my $udom=&getudom($safeeval);
 1479:     my $symb=&getusymb($safeeval);
 1480:     my $cid=&getcid($safeeval);
 1481:     my $cnum=&getcnum($safeeval);
 1482:     my $cdom=&getcdom($safeeval);
 1483:     my $chome=&getchome($safeeval);
 1484: 
 1485:     my $namespace;
 1486:     unless ($namespace=$cid) { return ''; }
 1487: 
 1488: # ----------------------------------------------------------- Get stored values
 1489: 
 1490:    my %returnhash=();
 1491: 
 1492:    if ($cachedassess eq $uname.':'.$udom) {
 1493: #
 1494: # get data out of the dumped stores
 1495: # 
 1496: 
 1497:        my $version=$cachedstores{'version:'.$symb};
 1498:        my $scope;
 1499:        for ($scope=1;$scope<=$version;$scope++) {
 1500:            map {
 1501:                $returnhash{$_}=$cachedstores{$scope.':'.$symb.':'.$_};
 1502:            } split(/\:/,$cachedstores{$scope.':keys:'.$symb}); 
 1503:        }
 1504: 
 1505:    } else {
 1506: #
 1507: # restore individual
 1508: #
 1509: 
 1510:     my $answer=&Apache::lonnet::reply(
 1511:        "restore:$udom:$uname:".
 1512:        &Apache::lonnet::escape($namespace).":".
 1513:        &Apache::lonnet::escape($symb),$uhome);
 1514:     map {
 1515: 	my ($name,$value)=split(/\=/,$_);
 1516:         $returnhash{&Apache::lonnet::unescape($name)}=
 1517:                     &Apache::lonnet::unescape($value);
 1518:     } split(/\&/,$answer);
 1519:     my $version;
 1520:     for ($version=1;$version<=$returnhash{'version'};$version++) {
 1521:        map {
 1522:           $returnhash{$_}=$returnhash{$version.':'.$_};
 1523:        } split(/\:/,$returnhash{$version.':keys'});
 1524:     }
 1525:    }
 1526: # ----------------------------- returnhash now has all stores for this resource
 1527: 
 1528: # ---------------------------- initialize coursedata and userdata for this user
 1529:     undef %courseopt;
 1530:     undef %useropt;
 1531: 
 1532:     my $userprefix=$uname.'_'.$udom.'_';
 1533: 
 1534:     unless ($uhome eq 'no_host') { 
 1535: # -------------------------------------------------------------- Get coursedata
 1536:       unless
 1537:         ((time-$courserdatas{$cid.'.last_cache'})<240) {
 1538:          my $reply=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.
 1539:               ':resourcedata',$chome);
 1540:          if ($reply!~/^error\:/) {
 1541:             $courserdatas{$cid}=$reply;
 1542:             $courserdatas{$cid.'.last_cache'}=time;
 1543:          }
 1544:       }
 1545:       map {
 1546:          my ($name,$value)=split(/\=/,$_);
 1547:          $courseopt{$userprefix.&Apache::lonnet::unescape($name)}=
 1548:                     &Apache::lonnet::unescape($value);  
 1549:       } split(/\&/,$courserdatas{$cid});
 1550: # --------------------------------------------------- Get userdata (if present)
 1551:       unless
 1552:         ((time-$userrdatas{$uname.'___'.$udom.'.last_cache'})<240) {
 1553:          my $reply=
 1554:        &Apache::lonnet::reply('dump:'.$udom.':'.$uname.':resourcedata',$uhome);
 1555:          if ($reply!~/^error\:/) {
 1556: 	     $userrdatas{$uname.'___'.$udom}=$reply;
 1557: 	     $userrdatas{$uname.'___'.$udom.'.last_cache'}=time;
 1558:          }
 1559:       }
 1560:       map {
 1561:          my ($name,$value)=split(/\=/,$_);
 1562:          $useropt{$userprefix.&Apache::lonnet::unescape($name)}=
 1563: 	          &Apache::lonnet::unescape($value);
 1564:       } split(/\&/,$userrdatas{$uname.'___'.$udom});
 1565:     }
 1566: # ----------------- now courseopt, useropt initialized for this user and course
 1567: # (used by parmval)
 1568: 
 1569: #
 1570: # Load keys for this assessment only
 1571: #
 1572:     my %thisassess=();
 1573:     my ($symap,$syid,$srcf)=split(/\_\_\_/,$symb);
 1574:     
 1575:     map {
 1576:         $thisassess{$_}=1;
 1577:     } split(/\,/,&Apache::lonnet::metadata($srcf,'keys'));
 1578: #
 1579: # Load parameters
 1580: #
 1581:    my %c=();
 1582: 
 1583:    if (tie(%parmhash,'GDBM_File',
 1584:            &getcfn($safeeval).'_parms.db',&GDBM_READER,0640)) {
 1585:     my %f=&getformulas($safeeval);
 1586:     map {
 1587: 	if ($_=~/^A/) {
 1588:             unless ($f{$_}=~/^\!/) {
 1589:   	       if ($f{$_}=~/^parameter/) {
 1590: 		if ($thisassess{$f{$_}}) {
 1591:                   my $val=&parmval($f{$_},$safeeval);
 1592:                   $c{$_}=$val;
 1593:                   $c{$f{$_}}=$val;
 1594: 	        }
 1595: 	       } else {
 1596: 		  my $key=$f{$_};
 1597:                   my $ckey=$key;
 1598:                   $key=~s/^stores\_/resource\./;
 1599:                   $key=~s/\_/\./g;
 1600:  	          $c{$_}=$returnhash{$key};
 1601:                   $c{$ckey}=$returnhash{$key};
 1602: 	       }
 1603: 	   }
 1604:         }
 1605:     } keys %f;
 1606:     untie(%parmhash);
 1607:    }
 1608:    &setconstants($safeeval,%c);
 1609: }
 1610: 
 1611: # --------------------------------------------------------- Various form fields
 1612: 
 1613: sub textfield {
 1614:     my ($title,$name,$value)=@_;
 1615:     return "\n<p><b>$title:</b><br>".
 1616:            '<input type=text name="'.$name.'" size=80 value="'.$value.'">';
 1617: }
 1618: 
 1619: sub hiddenfield {
 1620:     my ($name,$value)=@_;
 1621:     return "\n".'<input type=hidden name="'.$name.'" value="'.$value.'">';
 1622: }
 1623: 
 1624: sub selectbox {
 1625:     my ($title,$name,$value,%options)=@_;
 1626:     my $selout="\n<p><b>$title:</b><br>".'<select name="'.$name.'">';
 1627:     map {
 1628:         $selout.='<option value="'.$_.'"';
 1629:         if ($_ eq $value) { $selout.=' selected'; }
 1630:         $selout.='>'.$options{$_}.'</option>';
 1631:     } sort keys %options;
 1632:     return $selout.'</select>';
 1633: }
 1634: 
 1635: # =============================================== Update information in a sheet
 1636: #
 1637: # Add new users or assessments, etc.
 1638: #
 1639: 
 1640: sub updatesheet {
 1641:     my $safeeval=shift;
 1642:     my $stype=&gettype($safeeval);
 1643:     if ($stype eq 'classcalc') {
 1644: 	return &updateclasssheet($safeeval);
 1645:     } else {
 1646:         return &updatestudentassesssheet($safeeval);
 1647:     }
 1648: }
 1649: 
 1650: # =================================================== Load the rows for a sheet
 1651: #
 1652: # Import the data for rows
 1653: #
 1654: 
 1655: sub loadrows {
 1656:     my ($safeeval,$r)=@_;
 1657:     my $stype=&gettype($safeeval);
 1658:     if ($stype eq 'classcalc') {
 1659: 	&loadcourse($safeeval,$r);
 1660:     } elsif ($stype eq 'studentcalc') {
 1661:         &loadstudent($safeeval);
 1662:     } else {
 1663:         &loadassessment($safeeval);
 1664:     }
 1665: }
 1666: 
 1667: # ======================================================= Forced recalculation?
 1668: 
 1669: sub checkthis {
 1670:     my ($keyname,$time)=@_;
 1671:     return ($time<$expiredates{$keyname});
 1672: }
 1673: sub forcedrecalc {
 1674:     my ($uname,$udom,$stype,$usymb)=@_;
 1675:     my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
 1676:     my $time=$oldsheets{$key.'.time'};
 1677:     if ($ENV{'form.forcerecalc'}) { return 1; }
 1678:     unless ($time) { return 1; }
 1679:     if ($stype eq 'assesscalc') {
 1680:         my $map=(split(/\_\_\_/,$usymb))[0];
 1681:         if (&checkthis('::assesscalc:',$time) ||
 1682:             &checkthis('::assesscalc:'.$map,$time) ||
 1683:             &checkthis('::assesscalc:'.$usymb,$time) ||
 1684:             &checkthis($uname.':'.$udom.':assesscalc:',$time) ||
 1685:             &checkthis($uname.':'.$udom.':assesscalc:'.$map,$time) ||
 1686:             &checkthis($uname.':'.$udom.':assesscalc:'.$usymb,$time)) {
 1687:             return 1;
 1688:         } 
 1689:     } else {
 1690:         if (&checkthis('::studentcalc:',$time) || 
 1691:             &checkthis($uname.':'.$udom.':studentcalc:',$time)) {
 1692: 	    return 1;
 1693:         }
 1694:     }
 1695:     return 0; 
 1696: }
 1697: 
 1698: # ============================================================== Export handler
 1699: #
 1700: # Non-interactive call from with program
 1701: #
 1702: 
 1703: sub exportsheet {
 1704:  my ($uname,$udom,$stype,$usymb,$fn)=@_;
 1705:  my @exportarr=();
 1706: #
 1707: # Check if cached
 1708: #
 1709: 
 1710:  my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
 1711:  my $found='';
 1712: 
 1713:  if ($oldsheets{$key}) {
 1714:      map {
 1715:          my ($name,$value)=split(/\_\_\_\=\_\_\_/,$_);
 1716:          if ($name eq $fn) {
 1717: 	     $found=$value;
 1718:          }
 1719:      } split(/\_\_\_\&\_\_\_/,$oldsheets{$key});
 1720:  }
 1721: 
 1722:  unless ($found) {
 1723:      &cachedssheets($uname,$udom,&Apache::lonnet::homeserver($uname,$udom));
 1724:      if ($oldsheets{$key}) {
 1725:         map {
 1726:             my ($name,$value)=split(/\_\_\_\=\_\_\_/,$_);
 1727:             if ($name eq $fn) {
 1728: 	        $found=$value;
 1729:             }
 1730:         } split(/\_\_\_\&\_\_\_/,$oldsheets{$key});
 1731:      }
 1732:  }
 1733: #
 1734: # Check if still valid
 1735: #
 1736:  if ($found) {
 1737:      if (&forcedrecalc($uname,$udom,$stype,$usymb)) {
 1738: 	 $found='';
 1739:      }
 1740:  }
 1741:  
 1742:  if ($found) {
 1743: #
 1744: # Return what was cached
 1745: #
 1746:      @exportarr=split(/\_\_\_\;\_\_\_/,$found);
 1747: 
 1748:  } else {
 1749: #
 1750: # Not cached
 1751: #        
 1752: 
 1753:     my $thissheet=&makenewsheet($uname,$udom,$stype,$usymb);
 1754:     &readsheet($thissheet,$fn);
 1755:     &updatesheet($thissheet);
 1756:     &loadrows($thissheet);
 1757:     &calcsheet($thissheet); 
 1758:     @exportarr=&exportdata($thissheet);
 1759: #
 1760: # Store now
 1761: #
 1762:     my $cid=$ENV{'request.course.id'}; 
 1763:     my $current='';
 1764:     if ($stype eq 'studentcalc') {
 1765:        $current=&Apache::lonnet::reply('get:'.
 1766:                                      $ENV{'course.'.$cid.'.domain'}.':'.
 1767:                                      $ENV{'course.'.$cid.'.num'}.
 1768: 				     ':nohist_calculatedsheets:'.
 1769:                                      &Apache::lonnet::escape($key),
 1770:                                      $ENV{'course.'.$cid.'.home'});
 1771:     } else {
 1772:        $current=&Apache::lonnet::reply('get:'.
 1773:                                      &getudom($thissheet).':'.
 1774:                                      &getuname($thissheet).
 1775: 				     ':nohist_calculatedsheets_'.
 1776:                                      $ENV{'request.course.id'}.':'.
 1777:                                      &Apache::lonnet::escape($key),
 1778:                                      &getuhome($thissheet));
 1779: 
 1780:     }
 1781:     my %currentlystored=();
 1782:     unless ($current=~/^error\:/) {
 1783:        map {
 1784:            my ($name,$value)=split(/\_\_\_\=\_\_\_/,$_);
 1785:            $currentlystored{$name}=$value;
 1786:        } split(/\_\_\_\&\_\_\_/,&Apache::lonnet::unescape($current));
 1787:     }
 1788:     $currentlystored{$fn}=join('___;___',@exportarr);
 1789: 
 1790:     my $newstore='';
 1791:     map {
 1792:         if ($newstore) { $newstore.='___&___'; }
 1793:         $newstore.=$_.'___=___'.$currentlystored{$_};
 1794:     } keys %currentlystored;
 1795:     my $now=time;
 1796:     if ($stype eq 'studentcalc') {
 1797:        &Apache::lonnet::reply('put:'.
 1798:                          $ENV{'course.'.$cid.'.domain'}.':'.
 1799:                          $ENV{'course.'.$cid.'.num'}.
 1800: 			 ':nohist_calculatedsheets:'.
 1801:                          &Apache::lonnet::escape($key).'='.
 1802: 			 &Apache::lonnet::escape($newstore).'&'.
 1803:                          &Apache::lonnet::escape($key).'.time='.$now,
 1804:                          $ENV{'course.'.$cid.'.home'});
 1805:    } else {
 1806:        &Apache::lonnet::reply('put:'.
 1807:                          &getudom($thissheet).':'.
 1808:                          &getuname($thissheet).
 1809: 			 ':nohist_calculatedsheets_'.
 1810:                          $ENV{'request.course.id'}.':'.
 1811:                          &Apache::lonnet::escape($key).'='.
 1812: 			 &Apache::lonnet::escape($newstore).'&'.
 1813:                          &Apache::lonnet::escape($key).'.time='.$now,
 1814:                          &getuhome($thissheet));
 1815:    }
 1816:  }
 1817:  return @exportarr;
 1818: }
 1819: # ============================================================ Expiration Dates
 1820: #
 1821: # Load previously cached student spreadsheets for this course
 1822: #
 1823: 
 1824: sub expirationdates {
 1825:     undef %expiredates;
 1826:     my $cid=$ENV{'request.course.id'};
 1827:     my $reply=&Apache::lonnet::reply('dump:'.
 1828: 				     $ENV{'course.'.$cid.'.domain'}.':'.
 1829:                                      $ENV{'course.'.$cid.'.num'}.
 1830: 				     ':nohist_expirationdates',
 1831:                                      $ENV{'course.'.$cid.'.home'});
 1832:     unless ($reply=~/^error\:/) {
 1833: 	map {
 1834:             my ($name,$value)=split(/\=/,$_);
 1835:             $expiredates{&Apache::lonnet::unescape($name)}
 1836:                         =&Apache::lonnet::unescape($value);
 1837:         } split(/\&/,$reply);
 1838:     }
 1839: }
 1840: 
 1841: # ===================================================== Calculated sheets cache
 1842: #
 1843: # Load previously cached student spreadsheets for this course
 1844: #
 1845: 
 1846: sub cachedcsheets {
 1847:     my $cid=$ENV{'request.course.id'};
 1848:     my $reply=&Apache::lonnet::reply('dump:'.
 1849: 				     $ENV{'course.'.$cid.'.domain'}.':'.
 1850:                                      $ENV{'course.'.$cid.'.num'}.
 1851: 				     ':nohist_calculatedsheets',
 1852:                                      $ENV{'course.'.$cid.'.home'});
 1853:     unless ($reply=~/^error\:/) {
 1854: 	map {
 1855:             my ($name,$value)=split(/\=/,$_);
 1856:             $oldsheets{&Apache::lonnet::unescape($name)}
 1857:                       =&Apache::lonnet::unescape($value);
 1858:         } split(/\&/,$reply);
 1859:     }
 1860: }
 1861: 
 1862: # ===================================================== Calculated sheets cache
 1863: #
 1864: # Load previously cached assessment spreadsheets for this student
 1865: #
 1866: 
 1867: sub cachedssheets {
 1868:   my ($sname,$sdom,$shome)=@_;
 1869:   unless (($loadedcaches{$sname.'_'.$sdom}) || ($shome eq 'no_host')) {
 1870:     my $cid=$ENV{'request.course.id'};
 1871:     my $reply=&Apache::lonnet::reply('dump:'.$sdom.':'.$sname.
 1872: 			             ':nohist_calculatedsheets_'.
 1873:                                       $ENV{'request.course.id'},
 1874:                                      $shome);
 1875:     unless ($reply=~/^error\:/) {
 1876: 	map {
 1877:             my ($name,$value)=split(/\=/,$_);
 1878:             $oldsheets{&Apache::lonnet::unescape($name)}
 1879:                       =&Apache::lonnet::unescape($value);
 1880:         } split(/\&/,$reply);
 1881:     }
 1882:     $loadedcaches{$sname.'_'.$sdom}=1;
 1883:   }
 1884: }
 1885: 
 1886: # ===================================================== Calculated sheets cache
 1887: #
 1888: # Load previously cached assessment spreadsheets for this student
 1889: #
 1890: 
 1891: # ================================================================ Main handler
 1892: #
 1893: # Interactive call to screen
 1894: #
 1895: #
 1896: 
 1897: 
 1898: sub handler {
 1899:     my $r=shift;
 1900: 
 1901:     if ($r->header_only) {
 1902:       $r->content_type('text/html');
 1903:       $r->send_http_header;
 1904:       return OK;
 1905:     }
 1906: 
 1907: # ---------------------------------------------------- Global directory configs
 1908: 
 1909: $includedir=$r->dir_config('lonIncludes');
 1910: $tmpdir=$r->dir_config('lonDaemons').'/tmp/';
 1911: 
 1912: # ----------------------------------------------------- Needs to be in a course
 1913: 
 1914:   if ($ENV{'request.course.fn'}) { 
 1915: 
 1916: # --------------------------- Get query string for limited number of parameters
 1917: 
 1918:     map {
 1919:        my ($name, $value) = split(/=/,$_);
 1920:        $value =~ tr/+/ /;
 1921:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
 1922:        if (($name eq 'uname') || ($name eq 'udom') || 
 1923:            ($name eq 'usymb') || ($name eq 'ufn')) {
 1924:            unless ($ENV{'form.'.$name}) {
 1925:               $ENV{'form.'.$name}=$value;
 1926: 	   }
 1927:        }
 1928:     } (split(/&/,$ENV{'QUERY_STRING'}));
 1929: 
 1930: # -------------------------------------- Interactive loading of specific sheet?
 1931:     if (($ENV{'form.load'}) && ($ENV{'form.loadthissheet'} ne 'Default')) {
 1932: 	$ENV{'form.ufn'}=$ENV{'form.loadthissheet'};
 1933:     }
 1934: # ------------------------------------------- Nothing there? Must be login user
 1935: 
 1936:     my $aname;
 1937:     my $adom;
 1938: 
 1939:     unless ($ENV{'form.uname'}) {
 1940: 	$aname=$ENV{'user.name'};
 1941:         $adom=$ENV{'user.domain'};
 1942:     } else {
 1943:         $aname=$ENV{'form.uname'};
 1944:         $adom=$ENV{'form.udom'};
 1945:     }
 1946: 
 1947: # ------------------------------------------------------------------- Open page
 1948: 
 1949:     $r->content_type('text/html');
 1950:     $r->header_out('Cache-control','no-cache');
 1951:     $r->header_out('Pragma','no-cache');
 1952:     $r->send_http_header;
 1953: 
 1954: # --------------------------------------------------------------- Screen output
 1955: 
 1956:     $r->print('<html><head><title>LON-CAPA Spreadsheet</title>');
 1957:     $r->print(<<ENDSCRIPT);
 1958: <script language="JavaScript">
 1959: 
 1960:     function celledit(cn,cf) {
 1961:         var cnf=prompt(cn,cf);
 1962: 	if (cnf!=null) {
 1963: 	    document.sheet.unewfield.value=cn;
 1964:             document.sheet.unewformula.value=cnf;
 1965:             document.sheet.submit();
 1966:         }
 1967:     }
 1968: 
 1969:     function changesheet(cn) {
 1970: 	document.sheet.unewfield.value=cn;
 1971:         document.sheet.unewformula.value='changesheet';
 1972:         document.sheet.submit();
 1973:     }
 1974: 
 1975: </script>
 1976: ENDSCRIPT
 1977:     $r->print('</head><body bgcolor="#FFFFFF">'.
 1978:        '<img align=right src=/adm/lonIcons/lonlogos.gif>'.
 1979:        '<h1>LON-CAPA Spreadsheet</h1>'.
 1980:        '<form action="'.$r->uri.'" name=sheet method=post>'.
 1981:        &hiddenfield('uname',$ENV{'form.uname'}).
 1982:        &hiddenfield('udom',$ENV{'form.udom'}).
 1983:        &hiddenfield('usymb',$ENV{'form.usymb'}).
 1984:        &hiddenfield('unewfield','').
 1985:        &hiddenfield('unewformula',''));
 1986: 
 1987: # ---------------------- Make sure that this gets out, even if user hits "stop"
 1988: 
 1989:     $r->rflush();
 1990: 
 1991: # ---------------------------------------------------------------- Full recalc?
 1992: 
 1993: 
 1994:     if ($ENV{'form.forcerecalc'}) {
 1995: 	$r->print('<h4>Completely Recalculating Sheet ...</h4>');
 1996:         undef %spreadsheets;
 1997:         undef %courserdatas;
 1998:         undef %userrdatas;
 1999:         undef %defaultsheets;
 2000:         undef %updatedata;
 2001:    }
 2002:  
 2003: # ---------------------------------------- Read new sheet or modified worksheet
 2004: 
 2005:     $r->uri=~/\/(\w+)$/;
 2006: 
 2007:     my $asheet=&makenewsheet($aname,$adom,$1,$ENV{'form.usymb'});
 2008: 
 2009: # ------------------------ If a new formula had been entered, go from work copy
 2010: 
 2011:     if ($ENV{'form.unewfield'}) {
 2012:         $r->print('<h2>Modified Workcopy</h2>');
 2013:         $ENV{'form.unewformula'}=~s/\'/\"/g;
 2014:         $r->print('<p>New formula: '.$ENV{'form.unewfield'}.'='.
 2015:                   $ENV{'form.unewformula'}.'<p>');
 2016:         &setfilename($asheet,$ENV{'form.ufn'});
 2017: 	&tmpread($asheet,
 2018:                  $ENV{'form.unewfield'},$ENV{'form.unewformula'});
 2019: 
 2020:      } elsif ($ENV{'form.saveas'}) {
 2021:         &setfilename($asheet,$ENV{'form.ufn'});
 2022: 	&tmpread($asheet);
 2023:     } else {
 2024:         &readsheet($asheet,$ENV{'form.ufn'});
 2025:     }
 2026: 
 2027: # -------------------------------------------------- Print out user information
 2028: 
 2029:     unless (&gettype($asheet) eq 'classcalc') {
 2030:         $r->print('<p><b>User:</b> '.&getuname($asheet).
 2031:                   '<br><b>Domain:</b> '.&getudom($asheet));
 2032:         if (&getcsec($asheet) eq '-1') {
 2033:            $r->print('<h3><font color=red>'.
 2034:                      'Not a student in this course</font></h3>');
 2035:         } else {
 2036:            $r->print('<br><b>Section/Group:</b> '.&getcsec($asheet));
 2037:         }
 2038:     }
 2039: 
 2040: # ---------------------------------------------------------------- Course title
 2041: 
 2042:     $r->print('<h1>'.
 2043:             $ENV{'course.'.$ENV{'request.course.id'}.'.description'}.
 2044:              '</h1><h3>'.localtime().'</h3>');
 2045: 
 2046: # ---------------------------------------------------- See if user can see this
 2047: 
 2048:     if ((&gettype($asheet) eq 'classcalc') || 
 2049:         (&getuname($asheet) ne $ENV{'user.name'}) ||
 2050:         (&getudom($asheet) ne $ENV{'user.domain'})) {
 2051:         unless (&Apache::lonnet::allowed('vgr',&getcid($asheet))) {
 2052: 	    $r->print(
 2053:            '<h1>Access Permission Denied</h1></form></body></html>');
 2054:             return OK;
 2055:         }
 2056:     }
 2057: 
 2058: # ---------------------------------------------------------- Additional options
 2059: 
 2060:     $r->print(
 2061:  '<input type=submit name=forcerecalc value="Completely Recalculate Sheet"><p>'
 2062: 		 );
 2063:     if (&gettype($asheet) eq 'assesscalc') {
 2064:        $r->print ('<p><font size=+2><a href="/adm/studentcalc?uname='.
 2065:                                                &getuname($asheet).
 2066:                                                '&udom='.&getudom($asheet).
 2067:                   '">Level up: Student Sheet</a></font><p>');
 2068:     }
 2069:     
 2070:     if ((&gettype($asheet) eq 'studentcalc') && 
 2071:         (&Apache::lonnet::allowed('vgr',&getcid($asheet)))) {
 2072:        $r->print (
 2073:                    '<p><font size=+2><a href="/adm/classcalc">'.
 2074:                    'Level up: Course Sheet</a></font><p>');
 2075:     }
 2076:     
 2077: 
 2078: # ----------------------------------------------------------------- Save dialog
 2079: 
 2080: 
 2081:     if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
 2082:         my $fname=$ENV{'form.ufn'};
 2083:         $fname=~s/\_[^\_]+$//;
 2084:         if ($fname eq 'default') { $fname='course_default'; }
 2085:         $r->print('<input type=submit name=saveas value="Save as ...">'.
 2086:               '<input type=text size=20 name=newfn value="'.$fname.
 2087:               '"> (make default: <input type=checkbox name="makedefufn">)<p>');
 2088:     }
 2089: 
 2090:     $r->print(&hiddenfield('ufn',&getfilename($asheet)));
 2091: 
 2092: # ----------------------------------------------------------------- Load dialog
 2093:     if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
 2094: 	$r->print('<p><input type=submit name=load value="Load ...">'.
 2095:                   '<select name="loadthissheet">'.
 2096:                   '<option name="default">Default</option>');
 2097:         map {
 2098: 	    $r->print('<option name="'.$_.'"');
 2099:             if ($ENV{'form.ufn'} eq $_) {
 2100:                $r->print(' selected');
 2101:             }
 2102:             $r->print('>'.$_.'</option>');
 2103:         } &othersheets($asheet,&gettype($asheet));
 2104:         $r->print('</select><p>');
 2105:         if (&gettype($asheet) eq 'studentcalc') {
 2106: 	    &setothersheets($asheet,&othersheets($asheet,'assesscalc'));
 2107:         }
 2108:     }
 2109: 
 2110: # --------------------------------------------------------------- Cached sheets
 2111: 
 2112:     &expirationdates();
 2113: 
 2114:     undef %oldsheets;
 2115:     undef %loadedcaches;
 2116: 
 2117:     if (&gettype($asheet) eq 'classcalc') {
 2118:         $r->print("Loading previously calculated student sheets ...<br>\n");
 2119:         $r->rflush();
 2120:         &cachedcsheets();
 2121:     } elsif (&gettype($asheet) eq 'studentcalc') {
 2122:         $r->print("Loading previously calculated assessment sheets ...<br>\n");
 2123:         $r->rflush();
 2124:         &cachedssheets(&getuname($asheet),&getudom($asheet),
 2125:                        &getuhome($asheet));
 2126:     }
 2127: 
 2128: # ----------------------------------------------------- Update sheet, load rows
 2129: 
 2130:     $r->print("Loaded sheet(s), updating rows ...<br>\n");
 2131:     $r->rflush();
 2132: 
 2133:     &updatesheet($asheet);
 2134: 
 2135:     $r->print("Updated rows, loading row data ...<br>\n");
 2136:     $r->rflush();
 2137: 
 2138:     &loadrows($asheet,$r);
 2139: 
 2140:     $r->print("Loaded row data, calculating sheet ...<br>\n");
 2141:     $r->rflush();
 2142: 
 2143:     my $calcoutput=&calcsheet($asheet);
 2144:     $r->print('<h3><font color=red>'.$calcoutput.'</h3></font>');
 2145: 
 2146: # ---------------------------------------------------- See if something to save
 2147: 
 2148:     if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
 2149:         my $fname='';
 2150: 	if ($ENV{'form.saveas'} && ($fname=$ENV{'form.newfn'})) {
 2151:             $fname=~s/\W/\_/g;
 2152:             if ($fname eq 'default') { $fname='course_default'; }
 2153:             $fname.='_'.&gettype($asheet);
 2154:             &setfilename($asheet,$fname);
 2155:             $ENV{'form.ufn'}=$fname;
 2156: 	    $r->print('<p>Saving spreadsheet: '.
 2157:                          &writesheet($asheet,$ENV{'form.makedefufn'}).'<p>');
 2158: 	}
 2159:     }
 2160: 
 2161: # ------------------------------------------------ Write the modified worksheet
 2162: 
 2163:    $r->print('<b>Current sheet:</b> '.&getfilename($asheet).'<p>');
 2164: 
 2165:    &tmpwrite($asheet);
 2166: 
 2167:     if (&gettype($asheet) eq 'studentcalc') {
 2168: 	$r->print('<br>Show rows with empty A column: ');
 2169:     } else {
 2170:         $r->print('<br>Show empty rows: ');
 2171:     } 
 2172:     $r->print('<input type=checkbox name=showall onClick="submit()"');
 2173:     if ($ENV{'form.showall'}) { $r->print(' checked'); }
 2174:     $r->print('>');
 2175:     if (&gettype($asheet) eq 'classcalc') {
 2176:        $r->print(
 2177:    ' Output CSV format: <input type=checkbox name=showcsv onClick="submit()"');
 2178:        if ($ENV{'form.showcsv'}) { $r->print(' checked'); }
 2179:        $r->print('>');
 2180:     }
 2181: # ------------------------------------------------------------- Print out sheet
 2182: 
 2183:     &outsheet($r,$asheet);
 2184:     $r->print('</form></body></html>');
 2185: 
 2186: # ------------------------------------------------------------------------ Done
 2187:   } else {
 2188: # ----------------------------- Not in a course, or not allowed to modify parms
 2189:       $ENV{'user.error.msg'}=
 2190:         $r->uri.":opa:0:0:Cannot modify spreadsheet";
 2191:       return HTTP_NOT_ACCEPTABLE; 
 2192:   }
 2193:     return OK;
 2194: 
 2195: }
 2196: 
 2197: 1;
 2198: __END__

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