Annotation of loncom/interface/lonspreadsheet.pm, revision 1.41
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.41 ! www 7: # 03/05,03/08,03/10 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]) {
1.41 ! www 1210: my $col=$_;
! 1211: if ($assessdata[$index]=~/\D/) {
! 1212: $c{$col.$row}="'".$assessdata[$index]."'";
! 1213: } else {
! 1214: $c{$col.$row}=$assessdata[$index];
! 1215: }
! 1216: unless ($col eq 'A') {
! 1217: $f{$col.$row}='import';
1.30 www 1218: }
1219: }
1220: $index++;
1221: } ('A','B','C','D','E','F','G','H','I','J','K','L','M',
1222: 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
1.16 www 1223: }
1224: }
1225: } keys %f;
1.39 www 1226: $cachedassess='';
1227: undef %cachedstores;
1.18 www 1228: &setformulas($safeeval,%f);
1.16 www 1229: &setconstants($safeeval,%c);
1230: }
1231:
1.24 www 1232: # --------------------------------------------------- Load data for one student
1233:
1.30 www 1234: sub loadcourse {
1.37 www 1235: my ($safeeval,$r)=@_;
1.24 www 1236: my %c=();
1237: my %f=&getformulas($safeeval);
1.37 www 1238: my $total=0;
1239: map {
1240: if ($_=~/^A(\d+)/) {
1241: unless ($f{$_}=~/^\!/) { $total++; }
1242: }
1243: } keys %f;
1244: my $now=0;
1245: my $since=time;
1.39 www 1246: $r->print(<<ENDPOP);
1247: <script>
1248: popwin=open('','popwin','width=400,height=100');
1249: popwin.document.writeln('<html><body bgcolor="#FFFFFF">'+
1250: '<h1>Spreadsheet Calculation Progress</h1>'+
1251: '<form name=popremain>'+
1252: '<input type=text size=35 name=remaining value=Starting></form>'+
1253: '</body></html>');
1254: popwin.document.close;
1255: </script>
1256: ENDPOP
1.37 www 1257: $r->rflush();
1.24 www 1258: map {
1259: if ($_=~/^A(\d+)/) {
1260: my $row=$1;
1.36 www 1261: unless ($f{$_}=~/^\!/) {
1.37 www 1262: my @studentdata=&exportsheet(split(/\:/,$f{$_}),
1.30 www 1263: 'studentcalc');
1.37 www 1264: undef %userrdatas;
1265: $now++;
1.39 www 1266: $r->print('<script>popwin.document.popremain.remaining.value="'.
1.37 www 1267: $now.'/'.$total.': '.int((time-$since)/$now*($total-$now)).
1.39 www 1268: ' secs remaining";</script>');
1.37 www 1269: $r->rflush();
1270:
1.24 www 1271: my $index=0;
1.30 www 1272: map {
1273: if ($studentdata[$index]) {
1.41 ! www 1274: my $col=$_;
! 1275: if ($studentdata[$index]=~/\D/) {
! 1276: $c{$col.$row}="'".$studentdata[$index]."'";
! 1277: } else {
! 1278: $c{$col.$row}=$studentdata[$index];
! 1279: }
! 1280: unless ($col eq 'A') {
! 1281: $f{$col.$row}='import';
1.30 www 1282: }
1283: }
1284: $index++;
1285: } ('A','B','C','D','E','F','G','H','I','J','K','L','M',
1286: 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
1.24 www 1287: }
1288: }
1289: } keys %f;
1290: &setformulas($safeeval,%f);
1291: &setconstants($safeeval,%c);
1.39 www 1292: $r->print('<script>popwin.close</script>');
1.37 www 1293: $r->rflush();
1.24 www 1294: }
1295:
1.6 www 1296: # ------------------------------------------------ Load data for one assessment
1297:
1.29 www 1298: sub loadassessment {
1299: my $safeeval=shift;
1300:
1301: my $uhome=&getuhome($safeeval);
1302: my $uname=&getuname($safeeval);
1303: my $udom=&getudom($safeeval);
1304: my $symb=&getusymb($safeeval);
1305: my $cid=&getcid($safeeval);
1306: my $cnum=&getcnum($safeeval);
1307: my $cdom=&getcdom($safeeval);
1308: my $chome=&getchome($safeeval);
1309:
1.6 www 1310: my $namespace;
1.29 www 1311: unless ($namespace=$cid) { return ''; }
1.11 www 1312:
1313: # ----------------------------------------------------------- Get stored values
1.39 www 1314:
1315: my %returnhash=();
1316:
1317: if ($cachedassess eq $uname.':'.$udom) {
1318: #
1319: # get data out of the dumped stores
1320: #
1321:
1322: my $version=$cachedstores{'version:'.$symb};
1323: my $scope;
1324: for ($scope=1;$scope<=$version;$scope++) {
1325: map {
1326: $returnhash{$_}=$cachedstores{$scope.':'.$symb.':'.$_};
1327: } split(/\:/,$cachedstores{$scope.':keys:'.$symb});
1328: }
1329:
1330: } else {
1331: #
1332: # restore individual
1333: #
1334:
1.11 www 1335: my $answer=&Apache::lonnet::reply(
1.15 www 1336: "restore:$udom:$uname:".
1337: &Apache::lonnet::escape($namespace).":".
1338: &Apache::lonnet::escape($symb),$uhome);
1.6 www 1339: map {
1340: my ($name,$value)=split(/\=/,$_);
1.11 www 1341: $returnhash{&Apache::lonnet::unescape($name)}=
1342: &Apache::lonnet::unescape($value);
1.6 www 1343: } split(/\&/,$answer);
1344: my $version;
1345: for ($version=1;$version<=$returnhash{'version'};$version++) {
1346: map {
1347: $returnhash{$_}=$returnhash{$version.':'.$_};
1348: } split(/\:/,$returnhash{$version.':keys'});
1349: }
1.39 www 1350: }
1.11 www 1351: # ----------------------------- returnhash now has all stores for this resource
1352:
1353: # ---------------------------- initialize coursedata and userdata for this user
1.31 www 1354: undef %courseopt;
1355: undef %useropt;
1.29 www 1356:
1357: my $userprefix=$uname.'_'.$udom.'_';
1358:
1.11 www 1359: unless ($uhome eq 'no_host') {
1360: # -------------------------------------------------------------- Get coursedata
1.13 www 1361: unless
1.32 www 1362: ((time-$courserdatas{$cid.'.last_cache'})<240) {
1.29 www 1363: my $reply=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.
1364: ':resourcedata',$chome);
1.11 www 1365: if ($reply!~/^error\:/) {
1.29 www 1366: $courserdatas{$cid}=$reply;
1367: $courserdatas{$cid.'.last_cache'}=time;
1.11 www 1368: }
1369: }
1370: map {
1371: my ($name,$value)=split(/\=/,$_);
1.29 www 1372: $courseopt{$userprefix.&Apache::lonnet::unescape($name)}=
1.11 www 1373: &Apache::lonnet::unescape($value);
1.36 www 1374: } split(/\&/,$courserdatas{$cid});
1.11 www 1375: # --------------------------------------------------- Get userdata (if present)
1.13 www 1376: unless
1.32 www 1377: ((time-$userrdatas{$uname.'___'.$udom.'.last_cache'})<240) {
1.11 www 1378: my $reply=
1379: &Apache::lonnet::reply('dump:'.$udom.':'.$uname.':resourcedata',$uhome);
1380: if ($reply!~/^error\:/) {
1381: $userrdatas{$uname.'___'.$udom}=$reply;
1.13 www 1382: $userrdatas{$uname.'___'.$udom.'.last_cache'}=time;
1.11 www 1383: }
1384: }
1385: map {
1386: my ($name,$value)=split(/\=/,$_);
1.29 www 1387: $useropt{$userprefix.&Apache::lonnet::unescape($name)}=
1.15 www 1388: &Apache::lonnet::unescape($value);
1.11 www 1389: } split(/\&/,$userrdatas{$uname.'___'.$udom});
1.29 www 1390: }
1391: # ----------------- now courseopt, useropt initialized for this user and course
1392: # (used by parmval)
1393:
1394: my %c=();
1.6 www 1395:
1.29 www 1396: if (tie(%parmhash,'GDBM_File',
1397: &getcfn($safeeval).'_parms.db',&GDBM_READER,0640)) {
1.6 www 1398: my %f=&getformulas($safeeval);
1399: map {
1400: if ($_=~/^A/) {
1401: unless ($f{$_}=~/^\!/) {
1.11 www 1402: if ($f{$_}=~/^parameter/) {
1.40 www 1403: my $val=&parmval($f{$_},$safeeval);
1404: $c{$_}=$val;
1405: $c{$f{$_}}=$val;
1.11 www 1406: } else {
1.15 www 1407: my $key=$f{$_};
1.40 www 1408: my $ckey=$key;
1.15 www 1409: $key=~s/^stores\_/resource\./;
1410: $key=~s/\_/\./;
1411: $c{$_}=$returnhash{$key};
1.40 www 1412: $c{$ckey}=$returnhash{$key};
1.11 www 1413: }
1414: }
1.6 www 1415: }
1416: } keys %f;
1.29 www 1417: untie(%parmhash);
1418: }
1419: &setconstants($safeeval,%c);
1.6 www 1420: }
1421:
1.10 www 1422: # --------------------------------------------------------- Various form fields
1423:
1424: sub textfield {
1425: my ($title,$name,$value)=@_;
1426: return "\n<p><b>$title:</b><br>".
1427: '<input type=text name="'.$name.'" size=80 value="'.$value.'">';
1428: }
1429:
1430: sub hiddenfield {
1431: my ($name,$value)=@_;
1432: return "\n".'<input type=hidden name="'.$name.'" value="'.$value.'">';
1433: }
1434:
1435: sub selectbox {
1436: my ($title,$name,$value,%options)=@_;
1437: my $selout="\n<p><b>$title:</b><br>".'<select name="'.$name.'">';
1438: map {
1439: $selout.='<option value="'.$_.'"';
1440: if ($_ eq $value) { $selout.=' selected'; }
1441: $selout.='>'.$options{$_}.'</option>';
1442: } sort keys %options;
1443: return $selout.'</select>';
1444: }
1445:
1.28 www 1446: # =============================================== Update information in a sheet
1447: #
1448: # Add new users or assessments, etc.
1449: #
1450:
1451: sub updatesheet {
1452: my $safeeval=shift;
1453: my $stype=&gettype($safeeval);
1454: if ($stype eq 'classcalc') {
1455: return &updateclasssheet($safeeval);
1456: } else {
1457: return &updatestudentassesssheet($safeeval);
1458: }
1459: }
1460:
1461: # =================================================== Load the rows for a sheet
1462: #
1463: # Import the data for rows
1464: #
1465:
1.37 www 1466: sub loadrows {
1467: my ($safeeval,$r)=@_;
1.28 www 1468: my $stype=&gettype($safeeval);
1469: if ($stype eq 'classcalc') {
1.37 www 1470: &loadcourse($safeeval,$r);
1.28 www 1471: } elsif ($stype eq 'studentcalc') {
1.29 www 1472: &loadstudent($safeeval);
1.28 www 1473: } else {
1.29 www 1474: &loadassessment($safeeval);
1.28 www 1475: }
1476: }
1477:
1478: # ============================================================== Export handler
1479: #
1480: # Non-interactive call from with program
1481: #
1482:
1483: sub exportsheet {
1.36 www 1484:
1.28 www 1485: my ($uname,$udom,$stype,$usymb,$fn)=@_;
1.29 www 1486: my $thissheet=&makenewsheet($uname,$udom,$stype,$usymb);
1.28 www 1487: &readsheet($thissheet,$fn);
1488: &updatesheet($thissheet);
1489: &loadrows($thissheet);
1490: &calcsheet($thissheet);
1.36 www 1491: return &exportdata($thissheet);
1.28 www 1492: }
1493:
1.12 www 1494: # ================================================================ Main handler
1.28 www 1495: #
1496: # Interactive call to screen
1497: #
1498: #
1499:
1.3 www 1500:
1501: sub handler {
1.7 www 1502: my $r=shift;
1503:
1.28 www 1504: if ($r->header_only) {
1.7 www 1505: $r->content_type('text/html');
1506: $r->send_http_header;
1507: return OK;
1.28 www 1508: }
1509:
1510: # ---------------------------------------------------- Global directory configs
1511:
1.29 www 1512: $includedir=$r->dir_config('lonIncludes');
1.28 www 1513: $tmpdir=$r->dir_config('lonDaemons').'/tmp/';
1.3 www 1514:
1.7 www 1515: # ----------------------------------------------------- Needs to be in a course
1.3 www 1516:
1.29 www 1517: if ($ENV{'request.course.fn'}) {
1.10 www 1518:
1519: # --------------------------- Get query string for limited number of parameters
1.17 www 1520:
1.10 www 1521: map {
1522: my ($name, $value) = split(/=/,$_);
1523: $value =~ tr/+/ /;
1524: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.19 www 1525: if (($name eq 'uname') || ($name eq 'udom') ||
1526: ($name eq 'usymb') || ($name eq 'ufn')) {
1.10 www 1527: unless ($ENV{'form.'.$name}) {
1528: $ENV{'form.'.$name}=$value;
1529: }
1530: }
1531: } (split(/&/,$ENV{'QUERY_STRING'}));
1532:
1533: # ------------------------------------------- Nothing there? Must be login user
1.29 www 1534:
1535: my $aname;
1536: my $adom;
1537:
1.10 www 1538: unless ($ENV{'form.uname'}) {
1.29 www 1539: $aname=$ENV{'user.name'};
1540: $adom=$ENV{'user.domain'};
1.11 www 1541: } else {
1.29 www 1542: $aname=$ENV{'form.uname'};
1543: $adom=$ENV{'form.udom'};
1.10 www 1544: }
1.14 www 1545:
1.10 www 1546: # ------------------------------------------------------------------- Open page
1547:
1.7 www 1548: $r->content_type('text/html');
1.11 www 1549: $r->header_out('Cache-control','no-cache');
1550: $r->header_out('Pragma','no-cache');
1.7 www 1551: $r->send_http_header;
1.3 www 1552:
1.14 www 1553: # --------------------------------------------------------------- Screen output
1554:
1.10 www 1555: $r->print('<html><head><title>LON-CAPA Spreadsheet</title>');
1556: $r->print(<<ENDSCRIPT);
1557: <script language="JavaScript">
1558:
1559: function celledit(cn,cf) {
1560: var cnf=prompt(cn,cf);
1561: if (cnf!=null) {
1562: document.sheet.unewfield.value=cn;
1563: document.sheet.unewformula.value=cnf;
1564: document.sheet.submit();
1565: }
1566: }
1567:
1568: </script>
1569: ENDSCRIPT
1570: $r->print('</head><body bgcolor="#FFFFFF">'.
1.21 www 1571: '<img align=right src=/adm/lonIcons/lonlogos.gif>'.
1572: '<h1>LON-CAPA Spreadsheet</h1>'.
1.10 www 1573: '<form action="'.$r->uri.'" name=sheet method=post>'.
1574: &hiddenfield('uname',$ENV{'form.uname'}).
1575: &hiddenfield('udom',$ENV{'form.udom'}).
1576: &hiddenfield('usymb',$ENV{'form.usymb'}).
1577: &hiddenfield('unewfield','').
1578: &hiddenfield('unewformula',''));
1.29 www 1579:
1580: # ---------------------- Make sure that this gets out, even if user hits "stop"
1581:
1.24 www 1582: $r->rflush();
1.29 www 1583:
1.14 www 1584: # ---------------------------------------- Read new sheet or modified worksheet
1585:
1.19 www 1586: $r->uri=~/\/(\w+)$/;
1.14 www 1587:
1.29 www 1588: my $asheet=&makenewsheet($aname,$adom,$1,$ENV{'form.usymb'});
1589:
1.30 www 1590: # ------------------------ If a new formula had been entered, go from work copy
1591:
1592: if ($ENV{'form.unewfield'}) {
1593: $r->print('<h2>Modified Workcopy</h2>');
1594: $ENV{'form.unewformula'}=~s/\'/\"/g;
1595: $r->print('<p>New formula: '.$ENV{'form.unewfield'}.'='.
1596: $ENV{'form.unewformula'}.'<p>');
1597: &setfilename($asheet,$ENV{'form.ufn'});
1598: &tmpread($asheet,
1599: $ENV{'form.unewfield'},$ENV{'form.unewformula'});
1600:
1601: } elsif ($ENV{'form.saveas'}) {
1602: &setfilename($asheet,$ENV{'form.ufn'});
1603: &tmpread($asheet);
1604: } else {
1605: &readsheet($asheet,$ENV{'form.ufn'});
1606: }
1607:
1608: # -------------------------------------------------- Print out user information
1609:
1610: unless (&gettype($asheet) eq 'classcalc') {
1611: $r->print('<p><b>User:</b> '.&getuname($asheet).
1612: '<br><b>Domain:</b> '.&getudom($asheet));
1613: if (&getcsec($asheet) eq '-1') {
1614: $r->print('<h3><font color=red>'.
1615: 'Not a student in this course</font></h3>');
1616: } else {
1617: $r->print('<br><b>Section/Group:</b> '.&getcsec($asheet));
1618: }
1619: }
1620:
1621: # ---------------------------------------------------------------- Course title
1622:
1623: $r->print('<h1>'.
1624: $ENV{'course.'.$ENV{'request.course.id'}.'.description'}.'</h1>');
1625:
1626:
1.22 www 1627: # ---------------------------------------------------- See if something to save
1.30 www 1628:
1629: if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
1630: my $fname='';
1631: if ($ENV{'form.saveas'} && ($fname=$ENV{'form.newfn'})) {
1632: $fname=~s/\W/\_/g;
1633: if ($fname eq 'default') { $fname='course_default'; }
1634: $fname.='_'.&gettype($asheet);
1635: &setfilename($asheet,$fname);
1636: $ENV{'form.ufn'}=$fname;
1637: $r->print('<p>Saving spreadsheet: '.
1638: &writesheet($asheet,$ENV{'form.makedefufn'}).'<p>');
1639: }
1640: }
1641:
1.14 www 1642: # ------------------------------------------------ Write the modified worksheet
1643:
1.30 www 1644: $r->print('<b>Current sheet:</b> '.&getfilename($asheet).'<p>');
1645:
1646: &tmpwrite($asheet);
1647:
1648: # ----------------------------------------------------------------- Save dialog
1649:
1650:
1651: if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
1652: my $fname=$ENV{'form.ufn'};
1653: $fname=~s/\_[^\_]+$//;
1654: if ($fname eq 'default') { $fname='course_default'; }
1655: $r->print('<input type=submit name=saveas value="Save as ...">'.
1656: '<input type=text size=20 name=newfn value="'.$fname.
1657: '"> (make default: <input type=checkbox name="makedefufn">)<p>');
1658: }
1659:
1660: $r->print(&hiddenfield('ufn',&getfilename($asheet)));
1661:
1662:
1663: # ----------------------------------------------------- Update sheet, load rows
1.14 www 1664:
1.36 www 1665: $r->print("Loaded sheet, updating rows ...<br>\n");
1666: $r->rflush();
1667:
1.29 www 1668: &updatesheet($asheet);
1.36 www 1669:
1670: $r->print("Updated rows, loading row data ...<br>\n");
1671: $r->rflush();
1672:
1.37 www 1673: &loadrows($asheet,$r);
1.14 www 1674:
1.36 www 1675: $r->print("Loaded row data, calculating sheet ...<br>\n");
1676: $r->rflush();
1.14 www 1677:
1.29 www 1678: my $calcoutput=&calcsheet($asheet);
1679: $r->print('<h3><font color=red>'.$calcoutput.'</h3></font>');
1.8 www 1680:
1.29 www 1681: &outsheet($r,$asheet);
1.10 www 1682: $r->print('</form></body></html>');
1.29 www 1683:
1.14 www 1684: # ------------------------------------------------------------------------ Done
1.7 www 1685: } else {
1686: # ----------------------------- Not in a course, or not allowed to modify parms
1687: $ENV{'user.error.msg'}=
1688: $r->uri.":opa:0:0:Cannot modify spreadsheet";
1689: return HTTP_NOT_ACCEPTABLE;
1690: }
1.3 www 1691: return OK;
1.28 www 1692:
1.1 www 1693: }
1694:
1695: 1;
1696: __END__
1697:
1698:
1699:
1700:
1701:
1702:
1703:
1704:
1705:
1706:
1707:
1708:
1709:
1710:
1711:
1712:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>