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