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