version 1.27, 2003/09/16 15:36:32
|
version 1.35, 2004/02/24 17:02:04
|
Line 59 use HTML::Entities();
|
Line 59 use HTML::Entities();
|
use HTML::TokeParser; |
use HTML::TokeParser; |
use Spreadsheet::WriteExcel; |
use Spreadsheet::WriteExcel; |
use Time::HiRes; |
use Time::HiRes; |
|
use Apache::lonlocal; |
|
|
## |
## |
## Package Variables |
## Package Variables |
Line 159 sub filename {
|
Line 160 sub filename {
|
if ($newfilename !~ /\w/ || $newfilename =~ /^\W*$/) { |
if ($newfilename !~ /\w/ || $newfilename =~ /^\W*$/) { |
$newfilename = 'default_'.$self->{'type'}; |
$newfilename = 'default_'.$self->{'type'}; |
} |
} |
if ($newfilename !~ /^default\.$self->{'type'}$/ ) { |
if ($newfilename !~ /^default\.$self->{'type'}$/ && |
|
$newfilename !~ /^\/res\/(.*)spreadsheet$/) { |
if ($newfilename !~ /_$self->{'type'}$/) { |
if ($newfilename !~ /_$self->{'type'}$/) { |
$newfilename =~ s/[\s_]*$//; |
$newfilename =~ s/[\s_]*$//; |
$newfilename .= '_'.$self->{'type'}; |
$newfilename .= '_'.$self->{'type'}; |
Line 613 parametername should be a string such as
|
Line 615 parametername should be a string such as
|
sub MINPARM { |
sub MINPARM { |
my ($expression) = @_; |
my ($expression) = @_; |
my $min = undef; |
my $min = undef; |
study($expression); |
|
foreach $parameter (keys(%c)) { |
foreach $parameter (keys(%c)) { |
next if ($parameter !~ /$expression/); |
next if ($parameter !~ /$expression/); |
if ((! defined($min)) || ($min > $c{$parameter})) { |
if ((! defined($min)) || ($min > $c{$parameter})) { |
Line 638 parametername should be a string such as
|
Line 639 parametername should be a string such as
|
sub MAXPARM { |
sub MAXPARM { |
my ($expression) = @_; |
my ($expression) = @_; |
my $max = undef; |
my $max = undef; |
study($expression); |
|
foreach $parameter (keys(%c)) { |
foreach $parameter (keys(%c)) { |
next if ($parameter !~ /$expression/); |
next if ($parameter !~ /$expression/); |
if ((! defined($min)) || ($max < $c{$parameter})) { |
if ((! defined($min)) || ($max < $c{$parameter})) { |
Line 677 sub calc {
|
Line 677 sub calc {
|
return $lastcalc.': Maximum calculation depth exceeded'; |
return $lastcalc.': Maximum calculation depth exceeded'; |
} |
} |
} |
} |
return ''; |
return 'okay'; |
} |
} |
|
|
# ------------------------------------------- End of "Inside of the safe space" |
# ------------------------------------------- End of "Inside of the safe space" |
Line 844 sub expandnamed {
|
Line 844 sub expandnamed {
|
my @matches = (); |
my @matches = (); |
my @values = (); |
my @values = (); |
$#matches = -1; |
$#matches = -1; |
study $expression; |
|
while (my($parameter,$value) = each(%{$self->{'constants'}})) { |
while (my($parameter,$value) = each(%{$self->{'constants'}})) { |
next if ($parameter !~ /$expression/); |
next if ($parameter !~ /$expression/); |
push(@matches,$parameter); |
push(@matches,$parameter); |
Line 1138 sub calcsheet {
|
Line 1137 sub calcsheet {
|
# $self->logthis($self->get_errorlog()); |
# $self->logthis($self->get_errorlog()); |
%{$self->{'values'}} = %{$self->{'safe'}->varglob('sheet_values')}; |
%{$self->{'values'}} = %{$self->{'safe'}->varglob('sheet_values')}; |
# $self->logthis($self->get_errorlog()); |
# $self->logthis($self->get_errorlog()); |
|
if ($result ne 'okay') { |
|
$self->set_calcerror($result); |
|
} |
return $result; |
return $result; |
} |
} |
|
|
|
sub set_badcalc { |
|
my $self = shift(); |
|
$self->{'badcalc'} =1; |
|
return; |
|
} |
|
|
|
sub badcalc { |
|
my $self = shift; |
|
if (exists($self->{'badcalc'}) && $self->{'badcalc'}) { |
|
return 1; |
|
} else { |
|
return 0; |
|
} |
|
} |
|
|
|
sub set_calcerror { |
|
my $self = shift; |
|
if (@_) { |
|
$self->set_badcalc(); |
|
if (exists($self->{'calcerror'})) { |
|
$self->{'calcerror'}.="\n".$_[0]; |
|
} else { |
|
$self->{'calcerror'}.=$_[0]; |
|
} |
|
} |
|
} |
|
|
|
sub calcerror { |
|
my $self = shift; |
|
if ($self->badcalc()) { |
|
if (exists($self->{'calcerror'})) { |
|
return $self->{'calcerror'}; |
|
} |
|
} |
|
return; |
|
} |
|
|
########################################################### |
########################################################### |
## |
## |
## Output Helpers |
## Output Helpers |
Line 1149 sub calcsheet {
|
Line 1188 sub calcsheet {
|
sub display { |
sub display { |
my $self = shift; |
my $self = shift; |
my ($r) = @_; |
my ($r) = @_; |
$self->compute($r); |
|
my $outputmode = 'html'; |
my $outputmode = 'html'; |
if ($ENV{'form.output_format'} =~ /^(html|excel|csv)$/) { |
foreach ($self->output_options()) { |
$outputmode = $ENV{'form.output_format'}; |
if ($ENV{'form.output_format'} eq $_->{'value'}) { |
|
$outputmode = $_->{'value'}; |
|
last; |
|
} |
} |
} |
if ($outputmode eq 'html') { |
if ($outputmode eq 'html') { |
|
$self->compute($r); |
$self->outsheet_html($r); |
$self->outsheet_html($r); |
|
} elsif ($outputmode eq 'htmlclasslist') { |
|
# No computation neccessary... This is kludgy |
|
$self->outsheet_htmlclasslist($r); |
} elsif ($outputmode eq 'excel') { |
} elsif ($outputmode eq 'excel') { |
|
$self->compute($r); |
$self->outsheet_excel($r); |
$self->outsheet_excel($r); |
} elsif ($outputmode eq 'csv') { |
} elsif ($outputmode eq 'csv') { |
|
$self->compute($r); |
$self->outsheet_csv($r); |
$self->outsheet_csv($r); |
|
} elsif ($outputmode eq 'xml') { |
|
# $self->compute($r); |
|
$self->outsheet_xml($r); |
} |
} |
$self->cleanup(); |
$self->cleanup(); |
return; |
return; |
Line 1168 sub display {
|
Line 1218 sub display {
|
############################################ |
############################################ |
## HTML output routines ## |
## HTML output routines ## |
############################################ |
############################################ |
|
sub html_report_error { |
|
my $self = shift(); |
|
my $Str = ''; |
|
if ($self->badcalc()) { |
|
$Str = '<h3 style="color:red">'. |
|
&mt('An error occurred while calculating this spreadsheet'). |
|
"</h3>\n". |
|
'<pre>'.$self->calcerror()."</pre>\n"; |
|
} |
|
return $Str; |
|
} |
|
|
sub html_export_row { |
sub html_export_row { |
my $self = shift(); |
my $self = shift(); |
my ($color) = @_; |
my ($color) = @_; |
Line 1282 sub html_header {
|
Line 1344 sub html_header {
|
my $self = shift; |
my $self = shift; |
return '' if (! $ENV{'request.role.adv'}); |
return '' if (! $ENV{'request.role.adv'}); |
return "<table>\n". |
return "<table>\n". |
'<tr><th align="center">Output Format</th><tr>'."\n". |
'<tr><th align="center">'.&mt('Output Format').'</th></tr>'."\n". |
'<tr><td>'.&output_selector()."</td></tr>\n". |
'<tr><td>'.$self->output_selector()."</td></tr>\n". |
"</table>\n"; |
"</table>\n"; |
} |
} |
|
|
|
## |
|
## Default output types are HTML, Excel, and CSV |
|
sub output_options { |
|
my $self = shift(); |
|
return ({value => 'html', |
|
description => 'HTML'}, |
|
{value => 'excel', |
|
description => 'Excel'}, |
|
# {value => 'xml', |
|
# description => 'XML'}, |
|
{value => 'csv', |
|
description => 'Comma Separated Values'},); |
|
} |
|
|
sub output_selector { |
sub output_selector { |
|
my $self = shift(); |
my $output_selector = '<select name="output_format" size="3">'."\n"; |
my $output_selector = '<select name="output_format" size="3">'."\n"; |
my $default = 'html'; |
my $default = 'html'; |
if (exists($ENV{'form.output_format'})) { |
if (exists($ENV{'form.output_format'})) { |
Line 1295 sub output_selector {
|
Line 1372 sub output_selector {
|
} else { |
} else { |
$ENV{'form.output_format'} = $default; |
$ENV{'form.output_format'} = $default; |
} |
} |
foreach (['html','HTML'], |
foreach ($self->output_options()) { |
['excel','Excel'], |
$output_selector.='<option value="'.$_->{'value'}.'"'; |
['csv','Comma Seperated Values']) { |
if ($_->{'value'} eq $default) { |
my ($name,$description) = @{$_}; |
|
$output_selector.=qq{<option value="$name"}; |
|
if ($name eq $default) { |
|
$output_selector .= ' selected'; |
$output_selector .= ' selected'; |
} |
} |
$output_selector .= ">$description</option>\n"; |
$output_selector .= ">".&mt($_->{'description'})."</option>\n"; |
} |
} |
$output_selector .= "</select>\n"; |
$output_selector .= "</select>\n"; |
return $output_selector; |
return $output_selector; |
Line 1336 sub create_excel_spreadsheet {
|
Line 1410 sub create_excel_spreadsheet {
|
my $workbook = Spreadsheet::WriteExcel->new('/home/httpd'.$filename); |
my $workbook = Spreadsheet::WriteExcel->new('/home/httpd'.$filename); |
if (! defined($workbook)) { |
if (! defined($workbook)) { |
$r->log_error("Error creating excel spreadsheet $filename: $!"); |
$r->log_error("Error creating excel spreadsheet $filename: $!"); |
$r->print("Problems creating new Excel file. ". |
$r->print(&mt("Problems creating new Excel file. ". |
"This error has been logged. ". |
"This error has been logged. ". |
"Please alert your LON-CAPA administrator"); |
"Please alert your LON-CAPA administrator")); |
return undef; |
return undef; |
} |
} |
# |
# |
Line 1352 sub create_excel_spreadsheet {
|
Line 1426 sub create_excel_spreadsheet {
|
return ($workbook,$filename); |
return ($workbook,$filename); |
} |
} |
|
|
|
# |
|
# This routine is just a stub |
|
sub outsheet_htmlclasslist { |
|
my $self = shift; |
|
my ($r) = @_; |
|
$r->print('<h2>'.&mt("This output is not supported").'</h2>'); |
|
$r->rflush(); |
|
return; |
|
} |
|
|
sub outsheet_excel { |
sub outsheet_excel { |
my $self = shift; |
my $self = shift; |
my ($r) = @_; |
my ($r) = @_; |
my $connection = $r->connection(); |
my $connection = $r->connection(); |
$r->print("<h2>Preparing Excel Spreadsheet</h2>"); |
# |
|
$r->print($self->html_report_error()); |
|
$r->rflush(); |
|
# |
|
$r->print("<h2>".&mt('Preparing Excel Spreadsheet')."</h2>"); |
# |
# |
# Create excel worksheet |
# Create excel worksheet |
my ($workbook,$filename) = $self->create_excel_spreadsheet($r); |
my ($workbook,$filename) = $self->create_excel_spreadsheet($r); |
Line 1398 sub outsheet_csv {
|
Line 1486 sub outsheet_csv {
|
my $self = shift; |
my $self = shift; |
my ($r) = @_; |
my ($r) = @_; |
my $connection = $r->connection(); |
my $connection = $r->connection(); |
|
# |
|
$r->print($self->html_report_error()); |
|
$r->rflush(); |
|
# |
my $csvdata = ''; |
my $csvdata = ''; |
my @Values; |
my @Values; |
# |
# |
Line 1408 sub outsheet_csv {
|
Line 1500 sub outsheet_csv {
|
my $file; |
my $file; |
unless ($file = Apache::File->new('>'.'/home/httpd'.$filename)) { |
unless ($file = Apache::File->new('>'.'/home/httpd'.$filename)) { |
$r->log_error("Couldn't open $filename for output $!"); |
$r->log_error("Couldn't open $filename for output $!"); |
$r->print("Problems occured in writing the csv file. ". |
$r->print(&mt("Problems occured in writing the csv file. ". |
"This error has been logged. ". |
"This error has been logged. ". |
"Please alert your LON-CAPA administrator."); |
"Please alert your LON-CAPA administrator.")); |
$r->print("<pre>\n".$csvdata."</pre>\n"); |
$r->print("<pre>\n".$csvdata."</pre>\n"); |
return 0; |
return 0; |
} |
} |
Line 1426 sub outsheet_csv {
|
Line 1518 sub outsheet_csv {
|
# Close the csv file |
# Close the csv file |
close($file); |
close($file); |
$r->print('<br /><br />'. |
$r->print('<br /><br />'. |
'<a href="'.$filename.'">Your CSV spreadsheet.</a>'."\n"); |
'<a href="'.$filename.'">'.&mt('Your CSV spreadsheet.').'</a>'."\n"); |
# |
# |
return 1; |
return 1; |
} |
} |
Line 1462 sub outsheet_xml {
|
Line 1554 sub outsheet_xml {
|
## But not on this day |
## But not on this day |
my $Str = '<spreadsheet type="'.$self->{'type'}.'">'."\n"; |
my $Str = '<spreadsheet type="'.$self->{'type'}.'">'."\n"; |
while (my ($cell,$formula) = each(%{$self->{'formulas'}})) { |
while (my ($cell,$formula) = each(%{$self->{'formulas'}})) { |
if ($cell =~ /^template_(\d+)/) { |
if ($cell =~ /^template_(\w+)/) { |
my $col = $1; |
my $col = $1; |
$Str .= '<template col="'.$col.'">'.$formula.'</template>'."\n"; |
$Str .= '<template col="'.$col.'">'.$formula.'</template>'."\n"; |
} else { |
} else { |
my ($row,$col) = ($cell =~ /^([A-z])(\d+)/); |
my ($col,$row) = ($cell =~ /^([A-z])(\d+)/); |
next if (! defined($row) || ! defined($col)); |
next if (! defined($row) || ! defined($col)); |
$Str .= '<field row="'.$row.'" col="'.$col.'" >'.$formula.'</cell>' |
next if ($row != 0); |
|
$Str .= |
|
'<field row="'.$row.'" col="'.$col.'" >'.$formula.'</field>' |
."\n"; |
."\n"; |
} |
} |
} |
} |
$Str.="</spreadsheet>"; |
$Str.="</spreadsheet>"; |
|
$r->print("<pre>\n\n\n".$Str."\n\n\n</pre>"); |
return $Str; |
return $Str; |
} |
} |
|
|
Line 1499 sub parse_sheet {
|
Line 1594 sub parse_sheet {
|
$formulas{$cell} = $formula; |
$formulas{$cell} = $formula; |
$sources{$cell} = $source if (defined($source)); |
$sources{$cell} = $source if (defined($source)); |
$parser->get_text('/field'); |
$parser->get_text('/field'); |
} |
} elsif ($token->[1] eq 'template') { |
if ($token->[1] eq 'template') { |
|
$formulas{'template_'.$token->[2]->{'col'}}= |
$formulas{'template_'.$token->[2]->{'col'}}= |
$parser->get_text('/template'); |
$parser->get_text('/template'); |
} |
} |
Line 1554 sub load {
|
Line 1648 sub load {
|
# Not cached, need to read |
# Not cached, need to read |
if (! defined($filename)) { |
if (! defined($filename)) { |
$formulas = $self->load_system_default_sheet(); |
$formulas = $self->load_system_default_sheet(); |
} elsif($self->filename() =~ /^\/res\/.*\.spreadsheet$/) { |
} elsif($filename =~ /^\/res\/.*\.spreadsheet$/) { |
# Load a spreadsheet definition file |
# Load a spreadsheet definition file |
my $sheetxml=&Apache::lonnet::getfile |
my $sheetxml=&Apache::lonnet::getfile |
(&Apache::lonnet::filelocation('',$filename)); |
(&Apache::lonnet::filelocation('',$filename)); |
Line 1617 sub set_row_numbers {
|
Line 1711 sub set_row_numbers {
|
## |
## |
sub exportrow { |
sub exportrow { |
my $self = shift; |
my $self = shift; |
|
if (exists($self->{'badcalc'}) && $self->{'badcalc'}) { |
|
return (); |
|
} |
my @exportarray; |
my @exportarray; |
foreach my $column (@UC_Columns) { |
foreach my $column (@UC_Columns) { |
push(@exportarray,$self->value($column.'0')); |
push(@exportarray,$self->value($column.'0')); |
Line 1752 sub othersheets {
|
Line 1849 sub othersheets {
|
$self->{'cdom'}, $self->{'cnum'}); |
$self->{'cdom'}, $self->{'cnum'}); |
my ($tmp) = keys(%results); |
my ($tmp) = keys(%results); |
if ($tmp =~ /^(con_lost|error|no_such_host)/i ) { |
if ($tmp =~ /^(con_lost|error|no_such_host)/i ) { |
@alternatives = ('Default'); |
@alternatives = (&mt('Default')); |
} else { |
} else { |
@alternatives = ('Default', sort (keys(%results))); |
@alternatives = (&mt('Default'), sort (keys(%results))); |
} |
} |
return @alternatives; |
return @alternatives; |
} |
} |