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