version 1.139, 2002/11/12 22:44:28
|
version 1.145, 2002/11/21 18:56:36
|
Line 53 built-in functions.
|
Line 53 built-in functions.
|
package Apache::lonspreadsheet; |
package Apache::lonspreadsheet; |
|
|
use strict; |
use strict; |
|
use Apache::Constants qw(:common :http); |
|
use Apache::lonnet; |
|
use Apache::lonhtmlcommon; |
|
use Apache::loncoursedata; |
|
use Apache::File(); |
use Safe; |
use Safe; |
use Safe::Hole; |
use Safe::Hole; |
use Opcode; |
use Opcode; |
use Apache::lonnet; |
|
use Apache::Constants qw(:common :http); |
|
use GDBM_File; |
use GDBM_File; |
use HTML::TokeParser; |
use HTML::TokeParser; |
use Apache::lonhtmlcommon; |
|
use Apache::loncoursedata; |
|
use Apache::File(); |
|
use Spreadsheet::WriteExcel; |
use Spreadsheet::WriteExcel; |
|
|
# |
# |
Line 1149 sub exportdata {
|
Line 1149 sub exportdata {
|
|
|
|
|
sub update_student_sheet{ |
sub update_student_sheet{ |
my $sheet = shift; |
my ($sheet,$r,$c) = @_; |
# Load in the studentcalc sheet |
# Load in the studentcalc sheet |
&readsheet($sheet,'default_studentcalc'); |
&readsheet($sheet,'default_studentcalc'); |
# Determine the structure (contained assessments, etc) of the sheet |
# Determine the structure (contained assessments, etc) of the sheet |
Line 1157 sub update_student_sheet{
|
Line 1157 sub update_student_sheet{
|
# Load in the cached sheets for this student |
# Load in the cached sheets for this student |
&cachedssheets($sheet); |
&cachedssheets($sheet); |
# Load in the (possibly cached) data from the assessment sheets |
# Load in the (possibly cached) data from the assessment sheets |
&loadstudent($sheet); |
&loadstudent($sheet,$r,$c); |
# Compute the sheet |
# Compute the sheet |
&calcsheet($sheet); |
&calcsheet($sheet); |
} |
} |
Line 1186 sub get_row {
|
Line 1186 sub get_row {
|
######################################################################## |
######################################################################## |
sub sort_indicies { |
sub sort_indicies { |
my $sheet = shift; |
my $sheet = shift; |
# |
|
# Sort the rows in some manner |
|
# |
|
my @sortby=(); |
|
my @sortidx=(); |
my @sortidx=(); |
# Skip row 0 |
# |
for (my $row=1;$row<=$sheet->{'maxrow'};$row++) { |
if ($sheet->{'sheettype'} eq 'classcalc') { |
push (@sortby, $sheet->{'safe'}->reval('$f{"A'.$row.'"}')); |
my @sortby=(undef); |
push (@sortidx, $row); |
# Skip row 0 |
|
for (my $row=1;$row<=$sheet->{'maxrow'};$row++) { |
|
my (undef,$sname,$sdom,$fullname,$section,$id) = |
|
split(':',$sheet->{'rowlabel'}->{$sheet->{'f'}->{'A'.$row}}); |
|
push (@sortby, lc($fullname)); |
|
push (@sortidx, $row); |
|
} |
|
@sortidx = sort { $sortby[$a] cmp $sortby[$b]; } @sortidx; |
|
} elsif ($sheet->{'sheettype'} eq 'studentcalc') { |
|
my @sortby1=(undef); |
|
my @sortby2=(undef); |
|
# Skip row 0 |
|
for (my $row=1;$row<=$sheet->{'maxrow'};$row++) { |
|
my (undef,$symb,$uname,$udom,$mapid,$resid,$title) = |
|
split(':',$sheet->{'rowlabel'}->{$sheet->{'f'}->{'A'.$row}}); |
|
$symb = &Apache::lonnet::unescape($symb); |
|
my ($sequence) = ($symb =~ /\/([^\/]*\.sequence)/); |
|
if ($sequence eq '') { |
|
$sequence = $symb; |
|
} |
|
push (@sortby1, $sequence); |
|
push (@sortby2, $title); |
|
push (@sortidx, $row); |
|
} |
|
@sortidx = sort { $sortby1[$a] cmp $sortby1[$b] || |
|
$sortby2[$a] cmp $sortby2[$b] } @sortidx; |
|
} else { |
|
my @sortby=(undef); |
|
# Skip row 0 |
|
for (my $row=1;$row<=$sheet->{'maxrow'};$row++) { |
|
push (@sortby, $sheet->{'safe'}->reval('$f{"A'.$row.'"}')); |
|
push (@sortidx, $row); |
|
} |
|
@sortidx = sort { $sortby[$a] cmp $sortby[$b]; } @sortidx; |
} |
} |
@sortidx=sort { lc($sortby[$a]) cmp lc($sortby[$b]); } @sortidx; |
|
return @sortidx; |
return @sortidx; |
} |
} |
|
|
Line 1332 END
|
Line 1360 END
|
foreach my $rownum (@Rows) { |
foreach my $rownum (@Rows) { |
my ($rowlabel,@rowdata) = &get_row($sheet,$rownum); |
my ($rowlabel,@rowdata) = &get_row($sheet,$rownum); |
next if ($rowlabel =~ /^\s*$/); |
next if ($rowlabel =~ /^\s*$/); |
|
next if (($sheet->{'sheettype'} eq 'assesscalc') && |
|
(! $ENV{'form.showall'}) && |
|
($rowdata[0]->{'value'} =~ /^\s*$/)); |
|
if (! $ENV{'form.showall'} && |
|
$sheet->{'sheettype'} =~ /^(studentcalc|classcalc)$/) { |
|
my $row_is_empty = 1; |
|
foreach my $cell (@rowdata) { |
|
if ($cell->{'value'} !~ /^\s*$/) { |
|
$row_is_empty = 0; |
|
last; |
|
} |
|
} |
|
next if ($row_is_empty); |
|
} |
# |
# |
my $defaultbg='#E0FF'; |
my $defaultbg='#E0FF'; |
# |
# |
Line 1496 END
|
Line 1538 END
|
# Create a new spreadsheet |
# Create a new spreadsheet |
my $studentsheet = &makenewsheet($sname,$sdom,'studentcalc',undef); |
my $studentsheet = &makenewsheet($sname,$sdom,'studentcalc',undef); |
# Read in the spreadsheet definition |
# Read in the spreadsheet definition |
&update_student_sheet($studentsheet); |
&update_student_sheet($studentsheet,$r,$c); |
# Stuff the sheet into excel |
# Stuff the sheet into excel |
&export_sheet_as_excel($studentsheet,$student_excel_worksheet); |
&export_sheet_as_excel($studentsheet,$student_excel_worksheet); |
my $totaltime = int((time - $starttime) / $count * $sheet->{'maxrow'}); |
my $totaltime = int((time - $starttime) / $count * $sheet->{'maxrow'}); |
Line 1612 sub export_sheet_as_excel {
|
Line 1654 sub export_sheet_as_excel {
|
# Loop through the rows and output them one at a time |
# Loop through the rows and output them one at a time |
foreach my $rownum (@Rows) { |
foreach my $rownum (@Rows) { |
my ($rowlabel,@rowdata) = &get_row($sheet,$rownum); |
my ($rowlabel,@rowdata) = &get_row($sheet,$rownum); |
next if ($rowlabel =~ /^\s*$/); |
next if ($rowlabel =~ /^[\s]*$/); |
$cols_output = 0; |
$cols_output = 0; |
my $label = &format_excel_rowlabel($rowlabel); |
my $label = &format_excel_rowlabel($rowlabel); |
|
if ( ! $ENV{'form.showall'} && |
|
$sheet->{'sheettype'} =~ /^(studentcalc|classcalc)$/) { |
|
my $row_is_empty = 1; |
|
foreach my $cell (@rowdata) { |
|
if ($cell->{'value'} !~ /^\s*$/) { |
|
$row_is_empty = 0; |
|
last; |
|
} |
|
} |
|
next if ($row_is_empty); |
|
} |
$worksheet->write($rows_output,$cols_output++,$label); |
$worksheet->write($rows_output,$cols_output++,$label); |
if (ref($label)) { |
if (ref($label)) { |
$cols_output = (scalar(@$label)); |
$cols_output = (scalar(@$label)); |
Line 1641 sub outsheet_xml {
|
Line 1694 sub outsheet_xml {
|
## Outsheet - calls other outsheet_* functions |
## Outsheet - calls other outsheet_* functions |
## |
## |
sub outsheet { |
sub outsheet { |
my ($r,$sheet)=@_; |
my ($sheet,$r)=@_; |
if (! exists($ENV{'form.output'})) { |
if (! exists($ENV{'form.output'})) { |
$ENV{'form.output'} = 'HTML'; |
$ENV{'form.output'} = 'HTML'; |
} |
} |
Line 1753 sub readsheet {
|
Line 1806 sub readsheet {
|
if ($fh=Apache::File->new($includedir.'/'.$dfn)) { |
if ($fh=Apache::File->new($includedir.'/'.$dfn)) { |
$sheetxml=join('',<$fh>); |
$sheetxml=join('',<$fh>); |
} else { |
} else { |
$sheetxml='<field row="0" col="A">"Error"</field>'; |
# $sheetxml='<field row="0" col="A">"Error"</field>'; |
|
$sheetxml='<field row="0" col="A"></field>'; |
} |
} |
%f=%{&parse_sheet(\$sheetxml)}; |
%f=%{&parse_sheet(\$sheetxml)}; |
} elsif($fn=~/\/*\.spreadsheet$/) { |
} elsif($fn=~/\/*\.spreadsheet$/) { |
Line 2296 sub updatestudentassesssheet {
|
Line 2350 sub updatestudentassesssheet {
|
# ------------------------------------------------ Load data for one assessment |
# ------------------------------------------------ Load data for one assessment |
|
|
sub loadstudent{ |
sub loadstudent{ |
my ($sheet)=@_; |
my ($sheet,$r,$c)=@_; |
my %c=(); |
my %constants=(); |
my %f=&getformulas($sheet); |
my %formulas=&getformulas($sheet); |
$cachedassess=$sheet->{'uname'}.':'.$sheet->{'udom'}; |
$cachedassess=$sheet->{'uname'}.':'.$sheet->{'udom'}; |
# Get ALL the student preformance data |
# Get ALL the student preformance data |
my @tmp = &Apache::lonnet::dump($sheet->{'cid'}, |
my @tmp = &Apache::lonnet::dump($sheet->{'cid'}, |
Line 2311 sub loadstudent{
|
Line 2365 sub loadstudent{
|
undef @tmp; |
undef @tmp; |
# |
# |
my @assessdata=(); |
my @assessdata=(); |
while (my ($cell,$value) = each (%f)) { |
foreach my $cell (keys(%formulas)) { |
|
my $value = $formulas{$cell}; |
|
if(defined($c) && ($c->aborted())) { |
|
last; |
|
} |
next if ($cell !~ /^A(\d+)/); |
next if ($cell !~ /^A(\d+)/); |
my $row=$1; |
my $row=$1; |
next if (($value =~ /^[!~-]/) || ($row==0)); |
next if (($value =~ /^[!~-]/) || ($row==0)); |
my ($usy,$ufn)=split(/__&&&\__/,$value); |
my ($usy,$ufn)=split(/__&&&\__/,$value); |
@assessdata=&exportsheet($sheet,$sheet->{'uname'}, |
@assessdata=&exportsheet($sheet,$sheet->{'uname'}, |
$sheet->{'udom'}, |
$sheet->{'udom'}, |
'assesscalc',$usy,$ufn); |
'assesscalc',$usy,$ufn,$r); |
my $index=0; |
my $index=0; |
foreach ('A','B','C','D','E','F','G','H','I','J','K','L','M', |
foreach my $col ('A','B','C','D','E','F','G','H','I','J','K','L','M', |
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z') { |
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z') { |
if (defined($assessdata[$index])) { |
if (defined($assessdata[$index])) { |
my $col=$_; |
|
if ($assessdata[$index]=~/\D/) { |
if ($assessdata[$index]=~/\D/) { |
$c{$col.$row}="'".$assessdata[$index]."'"; |
$constants{$col.$row}="'".$assessdata[$index]."'"; |
} else { |
} else { |
$c{$col.$row}=$assessdata[$index]; |
$constants{$col.$row}=$assessdata[$index]; |
} |
|
unless ($col eq 'A') { |
|
$f{$col.$row}='import'; |
|
} |
} |
|
$formulas{$col.$row}='import' if ($col ne 'A'); |
} |
} |
$index++; |
$index++; |
} |
} |
} |
} |
$cachedassess=''; |
$cachedassess=''; |
undef %cachedstores; |
undef %cachedstores; |
$sheet->{'f'} = \%f; |
$sheet->{'f'} = \%formulas; |
&setformulas($sheet); |
&setformulas($sheet); |
&setconstants($sheet,\%c); |
&setconstants($sheet,\%constants); |
} |
} |
|
|
# --------------------------------------------------- Load data for one student |
# --------------------------------------------------- Load data for one student |
# |
# |
sub loadcourse { |
sub loadcourse { |
my ($sheet,$r)=@_; |
my ($sheet,$r,$c)=@_; |
# |
# |
my %c=(); |
my %constants=(); |
my %f=&getformulas($sheet); |
my %formulas=&getformulas($sheet); |
# |
# |
my $total=0; |
my $total=0; |
foreach (keys(%f)) { |
foreach (keys(%formulas)) { |
if ($_=~/^A(\d+)/) { |
if ($_=~/^A(\d+)/) { |
unless ($f{$_}=~/^[\!\~\-]/) { $total++; } |
unless ($formulas{$_}=~/^[\!\~\-]/) { $total++; } |
} |
} |
} |
} |
my $now=0; |
my $now=0; |
Line 2371 sub loadcourse {
|
Line 2426 sub loadcourse {
|
</script> |
</script> |
ENDPOP |
ENDPOP |
$r->rflush(); |
$r->rflush(); |
foreach (keys(%f)) { |
foreach (keys(%formulas)) { |
|
if(defined($c) && ($c->aborted())) { |
|
last; |
|
} |
next if ($_!~/^A(\d+)/); |
next if ($_!~/^A(\d+)/); |
my $row=$1; |
my $row=$1; |
next if (($f{$_}=~/^[\!\~\-]/) || ($row==0)); |
next if (($formulas{$_}=~/^[\!\~\-]/) || ($row==0)); |
my ($sname,$sdom) = split(':',$f{$_}); |
my ($sname,$sdom) = split(':',$formulas{$_}); |
my @studentdata=&exportsheet($sheet,$sname,$sdom,'studentcalc'); |
my @studentdata=&exportsheet($sheet,$sname,$sdom,'studentcalc', |
|
undef,undef,$r); |
undef %userrdatas; |
undef %userrdatas; |
$now++; |
$now++; |
$r->print('<script>popwin.document.popremain.remaining.value="'. |
$r->print('<script>popwin.document.popremain.remaining.value="'. |
Line 2390 ENDPOP
|
Line 2449 ENDPOP
|
if (defined($studentdata[$index])) { |
if (defined($studentdata[$index])) { |
my $col=$_; |
my $col=$_; |
if ($studentdata[$index]=~/\D/) { |
if ($studentdata[$index]=~/\D/) { |
$c{$col.$row}="'".$studentdata[$index]."'"; |
$constants{$col.$row}="'".$studentdata[$index]."'"; |
} else { |
} else { |
$c{$col.$row}=$studentdata[$index]; |
$constants{$col.$row}=$studentdata[$index]; |
} |
} |
unless ($col eq 'A') { |
unless ($col eq 'A') { |
$f{$col.$row}='import'; |
$formulas{$col.$row}='import'; |
} |
} |
} |
} |
$index++; |
$index++; |
} |
} |
} |
} |
$sheet->{'f'}=\%f; |
$sheet->{'f'}=\%formulas; |
&setformulas($sheet); |
&setformulas($sheet); |
&setconstants($sheet,\%c); |
&setconstants($sheet,\%constants); |
$r->print('<script>popwin.close()</script>'); |
$r->print('<script>popwin.close()</script>'); |
$r->rflush(); |
$r->rflush(); |
} |
} |
Line 2411 ENDPOP
|
Line 2470 ENDPOP
|
# ------------------------------------------------ Load data for one assessment |
# ------------------------------------------------ Load data for one assessment |
# |
# |
sub loadassessment { |
sub loadassessment { |
my ($sheet)=@_; |
my ($sheet,$r,$c)=@_; |
|
|
my $uhome = $sheet->{'uhome'}; |
my $uhome = $sheet->{'uhome'}; |
my $uname = $sheet->{'uname'}; |
my $uname = $sheet->{'uname'}; |
Line 2575 sub updatesheet {
|
Line 2634 sub updatesheet {
|
|
|
sub loadrows { |
sub loadrows { |
my ($sheet,$r)=@_; |
my ($sheet,$r)=@_; |
|
my $c = $r->connection; |
my $stype=$sheet->{'sheettype'}; |
my $stype=$sheet->{'sheettype'}; |
if ($stype eq 'classcalc') { |
if ($stype eq 'classcalc') { |
&loadcourse($sheet,$r); |
&loadcourse($sheet,$r,$c); |
} elsif ($stype eq 'studentcalc') { |
} elsif ($stype eq 'studentcalc') { |
&loadstudent($sheet); |
&loadstudent($sheet,$r,$c); |
} else { |
} else { |
&loadassessment($sheet); |
&loadassessment($sheet,$r,$c); |
} |
} |
} |
} |
|
|
Line 2589 sub loadrows {
|
Line 2649 sub loadrows {
|
|
|
sub checkthis { |
sub checkthis { |
my ($keyname,$time)=@_; |
my ($keyname,$time)=@_; |
return ($time<$expiredates{$keyname}); |
if (! exists($expiredates{$keyname})) { |
|
return 0; |
|
} else { |
|
return ($time<$expiredates{$keyname}); |
|
} |
} |
} |
|
|
sub forcedrecalc { |
sub forcedrecalc { |
Line 2622 sub forcedrecalc {
|
Line 2686 sub forcedrecalc {
|
# returns the export row for a spreadsheet. |
# returns the export row for a spreadsheet. |
# |
# |
sub exportsheet { |
sub exportsheet { |
my ($sheet,$uname,$udom,$stype,$usymb,$fn)=@_; |
my ($sheet,$uname,$udom,$stype,$usymb,$fn,$r)=@_; |
|
my $flag = 0; |
$uname = $uname || $sheet->{'uname'}; |
$uname = $uname || $sheet->{'uname'}; |
$udom = $udom || $sheet->{'udom'}; |
$udom = $udom || $sheet->{'udom'}; |
$stype = $stype || $sheet->{'sheettype'}; |
$stype = $stype || $sheet->{'sheettype'}; |
Line 2676 sub exportsheet {
|
Line 2741 sub exportsheet {
|
my ($newsheet)=&makenewsheet($uname,$udom,$stype,$usymb); |
my ($newsheet)=&makenewsheet($uname,$udom,$stype,$usymb); |
&readsheet($newsheet,$fn); |
&readsheet($newsheet,$fn); |
&updatesheet($newsheet); |
&updatesheet($newsheet); |
&loadrows($newsheet); |
&loadrows($newsheet,$r); |
&calcsheet($newsheet); |
&calcsheet($newsheet); |
@exportarr=&exportdata($newsheet); |
@exportarr=&exportdata($newsheet); |
## |
## |
Line 2691 sub exportsheet {
|
Line 2756 sub exportsheet {
|
[$key], |
[$key], |
$sheet->{'cdom'},$sheet->{'cnum'}); |
$sheet->{'cdom'},$sheet->{'cnum'}); |
if ($tmp[0]!~/^error/) { |
if ($tmp[0]!~/^error/) { |
%currentlystored = @tmp; |
# We only got one key, so we will access it directly. |
|
foreach (split('___&___',$tmp[1])) { |
|
my ($key,$value) = split('___=___',$_); |
|
$key = '' if (! defined($key)); |
|
$currentlystored{$key} = $value; |
|
} |
} |
} |
} else { |
} else { |
my @tmp = &Apache::lonnet::get('nohist_calculatedsheets_'. |
my @tmp = &Apache::lonnet::get('nohist_calculatedsheets_'. |
$sheet->{'cid'},[$key], |
$sheet->{'cid'},[$key], |
$sheet->{'udom'},$sheet->{'uname'}); |
$sheet->{'udom'},$sheet->{'uname'}); |
if ($tmp[0]!~/^error/) { |
if ($tmp[0]!~/^error/) { |
%currentlystored = @tmp; |
# We only got one key, so we will access it directly. |
|
foreach (split('___&___',$tmp[1])) { |
|
my ($key,$value) = split('___=___',$_); |
|
$key = '' if (! defined($key)); |
|
$currentlystored{$key} = $value; |
|
} |
} |
} |
} |
} |
# |
# |
Line 2717 sub exportsheet {
|
Line 2792 sub exportsheet {
|
# |
# |
# Store away the new value |
# Store away the new value |
# |
# |
|
my $timekey = $key.'.time'; |
if ($stype eq 'studentcalc') { |
if ($stype eq 'studentcalc') { |
&Apache::lonnet::put('nohist_calculatedsheets', |
my $result = &Apache::lonnet::put('nohist_calculatedsheets', |
{ $key => $newstore, |
{ $key => $newstore, |
$key.time => $now }, |
$timekey => $now }, |
$sheet->{'cdom'},$sheet->{'cnum'}); |
$sheet->{'cdom'}, |
} else { |
$sheet->{'cnum'}); |
&Apache::lonnet::put('nohist_calculatedsheets_'.$sheet->{'cid'}, |
} else { |
{ $key => $newstore, |
my $result = &Apache::lonnet::put('nohist_calculatedsheets_'.$sheet->{'cid'}, |
$key.time => $now }, |
{ $key => $newstore, |
$sheet->{'udom'}, |
$timekey => $now }, |
$sheet->{'uname'}) |
$sheet->{'udom'}, |
|
$sheet->{'uname'}); |
} |
} |
return @exportarr; |
return @exportarr; |
} |
} |
Line 2742 sub load_spreadsheet_expirationdates {
|
Line 2819 sub load_spreadsheet_expirationdates {
|
my @tmp = &Apache::lonnet::dump('nohist_expirationdates', |
my @tmp = &Apache::lonnet::dump('nohist_expirationdates', |
$ENV{'course.'.$cid.'.domain'}, |
$ENV{'course.'.$cid.'.domain'}, |
$ENV{'course.'.$cid.'.num'}); |
$ENV{'course.'.$cid.'.num'}); |
if (lc($tmp[0])!~/^error/){ |
if (lc($tmp[0]) !~ /^error/){ |
%expiredates = @tmp; |
%expiredates = @tmp; |
} |
} |
} |
} |
Line 3107 ENDSCRIPT
|
Line 3184 ENDSCRIPT
|
# Write the modified worksheet |
# Write the modified worksheet |
$r->print('<b>Current sheet:</b> '.$sheet->{'filename'}.'</p>'); |
$r->print('<b>Current sheet:</b> '.$sheet->{'filename'}.'</p>'); |
&tmpwrite($sheet); |
&tmpwrite($sheet); |
if ($sheet->{'sheettype'} eq 'studentcalc') { |
if ($sheet->{'sheettype'} eq 'assesscalc') { |
$r->print('<p>Show rows with empty A column: '); |
$r->print('<p>Show rows with empty A column: '); |
} else { |
} else { |
$r->print('<p>>Show empty rows: '); |
$r->print('<p>Show empty rows: '); |
} |
} |
# |
# |
$r->print(&hiddenfield('userselhidden','true'). |
$r->print(&hiddenfield('userselhidden','true'). |
Line 3163 ENDSCRIPT
|
Line 3240 ENDSCRIPT
|
#value='Insert Row Bottom'><br> |
#value='Insert Row Bottom'><br> |
#ENDINSERTBUTTONS |
#ENDINSERTBUTTONS |
# Print out sheet |
# Print out sheet |
&outsheet($r,$sheet); |
&outsheet($sheet,$r); |
$r->print('</form></body></html>'); |
$r->print('</form></body></html>'); |
# Done |
# Done |
return OK; |
return OK; |