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