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