--- loncom/interface/spreadsheet/Spreadsheet.pm 2005/05/12 22:42:57 1.44 +++ loncom/interface/spreadsheet/Spreadsheet.pm 2006/04/06 20:24:13 1.67 @@ -1,5 +1,5 @@ # -# $Id: Spreadsheet.pm,v 1.44 2005/05/12 22:42:57 albertel Exp $ +# $Id: Spreadsheet.pm,v 1.67 2006/04/06 20:24:13 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -85,7 +85,7 @@ sub new { my $class = ref($this) || $this; my ($stype) = ($class =~ /Apache::(.*)$/); # - my ($name,$domain,$filename,$usymb)=@_; + my ($name,$domain,$filename,$usymb,$section,$group)=@_; if (defined($usymb) && ref($usymb)) { $usymb = $usymb->symb; } @@ -95,10 +95,25 @@ 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($group) || $group eq '') { + my $grouplist = &Apache::lonnet::get_users_groups($domain,$name, + $env{'request.course.id'}); + if ($grouplist) { + my @groups = &Apache::lonnet::sort_course_groups($grouplist, + $env{'request.course.id'}); + $group = $groups[0]; + } + } # my $self = { name => $name, domain => $domain, + section => $section, + group => $group, 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; } @@ -200,6 +206,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 +228,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'}; } @@ -330,6 +330,10 @@ sub initialize_safe_space { $safehole->wrap(\&mask,$safeeval,'&mask'); $safehole->wrap(\&Apache::lonnet::logthis,$safeeval,'&logthis'); $safeeval->share('$@'); + # Holds the (computed, final) values for the sheet + # This is only written to by &calc, the spreadsheet computation routine. + # It is read by many functions + $safeeval->share('%sheet_values'); my $code=<<'ENDDEFS'; # ---------------------------------------------------- Inside of the safe space # @@ -339,12 +343,9 @@ sub initialize_safe_space { # c: preloaded constants (A-column) # rl: row label # os: other spreadsheets (for student spreadsheet only) -undef %sheet_values; # Holds the (computed, final) values for the sheet - # This is only written to by &calc, the spreadsheet computation routine. - # It is read by many functions -undef %t; # Holds the values of the spreadsheet temporarily. Set in &sett, - # which does the translation of strings like C5 into the value in C5. - # Used in &calc - %t holds the values that are actually eval'd. +undef %t; # Holds the forumlas of the spreadsheet to be computed. Set in + # &sett, which does the translation of strings like C5 into the value + # in C5. Used in &calc - %t holds the values that are actually eval'd. undef %f; # Holds the formulas for each cell. This is the users # (spreadsheet authors) data for each cell. undef %c; # Holds the constants for a sheet. In the assessment @@ -365,12 +366,10 @@ $filename = ''; # # user data $name = ''; -$uhome = ''; $domain = ''; # # course data $csec = ''; -$chome= ''; $cnum = ''; $cdom = ''; $cid = ''; @@ -394,10 +393,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; } @@ -670,34 +671,65 @@ sub MAXPARM { return $max; } +#------------------------------------------------------- + +=pod + +=item &get_values($lower,$upper) + +Inputs: $lower and $upper, cell names ("X12" or "a150") or globs ("X*"). + +Returns: an array ref of the values of the cells that exist in the + speced range + +=cut + +#------------------------------------------------------- sub get_values { my ($lower,$upper)=@_; - my $mask=&mask(@_); + $upper = $lower if (! defined($upper)); my @values; - foreach (grep eval("/$mask/"),keys(%sheet_values)) { - push(@values,$sheet_values{$_}); - } - return \@values; - if (0) { - # perhaps creating a list of possible cells and looking if they exist - # would be faster somtimes? - &logthis("mask is ".$mask); + my ($la,$ld) = ($lower=~/([A-z]|\*)(\d+|\*)/); + my ($ua,$ud) = ($upper=~/([A-z]|\*)(\d+|\*)/); + my ($alpha,$num); + if ($ld ne '*' && $ud ne '*') { my @alpha; if (($la eq '*') || ($ua eq '*')) { @alpha=('A'..'z'); } else { - if ($la gt $ua) { - my $tmp = $ua; - $ua = $la; - $la = $ua; + if ($la gt $ua) { ($la,$ua)=($ua,$la); } + if ((lc($la) ne $la) && (lc($ua) eq $ua)) { + @alpha=($la..'Z','a'..$ua); + } else { + @alpha=($la..$ua); + } + } + my @num=($ld..$ud); + foreach my $a (@alpha) { + foreach my $n (@num) { + if (exists($sheet_values{$a.$n})) { + push(@values,$sheet_values{$a.$n}); + } } - $alpha=($la..$ua); } + return \@values; + } else { + $num = '([1-9]\d*)'; + } + if (($la eq '*') || ($ua eq '*')) { + $alpha='[A-z]'; + } else { + if ($la gt $ua) { ($la,$ua)=($ua,$la); } + $alpha=qq/[$la-$ua]/; + } + my $expression = '^'.$alpha.$num.'$'; + foreach (grep /$expression/,keys(%sheet_values)) { + push(@values,$sheet_values{$_}); } + return \@values; } sub calc { - %sheet_values = %t; my $notfinished = 1; my $lastcalc = ''; my $depth = 0; @@ -738,7 +770,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";}; @@ -758,92 +790,6 @@ ENDDEFS ###################################################### - -###################################################### - -=pod - -=item &mask($lower,$upper) - -Inputs: $lower and $upper, cell names ("X12" or "a150") or globs ("X*"). - -Returns: Regular expression matching spreadsheet cells that are within -the rectangle defined by $lower and $upper. Due to the nature of the -regular expression this result must be used inside an eval(). - -=cut - -###################################################### -{ - -my %memoizer; - -sub mask { - my ($lower,$upper)=@_; - my $key = $lower.'_'.$upper; - if (exists($memoizer{$key})) { - return $memoizer{$key}; - } - $upper = $lower if (! defined($upper)); - # - my ($la,$ld) = ($lower=~/([A-z]|\*)(\d+|\*)/); - my ($ua,$ud) = ($upper=~/([A-z]|\*)(\d+|\*)/); - # - my $alpha=''; - my $num=''; - # - # Do not put parenthases around $alpha. - # $num depends on the value in $1. - if (($la eq '*') || ($ua eq '*')) { - $alpha='[A-z]'; - } else { - if ($la gt $ua) { - my $tmp = $ua; - $ua = $la; - $la = $ua; - } - $alpha=qq/[$la-$ua]/; - } - if ($ld ne '*' && $ud ne '*') { - # Make sure $ld <= $ud - if ($ld > $ud) { - my $tmp = $ud; - $ud = $ld; - $ld = $tmp; - } - # Here we make a regular expression using some advanced regexp - # abilities. - # (\d+) will match the digits of the cell name and dump them in - # to $1 - # (?(?{ ... code ...} pattern_if_true | pattern_if_false)) will - # choose pattern_if_true if { ... code ... } is true and - # pattern_if_false if { ... code ... } is false. - # In this case, pattern_if_true is empty. pattern_if_false is - # 'donotmatch' and will not match our cells because none of - # them end with donotmatch. - # Unfortunately, the use of this type of regular expression - # requires that each match be wrapped in an eval(). Search for - # $mask in this module for examples - $num = '(\d+)(?(?{$1>= '.$ld.' && $1<='.$ud.'})|donotmatch)'; - } else { - $num = '(\d+)'; - } - my $expression = '^'.$alpha.$num.'$'; - $memoizer{$key} = $expression; - return $expression; -} - -# -# Debugging routine -sub dump_memoized_values { - while (my ($key,$value) = each(%memoizer)) { - &Apache::lonnet::logthis('memoizer: '.$key.' = '.$value); - } - return; -} - -} - ## ## sub add_hash_to_safe {} # spreadsheet, would like to destroy ## @@ -877,7 +823,7 @@ sub expandnamed { $result.=$thissum.'+'; } $result=~s/\+$//; - return $result; + return '('.$result.')'; } else { return 0; } @@ -923,6 +869,7 @@ sub expandnamed { sub sett { my $self = shift; my %t=(); + undef(%Apache::Spreadsheet::sheet_values); # # Deal with the template row foreach my $col ($self->template_cells()) { @@ -955,8 +902,8 @@ sub sett { } elsif ( $col =~ /^[A-Z]$/ ) { if ($formula !~ /^\!/ && exists($self->{'constants'}->{$cell}) && $self->{'constants'}->{$cell} ne '') { - my $data = $self->{'constants'}->{$cell}; - $t{$cell} = $data; + $Apache::Spreadsheet::sheet_values{$cell}= + eval($self->{'constants'}->{$cell}); } } else { # $row > 1 and $col =~ /[a-z] $t{$cell}=$formula; @@ -975,11 +922,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'}}; } ## @@ -1032,15 +979,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; } @@ -1050,7 +1014,7 @@ sub set_formula { ## sub formulas_keys { my $self = shift; - my @keys = keys(%{$self->{'formulas'}}); + $self->check_formulas_loaded(); return keys(%{$self->{'formulas'}}); } @@ -1061,6 +1025,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}; } @@ -1139,6 +1104,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+)/); @@ -1244,12 +1210,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); @@ -1289,10 +1262,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; @@ -1312,14 +1287,15 @@ sub html_template_row { &html_uneditable_cell($cell,'#FFDDDD',$allowed).''; } else { $row_html .= ''. - &html_editable_cell($cell,'#EOFFDD',$allowed).''; + &html_editable_cell($cell,'#EOFFDD',$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)) { @@ -1329,7 +1305,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'; @@ -1382,7 +1364,8 @@ sub html_row { $row_html .= &html_uneditable_cell($cell,'#FFDDDD'); } else { $row_html .= ''; - $row_html .= &html_editable_cell($cell,'#E0FFDD',$allowed); + $row_html .= &html_editable_cell($cell,'#E0FFDD',$allowed, + $self->{outputmode} eq 'source'); } $row_html .= ''; } @@ -1406,6 +1389,8 @@ sub output_options { description => 'HTML'}, {value => 'excel', description => 'Excel'}, + {value => 'source', + description => 'Show Source'}, # {value => 'xml', # description => 'XML'}, {value => 'csv', @@ -1579,6 +1564,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; @@ -1661,7 +1647,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)); @@ -1670,6 +1655,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)) { @@ -1703,17 +1692,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->formulas($formulas); + $self->set_row_sources(); + $self->set_row_numbers(); + $self->cache_sheet($formulas); + } +} + +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; @@ -1724,6 +1734,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)); @@ -1758,11 +1769,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'); @@ -1778,6 +1787,8 @@ 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') {