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