Annotation of loncom/interface/lonspreadsheet.pm, revision 1.11
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.4 www 321: # ------------------------------------------- End of "Inside of the safe space"
322: ENDDEFS
323: $safeeval->reval($code);
324: return $safeeval;
325: }
326:
327: # ------------------------------------------------ Add or change formula values
328:
329: sub setformulas {
330: my ($safeeval,@f)=@_;
1.6 www 331: $safeeval->reval('%f='."('".join("','",@f)."');");
332: }
333:
334: # ------------------------------------------------ Add or change formula values
335:
336: sub setconstants {
337: my ($safeeval,@c)=@_;
338: $safeeval->reval('%c='."('".join("','",@c)."');");
339: }
340:
341: # ------------------------------------------------ Add or change formula values
342:
343: sub setrowlabels {
344: my ($safeeval,@rl)=@_;
345: $safeeval->reval('%rl='."('".join("','",@rl)."');");
1.4 www 346: }
347:
348: # ------------------------------------------------------- Calculate spreadsheet
349:
350: sub calcsheet {
351: my $safeeval=shift;
352: $safeeval->reval('&calc();');
353: }
354:
355: # ------------------------------------------------------------------ Get values
356:
357: sub getvalues {
358: my $safeeval=shift;
359: return $safeeval->reval('%v');
360: }
361:
362: # ---------------------------------------------------------------- Get formulas
363:
364: sub getformulas {
365: my $safeeval=shift;
366: return $safeeval->reval('%f');
367: }
368:
1.5 www 369: # -------------------------------------------------------------------- Set type
370:
371: sub settype {
372: my ($safeeval,$type)=@_;
1.11 ! www 373: $safeeval->reval('$sheettype="'.$type.'";');
1.5 www 374: }
375:
376: # -------------------------------------------------------------------- Get type
377:
378: sub gettype {
379: my $safeeval=shift;
380: return $safeeval->reval('$sheettype');
381: }
1.6 www 382: # ------------------------------------------------------------------ Set maxrow
383:
384: sub setmaxrow {
385: my ($safeeval,$row)=@_;
386: $safeeval->reval('$maxrow='.$row.';');
387: }
388:
389: # ------------------------------------------------------------------ Get maxrow
390:
391: sub getmaxrow {
392: my $safeeval=shift;
393: return $safeeval->reval('$maxrow');
394: }
1.5 www 395:
1.6 www 396: # ---------------------------------------------------------------- Set filename
1.5 www 397:
398: sub setfilename {
399: my ($safeeval,$fn)=@_;
1.11 ! www 400: $safeeval->reval('$filename="'.$fn.'";');
1.5 www 401: }
402:
1.6 www 403: # ---------------------------------------------------------------- Get filename
1.5 www 404:
405: sub getfilename {
406: my $safeeval=shift;
407: return $safeeval->reval('$filename');
408: }
409:
410: # ========================================================== End of Spreadsheet
411: # =============================================================================
412:
413:
1.6 www 414: # --------------------------------------------- Produce output row n from sheet
415:
416: sub rown {
417: my ($safeeval,$n)=@_;
1.9 www 418: my $defaultbg=((($n-1)/5)==int(($n-1)/5))?'#E0E0':'#FFFF';
1.6 www 419: my $rowdata="\n<tr><td><b><font size=+1>$n</font></b></td>";
420: my $showf=0;
421: map {
1.9 www 422: my $bgcolor=$defaultbg.((($showf-1)/5==int(($showf-1)/5))?'99':'DD');
1.6 www 423: my ($fm,$vl)=split(/\_\_\_eq\_\_\_/,$_);
424: if ($showf==0) { $vl=$_; }
1.9 www 425: if ($showf<=1) { $bgcolor='#FFDDDD'; }
426: if (($n==0) && ($showf<=26)) { $bgcolor='#CCCCFF'; }
1.6 www 427: if ($showf>1) {
428: if ($vl eq '') {
1.9 www 429: $vl='<font size=+2 color='.$bgcolor.'>#</font>';
1.6 www 430: }
431: $rowdata.=
1.10 www 432: '<td bgcolor='.$bgcolor.'><a href="javascript:celledit('.$fm.');">'.$vl.
1.6 www 433: '</a></td>';
434: } else {
1.9 www 435: $rowdata.='<td bgcolor='.$bgcolor.'> '.$vl.' </td>';
1.6 www 436: }
437: $showf++;
438: } $safeeval->reval('&outrow('.$n.')');
439: return $rowdata.'</tr>';
440: }
441:
442: # ------------------------------------------------------------- Print out sheet
443:
444: sub outsheet {
445: my $safeeval=shift;
1.9 www 446: my $tabledata='<table border=2><tr><td colspan=2> </td>'.
447: '<td bgcolor=#FFDDDD><b>A Import</b></td>';
1.6 www 448: map {
449: $tabledata.="<td><b><font size=+1>$_</font></b></td>";
1.9 www 450: } ('B','C','D','E','F','G','H','I','J','K','L','M',
1.7 www 451: 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
452: 'a','b','c','d','e','f','g','h','i','j','k','l','m',
453: 'n','o','p','q','r','s','t','u','v','w','x','y','z');
1.6 www 454: $tabledata.='</tr>';
455: my $row;
456: my $maxrow=&getmaxrow($safeeval);
457: for ($row=0;$row<=$maxrow;$row++) {
458: $tabledata.=&rown($safeeval,$row);
459: }
460: $tabledata.='</table>';
461: }
462:
463:
1.5 www 464:
465: # --------------------------------------- Read spreadsheet formulas from a file
1.3 www 466:
1.5 www 467: sub readsheet {
1.8 www 468: my ($safeeval,$fn)=@_;
1.5 www 469: &setfilename($safeeval,$fn);
470: $fn=~/\.(\w+)/;
471: &settype($safeeval,$1);
1.4 www 472: my %f=();
1.10 www 473: unless ($spreadsheets{$fn}) {
474: $spreadsheets{$fn}='';
475: {
476: my $fh;
477: if ($fh=Apache::File->new($fn)) {
478: $spreadsheets{$fn}=join('',<$fh>);
479: }
480: }
1.3 www 481: }
482: {
1.10 www 483: my $parser=HTML::TokeParser->new(\$spreadsheets{$fn});
1.3 www 484: my $token;
485: while ($token=$parser->get_token) {
486: if ($token->[0] eq 'S') {
487: if ($token->[1] eq 'field') {
488: $f{$token->[2]->{'col'}.$token->[2]->{'row'}}=
489: $parser->get_text('/field');
490: }
491: }
492: }
493: }
1.5 www 494: &setformulas($safeeval,%f);
1.3 www 495: }
496:
1.10 www 497: # ----------------------------------------------- Make a temp copy of the sheet
498:
499: sub tmpwrite {
1.11 ! www 500: my ($safeeval,$tmpdir,$symb)=@_;
1.10 www 501: my $fn=$uname.'_'.$udom.'_spreadsheet_'.&getfilename($safeeval);
502: $fn=~s/\W/\_/g;
503: $fn=$tmpdir.$fn.'.tmp';
504: my $fh;
505: if ($fh=Apache::File->new('>'.$fn)) {
506: print $fh join("\n",&getformulas($safeeval));
507: }
508: }
509:
510: # ---------------------------------------------------------- Read the temp copy
511:
512: sub tmpread {
1.11 ! www 513: my ($safeeval,$tmpdir,$symb,$nfield,$nform)=@_;
1.10 www 514: my $fn=$uname.'_'.$udom.'_spreadsheet_'.&getfilename($safeeval);
515: $fn=~s/\W/\_/g;
516: $fn=$tmpdir.$fn.'.tmp';
517: my $fh;
518: my %fo=();
519: if ($fh=Apache::File->new($fn)) {
520: my $name;
521: while ($name=<$fh>) {
522: chomp($name);
523: my $value=<$fh>;
524: chomp($value);
525: $fo{$name}=$value;
526: }
527: }
528: $fo{$nfield}=$nform;
529: &setformulas($safeeval,%fo);
530: }
531:
1.3 www 532: # --------------------------------------------------------------- Read metadata
533:
534: sub readmeta {
535: my $fn=shift;
536: unless ($fn=~/\.meta$/) { $fn.='meta'; }
537: my $content;
538: my %returnhash=();
539: {
540: my $fh=Apache::File->new($fn);
541: $content=join('',<$fh>);
542: }
543: my $parser=HTML::TokeParser->new(\$content);
544: my $token;
545: while ($token=$parser->get_token) {
546: if ($token->[0] eq 'S') {
547: my $entry=$token->[1];
548: if (($entry eq 'stores') || ($entry eq 'parameter')) {
549: my $unikey=$entry;
550: $unikey.='_'.$token->[2]->{'part'};
551: $unikey.='_'.$token->[2]->{'name'};
552: $returnhash{$unikey}=$token->[2]->{'display'};
553: }
554: }
555: }
556: return %returnhash;
557: }
558:
1.11 ! www 559: # ================================================================== Parameters
! 560: # -------------------------------------------- Figure out a cascading parameter
! 561:
! 562: sub parmval {
! 563: my ($what,$symb)=@_;
! 564:
! 565: unless ($symb) { return ''; }
! 566: my $result='';
! 567:
! 568: my ($mapname,$id,$fn)=split(/\_\_\_/,$symb);
! 569: # ----------------------------------------------------- Cascading lookup scheme
! 570:
! 571: my $symbparm=$symb.'.'.$what;
! 572: my $mapparm=$mapname.'___(all).'.$what;
! 573:
! 574: my $seclevel=
! 575: $ENV{'request.course.id'}.'.['.
! 576: $csec.'].'.$what;
! 577: my $seclevelr=
! 578: $ENV{'request.course.id'}.'.['.
! 579: $csec.'].'.$symbparm;
! 580: my $seclevelm=
! 581: $ENV{'request.course.id'}.'.['.
! 582: $csec.'].'.$mapparm;
! 583:
! 584: my $courselevel=
! 585: $ENV{'request.course.id'}.'.'.$what;
! 586: my $courselevelr=
! 587: $ENV{'request.course.id'}.'.'.$symbparm;
! 588: my $courselevelm=
! 589: $ENV{'request.course.id'}.'.'.$mapparm;
! 590: # ---------------------------------------------------------- fourth, check user
! 591:
! 592: if ($uname) {
! 593:
! 594: if ($useropt{$courselevelr}) { return $useropt{$courselevelr}; }
! 595:
! 596: if ($useropt{$courselevelm}) { return $useropt{$courselevelm}; }
! 597:
! 598: if ($useropt{$courselevel}) { return $useropt{$courselevel}; }
! 599:
! 600: }
! 601:
! 602: # --------------------------------------------------------- third, check course
! 603:
! 604: if ($csec) {
! 605:
! 606: if ($courseopt{$seclevelr}) { return $courseopt{$seclevelr}; }
! 607:
! 608: if ($courseopt{$seclevelm}) { return $courseopt{$seclevelm}; }
! 609:
! 610: if ($courseopt{$seclevel}) { return $courseopt{$seclevel}; }
! 611:
! 612: }
! 613:
! 614: if ($courseopt{$courselevelr}) { return $courseopt{$courselevelr}; }
! 615:
! 616: if ($courseopt{$courselevelm}) { return $courseopt{$courselevelm}; }
! 617:
! 618: if ($courseopt{$courselevel}) { return $courseopt{$courselevel}; }
! 619:
! 620: # ----------------------------------------------------- second, check map parms
! 621:
! 622: my $thisparm=$parmhash{$symbparm};
! 623: if ($thisparm) { return $thisparm; }
! 624:
! 625: # -------------------------------------------------------- first, check default
! 626:
! 627: return &Apache::lonnet::metadata($fn,$what.'.default');
! 628:
! 629: }
! 630:
! 631:
1.5 www 632: # ----------------------------------------------------------------- Update rows
633:
634: sub updaterows {
635: my $safeeval=shift;
636: my %bighash;
637: # -------------------------------------------------------------------- Tie hash
638: if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
639: &GDBM_READER,0640)) {
640: # --------------------------------------------------------- Get all assessments
641:
642: my %allkeys=();
643: my %allassess=();
644:
645: my $stype=&gettype($safeeval);
646:
647: map {
648: if ($_=~/^src\_(\d+)\.(\d+)$/) {
649: my $mapid=$1;
650: my $resid=$2;
651: my $id=$mapid.'.'.$resid;
652: my $srcf=$bighash{$_};
653: if ($srcf=~/\.(problem|exam|quiz|assess|survey|form)$/) {
654: my $symb=
655: &Apache::lonnet::declutter($bighash{'map_id_'.$mapid}).
656: '___'.$resid.'___'.
657: &Apache::lonnet::declutter($srcf);
658: $allassess{$symb}=$bighash{'title_'.$id};
1.8 www 659:
1.6 www 660: if ($stype eq 'assesscalc') {
1.5 www 661: map {
1.11 ! www 662: if (($_=~/^stores\_(.*)/) || ($_=~/^parameter\_(.*)/)) {
1.5 www 663: my $key=$_;
664: my $display=
665: &Apache::lonnet::metadata($srcf,$key.'.display');
666: unless ($display) {
667: $display=
668: &Apache::lonnet::metadata($srcf,$key.'.name');
669: }
670: $allkeys{$key}=$display;
671: }
672: } split(/\,/,&Apache::lonnet::metadata($srcf,'keys'));
673: }
674: }
675: }
676: } keys %bighash;
677: untie(%bighash);
678:
679: #
1.11 ! www 680: # %allkeys has a list of storage and parameter displays by unikey
1.5 www 681: # %allassess has a list of all resource displays by symb
682: #
683: # -------------------- Find discrepancies between the course row table and this
684: #
685: my %f=&getformulas($safeeval);
1.6 www 686: my $changed=0;
687:
688: my %current=();
689: if ($stype eq 'assesscalc') {
690: %current=%allkeys;
691: } elsif ($stype eq 'studentcalc') {
692: %current=%allassess;
693: }
694:
695: my $maxrow=0;
696: my %existing=();
697:
698: # ----------------------------------------------------------- Now obsolete rows
1.5 www 699: map {
1.6 www 700: if ($_=~/^A(\d+)/) {
701: $maxrow=($1>$maxrow)?$1:$maxrow;
702: $existing{$f{$_}}=1;
703: unless (defined($current{$f{$_}})) {
704: $f{$_}='!!! Obsolete';
705: $changed=1;
1.5 www 706: }
707: }
708: } keys %f;
1.6 www 709:
710: # -------------------------------------------------------- New and unknown keys
711:
712: map {
713: unless ($existing{$_}) {
714: $changed=1;
715: $maxrow++;
716: $f{'A'.$maxrow}=$_;
717: }
718: } keys %current;
719:
720: if ($changed) { &setformulas($safeeval,%f); }
721:
722: &setmaxrow($safeeval,$maxrow);
723: &setrowlabels($safeeval,%current);
1.5 www 724:
725: } else {
726: return 'Could not access course data';
727: }
728: }
1.3 www 729:
1.6 www 730: # ------------------------------------------------ Load data for one assessment
731:
732: sub rowaassess {
1.11 ! www 733: my ($safeeval,$symb)=@_;
1.6 www 734: my $uhome=&Apache::lonnet::homeserver($uname,$udom);
735: my $namespace;
736: unless ($namespace=$ENV{'request.course.id'}) { return ''; }
1.11 ! www 737:
! 738: # ----------------------------------------------------------- Get stored values
! 739: my $answer=&Apache::lonnet::reply(
! 740: "restore:$udom:$uname:$namespace:$symb",$uhome);
1.6 www 741: my %returnhash=();
742: map {
743: my ($name,$value)=split(/\=/,$_);
1.11 ! www 744: $returnhash{&Apache::lonnet::unescape($name)}=
! 745: &Apache::lonnet::unescape($value);
1.6 www 746: } split(/\&/,$answer);
747: my $version;
748: for ($version=1;$version<=$returnhash{'version'};$version++) {
749: map {
750: $returnhash{$_}=$returnhash{$version.':'.$_};
751: } split(/\:/,$returnhash{$version.':keys'});
752: }
1.11 ! www 753: # ----------------------------- returnhash now has all stores for this resource
! 754:
! 755: # ---------------------------- initialize coursedata and userdata for this user
! 756: %courseopt=();
! 757: %useropt=();
! 758: my $uhome=&Apache::lonnet::homeserver($uname,$udom);
! 759: unless ($uhome eq 'no_host') {
! 760: # -------------------------------------------------------------- Get coursedata
! 761: unless ($courserdatas{$ENV{'request.course.id'}}) {
! 762: my $reply=&Apache::lonnet::reply('dump:'.
! 763: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'}.':'.
! 764: $ENV{'course.'.$ENV{'request.course.id'}.'.num'}.':resourcedata',
! 765: $ENV{'course.'.$ENV{'request.course.id'}.'.home'});
! 766: if ($reply!~/^error\:/) {
! 767: $courserdatas{$ENV{'request.course.id'}}=$reply;
! 768: }
! 769: }
! 770: map {
! 771: my ($name,$value)=split(/\=/,$_);
! 772: $courseopt{&Apache::lonnet::unescape($name)}=
! 773: &Apache::lonnet::unescape($value);
! 774: } split(/\&/,$courserdatas{$ENV{'request.course.id'}});
! 775: # --------------------------------------------------- Get userdata (if present)
! 776: unless ($userrdatas{$uname.'___'.$udom}) {
! 777: my $reply=
! 778: &Apache::lonnet::reply('dump:'.$udom.':'.$uname.':resourcedata',$uhome);
! 779: if ($reply!~/^error\:/) {
! 780: $userrdatas{$uname.'___'.$udom}=$reply;
! 781: }
! 782: }
! 783: map {
! 784: my ($name,$value)=split(/\=/,$_);
! 785: $useropt{&Apache::lonnet::unescape($name)}=
! 786: &Apache::lonnet::unescape($value);
! 787: } split(/\&/,$userrdatas{$uname.'___'.$udom});
! 788: }
! 789: # -- now courseopt, useropt initialized for this user and course (used parmval)
1.6 www 790:
791: my %c=();
792: my %f=&getformulas($safeeval);
793: map {
794: if ($_=~/^A/) {
795: unless ($f{$_}=~/^\!/) {
1.11 ! www 796: if ($f{$_}=~/^parameter/) {
! 797: $c{$_}=&parmval($f{$_},$symb);
! 798: } else {
! 799: $c{$_}=$returnhash{$f{$_}};
! 800: }
! 801: }
1.6 www 802: }
803: } keys %f;
1.11 ! www 804:
1.6 www 805: &setconstants($safeeval,%c);
806: }
807:
1.10 www 808: # --------------------------------------------------------- Various form fields
809:
810: sub textfield {
811: my ($title,$name,$value)=@_;
812: return "\n<p><b>$title:</b><br>".
813: '<input type=text name="'.$name.'" size=80 value="'.$value.'">';
814: }
815:
816: sub hiddenfield {
817: my ($name,$value)=@_;
818: return "\n".'<input type=hidden name="'.$name.'" value="'.$value.'">';
819: }
820:
821: sub selectbox {
822: my ($title,$name,$value,%options)=@_;
823: my $selout="\n<p><b>$title:</b><br>".'<select name="'.$name.'">';
824: map {
825: $selout.='<option value="'.$_.'"';
826: if ($_ eq $value) { $selout.=' selected'; }
827: $selout.='>'.$options{$_}.'</option>';
828: } sort keys %options;
829: return $selout.'</select>';
830: }
831:
832: # ---------------------------------------------------------------- Main handler
1.3 www 833:
834: sub handler {
1.7 www 835: my $r=shift;
836:
1.11 ! www 837: $uname='';
! 838: $udom='';
! 839: $csec='';
! 840:
1.7 www 841: if ($r->header_only) {
842: $r->content_type('text/html');
843: $r->send_http_header;
844: return OK;
845: }
1.3 www 846:
1.7 www 847: # ----------------------------------------------------- Needs to be in a course
1.3 www 848:
1.7 www 849: if (($ENV{'request.course.fn'}) ||
850: ($ENV{'request.state'} eq 'construct')) {
1.10 www 851:
852: # --------------------------- Get query string for limited number of parameters
853: map {
854: my ($name, $value) = split(/=/,$_);
855: $value =~ tr/+/ /;
856: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.11 ! www 857: if (($name eq 'uname') || ($name eq 'udom') || ($name eq 'usymb')) {
1.10 www 858: unless ($ENV{'form.'.$name}) {
859: $ENV{'form.'.$name}=$value;
860: }
861: }
862: } (split(/&/,$ENV{'QUERY_STRING'}));
863:
864: # ------------------------------------------- Nothing there? Must be login user
865:
866: unless ($ENV{'form.uname'}) {
1.11 ! www 867: $uname=$ENV{'user.name'};
! 868: $udom=$ENV{'user.domain'};
! 869: } else {
! 870: $uname=$ENV{'form.uname'};
! 871: $udom=$ENV{'form.udom'};
1.10 www 872: }
873: # ------------------------------------------------------------------- Open page
874:
1.7 www 875: $r->content_type('text/html');
1.11 ! www 876: $r->header_out('Cache-control','no-cache');
! 877: $r->header_out('Pragma','no-cache');
1.7 www 878: $r->send_http_header;
1.3 www 879:
1.10 www 880: $r->print('<html><head><title>LON-CAPA Spreadsheet</title>');
881: $r->print(<<ENDSCRIPT);
882: <script language="JavaScript">
883:
884: function celledit(cn,cf) {
885: var cnf=prompt(cn,cf);
886: if (cnf!=null) {
887: document.sheet.unewfield.value=cn;
888: document.sheet.unewformula.value=cnf;
889: document.sheet.submit();
890: }
891: }
892:
893: </script>
894: ENDSCRIPT
895: $r->print('</head><body bgcolor="#FFFFFF">'.
896: '<form action="'.$r->uri.'" name=sheet method=post>'.
897: &hiddenfield('uname',$ENV{'form.uname'}).
898: &hiddenfield('udom',$ENV{'form.udom'}).
899: &hiddenfield('usymb',$ENV{'form.usymb'}).
900: &hiddenfield('unewfield','').
901: &hiddenfield('unewformula',''));
1.4 www 902: my $sheetone=initsheet();
1.10 www 903: if ($ENV{'form.unewfield'}) {
904: $r->print('<h2>Modified Workcopy</h2>');
905: $ENV{'form.unewformula'}=~s/\'/\"/g;
906: $r->print('New formula: '.$ENV{'form.unewfield'}.'='.
907: $ENV{'form.unewformula'}.'<br>');
908: &tmpread($sheetone,$r->dir_config('lonDaemons').'/tmp/',
1.11 ! www 909: $ENV{'form.usymb'},
1.10 www 910: $ENV{'form.unewfield'},$ENV{'form.unewformula'});
911: &setfilename($sheetone,$r->filename);
912: $r->filename=~/\.(\w+)/;
913: &settype($sheetone,$1);
1.11 ! www 914: } else {
! 915: &readsheet($sheetone,$r->filename);
! 916: }
! 917: if (tie(%parmhash,'GDBM_File',
! 918: $ENV{'request.course.fn'}.'_parms.db',&GDBM_READER,0640)) {
! 919: $csec=&Apache::lonnet::usection($udom,$uname,$ENV{'request.course.id'});
! 920: if ($csec eq '-1') {
! 921: $r->print('<h3><font color=red>'.
! 922: "User '$uname' at domain '$udom' not a student in this course</font></h3>");
! 923: }
! 924: &updaterows($sheetone);
! 925: untie(%parmhash);
1.10 www 926: } else {
1.11 ! www 927: $r->print('<h3><font color=red>'.
! 928: 'Could not initialize import fields (not in a course)</font></h3>');
! 929: }
! 930: &tmpwrite($sheetone,$r->dir_config('lonDaemons').'/tmp/',
! 931: $ENV{'form.usymb'});
! 932: $r->print("<b>User '$uname' at domain '$udom' for '".
! 933: $ENV{'course.'.$ENV{'request.course.id'}.'.description'}."'");
! 934: if ($csec) {
! 935: $r->print(", group/section '$csec'");
! 936: }
! 937: $r->print("</b>\n");
! 938: if (&gettype($sheetone) eq 'assesscalc') {
! 939: &rowaassess($sheetone,$ENV{'form.usymb'});
1.10 www 940: }
1.6 www 941: &calcsheet($sheetone);
942: $r->print(&outsheet($sheetone));
1.8 www 943:
1.10 www 944: $r->print('</form></body></html>');
1.7 www 945:
946: } else {
947: # ----------------------------- Not in a course, or not allowed to modify parms
948: $ENV{'user.error.msg'}=
949: $r->uri.":opa:0:0:Cannot modify spreadsheet";
950: return HTTP_NOT_ACCEPTABLE;
951: }
1.3 www 952: return OK;
1.1 www 953: }
954:
955: 1;
956: __END__
957:
958:
959:
960:
961:
962:
963:
964:
965:
966:
967:
968:
969:
970:
971:
972:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>