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