Annotation of loncom/interface/lonspreadsheet.pm, revision 1.30
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.
1.30 ! www 784: '";$csec="'.&Apache::lonnet::usection($udom,$uname,
! 785: $ENV{'request.course.id'}).
1.29 www 786: '";$cid="'.$ENV{'request.course.id'}.
787: '";$cfn="'.$ENV{'request.course.fn'}.
788: '";$cnum="'.$ENV{'course.'.$ENV{'request.course.id'}.'.num'}.
789: '";$cdom="'.$ENV{'course.'.$ENV{'request.course.id'}.'.domain'}.
790: '";$chome="'.$ENV{'course.'.$ENV{'request.course.id'}.'.home'}.'";');
1.28 www 791: return $safeeval;
792: }
793:
1.19 www 794: # ------------------------------------------------------------ Save spreadsheet
795:
796: sub writesheet {
1.28 www 797: my ($safeeval,$makedef)=@_;
798: my $cid=&getcid($safeeval);
799: if (&Apache::lonnet::allowed('opa',$cid)) {
1.19 www 800: my %f=&getformulas($safeeval);
1.28 www 801: my $stype=&gettype($safeeval);
802: my $cnum=&getcnum($safeeval);
803: my $cdom=&getcdom($safeeval);
804: my $chome=&getchome($safeeval);
805: my $fn=&getfilename($safeeval);
806:
807: # ------------------------------------------------------------- Cache new sheet
808: $spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}=join('___;___',%f);
809: # ----------------------------------------------------------------- Write sheet
1.19 www 810: my $sheetdata='';
811: map {
812: $sheetdata.=&Apache::lonnet::escape($_).'='.
813: &Apache::lonnet::escape($f{$_}).'&';
814: } keys %f;
815: $sheetdata=~s/\&$//;
1.28 www 816: my $reply=&Apache::lonnet::reply('put:'.$cdom.':'.$cnum.':'.$fn.':'.
817: $sheetdata,$chome);
1.22 www 818: if ($reply eq 'ok') {
1.28 www 819: $reply=&Apache::lonnet::reply('put:'.$cdom.':'.$cnum.':'.
1.30 ! www 820: $stype.'_spreadsheets:'.
1.28 www 821: &Apache::lonnet::escape($fn).'='.$ENV{'user.name'},
822: $chome);
823: if ($reply eq 'ok') {
824: if ($makedef) {
825: return &Apache::lonnet::reply('put:'.$cdom.':'.$cnum.
826: ':environment:spreadsheet_default_'.$stype.'='.
827: &Apache::lonnet::escape($fn),
828: $chome);
829: } else {
830: return $reply;
831: }
832: } else {
833: return $reply;
834: }
1.22 www 835: } else {
836: return $reply;
837: }
838: }
839: return 'unauthorized';
1.19 www 840: }
841:
1.10 www 842: # ----------------------------------------------- Make a temp copy of the sheet
1.28 www 843: # "Modified workcopy" - interactive only
844: #
1.10 www 845:
846: sub tmpwrite {
1.28 www 847: my $safeeval=shift;
848: my $fn=$ENV{'user.name'}.'_'.
849: $ENV{'user.domain'}.'_spreadsheet_'.&getusymb($safeeval).'_'.
850: &getfilename($safeeval);
1.10 www 851: $fn=~s/\W/\_/g;
852: $fn=$tmpdir.$fn.'.tmp';
853: my $fh;
854: if ($fh=Apache::File->new('>'.$fn)) {
855: print $fh join("\n",&getformulas($safeeval));
856: }
857: }
858:
859: # ---------------------------------------------------------- Read the temp copy
860:
861: sub tmpread {
1.28 www 862: my ($safeeval,$nfield,$nform)=@_;
863: my $fn=$ENV{'user.name'}.'_'.
864: $ENV{'user.domain'}.'_spreadsheet_'.&getusymb($safeeval).'_'.
865: &getfilename($safeeval);
1.10 www 866: $fn=~s/\W/\_/g;
867: $fn=$tmpdir.$fn.'.tmp';
868: my $fh;
869: my %fo=();
870: if ($fh=Apache::File->new($fn)) {
871: my $name;
872: while ($name=<$fh>) {
873: chomp($name);
874: my $value=<$fh>;
875: chomp($value);
876: $fo{$name}=$value;
877: }
878: }
1.22 www 879: if ($nfield) { $fo{$nfield}=$nform; }
1.10 www 880: &setformulas($safeeval,%fo);
881: }
882:
1.11 www 883: # ================================================================== Parameters
884: # -------------------------------------------- Figure out a cascading parameter
1.28 www 885: #
1.29 www 886: # For this function to work
887: #
888: # * parmhash needs to be tied
889: # * courseopt and useropt need to be initialized for this user and course
890: #
1.11 www 891:
892: sub parmval {
1.28 www 893: my ($what,$safeeval)=@_;
894: my $cid=&getcid($safeeval);
895: my $csec=&getcsec($safeeval);
896: my $uname=&getuname($safeeval);
897: my $udom=&getudom($safeeval);
898: my $symb=&getusymb($safeeval);
1.11 www 899:
900: unless ($symb) { return ''; }
901: my $result='';
902:
903: my ($mapname,$id,$fn)=split(/\_\_\_/,$symb);
904: # ----------------------------------------------------- Cascading lookup scheme
1.12 www 905: my $rwhat=$what;
906: $what=~s/^parameter\_//;
907: $what=~s/\_/\./;
1.11 www 908:
909: my $symbparm=$symb.'.'.$what;
910: my $mapparm=$mapname.'___(all).'.$what;
1.29 www 911: my $usercourseprefix=$uname.'_'.$udom.'_'.$cid;
1.11 www 912:
913: my $seclevel=
1.28 www 914: $usercourseprefix.'.['.
1.11 www 915: $csec.'].'.$what;
916: my $seclevelr=
1.28 www 917: $usercourseprefix.'.['.
1.11 www 918: $csec.'].'.$symbparm;
919: my $seclevelm=
1.28 www 920: $usercourseprefix.'.['.
1.11 www 921: $csec.'].'.$mapparm;
922:
923: my $courselevel=
1.28 www 924: $usercourseprefix.'.'.$what;
1.11 www 925: my $courselevelr=
1.28 www 926: $usercourseprefix.'.'.$symbparm;
1.11 www 927: my $courselevelm=
1.28 www 928: $usercourseprefix.'.'.$mapparm;
1.12 www 929:
1.11 www 930: # ---------------------------------------------------------- fourth, check user
931:
932: if ($uname) {
933:
934: if ($useropt{$courselevelr}) { return $useropt{$courselevelr}; }
935:
936: if ($useropt{$courselevelm}) { return $useropt{$courselevelm}; }
937:
938: if ($useropt{$courselevel}) { return $useropt{$courselevel}; }
939:
940: }
941:
942: # --------------------------------------------------------- third, check course
943:
944: if ($csec) {
945:
946: if ($courseopt{$seclevelr}) { return $courseopt{$seclevelr}; }
947:
948: if ($courseopt{$seclevelm}) { return $courseopt{$seclevelm}; }
949:
950: if ($courseopt{$seclevel}) { return $courseopt{$seclevel}; }
951:
952: }
953:
954: if ($courseopt{$courselevelr}) { return $courseopt{$courselevelr}; }
955:
956: if ($courseopt{$courselevelm}) { return $courseopt{$courselevelm}; }
957:
958: if ($courseopt{$courselevel}) { return $courseopt{$courselevel}; }
959:
960: # ----------------------------------------------------- second, check map parms
961:
962: my $thisparm=$parmhash{$symbparm};
963: if ($thisparm) { return $thisparm; }
964:
965: # -------------------------------------------------------- first, check default
966:
1.12 www 967: return &Apache::lonnet::metadata($fn,$rwhat.'.default');
1.11 www 968:
969: }
970:
1.23 www 971: # ---------------------------------------------- Update rows for course listing
1.11 www 972:
1.28 www 973: sub updateclasssheet {
1.23 www 974: my $safeeval=shift;
1.28 www 975: my $cnum=&getcnum($safeeval);
976: my $cdom=&getcdom($safeeval);
977: my $cid=&getcid($safeeval);
978: my $chome=&getchome($safeeval);
979:
980: # ---------------------------------------------- Read class list and row labels
981:
1.23 www 982: my $classlst=&Apache::lonnet::reply
1.28 www 983: ('dump:'.$cdom.':'.$cnum.':classlist',$chome);
1.23 www 984: my %currentlist=();
985: my $now=time;
986: unless ($classlst=~/^error\:/) {
987: map {
988: my ($name,$value)=split(/\=/,$_);
1.24 www 989: my ($end,$start)=split(/\:/,&Apache::lonnet::unescape($value));
1.23 www 990: my $active=1;
991: if (($end) && ($now>$end)) { $active=0; }
992: if ($active) {
1.24 www 993: my $rowlabel='';
994: $name=&Apache::lonnet::unescape($name);
1.28 www 995: my ($sname,$sdom)=split(/\:/,$name);
996: my $ssec=&Apache::lonnet::usection($sdom,$sname,$cid);
997: if ($ssec==-1) {
1.24 www 998: $rowlabel='<font color=red>Data not available: '.$name.
999: '</font>';
1000: } else {
1.28 www 1001: my %reply=&Apache::lonnet::idrget($sdom,$sname);
1002: my $reply=&Apache::lonnet::reply('get:'.$sdom.':'.$sname.
1.24 www 1003: ':environment:firstname&middlename&lastname&generation',
1.28 www 1004: &Apache::lonnet::homeserver($sname,$sdom));
1005: $rowlabel=$ssec.' '.$reply{$sname}.'<br>';
1.24 www 1006: map {
1007: $rowlabel.=&Apache::lonnet::unescape($_).' ';
1008: } split(/\&/,$reply);
1009: }
1010: $currentlist{&Apache::lonnet::unescape($name)}=$rowlabel;
1.23 www 1011: }
1012: } split(/\&/,$classlst);
1013: #
1014: # -------------------- Find discrepancies between the course row table and this
1015: #
1016: my %f=&getformulas($safeeval);
1017: my $changed=0;
1018:
1019: my $maxrow=0;
1020: my %existing=();
1021:
1022: # ----------------------------------------------------------- Now obsolete rows
1023: map {
1024: if ($_=~/^A(\d+)/) {
1025: $maxrow=($1>$maxrow)?$1:$maxrow;
1026: $existing{$f{$_}}=1;
1027: unless ((defined($currentlist{$f{$_}})) || (!$1)) {
1028: $f{$_}='!!! Obsolete';
1029: $changed=1;
1030: }
1031: }
1032: } keys %f;
1033:
1034: # -------------------------------------------------------- New and unknown keys
1035:
1036: map {
1037: unless ($existing{$_}) {
1038: $changed=1;
1039: $maxrow++;
1040: $f{'A'.$maxrow}=$_;
1041: }
1.24 www 1042: } sort keys %currentlist;
1.23 www 1043:
1044: if ($changed) { &setformulas($safeeval,%f); }
1045:
1046: &setmaxrow($safeeval,$maxrow);
1047: &setrowlabels($safeeval,%currentlist);
1048:
1049: } else {
1050: return 'Could not access course data';
1051: }
1052: }
1.5 www 1053:
1.28 www 1054: # ----------------------------------- Update rows for student and assess sheets
1055:
1056: sub updatestudentassesssheet {
1.5 www 1057: my $safeeval=shift;
1058: my %bighash;
1059: # -------------------------------------------------------------------- Tie hash
1060: if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
1061: &GDBM_READER,0640)) {
1062: # --------------------------------------------------------- Get all assessments
1063:
1064: my %allkeys=();
1065: my %allassess=();
1066:
1067: my $stype=&gettype($safeeval);
1068:
1069: map {
1070: if ($_=~/^src\_(\d+)\.(\d+)$/) {
1071: my $mapid=$1;
1072: my $resid=$2;
1073: my $id=$mapid.'.'.$resid;
1074: my $srcf=$bighash{$_};
1075: if ($srcf=~/\.(problem|exam|quiz|assess|survey|form)$/) {
1076: my $symb=
1077: &Apache::lonnet::declutter($bighash{'map_id_'.$mapid}).
1078: '___'.$resid.'___'.
1079: &Apache::lonnet::declutter($srcf);
1080: $allassess{$symb}=$bighash{'title_'.$id};
1.8 www 1081:
1.6 www 1082: if ($stype eq 'assesscalc') {
1.5 www 1083: map {
1.11 www 1084: if (($_=~/^stores\_(.*)/) || ($_=~/^parameter\_(.*)/)) {
1.5 www 1085: my $key=$_;
1086: my $display=
1087: &Apache::lonnet::metadata($srcf,$key.'.display');
1088: unless ($display) {
1089: $display=
1090: &Apache::lonnet::metadata($srcf,$key.'.name');
1091: }
1092: $allkeys{$key}=$display;
1093: }
1094: } split(/\,/,&Apache::lonnet::metadata($srcf,'keys'));
1095: }
1096: }
1097: }
1098: } keys %bighash;
1099: untie(%bighash);
1100:
1101: #
1.11 www 1102: # %allkeys has a list of storage and parameter displays by unikey
1.5 www 1103: # %allassess has a list of all resource displays by symb
1104: #
1105: # -------------------- Find discrepancies between the course row table and this
1106: #
1107: my %f=&getformulas($safeeval);
1.6 www 1108: my $changed=0;
1109:
1110: my %current=();
1111: if ($stype eq 'assesscalc') {
1112: %current=%allkeys;
1113: } elsif ($stype eq 'studentcalc') {
1114: %current=%allassess;
1115: }
1116:
1117: my $maxrow=0;
1118: my %existing=();
1119:
1120: # ----------------------------------------------------------- Now obsolete rows
1.5 www 1121: map {
1.6 www 1122: if ($_=~/^A(\d+)/) {
1123: $maxrow=($1>$maxrow)?$1:$maxrow;
1124: $existing{$f{$_}}=1;
1.17 www 1125: unless ((defined($current{$f{$_}})) || (!$1)) {
1.6 www 1126: $f{$_}='!!! Obsolete';
1127: $changed=1;
1.5 www 1128: }
1129: }
1130: } keys %f;
1.6 www 1131:
1132: # -------------------------------------------------------- New and unknown keys
1133:
1134: map {
1135: unless ($existing{$_}) {
1136: $changed=1;
1137: $maxrow++;
1138: $f{'A'.$maxrow}=$_;
1139: }
1140: } keys %current;
1141:
1142: if ($changed) { &setformulas($safeeval,%f); }
1143:
1144: &setmaxrow($safeeval,$maxrow);
1145: &setrowlabels($safeeval,%current);
1.5 www 1146:
1147: } else {
1148: return 'Could not access course data';
1149: }
1150: }
1.3 www 1151:
1.24 www 1152: # ------------------------------------------------ Load data for one assessment
1.16 www 1153:
1.29 www 1154: sub loadstudent {
1.16 www 1155: my $safeeval=shift;
1156: my %c=();
1157: my %f=&getformulas($safeeval);
1158: map {
1.17 www 1159: if ($_=~/^A(\d+)/) {
1160: my $row=$1;
1.16 www 1161: unless ($f{$_}=~/^\!/) {
1.30 ! www 1162: my @assessdata=&exportsheet(&getuname($safeeval),
! 1163: &getudom($safeeval),
! 1164: 'assesscalc',$f{$_});
1.17 www 1165: my $index=0;
1.30 ! www 1166: map {
! 1167: if ($assessdata[$index]) {
! 1168: $c{$_.$row}=$assessdata[$index];
! 1169: unless ($_ eq 'A') {
! 1170: $f{$_.$row}='import';
! 1171: }
! 1172: }
! 1173: $index++;
! 1174: } ('A','B','C','D','E','F','G','H','I','J','K','L','M',
! 1175: 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
1.16 www 1176: }
1177: }
1178: } keys %f;
1.18 www 1179: &setformulas($safeeval,%f);
1.16 www 1180: &setconstants($safeeval,%c);
1181: }
1182:
1.24 www 1183: # --------------------------------------------------- Load data for one student
1184:
1.30 ! www 1185: sub loadcourse {
1.24 www 1186: my $safeeval=shift;
1187: my %c=();
1188: my %f=&getformulas($safeeval);
1189: map {
1190: if ($_=~/^A(\d+)/) {
1191: my $row=$1;
1.30 ! www 1192: unless (($f{$_}=~/^\!/)
! 1193:
! 1194: || ($row>5))
! 1195:
! 1196: {
! 1197: my @studentdata=&exportsheet(&getuname($safeeval),
! 1198: &getudom($safeeval),
! 1199: 'studentcalc');
1.24 www 1200: my $index=0;
1.30 ! www 1201: map {
! 1202: if ($studentdata[$index]) {
! 1203: $c{$_.$row}=$studentdata[$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:
1.30 ! www 1487: # ------------------------ If a new formula had been entered, go from work copy
! 1488:
! 1489: if ($ENV{'form.unewfield'}) {
! 1490: $r->print('<h2>Modified Workcopy</h2>');
! 1491: $ENV{'form.unewformula'}=~s/\'/\"/g;
! 1492: $r->print('<p>New formula: '.$ENV{'form.unewfield'}.'='.
! 1493: $ENV{'form.unewformula'}.'<p>');
! 1494: &setfilename($asheet,$ENV{'form.ufn'});
! 1495: &tmpread($asheet,
! 1496: $ENV{'form.unewfield'},$ENV{'form.unewformula'});
! 1497:
! 1498: } elsif ($ENV{'form.saveas'}) {
! 1499: &setfilename($asheet,$ENV{'form.ufn'});
! 1500: &tmpread($asheet);
! 1501: } else {
! 1502: &readsheet($asheet,$ENV{'form.ufn'});
! 1503: }
! 1504:
! 1505: # -------------------------------------------------- Print out user information
! 1506:
! 1507: unless (&gettype($asheet) eq 'classcalc') {
! 1508: $r->print('<p><b>User:</b> '.&getuname($asheet).
! 1509: '<br><b>Domain:</b> '.&getudom($asheet));
! 1510: if (&getcsec($asheet) eq '-1') {
! 1511: $r->print('<h3><font color=red>'.
! 1512: 'Not a student in this course</font></h3>');
! 1513: } else {
! 1514: $r->print('<br><b>Section/Group:</b> '.&getcsec($asheet));
! 1515: }
! 1516: }
! 1517:
! 1518: # ---------------------------------------------------------------- Course title
! 1519:
! 1520: $r->print('<h1>'.
! 1521: $ENV{'course.'.$ENV{'request.course.id'}.'.description'}.'</h1>');
! 1522:
! 1523:
1.22 www 1524: # ---------------------------------------------------- See if something to save
1.30 ! www 1525:
! 1526: if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
! 1527: my $fname='';
! 1528: if ($ENV{'form.saveas'} && ($fname=$ENV{'form.newfn'})) {
! 1529: $fname=~s/\W/\_/g;
! 1530: if ($fname eq 'default') { $fname='course_default'; }
! 1531: $fname.='_'.&gettype($asheet);
! 1532: &setfilename($asheet,$fname);
! 1533: $ENV{'form.ufn'}=$fname;
! 1534: $r->print('<p>Saving spreadsheet: '.
! 1535: &writesheet($asheet,$ENV{'form.makedefufn'}).'<p>');
! 1536: }
! 1537: }
! 1538:
1.14 www 1539: # ------------------------------------------------ Write the modified worksheet
1540:
1.30 ! www 1541: $r->print('<b>Current sheet:</b> '.&getfilename($asheet).'<p>');
! 1542:
! 1543: &tmpwrite($asheet);
! 1544:
! 1545: # ----------------------------------------------------------------- Save dialog
! 1546:
! 1547:
! 1548: if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
! 1549: my $fname=$ENV{'form.ufn'};
! 1550: $fname=~s/\_[^\_]+$//;
! 1551: if ($fname eq 'default') { $fname='course_default'; }
! 1552: $r->print('<input type=submit name=saveas value="Save as ...">'.
! 1553: '<input type=text size=20 name=newfn value="'.$fname.
! 1554: '"> (make default: <input type=checkbox name="makedefufn">)<p>');
! 1555: }
! 1556:
! 1557: $r->print(&hiddenfield('ufn',&getfilename($asheet)));
! 1558:
! 1559:
! 1560: # ----------------------------------------------------- Update sheet, load rows
1.14 www 1561:
1.29 www 1562: &updatesheet($asheet);
1563: &loadrows($asheet);
1.14 www 1564:
1565:
1.29 www 1566: my $calcoutput=&calcsheet($asheet);
1567: $r->print('<h3><font color=red>'.$calcoutput.'</h3></font>');
1.8 www 1568:
1.29 www 1569: &outsheet($r,$asheet);
1.10 www 1570: $r->print('</form></body></html>');
1.29 www 1571:
1572:
1.14 www 1573: # ------------------------------------------------------------------------ Done
1.7 www 1574: } else {
1575: # ----------------------------- Not in a course, or not allowed to modify parms
1576: $ENV{'user.error.msg'}=
1577: $r->uri.":opa:0:0:Cannot modify spreadsheet";
1578: return HTTP_NOT_ACCEPTABLE;
1579: }
1.3 www 1580: return OK;
1.28 www 1581:
1.1 www 1582: }
1583:
1584: 1;
1585: __END__
1586:
1587:
1588:
1589:
1590:
1591:
1592:
1593:
1594:
1595:
1596:
1597:
1598:
1599:
1600:
1601:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>