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