--- loncom/interface/spreadsheet/Spreadsheet.pm 2005/05/13 21:14:46 1.46 +++ loncom/interface/spreadsheet/Spreadsheet.pm 2007/01/23 18:22:47 1.77 @@ -1,5 +1,5 @@ # -# $Id: Spreadsheet.pm,v 1.46 2005/05/13 21:14:46 albertel Exp $ +# $Id: Spreadsheet.pm,v 1.77 2007/01/23 18:22:47 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -60,6 +60,9 @@ use HTML::TokeParser; use Spreadsheet::WriteExcel; use Time::HiRes; use Apache::lonlocal; +use lib '/home/httpd/lib/perl/'; +use LONCAPA; + ## ## Package Variables @@ -85,7 +88,7 @@ sub new { my $class = ref($this) || $this; my ($stype) = ($class =~ /Apache::(.*)$/); # - my ($name,$domain,$filename,$usymb)=@_; + my ($name,$domain,$filename,$usymb,$section,$groups)=@_; if (defined($usymb) && ref($usymb)) { $usymb = $usymb->symb; } @@ -95,10 +98,22 @@ sub new { if (! defined($domain) || $domain eq '') { $domain = $env{'user.domain'}; } + if (! defined($section) || $section eq '') { + $section = &Apache::lonnet::getsection($domain,$name, + $env{'request.course.id'}); + } + if (! defined($groups)) { + + my @usersgroups = &Apache::lonnet::get_users_groups($domain,$name, + $env{'request.course.id'}); + $groups = \@usersgroups; + } # my $self = { name => $name, domain => $domain, + section => $section, + groups => $groups, type => $stype, symb => $usymb, errorlog => '', @@ -106,13 +121,13 @@ sub new { 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'}, # # Flags temporary => 0, # true if this sheet has been modified but not saved - new_rows => 0, # true if this sheet has new rows + new_rows => 0, # true if this sheet has new rows + loaded => 0, # true if the formulas have been loaded # # blackout is used to determine if any data needs to be hidden from the # student. @@ -126,18 +141,9 @@ sub new { othersheets => [], }; # - $self->{'uhome'} = &Apache::lonnet::homeserver($name,$domain); - # bless($self,$class); - # - # Load in the spreadsheet definition $self->filename($filename); - if (exists($env{'form.workcopy'}) && - $self->{'type'} eq $env{'form.workcopy'}) { - $self->load_tmp(); - } else { - $self->load(); - } + # return $self; } @@ -166,16 +172,20 @@ sub filename { $newfilename = 'default_'.$self->{'type'}; } } - if ($newfilename !~ /\w/ || $newfilename =~ /^\W*$/) { - $newfilename = 'default_'.$self->{'type'}; - } - if ($newfilename !~ /^default\.$self->{'type'}$/ && - $newfilename !~ /^\/res\/(.*)spreadsheet$/) { - if ($newfilename !~ /_$self->{'type'}$/) { - $newfilename =~ s/[\s_]*$//; - $newfilename .= '_'.$self->{'type'}; - } - } + if ($newfilename eq &mt('LON-CAPA Standard')) { + undef($newfilename); + } else { + if ($newfilename !~ /\w/ || $newfilename =~ /^\W*$/) { + $newfilename = 'default_'.$self->{'type'}; + } + if ($newfilename !~ /^default\.$self->{'type'}$/ && + $newfilename !~ /^\/res\/(.*)spreadsheet$/) { + if ($newfilename !~ /_$self->{'type'}$/) { + $newfilename =~ s/[\s_]*$//; + $newfilename .= '_'.$self->{'type'}; + } + } + } $self->{'filename'} = $newfilename; return; } @@ -200,6 +210,8 @@ sub make_default { {'spreadsheet_default_'.$self->{'type'} => $self->filename()}, $self->{'cdom'},$self->{'cnum'}); return $result if ($result ne 'ok'); + &Apache::lonnet::appenv('course.'.$self->{'cid'}.'.spreadsheet_default_'. + $self->{'type'} => $self->filename()); my $symb = $self->{'symb'}; $symb = '' if (! defined($symb)); &Apache::lonnet::expirespread('','',$self->{'type'},$symb); @@ -220,16 +232,8 @@ course environment. Returns 0 otherwise sub is_default { my $self = shift; # Check to find out if we are the default spreadsheet (filenames match) - my $default_filename = ''; - 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) { - $default_filename = $tmphash{'spreadsheet_default_'.$self->{'type'}}; - } + my $default_filename = $env{'course.'.$self->{'cid'}. + '.spreadsheet_default_'.$self->{'type'}}; if ($default_filename =~ /^\s*$/) { $default_filename = 'default_'.$self->{'type'}; } @@ -366,12 +370,10 @@ $filename = ''; # # user data $name = ''; -$uhome = ''; $domain = ''; # # course data $csec = ''; -$chome= ''; $cnum = ''; $cdom = ''; $cid = ''; @@ -395,10 +397,12 @@ Calls the system EXT function to determi #------------------------------------------------------- sub EXT { - my ($parameter) = @_; + my ($parameter,$specific_symb) = @_; return '' if (! defined($parameter) || $parameter eq ''); $parameter =~ s/^parameter\./resource\./; - my $value = &Apache::lonnet::EXT($parameter,$symb,$domain,$name,$usection); + if ($specific_symb eq '') { $specific_symb = $symb; } + my $value = &Apache::lonnet::EXT($parameter,$specific_symb,$domain,$name, + $usection); return $value; } @@ -671,6 +675,21 @@ sub MAXPARM { return $max; } + +=pod + +=item PARM(parametername) + +Returns the value of the parameter matching the input parameter name. +parametername should be a string such as 'parameter_1_opendate'. + +=cut + +#------------------------------------------------------- +sub PARM { + return $c{$_[0]}; +} + #------------------------------------------------------- =pod @@ -714,7 +733,7 @@ sub get_values { } return \@values; } else { - $num = '(\d+)'; + $num = '([1-9]\d*)'; } if (($la eq '*') || ($ua eq '*')) { $alpha='[A-z]'; @@ -770,7 +789,7 @@ ENDDEFS # itself my $initstring = ''; foreach (qw/name domain type symb cid csec coursefilename - cnum cdom chome uhome/) { + cnum cdom/) { $initstring.= qq{\$$_="$self->{$_}";}; } $initstring.=qq{\$usection="$usection";}; @@ -801,18 +820,20 @@ sub expandnamed { my $self = shift; my $expression=shift; if ($expression=~/^\&/) { - my ($func,$var,$formula)=($expression=~/^\&(\w+)\(([^\;]+)\;(.*)\)/); + my ($func,$var,$formula)=($expression=~/^\&(\w+)\(([^\;]+)\;(.*)\)/s); my @vars=split(/\W+/,$formula); + # make the list uniq + @vars = keys(%{{ map { $_ => 1 } @vars }}); my %values=(); foreach my $varname ( @vars ) { if ($varname=~/^(parameter|stores|timestamp)/) { $formula=~s/$varname/'$c{\''.$varname.'\'}'/ge; - $varname=~s/$var/\([\\w:\\- ]\+\)/g; - foreach (keys(%{$self->{'constants'}})) { - if ($_=~/$varname/) { - $values{$1}=1; - } - } + $varname=~s/$var/\([\\w:\\- ]\+\)/g; + foreach (keys(%{$self->{'constants'}})) { + if ($_=~/$varname/) { + $values{$1}=1; + } + } } } if ($func eq 'EXPANDSUM') { @@ -823,7 +844,7 @@ sub expandnamed { $result.=$thissum.'+'; } $result=~s/\+$//; - return $result; + return '('.$result.')'; } else { return 0; } @@ -886,7 +907,7 @@ sub sett { # Replace 'A0' with the value from 'A0' $t{$cell}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g; # Replace parameters - $t{$cell}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.$self->expandnamed($2)/ge; + $t{$cell}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.$self->expandnamed($2)/sge; } } # @@ -898,7 +919,7 @@ sub sett { $t{$cell}=$formula; $t{$cell}=~s/\.\.+/\,/g; $t{$cell}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g; - $t{$cell}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.$self->expandnamed($2)/ge; + $t{$cell}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.$self->expandnamed($2)/sge; } elsif ( $col =~ /^[A-Z]$/ ) { if ($formula !~ /^\!/ && exists($self->{'constants'}->{$cell}) && $self->{'constants'}->{$cell} ne '') { @@ -909,7 +930,7 @@ sub sett { $t{$cell}=$formula; $t{$cell}=~s/\.\.+/\,/g; $t{$cell}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g; - $t{$cell}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.$self->expandnamed($2)/ge; + $t{$cell}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.$self->expandnamed($2)/sge; } } %{$self->{'safe'}->varglob('t')}=%t; @@ -922,11 +943,11 @@ sub sett { sub sync_safe_space { my $self = shift; # Inside the safe space 'formulas' has a diabolical alter-ego named 'f'. - %{$self->{'safe'}->varglob('f')}=%{$self->{'formulas'}}; + #%{$self->{'safe'}->varglob('f')}=%{$self->{'formulas'}}; # 'constants' leads a peaceful hidden life of 'c'. %{$self->{'safe'}->varglob('c')}=%{$self->{'constants'}}; # 'othersheets' hides as 'os', a disguise few can penetrate. - @{$self->{'safe'}->varglob('os')}=@{$self->{'othersheets'}}; + #@{$self->{'safe'}->varglob('os')}=@{$self->{'othersheets'}}; } ## @@ -979,15 +1000,32 @@ sub formulas { $self->{'formulas'} = $formulas; $self->{'rows'} = []; $self->{'template_cells'} = []; + $self->{'loaded'} = 1; return; } else { + $self->check_formulas_loaded(); return %{$self->{'formulas'}}; } } +sub check_formulas_loaded { + my $self=shift; + if (!$self->{'loaded'}) { + $self->{'loaded'}=1; + # Load in the spreadsheet definition + if (exists($env{'form.workcopy'}) && + $self->{'type'} eq $env{'form.workcopy'}) { + $self->load_tmp(); + } else { + $self->load(); + } + } +} + sub set_formula { my $self = shift; my ($cell,$formula) = @_; + $self->check_formulas_loaded(); $self->{'formulas'}->{$cell}=$formula; return; } @@ -997,7 +1035,7 @@ sub set_formula { ## sub formulas_keys { my $self = shift; - my @keys = keys(%{$self->{'formulas'}}); + $self->check_formulas_loaded(); return keys(%{$self->{'formulas'}}); } @@ -1008,6 +1046,7 @@ sub formulas_keys { sub formula { my $self = shift; my $cell = shift; + $self->check_formulas_loaded(); if (defined($cell) && exists($self->{'formulas'}->{$cell})) { return $self->{'formulas'}->{$cell}; } @@ -1086,6 +1125,7 @@ sub rebuild_stats { my $self = shift; $self->{'rows'}=[]; $self->{'template_cells'}=[]; + $self->check_formulas_loaded(); while (my ($cell,$formula) = each(%{$self->{'formulas'}})) { push(@{$self->{'rows'}},$1) if ($cell =~ /^A(\d+)/ && $1 != 0); push(@{$self->{'template_cells'}},$1) if ($cell =~ /^template_(\w+)/); @@ -1191,12 +1231,19 @@ sub display { last; } } + $self->{outputmode} = $outputmode; if ($outputmode eq 'html') { $self->compute($r); $self->outsheet_html($r); } elsif ($outputmode eq 'htmlclasslist') { # No computation neccessary... This is kludgy $self->outsheet_htmlclasslist($r); + } elsif ($outputmode eq 'source') { + # No computation necessary. Rumor has it that this is some + # sort of kludge w.r.t. not "computing". It's also + # a bit of of a kludge that we call "outsheet_html" and + # let the 'outputmode' cause the outputting of source. + $self->outsheet_html($r); } elsif ($outputmode eq 'excel') { $self->compute($r); $self->outsheet_excel($r); @@ -1236,10 +1283,12 @@ sub html_export_row { foreach my $cell (@rowdata) { if ($cell->{'name'} =~ /^[A-Z]/) { $row_html .= ''. - &html_editable_cell($cell,$color,$allowed).''; + &html_editable_cell($cell,$color,$allowed, + $self->{outputmode} eq 'source').''; } else { $row_html .= ''. - &html_editable_cell($cell,'#DDCCFF',$allowed).''; + &html_editable_cell($cell,'#DDCCFF',$allowed, + $self->{outputmode} eq 'source').''; } } return $row_html; @@ -1258,15 +1307,16 @@ sub html_template_row { $row_html .= ''. &html_uneditable_cell($cell,'#FFDDDD',$allowed).''; } else { - $row_html .= ''. - &html_editable_cell($cell,'#EOFFDD',$allowed).''; + $row_html .= ''. + &html_editable_cell($cell,'#E0FFDD',$allowed, + $self->{outputmode} eq 'source').''; } } return $row_html; } sub html_editable_cell { - my ($cell,$bgcolor,$allowed) = @_; + my ($cell,$bgcolor,$allowed,$showsource) = @_; my $result; my ($name,$formula,$value); if (defined($cell)) { @@ -1276,7 +1326,13 @@ sub html_editable_cell { } $name = '' if (! defined($name)); $formula = '' if (! defined($formula)); - if (! defined($value)) { + if ($showsource) { + if (!defined($formula) || $formula =~ /^\s*$/) { + $value = '#'; + } else { + $value = &HTML::Entities::encode($formula, '<>&"'); + } + } elsif (! defined($value)) { $value = '#'; if ($formula ne '') { $value = 'undefined value'; @@ -1328,8 +1384,9 @@ sub html_row { $row_html .= ''; $row_html .= &html_uneditable_cell($cell,'#FFDDDD'); } else { - $row_html .= ''; - $row_html .= &html_editable_cell($cell,'#E0FFDD',$allowed); + $row_html .= ''; + $row_html .= &html_editable_cell($cell,'#E0FFDD',$allowed, + $self->{outputmode} eq 'source'); } $row_html .= ''; } @@ -1353,6 +1410,8 @@ sub output_options { description => 'HTML'}, {value => 'excel', description => 'Excel'}, + {value => 'source', + description => 'Show Source'}, # {value => 'xml', # description => 'XML'}, {value => 'csv', @@ -1526,6 +1585,7 @@ sub outsheet_xml { ## Will be rendered for the user ## But not on this day my $Str = ''."\n"; + $self->check_formulas_loaded(); while (my ($cell,$formula) = each(%{$self->{'formulas'}})) { if ($cell =~ /^template_(\w+)/) { my $col = $1; @@ -1608,7 +1668,6 @@ sub load { my $stype = $self->{'type'}; my $cnum = $self->{'cnum'}; my $cdom = $self->{'cdom'}; - my $chome = $self->{'chome'}; # my $filename = $self->filename(); my $cachekey = join('_',($cnum,$cdom,$stype,$filename)); @@ -1617,6 +1676,10 @@ sub load { my ($formulas); if (exists($spreadsheets{$cachekey})) { $formulas = $spreadsheets{$cachekey}->{'formulas'}; + $self->formulas($formulas); + $self->{'row_source'}=$spreadsheets{$cachekey}->{'row_source'}; + $self->{'row_numbers'}=$spreadsheets{$cachekey}->{'row_numbers'}; + $self->{'maxrow'}=$spreadsheets{$cachekey}->{'maxrow'}; } else { # Not cached, need to read if (! defined($filename)) { @@ -1642,7 +1705,8 @@ sub load { # Load the spreadsheet definition file from the save file my %tmphash = &Apache::lonnet::dump($filename,$cdom,$cnum); my ($tmp) = keys(%tmphash); - if ($tmp !~ /^(con_lost|error|no_such_host)/i) { + if (%tmphash + && $tmp !~ /^(con_lost|error|no_such_host)/i) { while (my ($cell,$formula) = each(%tmphash)) { $formulas->{$cell}=$formula; } @@ -1650,17 +1714,38 @@ sub load { $formulas = $self->load_system_default_sheet(); } } - $filename=$self->filename(); # filename may have changed - $cachekey = join('_',($cnum,$cdom,$stype,$filename)); - %{$spreadsheets{$cachekey}->{'formulas'}} = %{$formulas}; + $self->formulas($formulas); + $self->set_row_sources(); + $self->set_row_numbers(); + $self->cache_sheet($formulas); } - $self->formulas($formulas); - $self->set_row_sources(); - $self->set_row_numbers(); +} + +sub cache_sheet { + my $self = shift; + my ($formulas) = @_; + my $stype = $self->{'type'}; + my $cnum = $self->{'cnum'}; + my $cdom = $self->{'cdom'}; + # + my $filename = $self->filename(); + my $cachekey = join('_',($cnum,$cdom,$stype,$filename)); + + if (ref($formulas) eq 'HASH') { + %{$spreadsheets{$cachekey}->{'formulas'}} = %{$formulas}; + } + if (ref($self->{'row_source'})) { + %{$spreadsheets{$cachekey}->{'row_source'}} =%{$self->{'row_source'}}; + } + if (ref($self->{'row_numbers'})) { + %{$spreadsheets{$cachekey}->{'row_numbers'}}=%{$self->{'row_numbers'}}; + } + $spreadsheets{$cachekey}->{'maxrow'} = $self->{'maxrow'}; } sub set_row_sources { my $self = shift; + $self->check_formulas_loaded(); while (my ($cell,$value) = each(%{$self->{'formulas'}})) { next if ($cell !~ /^A(\d+)/ || $1 < 1); my $row = $1; @@ -1671,6 +1756,7 @@ sub set_row_sources { sub set_row_numbers { my $self = shift; + $self->check_formulas_loaded(); while (my ($cell,$value) = each(%{$self->{'formulas'}})) { next if ($cell !~ /^A(\d+)$/); next if (! defined($value)); @@ -1705,11 +1791,9 @@ sub save { my $stype = $self->{'type'}; my $cnum = $self->{'cnum'}; my $cdom = $self->{'cdom'}; - my $chome = $self->{'chome'}; my $filename = $self->{'filename'}; - my $cachekey = join('_',($cnum,$cdom,$stype,$filename)); # Cache new sheet - %{$spreadsheets{$cachekey}->{'formulas'}}=%f; + $self->cache_sheet(\%f); # Write sheet foreach (keys(%f)) { delete($f{$_}) if ($f{$_} eq 'import'); @@ -1725,14 +1809,14 @@ sub save { {'spreadsheet_default_'.$stype => $filename }, $cdom,$cnum); return $reply if ($reply ne 'ok'); + &Apache::lonnet::appenv('course.'.$self->{'cid'}.'.spreadsheet_default_'. + $self->{'type'} => $self->filename()); } - if ($self->is_default()) { - if ($self->{'type'} eq 'studentcalc') { - &Apache::lonnet::expirespread('','','studentcalc',''); - } elsif ($self->{'type'} eq 'assesscalc') { - &Apache::lonnet::expirespread('','','assesscalc',''); - &Apache::lonnet::expirespread('','','studentcalc',''); - } + if ($self->{'type'} eq 'studentcalc') { + &Apache::lonnet::expirespread('','','studentcalc',''); + } elsif ($self->{'type'} eq 'assesscalc') { + &Apache::lonnet::expirespread('','','assesscalc',''); + &Apache::lonnet::expirespread('','','studentcalc',''); } return $reply; } @@ -1754,8 +1838,8 @@ sub save_tmp { my %f = $self->formulas(); while( my ($cell,$formula) = each(%f)) { next if ($formula eq 'import'); - print $fh &Apache::lonnet::escape($cell)."=". - &Apache::lonnet::escape($formula)."\n"; + print $fh &escape($cell)."=". + &escape($formula)."\n"; } $fh->close(); } @@ -1773,8 +1857,8 @@ sub load_tmp { while (<$spreadsheet_file>) { chomp; my ($cell,$formula) = split(/=/); - $cell = &Apache::lonnet::unescape($cell); - $formula = &Apache::lonnet::unescape($formula); + $cell = &unescape($cell); + $formula = &unescape($formula); $formulas{$cell} = $formula; } $spreadsheet_file->close(); @@ -1817,14 +1901,13 @@ sub othersheets { my ($stype) = @_; $stype = $self->{'type'} if (! defined($stype) || $stype !~ /calc$/); # - my @alternatives=(); + my @alternatives=(&mt('Default'), &mt('LON-CAPA Standard')); my %results=&Apache::lonnet::dump($stype.'_spreadsheets', $self->{'cdom'}, $self->{'cnum'}); my ($tmp) = keys(%results); - if ($tmp =~ /^(con_lost|error|no_such_host)/i ) { - @alternatives = (&mt('Default')); - } else { - @alternatives = (&mt('Default'), sort (keys(%results))); + if (%results + && $tmp !~ /^(con_lost|error|no_such_host)/i ) { + push(@alternatives, sort(keys(%results))); } return @alternatives; }