Diff for /loncom/homework/lonhomework.pm between versions 1.187 and 1.207

version 1.187, 2004/12/17 22:30:30 version 1.207, 2005/05/10 20:29:26
Line 30  package Apache::lonhomework; Line 30  package Apache::lonhomework;
 use strict;  use strict;
 use Apache::style();  use Apache::style();
 use Apache::lonxml();  use Apache::lonxml();
 use Apache::lonnet();  use Apache::lonnet;
 use Apache::lonplot();  use Apache::lonplot();
 use Apache::inputtags();  use Apache::inputtags();
 use Apache::structuretags();  use Apache::structuretags();
Line 53  use HTML::Entities(); Line 53  use HTML::Entities();
 use Apache::loncommon();  use Apache::loncommon();
 use Apache::lonlocal;  use Apache::lonlocal;
 use Time::HiRes qw( gettimeofday tv_interval );  use Time::HiRes qw( gettimeofday tv_interval );
   use Apache::lonnet();
   
   # FIXME - improve commenting
   
   
 BEGIN {  BEGIN {
     &Apache::lonxml::register_insert();      &Apache::lonxml::register_insert();
 }  }
   
   
   #
   # Decides what targets to render for.
   # Implicit inputs:
   #   Various session environment variables:
   #      request.state -  published  - is a /res/ resource
   #                       uploaded   - is a /uploaded/ resource
   #                       contruct   - is a /priv/ resource
   #      form.grade_target - a form parameter requesting a specific target
 sub get_target {  sub get_target {
     if (($ENV{'request.state'} eq "published") ||      &Apache::lonxml::debug("request.state = $env{'request.state'}");
  ($ENV{'request.state'} eq "uploaded")) {      if( defined($env{'form.grade_target'})) {
  if ( defined($ENV{'form.grade_target'}  )    &Apache::lonxml::debug("form.grade_target= $env{'form.grade_target'}");
      && ($ENV{'form.grade_target'} eq 'tex')) {      } else {
     return ($ENV{'form.grade_target'});   &Apache::lonxml::debug("form.grade_target <undefined>");
  } elsif ( defined($ENV{'form.grade_target'}  )       }
       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' )) {    && ($Apache::lonhomework::viewgrades eq 'F' )) {
     return ($ENV{'form.grade_target'});      return ($env{'form.grade_target'});
  }   }
    if ($env{'form.webgrade'} &&
  if ( defined($ENV{'form.submitted'}) &&      $Apache::lonhomework::modifygrades eq 'F') {
      ( !defined($ENV{'form.resetdata'})) &&      return ('grade','webgrade');
      ( !defined($ENV{'form.newrandomization'}))) {   }
    if ( defined($env{'form.submitted'}) &&
        ( !defined($env{'form.resetdata'})) &&
        ( !defined($env{'form.newrandomization'}))) {
     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.grade_target'}) ) {   if ( defined($env{'form.grade_target'}) ) {
     return ($ENV{'form.grade_target'});      return ($env{'form.grade_target'});
  }   }
  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 {
     if ( $ENV{'form.problemmode'} eq &mt('View') ||      if ( $env{'form.problemmode'} eq &mt('View') ||
  $ENV{'form.problemmode'} eq &mt('Discard Edits and View')) {   $env{'form.problemmode'} eq &mt('Discard Edits and View')) {
  if ( defined($ENV{'form.submitted'}) &&   if ( defined($env{'form.submitted'}) &&
      (!defined($ENV{'form.resetdata'})) &&       (!defined($env{'form.resetdata'})) &&
      (!defined($ENV{'form.newrandomization'}))) {       (!defined($env{'form.newrandomization'}))) {
     return ('grade', 'web','answer');      return ('grade', 'web','answer');
  } else {   } else {
     return ('web','answer');      return ('web','answer');
  }   }
     } elsif ( $ENV{'form.problemmode'} eq &mt('Edit') ) {      } elsif ( $env{'form.problemmode'} eq &mt('Edit') ||
  if ( $ENV{'form.submitted'} eq 'edit' ) {        $env{'form.problemmode'} eq 'Edit') {
     if ( $ENV{'form.submit'} eq &mt('Submit Changes and View') ) {   if ( $env{'form.submitted'} eq 'edit' ) {
       if ( $env{'form.submit'} eq &mt('Submit Changes and View') ) {
  return ('modified','web','answer');   return ('modified','web','answer');
     } else {      } else {
  return ('modified','edit');   return ('modified','edit');
Line 123  sub setup_vars { Line 146  sub setup_vars {
 sub send_header {  sub send_header {
     my ($request)= @_;      my ($request)= @_;
     $request->print(&Apache::lontexconvert::header());      $request->print(&Apache::lontexconvert::header());
 #  $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.'">');
 }  }
   
 sub createmenu {  sub createmenu {
Line 142  sub send_footer { Line 165  sub send_footer {
     $request->print(&Apache::lontexconvert::footer());      $request->print(&Apache::lontexconvert::footer());
 }  }
   
 $Apache::lonxml::browse='';  sub proctor_checked_in {
       my ($slot_name,$slot)=@_;
       my @allowed=split(",",$slot->{'proctor'});
       foreach my $possible (@allowed) { 
    if ($Apache::lonhomework::history{'resource.checkedin'} eq $possible &&
       $Apache::lonhomework::history{'resource.checkedin.slot'} eq 
           $slot_name ) {
       return 1;
    }
       }
       return 0;
   }
   
   $Apache::lonxml::browse='';
 sub check_ip_acc {  sub check_ip_acc {
     my ($acc)=@_;      my ($acc)=@_;
     if (!defined($acc) || $acc =~ /^\s*$/) { return 1; }      if (!defined($acc) || $acc =~ /^\s*$/) { return 1; }
Line 189  sub check_ip_acc { Line 224  sub check_ip_acc {
     }      }
     return $allowed;      return $allowed;
 }  }
   
   sub check_task_access {
       # does it pass normal muster
       # yes we really do want the default args passing
       my ($status,$datemsg)=&check_access;
       if ($status eq 'SHOW_ANSWER' ||
    $status eq 'CLOSED' ||
    $status eq 'CANNOT_ANSWER' ||
    $status eq 'INVALID_ACCESS' ||
    $status eq 'UNAVAILABLE') {
    return ($status,$datemsg);
       }
       if ($env{'request.state'} eq "construct") {
    return ($status,$datemsg);
       }
       
       if ($Apache::lonhomework::history{'resource.checkedin'}) {
    if ($Apache::lonhomework::history{'resource.status'} eq 'pass' ||
       $Apache::lonhomework::history{'resource.status'} eq 'fail') {
       return ('SHOW_ANSWER');
    }
       }
       my ($id)=@_;
       my @slots=split(':',&Apache::lonnet::EXT("resource.$id.available"));
   #    if (!@slots) {
   # return ($status,$datemsg);
   #    }
       my $slotstatus='NOT_IN_A_SLOT';
       my ($returned_slot,$slot_name);
       foreach my $slot (sort(@slots)) {
    &Apache::lonxml::debug("getting $slot");
    my %slot=&Apache::lonnet::get_slot($slot);
    &Apache::lonhomework::showhash(%slot);
    if ($slot{'starttime'} < time &&
       $slot{'endtime'} > time &&
       &check_ip_acc($slot{'ip'})) {
       &Apache::lonxml::debug("$slot is good");
       $slotstatus='NEEDS_CHECKIN';
       $returned_slot=\%slot;
       $slot_name=$slot;
       last;
    }
       }
       if ($slotstatus eq 'NEEDS_CHECKIN' &&
    &proctor_checked_in($slot_name,$returned_slot)) {
    &Apache::lonxml::debug("protoctor checked in");
    $slotstatus='CAN_ANSWER';
       }
       if ( ($slotstatus eq 'NOT_IN_A_SLOT' || $slotstatus eq 'NEEDS_CHECKIN') && 
    $Apache::lonhomework::history{'resource.checkedin'}) {
    return ('WAITING_FOR_GRADE');
       }
       return ($slotstatus,$datemsg,$slot_name,$returned_slot);
   }
   
 # JB, 9/24/2002: Any changes in this function may require a change  # JB, 9/24/2002: Any changes in this function may require a change
 # in lonnavmaps::resource::getDateStatus.  # in lonnavmaps::resource::getDateStatus.
 sub check_access {  sub check_access {
Line 201  sub check_access { Line 291  sub check_access {
     my $type;      my $type;
     my $passed;      my $passed;
   
     if ($ENV{'request.state'} eq "construct") {      if ($env{'request.state'} eq "construct") {
  if ($ENV{'form.problemstate'}) {   if ($env{'form.problemstate'}) {
     if ($ENV{'form.problemstate'} =~ /^CANNOT_ANSWER/) {      if ($env{'form.problemstate'} =~ /^CANNOT_ANSWER/) {
  if ( ! ($ENV{'form.problemstate'} eq 'CANNOT_ANSWER_correct' &&   if ( ! ($env{'form.problemstate'} eq 'CANNOT_ANSWER_correct' &&
  lc($Apache::lonhomework::problemstatus) eq 'no')) {   lc($Apache::lonhomework::problemstatus) eq 'no')) {
     return ('CANNOT_ANSWER',      return ('CANNOT_ANSWER',
     &mt('is in this state due to author settings.'));      &mt('is in this state due to author settings.'));
  }   }
     } else {      } else {
  return ($ENV{'form.problemstate'},   return ($env{'form.problemstate'},
  &mt('is in this state due to author settings.'));   &mt('is in this state due to author settings.'));
     }      }
  }   }
Line 223  sub check_access { Line 313  sub check_access {
     &Apache::lonxml::debug("checking for part :$id:");      &Apache::lonxml::debug("checking for part :$id:");
     &Apache::lonxml::debug("time:".time);      &Apache::lonxml::debug("time:".time);
   
     if ($ENV{'request.state'} ne "construct") {      if ($env{'request.state'} ne "construct") {
  my $allowed=&check_ip_acc(&Apache::lonnet::EXT("resource.$id.acc"));   my $allowed=&check_ip_acc(&Apache::lonnet::EXT("resource.$id.acc"));
  if (!$allowed && ($Apache::lonhomework::browse ne 'F')) {   if (!$allowed && ($Apache::lonhomework::browse ne 'F')) {
     $status='INVALID_ACCESS';      $status='INVALID_ACCESS';
Line 281  sub check_access { Line 371  sub check_access {
  my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");   my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
  if ( $tries eq '' ) { $tries = '0'; }   if ( $tries eq '' ) { $tries = '0'; }
  if ( $maxtries eq '' &&    if ( $maxtries eq '' && 
      $ENV{'request.state'} ne 'construct') { $maxtries = '2'; }        $env{'request.state'} ne 'construct') { $maxtries = '2'; } 
  if ($maxtries && $tries >= $maxtries) { $status = 'CANNOT_ANSWER'; }   if ($maxtries && $tries >= $maxtries) { $status = 'CANNOT_ANSWER'; }
  # if (correct and show prob status) or excused then CANNOT_ANSWER   # if (correct and show prob status) or excused then CANNOT_ANSWER
  if(($Apache::lonhomework::history{"resource.$id.solved"}=~/^correct/   if(($Apache::lonhomework::history{"resource.$id.solved"}=~/^correct/
Line 300  sub check_access { Line 390  sub check_access {
     &Apache::lonxml::debug("looking for accesstime $first_access");      &Apache::lonxml::debug("looking for accesstime $first_access");
     if (!$first_access) {      if (!$first_access) {
  $status='NOT_YET_VIEWED';   $status='NOT_YET_VIEWED';
  $datemsg=' '.$interval.' seconds';   $datemsg=&seconds_to_human_length($interval);
     } else {      } else {
  my $newdate=localtime($first_access+$interval);   my $newdate=localtime($first_access+$interval);
  if (time > ($first_access+$interval)) {   if (time > ($first_access+$interval)) {
Line 328  sub check_access { Line 418  sub check_access {
     return ($status,$datemsg);      return ($status,$datemsg);
 }  }
   
   sub seconds_to_human_length {
       my ($length)=@_;
   
       my $seconds=$length%60; $length=int($length/60);
       my $minutes=$length%60; $length=int($length/60);
       my $hours=$length%24;   $length=int($length/24);
       my $days=$length;
   
       my $timestr;
       if ($days > 0) { $timestr.=&mt('[quant,_1,day]',$days); }
       if ($hours > 0) { $timestr.=($timestr?", ":"").
     &mt('[quant,_1,hour]',$hours); }
       if ($minutes > 0) { $timestr.=($timestr?", ":"").
       &mt('[quant,_1,minute]',$minutes); }
       if ($seconds > 0) { $timestr.=($timestr?", ":"").
       &mt('[quant,_1,second]',$seconds); }
       return $timestr;
   }
   
 sub showhash {  sub showhash {
     my (%hash) = @_;      my (%hash) = @_;
     &showhashsubset(\%hash,'.');      &showhashsubset(\%hash,'.');
Line 338  sub showarray { Line 447  sub showarray {
     my ($array)=@_;      my ($array)=@_;
     my $string="(";      my $string="(";
     foreach my $elm (@{ $array }) {      foreach my $elm (@{ $array }) {
  if (ref($elm)) {   if (ref($elm) eq 'ARRAY') {
     if ($elm =~ /ARRAY/ ) {      $string.=&showarray($elm);
  $string.=&showarray($elm);   } elsif (ref($elm) eq 'HASH') {
     }      $string.= "HASH --- \n<br />";
       $string.= &showhashsubset($elm,'.');
  } else {   } else {
     $string.="$elm,"      $string.="$elm,"
  }   }
Line 355  sub showhashsubset { Line 465  sub showhashsubset {
     my ($hash,$keyre) = @_;      my ($hash,$keyre) = @_;
     my $resultkey;      my $resultkey;
     foreach $resultkey (sort keys %$hash) {      foreach $resultkey (sort keys %$hash) {
  if ($resultkey =~ /$keyre/) {   if ($resultkey !~ /$keyre/) { next; }
     if (ref($$hash{$resultkey})) {   if (ref($$hash{$resultkey})  eq 'ARRAY' ) {
  if ($$hash{$resultkey} =~ /ARRAY/ ) {      &Apache::lonxml::debug("$resultkey ---- ".
     &Apache::lonxml::debug("$resultkey ---- ".     &showarray($$hash{$resultkey}));
    &showarray($$hash{$resultkey}));   } elsif (ref($$hash{$resultkey}) eq 'HASH' ) {
  } elsif ($$hash{$resultkey} =~ /HASH/ ) {      &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
     &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");      &showhashsubset($$hash{$resultkey},'.');
     &showhashsubset($$hash{$resultkey},'.');   } else {
  } else {      &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
     &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");  
  }  
     } else {  
  &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");  
     }  
  }   }
     }      }
     &Apache::lonxml::debug("\n<br />restored values^</br>\n");      &Apache::lonxml::debug("\n<br />restored values^</br>\n");
Line 376  sub showhashsubset { Line 481  sub showhashsubset {
 }  }
   
 sub setuppermissions {  sub setuppermissions {
     $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});      $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$env{'request.filename'});
     my $viewgrades = &Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});      my $viewgrades = &Apache::lonnet::allowed('vgr',$env{'request.course.id'});
     if (! $viewgrades &&       if (! $viewgrades && 
  exists($ENV{'request.course.sec'}) &&    exists($env{'request.course.sec'}) && 
  $ENV{'request.course.sec'} !~ /^\s*$/) {   $env{'request.course.sec'} !~ /^\s*$/) {
  $viewgrades = &Apache::lonnet::allowed('vgr',$ENV{'request.course.id'}.   $viewgrades = &Apache::lonnet::allowed('vgr',$env{'request.course.id'}.
                                                '/'.$ENV{'request.course.sec'});                                                 '/'.$env{'request.course.sec'});
     }      }
     $Apache::lonhomework::viewgrades = $viewgrades;      $Apache::lonhomework::viewgrades = $viewgrades; # File global variable...dirt.
     if ($Apache::lonhomework::browse eq 'F' &&       if ($Apache::lonhomework::browse eq 'F' && 
  $ENV{'form.devalidatecourseresdata'} eq 'on') {   $env{'form.devalidatecourseresdata'} eq 'on') {
  my (undef,$courseid) = &Apache::lonxml::whichuser();   my (undef,$courseid) = &Apache::lonxml::whichuser();
  &Apache::lonnet::devalidatecourseresdata($ENV{"course.$courseid.num"},   &Apache::lonnet::devalidatecourseresdata($env{"course.$courseid.num"},
       $ENV{"course.$courseid.domain"});        $env{"course.$courseid.domain"});
     }      }
     return ''      my $modifygrades = &Apache::lonnet::allowed('mgr',$env{'request.course.id'});
       if (! $modifygrades && 
    exists($env{'request.course.sec'}) && 
    $env{'request.course.sec'} !~ /^\s*$/) {
    $modifygrades = 
       &Apache::lonnet::allowed('mgr',$env{'request.course.id'}.
        '/'.$env{'request.course.sec'});
       }
       $Apache::lonhomework::modifygrades = $modifygrades;
       return '';
 }  }
   
 sub setupheader {  sub setupheader {
     my $request=$_[0];      my $request=$_[0];
     if ($ENV{'browser.mathml'}) {      &Apache::loncommon::content_type($request,'text/html');
  &Apache::loncommon::content_type($request,'text/xml');  
     } else {  
  &Apache::loncommon::content_type($request,'text/html');  
     }  
     if (!$Apache::lonxml::debug && ($ENV{'REQUEST_METHOD'} eq 'GET')) {      if (!$Apache::lonxml::debug && ($ENV{'REQUEST_METHOD'} eq 'GET')) {
  &Apache::loncommon::no_cache($request);   &Apache::loncommon::no_cache($request);
     }      }
   #    $request->set_last_modified(&Apache::lonnet::metadata($request->uri,
   #  'lastrevisiondate'));
     $request->send_http_header;      $request->send_http_header;
     return OK if $request->header_only;      return OK if $request->header_only;
     return ''      return ''
Line 418  sub handle_save_or_undo { Line 530  sub handle_save_or_undo {
   
     &Apache::lonnet::correct_line_ends($result);      &Apache::lonnet::correct_line_ends($result);
   
     if ($ENV{'form.Undo'} eq &mt('undo')) {      if ($env{'form.Undo'} eq &mt('undo')) {
  my $error=0;   my $error=0;
  if (!copy($file,$filetmp)) { $error=1; }   if (!copy($file,$filetmp)) { $error=1; }
  if ((!$error) && (!copy($filebak,$file))) { $error=1; }   if ((!$error) && (!copy($filebak,$file))) { $error=1; }
Line 456  sub handle_save_or_undo { Line 568  sub handle_save_or_undo {
 sub analyze_header {  sub analyze_header {
     my ($request) = @_;      my ($request) = @_;
     my $bodytag='<body bgcolor="#ffffff">';      my $bodytag='<body bgcolor="#ffffff">';
     if ($ENV{'environment.remote'} eq 'off') {      if ($env{'environment.remote'} eq 'off') {
  $bodytag=&Apache::loncommon::bodytag();   $bodytag=&Apache::loncommon::bodytag();
     }      }
     my $result.='<html>      my $html=&Apache::lonxml::xmlbegin();
       my $result.=$html.'
             <head><title>'.&mt("Analyzing a problem").'</title></head>              <head><title>'.&mt("Analyzing a problem").'</title></head>
             '.$bodytag.&Apache::lonxml::message_location().'              '.$bodytag.&Apache::lonxml::message_location().'
             <form name="lonhomework" method="POST" action="'.              <form name="lonhomework" method="POST" action="'.
     &HTML::Entities::encode($ENV{'request.uri'},'<>&"').'">'.      &HTML::Entities::encode($env{'request.uri'},'<>&"').'">'.
     &Apache::structuretags::remember_problem_state().'      &Apache::structuretags::remember_problem_state().'
             <input type="submit" name="problemmode" value="'.&mt("EditXML").'" />              <input type="submit" name="problemmode" value="'.&mt("EditXML").'" />
             <input type="submit" name="problemmode" value="'.&mt('Edit').'" />              <input type="submit" name="problemmode" value="'.&mt('Edit').'" />
Line 489  sub analyze { Line 602  sub analyze {
     my $result;      my $result;
     my %overall;      my %overall;
     my %allparts;      my %allparts;
     my $rndseed=$ENV{'form.rndseed'};      my $rndseed=$env{'form.rndseed'};
     &analyze_header($request);      &analyze_header($request);
     my %prog_state=      my %prog_state=
  &Apache::lonhtmlcommon::Create_PrgWin($request,&mt('Analyze Progress'),   &Apache::lonhtmlcommon::Create_PrgWin($request,&mt('Analyze Progress'),
       &mt('Getting Problem Variants'),        &mt('Getting Problem Variants'),
       $ENV{'form.numtoanalyze'},        $env{'form.numtoanalyze'},
       'inline',undef);        'inline',undef);
     for(my $i=1;$i<$ENV{'form.numtoanalyze'}+1;$i++) {      for(my $i=1;$i<$env{'form.numtoanalyze'}+1;$i++) {
  &Apache::lonhtmlcommon::Increment_PrgWin($request,\%prog_state,   &Apache::lonhtmlcommon::Increment_PrgWin($request,\%prog_state,
  &mt('last problem'));   &mt('last problem'));
  if (&Apache::loncommon::connection_aborted($request)) { return; }   if (&Apache::loncommon::connection_aborted($request)) { return; }
Line 561  sub editxmlmode { Line 674  sub editxmlmode {
        " <i>$file</i></b>");         " <i>$file</i></b>");
  $problem='';   $problem='';
     }      }
     if (defined($ENV{'form.editxmltext'}) || defined($ENV{'form.Undo'})) {      if (defined($env{'form.editxmltext'}) || defined($env{'form.Undo'})) {
  my $error=&handle_save_or_undo($request,\$problem,   my $error=&handle_save_or_undo($request,\$problem,
        \$ENV{'form.editxmltext'});         \$env{'form.editxmltext'});
  if (!$error) { $problem=&Apache::lonnet::getfile($file); }   if (!$error) { $problem=&Apache::lonnet::getfile($file); }
     }      }
     &Apache::lonhomework::showhashsubset(\%ENV,'^form');      &Apache::lonhomework::showhashsubset(\%env,'^form');
     if ( $ENV{'form.submit'} eq &mt('Submit Changes and View') ) {      if ( $env{'form.submit'} eq &mt('Submit Changes and View') ) {
  &Apache::lonhomework::showhashsubset(\%ENV,'^form');   &Apache::lonhomework::showhashsubset(\%env,'^form');
  $ENV{'form.problemmode'}='View';   $env{'form.problemmode'}='View';
  &renderpage($request,$file);   &renderpage($request,$file);
     } else {      } else {
  my ($rows,$cols) = &Apache::edit::textarea_sizes(\$problem);   my ($rows,$cols) = &Apache::edit::textarea_sizes(\$problem);
Line 583  sub editxmlmode { Line 696  sub editxmlmode {
  if ($cols < 70) { $cols = 70; }   if ($cols < 70) { $cols = 70; }
  if ($rows < 20) { $rows = 20; }   if ($rows < 20) { $rows = 20; }
  my $bodytag='<body bgcolor="#ffffff">';   my $bodytag='<body bgcolor="#ffffff">';
  if ($ENV{'environment.remote'} eq 'off') {   if ($env{'environment.remote'} eq 'off') {
     $bodytag=&Apache::loncommon::bodytag();      $bodytag=&Apache::loncommon::bodytag();
  }   }
  $result.='<html>'.$bodytag.&Apache::lonxml::message_location().'   my $html=&Apache::lonxml::xmlbegin();
    $result.=$html.$bodytag.&Apache::lonxml::message_location().'
             <form name="lonhomework" method="POST" action="'.              <form name="lonhomework" method="POST" action="'.
     &HTML::Entities::encode($ENV{'request.uri'},'<>&"').'">'.      &HTML::Entities::encode($env{'request.uri'},'<>&"').'">'.
     &Apache::structuretags::remember_problem_state().'      &Apache::structuretags::remember_problem_state().'
             <input type="hidden" name="problemmode" value="'.&mt('EditXML').'" />              <input type="hidden" name="problemmode" value="'.&mt('EditXML').'" />
             <input type="submit" name="problemmode" accesskey="d" value="'.&mt('Discard Edits and View').'" />              <input type="submit" name="problemmode" accesskey="d" value="'.&mt('Discard Edits and View').'" />
Line 599  sub editxmlmode { Line 713  sub editxmlmode {
             <input type="submit" name="Undo" accesskey="u" value="'.&mt('undo').'" />              <input type="submit" name="Undo" accesskey="u" value="'.&mt('undo').'" />
             <hr />              <hr />
             ' . $xml_help . '              ' . $xml_help . '
             <textarea rows="'.$rows.'" cols="'.$cols.'" name="editxmltext">'.              <textarea style="width:100%" rows="'.$rows.'" cols="'.$cols.'" name="editxmltext">'.
     &HTML::Entities::encode($problem,'<>&"').'</textarea>      &HTML::Entities::encode($problem,'<>&"').'</textarea>
             </form></body></html>';              </form></body></html>';
  &Apache::lonxml::add_messages(\$result);   &Apache::lonxml::add_messages(\$result);
Line 608  sub editxmlmode { Line 722  sub editxmlmode {
     return '';      return '';
 }  }
   
   #
   #    Render the page in whatever target desired.
   #
 sub renderpage {  sub renderpage {
     my ($request,$file) = @_;      my ($request,$file) = @_;
   
     my (@targets) = &get_target();      my (@targets) = &get_target();
     &Apache::lonhomework::showhashsubset(\%ENV,'form.');      &Apache::lonhomework::showhashsubset(\%env,'form.');
     &Apache::lonxml::debug("Running targets ".join(':',@targets));      &Apache::lonxml::debug("Running targets ".join(':',@targets));
     my $overall_result;      my $overall_result;
     foreach my $target (@targets) {      foreach my $target (@targets) {
Line 634  sub renderpage { Line 751  sub renderpage {
  my $result = '';   my $result = '';
  if ($target eq 'analyze') { %Apache::lonhomework::analyze=(); }   if ($target eq 'analyze') { %Apache::lonhomework::analyze=(); }
  if ($target eq 'answer') { &showhash(%Apache::lonhomework::history); }   if ($target eq 'answer') { &showhash(%Apache::lonhomework::history); }
  if ($target eq 'web') {&Apache::lonhomework::showhashsubset(\%ENV,'^form');}   if ($target eq 'web') {&Apache::lonhomework::showhashsubset(\%env,'^form');}
   
  &Apache::lonxml::debug("Should be parsing now");   &Apache::lonxml::debug("Should be parsing now");
  $result = &Apache::lonxml::xmlparse($request, $target, $problem,   $result = &Apache::lonxml::xmlparse($request, $target, $problem,
Line 694  sub newproblem { Line 811  sub newproblem {
     $extension=~s:^.*\.([\w]+)$:$1:;      $extension=~s:^.*\.([\w]+)$:$1:;
     &Apache::lonxml::debug("Looking for :$extension:");      &Apache::lonxml::debug("Looking for :$extension:");
     my $templatelist=&get_template_list('',$extension);      my $templatelist=&get_template_list('',$extension);
     if ($ENV{'form.template'} &&      if ($env{'form.template'} &&
  $ENV{'form.template'} ne "Select a $extension template") {   $env{'form.template'} ne "Select a $extension template") {
  use File::Copy;   use File::Copy;
  my $file = &get_template_list($ENV{'form.template'},$extension);   my $file = &get_template_list($env{'form.template'},$extension);
  my $dest = &Apache::lonnet::filelocation("",$request->uri);   my $dest = &Apache::lonnet::filelocation("",$request->uri);
  copy($file,$dest);   copy($file,$dest);
  &renderpage($request,$dest);   &renderpage($request,$dest);
     } elsif($ENV{'form.newfile'} && !$templatelist) {      } elsif($env{'form.newfile'} && !$templatelist) {
  # I don't like hard-coded filenames but for now, this will work.   # I don't like hard-coded filenames but for now, this will work.
  use File::Copy;   use File::Copy;
  my $templatefilename =   my $templatefilename =
Line 716  sub newproblem { Line 833  sub newproblem {
  $shownurl=~s-^/~-/priv/-;   $shownurl=~s-^/~-/priv/-;
  my $dest = &Apache::lonnet::filelocation("",$request->uri);   my $dest = &Apache::lonnet::filelocation("",$request->uri);
  my $errormsg;   my $errormsg;
  if ($ENV{'form.newfile'}) {   if ($env{'form.newfile'}) {
     $errormsg='<p><font color="red">'.&mt('You did not select a template.').'</font></p>'."\n";      $errormsg='<p><font color="red">'.&mt('You did not select a template.').'</font></p>'."\n";
  }   }
  my $instructions;   my $instructions;
  my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,   my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,
  ($ENV{'environment.remote'} ne 'off'));   ($env{'environment.remote'} ne 'off'));
  if ($templatelist) { $instructions=&mt(", select a template from the pull-down menu below.").'<br />'.&mt("Then");}   if ($templatelist) { $instructions=&mt(", select a template from the pull-down menu below.").'<br />'.&mt("Then");}
  my %lt=&Apache::lonlocal::texthash( 'create' => 'Creating a new',   my %lt=&Apache::lonlocal::texthash( 'create' => 'Creating a new',
   'resource' => 'resource',    'resource' => 'resource',
Line 773  sub handler { Line 890  sub handler {
     #my $t0 = [&gettimeofday()];      #my $t0 = [&gettimeofday()];
     my $request=$_[0];      my $request=$_[0];
           
     $Apache::lonxml::debug=$ENV{'user.debug'};      $Apache::lonxml::debug=$env{'user.debug'};
     $ENV{'request.uri'}=$request->uri;      $env{'request.uri'}=$request->uri;
     &setuppermissions();      &setuppermissions();
     &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:");  
     # some times multiple problemmodes are submitted, need to select      # some times multiple problemmodes are submitted, need to select
     # the last one      # the last one
     &Apache::lonxml::debug("Problem Mode ".$ENV{'form.problemmode'});      if ( defined($env{'form.problemmode'}) && ref($env{'form.problemmode'}) ) {
     if ( defined($ENV{'form.problemmode'}) &&   my $mode=$env{'form.problemmode'}->[-1];
  ref($ENV{'form.problemmode'}) ) {   undef $env{'form.problemmode'};
  &Apache::lonxml::debug("Problem Mode ".join(",",@$ENV{'form.problemmode'}));   $env{'form.problemmode'}=$mode;
  my $mode=$ENV{'form.problemmode'}->[-1];  
  undef $ENV{'form.problemmode'};  
  $ENV{'form.problemmode'}=$mode;  
     }      }
     &Apache::lonxml::debug("Problem Mode ".$ENV{'form.problemmode'});  
     my $file=&Apache::lonnet::filelocation("",$request->uri);      my $file=&Apache::lonnet::filelocation("",$request->uri);
   
     #check if we know where we are      #check if we know where we are
     if ($ENV{'request.course.fn'} && !&Apache::lonnet::symbread()) {       if ($env{'request.course.fn'} && !&Apache::lonnet::symbread()) { 
  # if we are browsing we might not be able to know where we are   # if we are browsing we might not be able to know where we are
  if ($Apache::lonhomework::browse ne 'F' &&    if ($Apache::lonhomework::browse ne 'F' && 
     $ENV{'request.state'} ne "construct") {      $env{'request.state'} ne "construct") {
     #should know where we are, so ask      #should know where we are, so ask
     if ( &Apache::lonnet::mod_perl_version() == 2 ) {      if ( &Apache::lonnet::mod_perl_version() == 2 ) {
  &Apache::lonnet::cleanenv();   &Apache::lonnet::cleanenv();
     }      }
     $request->internal_redirect('/adm/ambiguous'); return;      &Apache::lonnet::logthis(&Apache::lonnet::mod_perl_version());
       $request->internal_redirect('/adm/ambiguous'); return OK;
  }   }
     }      }
     if (&setupheader($request)) { return OK; }      if (&setupheader($request)) { return OK; }
       &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:");
       &Apache::lonxml::debug("Problem Mode ".$env{'form.problemmode'});
     my ($symb) = &Apache::lonxml::whichuser();      my ($symb) = &Apache::lonxml::whichuser();
     &Apache::lonxml::debug('symb is '.$symb);      &Apache::lonxml::debug('symb is '.$symb);
     if ($ENV{'request.state'} eq "construct" || $symb eq '') {      if ($env{'request.state'} eq "construct" || $symb eq '') {
  if ($ENV{'form.resetdata'} eq &mt('Reset Submissions') ||   if ($env{'form.resetdata'} eq &mt('Reset Submissions') ||
     $ENV{'form.resetdata'} eq &mt('New Problem Variation') ||      $env{'form.resetdata'} eq &mt('New Problem Variation') ||
     $ENV{'form.newrandomization'} eq &mt('New Randomization')) {      $env{'form.newrandomization'} eq &mt('New Randomization')) {
     my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();      my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
     &Apache::lonnet::tmpreset($symb,'',$domain,$name);      &Apache::lonnet::tmpreset($symb,'',$domain,$name);
     &Apache::lonxml::debug("Attempt reset");      &Apache::lonxml::debug("Attempt reset");
  }   }
     }      }
     if ($ENV{'request.state'} eq "construct") {      if ($env{'request.state'} eq "construct") {
  if ( -e $file ) {   if ( -e $file ) {
     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},      &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
     ['problemmode']);      ['problemmode']);
     if (!(defined $ENV{'form.problemmode'})) {      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);
  $ENV{'form.problemmode'}='View';   $env{'form.problemmode'}='View';
  &renderpage($request,$file);   &renderpage($request,$file);
     } elsif ($ENV{'form.problemmode'} eq &mt('EditXML')) {      } elsif ($env{'form.problemmode'} eq &mt('EditXML') ||
        $env{'form.problemmode'} eq 'EditXML') {
  &editxmlmode($request,$file);   &editxmlmode($request,$file);
     } elsif ($ENV{'form.problemmode'} eq &mt('Calculate answers')) {      } elsif ($env{'form.problemmode'} eq &mt('Calculate answers')) {
  &analyze($request,$file);   &analyze($request,$file);
     } else {      } else {
  &renderpage($request,$file);   &renderpage($request,$file);

Removed from v.1.187  
changed lines
  Added in v.1.207


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