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