--- loncom/homework/lonhomework.pm 2001/11/29 18:36:59 1.63
+++ loncom/homework/lonhomework.pm 2002/02/28 01:18:47 1.72
@@ -1,7 +1,7 @@
# The LearningOnline Network with CAPA
# The LON-CAPA Homework handler
#
-# $Id: lonhomework.pm,v 1.63 2001/11/29 18:36:59 albertel Exp $
+# $Id: lonhomework.pm,v 1.72 2002/02/28 01:18:47 albertel Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -34,6 +34,7 @@ use strict;
use Apache::style;
use Apache::lonxml;
use Apache::lonnet;
+use Apache::lonplot;
use Apache::inputtags;
use Apache::structuretags;
use Apache::randomlabel;
@@ -43,12 +44,16 @@ use Apache::outputtags;
use Apache::Constants qw(:common);
#use Time::HiRes qw( gettimeofday tv_interval );
-sub BEGIN {
+BEGIN {
&Apache::lonxml::register_insert();
}
sub get_target {
if ( $ENV{'request.state'} eq "published") {
+ if ( defined($ENV{'form.grade_target'})
+ && ($Apache::lonhomework::viewgrades == 'F' )) {
+ return ($ENV{'form.grade_target'});
+ }
if ( defined($ENV{'form.submitted'})) {
return ('grade', 'web');
} else {
@@ -203,7 +208,9 @@ sub setupheader {
} else {
$request->content_type('text/html');
}
- &Apache::loncommon::no_cache($request);
+ if (!$Apache::lonxml::debug && ($ENV{'REQUEST_METHOD'} eq 'GET')) {
+ &Apache::loncommon::no_cache($request);
+ }
$request->send_http_header;
return OK if $request->header_only;
return ''
@@ -211,37 +218,80 @@ sub setupheader {
sub handle_save_or_undo {
my ($request,$problem,$result) = @_;
- my $fileout = &Apache::lonnet::filelocation("",$request->uri);
- my $filesave=$fileout.".bak";
+ my $file = &Apache::lonnet::filelocation("",$request->uri);
+ my $filebak =$file.".bak";
+ my $filetmp =$file.".tmp";
+ my $error=0;
if ($ENV{'form.Undo'} eq 'undo') {
- if (copy($filesave,$fileout)) {
- $request->print("Undid changes, Copied $filesave to $fileout
");
+ my $error=0;
+ if (!copy($file,$filetmp)) { $error=1; }
+ if ((!$error) && (!copy($filebak,$file))) { $error=1; }
+ if ((!$error) && (!move($filetmp,$filebak))) { $error=1; }
+ if (!$error) {
+ $request->print("
Undid changes, Switched $filebak and $file
");
} else {
- $request->print("Unable to undo, unable to copy $filesave to $fileout
");
+ $request->print("Unable to undo, unable to switch $filebak and $file
");
+ $error=1;
}
} else {
- my $fs=Apache::File->new(">$filesave");
+ my $fs=Apache::File->new(">$filebak");
if (defined($fs)) {
print $fs $$problem;
- $request->print("Making Backup to $filesave
");
+ $request->print("Making Backup to $filebak
");
} else {
- $request->print("Unable to make backup $filesave");
+ $request->print("Unable to make backup $filebak");
+ $error=2;
}
- my $fh=Apache::File->new(">$fileout");
+ my $fh=Apache::File->new(">$file");
if (defined($fh)) {
print $fh $$result;
- $request->print("Saving Modifications to $fileout
");
+ $request->print("Saving Modifications to $file
");
} else {
- $request->print("Unable to write to $fileout");
+ $request->print("Unable to write to $file");
+ $error|=4;
}
}
+ return $error;
+}
+
+sub editxmlmode {
+ my ($request,$file) = @_;
+ my $result;
+ my $problem=&Apache::lonnet::getfile($file);
+ if ($problem == -1) {
+ &Apache::lonxml::error(" Unable to find $file");
+ $problem='';
+ }
+ if (defined($ENV{'form.editxmltext'}) || defined($ENV{'form.Undo'})) {
+ my $error=&handle_save_or_undo($request,\$problem,
+ \$ENV{'form.editxmltext'});
+ if (!$error) { $problem=&Apache::lonnet::getfile($file); }
+ }
+ my ($rows,$cols) = &Apache::edit::textarea_sizes(\$problem);
+ if ($cols > 80) { $cols = 80; }
+ $result.='
+ ';
+ $request->print($result);
+ return '';
}
sub renderpage {
my ($request,$file) = @_;
my (@targets) = &get_target();
+ &Apache::lonxml::debug("Running targets ".join(':',@targets));
foreach my $target (@targets) {
#my $t0 = [&gettimeofday()];
my $problem=&Apache::lonnet::getfile($file);
@@ -263,13 +313,14 @@ sub renderpage {
}
#if ($Apache::lonhomework::viewgrades eq 'F') {&createmenu('grade',$request); }
}
- if ($target eq 'grade') { &showhash(%Apache::lonhomework::history); }
+ #if ($target eq 'grade') { &showhash(%Apache::lonhomework::history); }
my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
if ($default == -1) {
&Apache::lonxml::error("Unable to find default_homework.lcpm");
$default='';
}
+ &Apache::lonxml::debug("Should be parsing now");
$result = &Apache::lonxml::xmlparse($target, $problem,
$default.&setup_vars($target),%mystyle);
@@ -308,24 +359,44 @@ sub get_template_list {
}
sub newproblem {
- my ($request) = @_;
-
- my $extension=$request->uri;
- $extension=~s:^.*\.([\w]+)$:$1:;
- &Apache::lonxml::debug("Looking for :$extension:");
- if ($ENV{'form.template'}) {
- use File::Copy;
- my $file = &get_template_list($ENV{'form.template'},$extension);
- my $dest = &Apache::lonnet::filelocation("",$request->uri);
- copy($file,$dest);
- &renderpage($request,$file);
- } else {
- my $templatelist=&get_template_list('',$extension);
- my $url=$request->uri;
- my $dest = &Apache::lonnet::filelocation("",$request->uri);
- $request->print(<uri;
+ $extension=~s:^.*\.([\w]+)$:$1:;
+ &Apache::lonxml::debug("Looking for :$extension:");
+ if ($ENV{'form.template'}) {
+ use File::Copy;
+ my $file = &get_template_list($ENV{'form.template'},$extension);
+ my $dest = &Apache::lonnet::filelocation("",$request->uri);
+ copy($file,$dest);
+ &renderpage($request,$dest);
+ } elsif($ENV{'form.newfile'}) {
+ # I don't like hard-coded filenames but for now, this will work.
+ use File::Copy;
+ my $templatefilename =
+ $request->dir_config('lonIncludes').'/templates/blank.problem';
+ &Apache::lonxml::debug("$templatefilename");
+ my $dest = &Apache::lonnet::filelocation("",$request->uri);
+ copy($templatefilename,$dest);
+ &renderpage($request,$dest);
+ }else {
+ my $templatelist=&get_template_list('',$extension);
+ my $url=$request->uri;
+ my $dest = &Apache::lonnet::filelocation("",$request->uri);
+ if (!defined($templatelist)) {
+ # We didn't find a template, so just create a blank problem.
+ $request->print(<
-The request file $url doesn\'t exist. You can create a new $extension
+The requested file $url doesn\'t exist. You can create a new $extension
+
+