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