Annotation of loncom/interface/lonspreadsheet.pm, revision 1.12
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
1.12 ! www 570: my $rwhat=$what;
! 571: $what=~s/^parameter\_//;
! 572: $what=~s/\_/\./;
1.11 www 573:
574: my $symbparm=$symb.'.'.$what;
575: my $mapparm=$mapname.'___(all).'.$what;
576:
577: my $seclevel=
578: $ENV{'request.course.id'}.'.['.
579: $csec.'].'.$what;
580: my $seclevelr=
581: $ENV{'request.course.id'}.'.['.
582: $csec.'].'.$symbparm;
583: my $seclevelm=
584: $ENV{'request.course.id'}.'.['.
585: $csec.'].'.$mapparm;
586:
587: my $courselevel=
588: $ENV{'request.course.id'}.'.'.$what;
589: my $courselevelr=
590: $ENV{'request.course.id'}.'.'.$symbparm;
591: my $courselevelm=
592: $ENV{'request.course.id'}.'.'.$mapparm;
1.12 ! www 593:
1.11 www 594: # ---------------------------------------------------------- fourth, check user
595:
596: if ($uname) {
597:
598: if ($useropt{$courselevelr}) { return $useropt{$courselevelr}; }
599:
600: if ($useropt{$courselevelm}) { return $useropt{$courselevelm}; }
601:
602: if ($useropt{$courselevel}) { return $useropt{$courselevel}; }
603:
604: }
605:
606: # --------------------------------------------------------- third, check course
607:
608: if ($csec) {
609:
610: if ($courseopt{$seclevelr}) { return $courseopt{$seclevelr}; }
611:
612: if ($courseopt{$seclevelm}) { return $courseopt{$seclevelm}; }
613:
614: if ($courseopt{$seclevel}) { return $courseopt{$seclevel}; }
615:
616: }
617:
618: if ($courseopt{$courselevelr}) { return $courseopt{$courselevelr}; }
619:
620: if ($courseopt{$courselevelm}) { return $courseopt{$courselevelm}; }
621:
622: if ($courseopt{$courselevel}) { return $courseopt{$courselevel}; }
623:
624: # ----------------------------------------------------- second, check map parms
625:
626: my $thisparm=$parmhash{$symbparm};
627: if ($thisparm) { return $thisparm; }
628:
629: # -------------------------------------------------------- first, check default
630:
1.12 ! www 631: return &Apache::lonnet::metadata($fn,$rwhat.'.default');
1.11 www 632:
633: }
634:
635:
1.5 www 636: # ----------------------------------------------------------------- Update rows
637:
638: sub updaterows {
639: my $safeeval=shift;
640: my %bighash;
641: # -------------------------------------------------------------------- Tie hash
642: if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
643: &GDBM_READER,0640)) {
644: # --------------------------------------------------------- Get all assessments
645:
646: my %allkeys=();
647: my %allassess=();
648:
649: my $stype=&gettype($safeeval);
650:
651: map {
652: if ($_=~/^src\_(\d+)\.(\d+)$/) {
653: my $mapid=$1;
654: my $resid=$2;
655: my $id=$mapid.'.'.$resid;
656: my $srcf=$bighash{$_};
657: if ($srcf=~/\.(problem|exam|quiz|assess|survey|form)$/) {
658: my $symb=
659: &Apache::lonnet::declutter($bighash{'map_id_'.$mapid}).
660: '___'.$resid.'___'.
661: &Apache::lonnet::declutter($srcf);
662: $allassess{$symb}=$bighash{'title_'.$id};
1.8 www 663:
1.6 www 664: if ($stype eq 'assesscalc') {
1.5 www 665: map {
1.11 www 666: if (($_=~/^stores\_(.*)/) || ($_=~/^parameter\_(.*)/)) {
1.5 www 667: my $key=$_;
668: my $display=
669: &Apache::lonnet::metadata($srcf,$key.'.display');
670: unless ($display) {
671: $display=
672: &Apache::lonnet::metadata($srcf,$key.'.name');
673: }
674: $allkeys{$key}=$display;
675: }
676: } split(/\,/,&Apache::lonnet::metadata($srcf,'keys'));
677: }
678: }
679: }
680: } keys %bighash;
681: untie(%bighash);
682:
683: #
1.11 www 684: # %allkeys has a list of storage and parameter displays by unikey
1.5 www 685: # %allassess has a list of all resource displays by symb
686: #
687: # -------------------- Find discrepancies between the course row table and this
688: #
689: my %f=&getformulas($safeeval);
1.6 www 690: my $changed=0;
691:
692: my %current=();
693: if ($stype eq 'assesscalc') {
694: %current=%allkeys;
695: } elsif ($stype eq 'studentcalc') {
696: %current=%allassess;
697: }
698:
699: my $maxrow=0;
700: my %existing=();
701:
702: # ----------------------------------------------------------- Now obsolete rows
1.5 www 703: map {
1.6 www 704: if ($_=~/^A(\d+)/) {
705: $maxrow=($1>$maxrow)?$1:$maxrow;
706: $existing{$f{$_}}=1;
707: unless (defined($current{$f{$_}})) {
708: $f{$_}='!!! Obsolete';
709: $changed=1;
1.5 www 710: }
711: }
712: } keys %f;
1.6 www 713:
714: # -------------------------------------------------------- New and unknown keys
715:
716: map {
717: unless ($existing{$_}) {
718: $changed=1;
719: $maxrow++;
720: $f{'A'.$maxrow}=$_;
721: }
722: } keys %current;
723:
724: if ($changed) { &setformulas($safeeval,%f); }
725:
726: &setmaxrow($safeeval,$maxrow);
727: &setrowlabels($safeeval,%current);
1.5 www 728:
729: } else {
730: return 'Could not access course data';
731: }
732: }
1.3 www 733:
1.6 www 734: # ------------------------------------------------ Load data for one assessment
735:
736: sub rowaassess {
1.11 www 737: my ($safeeval,$symb)=@_;
1.6 www 738: my $uhome=&Apache::lonnet::homeserver($uname,$udom);
739: my $namespace;
740: unless ($namespace=$ENV{'request.course.id'}) { return ''; }
1.11 www 741:
742: # ----------------------------------------------------------- Get stored values
743: my $answer=&Apache::lonnet::reply(
744: "restore:$udom:$uname:$namespace:$symb",$uhome);
1.6 www 745: my %returnhash=();
746: map {
747: my ($name,$value)=split(/\=/,$_);
1.11 www 748: $returnhash{&Apache::lonnet::unescape($name)}=
749: &Apache::lonnet::unescape($value);
1.6 www 750: } split(/\&/,$answer);
751: my $version;
752: for ($version=1;$version<=$returnhash{'version'};$version++) {
753: map {
754: $returnhash{$_}=$returnhash{$version.':'.$_};
755: } split(/\:/,$returnhash{$version.':keys'});
756: }
1.11 www 757: # ----------------------------- returnhash now has all stores for this resource
758:
759: # ---------------------------- initialize coursedata and userdata for this user
760: %courseopt=();
761: %useropt=();
762: my $uhome=&Apache::lonnet::homeserver($uname,$udom);
763: unless ($uhome eq 'no_host') {
764: # -------------------------------------------------------------- Get coursedata
765: unless ($courserdatas{$ENV{'request.course.id'}}) {
766: my $reply=&Apache::lonnet::reply('dump:'.
767: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'}.':'.
768: $ENV{'course.'.$ENV{'request.course.id'}.'.num'}.':resourcedata',
769: $ENV{'course.'.$ENV{'request.course.id'}.'.home'});
770: if ($reply!~/^error\:/) {
771: $courserdatas{$ENV{'request.course.id'}}=$reply;
772: }
773: }
774: map {
775: my ($name,$value)=split(/\=/,$_);
776: $courseopt{&Apache::lonnet::unescape($name)}=
777: &Apache::lonnet::unescape($value);
778: } split(/\&/,$courserdatas{$ENV{'request.course.id'}});
779: # --------------------------------------------------- Get userdata (if present)
780: unless ($userrdatas{$uname.'___'.$udom}) {
781: my $reply=
782: &Apache::lonnet::reply('dump:'.$udom.':'.$uname.':resourcedata',$uhome);
783: if ($reply!~/^error\:/) {
784: $userrdatas{$uname.'___'.$udom}=$reply;
785: }
786: }
787: map {
788: my ($name,$value)=split(/\=/,$_);
789: $useropt{&Apache::lonnet::unescape($name)}=
790: &Apache::lonnet::unescape($value);
791: } split(/\&/,$userrdatas{$uname.'___'.$udom});
792: }
793: # -- now courseopt, useropt initialized for this user and course (used parmval)
1.6 www 794:
795: my %c=();
796: my %f=&getformulas($safeeval);
797: map {
798: if ($_=~/^A/) {
799: unless ($f{$_}=~/^\!/) {
1.11 www 800: if ($f{$_}=~/^parameter/) {
801: $c{$_}=&parmval($f{$_},$symb);
802: } else {
803: $c{$_}=$returnhash{$f{$_}};
804: }
805: }
1.6 www 806: }
807: } keys %f;
1.11 www 808:
1.6 www 809: &setconstants($safeeval,%c);
810: }
811:
1.10 www 812: # --------------------------------------------------------- Various form fields
813:
814: sub textfield {
815: my ($title,$name,$value)=@_;
816: return "\n<p><b>$title:</b><br>".
817: '<input type=text name="'.$name.'" size=80 value="'.$value.'">';
818: }
819:
820: sub hiddenfield {
821: my ($name,$value)=@_;
822: return "\n".'<input type=hidden name="'.$name.'" value="'.$value.'">';
823: }
824:
825: sub selectbox {
826: my ($title,$name,$value,%options)=@_;
827: my $selout="\n<p><b>$title:</b><br>".'<select name="'.$name.'">';
828: map {
829: $selout.='<option value="'.$_.'"';
830: if ($_ eq $value) { $selout.=' selected'; }
831: $selout.='>'.$options{$_}.'</option>';
832: } sort keys %options;
833: return $selout.'</select>';
834: }
835:
1.12 ! www 836: # ==================================== Sub handler to get export of assessments
! 837:
! 838: # ================================================================ Main handler
1.3 www 839:
840: sub handler {
1.7 www 841: my $r=shift;
842:
1.11 www 843: $uname='';
844: $udom='';
845: $csec='';
846:
1.7 www 847: if ($r->header_only) {
848: $r->content_type('text/html');
849: $r->send_http_header;
850: return OK;
851: }
1.3 www 852:
1.7 www 853: # ----------------------------------------------------- Needs to be in a course
1.3 www 854:
1.7 www 855: if (($ENV{'request.course.fn'}) ||
856: ($ENV{'request.state'} eq 'construct')) {
1.10 www 857:
858: # --------------------------- Get query string for limited number of parameters
859: map {
860: my ($name, $value) = split(/=/,$_);
861: $value =~ tr/+/ /;
862: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.11 www 863: if (($name eq 'uname') || ($name eq 'udom') || ($name eq 'usymb')) {
1.10 www 864: unless ($ENV{'form.'.$name}) {
865: $ENV{'form.'.$name}=$value;
866: }
867: }
868: } (split(/&/,$ENV{'QUERY_STRING'}));
869:
870: # ------------------------------------------- Nothing there? Must be login user
871:
872: unless ($ENV{'form.uname'}) {
1.11 www 873: $uname=$ENV{'user.name'};
874: $udom=$ENV{'user.domain'};
875: } else {
876: $uname=$ENV{'form.uname'};
877: $udom=$ENV{'form.udom'};
1.10 www 878: }
879: # ------------------------------------------------------------------- Open page
880:
1.7 www 881: $r->content_type('text/html');
1.11 www 882: $r->header_out('Cache-control','no-cache');
883: $r->header_out('Pragma','no-cache');
1.7 www 884: $r->send_http_header;
1.3 www 885:
1.10 www 886: $r->print('<html><head><title>LON-CAPA Spreadsheet</title>');
887: $r->print(<<ENDSCRIPT);
888: <script language="JavaScript">
889:
890: function celledit(cn,cf) {
891: var cnf=prompt(cn,cf);
892: if (cnf!=null) {
893: document.sheet.unewfield.value=cn;
894: document.sheet.unewformula.value=cnf;
895: document.sheet.submit();
896: }
897: }
898:
899: </script>
900: ENDSCRIPT
901: $r->print('</head><body bgcolor="#FFFFFF">'.
902: '<form action="'.$r->uri.'" name=sheet method=post>'.
903: &hiddenfield('uname',$ENV{'form.uname'}).
904: &hiddenfield('udom',$ENV{'form.udom'}).
905: &hiddenfield('usymb',$ENV{'form.usymb'}).
906: &hiddenfield('unewfield','').
907: &hiddenfield('unewformula',''));
1.4 www 908: my $sheetone=initsheet();
1.10 www 909: if ($ENV{'form.unewfield'}) {
910: $r->print('<h2>Modified Workcopy</h2>');
911: $ENV{'form.unewformula'}=~s/\'/\"/g;
912: $r->print('New formula: '.$ENV{'form.unewfield'}.'='.
913: $ENV{'form.unewformula'}.'<br>');
914: &tmpread($sheetone,$r->dir_config('lonDaemons').'/tmp/',
1.11 www 915: $ENV{'form.usymb'},
1.10 www 916: $ENV{'form.unewfield'},$ENV{'form.unewformula'});
917: &setfilename($sheetone,$r->filename);
918: $r->filename=~/\.(\w+)/;
919: &settype($sheetone,$1);
1.11 www 920: } else {
921: &readsheet($sheetone,$r->filename);
922: }
923: if (tie(%parmhash,'GDBM_File',
924: $ENV{'request.course.fn'}.'_parms.db',&GDBM_READER,0640)) {
925: $csec=&Apache::lonnet::usection($udom,$uname,$ENV{'request.course.id'});
926: if ($csec eq '-1') {
927: $r->print('<h3><font color=red>'.
928: "User '$uname' at domain '$udom' not a student in this course</font></h3>");
929: }
930: &updaterows($sheetone);
931: untie(%parmhash);
1.10 www 932: } else {
1.11 www 933: $r->print('<h3><font color=red>'.
934: 'Could not initialize import fields (not in a course)</font></h3>');
935: }
936: &tmpwrite($sheetone,$r->dir_config('lonDaemons').'/tmp/',
937: $ENV{'form.usymb'});
938: $r->print("<b>User '$uname' at domain '$udom' for '".
939: $ENV{'course.'.$ENV{'request.course.id'}.'.description'}."'");
940: if ($csec) {
941: $r->print(", group/section '$csec'");
942: }
943: $r->print("</b>\n");
944: if (&gettype($sheetone) eq 'assesscalc') {
945: &rowaassess($sheetone,$ENV{'form.usymb'});
1.10 www 946: }
1.6 www 947: &calcsheet($sheetone);
948: $r->print(&outsheet($sheetone));
1.8 www 949:
1.10 www 950: $r->print('</form></body></html>');
1.7 www 951:
952: } else {
953: # ----------------------------- Not in a course, or not allowed to modify parms
954: $ENV{'user.error.msg'}=
955: $r->uri.":opa:0:0:Cannot modify spreadsheet";
956: return HTTP_NOT_ACCEPTABLE;
957: }
1.3 www 958: return OK;
1.1 www 959: }
960:
961: 1;
962: __END__
963:
964:
965:
966:
967:
968:
969:
970:
971:
972:
973:
974:
975:
976:
977:
978:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>