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