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