--- loncom/homework/lonhomework.pm 2001/12/04 14:51:19 1.64 +++ loncom/homework/lonhomework.pm 2002/05/24 18:55:23 1.79 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # The LON-CAPA Homework handler # -# $Id: lonhomework.pm,v 1.64 2001/12/04 14:51:19 albertel Exp $ +# $Id: lonhomework.pm,v 1.79 2002/05/24 18:55:23 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -31,30 +31,39 @@ package Apache::lonhomework; use strict; -use Apache::style; -use Apache::lonxml; -use Apache::lonnet; -use Apache::inputtags; -use Apache::structuretags; -use Apache::randomlabel; -use Apache::response; -use Apache::hint; -use Apache::outputtags; +use Apache::style(); +use Apache::lonxml(); +use Apache::lonnet(); +use Apache::lonplot(); +use Apache::inputtags(); +use Apache::structuretags(); +use Apache::randomlabel(); +use Apache::response(); +use Apache::hint(); +use Apache::outputtags(); use Apache::Constants qw(:common); +use HTML::Entities(); #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 { 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'); @@ -123,6 +132,7 @@ sub check_access { my $type; my $passed; &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"); @@ -182,9 +192,30 @@ sub check_access { sub showhash { my (%hash) = @_; + &showhashsubset(\%hash,''); + return ''; +} + +sub showhashsubset { + my ($hash,$keyre) = @_; my $resultkey; - foreach $resultkey (sort keys %hash) { - &Apache::lonxml::debug("$resultkey ---- $hash{$resultkey}"); + foreach $resultkey (sort keys %$hash) { + if ($resultkey =~ /$keyre/) { + if (ref($$hash{$resultkey})) { + if ($$hash{$resultkey} =~ /ARRAY/ ) { + my $string="$resultkey ---- ("; + foreach my $elm (@{ $$hash{$resultkey} }) { + $string.="$elm,"; + } + chop($string); + &Apache::lonxml::debug("$string)"); + } else { + &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}"); + } + } else { + &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}"); + } + } } &Apache::lonxml::debug("\n
restored values^
\n"); return ''; @@ -203,7 +234,9 @@ sub setupheader { } else { $request->content_type('text/html'); } - if (!$Apache::lonxml::debug) { &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,38 +244,53 @@ 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 analyze { + my ($request,$file) = @_; + &Apache::lonxml::debug("Analyze"); + my $result=&Apache::lonnet::ssi($request->uri,('grade_target' => 'analyze')); + &Apache::lonxml::debug(":$result:"); + (my $garbage,$result)=split(/_HASH_REF__/,$result,2); + &showhash(&Apache::lonnet::str2hash($result)); + return $result; +} + sub editxmlmode { my ($request,$file) = @_; my $result; @@ -269,7 +317,7 @@ sub editxmlmode {
+ &HTML::Entities::encode($problem).' '; $request->print($result); return ''; @@ -279,6 +327,7 @@ 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); @@ -291,8 +340,10 @@ sub renderpage { my $result = ''; &Apache::inputtags::initialize_inputtags; &Apache::edit::initialize_edit; + if ($target eq 'analyze') { %Apache::lonhomework::anaylze=(); } if ($target eq 'web') { - if (&Apache::lonnet::symbread() eq '') { + my ($symb)=&Apache::lonxml::whichuser(); + if ($symb eq '') { if ($ENV{'request.state'} eq "construct") { } else { $request->print("Browsing or ambiguous reference, submissions ignored
"); @@ -301,19 +352,25 @@ sub renderpage { #if ($Apache::lonhomework::viewgrades eq 'F') {&createmenu('grade',$request); } } #if ($target eq 'grade') { &showhash(%Apache::lonhomework::history); } + #if ($target eq 'web') { &showhash(%ENV); } 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=''; } - $result = &Apache::lonxml::xmlparse($target, $problem, + &Apache::lonxml::debug("Should be parsing now"); + $result = &Apache::lonxml::xmlparse($request, $target, $problem, $default.&setup_vars($target),%mystyle); #$request->print("Result follows:"); if ($target eq 'modified') { &handle_save_or_undo($request,\$problem,\$result); } else { + if ($target eq 'analyze') { + $result=&Apache::lonnet::hashref2str(\%Apache::lonhomework::analyze); + undef(%Apache::lonhomework::analyze); + } #my $td=&tv_interval($t0); #if ( $Apache::lonxml::debug) { #$result =~ s:::; @@ -345,24 +402,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
+
+
+
+ +ENDNEWPROBLEM + return ''; + } + $request->print(< +The requested file $url doesn\'t exist. You can create a new $extension