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