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