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