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