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