Annotation of loncom/interface/spreadsheet/Spreadsheet.pm, revision 1.22
1.1 matthew 1: #
1.22 ! matthew 2: # $Id: Spreadsheet.pm,v 1.21 2003/08/26 19:14:06 matthew Exp $
1.1 matthew 3: #
4: # Copyright Michigan State University Board of Trustees
5: #
6: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
7: #
8: # LON-CAPA is free software; you can redistribute it and/or modify
9: # it under the terms of the GNU General Public License as published by
10: # the Free Software Foundation; either version 2 of the License, or
11: # (at your option) any later version.
12: #
13: # LON-CAPA is distributed in the hope that it will be useful,
14: # but WITHOUT ANY WARRANTY; without even the implied warranty of
15: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16: # GNU General Public License for more details.
17: #
18: # You should have received a copy of the GNU General Public License
19: # along with LON-CAPA; if not, write to the Free Software
20: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21: #
22: # /home/httpd/html/adm/gpl.txt
23: #
24: # http://www.lon-capa.org/
25: #
26: # The LearningOnline Network with CAPA
27: # Spreadsheet/Grades Display Handler
28: #
29: # POD required stuff:
30:
31: =head1 NAME
32:
33: Spreadsheet
34:
35: =head1 SYNOPSIS
36:
37: =head1 DESCRIPTION
38:
39: =over 4
40:
41: =cut
42:
43: ###################################################
44: ###################################################
45: ### Spreadsheet ###
46: ###################################################
47: ###################################################
48: package Apache::Spreadsheet;
49:
50: use strict;
1.22 ! matthew 51: use warnings FATAL=>'all';
! 52: no warnings 'uninitialized';
1.1 matthew 53: use Apache::Constants qw(:common :http);
54: use Apache::lonnet;
55: use Safe;
56: use Safe::Hole;
57: use Opcode;
58: use HTML::Entities();
59: use HTML::TokeParser;
60: use Spreadsheet::WriteExcel;
61: use Time::HiRes;
62:
63: ##
64: ## Package Variables
65: ##
66: my %expiredates;
67:
68: my @UC_Columns = split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
69: my @LC_Columns = split(//,'abcdefghijklmnopqrstuvwxyz');
70:
71: ######################################################
72:
73: =pod
74:
75: =item &new
76:
77: Returns a new spreadsheet object.
78:
79: =cut
80:
81: ######################################################
82: sub new {
83: my $this = shift;
84: my $class = ref($this) || $this;
85: my ($stype) = ($class =~ /Apache::(.*)$/);
86: #
87: my ($name,$domain,$filename,$usymb)=@_;
88: #
89: my $self = {
90: name => $name,
91: domain => $domain,
92: type => $stype,
93: symb => $usymb,
94: errorlog => '',
1.22 ! matthew 95: maxrow => 0,
1.1 matthew 96: cid => $ENV{'request.course.id'},
97: cnum => $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
98: cdom => $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
99: chome => $ENV{'course.'.$ENV{'request.course.id'}.'.home'},
100: coursedesc => $ENV{'course.'.$ENV{'request.course.id'}.'.description'},
101: coursefilename => $ENV{'request.course.fn'},
1.12 matthew 102: #
103: # Flags
104: temporary => 0, # true if this sheet has been modified but not saved
105: new_rows => 0, # true if this sheet has new rows
1.1 matthew 106: #
1.3 matthew 107: # blackout is used to determine if any data needs to be hidden from the
108: # student.
109: blackout => 0,
110: #
1.1 matthew 111: # Data storage
112: formulas => {},
113: constants => {},
114: rows => [],
115: row_source => {},
116: othersheets => [],
117: };
118: #
119: $self->{'uhome'} = &Apache::lonnet::homeserver($name,$domain);
120: #
121: bless($self,$class);
122: #
123: # Load in the spreadsheet definition
124: $self->filename($filename);
125: if (exists($ENV{'form.workcopy'}) &&
126: $self->{'type'} eq $ENV{'form.workcopy'}) {
127: $self->load_tmp();
128: } else {
129: $self->load();
130: }
131: return $self;
132: }
133:
134: ######################################################
135:
136: =pod
137:
138: =item &filename
139:
140: get or set the filename for a spreadsheet.
141:
142: =cut
143:
144: ######################################################
145: sub filename {
146: my $self = shift();
147: if (@_) {
148: my ($newfilename) = @_;
149: if (! defined($newfilename) || $newfilename eq 'Default' ||
1.13 matthew 150: $newfilename !~ /\w/ || $newfilename eq '') {
151: my $key = 'course.'.$self->{'cid'}.'.spreadsheet_default_'.
152: $self->{'type'};
153: if (exists($ENV{$key}) && $ENV{$key} ne '') {
154: $newfilename = $ENV{$key};
155: } else {
156: $newfilename = 'default_'.$self->{'type'};
1.1 matthew 157: }
1.13 matthew 158: }
159: if ($newfilename !~ /\w/ || $newfilename =~ /^\W*$/) {
160: $newfilename = 'default_'.$self->{'type'};
161: }
162: if ($newfilename !~ /^default\.$self->{'type'}$/ ) {
163: if ($newfilename !~ /_$self->{'type'}$/) {
164: $newfilename =~ s/[\s_]*$//;
1.1 matthew 165: $newfilename .= '_'.$self->{'type'};
166: }
167: }
168: $self->{'filename'} = $newfilename;
169: return;
170: }
171: return $self->{'filename'};
172: }
173:
174: ######################################################
175:
176: =pod
177:
178: =item &make_default()
179:
180: Make the current spreadsheet file the default for the course. Expires all the
181: default spreadsheets.......!
182:
183: =cut
184:
185: ######################################################
186: sub make_default {
187: my $self = shift();
188: my $result = &Apache::lonnet::put('environment',
1.13 matthew 189: {'spreadsheet_default_'.$self->{'type'} => $self->filename()},
1.1 matthew 190: $self->{'cdom'},$self->{'cnum'});
191: return $result if ($result ne 'ok');
192: my $symb = $self->{'symb'};
193: $symb = '' if (! defined($symb));
194: &Apache::lonnet::expirespread('','',$self->{'type'},$symb);
195: }
196:
197: ######################################################
198:
199: =pod
200:
201: =item &is_default()
202:
203: Returns 1 if the current spreadsheet is the default as specified in the
204: course environment. Returns 0 otherwise.
205:
206: =cut
207:
208: ######################################################
209: sub is_default {
210: my $self = shift;
211: # Check to find out if we are the default spreadsheet (filenames match)
212: my $default_filename = '';
213: my %tmphash = &Apache::lonnet::get('environment',
214: ['spreadsheet_default_'.
215: $self->{'type'}],
216: $self->{'cdom'},
217: $self->{'cnum'});
218: my ($tmp) = keys(%tmphash);
219: if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
220: $default_filename = $tmphash{'spreadsheet_default_'.$self->{'type'}};
221: }
1.13 matthew 222: if ($default_filename =~ /^\s*$/) {
223: $default_filename = 'default_'.$self->{'type'};
224: }
1.1 matthew 225: return 1 if ($self->filename() eq $default_filename);
226: return 0;
1.11 matthew 227: }
228:
229: sub initialize {
230: # This method is here to remind you that it will be overridden by
231: # the descendents of the spreadsheet class.
1.1 matthew 232: }
233:
234: sub initialize_spreadsheet_package {
235: &load_spreadsheet_expirationdates();
236: &clear_spreadsheet_definition_cache();
237: }
238:
239: sub load_spreadsheet_expirationdates {
240: undef %expiredates;
241: my $cid=$ENV{'request.course.id'};
242: my @tmp = &Apache::lonnet::dump('nohist_expirationdates',
243: $ENV{'course.'.$cid.'.domain'},
244: $ENV{'course.'.$cid.'.num'});
245: if (lc($tmp[0]) !~ /^error/){
246: %expiredates = @tmp;
247: }
248: }
249:
250: sub check_expiration_time {
251: my $self = shift;
252: my ($time)=@_;
1.22 ! matthew 253: return 0 if (! defined($time));
1.19 matthew 254: my ($key1,$key2,$key3,$key4,$key5);
255: # Description of keys
256: #
257: # key1: all sheets of this type have expired
258: # key2: all sheets of this type for this student
259: # key3: all sheets of this type in this map for this student
260: # key4: this assessment sheet for this student
261: # key5: this assessment sheet for all students
1.1 matthew 262: $key1 = '::'.$self->{'type'}.':';
263: $key2 = $self->{'name'}.':'.$self->{'domain'}.':'.$self->{'type'}.':';
264: $key3 = $key2.$self->{'container'} if (defined($self->{'container'}));
1.19 matthew 265: $key4 = $key2.$self->{'symb'} if (defined($self->{'symb'}));
266: $key5 = $key1.$self->{'symb'} if (defined($self->{'symb'}));
267: my $returnvalue = 1; # default to okay
268: foreach my $key ($key1,$key2,$key3,$key4,$key5) {
1.1 matthew 269: next if (! defined($key));
1.19 matthew 270: if (exists($expiredates{$key}) && $expiredates{$key} > $time) {
271: $returnvalue = 0; # need to recompute
1.1 matthew 272: }
273: }
1.19 matthew 274: return $returnvalue;
1.1 matthew 275: }
276:
277: ######################################################
278:
279: =pod
280:
281: =item &initialize_safe_space
282:
283: Returns the safe space required by a Spreadsheet object.
284:
285: =head 2 Safe Space Functions
286:
287: =over 4
288:
289: =cut
290:
291: ######################################################
292: sub initialize_safe_space {
293: my $self = shift;
294: my $safeeval = new Safe(shift);
295: my $safehole = new Safe::Hole;
296: $safeeval->permit("entereval");
297: $safeeval->permit(":base_math");
298: $safeeval->permit("sort");
299: $safeeval->deny(":base_io");
300: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
301: $safehole->wrap(\&mask,$safeeval,'&mask');
302: $safeeval->share('$@');
303: my $code=<<'ENDDEFS';
304: # ---------------------------------------------------- Inside of the safe space
305: #
306: # f: formulas
307: # t: intermediate format (variable references expanded)
308: # v: output values
309: # c: preloaded constants (A-column)
310: # rl: row label
311: # os: other spreadsheets (for student spreadsheet only)
312: undef %sheet_values; # Holds the (computed, final) values for the sheet
313: # This is only written to by &calc, the spreadsheet computation routine.
314: # It is read by many functions
315: undef %t; # Holds the values of the spreadsheet temporarily. Set in &sett,
316: # which does the translation of strings like C5 into the value in C5.
317: # Used in &calc - %t holds the values that are actually eval'd.
318: undef %f; # Holds the formulas for each cell. This is the users
319: # (spreadsheet authors) data for each cell.
320: undef %c; # Holds the constants for a sheet. In the assessment
321: # sheets, this is the A column. Used in &MINPARM, &MAXPARM, &expandnamed,
322: # &sett, and &constants. There is no &getconstants.
323: # &constants is called by &loadstudent, &loadcourse, &load assessment,
324: undef @os; # Holds the names of other spreadsheets - this is used to specify
325: # the spreadsheets that are available for the assessment sheet.
326: # Set by &setothersheets. &setothersheets is called by &handler. A
327: # related subroutine is &othersheets.
328: $errorlog = '';
329: #
330: $maxrow = 0;
331: $type = '';
332: #
333: # filename/reference of the sheet
334: $filename = '';
335: #
336: # user data
337: $name = '';
338: $uhome = '';
339: $domain = '';
340: #
341: # course data
342: $csec = '';
343: $chome= '';
344: $cnum = '';
345: $cdom = '';
346: $cid = '';
347: $coursefilename = '';
348: #
349: # symb
350: $usymb = '';
351: #
352: # error messages
353: $errormsg = '';
354: #
355: #-------------------------------------------------------
356:
357: =pod
358:
359: =item NUM(range)
360:
361: returns the number of items in the range.
362:
363: =cut
364:
365: #-------------------------------------------------------
366: sub NUM {
367: my $mask=&mask(@_);
368: my $num= $#{@{grep(/$mask/,keys(%sheet_values))}}+1;
369: return $num;
370: }
371:
372: #-------------------------------------------------------
373:
374: =pod
375:
376: =item BIN(low,high,lower,upper)
377:
378: =cut
379:
380: #-------------------------------------------------------
381: sub BIN {
382: my ($low,$high,$lower,$upper)=@_;
383: my $mask=&mask($lower,$upper);
384: my $num=0;
385: foreach (grep /$mask/,keys(%sheet_values)) {
386: if (($sheet_values{$_}>=$low) && ($sheet_values{$_}<=$high)) {
387: $num++;
388: }
389: }
390: return $num;
391: }
392:
393: #-------------------------------------------------------
394:
395: =pod
396:
397: =item SUM(range)
398:
399: returns the sum of items in the range.
400:
401: =cut
402:
403: #-------------------------------------------------------
404: sub SUM {
405: my $mask=&mask(@_);
406: my $sum=0;
407: foreach (grep /$mask/,keys(%sheet_values)) {
408: $sum+=$sheet_values{$_};
409: }
410: return $sum;
411: }
412:
413: #-------------------------------------------------------
414:
415: =pod
416:
417: =item MEAN(range)
418:
419: compute the average of the items in the range.
420:
421: =cut
422:
423: #-------------------------------------------------------
424: sub MEAN {
425: my $mask=&mask(@_);
426: my $sum=0;
427: my $num=0;
428: foreach (grep /$mask/,keys(%sheet_values)) {
429: $sum+=$sheet_values{$_};
430: $num++;
431: }
432: if ($num) {
433: return $sum/$num;
434: } else {
435: return undef;
436: }
437: }
438:
439: #-------------------------------------------------------
440:
441: =pod
442:
443: =item STDDEV(range)
444:
445: compute the standard deviation of the items in the range.
446:
447: =cut
448:
449: #-------------------------------------------------------
450: sub STDDEV {
451: my $mask=&mask(@_);
452: my $sum=0; my $num=0;
453: foreach (grep /$mask/,keys(%sheet_values)) {
454: $sum+=$sheet_values{$_};
455: $num++;
456: }
457: unless ($num>1) { return undef; }
458: my $mean=$sum/$num;
459: $sum=0;
460: foreach (grep /$mask/,keys(%sheet_values)) {
461: $sum+=($sheet_values{$_}-$mean)**2;
462: }
463: return sqrt($sum/($num-1));
464: }
465:
466: #-------------------------------------------------------
467:
468: =pod
469:
470: =item PROD(range)
471:
472: compute the product of the items in the range.
473:
474: =cut
475:
476: #-------------------------------------------------------
477: sub PROD {
478: my $mask=&mask(@_);
479: my $prod=1;
480: foreach (grep /$mask/,keys(%sheet_values)) {
481: $prod*=$sheet_values{$_};
482: }
483: return $prod;
484: }
485:
486: #-------------------------------------------------------
487:
488: =pod
489:
490: =item MAX(range)
491:
492: compute the maximum of the items in the range.
493:
494: =cut
495:
496: #-------------------------------------------------------
497: sub MAX {
498: my $mask=&mask(@_);
499: my $max='-';
500: foreach (grep /$mask/,keys(%sheet_values)) {
501: unless ($max) { $max=$sheet_values{$_}; }
502: if (($sheet_values{$_}>$max) || ($max eq '-')) {
503: $max=$sheet_values{$_};
504: }
505: }
506: return $max;
507: }
508:
509: #-------------------------------------------------------
510:
511: =pod
512:
513: =item MIN(range)
514:
515: compute the minimum of the items in the range.
516:
517: =cut
518:
519: #-------------------------------------------------------
520: sub MIN {
521: my $mask=&mask(@_);
522: my $min='-';
523: foreach (grep /$mask/,keys(%sheet_values)) {
524: unless ($max) { $max=$sheet_values{$_}; }
525: if (($sheet_values{$_}<$min) || ($min eq '-')) {
526: $min=$sheet_values{$_};
527: }
528: }
529: return $min;
530: }
531:
532: #-------------------------------------------------------
533:
534: =pod
535:
536: =item SUMMAX(num,lower,upper)
537:
538: compute the sum of the largest 'num' items in the range from
539: 'lower' to 'upper'
540:
541: =cut
542:
543: #-------------------------------------------------------
544: sub SUMMAX {
545: my ($num,$lower,$upper)=@_;
546: my $mask=&mask($lower,$upper);
547: my @inside=();
548: foreach (grep /$mask/,keys(%sheet_values)) {
549: push (@inside,$sheet_values{$_});
550: }
551: @inside=sort(@inside);
552: my $sum=0; my $i;
553: for ($i=$#inside;(($i>$#inside-$num) && ($i>=0));$i--) {
554: $sum+=$inside[$i];
555: }
556: return $sum;
557: }
558:
559: #-------------------------------------------------------
560:
561: =pod
562:
563: =item SUMMIN(num,lower,upper)
564:
565: compute the sum of the smallest 'num' items in the range from
566: 'lower' to 'upper'
567:
568: =cut
569:
570: #-------------------------------------------------------
571: sub SUMMIN {
572: my ($num,$lower,$upper)=@_;
573: my $mask=&mask($lower,$upper);
574: my @inside=();
575: foreach (grep /$mask/,keys(%sheet_values)) {
576: $inside[$#inside+1]=$sheet_values{$_};
577: }
578: @inside=sort(@inside);
579: my $sum=0; my $i;
580: for ($i=0;(($i<$num) && ($i<=$#inside));$i++) {
581: $sum+=$inside[$i];
582: }
583: return $sum;
584: }
585:
586: #-------------------------------------------------------
587:
588: =pod
589:
590: =item MINPARM(parametername)
591:
592: Returns the minimum value of the parameters matching the parametername.
593: parametername should be a string such as 'duedate'.
594:
595: =cut
596:
597: #-------------------------------------------------------
598: sub MINPARM {
599: my ($expression) = @_;
600: my $min = undef;
601: study($expression);
602: foreach $parameter (keys(%c)) {
603: next if ($parameter !~ /$expression/);
604: if ((! defined($min)) || ($min > $c{$parameter})) {
605: $min = $c{$parameter}
606: }
607: }
608: return $min;
609: }
610:
611: #-------------------------------------------------------
612:
613: =pod
614:
615: =item MAXPARM(parametername)
616:
617: Returns the maximum value of the parameters matching the input parameter name.
618: parametername should be a string such as 'duedate'.
619:
620: =cut
621:
622: #-------------------------------------------------------
623: sub MAXPARM {
624: my ($expression) = @_;
625: my $max = undef;
626: study($expression);
627: foreach $parameter (keys(%c)) {
628: next if ($parameter !~ /$expression/);
629: if ((! defined($min)) || ($max < $c{$parameter})) {
630: $max = $c{$parameter}
631: }
632: }
633: return $max;
634: }
635:
636:
637: sub calc {
638: %sheet_values = %t;
639: my $notfinished = 1;
640: my $lastcalc = '';
641: my $depth = 0;
642: while ($notfinished) {
643: $notfinished=0;
644: while (my ($cell,$value) = each(%t)) {
645: my $old=$sheet_values{$cell};
646: $sheet_values{$cell}=eval $value;
647: # $errorlog .= $cell.' = '.$old.'->'.$sheet_values{$cell}."\n";
648: if ($@) {
649: undef %sheet_values;
650: return $cell.': '.$@;
651: }
652: if ($sheet_values{$cell} ne $old) {
653: $notfinished=1;
654: $lastcalc=$cell;
655: }
656: }
657: # $errorlog.="------------------------------------------------";
658:
659: $depth++;
660: if ($depth>100) {
661: undef %sheet_values;
662: return $lastcalc.': Maximum calculation depth exceeded';
663: }
664: }
665: return '';
666: }
667:
668: # ------------------------------------------- End of "Inside of the safe space"
669: ENDDEFS
670: $safeeval->reval($code);
671: $self->{'safe'} = $safeeval;
672: $self->{'root'} = $self->{'safe'}->root();
673: #
674: # Place some of the %$self items into the safe space except the safe space
675: # itself
676: my $initstring = '';
677: foreach (qw/name domain type usymb cid csec coursefilename
678: cnum cdom chome uhome/) {
679: $initstring.= qq{\$$_="$self->{$_}";};
680: }
681: $self->{'safe'}->reval($initstring);
682: return $self;
683: }
684: ######################################################
685:
686: =pod
687:
688: =back
689:
690: =cut
691:
692: ######################################################
693:
694:
695: ######################################################
696:
697:
698: ######################################################
699: {
700:
701: my %memoizer;
702:
703: sub mask {
704: my ($lower,$upper)=@_;
705: my $key = $lower.'_'.$upper;
706: if (exists($memoizer{$key})) {
707: return $memoizer{$key};
708: }
709: $upper = $lower if (! defined($upper));
710: #
711: my ($la,$ld) = ($lower=~/([A-Za-z]|\*)(\d+|\*)/);
712: my ($ua,$ud) = ($upper=~/([A-Za-z]|\*)(\d+|\*)/);
713: #
714: my $alpha='';
715: my $num='';
716: #
717: if (($la eq '*') || ($ua eq '*')) {
718: $alpha='[A-Za-z]';
719: } else {
720: if (($la=~/[A-Z]/) && ($ua=~/[A-Z]/) ||
721: ($la=~/[a-z]/) && ($ua=~/[a-z]/)) {
722: $alpha='['.$la.'-'.$ua.']';
723: } else {
724: $alpha='['.$la.'-Za-'.$ua.']';
725: }
726: }
727: if (($ld eq '*') || ($ud eq '*')) {
728: $num='\d+';
729: } else {
730: if (length($ld)!=length($ud)) {
731: $num.='(';
732: foreach ($ld=~m/\d/g) {
733: $num.='['.$_.'-9]';
734: }
735: if (length($ud)-length($ld)>1) {
736: $num.='|\d{'.(length($ld)+1).','.(length($ud)-1).'}';
737: }
738: $num.='|';
739: foreach ($ud=~m/\d/g) {
740: $num.='[0-'.$_.']';
741: }
742: $num.=')';
743: } else {
744: my @lda=($ld=~m/\d/g);
745: my @uda=($ud=~m/\d/g);
746: my $i;
747: my $j=0;
748: my $notdone=1;
749: for ($i=0;($i<=$#lda)&&($notdone);$i++) {
750: if ($lda[$i]==$uda[$i]) {
751: $num.=$lda[$i];
752: $j=$i;
753: } else {
754: $notdone=0;
755: }
756: }
757: if ($j<$#lda-1) {
758: $num.='('.$lda[$j+1];
759: for ($i=$j+2;$i<=$#lda;$i++) {
760: $num.='['.$lda[$i].'-9]';
761: }
762: if ($uda[$j+1]-$lda[$j+1]>1) {
763: $num.='|['.($lda[$j+1]+1).'-'.($uda[$j+1]-1).']\d{'.
764: ($#lda-$j-1).'}';
765: }
766: $num.='|'.$uda[$j+1];
767: for ($i=$j+2;$i<=$#uda;$i++) {
768: $num.='[0-'.$uda[$i].']';
769: }
770: $num.=')';
771: } else {
772: if ($lda[-1]!=$uda[-1]) {
773: $num.='['.$lda[-1].'-'.$uda[-1].']';
774: }
775: }
776: }
777: }
778: my $expression ='^'.$alpha.$num."\$";
779: $memoizer{$key} = $expression;
780: return $expression;
781: }
782:
783: }
784:
785: ##
786: ## sub add_hash_to_safe {} # spreadsheet, would like to destroy
787: ##
788:
789: #
790: # expandnamed used to reside in the safe space
791: #
792: sub expandnamed {
793: my $self = shift;
794: my $expression=shift;
795: if ($expression=~/^\&/) {
796: my ($func,$var,$formula)=($expression=~/^\&(\w+)\(([^\;]+)\;(.*)\)/);
797: my @vars=split(/\W+/,$formula);
798: my %values=();
799: foreach my $varname ( @vars ) {
1.20 matthew 800: if ($varname=~/^(parameter|stores|timestamp)/) {
801: $formula=~s/$varname/'$c{\''.$varname.'\'}'/ge;
1.1 matthew 802: $varname=~s/$var/\([\\w:\\- ]\+\)/g;
803: foreach (keys(%{$self->{'constants'}})) {
804: if ($_=~/$varname/) {
805: $values{$1}=1;
806: }
807: }
808: }
809: }
810: if ($func eq 'EXPANDSUM') {
811: my $result='';
812: foreach (keys(%values)) {
813: my $thissum=$formula;
814: $thissum=~s/$var/$_/g;
815: $result.=$thissum.'+';
816: }
817: $result=~s/\+$//;
818: return $result;
819: } else {
820: return 0;
821: }
822: } else {
823: # it is not a function, so it is a parameter name
824: # We should do the following:
825: # 1. Take the list of parameter names
826: # 2. look through the list for ones that match the parameter we want
827: # 3. If there are no collisions, return the one that matches
828: # 4. If there is a collision, return 'bad parameter name error'
829: my $returnvalue = '';
830: my @matches = ();
1.14 matthew 831: my @values = ();
1.1 matthew 832: $#matches = -1;
833: study $expression;
1.14 matthew 834: while (my($parameter,$value) = each(%{$self->{'constants'}})) {
835: next if ($parameter !~ /$expression/);
836: push(@matches,$parameter);
837: push(@values,$value);
1.1 matthew 838: }
839: if (scalar(@matches) == 0) {
1.10 matthew 840: $returnvalue = '""';#'"unmatched parameter: '.$parameter.'"';
1.1 matthew 841: } elsif (scalar(@matches) == 1) {
842: # why do we not do this lookup here, instead of delaying it?
1.14 matthew 843: $returnvalue = $values[0];
1.1 matthew 844: } elsif (scalar(@matches) > 0) {
845: # more than one match. Look for a concise one
846: $returnvalue = "'non-unique parameter name : $expression'";
1.14 matthew 847: for (my $i=0; $i<=$#matches;$i++) {
848: if ($matches[$i] =~ /^$expression$/) {
1.1 matthew 849: # why do we not do this lookup here?
1.14 matthew 850: $returnvalue = $values[$i];
1.1 matthew 851: }
852: }
853: } else {
854: # There was a negative number of matches, which indicates
855: # something is wrong with reality. Better warn the user.
1.14 matthew 856: $returnvalue = '"bizzare parameter: '.$expression.'"';
1.1 matthew 857: }
858: return $returnvalue;
859: }
860: }
861:
862: sub sett {
863: my $self = shift;
864: my %t=();
865: #
866: # Deal with the template row
867: foreach my $col ($self->template_cells()) {
868: next if ($col=~/^[A-Z]/);
869: foreach my $row ($self->rows()) {
870: # Get the name of this cell
871: my $cell=$col.$row;
872: # Grab the template declaration
873: $t{$cell}=$self->formula('template_'.$col);
874: # Replace '#' with the row number
875: $t{$cell}=~s/\#/$row/g;
876: # Replace '....' with ','
877: $t{$cell}=~s/\.\.+/\,/g;
878: # Replace 'A0' with the value from 'A0'
879: $t{$cell}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
880: # Replace parameters
881: $t{$cell}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.$self->expandnamed($2)/ge;
882: }
883: }
884: #
885: # Deal with the normal cells
886: while (my($cell,$formula) = each(%{$self->{'formulas'}})) {
887: next if ($_=~/^template\_/);
888: my ($col,$row) = ($cell =~ /^([A-z])(\d+)$/);
889: if ($row eq '0') {
890: $t{$cell}=$formula;
891: $t{$cell}=~s/\.\.+/\,/g;
892: $t{$cell}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
893: $t{$cell}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.$self->expandnamed($2)/ge;
894: } elsif ( $col =~ /^[A-Z]$/ ) {
895: if ($formula !~ /^\!/ && exists($self->{'constants'}->{$cell})) {
896: my $data = $self->{'constants'}->{$cell};
897: $t{$cell} = $data;
898: }
899: } else { # $row > 1 and $col =~ /[a-z]
900: $t{$cell}=$formula;
901: $t{$cell}=~s/\.\.+/\,/g;
902: $t{$cell}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
903: $t{$cell}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.$self->expandnamed($2)/ge;
904: }
905: }
906: %{$self->{'safe'}->varglob('t')}=%t;
907: }
908:
909: ##
910: ## sync_safe_space: Called by calcsheet to make sure all the data we
911: # need to calculate is placed into the safe space
912: ##
913: sub sync_safe_space {
914: my $self = shift;
915: # Inside the safe space 'formulas' has a diabolical alter-ego named 'f'.
916: %{$self->{'safe'}->varglob('f')}=%{$self->{'formulas'}};
917: # 'constants' leads a peaceful hidden life of 'c'.
918: %{$self->{'safe'}->varglob('c')}=%{$self->{'constants'}};
919: # 'othersheets' hides as 'os', a disguise few can penetrate.
920: @{$self->{'safe'}->varglob('os')}=@{$self->{'othersheets'}};
921: }
922:
923: ##
924: ## Retrieve the error log from the safe space (used for debugging)
925: ##
926: sub get_errorlog {
927: my $self = shift;
928: $self->{'errorlog'} = $ { $self->{'safe'}->varglob('errorlog') };
929: return $self->{'errorlog'};
930: }
931:
932: ##
933: ## Clear the error log inside the safe space
934: ##
935: sub clear_errorlog {
936: my $self = shift;
937: $ {$self->{'safe'}->varglob('errorlog')} = '';
938: $self->{'errorlog'} = '';
939: }
940:
941: ##
942: ## constants: either set or get the constants
943: ##
944: sub constants {
945: my $self=shift;
946: my ($constants) = @_;
947: if (defined($constants)) {
948: if (! ref($constants)) {
949: my %tmp = @_;
950: $constants = \%tmp;
951: }
952: $self->{'constants'} = $constants;
953: return;
954: } else {
955: return %{$self->{'constants'}};
956: }
957: }
958:
959: ##
960: ## formulas: either set or get the formulas
961: ##
962: sub formulas {
963: my $self=shift;
964: my ($formulas) = @_;
965: if (defined($formulas)) {
966: if (! ref($formulas)) {
967: my %tmp = @_;
968: $formulas = \%tmp;
969: }
970: $self->{'formulas'} = $formulas;
971: $self->{'rows'} = [];
972: $self->{'template_cells'} = [];
973: return;
974: } else {
975: return %{$self->{'formulas'}};
976: }
977: }
978:
979: sub set_formula {
980: my $self = shift;
981: my ($cell,$formula) = @_;
982: $self->{'formulas'}->{$cell}=$formula;
983: return;
984: }
985:
986: ##
987: ## formulas_keys: Return the keys to the formulas hash.
988: ##
989: sub formulas_keys {
990: my $self = shift;
991: my @keys = keys(%{$self->{'formulas'}});
992: return keys(%{$self->{'formulas'}});
993: }
994:
995: ##
996: ## formula: Return the formula for a given cell in the spreadsheet
997: ## returns '' if the cell does not have a formula or does not exist
998: ##
999: sub formula {
1000: my $self = shift;
1001: my $cell = shift;
1002: if (defined($cell) && exists($self->{'formulas'}->{$cell})) {
1003: return $self->{'formulas'}->{$cell};
1004: }
1005: return '';
1006: }
1007:
1008: ##
1009: ## logthis: write the input to lonnet.log
1010: ##
1011: sub logthis {
1012: my $self = shift;
1013: my $message = shift;
1014: &Apache::lonnet::logthis($self->{'type'}.':'.
1015: $self->{'name'}.':'.$self->{'domain'}.':'.
1016: $message);
1017: return;
1018: }
1019:
1020: ##
1021: ## dump_formulas_to_log: makes lonnet.log huge...
1022: ##
1023: sub dump_formulas_to_log {
1024: my $self =shift;
1025: $self->logthis("Spreadsheet formulas");
1026: $self->logthis("--------------------------------------------------------");
1027: while (my ($cell, $formula) = each(%{$self->{'formulas'}})) {
1028: $self->logthis(' '.$cell.' = '.$formula);
1029: }
1030: $self->logthis("--------------------------------------------------------");}
1031:
1032: ##
1033: ## value: returns the computed value of a particular cell
1034: ##
1035: sub value {
1036: my $self = shift;
1037: my $cell = shift;
1038: if (defined($cell) && exists($self->{'values'}->{$cell})) {
1039: return $self->{'values'}->{$cell};
1040: }
1041: return '';
1042: }
1043:
1044: ##
1045: ## dump_values_to_log: makes lonnet.log huge...
1046: ##
1047: sub dump_values_to_log {
1048: my $self =shift;
1049: $self->logthis("Spreadsheet Values");
1050: $self->logthis("------------------------------------------------------");
1051: while (my ($cell, $value) = each(%{$self->{'values'}})) {
1052: $self->logthis(' '.$cell.' = '.$value);
1053: }
1054: $self->logthis("------------------------------------------------------");
1055: }
1056:
1057: ##
1058: ## Yet another debugging function
1059: ##
1060: sub dump_hash_to_log {
1061: my $self= shift();
1062: my %tmp = @_;
1063: if (@_<2) {
1064: %tmp = %{$_[0]};
1065: }
1066: $self->logthis('---------------------------- (begin hash dump)');
1067: while (my ($key,$val) = each (%tmp)) {
1068: $self->logthis(' '.$key.' = '.$val.':');
1069: }
1070: $self->logthis('---------------------------- (finished hash dump)');
1071: }
1072:
1073: ##
1074: ## rebuild_stats: rebuilds the rows and template_cells arrays
1075: ##
1076: sub rebuild_stats {
1077: my $self = shift;
1078: $self->{'rows'}=[];
1079: $self->{'template_cells'}=[];
1080: while (my ($cell,$formula) = each(%{$self->{'formulas'}})) {
1081: push(@{$self->{'rows'}},$1) if ($cell =~ /^A(\d+)/ && $1 != 0);
1082: push(@{$self->{'template_cells'}},$1) if ($cell =~ /^template_(\w+)/);
1083: }
1084: return;
1085: }
1086:
1087: ##
1088: ## template_cells returns a list of the cells defined in the template row
1089: ##
1090: sub template_cells {
1091: my $self = shift;
1092: $self->rebuild_stats() if (! defined($self->{'template_cells'}) ||
1093: ! @{$self->{'template_cells'}});
1094: return @{$self->{'template_cells'}};
1095: }
1096:
1097: ##
1098: ## Sigh....
1099: ##
1100: sub setothersheets {
1101: my $self = shift;
1102: my @othersheets = @_;
1103: $self->{'othersheets'} = \@othersheets;
1104: }
1105:
1106: ##
1107: ## rows returns a list of the names of cells defined in the A column
1108: ##
1109: sub rows {
1110: my $self = shift;
1111: $self->rebuild_stats() if (!@{$self->{'rows'}});
1112: return @{$self->{'rows'}};
1113: }
1114:
1115: #
1116: # calcsheet: makes all the calls to compute the spreadsheet.
1117: #
1118: sub calcsheet {
1119: my $self = shift;
1120: $self->sync_safe_space();
1121: $self->clear_errorlog();
1122: $self->sett();
1123: my $result = $self->{'safe'}->reval('&calc();');
1124: # $self->logthis($self->get_errorlog());
1125: %{$self->{'values'}} = %{$self->{'safe'}->varglob('sheet_values')};
1126: # $self->logthis($self->get_errorlog());
1127: return $result;
1128: }
1129:
1130: ###########################################################
1131: ##
1132: ## Output Helpers
1133: ##
1134: ###########################################################
1.5 matthew 1135: sub display {
1136: my $self = shift;
1137: my ($r) = @_;
1138: $self->compute($r);
1139: my $outputmode = 'html';
1140: if ($ENV{'form.output_format'} =~ /^(html|excel|csv)$/) {
1141: $outputmode = $ENV{'form.output_format'};
1142: }
1143: if ($outputmode eq 'html') {
1144: $self->outsheet_html($r);
1145: } elsif ($outputmode eq 'excel') {
1146: $self->outsheet_excel($r);
1147: } elsif ($outputmode eq 'csv') {
1148: $self->outsheet_csv($r);
1149: }
1.22 ! matthew 1150: $self->cleanup();
1.5 matthew 1151: return;
1152: }
1153:
1.1 matthew 1154: ############################################
1155: ## HTML output routines ##
1156: ############################################
1157: sub html_export_row {
1158: my $self = shift();
1.17 matthew 1159: my ($color) = @_;
1160: $color = '#CCCCFF' if (! defined($color));
1.1 matthew 1161: my $allowed = &Apache::lonnet::allowed('mgr',$ENV{'request.course.id'});
1162: my $row_html;
1163: my @rowdata = $self->get_row(0);
1164: foreach my $cell (@rowdata) {
1165: if ($cell->{'name'} =~ /^[A-Z]/) {
1.17 matthew 1166: $row_html .= '<td bgcolor="'.$color.'">'.
1167: &html_editable_cell($cell,$color,$allowed).'</td>';
1.1 matthew 1168: } else {
1169: $row_html .= '<td bgcolor="#DDCCFF">'.
1170: &html_editable_cell($cell,'#DDCCFF',$allowed).'</td>';
1171: }
1172: }
1173: return $row_html;
1174: }
1175:
1176: sub html_template_row {
1177: my $self = shift();
1178: my $allowed = &Apache::lonnet::allowed('mgr',$ENV{'request.course.id'});
1.17 matthew 1179: my ($num_uneditable,$importcolor) = @_;
1.1 matthew 1180: my $row_html;
1181: my @rowdata = $self->get_template_row();
1182: my $count = 0;
1183: for (my $i = 0; $i<=$#rowdata; $i++) {
1184: my $cell = $rowdata[$i];
1185: if ($i < $num_uneditable) {
1.17 matthew 1186: $row_html .= '<td bgcolor="'.$importcolor.'">'.
1.7 matthew 1187: &html_uneditable_cell($cell,'#FFDDDD',$allowed).'</td>';
1.1 matthew 1188: } else {
1189: $row_html .= '<td bgcolor="#EOFFDD">'.
1190: &html_editable_cell($cell,'#EOFFDD',$allowed).'</td>';
1191: }
1192: }
1193: return $row_html;
1194: }
1195:
1196: sub html_editable_cell {
1197: my ($cell,$bgcolor,$allowed) = @_;
1198: my $result;
1199: my ($name,$formula,$value);
1200: if (defined($cell)) {
1201: $name = $cell->{'name'};
1202: $formula = $cell->{'formula'};
1203: $value = $cell->{'value'};
1204: }
1205: $name = '' if (! defined($name));
1206: $formula = '' if (! defined($formula));
1207: if (! defined($value)) {
1208: $value = '<font color="'.$bgcolor.'">#</font>';
1209: if ($formula ne '') {
1210: $value = '<i>undefined value</i>';
1211: }
1212: } elsif ($value =~ /^\s*$/ ) {
1213: $value = '<font color="'.$bgcolor.'">#</font>';
1214: } else {
1215: $value = &HTML::Entities::encode($value) if ($value !~/ /);
1216: }
1217: return $value if (! $allowed);
1.18 matthew 1218: #
1.1 matthew 1219: # The formula will be parsed by the browser twice before being
1.18 matthew 1220: # displayed to the user for editing.
1221: #
1222: # The encoding string "^A-blah" is placed in []'s inside a regexp, so
1223: # we specify the characters we want left alone by putting a '^' in front.
1.21 matthew 1224: $formula = &HTML::Entities::encode($formula,'^A-z0-9 !#$%-;=?~');
1225: # HTML::Entities::encode does not catch everything - we need '\' encoded
1226: $formula =~ s/\\/&\#092/g;
1.18 matthew 1227: # Escape it again - this time the only encodable character is '&'
1228: $formula =~ s/\&/\&/g;
1.1 matthew 1229: # Glue everything together
1230: $result .= "<a href=\"javascript:celledit(\'".
1231: $name."','".$formula."');\">".$value."</a>";
1232: return $result;
1233: }
1234:
1235: sub html_uneditable_cell {
1236: my ($cell,$bgcolor) = @_;
1237: my $value = (defined($cell) ? $cell->{'value'} : '');
1238: $value = &HTML::Entities::encode($value) if ($value !~/ /);
1239: return ' '.$value.' ';
1240: }
1241:
1242: sub html_row {
1243: my $self = shift();
1.17 matthew 1244: my ($num_uneditable,$row,$exportcolor,$importcolor) = @_;
1.1 matthew 1245: my $allowed = &Apache::lonnet::allowed('mgr',$ENV{'request.course.id'});
1246: my @rowdata = $self->get_row($row);
1247: my $num_cols_output = 0;
1248: my $row_html;
1.17 matthew 1249: my $color = $importcolor;
1250: if ($row == 0) {
1251: $color = $exportcolor;
1252: }
1253: $color = '#FFDDDD' if (! defined($color));
1.1 matthew 1254: foreach my $cell (@rowdata) {
1255: if ($num_cols_output++ < $num_uneditable) {
1.17 matthew 1256: $row_html .= '<td bgcolor="'.$color.'">';
1.1 matthew 1257: $row_html .= &html_uneditable_cell($cell,'#FFDDDD');
1258: } else {
1259: $row_html .= '<td bgcolor="#EOFFDD">';
1260: $row_html .= &html_editable_cell($cell,'#E0FFDD',$allowed);
1261: }
1262: $row_html .= '</td>';
1263: }
1264: return $row_html;
1265: }
1266:
1.5 matthew 1267: sub html_header {
1268: my $self = shift;
1269: return '' if (! $ENV{'request.role.adv'});
1270: return "<table>\n".
1271: '<tr><th align="center">Output Format</th><tr>'."\n".
1272: '<tr><td>'.&output_selector()."</td></tr>\n".
1273: "</table>\n";
1274: }
1275:
1276: sub output_selector {
1277: my $output_selector = '<select name="output_format" size="3">'."\n";
1278: my $default = 'html';
1279: if (exists($ENV{'form.output_format'})) {
1280: $default = $ENV{'form.output_format'}
1281: } else {
1282: $ENV{'form.output_format'} = $default;
1283: }
1284: foreach (['html','HTML'],
1285: ['excel','Excel'],
1286: ['csv','Comma Seperated Values']) {
1287: my ($name,$description) = @{$_};
1288: $output_selector.=qq{<option value="$name"};
1289: if ($name eq $default) {
1290: $output_selector .= ' selected';
1291: }
1292: $output_selector .= ">$description</option>\n";
1293: }
1294: $output_selector .= "</select>\n";
1295: return $output_selector;
1296: }
1297:
1298: ################################################
1299: ## Excel output routines ##
1300: ################################################
1301: sub excel_output_row {
1302: my $self = shift;
1303: my ($worksheet,$rownum,$rows_output,@prepend) = @_;
1304: my $cols_output = 0;
1305: #
1306: my @rowdata = $self->get_row($rownum);
1307: foreach my $cell (@prepend,@rowdata) {
1308: my $value = $cell;
1309: $value = $cell->{'value'} if (ref($value));
1310: $value =~ s/\ / /gi;
1311: $worksheet->write($rows_output,$cols_output++,$value);
1312: }
1313: return;
1314: }
1315:
1.1 matthew 1316: sub create_excel_spreadsheet {
1317: my $self = shift;
1318: my ($r) = @_;
1319: my $filename = '/prtspool/'.
1320: $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
1321: time.'_'.rand(1000000000).'.xls';
1322: my $workbook = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
1323: if (! defined($workbook)) {
1324: $r->log_error("Error creating excel spreadsheet $filename: $!");
1325: $r->print("Problems creating new Excel file. ".
1326: "This error has been logged. ".
1327: "Please alert your LON-CAPA administrator");
1328: return undef;
1329: }
1330: #
1331: # The excel spreadsheet stores temporary data in files, then put them
1332: # together. If needed we should be able to disable this (memory only).
1333: # The temporary directory must be specified before calling 'addworksheet'.
1334: # File::Temp is used to determine the temporary directory.
1335: $workbook->set_tempdir('/home/httpd/perl/tmp');
1336: #
1337: # Determine the name to give the worksheet
1338: return ($workbook,$filename);
1.5 matthew 1339: }
1340:
1341: sub outsheet_excel {
1342: my $self = shift;
1343: my ($r) = @_;
1344: $r->print("<h2>Preparing Excel Spreadsheet</h2>");
1345: #
1346: # Create excel worksheet
1347: my ($workbook,$filename) = $self->create_excel_spreadsheet($r);
1348: return if (! defined($workbook));
1349: #
1350: # Create main worksheet
1351: my $worksheet = $workbook->addworksheet('main');
1352: my $rows_output = 0;
1353: my $cols_output = 0;
1354: #
1355: # Write excel header
1356: foreach my $value ($self->get_title()) {
1357: $cols_output = 0;
1358: $worksheet->write($rows_output++,$cols_output,$value);
1359: }
1360: $rows_output++; # skip a line
1361: #
1362: # Write summary/export row
1363: $cols_output = 0;
1364: $self->excel_output_row($worksheet,0,$rows_output++,'Summary');
1365: $rows_output++; # skip a line
1366: #
1367: $self->excel_rows($worksheet,$cols_output,$rows_output);
1368: #
1369: #
1370: # Close the excel file
1371: $workbook->close();
1372: #
1373: # Write a link to allow them to download it
1374: $r->print('<br />'.
1375: '<a href="'.$filename.'">Your Excel spreadsheet.</a>'."\n");
1.6 matthew 1376: return;
1377: }
1378:
1379: #################################
1380: ## CSV output routines ##
1381: #################################
1382: sub outsheet_csv {
1383: my $self = shift;
1384: my ($r) = @_;
1385: my $csvdata = '';
1386: my @Values;
1387: #
1388: # Open the csv file
1389: my $filename = '/prtspool/'.
1390: $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
1391: time.'_'.rand(1000000000).'.csv';
1392: my $file;
1393: unless ($file = Apache::File->new('>'.'/home/httpd'.$filename)) {
1394: $r->log_error("Couldn't open $filename for output $!");
1395: $r->print("Problems occured in writing the csv file. ".
1396: "This error has been logged. ".
1397: "Please alert your LON-CAPA administrator.");
1398: $r->print("<pre>\n".$csvdata."</pre>\n");
1399: return 0;
1400: }
1401: #
1402: # Output the title information
1403: foreach my $value ($self->get_title()) {
1404: print $file "'".&Apache::loncommon::csv_translate($value)."'\n";
1405: }
1406: #
1407: # Output the body of the spreadsheet
1408: $self->csv_rows($file);
1409: #
1410: # Close the csv file
1411: close($file);
1412: $r->print('<br /><br />'.
1413: '<a href="'.$filename.'">Your CSV spreadsheet.</a>'."\n");
1414: #
1415: return 1;
1416: }
1417:
1418: sub csv_output_row {
1419: my $self = shift;
1420: my ($filehandle,$rownum,@prepend) = @_;
1421: #
1422: my @rowdata = ();
1423: if (defined($rownum)) {
1424: @rowdata = $self->get_row($rownum);
1425: }
1426: my @output = ();
1427: foreach my $cell (@prepend,@rowdata) {
1428: my $value = $cell;
1429: $value = $cell->{'value'} if (ref($value));
1430: $value =~ s/\ / /gi;
1431: $value = "'".$value."'";
1432: push (@output,$value);
1433: }
1434: print $filehandle join(',',@output )."\n";
1.5 matthew 1435: return;
1.1 matthew 1436: }
1437:
1438: ############################################
1439: ## XML output routines ##
1440: ############################################
1441: sub outsheet_xml {
1442: my $self = shift;
1443: my ($r) = @_;
1444: ## Someday XML
1445: ## Will be rendered for the user
1446: ## But not on this day
1447: my $Str = '<spreadsheet type="'.$self->{'type'}.'">'."\n";
1448: while (my ($cell,$formula) = each(%{$self->{'formulas'}})) {
1449: if ($cell =~ /^template_(\d+)/) {
1450: my $col = $1;
1451: $Str .= '<template col="'.$col.'">'.$formula.'</template>'."\n";
1452: } else {
1453: my ($row,$col) = ($cell =~ /^([A-z])(\d+)/);
1454: next if (! defined($row) || ! defined($col));
1455: $Str .= '<field row="'.$row.'" col="'.$col.'" >'.$formula.'</cell>'
1456: ."\n";
1457: }
1458: }
1459: $Str.="</spreadsheet>";
1460: return $Str;
1461: }
1462:
1463: ############################################
1464: ### Filesystem routines ###
1465: ############################################
1466: sub parse_sheet {
1467: # $sheetxml is a scalar reference or a scalar
1468: my ($sheetxml) = @_;
1469: if (! ref($sheetxml)) {
1470: my $tmp = $sheetxml;
1471: $sheetxml = \$tmp;
1472: }
1473: my %formulas;
1474: my %sources;
1475: my $parser=HTML::TokeParser->new($sheetxml);
1476: my $token;
1477: while ($token=$parser->get_token) {
1478: if ($token->[0] eq 'S') {
1479: if ($token->[1] eq 'field') {
1480: my $cell = $token->[2]->{'col'}.$token->[2]->{'row'};
1481: my $source = $token->[2]->{'source'};
1482: my $formula = $parser->get_text('/field');
1483: $formulas{$cell} = $formula;
1484: $sources{$cell} = $source if (defined($source));
1485: $parser->get_text('/field');
1486: }
1487: if ($token->[1] eq 'template') {
1488: $formulas{'template_'.$token->[2]->{'col'}}=
1489: $parser->get_text('/template');
1490: }
1491: }
1492: }
1493: return (\%formulas,\%sources);
1494: }
1495:
1496: {
1497:
1498: my %spreadsheets;
1499:
1500: sub clear_spreadsheet_definition_cache {
1501: undef(%spreadsheets);
1502: }
1503:
1.13 matthew 1504: sub load_system_default_sheet {
1505: my $self = shift;
1506: my $includedir = $Apache::lonnet::perlvar{'lonIncludes'};
1507: # load in the default defined spreadsheet
1508: my $sheetxml='';
1509: my $fh;
1510: if ($fh=Apache::File->new($includedir.'/default_'.$self->{'type'})) {
1511: $sheetxml=join('',<$fh>);
1512: $fh->close();
1513: } else {
1514: # $sheetxml='<field row="0" col="A">"Error"</field>';
1515: $sheetxml='<field row="0" col="A"></field>';
1516: }
1517: $self->filename('default_');
1518: my ($formulas,undef) = &parse_sheet(\$sheetxml);
1519: return $formulas;
1520: }
1521:
1.1 matthew 1522: sub load {
1523: my $self = shift;
1524: #
1525: my $stype = $self->{'type'};
1526: my $cnum = $self->{'cnum'};
1527: my $cdom = $self->{'cdom'};
1528: my $chome = $self->{'chome'};
1529: #
1.13 matthew 1530: my $filename = $self->filename();
1.1 matthew 1531: my $cachekey = join('_',($cnum,$cdom,$stype,$filename));
1532: #
1533: # see if sheet is cached
1534: my ($formulas);
1535: if (exists($spreadsheets{$cachekey})) {
1536: $formulas = $spreadsheets{$cachekey}->{'formulas'};
1537: } else {
1538: # Not cached, need to read
1.13 matthew 1539: if (! defined($filename)) {
1540: $formulas = $self->load_system_default_sheet();
1.8 matthew 1541: } elsif($self->filename() =~ /^\/res\/.*\.spreadsheet$/) {
1.1 matthew 1542: # Load a spreadsheet definition file
1543: my $sheetxml=&Apache::lonnet::getfile
1544: (&Apache::lonnet::filelocation('',$filename));
1545: if ($sheetxml == -1) {
1546: $sheetxml='<field row="0" col="A">"Error loading spreadsheet '
1547: .$self->filename().'"</field>';
1548: }
1549: ($formulas,undef) = &parse_sheet(\$sheetxml);
1.13 matthew 1550: # Get just the filename and set the sheets filename
1551: my ($newfilename) = ($filename =~ /\/([^\/]*)\.spreadsheet$/);
1552: if ($self->is_default()) {
1553: $self->filename($newfilename);
1554: $self->make_default();
1555: } else {
1556: $self->filename($newfilename);
1557: }
1.1 matthew 1558: } else {
1559: # Load the spreadsheet definition file from the save file
1.13 matthew 1560: my %tmphash = &Apache::lonnet::dump($filename,$cdom,$cnum);
1.1 matthew 1561: my ($tmp) = keys(%tmphash);
1562: if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1563: while (my ($cell,$formula) = each(%tmphash)) {
1564: $formulas->{$cell}=$formula;
1565: }
1566: } else {
1.13 matthew 1567: $formulas = $self->load_system_default_sheet();
1.1 matthew 1568: }
1569: }
1.13 matthew 1570: $filename=$self->filename(); # filename may have changed
1.1 matthew 1571: $cachekey = join('_',($cnum,$cdom,$stype,$filename));
1572: %{$spreadsheets{$cachekey}->{'formulas'}} = %{$formulas};
1573: }
1574: $self->formulas($formulas);
1575: $self->set_row_sources();
1576: $self->set_row_numbers();
1577: }
1578:
1579: sub set_row_sources {
1580: my $self = shift;
1581: while (my ($cell,$value) = each(%{$self->{'formulas'}})) {
1.22 ! matthew 1582: next if ($cell !~ /^A(\d+)/ || $1 < 1);
1.1 matthew 1583: my $row = $1;
1584: $self->{'row_source'}->{$row} = $value;
1585: }
1586: return;
1587: }
1588:
1.12 matthew 1589: sub set_row_numbers {
1590: my $self = shift;
1591: while (my ($cell,$value) = each(%{$self->{'formulas'}})) {
1592: next if ($cell !~ /^A(\d+)$/);
1593: next if (! defined($value));
1594: $self->{'row_numbers'}->{$value} = $1;
1595: $self->{'maxrow'} = $1 if ($1 > $self->{'maxrow'});
1596: }
1597: }
1598:
1.1 matthew 1599: ##
1600: ## exportrow is *not* used to get the export row from a computed sub-sheet.
1601: ##
1602: sub exportrow {
1603: my $self = shift;
1604: my @exportarray;
1605: foreach my $column (@UC_Columns) {
1606: push(@exportarray,$self->value($column.'0'));
1607: }
1608: return @exportarray;
1609: }
1610:
1611: sub save {
1612: my $self = shift;
1613: my ($makedef)=@_;
1614: my $cid=$self->{'cid'};
1.4 matthew 1615: # If we are saving it, it must not be temporary
1616: $self->temporary(0);
1.1 matthew 1617: if (&Apache::lonnet::allowed('opa',$cid)) {
1618: my %f=$self->formulas();
1619: my $stype = $self->{'type'};
1620: my $cnum = $self->{'cnum'};
1621: my $cdom = $self->{'cdom'};
1622: my $chome = $self->{'chome'};
1.12 matthew 1623: my $filename = $self->{'filename'};
1624: my $cachekey = join('_',($cnum,$cdom,$stype,$filename));
1.1 matthew 1625: # Cache new sheet
1.13 matthew 1626: %{$spreadsheets{$cachekey}->{'formulas'}}=%f;
1.1 matthew 1627: # Write sheet
1628: foreach (keys(%f)) {
1629: delete($f{$_}) if ($f{$_} eq 'import');
1630: }
1.12 matthew 1631: my $reply = &Apache::lonnet::put($filename,\%f,$cdom,$cnum);
1.1 matthew 1632: return $reply if ($reply ne 'ok');
1633: $reply = &Apache::lonnet::put($stype.'_spreadsheets',
1.12 matthew 1634: {$filename => $ENV{'user.name'}.'@'.$ENV{'user.domain'}},
1.1 matthew 1635: $cdom,$cnum);
1636: return $reply if ($reply ne 'ok');
1637: if ($makedef) {
1638: $reply = &Apache::lonnet::put('environment',
1.12 matthew 1639: {'spreadsheet_default_'.$stype => $filename },
1.1 matthew 1640: $cdom,$cnum);
1641: return $reply if ($reply ne 'ok');
1642: }
1643: if ($self->is_default()) {
1.22 ! matthew 1644: if ($self->{'type'} eq 'studentcalc') {
! 1645: &Apache::lonnet::expirespread('','','studentcalc','');
! 1646: } elsif ($self->{'type'} eq 'assesscalc') {
! 1647: &Apache::lonnet::expirespread('','','assesscalc','');
1.16 matthew 1648: &Apache::lonnet::expirespread('','','studentcalc','');
1649: }
1.1 matthew 1650: }
1651: return $reply;
1652: }
1653: return 'unauthorized';
1654: }
1655:
1656: } # end of scope for %spreadsheets
1657:
1658: sub save_tmp {
1659: my $self = shift;
1.9 matthew 1660: my $filename=$ENV{'user.name'}.'_'.
1.19 matthew 1661: $ENV{'user.domain'}.'_spreadsheet_'.$self->{'symb'}.'_'.
1.1 matthew 1662: $self->{'filename'};
1.9 matthew 1663: $filename=~s/\W/\_/g;
1664: $filename=$Apache::lonnet::tmpdir.$filename.'.tmp';
1.4 matthew 1665: $self->temporary(1);
1.1 matthew 1666: my $fh;
1.9 matthew 1667: if ($fh=Apache::File->new('>'.$filename)) {
1.1 matthew 1668: my %f = $self->formulas();
1669: while( my ($cell,$formula) = each(%f)) {
1670: next if ($formula eq 'import');
1671: print $fh &Apache::lonnet::escape($cell)."=".
1672: &Apache::lonnet::escape($formula)."\n";
1673: }
1674: $fh->close();
1675: }
1676: }
1677:
1678: sub load_tmp {
1679: my $self = shift;
1680: my $filename=$ENV{'user.name'}.'_'.
1.19 matthew 1681: $ENV{'user.domain'}.'_spreadsheet_'.$self->{'symb'}.'_'.
1.1 matthew 1682: $self->{'filename'};
1683: $filename=~s/\W/\_/g;
1684: $filename=$Apache::lonnet::tmpdir.$filename.'.tmp';
1685: my %formulas = ();
1686: if (my $spreadsheet_file = Apache::File->new($filename)) {
1687: while (<$spreadsheet_file>) {
1688: chomp;
1689: my ($cell,$formula) = split(/=/);
1690: $cell = &Apache::lonnet::unescape($cell);
1691: $formula = &Apache::lonnet::unescape($formula);
1692: $formulas{$cell} = $formula;
1693: }
1694: $spreadsheet_file->close();
1695: }
1.4 matthew 1696: # flag the sheet as temporary
1697: $self->temporary(1);
1.1 matthew 1698: $self->formulas(\%formulas);
1699: $self->set_row_sources();
1700: $self->set_row_numbers();
1701: return;
1.4 matthew 1702: }
1703:
1704: sub temporary {
1705: my $self=shift;
1706: if (@_) {
1707: ($self->{'temporary'})= @_;
1708: }
1709: return $self->{'temporary'};
1.1 matthew 1710: }
1711:
1712: sub modify_cell {
1713: # studentcalc overrides this
1714: my $self = shift;
1715: my ($cell,$formula) = @_;
1716: if ($cell =~ /([A-z])\-/) {
1717: $cell = 'template_'.$1;
1718: } elsif ($cell !~ /^([A-z](\d+)|template_[A-z])$/) {
1719: return;
1720: }
1721: $self->set_formula($cell,$formula);
1722: $self->rebuild_stats();
1723: return;
1724: }
1725:
1726: ###########################################
1727: # othersheets: Returns the list of other spreadsheets available
1728: ###########################################
1729: sub othersheets {
1730: my $self = shift();
1731: my ($stype) = @_;
1732: $stype = $self->{'type'} if (! defined($stype) || $stype !~ /calc$/);
1733: #
1734: my @alternatives=();
1735: my %results=&Apache::lonnet::dump($stype.'_spreadsheets',
1736: $self->{'cdom'}, $self->{'cnum'});
1737: my ($tmp) = keys(%results);
1.2 matthew 1738: if ($tmp =~ /^(con_lost|error|no_such_host)/i ) {
1739: @alternatives = ('Default');
1740: } else {
1.13 matthew 1741: @alternatives = ('Default', sort (keys(%results)));
1.1 matthew 1742: }
1743: return @alternatives;
1.3 matthew 1744: }
1745:
1746: sub blackout {
1747: my $self = shift;
1748: $self->{'blackout'} = $_[0] if (@_);
1749: return $self->{'blackout'};
1.1 matthew 1750: }
1751:
1752: sub get_row {
1753: my $self = shift;
1754: my ($n)=@_;
1755: my @cols=();
1756: foreach my $col (@UC_Columns,@LC_Columns) {
1757: my $cell = $col.$n;
1758: push(@cols,{ name => $cell,
1759: formula => $self->formula($cell),
1760: value => $self->value($cell)});
1761: }
1762: return @cols;
1763: }
1764:
1765: sub get_template_row {
1766: my $self = shift;
1767: my @cols=();
1768: foreach my $col (@UC_Columns,@LC_Columns) {
1769: my $cell = 'template_'.$col;
1770: push(@cols,{ name => $cell,
1771: formula => $self->formula($cell),
1772: value => $self->formula($cell) });
1773: }
1774: return @cols;
1775: }
1776:
1.12 matthew 1777: sub need_to_save {
1.1 matthew 1778: my $self = shift;
1.12 matthew 1779: if ($self->{'new_rows'} && ! $self->temporary()) {
1780: return 1;
1.1 matthew 1781: }
1.12 matthew 1782: return 0;
1.1 matthew 1783: }
1784:
1785: sub get_row_number_from_key {
1786: my $self = shift;
1787: my ($key) = @_;
1788: if (! exists($self->{'row_numbers'}->{$key}) ||
1789: ! defined($self->{'row_numbers'}->{$key})) {
1790: # I used to set $f here to the new value, but the key passed for lookup
1791: # may not be the key we need to save
1792: $self->{'maxrow'}++;
1793: $self->{'row_numbers'}->{$key} = $self->{'maxrow'};
1.13 matthew 1794: # $self->logthis('added row '.$self->{'row_numbers'}->{$key}.
1795: # ' for '.$key);
1.12 matthew 1796: $self->{'new_rows'} = 1;
1.1 matthew 1797: }
1798: return $self->{'row_numbers'}->{$key};
1799: }
1800:
1801: 1;
1802:
1803: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>