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