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