Annotation of loncom/interface/lonspreadsheet.pm, revision 1.59
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.59 ! www 9: # 07/09,07/14,07/21,09/01 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.21 www 673: unless ($n eq '-') {
674: $defaultbg=((($n-1)/5)==int(($n-1)/5))?'#E0E0':'#FFFF';
675: } else {
676: $defaultbg='#E0FF';
677: }
1.24 www 678: if ((($n-1)/25)==int(($n-1)/25)) {
679: my $what='Student';
680: if (&gettype($safeeval) eq 'assesscalc') {
681: $what='Item';
682: } elsif (&gettype($safeeval) eq 'studentcalc') {
683: $what='Assessment';
684: }
685: $rowdata.="</table>\n<br><table border=2>".
686: '<tr><td> <td>'.$what.'</td>';
687: map {
688: $rowdata.='<td>'.$_.'</td>';
689: } ('A','B','C','D','E','F','G','H','I','J','K','L','M',
690: 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
691: 'a','b','c','d','e','f','g','h','i','j','k','l','m',
692: 'n','o','p','q','r','s','t','u','v','w','x','y','z');
693: $rowdata.='</tr>';
694: }
695: $rowdata.="\n<tr><td><b><font size=+1>$n</font></b></td>";
1.6 www 696: my $showf=0;
1.16 www 697: my $proc;
1.18 www 698: my $maxred;
1.55 www 699: if (&gettype($safeeval) eq 'studentcalc') {
700: $proc='&outrowassess';
701: $maxred=26;
702: } else {
703: $proc='&outrow';
704: }
1.16 www 705: if (&gettype($safeeval) eq 'assesscalc') {
1.18 www 706: $maxred=1;
1.16 www 707: } else {
1.18 www 708: $maxred=26;
1.16 www 709: }
1.21 www 710: if ($n eq '-') { $proc='&templaterow'; $n=-1; }
1.6 www 711: map {
1.9 www 712: my $bgcolor=$defaultbg.((($showf-1)/5==int(($showf-1)/5))?'99':'DD');
1.6 www 713: my ($fm,$vl)=split(/\_\_\_eq\_\_\_/,$_);
714: if ($showf==0) { $vl=$_; }
1.18 www 715: if ($showf<=$maxred) { $bgcolor='#FFDDDD'; }
1.9 www 716: if (($n==0) && ($showf<=26)) { $bgcolor='#CCCCFF'; }
1.18 www 717: if (($showf>$maxred) || ((!$n) && ($showf>0))) {
1.6 www 718: if ($vl eq '') {
1.9 www 719: $vl='<font size=+2 color='.$bgcolor.'>#</font>';
1.6 www 720: }
721: $rowdata.=
1.10 www 722: '<td bgcolor='.$bgcolor.'><a href="javascript:celledit('.$fm.');">'.$vl.
1.6 www 723: '</a></td>';
724: } else {
1.9 www 725: $rowdata.='<td bgcolor='.$bgcolor.'> '.$vl.' </td>';
1.6 www 726: }
727: $showf++;
1.16 www 728: } $safeeval->reval($proc.'('.$n.')');
1.6 www 729: return $rowdata.'</tr>';
730: }
731:
732: # ------------------------------------------------------------- Print out sheet
733:
734: sub outsheet {
1.24 www 735: my ($r,$safeeval)=@_;
1.18 www 736: my $maxred;
737: my $realm;
738: if (&gettype($safeeval) eq 'assesscalc') {
739: $maxred=1;
740: $realm='Assessment';
741: } elsif (&gettype($safeeval) eq 'studentcalc') {
742: $maxred=26;
743: $realm='User';
744: } else {
745: $maxred=26;
746: $realm='Course';
747: }
748: my $maxyellow=52-$maxred;
1.24 www 749: my $tabledata=
750: '<table border=2><tr><th colspan=2 rowspan=2><font size=+2>'.
1.18 www 751: $realm.'</font></th>'.
752: '<td bgcolor=#FFDDDD colspan='.$maxred.
753: '><b><font size=+1>Import</font></b></td>'.
754: '<td colspan='.$maxyellow.
755: '><b><font size=+1>Calculations</font></b></td></tr><tr>';
756: my $showf=0;
1.6 www 757: map {
1.18 www 758: $showf++;
759: if ($showf<=$maxred) {
760: $tabledata.='<td bgcolor="#FFDDDD">';
761: } else {
762: $tabledata.='<td>';
763: }
764: $tabledata.="<b><font size=+1>$_</font></b></td>";
765: } ('A','B','C','D','E','F','G','H','I','J','K','L','M',
1.7 www 766: 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
767: 'a','b','c','d','e','f','g','h','i','j','k','l','m',
768: 'n','o','p','q','r','s','t','u','v','w','x','y','z');
1.6 www 769: $tabledata.='</tr>';
770: my $row;
771: my $maxrow=&getmaxrow($safeeval);
1.21 www 772: $tabledata.=&rown($safeeval,'-');
1.24 www 773: $r->print($tabledata);
1.6 www 774: for ($row=0;$row<=$maxrow;$row++) {
1.24 www 775: $r->print(&rown($safeeval,$row));
1.6 www 776: }
1.24 www 777: $r->print('</table>');
1.6 www 778: }
779:
1.27 www 780: #
1.55 www 781: # ----------------------------------------------- Read list of available sheets
782: #
783:
784: sub othersheets {
785: my ($safeeval,$stype)=@_;
786:
787: my $cnum=&getcnum($safeeval);
788: my $cdom=&getcdom($safeeval);
789: my $chome=&getchome($safeeval);
790:
791: my @alternatives=();
792: my $result=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.':'.
793: $stype.'_spreadsheets',$chome);
794: if ($result!~/^error\:/) {
795: map {
796: $alternatives[$#alternatives+1]=
797: &Apache::lonnet::unescape((split(/\=/,$_))[0]);
798: } split(/\&/,$result);
799: }
800: return @alternatives;
801: }
802:
803: #
1.27 www 804: # -------------------------------------- Read spreadsheet formulas for a course
805: #
806:
807: sub readsheet {
808: my ($safeeval,$fn)=@_;
809: my $stype=&gettype($safeeval);
810: my $cnum=&getcnum($safeeval);
1.28 www 811: my $cdom=&getcdom($safeeval);
812: my $chome=&getchome($safeeval);
1.27 www 813:
814: # --------- There is no filename. Look for defaults in course and global, cache
815:
816: unless($fn) {
1.28 www 817: unless ($fn=$defaultsheets{$cnum.'_'.$cdom.'_'.$stype}) {
818: $fn=&Apache::lonnet::reply('get:'.$cdom.':'.$cnum.
819: ':environment:spreadsheet_default_'.$stype,
820: $chome);
1.27 www 821: unless (($fn) && ($fn!~/^error\:/)) {
822: $fn='default_'.$stype;
823: }
1.28 www 824: $defaultsheets{$cnum.'_'.$cdom.'_'.$stype}=$fn;
1.27 www 825: }
826: }
827:
828: # ---------------------------------------------------------- fn now has a value
829:
830: &setfilename($safeeval,$fn);
831:
832: # ------------------------------------------------------ see if sheet is cached
833: my $fstring='';
1.28 www 834: if ($fstring=$spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}) {
835: &setformulas($safeeval,split(/\_\_\_\;\_\_\_/,$fstring));
1.27 www 836: } else {
1.6 www 837:
1.27 www 838: # ---------------------------------------------------- Not cached, need to read
1.5 www 839:
1.27 www 840: my %f=();
1.3 www 841:
1.27 www 842: if ($fn=~/^default\_/) {
1.19 www 843: my $sheetxml='';
1.10 www 844: {
845: my $fh;
1.28 www 846: if ($fh=Apache::File->new($includedir.
1.19 www 847: '/default.'.&gettype($safeeval))) {
848: $sheetxml=join('',<$fh>);
849: }
850: }
1.27 www 851: my $parser=HTML::TokeParser->new(\$sheetxml);
852: my $token;
853: while ($token=$parser->get_token) {
1.19 www 854: if ($token->[0] eq 'S') {
855: if ($token->[1] eq 'field') {
856: $f{$token->[2]->{'col'}.$token->[2]->{'row'}}=
857: $parser->get_text('/field');
858: }
1.20 www 859: if ($token->[1] eq 'template') {
860: $f{'template_'.$token->[2]->{'col'}}=
861: $parser->get_text('/template');
862: }
1.19 www 863: }
1.27 www 864: }
865: } else {
1.28 www 866: my $sheet='';
867: my $reply=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.':'.$fn,
868: $chome);
1.19 www 869: unless ($reply=~/^error\:/) {
1.27 www 870: $sheet=$reply;
871: }
872: map {
873: my ($name,$value)=split(/\=/,$_);
874: $f{&Apache::lonnet::unescape($name)}=
875: &Apache::lonnet::unescape($value);
876: } split(/\&/,$sheet);
1.10 www 877: }
1.27 www 878: # --------------------------------------------------------------- Cache and set
1.28 www 879: $spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}=join('___;___',%f);
1.27 www 880: &setformulas($safeeval,%f);
1.3 www 881: }
882: }
883:
1.28 www 884: # -------------------------------------------------------- Make new spreadsheet
885:
886: sub makenewsheet {
887: my ($uname,$udom,$stype,$usymb)=@_;
1.36 www 888: my $safeeval=initsheet($stype);
1.28 www 889: $safeeval->reval(
1.29 www 890: '$uname="'.$uname.
891: '";$udom="'.$udom.
892: '";$uhome="'.&Apache::lonnet::homeserver($uname,$udom).
893: '";$sheettype="'.$stype.
894: '";$usymb="'.$usymb.
1.30 www 895: '";$csec="'.&Apache::lonnet::usection($udom,$uname,
896: $ENV{'request.course.id'}).
1.29 www 897: '";$cid="'.$ENV{'request.course.id'}.
898: '";$cfn="'.$ENV{'request.course.fn'}.
899: '";$cnum="'.$ENV{'course.'.$ENV{'request.course.id'}.'.num'}.
900: '";$cdom="'.$ENV{'course.'.$ENV{'request.course.id'}.'.domain'}.
901: '";$chome="'.$ENV{'course.'.$ENV{'request.course.id'}.'.home'}.'";');
1.28 www 902: return $safeeval;
903: }
904:
1.19 www 905: # ------------------------------------------------------------ Save spreadsheet
906:
907: sub writesheet {
1.28 www 908: my ($safeeval,$makedef)=@_;
909: my $cid=&getcid($safeeval);
910: if (&Apache::lonnet::allowed('opa',$cid)) {
1.19 www 911: my %f=&getformulas($safeeval);
1.28 www 912: my $stype=&gettype($safeeval);
913: my $cnum=&getcnum($safeeval);
914: my $cdom=&getcdom($safeeval);
915: my $chome=&getchome($safeeval);
916: my $fn=&getfilename($safeeval);
917:
918: # ------------------------------------------------------------- Cache new sheet
919: $spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}=join('___;___',%f);
920: # ----------------------------------------------------------------- Write sheet
1.19 www 921: my $sheetdata='';
922: map {
1.56 www 923: unless ($f{$_} eq 'import') {
1.19 www 924: $sheetdata.=&Apache::lonnet::escape($_).'='.
925: &Apache::lonnet::escape($f{$_}).'&';
1.56 www 926: }
1.19 www 927: } keys %f;
928: $sheetdata=~s/\&$//;
1.28 www 929: my $reply=&Apache::lonnet::reply('put:'.$cdom.':'.$cnum.':'.$fn.':'.
930: $sheetdata,$chome);
1.22 www 931: if ($reply eq 'ok') {
1.28 www 932: $reply=&Apache::lonnet::reply('put:'.$cdom.':'.$cnum.':'.
1.30 www 933: $stype.'_spreadsheets:'.
1.55 www 934: &Apache::lonnet::escape($fn).'='.$ENV{'user.name'}.'@'.
935: $ENV{'user.domain'},
1.28 www 936: $chome);
937: if ($reply eq 'ok') {
938: if ($makedef) {
939: return &Apache::lonnet::reply('put:'.$cdom.':'.$cnum.
940: ':environment:spreadsheet_default_'.$stype.'='.
941: &Apache::lonnet::escape($fn),
942: $chome);
943: } else {
944: return $reply;
945: }
946: } else {
947: return $reply;
948: }
1.22 www 949: } else {
950: return $reply;
951: }
952: }
953: return 'unauthorized';
1.19 www 954: }
955:
1.10 www 956: # ----------------------------------------------- Make a temp copy of the sheet
1.28 www 957: # "Modified workcopy" - interactive only
958: #
1.10 www 959:
960: sub tmpwrite {
1.28 www 961: my $safeeval=shift;
962: my $fn=$ENV{'user.name'}.'_'.
963: $ENV{'user.domain'}.'_spreadsheet_'.&getusymb($safeeval).'_'.
964: &getfilename($safeeval);
1.10 www 965: $fn=~s/\W/\_/g;
966: $fn=$tmpdir.$fn.'.tmp';
967: my $fh;
968: if ($fh=Apache::File->new('>'.$fn)) {
969: print $fh join("\n",&getformulas($safeeval));
970: }
971: }
972:
973: # ---------------------------------------------------------- Read the temp copy
974:
975: sub tmpread {
1.28 www 976: my ($safeeval,$nfield,$nform)=@_;
977: my $fn=$ENV{'user.name'}.'_'.
978: $ENV{'user.domain'}.'_spreadsheet_'.&getusymb($safeeval).'_'.
979: &getfilename($safeeval);
1.10 www 980: $fn=~s/\W/\_/g;
981: $fn=$tmpdir.$fn.'.tmp';
982: my $fh;
983: my %fo=();
984: if ($fh=Apache::File->new($fn)) {
985: my $name;
986: while ($name=<$fh>) {
987: chomp($name);
988: my $value=<$fh>;
989: chomp($value);
990: $fo{$name}=$value;
991: }
992: }
1.55 www 993: if ($nform eq 'changesheet') {
1.57 www 994: $fo{'A'.$nfield}=(split(/\_\_\&\&\&\_\_/,$fo{'A'.$nfield}))[0];
1.55 www 995: unless ($ENV{'form.sel_'.$nfield} eq 'Default') {
1.57 www 996: $fo{'A'.$nfield}.='__&&&__'.$ENV{'form.sel_'.$nfield};
1.55 www 997: }
998: } else {
999: if ($nfield) { $fo{$nfield}=$nform; }
1000: }
1.10 www 1001: &setformulas($safeeval,%fo);
1002: }
1003:
1.11 www 1004: # ================================================================== Parameters
1005: # -------------------------------------------- Figure out a cascading parameter
1.28 www 1006: #
1.29 www 1007: # For this function to work
1008: #
1009: # * parmhash needs to be tied
1010: # * courseopt and useropt need to be initialized for this user and course
1011: #
1.11 www 1012:
1013: sub parmval {
1.28 www 1014: my ($what,$safeeval)=@_;
1015: my $cid=&getcid($safeeval);
1016: my $csec=&getcsec($safeeval);
1017: my $uname=&getuname($safeeval);
1018: my $udom=&getudom($safeeval);
1019: my $symb=&getusymb($safeeval);
1.11 www 1020:
1021: unless ($symb) { return ''; }
1022: my $result='';
1023:
1024: my ($mapname,$id,$fn)=split(/\_\_\_/,$symb);
1025: # ----------------------------------------------------- Cascading lookup scheme
1.12 www 1026: my $rwhat=$what;
1027: $what=~s/^parameter\_//;
1028: $what=~s/\_/\./;
1.11 www 1029:
1030: my $symbparm=$symb.'.'.$what;
1031: my $mapparm=$mapname.'___(all).'.$what;
1.29 www 1032: my $usercourseprefix=$uname.'_'.$udom.'_'.$cid;
1.11 www 1033:
1034: my $seclevel=
1.28 www 1035: $usercourseprefix.'.['.
1.11 www 1036: $csec.'].'.$what;
1037: my $seclevelr=
1.28 www 1038: $usercourseprefix.'.['.
1.11 www 1039: $csec.'].'.$symbparm;
1040: my $seclevelm=
1.28 www 1041: $usercourseprefix.'.['.
1.11 www 1042: $csec.'].'.$mapparm;
1043:
1044: my $courselevel=
1.28 www 1045: $usercourseprefix.'.'.$what;
1.11 www 1046: my $courselevelr=
1.28 www 1047: $usercourseprefix.'.'.$symbparm;
1.11 www 1048: my $courselevelm=
1.28 www 1049: $usercourseprefix.'.'.$mapparm;
1.12 www 1050:
1.11 www 1051: # ---------------------------------------------------------- fourth, check user
1052:
1053: if ($uname) {
1054:
1055: if ($useropt{$courselevelr}) { return $useropt{$courselevelr}; }
1056:
1057: if ($useropt{$courselevelm}) { return $useropt{$courselevelm}; }
1058:
1059: if ($useropt{$courselevel}) { return $useropt{$courselevel}; }
1060:
1061: }
1062:
1063: # --------------------------------------------------------- third, check course
1064:
1065: if ($csec) {
1066:
1067: if ($courseopt{$seclevelr}) { return $courseopt{$seclevelr}; }
1068:
1069: if ($courseopt{$seclevelm}) { return $courseopt{$seclevelm}; }
1070:
1071: if ($courseopt{$seclevel}) { return $courseopt{$seclevel}; }
1072:
1073: }
1074:
1075: if ($courseopt{$courselevelr}) { return $courseopt{$courselevelr}; }
1076:
1077: if ($courseopt{$courselevelm}) { return $courseopt{$courselevelm}; }
1078:
1079: if ($courseopt{$courselevel}) { return $courseopt{$courselevel}; }
1080:
1081: # ----------------------------------------------------- second, check map parms
1082:
1083: my $thisparm=$parmhash{$symbparm};
1084: if ($thisparm) { return $thisparm; }
1085:
1086: # -------------------------------------------------------- first, check default
1087:
1.12 www 1088: return &Apache::lonnet::metadata($fn,$rwhat.'.default');
1.11 www 1089:
1090: }
1091:
1.23 www 1092: # ---------------------------------------------- Update rows for course listing
1.11 www 1093:
1.28 www 1094: sub updateclasssheet {
1.23 www 1095: my $safeeval=shift;
1.28 www 1096: my $cnum=&getcnum($safeeval);
1097: my $cdom=&getcdom($safeeval);
1098: my $cid=&getcid($safeeval);
1099: my $chome=&getchome($safeeval);
1100:
1101: # ---------------------------------------------- Read class list and row labels
1102:
1.23 www 1103: my $classlst=&Apache::lonnet::reply
1.28 www 1104: ('dump:'.$cdom.':'.$cnum.':classlist',$chome);
1.23 www 1105: my %currentlist=();
1106: my $now=time;
1107: unless ($classlst=~/^error\:/) {
1108: map {
1109: my ($name,$value)=split(/\=/,$_);
1.24 www 1110: my ($end,$start)=split(/\:/,&Apache::lonnet::unescape($value));
1.23 www 1111: my $active=1;
1112: if (($end) && ($now>$end)) { $active=0; }
1113: if ($active) {
1.24 www 1114: my $rowlabel='';
1115: $name=&Apache::lonnet::unescape($name);
1.28 www 1116: my ($sname,$sdom)=split(/\:/,$name);
1117: my $ssec=&Apache::lonnet::usection($sdom,$sname,$cid);
1118: if ($ssec==-1) {
1.24 www 1119: $rowlabel='<font color=red>Data not available: '.$name.
1120: '</font>';
1121: } else {
1.28 www 1122: my %reply=&Apache::lonnet::idrget($sdom,$sname);
1123: my $reply=&Apache::lonnet::reply('get:'.$sdom.':'.$sname.
1.24 www 1124: ':environment:firstname&middlename&lastname&generation',
1.28 www 1125: &Apache::lonnet::homeserver($sname,$sdom));
1.45 www 1126: $rowlabel='<a href="/adm/studentcalc?uname='.$sname.
1127: '&udom='.$sdom.'">'.
1128: $ssec.' '.$reply{$sname}.'<br>';
1.24 www 1129: map {
1130: $rowlabel.=&Apache::lonnet::unescape($_).' ';
1131: } split(/\&/,$reply);
1.45 www 1132: $rowlabel.='</a>';
1.24 www 1133: }
1134: $currentlist{&Apache::lonnet::unescape($name)}=$rowlabel;
1.23 www 1135: }
1136: } split(/\&/,$classlst);
1137: #
1138: # -------------------- Find discrepancies between the course row table and this
1139: #
1140: my %f=&getformulas($safeeval);
1141: my $changed=0;
1142:
1143: my $maxrow=0;
1144: my %existing=();
1145:
1146: # ----------------------------------------------------------- Now obsolete rows
1147: map {
1148: if ($_=~/^A(\d+)/) {
1149: $maxrow=($1>$maxrow)?$1:$maxrow;
1150: $existing{$f{$_}}=1;
1151: unless ((defined($currentlist{$f{$_}})) || (!$1)) {
1152: $f{$_}='!!! Obsolete';
1153: $changed=1;
1154: }
1155: }
1156: } keys %f;
1157:
1158: # -------------------------------------------------------- New and unknown keys
1159:
1160: map {
1161: unless ($existing{$_}) {
1162: $changed=1;
1163: $maxrow++;
1164: $f{'A'.$maxrow}=$_;
1165: }
1.24 www 1166: } sort keys %currentlist;
1.23 www 1167:
1168: if ($changed) { &setformulas($safeeval,%f); }
1169:
1170: &setmaxrow($safeeval,$maxrow);
1171: &setrowlabels($safeeval,%currentlist);
1172:
1173: } else {
1174: return 'Could not access course data';
1175: }
1176: }
1.5 www 1177:
1.28 www 1178: # ----------------------------------- Update rows for student and assess sheets
1179:
1180: sub updatestudentassesssheet {
1.5 www 1181: my $safeeval=shift;
1182: my %bighash;
1.35 www 1183: my $stype=&gettype($safeeval);
1184: my %current=();
1185: unless ($updatedata{$ENV{'request.course.fn'}.'_'.$stype}) {
1.5 www 1186: # -------------------------------------------------------------------- Tie hash
1187: if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
1188: &GDBM_READER,0640)) {
1189: # --------------------------------------------------------- Get all assessments
1190:
1.52 www 1191: my %allkeys=('timestamp' =>
1192: 'Timestamp of Last Transaction<br>timestamp');
1.5 www 1193: my %allassess=();
1194:
1.50 www 1195: my $adduserstr='';
1196: if ((&getuname($safeeval) ne $ENV{'user.name'}) ||
1197: (&getudom($safeeval) ne $ENV{'user.domain'})) {
1198: $adduserstr='&uname='.&getuname($safeeval).
1199: '&udom='.&getudom($safeeval);
1200: }
1201:
1.5 www 1202: map {
1203: if ($_=~/^src\_(\d+)\.(\d+)$/) {
1204: my $mapid=$1;
1205: my $resid=$2;
1206: my $id=$mapid.'.'.$resid;
1207: my $srcf=$bighash{$_};
1208: if ($srcf=~/\.(problem|exam|quiz|assess|survey|form)$/) {
1209: my $symb=
1210: &Apache::lonnet::declutter($bighash{'map_id_'.$mapid}).
1211: '___'.$resid.'___'.
1212: &Apache::lonnet::declutter($srcf);
1.38 www 1213: $allassess{$symb}=
1.50 www 1214: '<a href="/adm/assesscalc?usymb='.$symb.$adduserstr.'">'.
1215: $bighash{'title_'.$id}.'</a>';
1.6 www 1216: if ($stype eq 'assesscalc') {
1.5 www 1217: map {
1.11 www 1218: if (($_=~/^stores\_(.*)/) || ($_=~/^parameter\_(.*)/)) {
1.5 www 1219: my $key=$_;
1220: my $display=
1221: &Apache::lonnet::metadata($srcf,$key.'.display');
1222: unless ($display) {
1.40 www 1223: $display.=
1.5 www 1224: &Apache::lonnet::metadata($srcf,$key.'.name');
1225: }
1.40 www 1226: $display.='<br>'.$key;
1.5 www 1227: $allkeys{$key}=$display;
1228: }
1229: } split(/\,/,&Apache::lonnet::metadata($srcf,'keys'));
1230: }
1231: }
1232: }
1233: } keys %bighash;
1234: untie(%bighash);
1235:
1236: #
1.11 www 1237: # %allkeys has a list of storage and parameter displays by unikey
1.5 www 1238: # %allassess has a list of all resource displays by symb
1239: #
1.6 www 1240:
1241: if ($stype eq 'assesscalc') {
1242: %current=%allkeys;
1243: } elsif ($stype eq 'studentcalc') {
1244: %current=%allassess;
1245: }
1.35 www 1246: $updatedata{$ENV{'request.course.fn'}.'_'.$stype}=
1247: join('___;___',%current);
1248: } else {
1249: return 'Could not access course data';
1250: }
1251: # ------------------------------------------------------ Get current from cache
1252: } else {
1253: %current=split(/\_\_\_\;\_\_\_/,
1254: $updatedata{$ENV{'request.course.fn'}.'_'.$stype});
1255: }
1256: # -------------------- Find discrepancies between the course row table and this
1257: #
1258: my %f=&getformulas($safeeval);
1259: my $changed=0;
1.6 www 1260:
1261: my $maxrow=0;
1262: my %existing=();
1263:
1264: # ----------------------------------------------------------- Now obsolete rows
1.5 www 1265: map {
1.6 www 1266: if ($_=~/^A(\d+)/) {
1267: $maxrow=($1>$maxrow)?$1:$maxrow;
1.57 www 1268: my ($usy,$ufn)=split(/\_\_\&\&\&\_\_/,$f{$_});
1269: $existing{$usy}=1;
1270: unless ((defined($current{$usy})) || (!$1)) {
1.6 www 1271: $f{$_}='!!! Obsolete';
1272: $changed=1;
1.57 www 1273: } elsif ($ufn) {
1274: $current{$usy}
1275: =~s/assesscalc\?usymb\=/assesscalc\?ufn\=$ufn\&usymb\=/;
1.5 www 1276: }
1277: }
1278: } keys %f;
1.6 www 1279:
1280: # -------------------------------------------------------- New and unknown keys
1281:
1282: map {
1283: unless ($existing{$_}) {
1284: $changed=1;
1285: $maxrow++;
1286: $f{'A'.$maxrow}=$_;
1287: }
1288: } keys %current;
1.35 www 1289:
1.6 www 1290: if ($changed) { &setformulas($safeeval,%f); }
1291:
1292: &setmaxrow($safeeval,$maxrow);
1293: &setrowlabels($safeeval,%current);
1.35 www 1294:
1295: undef %current;
1296: undef %existing;
1.5 www 1297: }
1.3 www 1298:
1.24 www 1299: # ------------------------------------------------ Load data for one assessment
1.16 www 1300:
1.29 www 1301: sub loadstudent {
1.16 www 1302: my $safeeval=shift;
1303: my %c=();
1304: my %f=&getformulas($safeeval);
1.39 www 1305: $cachedassess=&getuname($safeeval).':'.&getudom($safeeval);
1306: %cachedstores=();
1307: {
1308: my $reply=&Apache::lonnet::reply('dump:'.&getudom($safeeval).':'.
1309: &getuname($safeeval).':'.
1310: &getcid($safeeval),
1311: &getuhome($safeeval));
1312: unless ($reply=~/^error\:/) {
1313: map {
1314: my ($name,$value)=split(/\=/,$_);
1315: $cachedstores{&Apache::lonnet::unescape($name)}=
1316: &Apache::lonnet::unescape($value);
1317: } split(/\&/,$reply);
1318: }
1319: }
1.36 www 1320: my @assessdata=();
1.16 www 1321: map {
1.17 www 1322: if ($_=~/^A(\d+)/) {
1323: my $row=$1;
1.42 www 1324: unless (($f{$_}=~/^\!/) || ($row==0)) {
1.55 www 1325: my ($usy,$ufn)=split(/\_\_\&\&\&\_\_/,$f{$_});
1.36 www 1326: @assessdata=&exportsheet(&getuname($safeeval),
1327: &getudom($safeeval),
1.55 www 1328: 'assesscalc',$usy,$ufn);
1.17 www 1329: my $index=0;
1.30 www 1330: map {
1331: if ($assessdata[$index]) {
1.41 www 1332: my $col=$_;
1333: if ($assessdata[$index]=~/\D/) {
1334: $c{$col.$row}="'".$assessdata[$index]."'";
1335: } else {
1336: $c{$col.$row}=$assessdata[$index];
1337: }
1338: unless ($col eq 'A') {
1339: $f{$col.$row}='import';
1.30 www 1340: }
1341: }
1342: $index++;
1343: } ('A','B','C','D','E','F','G','H','I','J','K','L','M',
1344: 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
1.16 www 1345: }
1346: }
1347: } keys %f;
1.39 www 1348: $cachedassess='';
1349: undef %cachedstores;
1.18 www 1350: &setformulas($safeeval,%f);
1.16 www 1351: &setconstants($safeeval,%c);
1352: }
1353:
1.24 www 1354: # --------------------------------------------------- Load data for one student
1355:
1.30 www 1356: sub loadcourse {
1.37 www 1357: my ($safeeval,$r)=@_;
1.24 www 1358: my %c=();
1359: my %f=&getformulas($safeeval);
1.37 www 1360: my $total=0;
1361: map {
1362: if ($_=~/^A(\d+)/) {
1363: unless ($f{$_}=~/^\!/) { $total++; }
1364: }
1365: } keys %f;
1366: my $now=0;
1367: my $since=time;
1.39 www 1368: $r->print(<<ENDPOP);
1369: <script>
1370: popwin=open('','popwin','width=400,height=100');
1371: popwin.document.writeln('<html><body bgcolor="#FFFFFF">'+
1.50 www 1372: '<h3>Spreadsheet Calculation Progress</h3>'+
1.39 www 1373: '<form name=popremain>'+
1374: '<input type=text size=35 name=remaining value=Starting></form>'+
1375: '</body></html>');
1.42 www 1376: popwin.document.close();
1.39 www 1377: </script>
1378: ENDPOP
1.37 www 1379: $r->rflush();
1.24 www 1380: map {
1381: if ($_=~/^A(\d+)/) {
1382: my $row=$1;
1.42 www 1383: unless (($f{$_}=~/^\!/) || ($row==0)) {
1.37 www 1384: my @studentdata=&exportsheet(split(/\:/,$f{$_}),
1.30 www 1385: 'studentcalc');
1.37 www 1386: undef %userrdatas;
1387: $now++;
1.39 www 1388: $r->print('<script>popwin.document.popremain.remaining.value="'.
1.37 www 1389: $now.'/'.$total.': '.int((time-$since)/$now*($total-$now)).
1.39 www 1390: ' secs remaining";</script>');
1.37 www 1391: $r->rflush();
1392:
1.24 www 1393: my $index=0;
1.30 www 1394: map {
1395: if ($studentdata[$index]) {
1.41 www 1396: my $col=$_;
1397: if ($studentdata[$index]=~/\D/) {
1398: $c{$col.$row}="'".$studentdata[$index]."'";
1399: } else {
1400: $c{$col.$row}=$studentdata[$index];
1401: }
1402: unless ($col eq 'A') {
1403: $f{$col.$row}='import';
1.30 www 1404: }
1405: }
1406: $index++;
1407: } ('A','B','C','D','E','F','G','H','I','J','K','L','M',
1408: 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
1.24 www 1409: }
1410: }
1411: } keys %f;
1412: &setformulas($safeeval,%f);
1413: &setconstants($safeeval,%c);
1.43 www 1414: $r->print('<script>popwin.close()</script>');
1.37 www 1415: $r->rflush();
1.24 www 1416: }
1417:
1.6 www 1418: # ------------------------------------------------ Load data for one assessment
1419:
1.29 www 1420: sub loadassessment {
1421: my $safeeval=shift;
1422:
1423: my $uhome=&getuhome($safeeval);
1424: my $uname=&getuname($safeeval);
1425: my $udom=&getudom($safeeval);
1426: my $symb=&getusymb($safeeval);
1427: my $cid=&getcid($safeeval);
1428: my $cnum=&getcnum($safeeval);
1429: my $cdom=&getcdom($safeeval);
1430: my $chome=&getchome($safeeval);
1431:
1.6 www 1432: my $namespace;
1.29 www 1433: unless ($namespace=$cid) { return ''; }
1.11 www 1434:
1435: # ----------------------------------------------------------- Get stored values
1.39 www 1436:
1437: my %returnhash=();
1438:
1439: if ($cachedassess eq $uname.':'.$udom) {
1440: #
1441: # get data out of the dumped stores
1442: #
1443:
1444: my $version=$cachedstores{'version:'.$symb};
1445: my $scope;
1446: for ($scope=1;$scope<=$version;$scope++) {
1447: map {
1448: $returnhash{$_}=$cachedstores{$scope.':'.$symb.':'.$_};
1449: } split(/\:/,$cachedstores{$scope.':keys:'.$symb});
1450: }
1451:
1452: } else {
1453: #
1454: # restore individual
1455: #
1456:
1.11 www 1457: my $answer=&Apache::lonnet::reply(
1.15 www 1458: "restore:$udom:$uname:".
1459: &Apache::lonnet::escape($namespace).":".
1460: &Apache::lonnet::escape($symb),$uhome);
1.6 www 1461: map {
1462: my ($name,$value)=split(/\=/,$_);
1.11 www 1463: $returnhash{&Apache::lonnet::unescape($name)}=
1464: &Apache::lonnet::unescape($value);
1.6 www 1465: } split(/\&/,$answer);
1466: my $version;
1467: for ($version=1;$version<=$returnhash{'version'};$version++) {
1468: map {
1469: $returnhash{$_}=$returnhash{$version.':'.$_};
1470: } split(/\:/,$returnhash{$version.':keys'});
1471: }
1.39 www 1472: }
1.11 www 1473: # ----------------------------- returnhash now has all stores for this resource
1474:
1475: # ---------------------------- initialize coursedata and userdata for this user
1.31 www 1476: undef %courseopt;
1477: undef %useropt;
1.29 www 1478:
1479: my $userprefix=$uname.'_'.$udom.'_';
1480:
1.11 www 1481: unless ($uhome eq 'no_host') {
1482: # -------------------------------------------------------------- Get coursedata
1.13 www 1483: unless
1.32 www 1484: ((time-$courserdatas{$cid.'.last_cache'})<240) {
1.29 www 1485: my $reply=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.
1486: ':resourcedata',$chome);
1.11 www 1487: if ($reply!~/^error\:/) {
1.29 www 1488: $courserdatas{$cid}=$reply;
1489: $courserdatas{$cid.'.last_cache'}=time;
1.11 www 1490: }
1491: }
1492: map {
1493: my ($name,$value)=split(/\=/,$_);
1.29 www 1494: $courseopt{$userprefix.&Apache::lonnet::unescape($name)}=
1.11 www 1495: &Apache::lonnet::unescape($value);
1.36 www 1496: } split(/\&/,$courserdatas{$cid});
1.11 www 1497: # --------------------------------------------------- Get userdata (if present)
1.13 www 1498: unless
1.32 www 1499: ((time-$userrdatas{$uname.'___'.$udom.'.last_cache'})<240) {
1.11 www 1500: my $reply=
1501: &Apache::lonnet::reply('dump:'.$udom.':'.$uname.':resourcedata',$uhome);
1502: if ($reply!~/^error\:/) {
1503: $userrdatas{$uname.'___'.$udom}=$reply;
1.13 www 1504: $userrdatas{$uname.'___'.$udom.'.last_cache'}=time;
1.11 www 1505: }
1506: }
1507: map {
1508: my ($name,$value)=split(/\=/,$_);
1.29 www 1509: $useropt{$userprefix.&Apache::lonnet::unescape($name)}=
1.15 www 1510: &Apache::lonnet::unescape($value);
1.11 www 1511: } split(/\&/,$userrdatas{$uname.'___'.$udom});
1.29 www 1512: }
1513: # ----------------- now courseopt, useropt initialized for this user and course
1514: # (used by parmval)
1515:
1516: my %c=();
1.6 www 1517:
1.29 www 1518: if (tie(%parmhash,'GDBM_File',
1519: &getcfn($safeeval).'_parms.db',&GDBM_READER,0640)) {
1.6 www 1520: my %f=&getformulas($safeeval);
1521: map {
1522: if ($_=~/^A/) {
1523: unless ($f{$_}=~/^\!/) {
1.11 www 1524: if ($f{$_}=~/^parameter/) {
1.40 www 1525: my $val=&parmval($f{$_},$safeeval);
1526: $c{$_}=$val;
1527: $c{$f{$_}}=$val;
1.11 www 1528: } else {
1.15 www 1529: my $key=$f{$_};
1.40 www 1530: my $ckey=$key;
1.15 www 1531: $key=~s/^stores\_/resource\./;
1532: $key=~s/\_/\./;
1533: $c{$_}=$returnhash{$key};
1.40 www 1534: $c{$ckey}=$returnhash{$key};
1.11 www 1535: }
1536: }
1.6 www 1537: }
1538: } keys %f;
1.29 www 1539: untie(%parmhash);
1540: }
1541: &setconstants($safeeval,%c);
1.6 www 1542: }
1543:
1.10 www 1544: # --------------------------------------------------------- Various form fields
1545:
1546: sub textfield {
1547: my ($title,$name,$value)=@_;
1548: return "\n<p><b>$title:</b><br>".
1549: '<input type=text name="'.$name.'" size=80 value="'.$value.'">';
1550: }
1551:
1552: sub hiddenfield {
1553: my ($name,$value)=@_;
1554: return "\n".'<input type=hidden name="'.$name.'" value="'.$value.'">';
1555: }
1556:
1557: sub selectbox {
1558: my ($title,$name,$value,%options)=@_;
1559: my $selout="\n<p><b>$title:</b><br>".'<select name="'.$name.'">';
1560: map {
1561: $selout.='<option value="'.$_.'"';
1562: if ($_ eq $value) { $selout.=' selected'; }
1563: $selout.='>'.$options{$_}.'</option>';
1564: } sort keys %options;
1565: return $selout.'</select>';
1566: }
1567:
1.28 www 1568: # =============================================== Update information in a sheet
1569: #
1570: # Add new users or assessments, etc.
1571: #
1572:
1573: sub updatesheet {
1574: my $safeeval=shift;
1575: my $stype=&gettype($safeeval);
1576: if ($stype eq 'classcalc') {
1577: return &updateclasssheet($safeeval);
1578: } else {
1579: return &updatestudentassesssheet($safeeval);
1580: }
1581: }
1582:
1583: # =================================================== Load the rows for a sheet
1584: #
1585: # Import the data for rows
1586: #
1587:
1.37 www 1588: sub loadrows {
1589: my ($safeeval,$r)=@_;
1.28 www 1590: my $stype=&gettype($safeeval);
1591: if ($stype eq 'classcalc') {
1.37 www 1592: &loadcourse($safeeval,$r);
1.28 www 1593: } elsif ($stype eq 'studentcalc') {
1.29 www 1594: &loadstudent($safeeval);
1.28 www 1595: } else {
1.29 www 1596: &loadassessment($safeeval);
1.28 www 1597: }
1598: }
1599:
1.47 www 1600: # ======================================================= Forced recalculation?
1601:
1602: sub checkthis {
1603: my ($keyname,$time)=@_;
1604: return ($time<$expiredates{$keyname});
1605: }
1606: sub forcedrecalc {
1607: my ($uname,$udom,$stype,$usymb)=@_;
1608: my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
1609: my $time=$oldsheets{$key.'.time'};
1.53 www 1610: if ($ENV{'form.forcerecalc'}) { return 1; }
1.47 www 1611: unless ($time) { return 1; }
1612: if ($stype eq 'assesscalc') {
1613: my $map=(split(/\_\_\_/,$usymb))[0];
1614: if (&checkthis('::assesscalc:',$time) ||
1615: &checkthis('::assesscalc:'.$map,$time) ||
1616: &checkthis('::assesscalc:'.$usymb,$time) ||
1.49 www 1617: &checkthis($uname.':'.$udom.':assesscalc:',$time) ||
1618: &checkthis($uname.':'.$udom.':assesscalc:'.$map,$time) ||
1619: &checkthis($uname.':'.$udom.':assesscalc:'.$usymb,$time)) {
1.47 www 1620: return 1;
1621: }
1622: } else {
1623: if (&checkthis('::studentcalc:',$time) ||
1.51 www 1624: &checkthis($uname.':'.$udom.':studentcalc:',$time)) {
1.47 www 1625: return 1;
1626: }
1627: }
1628: return 0;
1629: }
1630:
1.28 www 1631: # ============================================================== Export handler
1632: #
1633: # Non-interactive call from with program
1634: #
1635:
1636: sub exportsheet {
1.44 www 1637: my ($uname,$udom,$stype,$usymb,$fn)=@_;
1638: my @exportarr=();
1639: #
1640: # Check if cached
1641: #
1.46 www 1642:
1.44 www 1643: my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
1644: my $found='';
1645:
1646: if ($oldsheets{$key}) {
1647: map {
1648: my ($name,$value)=split(/\_\_\_\=\_\_\_/,$_);
1649: if ($name eq $fn) {
1650: $found=$value;
1651: }
1652: } split(/\_\_\_\&\_\_\_/,$oldsheets{$key});
1653: }
1654:
1.46 www 1655: unless ($found) {
1656: &cachedssheets($uname,$udom,&Apache::lonnet::homeserver($uname,$udom));
1657: if ($oldsheets{$key}) {
1658: map {
1659: my ($name,$value)=split(/\_\_\_\=\_\_\_/,$_);
1660: if ($name eq $fn) {
1661: $found=$value;
1662: }
1663: } split(/\_\_\_\&\_\_\_/,$oldsheets{$key});
1664: }
1665: }
1.47 www 1666: #
1667: # Check if still valid
1668: #
1669: if ($found) {
1670: if (&forcedrecalc($uname,$udom,$stype,$usymb)) {
1671: $found='';
1672: }
1673: }
1674:
1.44 www 1675: if ($found) {
1676: #
1677: # Return what was cached
1678: #
1679: @exportarr=split(/\_\_\_\;\_\_\_/,$found);
1680:
1681: } else {
1682: #
1683: # Not cached
1.46 www 1684: #
1685:
1.29 www 1686: my $thissheet=&makenewsheet($uname,$udom,$stype,$usymb);
1.28 www 1687: &readsheet($thissheet,$fn);
1688: &updatesheet($thissheet);
1689: &loadrows($thissheet);
1.44 www 1690: &calcsheet($thissheet);
1691: @exportarr=&exportdata($thissheet);
1692: #
1693: # Store now
1694: #
1695: my $cid=$ENV{'request.course.id'};
1.47 www 1696: my $current='';
1.46 www 1697: if ($stype eq 'studentcalc') {
1698: $current=&Apache::lonnet::reply('get:'.
1.44 www 1699: $ENV{'course.'.$cid.'.domain'}.':'.
1700: $ENV{'course.'.$cid.'.num'}.
1701: ':nohist_calculatedsheets:'.
1702: &Apache::lonnet::escape($key),
1703: $ENV{'course.'.$cid.'.home'});
1.46 www 1704: } else {
1705: $current=&Apache::lonnet::reply('get:'.
1706: &getudom($thissheet).':'.
1707: &getuname($thissheet).
1708: ':nohist_calculatedsheets_'.
1709: $ENV{'request.course.id'}.':'.
1710: &Apache::lonnet::escape($key),
1711: &getuhome($thissheet));
1712:
1713: }
1.44 www 1714: my %currentlystored=();
1715: unless ($current=~/^error\:/) {
1716: map {
1717: my ($name,$value)=split(/\_\_\_\=\_\_\_/,$_);
1718: $currentlystored{$name}=$value;
1719: } split(/\_\_\_\&\_\_\_/,&Apache::lonnet::unescape($current));
1720: }
1721: $currentlystored{$fn}=join('___;___',@exportarr);
1722:
1723: my $newstore='';
1724: map {
1725: if ($newstore) { $newstore.='___&___'; }
1726: $newstore.=$_.'___=___'.$currentlystored{$_};
1727: } keys %currentlystored;
1.47 www 1728: my $now=time;
1.46 www 1729: if ($stype eq 'studentcalc') {
1730: &Apache::lonnet::reply('put:'.
1.44 www 1731: $ENV{'course.'.$cid.'.domain'}.':'.
1732: $ENV{'course.'.$cid.'.num'}.
1733: ':nohist_calculatedsheets:'.
1734: &Apache::lonnet::escape($key).'='.
1.47 www 1735: &Apache::lonnet::escape($newstore).'&'.
1736: &Apache::lonnet::escape($key).'.time='.$now,
1.44 www 1737: $ENV{'course.'.$cid.'.home'});
1.46 www 1738: } else {
1739: &Apache::lonnet::reply('put:'.
1740: &getudom($thissheet).':'.
1741: &getuname($thissheet).
1742: ':nohist_calculatedsheets_'.
1743: $ENV{'request.course.id'}.':'.
1744: &Apache::lonnet::escape($key).'='.
1.47 www 1745: &Apache::lonnet::escape($newstore).'&'.
1746: &Apache::lonnet::escape($key).'.time='.$now,
1.46 www 1747: &getuhome($thissheet));
1748: }
1.44 www 1749: }
1750: return @exportarr;
1751: }
1.48 www 1752: # ============================================================ Expiration Dates
1753: #
1754: # Load previously cached student spreadsheets for this course
1755: #
1756:
1757: sub expirationdates {
1758: undef %expiredates;
1759: my $cid=$ENV{'request.course.id'};
1760: my $reply=&Apache::lonnet::reply('dump:'.
1761: $ENV{'course.'.$cid.'.domain'}.':'.
1762: $ENV{'course.'.$cid.'.num'}.
1763: ':nohist_expirationdates',
1764: $ENV{'course.'.$cid.'.home'});
1765: unless ($reply=~/^error\:/) {
1766: map {
1767: my ($name,$value)=split(/\=/,$_);
1768: $expiredates{&Apache::lonnet::unescape($name)}
1769: =&Apache::lonnet::unescape($value);
1770: } split(/\&/,$reply);
1771: }
1772: }
1.44 www 1773:
1774: # ===================================================== Calculated sheets cache
1775: #
1.46 www 1776: # Load previously cached student spreadsheets for this course
1.44 www 1777: #
1778:
1.46 www 1779: sub cachedcsheets {
1.44 www 1780: my $cid=$ENV{'request.course.id'};
1781: my $reply=&Apache::lonnet::reply('dump:'.
1782: $ENV{'course.'.$cid.'.domain'}.':'.
1783: $ENV{'course.'.$cid.'.num'}.
1784: ':nohist_calculatedsheets',
1785: $ENV{'course.'.$cid.'.home'});
1786: unless ($reply=~/^error\:/) {
1787: map {
1788: my ($name,$value)=split(/\=/,$_);
1789: $oldsheets{&Apache::lonnet::unescape($name)}
1790: =&Apache::lonnet::unescape($value);
1791: } split(/\&/,$reply);
1792: }
1.28 www 1793: }
1794:
1.46 www 1795: # ===================================================== Calculated sheets cache
1796: #
1797: # Load previously cached assessment spreadsheets for this student
1798: #
1799:
1800: sub cachedssheets {
1801: my ($sname,$sdom,$shome)=@_;
1802: unless (($loadedcaches{$sname.'_'.$sdom}) || ($shome eq 'no_host')) {
1803: my $cid=$ENV{'request.course.id'};
1804: my $reply=&Apache::lonnet::reply('dump:'.$sdom.':'.$sname.
1805: ':nohist_calculatedsheets_'.
1806: $ENV{'request.course.id'},
1807: $shome);
1808: unless ($reply=~/^error\:/) {
1809: map {
1810: my ($name,$value)=split(/\=/,$_);
1811: $oldsheets{&Apache::lonnet::unescape($name)}
1812: =&Apache::lonnet::unescape($value);
1813: } split(/\&/,$reply);
1814: }
1815: $loadedcaches{$sname.'_'.$sdom}=1;
1816: }
1817: }
1818:
1819: # ===================================================== Calculated sheets cache
1820: #
1821: # Load previously cached assessment spreadsheets for this student
1822: #
1823:
1.12 www 1824: # ================================================================ Main handler
1.28 www 1825: #
1826: # Interactive call to screen
1827: #
1828: #
1829:
1.3 www 1830:
1831: sub handler {
1.7 www 1832: my $r=shift;
1833:
1.28 www 1834: if ($r->header_only) {
1.7 www 1835: $r->content_type('text/html');
1836: $r->send_http_header;
1837: return OK;
1.28 www 1838: }
1839:
1840: # ---------------------------------------------------- Global directory configs
1841:
1.29 www 1842: $includedir=$r->dir_config('lonIncludes');
1.28 www 1843: $tmpdir=$r->dir_config('lonDaemons').'/tmp/';
1.3 www 1844:
1.7 www 1845: # ----------------------------------------------------- Needs to be in a course
1.3 www 1846:
1.29 www 1847: if ($ENV{'request.course.fn'}) {
1.10 www 1848:
1849: # --------------------------- Get query string for limited number of parameters
1.17 www 1850:
1.10 www 1851: map {
1852: my ($name, $value) = split(/=/,$_);
1853: $value =~ tr/+/ /;
1854: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.19 www 1855: if (($name eq 'uname') || ($name eq 'udom') ||
1856: ($name eq 'usymb') || ($name eq 'ufn')) {
1.10 www 1857: unless ($ENV{'form.'.$name}) {
1858: $ENV{'form.'.$name}=$value;
1859: }
1860: }
1861: } (split(/&/,$ENV{'QUERY_STRING'}));
1862:
1.55 www 1863: # -------------------------------------- Interactive loading of specific sheet?
1864: if (($ENV{'form.load'}) && ($ENV{'form.loadthissheet'} ne 'Default')) {
1865: $ENV{'form.ufn'}=$ENV{'form.loadthissheet'};
1866: }
1.10 www 1867: # ------------------------------------------- Nothing there? Must be login user
1.29 www 1868:
1869: my $aname;
1870: my $adom;
1871:
1.10 www 1872: unless ($ENV{'form.uname'}) {
1.29 www 1873: $aname=$ENV{'user.name'};
1874: $adom=$ENV{'user.domain'};
1.11 www 1875: } else {
1.29 www 1876: $aname=$ENV{'form.uname'};
1877: $adom=$ENV{'form.udom'};
1.10 www 1878: }
1.14 www 1879:
1.10 www 1880: # ------------------------------------------------------------------- Open page
1881:
1.7 www 1882: $r->content_type('text/html');
1.11 www 1883: $r->header_out('Cache-control','no-cache');
1884: $r->header_out('Pragma','no-cache');
1.7 www 1885: $r->send_http_header;
1.3 www 1886:
1.14 www 1887: # --------------------------------------------------------------- Screen output
1888:
1.10 www 1889: $r->print('<html><head><title>LON-CAPA Spreadsheet</title>');
1890: $r->print(<<ENDSCRIPT);
1891: <script language="JavaScript">
1892:
1893: function celledit(cn,cf) {
1894: var cnf=prompt(cn,cf);
1895: if (cnf!=null) {
1896: document.sheet.unewfield.value=cn;
1897: document.sheet.unewformula.value=cnf;
1898: document.sheet.submit();
1899: }
1900: }
1901:
1.55 www 1902: function changesheet(cn) {
1903: document.sheet.unewfield.value=cn;
1904: document.sheet.unewformula.value='changesheet';
1905: document.sheet.submit();
1906: }
1907:
1.10 www 1908: </script>
1909: ENDSCRIPT
1910: $r->print('</head><body bgcolor="#FFFFFF">'.
1.21 www 1911: '<img align=right src=/adm/lonIcons/lonlogos.gif>'.
1912: '<h1>LON-CAPA Spreadsheet</h1>'.
1.10 www 1913: '<form action="'.$r->uri.'" name=sheet method=post>'.
1914: &hiddenfield('uname',$ENV{'form.uname'}).
1915: &hiddenfield('udom',$ENV{'form.udom'}).
1916: &hiddenfield('usymb',$ENV{'form.usymb'}).
1917: &hiddenfield('unewfield','').
1918: &hiddenfield('unewformula',''));
1.29 www 1919:
1920: # ---------------------- Make sure that this gets out, even if user hits "stop"
1921:
1.24 www 1922: $r->rflush();
1.29 www 1923:
1.54 www 1924: # ---------------------------------------------------------------- Full recalc?
1925:
1926:
1927: if ($ENV{'form.forcerecalc'}) {
1928: $r->print('<h4>Completely Recalculating Sheet ...</h4>');
1929: undef %spreadsheets;
1930: undef %courserdatas;
1931: undef %userrdatas;
1932: undef %defaultsheets;
1933: undef %updatedata;
1934: }
1935:
1.14 www 1936: # ---------------------------------------- Read new sheet or modified worksheet
1937:
1.19 www 1938: $r->uri=~/\/(\w+)$/;
1.14 www 1939:
1.29 www 1940: my $asheet=&makenewsheet($aname,$adom,$1,$ENV{'form.usymb'});
1941:
1.30 www 1942: # ------------------------ If a new formula had been entered, go from work copy
1943:
1944: if ($ENV{'form.unewfield'}) {
1945: $r->print('<h2>Modified Workcopy</h2>');
1946: $ENV{'form.unewformula'}=~s/\'/\"/g;
1947: $r->print('<p>New formula: '.$ENV{'form.unewfield'}.'='.
1948: $ENV{'form.unewformula'}.'<p>');
1949: &setfilename($asheet,$ENV{'form.ufn'});
1950: &tmpread($asheet,
1951: $ENV{'form.unewfield'},$ENV{'form.unewformula'});
1952:
1953: } elsif ($ENV{'form.saveas'}) {
1954: &setfilename($asheet,$ENV{'form.ufn'});
1955: &tmpread($asheet);
1956: } else {
1957: &readsheet($asheet,$ENV{'form.ufn'});
1958: }
1959:
1960: # -------------------------------------------------- Print out user information
1961:
1962: unless (&gettype($asheet) eq 'classcalc') {
1963: $r->print('<p><b>User:</b> '.&getuname($asheet).
1964: '<br><b>Domain:</b> '.&getudom($asheet));
1965: if (&getcsec($asheet) eq '-1') {
1966: $r->print('<h3><font color=red>'.
1967: 'Not a student in this course</font></h3>');
1968: } else {
1969: $r->print('<br><b>Section/Group:</b> '.&getcsec($asheet));
1970: }
1971: }
1972:
1973: # ---------------------------------------------------------------- Course title
1974:
1975: $r->print('<h1>'.
1976: $ENV{'course.'.$ENV{'request.course.id'}.'.description'}.'</h1>');
1977:
1.53 www 1978: # ---------------------------------------------------- See if user can see this
1979:
1980: if ((&gettype($asheet) eq 'classcalc') ||
1981: (&getuname($asheet) ne $ENV{'user.name'}) ||
1982: (&getudom($asheet) ne $ENV{'user.domain'})) {
1983: unless (&Apache::lonnet::allowed('vgr',&getcid($asheet))) {
1984: $r->print(
1985: '<h1>Access Permission Denied</h1></form></body></html>');
1986: return OK;
1987: }
1988: }
1.30 www 1989:
1.53 www 1990: # ---------------------------------------------------------- Additional options
1991:
1992: $r->print(
1993: '<input type=submit name=forcerecalc value="Completely Recalculate Sheet"><p>'
1994: );
1995: if (&gettype($asheet) eq 'assesscalc') {
1996: $r->print ('<p><font size=+2><a href="/adm/studentcalc?uname='.
1997: &getuname($asheet).
1998: '&udom='.&getudom($asheet).
1999: '">Level up: Student Sheet</a></font><p>');
2000: }
2001:
2002: if ((&gettype($asheet) eq 'studentcalc') &&
2003: (&Apache::lonnet::allowed('vgr',&getcid($asheet)))) {
2004: $r->print (
2005: '<p><font size=+2><a href="/adm/classcalc">'.
2006: 'Level up: Course Sheet</a></font><p>');
2007: }
2008:
1.30 www 2009:
2010: # ----------------------------------------------------------------- Save dialog
2011:
2012:
2013: if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
2014: my $fname=$ENV{'form.ufn'};
2015: $fname=~s/\_[^\_]+$//;
2016: if ($fname eq 'default') { $fname='course_default'; }
2017: $r->print('<input type=submit name=saveas value="Save as ...">'.
2018: '<input type=text size=20 name=newfn value="'.$fname.
2019: '"> (make default: <input type=checkbox name="makedefufn">)<p>');
2020: }
2021:
2022: $r->print(&hiddenfield('ufn',&getfilename($asheet)));
1.55 www 2023:
2024: # ----------------------------------------------------------------- Load dialog
2025: if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
2026: $r->print('<p><input type=submit name=load value="Load ...">'.
2027: '<select name="loadthissheet">'.
2028: '<option name="default">Default</option>');
2029: map {
2030: $r->print('<option name="'.$_.'"');
2031: if ($ENV{'form.ufn'} eq $_) {
2032: $r->print(' selected');
2033: }
2034: $r->print('>'.$_.'</option>');
2035: } &othersheets($asheet,&gettype($asheet));
2036: $r->print('</select><p>');
2037: if (&gettype($asheet) eq 'studentcalc') {
2038: &setothersheets($asheet,&othersheets($asheet,'assesscalc'));
2039: }
2040: }
1.30 www 2041:
1.46 www 2042: # --------------------------------------------------------------- Cached sheets
1.48 www 2043:
2044: &expirationdates();
1.46 www 2045:
2046: undef %oldsheets;
2047: undef %loadedcaches;
1.44 www 2048:
1.46 www 2049: if (&gettype($asheet) eq 'classcalc') {
2050: $r->print("Loading previously calculated student sheets ...<br>\n");
2051: $r->rflush();
2052: &cachedcsheets();
2053: } elsif (&gettype($asheet) eq 'studentcalc') {
2054: $r->print("Loading previously calculated assessment sheets ...<br>\n");
2055: $r->rflush();
2056: &cachedssheets(&getuname($asheet),&getudom($asheet),
2057: &getuhome($asheet));
2058: }
1.30 www 2059:
2060: # ----------------------------------------------------- Update sheet, load rows
1.14 www 2061:
1.44 www 2062: $r->print("Loaded sheet(s), updating rows ...<br>\n");
1.36 www 2063: $r->rflush();
2064:
1.29 www 2065: &updatesheet($asheet);
1.36 www 2066:
2067: $r->print("Updated rows, loading row data ...<br>\n");
2068: $r->rflush();
2069:
1.37 www 2070: &loadrows($asheet,$r);
1.14 www 2071:
1.36 www 2072: $r->print("Loaded row data, calculating sheet ...<br>\n");
2073: $r->rflush();
1.14 www 2074:
1.29 www 2075: my $calcoutput=&calcsheet($asheet);
2076: $r->print('<h3><font color=red>'.$calcoutput.'</h3></font>');
1.56 www 2077:
2078: # ---------------------------------------------------- See if something to save
2079:
2080: if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
2081: my $fname='';
2082: if ($ENV{'form.saveas'} && ($fname=$ENV{'form.newfn'})) {
2083: $fname=~s/\W/\_/g;
2084: if ($fname eq 'default') { $fname='course_default'; }
2085: $fname.='_'.&gettype($asheet);
2086: &setfilename($asheet,$fname);
2087: $ENV{'form.ufn'}=$fname;
2088: $r->print('<p>Saving spreadsheet: '.
2089: &writesheet($asheet,$ENV{'form.makedefufn'}).'<p>');
2090: }
2091: }
2092:
2093: # ------------------------------------------------ Write the modified worksheet
2094:
2095: $r->print('<b>Current sheet:</b> '.&getfilename($asheet).'<p>');
2096:
2097: &tmpwrite($asheet);
2098:
2099: # ------------------------------------------------------------- Print out sheet
1.8 www 2100:
1.29 www 2101: &outsheet($r,$asheet);
1.10 www 2102: $r->print('</form></body></html>');
1.29 www 2103:
1.14 www 2104: # ------------------------------------------------------------------------ Done
1.7 www 2105: } else {
2106: # ----------------------------- Not in a course, or not allowed to modify parms
2107: $ENV{'user.error.msg'}=
2108: $r->uri.":opa:0:0:Cannot modify spreadsheet";
2109: return HTTP_NOT_ACCEPTABLE;
2110: }
1.3 www 2111: return OK;
1.28 www 2112:
1.1 www 2113: }
2114:
2115: 1;
2116: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>