--- loncom/interface/spreadsheet/studentcalc.pm 2003/05/19 15:48:18 1.3 +++ loncom/interface/spreadsheet/studentcalc.pm 2005/08/26 20:46:46 1.35 @@ -1,5 +1,5 @@ # -# $Id: studentcalc.pm,v 1.3 2003/05/19 15:48:18 matthew Exp $ +# $Id: studentcalc.pm,v 1.35 2005/08/26 20:46:46 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -45,6 +45,9 @@ studentcalc ################################################### package Apache::studentcalc; +use warnings FATAL=>'all'; +no warnings 'uninitialized'; + use strict; use Apache::Constants qw(:common :http); use Apache::lonnet; @@ -54,101 +57,162 @@ use Apache::lonnavmaps; use Apache::Spreadsheet(); use Apache::assesscalc(); use HTML::Entities(); -use Spreadsheet::WriteExcel; use Time::HiRes; +use Apache::lonlocal; @Apache::studentcalc::ISA = ('Apache::Spreadsheet'); my @Sequences = (); +my $navmap; my %Exportrows = (); my $current_course; +sub initialize { + &initialize_sequence_cache(); + &Apache::assesscalc::initialize($navmap); +} + sub initialize_package { - $current_course = $ENV{'request.course.id'}; + $current_course = $env{'request.course.id'}; &initialize_sequence_cache(); &load_cached_export_rows(); } +sub ensure_correct_sequence_data { + if ($current_course ne $env{'request.course.id'}) { + &initialize_sequence_cache(); + $current_course = $env{'request.course.id'}; + } + return; +} + sub initialize_sequence_cache { # # Set up the sequences and assessments - @Sequences = (); - my ($top,$sequences,$assessments) = - &Apache::loncoursedata::get_sequence_assessment_data(); - if (! defined($top) || ! ref($top)) { - # There has been an error, better report it - &Apache::lonnet::logthis('top is undefined (studentcalc.pm)'); - return; + undef(@Sequences); + undef($navmap); + $navmap = Apache::lonnavmaps::navmap->new(); + if (!defined($navmap)) { + &Apache::lonnet::logthis('student spreadsheet:Can not open Coursemap'); + } + my @all_sequences = $navmap->retrieveResources(undef, + sub { shift->is_map(); },1,0,1); + for my $sequence ($navmap->getById('0.0'), @all_sequences) { + if ($navmap->hasResource($sequence,sub { shift->is_problem(); }, 0)){ + push(@Sequences,$sequence); + &get_resources($sequence); + } } - @Sequences = @{$sequences} if (ref($sequences) eq 'ARRAY'); +} + +my %res_memoize; +sub get_resources { + my ($seq) = @_; + if (exists($res_memoize{$seq->symb()})) { + return @{$res_memoize{$seq->symb()}}; + } + return () if (! defined($navmap) || ! ref($navmap)); + my @resources = $navmap->retrieveResources($seq, + sub { shift->is_problem(); }, + 0,0,0); + $res_memoize{$seq->symb()}=\@resources; + return @resources; } sub clear_package { - @Sequences = undef; - %Exportrows = undef; + undef(@Sequences); + undef(%Exportrows); + undef(%res_memoize); + undef($navmap); + &Apache::assesscalc::clear_package(); } sub get_title { my $self = shift; - my $title = ''; - my %userenv = &Apache::loncoursedata::GetUserName($self->{'name'}, - $self->{'domain'}); - &Apache::lonnet::logthis('userenv = '.join(' ',%userenv)); - my $name = - join(' ',@userenv{'firstname','middlename','lastname','generation'}); - $name =~ s/\s+$//; - $title .= '

'.$name; - if ($ENV{'user.name'} ne $self->{'name'} && - $ENV{'user.domain'} ne $self->{'domain'}) { - $title .= &Apache::loncommon::aboutmewrapper + my @title = (); + # + # Determine the students name + my $name = &Apache::loncommon::plainname($self->{'name'}, + $self->{'domain'}); + push (@title,$name); + push (@title,$self->{'coursedesc'}); + push (@title,&Apache::lonlocal::locallocaltime(time)); + return @title; +} + +sub get_html_title { + my $self = shift; + my ($name,$desc,$time) = $self->get_title(); + my $title = '

'.$name; + if ($env{'user.name'} ne $self->{'name'} && + $env{'user.domain'} ne $self->{'domain'}) { + $title .= ' '.&Apache::loncommon::aboutmewrapper ($self->{'name'}.'@'.$self->{'domain'}, $self->{'name'},$self->{'domain'}); } $title .= "

\n"; - $title .= '

'.$self->{'coursedesc'}."

\n"; - $title .= '

'.localtime(time).'

'; + $title .= '

'.$desc."

\n"; + $title .= '

'.$time.'

'; return $title; } sub parent_link { my $self = shift; - my $link .= '

'. - 'Course level sheet

'."\n"; - return $link; + return '

'.&mt('Course level sheet').'

