--- loncom/interface/spreadsheet/Spreadsheet.pm 2003/05/16 20:55:11 1.1 +++ loncom/interface/spreadsheet/Spreadsheet.pm 2003/06/23 19:58:18 1.17 @@ -1,5 +1,5 @@ # -# $Id: Spreadsheet.pm,v 1.1 2003/05/16 20:55:11 matthew Exp $ +# $Id: Spreadsheet.pm,v 1.17 2003/06/23 19:58:18 matthew Exp $ # # Copyright Michigan State University Board of Trustees # @@ -98,6 +98,14 @@ sub new { coursedesc => $ENV{'course.'.$ENV{'request.course.id'}.'.description'}, coursefilename => $ENV{'request.course.fn'}, # + # 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. + blackout => 0, + # # Data storage formulas => {}, constants => {}, @@ -137,24 +145,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'}; } } @@ -179,7 +184,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'}; @@ -212,10 +217,18 @@ 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 initialize_spreadsheet_package { &load_spreadsheet_expirationdates(); &clear_spreadsheet_definition_cache(); @@ -803,30 +816,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; } @@ -1105,18 +1120,38 @@ 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); + } + 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).''; @@ -1128,15 +1163,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).''; @@ -1189,14 +1224,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 .= ''; @@ -1207,6 +1247,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 = '