--- loncom/interface/spreadsheet/Spreadsheet.pm 2003/05/23 14:52:51 1.4 +++ loncom/interface/spreadsheet/Spreadsheet.pm 2003/09/05 01:57:54 1.23 @@ -1,5 +1,5 @@ # -# $Id: Spreadsheet.pm,v 1.4 2003/05/23 14:52:51 matthew Exp $ +# $Id: Spreadsheet.pm,v 1.23 2003/09/05 01:57:54 matthew Exp $ # # Copyright Michigan State University Board of Trustees # @@ -48,6 +48,8 @@ Spreadsheet package Apache::Spreadsheet; use strict; +use warnings FATAL=>'all'; +no warnings 'uninitialized'; use Apache::Constants qw(:common :http); use Apache::lonnet; use Safe; @@ -90,14 +92,17 @@ sub new { type => $stype, symb => $usymb, errorlog => '', - maxrow => '', + maxrow => 0, cid => $ENV{'request.course.id'}, cnum => $ENV{'course.'.$ENV{'request.course.id'}.'.num'}, cdom => $ENV{'course.'.$ENV{'request.course.id'}.'.domain'}, chome => $ENV{'course.'.$ENV{'request.course.id'}.'.home'}, coursedesc => $ENV{'course.'.$ENV{'request.course.id'}.'.description'}, coursefilename => $ENV{'request.course.fn'}, - temporary => '', + # + # Flags + temporary => 0, # true if this sheet has been modified but not saved + new_rows => 0, # true if this sheet has new rows # # blackout is used to determine if any data needs to be hidden from the # student. @@ -142,24 +147,21 @@ sub filename { if (@_) { my ($newfilename) = @_; if (! defined($newfilename) || $newfilename eq 'Default' || - $newfilename !~ /\w/ || $newfilename =~ /\W/) { - my %tmphash = &Apache::lonnet::get('environment', - ['spreadsheet_default_'. - $self->{'type'}], - $self->{'cdom'}, - $self->{'cnum'}); - my ($tmp) = keys(%tmphash); - if ($tmp !~ /^(con_lost|error|no_such_host)/i) { - $newfilename=$tmphash{'spreadsheet_default_'.$self->{'type'}}; + $newfilename !~ /\w/ || $newfilename eq '') { + my $key = 'course.'.$self->{'cid'}.'.spreadsheet_default_'. + $self->{'type'}; + if (exists($ENV{$key}) && $ENV{$key} ne '') { + $newfilename = $ENV{$key}; + } else { + $newfilename = 'default_'.$self->{'type'}; } } - if (! defined($newfilename) || - $newfilename !~ /\w/ || - $newfilename =~ /^\W*$/) { - $newfilename = 'default.'.$self->{'type'}; - } else { - my $regexp = '_'.$self->{'type'}.'$'; - if ($newfilename !~ /$regexp/) { + if ($newfilename !~ /\w/ || $newfilename =~ /^\W*$/) { + $newfilename = 'default_'.$self->{'type'}; + } + if ($newfilename !~ /^default\.$self->{'type'}$/ ) { + if ($newfilename !~ /_$self->{'type'}$/) { + $newfilename =~ s/[\s_]*$//; $newfilename .= '_'.$self->{'type'}; } } @@ -184,7 +186,7 @@ default spreadsheets.......! sub make_default { my $self = shift(); my $result = &Apache::lonnet::put('environment', - {'spreadsheet_default_'.$self->{'type'} => $self->filename()}, + {'spreadsheet_default_'.$self->{'type'} => $self->filename()}, $self->{'cdom'},$self->{'cnum'}); return $result if ($result ne 'ok'); my $symb = $self->{'symb'}; @@ -217,10 +219,28 @@ sub is_default { if ($tmp !~ /^(con_lost|error|no_such_host)/i) { $default_filename = $tmphash{'spreadsheet_default_'.$self->{'type'}}; } + if ($default_filename =~ /^\s*$/) { + $default_filename = 'default_'.$self->{'type'}; + } return 1 if ($self->filename() eq $default_filename); return 0; } +sub initialize { + # This method is here to remind you that it will be overridden by + # the descendents of the spreadsheet class. +} + +sub clear_package { + # This method is here to remind you that it will be overridden by + # the descendents of the spreadsheet class. +} + +sub cleanup { + my $self = shift(); + $self->clear_package(); +} + sub initialize_spreadsheet_package { &load_spreadsheet_expirationdates(); &clear_spreadsheet_definition_cache(); @@ -240,18 +260,28 @@ sub load_spreadsheet_expirationdates { sub check_expiration_time { my $self = shift; my ($time)=@_; - my ($key1,$key2,$key3,$key4); + return 0 if (! defined($time)); + my ($key1,$key2,$key3,$key4,$key5); + # Description of keys + # + # key1: all sheets of this type have expired + # key2: all sheets of this type for this student + # key3: all sheets of this type in this map for this student + # key4: this assessment sheet for this student + # key5: this assessment sheet for all students $key1 = '::'.$self->{'type'}.':'; $key2 = $self->{'name'}.':'.$self->{'domain'}.':'.$self->{'type'}.':'; $key3 = $key2.$self->{'container'} if (defined($self->{'container'})); - $key4 = $key2.$self->{'usymb'} if (defined($self->{'usymb'})); - foreach my $key ($key1,$key2,$key3,$key4) { + $key4 = $key2.$self->{'symb'} if (defined($self->{'symb'})); + $key5 = $key1.$self->{'symb'} if (defined($self->{'symb'})); + my $returnvalue = 1; # default to okay + foreach my $key ($key1,$key2,$key3,$key4,$key5) { next if (! defined($key)); - if (exists($expiredates{$key}) &&$expiredates{$key} > $time) { - return 0; + if (exists($expiredates{$key}) && $expiredates{$key} > $time) { + $returnvalue = 0; # need to recompute } } - return 1; + return $returnvalue; } ###################################################### @@ -777,8 +807,8 @@ sub expandnamed { my @vars=split(/\W+/,$formula); my %values=(); foreach my $varname ( @vars ) { - if ($varname=~/\D/) { - $formula=~s/$varname/'$c{\''.$varname.'\'}'/ge; + if ($varname=~/^(parameter|stores|timestamp)/) { + $formula=~s/$varname/'$c{\''.$varname.'\'}'/ge; $varname=~s/$var/\([\\w:\\- ]\+\)/g; foreach (keys(%{$self->{'constants'}})) { if ($_=~/$varname/) { @@ -808,30 +838,32 @@ sub expandnamed { # 4. If there is a collision, return 'bad parameter name error' my $returnvalue = ''; my @matches = (); + my @values = (); $#matches = -1; study $expression; - my $parameter; - foreach $parameter (keys(%{$self->{'constants'}})) { - push @matches,$parameter if ($parameter =~ /$expression/); + while (my($parameter,$value) = each(%{$self->{'constants'}})) { + next if ($parameter !~ /$expression/); + push(@matches,$parameter); + push(@values,$value); } if (scalar(@matches) == 0) { - $returnvalue = 'unmatched parameter: '.$parameter; + $returnvalue = '""';#'"unmatched parameter: '.$parameter.'"'; } elsif (scalar(@matches) == 1) { # why do we not do this lookup here, instead of delaying it? - $returnvalue = '$c{\''.$matches[0].'\'}'; + $returnvalue = $values[0]; } elsif (scalar(@matches) > 0) { # more than one match. Look for a concise one $returnvalue = "'non-unique parameter name : $expression'"; - foreach (@matches) { - if (/^$expression$/) { + for (my $i=0; $i<=$#matches;$i++) { + if ($matches[$i] =~ /^$expression$/) { # why do we not do this lookup here? - $returnvalue = '$c{\''.$_.'\'}'; + $returnvalue = $values[$i]; } } } else { # There was a negative number of matches, which indicates # something is wrong with reality. Better warn the user. - $returnvalue = 'bizzare parameter: '.$parameter; + $returnvalue = '"bizzare parameter: '.$expression.'"'; } return $returnvalue; } @@ -1110,18 +1142,39 @@ sub calcsheet { ## Output Helpers ## ########################################################### +sub display { + my $self = shift; + my ($r) = @_; + $self->compute($r); + my $outputmode = 'html'; + if ($ENV{'form.output_format'} =~ /^(html|excel|csv)$/) { + $outputmode = $ENV{'form.output_format'}; + } + if ($outputmode eq 'html') { + $self->outsheet_html($r); + } elsif ($outputmode eq 'excel') { + $self->outsheet_excel($r); + } elsif ($outputmode eq 'csv') { + $self->outsheet_csv($r); + } + $self->cleanup(); + return; +} + ############################################ ## HTML output routines ## ############################################ sub html_export_row { my $self = shift(); + my ($color) = @_; + $color = '#CCCCFF' if (! defined($color)); my $allowed = &Apache::lonnet::allowed('mgr',$ENV{'request.course.id'}); my $row_html; my @rowdata = $self->get_row(0); foreach my $cell (@rowdata) { if ($cell->{'name'} =~ /^[A-Z]/) { - $row_html .= ''. - &html_editable_cell($cell,'#CCCCFF',$allowed).''; + $row_html .= ''. + &html_editable_cell($cell,$color,$allowed).''; } else { $row_html .= ''. &html_editable_cell($cell,'#DDCCFF',$allowed).''; @@ -1133,15 +1186,15 @@ sub html_export_row { sub html_template_row { my $self = shift(); my $allowed = &Apache::lonnet::allowed('mgr',$ENV{'request.course.id'}); - my ($num_uneditable) = @_; + my ($num_uneditable,$importcolor) = @_; my $row_html; my @rowdata = $self->get_template_row(); my $count = 0; for (my $i = 0; $i<=$#rowdata; $i++) { my $cell = $rowdata[$i]; if ($i < $num_uneditable) { - $row_html .= ''. - &html_editable_cell($cell,'#DDCCFF',$allowed).''; + $row_html .= ''. + &html_uneditable_cell($cell,'#FFDDDD',$allowed).''; } else { $row_html .= ''. &html_editable_cell($cell,'#EOFFDD',$allowed).''; @@ -1172,13 +1225,17 @@ sub html_editable_cell { $value = &HTML::Entities::encode($value) if ($value !~/ /); } return $value if (! $allowed); - # Make the formula safe for outputting - $formula =~ s/\'/\"/g; + # # The formula will be parsed by the browser twice before being - # displayed to the user for editing. - $formula = &HTML::Entities::encode(&HTML::Entities::encode($formula)); - # Escape newlines so they make it into the edit window - $formula =~ s/\n/\\n/gs; + # displayed to the user for editing. + # + # The encoding string "^A-blah" is placed in []'s inside a regexp, so + # we specify the characters we want left alone by putting a '^' in front. + $formula = &HTML::Entities::encode($formula,'^A-z0-9 !#$%-;=?~'); + # HTML::Entities::encode does not catch everything - we need '\' encoded + $formula =~ s/\\/&\#092/g; + # Escape it again - this time the only encodable character is '&' + $formula =~ s/\&/\&/g; # Glue everything together $result .= "".$value.""; @@ -1194,14 +1251,19 @@ sub html_uneditable_cell { sub html_row { my $self = shift(); - my ($num_uneditable,$row) = @_; + my ($num_uneditable,$row,$exportcolor,$importcolor) = @_; my $allowed = &Apache::lonnet::allowed('mgr',$ENV{'request.course.id'}); my @rowdata = $self->get_row($row); my $num_cols_output = 0; my $row_html; + my $color = $importcolor; + if ($row == 0) { + $color = $exportcolor; + } + $color = '#FFDDDD' if (! defined($color)); foreach my $cell (@rowdata) { if ($num_cols_output++ < $num_uneditable) { - $row_html .= ''; + $row_html .= ''; $row_html .= &html_uneditable_cell($cell,'#FFDDDD'); } else { $row_html .= ''; @@ -1212,6 +1274,55 @@ sub html_row { return $row_html; } +sub html_header { + my $self = shift; + return '' if (! $ENV{'request.role.adv'}); + return "\n". + ''."\n". + '\n". + "
Output Format
'.&output_selector()."
\n"; +} + +sub output_selector { + my $output_selector = '