'."\n"; +} + +sub convenience_links { + my $self = shift; + my ($resource) = @_; + my $result=&Apache::loncommon::submlink('',$self->{'name'},$self->{'domain'},$resource->symb,'LONcatInfo'); + $result .= &Apache::loncommon::pgrdlink('',$self->{'name'},$self->{'domain'},$resource->symb,'LONcatInfo'); + $result .= &Apache::loncommon::pprmlink('',$self->{'name'},$self->{'domain'},$resource->symb,'LONcatInfo'); + return $result; } sub outsheet_html { my $self = shift; my ($r) = @_; + my $importcolor = '#FFFFAA'; + my $exportcolor = '#88FF88'; #################################### # Get the list of assessment files # #################################### my @AssessFileNames = $self->othersheets('assesscalc'); my $editing_is_allowed = &Apache::lonnet::allowed('mgr', - $ENV{'request.course.id'}); + $env{'request.course.id'}); + #################################### + # Report any calculation errors # + #################################### + $r->print($self->html_report_error()); #################################### # Determine table structure # #################################### my $num_uneditable = 26; my $num_left = 52-$num_uneditable; + my %lt=&Apache::lonlocal::texthash( + 'st' => 'Student', + 'im' => 'Import', + 'ca' => 'Calculations', + 'as' => 'Assessment', + 'ro' => 'Row', + ); my $tableheader =<<"END";

- - + + + $lt{'ca'} END my $label_num = 0; foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){ if ($label_num<$num_uneditable) { - $tableheader .='\n"; - # - $r->print($tableheader); - # - # Print out template row - $r->print(''. - $self->html_template_row($num_uneditable)."\n"); - # - # Print out summary/export row - $r->print(''. - $self->html_export_row()."\n"); + if ($self->blackout()) { + $r->print('

'. + &mt('Some computations are not available at this time.').'
'. + &mt('There are problems whose status you are not allowed to view.'). + '

'."\n"); + } else { + $r->print($tableheader); + # + # Print out template row + if (exists($env{'request.role.adv'}) && $env{'request.role.adv'}) { + $r->print(''. + $self->html_template_row($num_uneditable, + $importcolor)."\n"); + } + # + # Print out summary/export row + $r->print(''. + $self->html_export_row($exportcolor)."\n"); + } $r->print("
Student - Import$lt{'st'} + $lt{'im'} - Calculations
'; + $tableheader .=''; } else { $tableheader .=''; } @@ -156,24 +220,41 @@ END $label_num++; } $tableheader .="
Template 
Export0
Template 
'.&mt('Summary').'0
\n"); # # Prepare to output rows - $tableheader =<<"END"; + if (exists($env{'request.role.adv'}) && $env{'request.role.adv'}) { + $tableheader =<<"END";

- + END + } else { + $tableheader =<<"END"; +

+

RowAssessment
$lt{'ro'} $lt{'as'}
+ +END + } foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){ if ($label_num<$num_uneditable) { $tableheader.=''; if ($editing_is_allowed) { $row_output .= ''; + $row_output .= ''; $row_output .= ''; } else { - $row_output .= ''; + } + if ($self->blackout() && $self->{'blackout_rows'}->{$rownum}>0) { + $row_output .= + ''."\n"; + } else { + $row_output .= $self->html_row($num_uneditable,$rownum, + $exportcolor,$importcolor). + "\n"; } - $row_output .= $self->html_row($num_uneditable,$rownum). - "\n"; $r->print($row_output); } $r->print("
 $lt{'as'}'; @@ -189,33 +270,39 @@ END &initialize_sequence_cache(); } foreach my $Sequence (@Sequences) { - next if ($Sequence->{'num_assess'} < 1); - $r->print("

".$Sequence->{'title'}."

\n"); + $r->print("

".$Sequence->compTitle."

\n"); $r->print($tableheader); - foreach my $resource (@{$Sequence->{'contents'}}) { - next if ($resource->{'type'} ne 'assessment'); - my $rownum = $self->get_row_number_from_key($resource->{'symb'}); + foreach my $resource (&get_resources($Sequence)) { + my $rownum = $self->get_row_number_from_key($resource->symb); my $assess_filename = $self->{'row_source'}->{$rownum}; my $row_output = '
'.$rownum.''.$self->convenience_links($resource).''. ''. - $resource->{'title'}.'
'; + '&usymb='.&Apache::lonnet::escape($resource->symb). + '">'.$resource->compTitle.'
'; $row_output .= &assess_file_selector($rownum, $assess_filename, \@AssessFileNames). '
src.'?symb='. + &Apache::lonnet::escape($resource->symb). + '">Go To'; + $row_output .= ''.$resource->compTitle.''.&mt('Unavailable at this time').'
\n"); @@ -240,7 +327,7 @@ sub assess_file_selector { if (!defined($AssessFiles) || ! @$AssessFiles) { return ''; } - return '' if (! &Apache::lonnet::allowed('mgr',$ENV{'request.course.id'})); + return '' if (! &Apache::lonnet::allowed('mgr',$env{'request.course.id'})); my $element_name = 'FileSelect_'.$row; my $load_dialog = '