--- loncom/homework/lonhomework.pm 2004/05/25 21:25:53 1.175 +++ loncom/homework/lonhomework.pm 2004/12/20 12:16:47 1.188 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # The LON-CAPA Homework handler # -# $Id: lonhomework.pm,v 1.175 2004/05/25 21:25:53 albertel Exp $ +# $Id: lonhomework.pm,v 1.188 2004/12/20 12:16:47 foxr Exp $ # # Copyright Michigan State University Board of Trustees # @@ -52,13 +52,77 @@ use Apache::Constants qw(:common); use HTML::Entities(); use Apache::loncommon(); use Apache::lonlocal; -#use Time::HiRes qw( gettimeofday tv_interval ); +use Time::HiRes qw( gettimeofday tv_interval ); +use Apache::lonnet(); +# +# FIXME!!!!!!! +# Be nice if someone +# - Took credit/blame for this module. +# - At least commented the purpose of each major +# sub +# - Even better commented the major blocks. +# +# I'll have a go at it for what I need. +# ?'s- mean maybe this is right. +# The number of ?'s represents the rough level +# of doubt I have about rightness, with ??? representing +# uncertainty based on the first blush interpretation. +# ?? - I have some code to back up this interpretation. +# ? - I"m pretty sure I understand the code that backs up this +# interpretation. +# ???? - Absolutely clueless. +# . - I'm pretty sure the interpretation is correct. +# RF. + + +# 'Exported' Variables: + +# viewgrades is 'F' if users are not allowed to view the grades.. T otherwise. + +$Apache::lonhomework::viewgrades = 'F'; # By default cannot. + +# Internal variables. + +my $DEBUG=0; # set nonzero to enable debugging. BEGIN { &Apache::lonxml::register_insert(); } +# +# Issue debug log messages. +# Parameters: +# The log message to issue. +# Implicit Inputs: +# DEBUG - If non zero the message is issued, otherwise not. +# +sub Debug { + my ($message) = @_; + + if($DEBUG) { + my @call_context = caller(1); + my $prefix = "$call_context[3]:$call_context[2]:: "; + &Apache::lonnet::logthis($prefix.$message); + } +} + +# +# I believe this sub defines how to render the +# problem based on the context of the rendering? +# Implicit inputs: +# Various session environment variables: +# request.state - State of the resource actually ?? +# published - Examining in resource space. +# uploaded - Not sure how this happens????? +# contruct - Rendering in construction space for viewing. +# form.grade_target - sub get_target { + &Debug("request.state = $ENV{'request.state'}"); + if( defined($ENV{'form.grade_target'})) { + &Debug("form.grade_target = $ENV{'form.grade_target'}"); + } else { + &Debug("form.grade_target "); + } if (($ENV{'request.state'} eq "published") || ($ENV{'request.state'} eq "uploaded")) { if ( defined($ENV{'form.grade_target'} ) @@ -292,7 +356,26 @@ sub check_access { $status = 'CANNOT_ANSWER'; } } - + if ($status eq 'CAN_ANSWER' || $status eq 'CANNOT_ANSWER') { + my $interval=&Apache::lonnet::EXT("resource.$id.interval"); + &Apache::lonxml::debug("looking for interval $interval"); + if ($interval) { + my $first_access=&Apache::lonnet::get_first_access('map'); + &Apache::lonxml::debug("looking for accesstime $first_access"); + if (!$first_access) { + $status='NOT_YET_VIEWED'; + $datemsg=' '.$interval.' seconds'; + } else { + my $newdate=localtime($first_access+$interval); + if (time > ($first_access+$interval)) { + $status='CLOSED'; + $datemsg = &mt("was due on")." $newdate".&mt(", and answers will be available on")." $date"; + } else { + $datemsg = &mt("is due at")." $newdate"; + } + } + } + } #if (($status ne 'CLOSED') && ($Apache::lonhomework::type eq 'exam') && # (!$Apache::lonhomework::history{"resource.0.outtoken"})) { # return ('UNCHECKEDOUT','needs to be checked out'); @@ -365,7 +448,13 @@ sub setuppermissions { $viewgrades = &Apache::lonnet::allowed('vgr',$ENV{'request.course.id'}. '/'.$ENV{'request.course.sec'}); } - $Apache::lonhomework::viewgrades = $viewgrades; + $Apache::lonhomework::viewgrades = $viewgrades; # File global variable...dirt. + if ($Apache::lonhomework::browse eq 'F' && + $ENV{'form.devalidatecourseresdata'} eq 'on') { + my (undef,$courseid) = &Apache::lonxml::whichuser(); + &Apache::lonnet::devalidatecourseresdata($ENV{"course.$courseid.num"}, + $ENV{"course.$courseid.domain"}); + } return '' } @@ -430,11 +519,16 @@ sub handle_save_or_undo { sub analyze_header { my ($request) = @_; + my $bodytag=''; + if ($ENV{'environment.remote'} eq 'off') { + $bodytag=&Apache::loncommon::bodytag(); + } my $result.=' '.&mt("Analyzing a problem").' - '.&Apache::lonxml::message_location().' + '.$bodytag.&Apache::lonxml::message_location().'
+ &HTML::Entities::encode($ENV{'request.uri'},'<>&"').'">'. + &Apache::structuretags::remember_problem_state().'
@@ -469,6 +563,7 @@ sub analyze { for(my $i=1;$i<$ENV{'form.numtoanalyze'}+1;$i++) { &Apache::lonhtmlcommon::Increment_PrgWin($request,\%prog_state, &mt('last problem')); + if (&Apache::loncommon::connection_aborted($request)) { return; } my $subresult=&Apache::lonnet::ssi($request->uri, ('grade_target' => 'analyze'), ('rndseed' => $i+$rndseed)); @@ -494,12 +589,12 @@ sub analyze { foreach my $part (sort(keys(%allparts))) { if (defined(@{ $overall{$part.'.answer'} })) { my $num_cols=scalar(@{ $overall{$part.'.answer'}->[0] }); - $request->print(''); + $request->print('
'.&mt('Part').' '.$part.'
'); my %frequency; foreach my $answer (sort {$a->[0] <=> $b->[0]} (@{ $overall{$part.'.answer'} })) { $frequency{join("\0",@{ $answer })}++; } - $request->print(''); + $request->print(''); foreach my $answer (sort {(split("\0",$a))[0] <=> (split("\0",$b))[0]} (keys(%frequency))) { $request->print('
'.&mt('Part').' '.$part.'
'.&mt('Answer').''.&mt('Frequency').'
'.&mt('Answer').''.&mt('Frequency').'
'. join('',split("\0",$answer)). @@ -546,15 +641,19 @@ sub editxmlmode { &Apache::loncommon::helpLatexCheatsheet("Problem_Editor_XML_Index", "Problem Editing Help"). ''. - &Apache::loncommon::help_open_faq(5). - &Apache::loncommon::help_open_bug('Authoring').'
'; + &Apache::loncommon::help_open_menu('',undef,undef,undef,5,'Authoring'). + ''; if ($cols > 80) { $cols = 80; } if ($cols < 70) { $cols = 70; } if ($rows < 20) { $rows = 20; } - $result.=''. - &Apache::lonxml::message_location().' + my $bodytag=''; + if ($ENV{'environment.remote'} eq 'off') { + $bodytag=&Apache::loncommon::bodytag(); + } + $result.=''.$bodytag.&Apache::lonxml::message_location().' + &HTML::Entities::encode($ENV{'request.uri'},'<>&"').'">'. + &Apache::structuretags::remember_problem_state().' @@ -572,6 +671,9 @@ sub editxmlmode { } return ''; } +# +# Render the page in whatever target desired. +# sub renderpage { my ($request,$file) = @_; @@ -581,6 +683,13 @@ sub renderpage { &Apache::lonxml::debug("Running targets ".join(':',@targets)); my $overall_result; foreach my $target (@targets) { + # FIXME need to do something intelligent when a problem goes + # from viewable to not viewable due to map conditions + #&setuppermissions(); + #if ( $Apache::lonhomework::browse ne '2' + # && $Apache::lonhomework::browse ne 'F' ) { + # $request->print(" You most likely shouldn't see me."); + #} #my $t0 = [&gettimeofday()]; my $problem=&Apache::lonnet::getfile($file); if ($problem eq -1) { @@ -669,8 +778,8 @@ sub newproblem { copy($templatefilename,$dest); &renderpage($request,$dest); } else { - my $url=$request->uri; - my $shownurl=$url; + my $url=&HTML::Entities::encode($request->uri,'<>&"'); + my $shownurl=$url; $shownurl=~s-^/~-/priv/-; my $dest = &Apache::lonnet::filelocation("",$request->uri); my $errormsg; @@ -678,7 +787,8 @@ sub newproblem { $errormsg='

'.&mt('You did not select a template.').'

'."\n"; } my $instructions; - my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1); + my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef, + ($ENV{'environment.remote'} ne 'off')); if ($templatelist) { $instructions=&mt(", select a template from the pull-down menu below.").'
'.&mt("Then");} my %lt=&Apache::lonlocal::texthash( 'create' => 'Creating a new', 'resource' => 'resource', @@ -709,7 +819,7 @@ ENDNEWPROBLEM sub view_or_edit_menu { my ($request) = @_; - my $url=$request->uri; + my $url=&HTML::Entities::encode($request->uri,'<>&"'); my %lt=&Apache::lonlocal::texthash( 'would' => 'Would you like to', 'view' => 'View', 'Edit' => 'edit', @@ -731,12 +841,8 @@ sub handler { my $request=$_[0]; $Apache::lonxml::debug=$ENV{'user.debug'}; - if (&setupheader($request)) { return OK; } $ENV{'request.uri'}=$request->uri; - - #setup permissions - $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'}); - $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'}); + &setuppermissions(); &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:"); # some times multiple problemmodes are submitted, need to select # the last one @@ -763,6 +869,7 @@ sub handler { $request->internal_redirect('/adm/ambiguous'); return; } } + if (&setupheader($request)) { return OK; } my ($symb) = &Apache::lonxml::whichuser(); &Apache::lonxml::debug('symb is '.$symb);