Diff for /loncom/homework/lonhomework.pm between versions 1.41 and 1.62

version 1.41, 2001/04/26 21:28:33 version 1.62, 2001/11/05 22:49:14
Line 1 Line 1
 # The LON-CAPA Homework handler  # The LON-CAPA Homework handler
 # Guy Albertelli  # Guy Albertelli
 # 11/30 Gerd Kortemeyer  # 11/30 Gerd Kortemeyer
 # 6/1 Gerd Kortemeyer  # 6/1,8/17,8/18 Gerd Kortemeyer
   
 package Apache::lonhomework;  package Apache::lonhomework;
 use strict;  use strict;
Line 10  use Apache::lonxml; Line 10  use Apache::lonxml;
 use Apache::lonnet;  use Apache::lonnet;
 use Apache::inputtags;  use Apache::inputtags;
 use Apache::structuretags;  use Apache::structuretags;
   use Apache::randomlabel;
 use Apache::response;  use Apache::response;
 use Apache::hint;  use Apache::hint;
 use Apache::outputtags;  use Apache::outputtags;
 use Apache::Constants qw(:common);  use Apache::Constants qw(:common);
   #use Time::HiRes qw( gettimeofday tv_interval );
   
   sub BEGIN {
     &Apache::lonxml::register_insert();
   }
   
 sub get_target {  sub get_target {
   if ( $ENV{'request.state'} eq "published") {    if ( $ENV{'request.state'} eq "published") {
     if ( defined $ENV{'form.submitted'}) {      if ( defined($ENV{'form.submitted'})) {
       return ('grade', 'web');        return ('grade', 'web');
     } else {      } else {
       return ('web');        return ('web');
     }      }
   } elsif ($ENV{'request.state'} eq "construct") {    } elsif ($ENV{'request.state'} eq "construct") {
     if ( defined $ENV{'form.preview'}) {      if ( defined($ENV{'form.preview'})) {
       if ( defined $ENV{'form.submitted'}) {        if ( defined($ENV{'form.submitted'})) {
  return ('grade', 'web');   return ('grade', 'web');
       } else {        } else {
  return ('web');   return ('web');
       }        }
     } else {      } else {
       return ('grade','web');        if ( $ENV{'form.problemmode'} eq 'View' ) {
       if ( $ENV{'form.submitted'} eq 'edit' ) {   if ( defined($ENV{'form.submitted'}) &&
  return ('modified','edit');       (!defined($ENV{'form.resetdata'})) ) {
     return ('grade', 'web','answer');
    } else {
     return ('web','answer');
    }
         } elsif ( $ENV{'form.problemmode'} eq 'Edit' ) {
    if ( $ENV{'form.submitted'} eq 'edit' ) {
     return ('modified','edit');
    } else {
     return ('edit');
    }
       } else {        } else {
  return ('edit');   return ('web');
       }        }
     }      }
   }    }
Line 50  sub setup_vars { Line 66  sub setup_vars {
 sub send_header {  sub send_header {
   my ($request)= @_;    my ($request)= @_;
   $request->print(&Apache::lontexconvert::header());    $request->print(&Apache::lontexconvert::header());
   $ENV{'request.uri'}=$request->uri;  
 #  $request->print('<form name='.$ENV{'form.request.prefix'}.'lonhomework method="POST" action="'.$request->uri.'">');  #  $request->print('<form name='.$ENV{'form.request.prefix'}.'lonhomework method="POST" action="'.$request->uri.'">');
 }  }
   
Line 71  sub send_footer { Line 86  sub send_footer {
 }  }
   
 $Apache::lonxml::browse='';  $Apache::lonxml::browse='';
 sub check_date {  
   sub check_access {
   my ($id) = @_;    my ($id) = @_;
   my $date ='';    my $date ='';
   my $status = '';    my $status = '';
Line 117  sub check_date { Line 133  sub check_date {
     if ( $maxtries eq '' ) { $maxtries = '2'; }       if ( $maxtries eq '' ) { $maxtries = '2'; } 
     if ($tries >= $maxtries) { $status = 'CANNOT_ANSWER'; }       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:");    &Apache::lonxml::debug("sending back :$status:$datemsg:");
   if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) {    if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) {
     &Apache::lonxml::debug("should be allowed to browse a resource when closed");      &Apache::lonxml::debug("should be allowed to browse a resource when closed");
Line 154  sub setupheader { Line 177  sub setupheader {
   } else {    } else {
     $request->content_type('text/html');      $request->content_type('text/html');
   }    }
     &Apache::loncommon::no_cache($request);
   $request->send_http_header;    $request->send_http_header;
   return OK if $request->header_only;    return OK if $request->header_only;
   &Apache::lonhomework::send_header($request);  
   return ''    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("<b>Undid changes, Copied $filesave to $fileout<br /></b>");
       } else {
         $request->print("<font color=\"red\" size=\"+1\"><b>Unable to undo, unable to copy $filesave to $fileout<br /></b></font>");
       }
     } else {
       my $fs=Apache::File->new(">$filesave");
       if (defined($fs)) {
         print $fs $$problem;
         $request->print("<b>Making Backup to $filesave</b><br />");
       } else {
         $request->print("<font color=\"red\" size=\"+1\"><b>Unable to make backup $filesave</b></font>");
       }
       my $fh=Apache::File->new(">$fileout");
       if (defined($fh)) {
         print $fh $$result;
         $request->print("<b>Saving Modifications to $fileout</b><br />");
       } else {
         $request->print("<font color=\"red\" size=\"+1\"><b>Unable to write to $fileout</b></font>");
       }
     }
   }
   
 sub renderpage {  sub renderpage {
   my ($request,$file) = @_;    my ($request,$file) = @_;
   
   my (@targets) = &get_target();    my (@targets) = &get_target();
   foreach my $target (@targets) {    foreach my $target (@targets) {
       #my $t0 = [&gettimeofday()];
     my $problem=&Apache::lonnet::getfile($file);      my $problem=&Apache::lonnet::getfile($file);
     if ($problem == -1) {      if ($problem == -1) {
       &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");        &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
Line 174  sub renderpage { Line 227  sub renderpage {
     my %mystyle;      my %mystyle;
     my $result = '';      my $result = '';
     &Apache::inputtags::initialize_inputtags;      &Apache::inputtags::initialize_inputtags;
     %Apache::lonhomework::results=();      &Apache::edit::initialize_edit;
     %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 ($target eq 'web') {
       if (&Apache::lonnet::symbread() eq '') {        if (&Apache::lonnet::symbread() eq '') {
  if ($ENV{'request.state'} eq "construct") {   if ($ENV{'request.state'} eq "construct") {
   $request->print("In construction space, submissions ignored<br />");  
  } else {   } else {
   $request->print("Browsing or <a href=\"/adm/ambiguous\">ambiguous</a> reference, submissions ignored<br />");    $request->print("Browsing or <a href=\"/adm/ambiguous\">ambiguous</a> reference, submissions ignored<br />");
  }   }
       }        }
       if ($Apache::lonhomework::viewgrades eq 'F') {&createmenu('grade',$request); }        #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); }
   
Line 201  sub renderpage { Line 249  sub renderpage {
   
     #$request->print("Result follows:");      #$request->print("Result follows:");
     if ($target eq 'modified') {      if ($target eq 'modified') {
       my $fileout = &Apache::lonnet::filelocation("",$request->uri);        &handle_save_or_undo($request,\$problem,\$result);
       my $filesave=$fileout.".bak";  
   
       my $fs=Apache::File->new(">$filesave");  
       print $fs $problem;  
       $request->print("<b>Making Backup to $filesave</b><br />");  
   
       my $fh=Apache::File->new(">$fileout");  
       print $fh $result;  
       $request->print("<b>Saving Modifications to $fileout</b><br />");  
     } else {      } else {
         #my $td=&tv_interval($t0);
         #if ( $Apache::lonxml::debug) {
    #$result =~ s:</body>::;
    #$result.="<br />Spent $td seconds processing target $target\n</body>";
         #}
       $request->print($result);        $request->print($result);
     }      }
     #$request->print(":Result ends");      #$request->print(":Result ends");
     if ($target eq 'grade') { &showhash(%Apache::lonhomework::results); }      #my $td=&tv_interval($t0);
     }
   }
   
     #store new values  # with no arg it returns a HTML <option> list of the template titles
     my ($temp) = keys %Apache::lonhomework::results;  # with one arg it returns the filename associated with the arg passed
     if ( $temp ne '' ) {  sub get_template_list {
       &Apache::lonxml::debug("Store return message:".    my ($namewanted,$extension) = @_;
      &Apache::lonnet::cstore(\%Apache::lonhomework::results));    my $result;
     &Apache::lonxml::debug("Looking for :$extension:");
     foreach my $file (</home/httpd/html/res/adm/includes/templates/*.$extension>) {
       my $name=&Apache::lonnet::metadata($file,'title');
       if ($namewanted && ($name eq $namewanted)) {
         $result=$file;
         last;
       } else {
         $result.="<option>$name</option>";
     }      }
   }    }
     return $result;
   }
   
   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(<<ENDNEWPROBLEM);
   <body bgcolor="#FFFFFF">
   The request file $url doesn\'t exist. You can create a new $extension <br />
   <form action="$url" method="POST">
   <input type="submit" value="New $extension"><br />
   <select name="template">
   $templatelist
   </select>
   </form>
   </body>
   ENDNEWPROBLEM
     }
   
     return '';
   }
   
   sub view_or_edit_menu {
     my ($request) = @_;
     my $url=$request->uri;
     $request->print(<<EDITMENU);
   <body bgcolor="#FFFFFF">
   <form action="$url" method="POST">
   Would you like to <input type="submit" name="problemmode" value="View"> or
   <input type="submit" name="problemmode" value="Edit"> the problem.
   </form>
   </body>
   EDITMENU
 }  }
   
 sub handler {  sub handler {
     #my $t0 = [&gettimeofday()];
   my $request=$_[0];    my $request=$_[0];
   
   if ( $ENV{'user.name'} eq 'albertel' ) {$Apache::lonxml::debug=1;}    if ( $ENV{'user.name'} eq 'albertel' ) {$Apache::lonxml::debug=1;}
   
   if (&setupheader($request)) { return OK; }    if (&setupheader($request)) { return OK; }
     $ENV{'request.uri'}=$request->uri;
   
   #setup permissions    #setup permissions
   $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});    $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
Line 250  sub handler { Line 352  sub handler {
   }    }
   
   if ($ENV{'request.state'} eq "construct") {    if ($ENV{'request.state'} eq "construct") {
       if ($ENV{'form.resetdata'} eq 'Reset Submissions') {
         my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
         &Apache::lonnet::tmpreset($symb,'',$domain,$name);
       }
     if ( -e $file ) {      if ( -e $file ) {
       if (!(defined $ENV{'form.showproblem'})) {        if (!(defined $ENV{'form.problemmode'})) {
  #first visit to problem in construction space   #first visit to problem in construction space
  &view_or_edit_menu($request);   &view_or_edit_menu($request);
    #&renderpage($request,$file);
       } else {        } else {
  &renderpage($request,$file);   &renderpage($request,$file);
       }        }
Line 265  sub handler { Line 372  sub handler {
     # just render the page normally outside of construction space      # just render the page normally outside of construction space
     &renderpage($request,$file);      &renderpage($request,$file);
   }    }
     #my $td=&tv_interval($t0);
   &Apache::lonhomework::send_footer($request);    #&Apache::lonxml::debug("Spent $td seconds processing");
     # &Apache::lonhomework::send_footer($request);
   # always turn off debug messages    # always turn off debug messages
   $Apache::lonxml::debug=0;    $Apache::lonxml::debug=0;
   return OK;    return OK;

Removed from v.1.41  
changed lines
  Added in v.1.62


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>