Annotation of loncom/interface/lonspreadsheet.pm, revision 1.14
1.1 www 1: # The LearningOnline Network with CAPA
2: # Spreadsheet/Grades Display Handler
3: #
1.11 www 4: # 11/11,11/15,11/27,12/04,12/05,12/06,12/07,12/08,12/09,12/11 Gerd Kortemeyer
1.1 www 5:
6: package Apache::lonspreadsheet;
7:
8: use strict;
9: use Safe;
1.3 www 10: use Safe::Hole;
1.1 www 11: use Opcode;
12: use Apache::lonnet;
1.7 www 13: use Apache::Constants qw(:common :http);
1.3 www 14: use HTML::TokeParser;
1.6 www 15: use GDBM_File;
1.3 www 16:
1.11 www 17: #
18: # These cache hashes need to be independent of user, resource and course
19: # (user and course are in the keys)
20: #
21: use vars qw(%spreadsheets %courserdatas %userrdatas);
22: #
23: # These global hashes are dependent on user, course and resource,
24: # and need to be initialized every time when a sheet is calculated
25: #
26: my %courseopt;
27: my %useropt;
28: my %parmhash;
29: my $csec;
30: my $uname;
31: my $udom;
32:
1.5 www 33: # =============================================================================
34: # ===================================== Implements an instance of a spreadsheet
1.4 www 35:
36: sub initsheet {
37: my $safeeval = new Safe;
38: my $safehole = new Safe::Hole;
39: $safeeval->permit("entereval");
40: $safeeval->permit(":base_math");
41: $safeeval->permit("sort");
42: $safeeval->deny(":base_io");
43: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
44: my $code=<<'ENDDEFS';
45: # ---------------------------------------------------- Inside of the safe space
46:
1.3 www 47: #
48: # f: formulas
1.4 www 49: # t: intermediate format (variable references expanded)
50: # v: output values
1.6 www 51: # c: preloaded constants (A-column)
52: # rl: row label
1.3 www 53:
1.4 www 54: %v=();
55: %t=();
56: %f=();
1.6 www 57: %c=();
58: %rl=();
59:
60: $maxrow=0;
1.5 www 61: $sheettype='';
62: $filename='';
1.1 www 63:
64: sub mask {
65: my ($lower,$upper)=@_;
66:
1.7 www 67: $lower=~/([A-Za-z]|\*)(\d+|\*)/;
1.1 www 68: my $la=$1;
69: my $ld=$2;
70:
1.7 www 71: $upper=~/([A-Za-z]|\*)(\d+|\*)/;
1.1 www 72: my $ua=$1;
73: my $ud=$2;
74: my $alpha='';
75: my $num='';
76:
77: if (($la eq '*') || ($ua eq '*')) {
1.7 www 78: $alpha='[A-Za-z]';
1.1 www 79: } else {
1.7 www 80: if (($la=~/[A-Z]/) && ($ua=~/[A-Z]/) ||
81: ($la=~/[a-z]/) && ($ua=~/[a-z]/)) {
82: $alpha='['.$la.'-'.$ua.']';
83: } else {
84: $alpha='['.$la.'-Za-'.$ua.']';
85: }
1.1 www 86: }
87:
88: if (($ld eq '*') || ($ud eq '*')) {
89: $num='\d+';
90: } else {
91: if (length($ld)!=length($ud)) {
92: $num.='(';
93: map {
94: $num.='['.$_.'-9]';
95: } ($ld=~m/\d/g);
96: if (length($ud)-length($ld)>1) {
97: $num.='|\d{'.(length($ld)+1).','.(length($ud)-1).'}';
98: }
99: $num.='|';
100: map {
101: $num.='[0-'.$_.']';
102: } ($ud=~m/\d/g);
103: $num.=')';
104: } else {
105: my @lda=($ld=~m/\d/g);
106: my @uda=($ud=~m/\d/g);
1.7 www 107: my $i; $j=0; $notdone=1;
108: for ($i=0;($i<=$#lda)&&($notdone);$i++) {
1.1 www 109: if ($lda[$i]==$uda[$i]) {
110: $num.=$lda[$i];
111: $j=$i;
1.7 www 112: } else {
113: $notdone=0;
1.1 www 114: }
115: }
116: if ($j<$#lda-1) {
117: $num.='('.$lda[$j+1];
118: for ($i=$j+2;$i<=$#lda;$i++) {
119: $num.='['.$lda[$i].'-9]';
120: }
121: if ($uda[$j+1]-$lda[$j+1]>1) {
122: $num.='|['.($lda[$j+1]+1).'-'.($uda[$j+1]-1).']\d{'.
123: ($#lda-$j-1).'}';
124: }
125: $num.='|'.$uda[$j+1];
126: for ($i=$j+2;$i<=$#uda;$i++) {
127: $num.='[0-'.$uda[$i].']';
128: }
129: $num.=')';
130: } else {
1.7 www 131: if ($lda[$#lda]!=$uda[$#uda]) {
132: $num.='['.$lda[$#lda].'-'.$uda[$#uda].']';
133: }
1.1 www 134: }
135: }
136: }
1.4 www 137: return '^'.$alpha.$num."\$";
1.1 www 138: }
139:
140: sub NUM {
141: my $mask=mask(@_);
142: my $num=0;
143: map {
144: $num++;
145: } grep /$mask/,keys %v;
146: return $num;
147: }
148:
149: sub BIN {
150: my ($low,$high,$lower,$upper)=@_;
151: my $mask=mask($lower,$upper);
152: my $num=0;
153: map {
154: if (($v{$_}>=$low) && ($v{$_}<=$high)) {
155: $num++;
156: }
157: } grep /$mask/,keys %v;
158: return $num;
159: }
160:
161:
162: sub SUM {
163: my $mask=mask(@_);
164: my $sum=0;
165: map {
166: $sum+=$v{$_};
167: } grep /$mask/,keys %v;
168: return $sum;
169: }
170:
171: sub MEAN {
172: my $mask=mask(@_);
173: my $sum=0; my $num=0;
174: map {
175: $sum+=$v{$_};
176: $num++;
177: } grep /$mask/,keys %v;
178: if ($num) {
179: return $sum/$num;
180: } else {
181: return undef;
182: }
183: }
184:
185: sub STDDEV {
186: my $mask=mask(@_);
187: my $sum=0; my $num=0;
188: map {
189: $sum+=$v{$_};
190: $num++;
191: } grep /$mask/,keys %v;
192: unless ($num>1) { return undef; }
193: my $mean=$sum/$num;
194: $sum=0;
195: map {
196: $sum+=($v{$_}-$mean)**2;
197: } grep /$mask/,keys %v;
198: return sqrt($sum/($num-1));
199: }
200:
201: sub PROD {
202: my $mask=mask(@_);
203: my $prod=1;
204: map {
205: $prod*=$v{$_};
206: } grep /$mask/,keys %v;
207: return $prod;
208: }
209:
210: sub MAX {
211: my $mask=mask(@_);
212: my $max='-';
213: map {
214: unless ($max) { $max=$v{$_}; }
215: if (($v{$_}>$max) || ($max eq '-')) { $max=$v{$_}; }
216: } grep /$mask/,keys %v;
217: return $max;
218: }
219:
220: sub MIN {
221: my $mask=mask(@_);
222: my $min='-';
223: map {
224: unless ($max) { $max=$v{$_}; }
225: if (($v{$_}<$min) || ($min eq '-')) { $min=$v{$_}; }
226: } grep /$mask/,keys %v;
227: return $min;
228: }
229:
230: sub SUMMAX {
231: my ($num,$lower,$upper)=@_;
232: my $mask=mask($lower,$upper);
233: my @inside=();
234: map {
235: $inside[$#inside+1]=$v{$_};
236: } grep /$mask/,keys %v;
237: @inside=sort(@inside);
238: my $sum=0; my $i;
239: for ($i=$#inside;(($i>$#inside-$num) && ($i>=0));$i--) {
240: $sum+=$inside[$i];
241: }
242: return $sum;
243: }
244:
245: sub SUMMIN {
246: my ($num,$lower,$upper)=@_;
247: my $mask=mask($lower,$upper);
248: my @inside=();
249: map {
250: $inside[$#inside+1]=$v{$_};
251: } grep /$mask/,keys %v;
252: @inside=sort(@inside);
253: my $sum=0; my $i;
254: for ($i=0;(($i<$num) && ($i<=$#inside));$i++) {
255: $sum+=$inside[$i];
256: }
257: return $sum;
258: }
259:
260: sub sett {
261: %t=();
262: map {
263: if ($f{$_}) {
1.6 www 264: if ($_=~/^A/) {
265: unless ($f{$_}=~/^\!/) {
266: $t{$_}=$c{$_};
267: }
268: } else {
269: $t{$_}=$f{$_};
1.7 www 270: $t{$_}=~s/\.\.+/\,/g;
271: $t{$_}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$v\{\'$2\'\}/g;
1.6 www 272: }
1.1 www 273: }
274: } keys %f;
275: }
276:
1.4 www 277: sub calc {
1.1 www 278: %v=();
1.4 www 279: &sett();
1.1 www 280: my $notfinished=1;
281: my $depth=0;
282: while ($notfinished) {
283: $notfinished=0;
284: map {
285: my $old=$v{$_};
1.4 www 286: $v{$_}=eval($t{$_});
1.1 www 287: if ($@) {
288: %v=();
289: return $@;
290: }
291: if ($v{$_} ne $old) { $notfinished=1; }
292: } keys %t;
293: $depth++;
294: if ($depth>100) {
295: %v=();
296: return 'Maximum calculation depth exceeded';
297: }
298: }
299: return '';
300: }
301:
1.6 www 302: sub outrow {
303: my $n=shift;
304: my @cols=();
305: if ($n) {
306: $cols[0]=$rl{$f{'A'.$n}};
307: } else {
308: $cols[0]='<b><font size=+1>Export</font></b>';
309: }
310: map {
311: my $fm=$f{$_.$n};
312: $fm=~s/[\'\"]/\&\#34;/g;
313: $cols[$#cols+1]="'$_$n','$fm'".'___eq___'.$v{$_.$n};
314: } ('A','B','C','D','E','F','G','H','I','J','K','L','M',
1.7 www 315: 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
316: 'a','b','c','d','e','f','g','h','i','j','k','l','m',
317: 'n','o','p','q','r','s','t','u','v','w','x','y','z');
1.6 www 318: return @cols;
319: }
320:
1.14 ! www 321: sub exportrowa {
! 322: my $rowa='';
! 323: map {
! 324: $rowa.=$v{$_.'0'}."','";
! 325: } ('A','B','C','D','E','F','G','H','I','J','K','L','M',
! 326: 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
! 327: $rowa=~s/\'\,\'$//;
! 328: return $rowa;
! 329: }
! 330:
1.4 www 331: # ------------------------------------------- End of "Inside of the safe space"
332: ENDDEFS
333: $safeeval->reval($code);
334: return $safeeval;
335: }
336:
337: # ------------------------------------------------ Add or change formula values
338:
339: sub setformulas {
340: my ($safeeval,@f)=@_;
1.6 www 341: $safeeval->reval('%f='."('".join("','",@f)."');");
342: }
343:
344: # ------------------------------------------------ Add or change formula values
345:
346: sub setconstants {
347: my ($safeeval,@c)=@_;
348: $safeeval->reval('%c='."('".join("','",@c)."');");
349: }
350:
351: # ------------------------------------------------ Add or change formula values
352:
353: sub setrowlabels {
354: my ($safeeval,@rl)=@_;
355: $safeeval->reval('%rl='."('".join("','",@rl)."');");
1.4 www 356: }
357:
358: # ------------------------------------------------------- Calculate spreadsheet
359:
360: sub calcsheet {
361: my $safeeval=shift;
362: $safeeval->reval('&calc();');
363: }
364:
365: # ------------------------------------------------------------------ Get values
366:
367: sub getvalues {
368: my $safeeval=shift;
369: return $safeeval->reval('%v');
370: }
371:
372: # ---------------------------------------------------------------- Get formulas
373:
374: sub getformulas {
375: my $safeeval=shift;
376: return $safeeval->reval('%f');
377: }
378:
1.5 www 379: # -------------------------------------------------------------------- Set type
380:
381: sub settype {
382: my ($safeeval,$type)=@_;
1.11 www 383: $safeeval->reval('$sheettype="'.$type.'";');
1.5 www 384: }
385:
386: # -------------------------------------------------------------------- Get type
387:
388: sub gettype {
389: my $safeeval=shift;
390: return $safeeval->reval('$sheettype');
391: }
1.6 www 392: # ------------------------------------------------------------------ Set maxrow
393:
394: sub setmaxrow {
395: my ($safeeval,$row)=@_;
396: $safeeval->reval('$maxrow='.$row.';');
397: }
398:
399: # ------------------------------------------------------------------ Get maxrow
400:
401: sub getmaxrow {
402: my $safeeval=shift;
403: return $safeeval->reval('$maxrow');
404: }
1.5 www 405:
1.6 www 406: # ---------------------------------------------------------------- Set filename
1.5 www 407:
408: sub setfilename {
409: my ($safeeval,$fn)=@_;
1.11 www 410: $safeeval->reval('$filename="'.$fn.'";');
1.5 www 411: }
412:
1.6 www 413: # ---------------------------------------------------------------- Get filename
1.5 www 414:
415: sub getfilename {
416: my $safeeval=shift;
417: return $safeeval->reval('$filename');
418: }
1.14 ! www 419:
! 420: # ------------------------------------------------------------- Export of A-row
! 421:
! 422: sub exportrow {
! 423: my $safeeval=shift;
! 424: return $safeeval->reval('&exportrowa()');
! 425: }
! 426:
1.5 www 427: # ========================================================== End of Spreadsheet
428: # =============================================================================
429:
430:
1.6 www 431: # --------------------------------------------- Produce output row n from sheet
432:
433: sub rown {
434: my ($safeeval,$n)=@_;
1.9 www 435: my $defaultbg=((($n-1)/5)==int(($n-1)/5))?'#E0E0':'#FFFF';
1.6 www 436: my $rowdata="\n<tr><td><b><font size=+1>$n</font></b></td>";
437: my $showf=0;
438: map {
1.9 www 439: my $bgcolor=$defaultbg.((($showf-1)/5==int(($showf-1)/5))?'99':'DD');
1.6 www 440: my ($fm,$vl)=split(/\_\_\_eq\_\_\_/,$_);
441: if ($showf==0) { $vl=$_; }
1.9 www 442: if ($showf<=1) { $bgcolor='#FFDDDD'; }
443: if (($n==0) && ($showf<=26)) { $bgcolor='#CCCCFF'; }
1.6 www 444: if ($showf>1) {
445: if ($vl eq '') {
1.9 www 446: $vl='<font size=+2 color='.$bgcolor.'>#</font>';
1.6 www 447: }
448: $rowdata.=
1.10 www 449: '<td bgcolor='.$bgcolor.'><a href="javascript:celledit('.$fm.');">'.$vl.
1.6 www 450: '</a></td>';
451: } else {
1.9 www 452: $rowdata.='<td bgcolor='.$bgcolor.'> '.$vl.' </td>';
1.6 www 453: }
454: $showf++;
455: } $safeeval->reval('&outrow('.$n.')');
456: return $rowdata.'</tr>';
457: }
458:
459: # ------------------------------------------------------------- Print out sheet
460:
461: sub outsheet {
462: my $safeeval=shift;
1.9 www 463: my $tabledata='<table border=2><tr><td colspan=2> </td>'.
464: '<td bgcolor=#FFDDDD><b>A Import</b></td>';
1.6 www 465: map {
466: $tabledata.="<td><b><font size=+1>$_</font></b></td>";
1.9 www 467: } ('B','C','D','E','F','G','H','I','J','K','L','M',
1.7 www 468: 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
469: 'a','b','c','d','e','f','g','h','i','j','k','l','m',
470: 'n','o','p','q','r','s','t','u','v','w','x','y','z');
1.6 www 471: $tabledata.='</tr>';
472: my $row;
473: my $maxrow=&getmaxrow($safeeval);
474: for ($row=0;$row<=$maxrow;$row++) {
475: $tabledata.=&rown($safeeval,$row);
476: }
477: $tabledata.='</table>';
478: }
479:
480:
1.5 www 481:
482: # --------------------------------------- Read spreadsheet formulas from a file
1.3 www 483:
1.5 www 484: sub readsheet {
1.8 www 485: my ($safeeval,$fn)=@_;
1.5 www 486: &setfilename($safeeval,$fn);
487: $fn=~/\.(\w+)/;
488: &settype($safeeval,$1);
1.4 www 489: my %f=();
1.10 www 490: unless ($spreadsheets{$fn}) {
491: $spreadsheets{$fn}='';
492: {
493: my $fh;
494: if ($fh=Apache::File->new($fn)) {
495: $spreadsheets{$fn}=join('',<$fh>);
496: }
497: }
1.3 www 498: }
499: {
1.10 www 500: my $parser=HTML::TokeParser->new(\$spreadsheets{$fn});
1.3 www 501: my $token;
502: while ($token=$parser->get_token) {
503: if ($token->[0] eq 'S') {
504: if ($token->[1] eq 'field') {
505: $f{$token->[2]->{'col'}.$token->[2]->{'row'}}=
506: $parser->get_text('/field');
507: }
508: }
509: }
510: }
1.5 www 511: &setformulas($safeeval,%f);
1.3 www 512: }
513:
1.10 www 514: # ----------------------------------------------- Make a temp copy of the sheet
515:
516: sub tmpwrite {
1.11 www 517: my ($safeeval,$tmpdir,$symb)=@_;
1.10 www 518: my $fn=$uname.'_'.$udom.'_spreadsheet_'.&getfilename($safeeval);
519: $fn=~s/\W/\_/g;
520: $fn=$tmpdir.$fn.'.tmp';
521: my $fh;
522: if ($fh=Apache::File->new('>'.$fn)) {
523: print $fh join("\n",&getformulas($safeeval));
524: }
525: }
526:
527: # ---------------------------------------------------------- Read the temp copy
528:
529: sub tmpread {
1.11 www 530: my ($safeeval,$tmpdir,$symb,$nfield,$nform)=@_;
1.10 www 531: my $fn=$uname.'_'.$udom.'_spreadsheet_'.&getfilename($safeeval);
532: $fn=~s/\W/\_/g;
533: $fn=$tmpdir.$fn.'.tmp';
534: my $fh;
535: my %fo=();
536: if ($fh=Apache::File->new($fn)) {
537: my $name;
538: while ($name=<$fh>) {
539: chomp($name);
540: my $value=<$fh>;
541: chomp($value);
542: $fo{$name}=$value;
543: }
544: }
545: $fo{$nfield}=$nform;
546: &setformulas($safeeval,%fo);
547: }
548:
1.3 www 549: # --------------------------------------------------------------- Read metadata
550:
551: sub readmeta {
552: my $fn=shift;
553: unless ($fn=~/\.meta$/) { $fn.='meta'; }
554: my $content;
555: my %returnhash=();
556: {
557: my $fh=Apache::File->new($fn);
558: $content=join('',<$fh>);
559: }
560: my $parser=HTML::TokeParser->new(\$content);
561: my $token;
562: while ($token=$parser->get_token) {
563: if ($token->[0] eq 'S') {
564: my $entry=$token->[1];
565: if (($entry eq 'stores') || ($entry eq 'parameter')) {
566: my $unikey=$entry;
567: $unikey.='_'.$token->[2]->{'part'};
568: $unikey.='_'.$token->[2]->{'name'};
569: $returnhash{$unikey}=$token->[2]->{'display'};
570: }
571: }
572: }
573: return %returnhash;
574: }
575:
1.11 www 576: # ================================================================== Parameters
577: # -------------------------------------------- Figure out a cascading parameter
578:
579: sub parmval {
580: my ($what,$symb)=@_;
581:
582: unless ($symb) { return ''; }
583: my $result='';
584:
585: my ($mapname,$id,$fn)=split(/\_\_\_/,$symb);
586: # ----------------------------------------------------- Cascading lookup scheme
1.12 www 587: my $rwhat=$what;
588: $what=~s/^parameter\_//;
589: $what=~s/\_/\./;
1.11 www 590:
591: my $symbparm=$symb.'.'.$what;
592: my $mapparm=$mapname.'___(all).'.$what;
593:
594: my $seclevel=
595: $ENV{'request.course.id'}.'.['.
596: $csec.'].'.$what;
597: my $seclevelr=
598: $ENV{'request.course.id'}.'.['.
599: $csec.'].'.$symbparm;
600: my $seclevelm=
601: $ENV{'request.course.id'}.'.['.
602: $csec.'].'.$mapparm;
603:
604: my $courselevel=
605: $ENV{'request.course.id'}.'.'.$what;
606: my $courselevelr=
607: $ENV{'request.course.id'}.'.'.$symbparm;
608: my $courselevelm=
609: $ENV{'request.course.id'}.'.'.$mapparm;
1.12 www 610:
1.11 www 611: # ---------------------------------------------------------- fourth, check user
612:
613: if ($uname) {
614:
615: if ($useropt{$courselevelr}) { return $useropt{$courselevelr}; }
616:
617: if ($useropt{$courselevelm}) { return $useropt{$courselevelm}; }
618:
619: if ($useropt{$courselevel}) { return $useropt{$courselevel}; }
620:
621: }
622:
623: # --------------------------------------------------------- third, check course
624:
625: if ($csec) {
626:
627: if ($courseopt{$seclevelr}) { return $courseopt{$seclevelr}; }
628:
629: if ($courseopt{$seclevelm}) { return $courseopt{$seclevelm}; }
630:
631: if ($courseopt{$seclevel}) { return $courseopt{$seclevel}; }
632:
633: }
634:
635: if ($courseopt{$courselevelr}) { return $courseopt{$courselevelr}; }
636:
637: if ($courseopt{$courselevelm}) { return $courseopt{$courselevelm}; }
638:
639: if ($courseopt{$courselevel}) { return $courseopt{$courselevel}; }
640:
641: # ----------------------------------------------------- second, check map parms
642:
643: my $thisparm=$parmhash{$symbparm};
644: if ($thisparm) { return $thisparm; }
645:
646: # -------------------------------------------------------- first, check default
647:
1.12 www 648: return &Apache::lonnet::metadata($fn,$rwhat.'.default');
1.11 www 649:
650: }
651:
652:
1.5 www 653: # ----------------------------------------------------------------- Update rows
654:
655: sub updaterows {
656: my $safeeval=shift;
657: my %bighash;
658: # -------------------------------------------------------------------- Tie hash
659: if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
660: &GDBM_READER,0640)) {
661: # --------------------------------------------------------- Get all assessments
662:
663: my %allkeys=();
664: my %allassess=();
665:
666: my $stype=&gettype($safeeval);
667:
668: map {
669: if ($_=~/^src\_(\d+)\.(\d+)$/) {
670: my $mapid=$1;
671: my $resid=$2;
672: my $id=$mapid.'.'.$resid;
673: my $srcf=$bighash{$_};
674: if ($srcf=~/\.(problem|exam|quiz|assess|survey|form)$/) {
675: my $symb=
676: &Apache::lonnet::declutter($bighash{'map_id_'.$mapid}).
677: '___'.$resid.'___'.
678: &Apache::lonnet::declutter($srcf);
679: $allassess{$symb}=$bighash{'title_'.$id};
1.8 www 680:
1.6 www 681: if ($stype eq 'assesscalc') {
1.5 www 682: map {
1.11 www 683: if (($_=~/^stores\_(.*)/) || ($_=~/^parameter\_(.*)/)) {
1.5 www 684: my $key=$_;
685: my $display=
686: &Apache::lonnet::metadata($srcf,$key.'.display');
687: unless ($display) {
688: $display=
689: &Apache::lonnet::metadata($srcf,$key.'.name');
690: }
691: $allkeys{$key}=$display;
692: }
693: } split(/\,/,&Apache::lonnet::metadata($srcf,'keys'));
694: }
695: }
696: }
697: } keys %bighash;
698: untie(%bighash);
699:
700: #
1.11 www 701: # %allkeys has a list of storage and parameter displays by unikey
1.5 www 702: # %allassess has a list of all resource displays by symb
703: #
704: # -------------------- Find discrepancies between the course row table and this
705: #
706: my %f=&getformulas($safeeval);
1.6 www 707: my $changed=0;
708:
709: my %current=();
710: if ($stype eq 'assesscalc') {
711: %current=%allkeys;
712: } elsif ($stype eq 'studentcalc') {
713: %current=%allassess;
714: }
715:
716: my $maxrow=0;
717: my %existing=();
718:
719: # ----------------------------------------------------------- Now obsolete rows
1.5 www 720: map {
1.6 www 721: if ($_=~/^A(\d+)/) {
722: $maxrow=($1>$maxrow)?$1:$maxrow;
723: $existing{$f{$_}}=1;
724: unless (defined($current{$f{$_}})) {
725: $f{$_}='!!! Obsolete';
726: $changed=1;
1.5 www 727: }
728: }
729: } keys %f;
1.6 www 730:
731: # -------------------------------------------------------- New and unknown keys
732:
733: map {
734: unless ($existing{$_}) {
735: $changed=1;
736: $maxrow++;
737: $f{'A'.$maxrow}=$_;
738: }
739: } keys %current;
740:
741: if ($changed) { &setformulas($safeeval,%f); }
742:
743: &setmaxrow($safeeval,$maxrow);
744: &setrowlabels($safeeval,%current);
1.5 www 745:
746: } else {
747: return 'Could not access course data';
748: }
749: }
1.3 www 750:
1.6 www 751: # ------------------------------------------------ Load data for one assessment
752:
753: sub rowaassess {
1.11 www 754: my ($safeeval,$symb)=@_;
1.6 www 755: my $uhome=&Apache::lonnet::homeserver($uname,$udom);
756: my $namespace;
757: unless ($namespace=$ENV{'request.course.id'}) { return ''; }
1.11 www 758:
759: # ----------------------------------------------------------- Get stored values
760: my $answer=&Apache::lonnet::reply(
761: "restore:$udom:$uname:$namespace:$symb",$uhome);
1.6 www 762: my %returnhash=();
763: map {
764: my ($name,$value)=split(/\=/,$_);
1.11 www 765: $returnhash{&Apache::lonnet::unescape($name)}=
766: &Apache::lonnet::unescape($value);
1.6 www 767: } split(/\&/,$answer);
768: my $version;
769: for ($version=1;$version<=$returnhash{'version'};$version++) {
770: map {
771: $returnhash{$_}=$returnhash{$version.':'.$_};
772: } split(/\:/,$returnhash{$version.':keys'});
773: }
1.11 www 774: # ----------------------------- returnhash now has all stores for this resource
775:
776: # ---------------------------- initialize coursedata and userdata for this user
777: %courseopt=();
778: %useropt=();
779: my $uhome=&Apache::lonnet::homeserver($uname,$udom);
780: unless ($uhome eq 'no_host') {
781: # -------------------------------------------------------------- Get coursedata
1.13 www 782: unless
783: ((time-$courserdatas{$ENV{'request.course.id'}.'.last_cache'})<120) {
1.11 www 784: my $reply=&Apache::lonnet::reply('dump:'.
785: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'}.':'.
786: $ENV{'course.'.$ENV{'request.course.id'}.'.num'}.':resourcedata',
787: $ENV{'course.'.$ENV{'request.course.id'}.'.home'});
788: if ($reply!~/^error\:/) {
789: $courserdatas{$ENV{'request.course.id'}}=$reply;
1.13 www 790: $courserdatas{$ENV{'request.course.id'}.'.last_cache'}=time;
1.11 www 791: }
792: }
793: map {
794: my ($name,$value)=split(/\=/,$_);
795: $courseopt{&Apache::lonnet::unescape($name)}=
796: &Apache::lonnet::unescape($value);
797: } split(/\&/,$courserdatas{$ENV{'request.course.id'}});
798: # --------------------------------------------------- Get userdata (if present)
1.13 www 799: unless
800: ((time-$userrdatas{$uname.'___'.$udom.'.last_cache'})<120) {
1.11 www 801: my $reply=
802: &Apache::lonnet::reply('dump:'.$udom.':'.$uname.':resourcedata',$uhome);
803: if ($reply!~/^error\:/) {
804: $userrdatas{$uname.'___'.$udom}=$reply;
1.13 www 805: $userrdatas{$uname.'___'.$udom.'.last_cache'}=time;
1.11 www 806: }
807: }
808: map {
809: my ($name,$value)=split(/\=/,$_);
810: $useropt{&Apache::lonnet::unescape($name)}=
811: &Apache::lonnet::unescape($value);
812: } split(/\&/,$userrdatas{$uname.'___'.$udom});
813: }
814: # -- now courseopt, useropt initialized for this user and course (used parmval)
1.6 www 815:
816: my %c=();
817: my %f=&getformulas($safeeval);
818: map {
819: if ($_=~/^A/) {
820: unless ($f{$_}=~/^\!/) {
1.11 www 821: if ($f{$_}=~/^parameter/) {
822: $c{$_}=&parmval($f{$_},$symb);
823: } else {
824: $c{$_}=$returnhash{$f{$_}};
825: }
826: }
1.6 www 827: }
828: } keys %f;
1.11 www 829:
1.6 www 830: &setconstants($safeeval,%c);
831: }
832:
1.10 www 833: # --------------------------------------------------------- Various form fields
834:
835: sub textfield {
836: my ($title,$name,$value)=@_;
837: return "\n<p><b>$title:</b><br>".
838: '<input type=text name="'.$name.'" size=80 value="'.$value.'">';
839: }
840:
841: sub hiddenfield {
842: my ($name,$value)=@_;
843: return "\n".'<input type=hidden name="'.$name.'" value="'.$value.'">';
844: }
845:
846: sub selectbox {
847: my ($title,$name,$value,%options)=@_;
848: my $selout="\n<p><b>$title:</b><br>".'<select name="'.$name.'">';
849: map {
850: $selout.='<option value="'.$_.'"';
851: if ($_ eq $value) { $selout.=' selected'; }
852: $selout.='>'.$options{$_}.'</option>';
853: } sort keys %options;
854: return $selout.'</select>';
855: }
856:
1.12 www 857: # ================================================================ Main handler
1.3 www 858:
859: sub handler {
1.7 www 860: my $r=shift;
861:
1.11 www 862: $uname='';
863: $udom='';
864: $csec='';
865:
1.7 www 866: if ($r->header_only) {
867: $r->content_type('text/html');
868: $r->send_http_header;
869: return OK;
870: }
1.3 www 871:
1.7 www 872: # ----------------------------------------------------- Needs to be in a course
1.3 www 873:
1.7 www 874: if (($ENV{'request.course.fn'}) ||
875: ($ENV{'request.state'} eq 'construct')) {
1.10 www 876:
877: # --------------------------- Get query string for limited number of parameters
878: map {
879: my ($name, $value) = split(/=/,$_);
880: $value =~ tr/+/ /;
881: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.11 www 882: if (($name eq 'uname') || ($name eq 'udom') || ($name eq 'usymb')) {
1.10 www 883: unless ($ENV{'form.'.$name}) {
884: $ENV{'form.'.$name}=$value;
885: }
886: }
887: } (split(/&/,$ENV{'QUERY_STRING'}));
888:
889: # ------------------------------------------- Nothing there? Must be login user
890:
891: unless ($ENV{'form.uname'}) {
1.11 www 892: $uname=$ENV{'user.name'};
893: $udom=$ENV{'user.domain'};
894: } else {
895: $uname=$ENV{'form.uname'};
896: $udom=$ENV{'form.udom'};
1.10 www 897: }
1.14 ! www 898: # ----------------------------------------------------------- Change of target?
! 899:
! 900: my $reroute=($ENV{'utarget'} eq 'export');
! 901:
1.10 www 902: # ------------------------------------------------------------------- Open page
903:
1.7 www 904: $r->content_type('text/html');
1.11 www 905: $r->header_out('Cache-control','no-cache');
906: $r->header_out('Pragma','no-cache');
1.7 www 907: $r->send_http_header;
1.3 www 908:
1.14 ! www 909: # --------------------------------------------------------------- Screen output
! 910:
! 911: unless ($reroute) {
1.10 www 912: $r->print('<html><head><title>LON-CAPA Spreadsheet</title>');
913: $r->print(<<ENDSCRIPT);
914: <script language="JavaScript">
915:
916: function celledit(cn,cf) {
917: var cnf=prompt(cn,cf);
918: if (cnf!=null) {
919: document.sheet.unewfield.value=cn;
920: document.sheet.unewformula.value=cnf;
921: document.sheet.submit();
922: }
923: }
924:
925: </script>
926: ENDSCRIPT
927: $r->print('</head><body bgcolor="#FFFFFF">'.
928: '<form action="'.$r->uri.'" name=sheet method=post>'.
929: &hiddenfield('uname',$ENV{'form.uname'}).
930: &hiddenfield('udom',$ENV{'form.udom'}).
931: &hiddenfield('usymb',$ENV{'form.usymb'}).
932: &hiddenfield('unewfield','').
933: &hiddenfield('unewformula',''));
1.14 ! www 934: }
! 935:
! 936: # ---------------------------------------- Read new sheet or modified worksheet
! 937:
1.4 www 938: my $sheetone=initsheet();
1.10 www 939: if ($ENV{'form.unewfield'}) {
940: $r->print('<h2>Modified Workcopy</h2>');
941: $ENV{'form.unewformula'}=~s/\'/\"/g;
942: $r->print('New formula: '.$ENV{'form.unewfield'}.'='.
943: $ENV{'form.unewformula'}.'<br>');
944: &tmpread($sheetone,$r->dir_config('lonDaemons').'/tmp/',
1.11 www 945: $ENV{'form.usymb'},
1.10 www 946: $ENV{'form.unewfield'},$ENV{'form.unewformula'});
947: &setfilename($sheetone,$r->filename);
948: $r->filename=~/\.(\w+)/;
949: &settype($sheetone,$1);
1.11 www 950: } else {
951: &readsheet($sheetone,$r->filename);
952: }
1.14 ! www 953:
! 954: # --------------------------------------------- See if all import rows uptodate
! 955:
1.11 www 956: if (tie(%parmhash,'GDBM_File',
957: $ENV{'request.course.fn'}.'_parms.db',&GDBM_READER,0640)) {
958: $csec=&Apache::lonnet::usection($udom,$uname,$ENV{'request.course.id'});
959: if ($csec eq '-1') {
960: $r->print('<h3><font color=red>'.
961: "User '$uname' at domain '$udom' not a student in this course</font></h3>");
962: }
963: &updaterows($sheetone);
964: untie(%parmhash);
1.10 www 965: } else {
1.11 www 966: $r->print('<h3><font color=red>'.
967: 'Could not initialize import fields (not in a course)</font></h3>');
968: }
1.14 ! www 969:
! 970: # ------------------------------------------------ Write the modified worksheet
! 971:
1.11 www 972: &tmpwrite($sheetone,$r->dir_config('lonDaemons').'/tmp/',
973: $ENV{'form.usymb'});
1.14 ! www 974:
! 975: # ----------------------------------------------------- Print user, course, etc
! 976: unless ($reroute) {
1.11 www 977: $r->print("<b>User '$uname' at domain '$udom' for '".
978: $ENV{'course.'.$ENV{'request.course.id'}.'.description'}."'");
979: if ($csec) {
980: $r->print(", group/section '$csec'");
981: }
982: $r->print("</b>\n");
1.14 ! www 983: }
! 984: # -------------------------------------------------------- Import and calculate
! 985:
1.11 www 986: if (&gettype($sheetone) eq 'assesscalc') {
987: &rowaassess($sheetone,$ENV{'form.usymb'});
1.10 www 988: }
1.6 www 989: &calcsheet($sheetone);
1.14 ! www 990:
! 991: # ------------------------------------------------------- Print or export sheet
! 992: unless ($reroute) {
1.6 www 993: $r->print(&outsheet($sheetone));
1.8 www 994:
1.10 www 995: $r->print('</form></body></html>');
1.14 ! www 996: } else {
! 997: $r->print(&exportrow($sheetone));
! 998: }
! 999: # ------------------------------------------------------------------------ Done
1.7 www 1000: } else {
1001: # ----------------------------- Not in a course, or not allowed to modify parms
1002: $ENV{'user.error.msg'}=
1003: $r->uri.":opa:0:0:Cannot modify spreadsheet";
1004: return HTTP_NOT_ACCEPTABLE;
1005: }
1.3 www 1006: return OK;
1.1 www 1007: }
1008:
1009: 1;
1010: __END__
1011:
1012:
1013:
1014:
1015:
1016:
1017:
1018:
1019:
1020:
1021:
1022:
1023:
1024:
1025:
1026:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>