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