--- loncom/homework/lonhomework.pm 2001/06/12 22:42:55 1.46
+++ loncom/homework/lonhomework.pm 2001/08/20 23:31:07 1.57
@@ -1,7 +1,7 @@
# The LON-CAPA Homework handler
# Guy Albertelli
# 11/30 Gerd Kortemeyer
-# 6/1 Gerd Kortemeyer
+# 6/1,8/17,8/18 Gerd Kortemeyer
package Apache::lonhomework;
use strict;
@@ -14,7 +14,7 @@ use Apache::response;
use Apache::hint;
use Apache::outputtags;
use Apache::Constants qw(:common);
-
+#use Time::HiRes qw( gettimeofday tv_interval );
sub BEGIN {
&Apache::lonxml::register_insert();
@@ -84,7 +84,8 @@ sub send_footer {
}
$Apache::lonxml::browse='';
-sub check_date {
+
+sub check_access {
my ($id) = @_;
my $date ='';
my $status = '';
@@ -130,6 +131,13 @@ sub check_date {
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");
@@ -144,6 +152,28 @@ sub check_date {
return ($status,$datemsg);
}
+# ----------------------------------------------------------------- whichuser
+# returns a list of $symb, $courseid, $domain, $name that is correct for
+# calls to lonnet functions for this setup.
+# - looks for form.grade_ parameters
+sub whichuser {
+ my $symb=&Apache::lonnet::symbread();
+ my $courseid=$ENV{'request.course.id'};
+ my $domain=$ENV{'user.domain'};
+ my $name=$ENV{'user.name'};
+ if (defined($ENV{'form.grade_symb'})) {
+ my $tmp_courseid=$ENV{'form.grade_courseid'};
+ my $allowed=&Apache::lonnet::allowed('mgr',$tmp_courseid);
+ if ($allowed) {
+ $symb=$ENV{'form.grade_symb'};
+ $courseid=$ENV{'form.grade_courseid'};
+ $domain=$ENV{'form.grade_domain'};
+ $name=$ENV{'form.grade_username'};
+ }
+ }
+ return ($symb,$courseid,$domain,$name);
+}
+
sub showhash {
my (%hash) = @_;
my $resultkey;
@@ -172,11 +202,41 @@ sub setupheader {
return ''
}
+sub handle_save_or_undo {
+ my ($request,$problem,$result) = @_;
+ my $fileout = &Apache::lonnet::filelocation("",$request->uri);
+ my $filesave=$fileout.".bak";
+
+ if ($ENV{'form.Undo'} eq 'undo') {
+ if (copy($filesave,$fileout)) {
+ $request->print("Undid changes, Copied $filesave to $fileout
");
+ } else {
+ $request->print("Unable to undo, unable to copy $filesave to $fileout
");
+ }
+ } else {
+ my $fs=Apache::File->new(">$filesave");
+ if (defined($fs)) {
+ print $fs $$problem;
+ $request->print("Making Backup to $filesave
");
+ } else {
+ $request->print("Unable to make backup $filesave");
+ }
+ my $fh=Apache::File->new(">$fileout");
+ if (defined($fh)) {
+ print $fh $$result;
+ $request->print("Saving Modifications to $fileout
");
+ } else {
+ $request->print("Unable to write to $fileout");
+ }
+ }
+}
+
sub renderpage {
my ($request,$file) = @_;
my (@targets) = &get_target();
foreach my $target (@targets) {
+ #my $t0 = [&gettimeofday()];
my $problem=&Apache::lonnet::getfile($file);
if ($problem == -1) {
&Apache::lonxml::error(" Unable to find $file");
@@ -187,11 +247,6 @@ sub renderpage {
my $result = '';
&Apache::inputtags::initialize_inputtags;
&Apache::edit::initialize_edit;
- %Apache::lonhomework::results=();
- %Apache::lonhomework::history=&Apache::lonnet::restore();
- #ignore error conditions
- my ($temp)=keys %Apache::lonhomework::history ;
- if ($temp =~ m/^error:.*/) { %Apache::lonhomework::history=(); }
if ($target eq 'web') {
if (&Apache::lonnet::symbread() eq '') {
if ($ENV{'request.state'} eq "construct") {
@@ -214,37 +269,27 @@ sub renderpage {
#$request->print("Result follows:");
if ($target eq 'modified') {
- my $fileout = &Apache::lonnet::filelocation("",$request->uri);
- my $filesave=$fileout.".bak";
-
- my $fs=Apache::File->new(">$filesave");
- print $fs $problem;
- $request->print("Making Backup to $filesave
");
-
- my $fh=Apache::File->new(">$fileout");
- print $fh $result;
- $request->print("Saving Modifications to $fileout
");
+ &handle_save_or_undo($request,\$problem,\$result);
} else {
+ #my $td=&tv_interval($t0);
+ #if ( $Apache::lonxml::debug) {
+ #$result =~ s: