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