--- loncom/homework/lonhomework.pm 2003/02/25 23:18:22 1.110 +++ loncom/homework/lonhomework.pm 2003/09/18 19:59:15 1.145 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # The LON-CAPA Homework handler # -# $Id: lonhomework.pm,v 1.110 2003/02/25 23:18:22 albertel Exp $ +# $Id: lonhomework.pm,v 1.145 2003/09/18 19:59:15 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -50,93 +50,98 @@ use Apache::essayresponse(); use Apache::externalresponse(); use Apache::rankresponse(); use Apache::matchresponse(); +use Apache::chemresponse(); use Apache::Constants qw(:common); use HTML::Entities(); use Apache::loncommon(); #use Time::HiRes qw( gettimeofday tv_interval ); BEGIN { - &Apache::lonxml::register_insert(); + &Apache::lonxml::register_insert(); } sub get_target { - if ( $ENV{'request.state'} eq "published") { - if ( defined($ENV{'form.grade_target'} ) - && ($ENV{'form.grade_target'} eq 'tex')) { - return ($ENV{'form.grade_target'}); - } elsif ( defined($ENV{'form.grade_target'} ) - && ($Apache::lonhomework::viewgrades == 'F' )) { - return ($ENV{'form.grade_target'}); - } - - if ( defined($ENV{'form.submitted'})) { - return ('grade', 'web'); - } else { - return ('web'); - } - } elsif ($ENV{'request.state'} eq "construct") { - if ( defined($ENV{'form.grade_target'}) ) { - return ($ENV{'form.grade_target'}); - } - if ( defined($ENV{'form.preview'})) { - if ( defined($ENV{'form.submitted'})) { - return ('grade', 'web'); - } else { - return ('web'); - } - } else { - if ( $ENV{'form.problemmode'} eq 'View' || - $ENV{'form.problemmode'} eq 'Discard Edits and View') { + if (($ENV{'request.state'} eq "published") || + ($ENV{'request.state'} eq "uploaded")) { + if ( defined($ENV{'form.grade_target'} ) + && ($ENV{'form.grade_target'} eq 'tex')) { + return ($ENV{'form.grade_target'}); + } elsif ( defined($ENV{'form.grade_target'} ) + && ($Apache::lonhomework::viewgrades eq 'F' )) { + return ($ENV{'form.grade_target'}); + } + if ( defined($ENV{'form.submitted'}) && - (!defined($ENV{'form.resetdata'})) ) { - return ('grade', 'web','answer'); + ( !defined($ENV{'form.resetdata'})) && + ( !defined($ENV{'form.newrandomization'}))) { + return ('grade', 'web'); + } else { + return ('web'); + } + } elsif ($ENV{'request.state'} eq "construct") { + if ( defined($ENV{'form.grade_target'}) ) { + return ($ENV{'form.grade_target'}); + } + if ( defined($ENV{'form.preview'})) { + if ( defined($ENV{'form.submitted'})) { + return ('grade', 'web'); + } else { + return ('web'); + } } else { - return ('web','answer'); + if ( $ENV{'form.problemmode'} eq 'View' || + $ENV{'form.problemmode'} eq 'Discard Edits and View') { + if ( defined($ENV{'form.submitted'}) && + (!defined($ENV{'form.resetdata'})) && + (!defined($ENV{'form.newrandomization'}))) { + return ('grade', 'web','answer'); + } else { + return ('web','answer'); + } + } elsif ( $ENV{'form.problemmode'} eq 'Edit' ) { + if ( $ENV{'form.submitted'} eq 'edit' ) { + if ( $ENV{'form.submit'} eq 'Submit Changes and View' ) { + return ('modified','web','answer'); + } else { + return ('modified','edit'); + } + } else { + return ('edit'); + } + } else { + return ('web'); + } } - } elsif ( $ENV{'form.problemmode'} eq 'Edit' ) { - if ( $ENV{'form.submitted'} eq 'edit' ) { - if ( $ENV{'form.submit'} eq 'Submit Changes and View' ) { - return ('modified','web','answer'); - } else { - return ('modified','edit'); - } - } else { - return ('edit'); - } - } else { - return ('web'); - } } - } - return (); + return (); } sub setup_vars { - my ($target) = @_; - return ';' + my ($target) = @_; + return ';' # return ';$external::target='.$target.';'; } sub send_header { - my ($request)= @_; - $request->print(&Apache::lontexconvert::header()); + my ($request)= @_; + $request->print(&Apache::lontexconvert::header()); # $request->print('
'); - $request->print(&Apache::lontexconvert::footer()); + $request->print(&Apache::lontexconvert::footer()); } $Apache::lonxml::browse=''; @@ -144,96 +149,104 @@ $Apache::lonxml::browse=''; # JB, 9/24/2002: Any changes in this function may require a change # in lonnavmaps::resource::getDateStatus. sub check_access { - my ($id) = @_; - my $date =''; - my $status; - my $datemsg = ''; - my $lastdate = ''; - my $temp; - my $type; - my $passed; - - if ($ENV{'request.state'} eq "construct") { - &Apache::lonxml::debug("in construction ignoring dates"); - $status='CAN_ANSWER'; - $datemsg='is in under construction'; - return ($status,$datemsg); - } + my ($id) = @_; + my $date =''; + my $status; + my $datemsg = ''; + my $lastdate = ''; + my $temp; + my $type; + my $passed; + + if ($ENV{'request.state'} eq "construct") { + &Apache::lonxml::debug("in construction ignoring dates"); + $status='CAN_ANSWER'; + $datemsg='is in under construction'; + return ($status,$datemsg); + } + + &Apache::lonxml::debug("checking for part :$id:"); + &Apache::lonxml::debug("time:".time); + foreach $temp ("opendate","duedate","answerdate") { + $lastdate = $date; + $date = &Apache::lonnet::EXT("resource.$id.$temp"); + my $thistype = &Apache::lonnet::EXT("resource.$id.$temp.type"); + if ($thistype =~ /^(con_lost|no_such_host)/ || + $date =~ /^(con_lost|no_such_host)/) { + $status='UNAVAILABLE'; + $date="may open later."; + return($status,$date); + } + if ($thistype eq 'date_interval') { + if ($temp eq 'opendate') { + $date=&Apache::lonnet::EXT("resource.$id.duedate")-$date; + } + if ($temp eq 'answerdate') { + $date=&Apache::lonnet::EXT("resource.$id.duedate")+$date; + } + } + &Apache::lonxml::debug("found :$date: for :$temp:"); + if ($date eq '') { + $date = "an unknown date"; $passed = 0; + } elsif ($date eq 'con_lost') { + $date = "an indeterminate date"; $passed = 0; + } else { + if (time < $date) { $passed = 0; } else { $passed = 1; } + $date = localtime $date; + } + if (!$passed) { $type=$temp; last; } + } + &Apache::lonxml::debug("have :$type:$passed:"); + if ($passed) { + $status='SHOW_ANSWER'; + $datemsg=$date; + } elsif ($type eq 'opendate') { + $status='CLOSED'; + $datemsg = "will open on $date"; + } elsif ($type eq 'duedate') { + $status='CAN_ANSWER'; + $datemsg = "is due at $date"; + } elsif ($type eq 'answerdate') { + $status='CLOSED'; + $datemsg = "was due on $lastdate, and answers will be available on $date"; + } + if ($status eq 'CAN_ANSWER') { + #check #tries, and if correct. + my $tries = $Apache::lonhomework::history{"resource.$id.tries"}; + my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries"); + if ( $tries eq '' ) { $tries = '0'; } + if ( $maxtries eq '' ) { $maxtries = '2'; } + if ($tries >= $maxtries) { $status = 'CANNOT_ANSWER'; } + # if (correct and show prob status) or excused then CANNOT_ANSWER + if(($Apache::lonhomework::history{"resource.$id.solved"}=~/^correct/ + && + lc($Apache::lonhomework::problemstatus) ne 'no') + || + $Apache::lonhomework::history{"resource.$id.solved"}=~/^excused/) { + $status = 'CANNOT_ANSWER'; + } + } - &Apache::lonxml::debug("checking for part :$id:"); - &Apache::lonxml::debug("time:".time); - foreach $temp ("opendate","duedate","answerdate") { - $lastdate = $date; - $date = &Apache::lonnet::EXT("resource.$id.$temp"); - my $thistype = &Apache::lonnet::EXT("resource.$id.$temp.type"); - if ($thistype =~ /^(con_lost|no_such_host)/ || - $date =~ /^(con_lost|no_such_host)/) { - $status='UNAVAILABLE'; - $date="may open later."; - return($status,$date); - } - if ($thistype eq 'date_interval') { - if ($temp eq 'opendate') { - $date=&Apache::lonnet::EXT("resource.$id.duedate")-$date; - } - if ($temp eq 'answerdate') { - $date=&Apache::lonnet::EXT("resource.$id.duedate")+$date; - } - } - &Apache::lonxml::debug("found :$date: for :$temp:"); - if ($date eq '') { - $date = "an unknown date"; $passed = 0; - } elsif ($date eq 'con_lost') { - $date = "an indeterminate date"; $passed = 0; - } else { - if (time < $date) { $passed = 0; } else { $passed = 1; } - $date = localtime $date; + #if (($status ne 'CLOSED') && ($Apache::lonhomework::type eq 'exam') && + # (!$Apache::lonhomework::history{"resource.0.outtoken"})) { + # return ('UNCHECKEDOUT','needs to be checked out'); + #} + + + &Apache::lonxml::debug("sending back :$status:$datemsg:"); + if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) { + &Apache::lonxml::debug("should be allowed to browse a resource when closed"); + $status='CAN_ANSWER'; + $datemsg='is closed but you are allowed to view it'; } - if (!$passed) { $type=$temp; last; } - } - &Apache::lonxml::debug("have :$type:$passed:"); - if ($passed) { - $status='SHOW_ANSWER'; - $datemsg=$date; - } elsif ($type eq 'opendate') { - $status='CLOSED'; - $datemsg = "will open on $date"; - } elsif ($type eq 'duedate') { - $status='CAN_ANSWER'; - $datemsg = "is due at $date"; - } elsif ($type eq 'answerdate') { - $status='CLOSED'; - $datemsg = "was due on $lastdate, and answers will be available on $date"; - } - if ($status eq 'CAN_ANSWER') { - #check #tries - my $tries = $Apache::lonhomework::history{"resource.$id.tries"}; - my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries"); - if ( $tries eq '' ) { $tries = '0'; } - if ( $maxtries eq '' ) { $maxtries = '2'; } - if ($tries >= $maxtries) { $status = 'CANNOT_ANSWER'; } - } - - if (($status ne 'CLOSED') && ($Apache::lonhomework::type eq 'exam') && - (!$Apache::lonhomework::history{"resource.0.outtoken"})) { - return ('UNCHECKEDOUT','needs to be checked out'); - } - - - &Apache::lonxml::debug("sending back :$status:$datemsg:"); - if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) { - &Apache::lonxml::debug("should be allowed to browse a resource when closed"); - $status='CAN_ANSWER'; - $datemsg='is closed but you are allowed to view it'; - } - return ($status,$datemsg); + return ($status,$datemsg); } sub showhash { - my (%hash) = @_; - &showhashsubset(\%hash,'.'); - return ''; + my (%hash) = @_; + &showhashsubset(\%hash,'.'); + return ''; } sub showarray { @@ -254,87 +267,95 @@ sub showarray { } sub showhashsubset { - my ($hash,$keyre) = @_; - my $resultkey; - foreach $resultkey (sort keys %$hash) { - if ($resultkey =~ /$keyre/) { - if (ref($$hash{$resultkey})) { - if ($$hash{$resultkey} =~ /ARRAY/ ) { - &Apache::lonxml::debug("$resultkey ---- ". - &showarray($$hash{$resultkey})); - } elsif ($$hash{$resultkey} =~ /HASH/ ) { - &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}"); - &showhashsubset($$hash{$resultkey},'.'); - } else { - &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}"); - } - } else { - &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}"); - } - } - } - &Apache::lonxml::debug("\nUndid changes, Switched $filebak and $file
"); - } else { - $request->print("Unable to undo, unable to switch $filebak and $file
"); - $error=1; - } - } else { - my $fs=Apache::File->new(">$filebak"); - if (defined($fs)) { - print $fs $$problem; - $request->print("Making Backup to $filebakUndid changes, Switched $filebak and $file
"); + } else { + $request->print("Unable to undo, unable to switch $filebak and $file
"); + $error=1; + } } else { - $request->print("Unable to write to $file"); - $error|=4; + my $fs=Apache::File->new(">$filebak"); + if (defined($fs)) { + print $fs $$problem; + $request->print("Making Backup to $filebak