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