File:  [LON-CAPA] / loncom / homework / lonhomework.pm
Revision 1.117: download - view: text, annotated - select for diffs
Tue Mar 25 23:07:07 2003 UTC (21 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: HEAD
- better as str compare

    1: # The LearningOnline Network with CAPA
    2: # The LON-CAPA Homework handler
    3: #
    4: # $Id: lonhomework.pm,v 1.117 2003/03/25 23:07:07 albertel Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: # Guy Albertelli
   29: # 11/30 Gerd Kortemeyer
   30: # 6/1,8/17,8/18 Gerd Kortemeyer
   31: # 7/18 Jeremy Bowers
   32: 
   33: package Apache::lonhomework;
   34: use strict;
   35: use Apache::style();
   36: use Apache::lonxml();
   37: use Apache::lonnet();
   38: use Apache::lonplot();
   39: use Apache::inputtags();
   40: use Apache::structuretags();
   41: use Apache::randomlabel();
   42: use Apache::response();
   43: use Apache::hint();
   44: use Apache::outputtags();
   45: use Apache::caparesponse();
   46: use Apache::radiobuttonresponse();
   47: use Apache::optionresponse();
   48: use Apache::imageresponse();
   49: use Apache::essayresponse();
   50: use Apache::externalresponse();
   51: use Apache::rankresponse();
   52: use Apache::matchresponse();
   53: use Apache::Constants qw(:common);
   54: use HTML::Entities();
   55: use Apache::loncommon();
   56: #use Time::HiRes qw( gettimeofday tv_interval );
   57: 
   58: BEGIN {
   59:   &Apache::lonxml::register_insert();
   60: }
   61: 
   62: sub get_target {
   63:   if ( $ENV{'request.state'} eq "published") {
   64:     if ( defined($ENV{'form.grade_target'}  ) 
   65: 	 && ($ENV{'form.grade_target'} eq 'tex')) {
   66:       return ($ENV{'form.grade_target'});
   67:     } elsif ( defined($ENV{'form.grade_target'}  ) 
   68: 	 && ($Apache::lonhomework::viewgrades == 'F' )) {
   69:       return ($ENV{'form.grade_target'});
   70:     }
   71:  
   72:     if ( defined($ENV{'form.submitted'})) {
   73:       return ('grade', 'web');
   74:     } else {
   75:       return ('web');
   76:     }
   77:   } elsif ($ENV{'request.state'} eq "construct") {
   78:     if ( defined($ENV{'form.grade_target'}) ) {
   79:       return ($ENV{'form.grade_target'});
   80:     }
   81:     if ( defined($ENV{'form.preview'})) {
   82:       if ( defined($ENV{'form.submitted'})) {
   83: 	return ('grade', 'web');
   84:       } else {
   85: 	return ('web');
   86:       }
   87:     } else {
   88:       if ( $ENV{'form.problemmode'} eq 'View' ||
   89: 	   $ENV{'form.problemmode'} eq 'Discard Edits and View') {
   90: 	if ( defined($ENV{'form.submitted'}) &&
   91: 	     (!defined($ENV{'form.resetdata'})) ) {
   92: 	  return ('grade', 'web','answer');
   93: 	} else {
   94: 	  return ('web','answer');
   95: 	}
   96:       } elsif ( $ENV{'form.problemmode'} eq 'Edit' ) {
   97: 	if ( $ENV{'form.submitted'} eq 'edit' ) {
   98: 	  if ( $ENV{'form.submit'} eq 'Submit Changes and View' ) {
   99: 	    return ('modified','web','answer');
  100: 	  } else {
  101: 	    return ('modified','edit');
  102: 	  }
  103: 	} else {
  104: 	  return ('edit');
  105: 	}
  106:       } else {
  107: 	return ('web');
  108:       }
  109:     }
  110:   }
  111:   return ();
  112: }
  113: 
  114: sub setup_vars {
  115:   my ($target) = @_;
  116:   return ';'
  117: #  return ';$external::target='.$target.';';
  118: }
  119: 
  120: sub send_header {
  121:   my ($request)= @_;
  122:   $request->print(&Apache::lontexconvert::header());
  123: #  $request->print('<form name='.$ENV{'form.request.prefix'}.'lonhomework method="POST" action="'.$request->uri.'">');
  124: }
  125: 
  126: sub createmenu {
  127:   my ($which,$request)=@_;
  128:   if ($which eq 'grade') {
  129:     $request->print('<script language="JavaScript"> 
  130:           hwkmenu=window.open("/res/adm/pages/homeworkmenu.html","homeworkremote",
  131:                  "height=350,width=150,menubar=no");
  132:           </script>');
  133:   }
  134: }
  135: 
  136: sub send_footer {
  137:   my ($request)= @_;
  138: #  $request->print('</form>');
  139:   $request->print(&Apache::lontexconvert::footer());
  140: }
  141: 
  142: $Apache::lonxml::browse='';
  143: 
  144: # JB, 9/24/2002: Any changes in this function may require a change
  145: # in lonnavmaps::resource::getDateStatus.
  146: sub check_access {
  147:   my ($id) = @_;
  148:   my $date ='';
  149:   my $status;
  150:   my $datemsg = '';
  151:   my $lastdate = '';
  152:   my $temp;
  153:   my $type;
  154:   my $passed;
  155: 
  156:   if ($ENV{'request.state'} eq "construct") {
  157:     &Apache::lonxml::debug("in construction ignoring dates");
  158:     $status='CAN_ANSWER';
  159:     $datemsg='is in under construction';
  160:     return ($status,$datemsg);
  161:   }
  162: 
  163:   &Apache::lonxml::debug("checking for part :$id:");
  164:   &Apache::lonxml::debug("time:".time);
  165:   foreach $temp ("opendate","duedate","answerdate") {
  166:     $lastdate = $date;
  167:     $date = &Apache::lonnet::EXT("resource.$id.$temp");
  168:     my $thistype = &Apache::lonnet::EXT("resource.$id.$temp.type");
  169:     if ($thistype =~ /^(con_lost|no_such_host)/ ||
  170: 	$date     =~ /^(con_lost|no_such_host)/) {
  171: 	$status='UNAVAILABLE';
  172: 	$date="may open later.";
  173: 	return($status,$date);
  174:     }
  175:     if ($thistype eq 'date_interval') {
  176: 	if ($temp eq 'opendate') {
  177:            $date=&Apache::lonnet::EXT("resource.$id.duedate")-$date;
  178:         }
  179:         if ($temp eq 'answerdate') {
  180:            $date=&Apache::lonnet::EXT("resource.$id.duedate")+$date;
  181:         }
  182:     }
  183:     &Apache::lonxml::debug("found :$date: for :$temp:");
  184:     if ($date eq '') {
  185:       $date = "an unknown date"; $passed = 0;
  186:     } elsif ($date eq 'con_lost') {
  187:       $date = "an indeterminate date"; $passed = 0;
  188:     } else {
  189:       if (time < $date) { $passed = 0; } else { $passed = 1; }
  190:       $date = localtime $date;
  191:     }
  192:     if (!$passed) { $type=$temp; last; }
  193:   }
  194:   &Apache::lonxml::debug("have :$type:$passed:");
  195:   if ($passed) {
  196:     $status='SHOW_ANSWER';
  197:     $datemsg=$date;
  198:   } elsif ($type eq 'opendate') {
  199:     $status='CLOSED';
  200:     $datemsg = "will open on $date";
  201:   } elsif ($type eq 'duedate') {
  202:     $status='CAN_ANSWER';
  203:     $datemsg = "is due at $date";
  204:   } elsif ($type eq 'answerdate') {
  205:     $status='CLOSED';
  206:     $datemsg = "was due on $lastdate, and answers will be available on $date";
  207:   }
  208:   if ($status eq 'CAN_ANSWER') {
  209:     #check #tries
  210:     my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
  211:     my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
  212:     if ( $tries eq '' ) { $tries = '0'; }
  213:     if ( $maxtries eq '' ) { $maxtries = '2'; } 
  214:     if ($tries >= $maxtries) { $status = 'CANNOT_ANSWER'; } 
  215:   }
  216: 
  217:   if (($status ne 'CLOSED') && ($Apache::lonhomework::type eq 'exam') &&
  218:       (!$Apache::lonhomework::history{"resource.0.outtoken"})) {
  219:       return ('UNCHECKEDOUT','needs to be checked out');
  220:   }
  221: 
  222: 
  223:   &Apache::lonxml::debug("sending back :$status:$datemsg:");
  224:   if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) {
  225:     &Apache::lonxml::debug("should be allowed to browse a resource when closed");
  226:     $status='CAN_ANSWER';
  227:     $datemsg='is closed but you are allowed to view it';
  228:   }
  229: 
  230:   return ($status,$datemsg);
  231: }
  232: 
  233: sub showhash {
  234:   my (%hash) = @_;
  235:   &showhashsubset(\%hash,'.');
  236:   return '';
  237: }
  238: 
  239: sub showarray {
  240:     my ($array)=@_;
  241:     my $string="(";
  242:     foreach my $elm (@{ $array }) {
  243: 	if (ref($elm)) {
  244: 	    if ($elm =~ /ARRAY/ ) {
  245: 		$string.=&showarray($elm);
  246: 	    }
  247: 	} else {
  248: 	    $string.="$elm,"
  249: 	}
  250:     }
  251:     chop($string);
  252:     $string.=")";
  253:     return $string;
  254: }
  255: 
  256: sub showhashsubset {
  257:   my ($hash,$keyre) = @_;
  258:   my $resultkey;
  259:   foreach $resultkey (sort keys %$hash) {
  260:     if ($resultkey =~ /$keyre/) {
  261:       if (ref($$hash{$resultkey})) {
  262: 	if ($$hash{$resultkey} =~ /ARRAY/ ) {
  263: 	    &Apache::lonxml::debug("$resultkey ---- ".
  264: 				   &showarray($$hash{$resultkey}));
  265: 	} elsif ($$hash{$resultkey} =~ /HASH/ ) {
  266: 	    &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
  267: 	    &showhashsubset($$hash{$resultkey},'.');
  268: 	} else {
  269: 	    &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
  270: 	}
  271:       } else {
  272: 	&Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
  273:       }
  274:     }
  275:   }
  276:   &Apache::lonxml::debug("\n<br />restored values^</br>\n");
  277:   return '';
  278: }
  279: 
  280: sub setuppermissions {
  281:   $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
  282:   $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
  283:   return ''
  284: }
  285: 
  286: sub setupheader {
  287:   my $request=$_[0];
  288:   if ($ENV{'browser.mathml'}) {
  289:     $request->content_type('text/xml');
  290:   } else {
  291:     $request->content_type('text/html');
  292:   }
  293:   if (!$Apache::lonxml::debug && ($ENV{'REQUEST_METHOD'} eq 'GET')) {
  294:     &Apache::loncommon::no_cache($request);
  295:   }
  296:   $request->send_http_header;
  297:   return OK if $request->header_only;
  298:   return ''
  299: }
  300: 
  301: sub handle_save_or_undo {
  302:   my ($request,$problem,$result) = @_;
  303:   my $file    = &Apache::lonnet::filelocation("",$request->uri);
  304:   my $filebak =$file.".bak";
  305:   my $filetmp =$file.".tmp";
  306:   my $error=0;
  307: 
  308:   if ($ENV{'form.Undo'} eq 'undo') {
  309:     my $error=0;
  310:     if (!copy($file,$filetmp)) { $error=1; }
  311:     if ((!$error) && (!copy($filebak,$file))) { $error=1; }
  312:     if ((!$error) && (!move($filetmp,$filebak))) { $error=1; }
  313:     if (!$error) {
  314:       $request->print("<p><b>Undid changes, Switched $filebak and $file</b></p>");
  315:     } else {
  316:       $request->print("<p><font color=\"red\" size=\"+1\"><b>Unable to undo, unable to switch $filebak and $file</b></font></p>");
  317:       $error=1;
  318:     }
  319:   } else {
  320:     my $fs=Apache::File->new(">$filebak");
  321:     if (defined($fs)) {
  322:       print $fs $$problem;
  323:       $request->print("<b>Making Backup to $filebak</b><br />");
  324:     } else {
  325:       $request->print("<font color=\"red\" size=\"+1\"><b>Unable to make backup $filebak</b></font>");
  326:       $error=2;
  327:     }
  328:     my $fh=Apache::File->new(">$file");
  329:     if (defined($fh)) {
  330:       print $fh $$result;
  331:       $request->print("<b>Saving Modifications to $file</b><br />");
  332:     } else {
  333:       $request->print("<font color=\"red\" size=\"+1\"><b>Unable to write to $file</b></font>");
  334:       $error|=4;
  335:     }
  336:   }
  337:   return $error;
  338: }
  339: 
  340: sub analyze_header {
  341:     my ($request) = @_;
  342:     my $result.='<html>
  343:             <head><title>Analyzing a problem</title></head>
  344:             <body bgcolor="#FFFFFF">
  345:             <form name="lonhomework" method="POST" action="'.
  346: 	      $ENV{'request.uri'}.'">
  347:             <input type="submit" name="problemmode" value="EditXML" />
  348:             <input type="submit" name="problemmode" value="Edit" />
  349:             <hr />
  350:             <input type="submit" name="submit" value="View" />
  351:             <hr />
  352:             List of possible answers:
  353:             </form>';
  354:     $request->print($result);
  355:     $request->rflush();
  356: }
  357: 
  358: sub analyze_footer {
  359:     my ($request) = @_;
  360:     my $result='</body></html>';
  361:     $request->print($result);
  362:     $request->rflush();
  363: }
  364: 
  365: sub analyze {
  366:     my ($request,$file) = @_;
  367:     &Apache::lonxml::debug("Analyze");
  368:     my $result;
  369:     my %overall;
  370:     my %allparts;
  371:     my $rndseed=$ENV{'form.rndseed'};
  372:     &analyze_header($request);
  373:     my %prog_state=
  374: 	&Apache::lonhtmlcommon::Create_PrgWin($request,'Analyze Progress',
  375: 					      'Getting Problem Variants',
  376: 					      $ENV{'form.numtoanalyze'});
  377:     for(my $i=1;$i<$ENV{'form.numtoanalyze'}+1;$i++) {
  378: 	&Apache::lonhtmlcommon::Increment_PrgWin($request,\%prog_state,
  379: 						 'last problem');
  380: 	my $subresult=&Apache::lonnet::ssi($request->uri,
  381: 					   ('grade_target' => 'analyze'),
  382: 					   ('rndseed' => $i));
  383: 	&Apache::lonxml::debug(":$subresult:");
  384: 	(my $garbage,$subresult)=split(/_HASH_REF__/,$subresult,2);
  385: 	my %analyze=&Apache::lonnet::str2hash($subresult);
  386: 	my @parts;
  387: 	if (defined(@{ $analyze{'parts'} })) {
  388: 	    @parts=@{ $analyze{'parts'} };
  389: 	}
  390: 	foreach my $part (@parts) {
  391: 	    if (!exists($allparts{$part})) {$allparts{$part}=1;};
  392: 	    if ($analyze{$part.'.type'} eq 'numericalresponse'	||
  393: 		$analyze{$part.'.type'} eq 'stringresponse'	||
  394: 		$analyze{$part.'.type'} eq 'formularesponse'   ) {
  395: 		push( @{ $overall{$part.'.answer'} },
  396: 		      [@{ $analyze{$part.'.answer'} }]);
  397: 	    }
  398: 	}
  399:     }
  400:     &Apache::lonhtmlcommon::Update_PrgWin($request,\%prog_state,
  401: 					  'Analyzing Results');
  402:     foreach my $part (keys(%allparts)) {
  403: 	if (defined(@{ $overall{$part.'.answer'} })) {
  404: 	    $request->print('<table><tr><td>Part '.$part.'</td></tr>');
  405: 	    foreach my $answer (sort {$a->[0] <=> $b->[0]} (@{ $overall{$part.'.answer'} })) {
  406: 		$request->print('<tr><td>'.join('</td><td>',@{ $answer }).
  407: 				'</td></tr>');
  408: 	    }
  409: 	    $request->print('</table>');
  410: 	} else {
  411: 	    $request->print('<p>Part '.$part.
  412: 			    ' is not analyzabale at this time</p>');
  413: 	}
  414:     }
  415:     &Apache::lonhtmlcommon::Close_PrgWin($request,\%prog_state);
  416:     &analyze_footer($request);
  417:     &Apache::lonhomework::showhash(%overall);
  418:     return $result;
  419: }
  420: 
  421: sub editxmlmode {
  422:   my ($request,$file) = @_;
  423:   my $result;
  424:   my $problem=&Apache::lonnet::getfile($file);
  425:   if ($problem eq -1) {
  426:     &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
  427:     $problem='';
  428:   }
  429:   if (defined($ENV{'form.editxmltext'}) || defined($ENV{'form.Undo'})) {
  430:     my $error=&handle_save_or_undo($request,\$problem,
  431: 				   \$ENV{'form.editxmltext'});
  432:     if (!$error) { $problem=&Apache::lonnet::getfile($file); }
  433:   }
  434:   &Apache::lonhomework::showhashsubset(\%ENV,'^form');
  435:   if ( $ENV{'form.submit'} eq 'Submit Changes and View' ) {
  436:     &Apache::lonhomework::showhashsubset(\%ENV,'^form');
  437:     $ENV{'form.problemmode'}='View';
  438:     &renderpage($request,$file);
  439:   } else {
  440:     my ($rows,$cols) = &Apache::edit::textarea_sizes(\$problem);
  441:     my $xml_help = '<table><tr><td>'.
  442: 	&Apache::loncommon::help_open_topic("Problem_Editor_XML_Index",'Problem Editing Help')
  443: 	    .'</td><td>'.
  444: 	&Apache::loncommon::help_open_topic("Greek_Symbols",'Greek Symbols',
  445: 					    undef,undef,600)
  446: 	    .'</td><td>'.
  447:         &Apache::loncommon::help_open_topic("Other_Symbols",'Other Symbols',
  448: 					    undef,undef,600)
  449: 	    .'</td></tr></table>';
  450:     if ($cols > 80) { $cols = 80; }
  451:     if ($cols < 70) { $cols = 70; }
  452:     if ($rows < 20) { $rows = 20; }
  453:     $result.='<html><body bgcolor="#FFFFFF">
  454:             <form name="lonhomework" method="POST" action="'.
  455: 	      $ENV{'request.uri'}.'">
  456:             <input type="hidden" name="problemmode" value="EditXML" />
  457:             <input type="submit" name="problemmode" value="Discard Edits and View" />
  458:             <input type="submit" name="problemmode" value="Edit" />
  459:             <hr />
  460:             <input type="submit" name="submit" value="Submit Changes" />
  461:             <input type="submit" name="submit" value="Submit Changes and View" />
  462:             <input type="submit" name="Undo" value="undo" />
  463:             <hr />
  464:             ' . $xml_help . '
  465:             <textarea rows="'.$rows.'" cols="'.$cols.'" name="editxmltext">'.
  466: 	      &HTML::Entities::encode($problem).'</textarea>
  467:             </form></body></html>';
  468:     $request->print($result);
  469:   }
  470:   return '';
  471: }
  472: 
  473: sub renderpage {
  474:   my ($request,$file) = @_;
  475: 
  476:   my (@targets) = &get_target();
  477:   &Apache::lonxml::debug("Running targets ".join(':',@targets));
  478:   foreach my $target (@targets) {
  479:     #my $t0 = [&gettimeofday()];
  480:     my $problem=&Apache::lonnet::getfile($file);
  481:     if ($problem eq -1) {
  482:       &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
  483:       $problem='';
  484:     }
  485: 
  486:     my %mystyle;
  487:     my $result = '';
  488:     if ($target eq 'analyze') { %Apache::lonhomework::analyze=(); }
  489:     if ($target eq 'answer') { &showhash(%Apache::lonhomework::history); }
  490:     if ($target eq 'web') {&Apache::lonhomework::showhashsubset(\%ENV,'^form');}
  491: 
  492:     &Apache::lonxml::debug("Should be parsing now");
  493:     $result = &Apache::lonxml::xmlparse($request, $target, $problem,
  494: 			&setup_vars($target),%mystyle);
  495:     undef($Apache::lonhomework::parsing_a_problem);
  496:     #$request->print("Result follows:");
  497:     if ($target eq 'modified') {
  498:       &handle_save_or_undo($request,\$problem,\$result);
  499:     } else {
  500:       if ($target eq 'analyze') {
  501: 	$result=&Apache::lonnet::hashref2str(\%Apache::lonhomework::analyze);
  502: 	undef(%Apache::lonhomework::analyze);
  503:       }
  504:       #my $td=&tv_interval($t0);
  505:       #if ( $Apache::lonxml::debug) {
  506: 	#$result =~ s:</body>::;
  507: 	#$result.="<br />Spent $td seconds processing target $target\n</body>";
  508:       #}
  509:       $request->print($result);
  510:       $request->rflush();
  511:     }
  512:     #$request->print(":Result ends");
  513:     #my $td=&tv_interval($t0);
  514:   }
  515: }
  516: 
  517: # with no arg it returns a HTML <option> list of the template titles
  518: # with one arg it returns the filename associated with the arg passed
  519: sub get_template_list {
  520:   my ($namewanted,$extension) = @_;
  521:   my $result;
  522:   my @allnames;
  523:   &Apache::lonxml::debug("Looking for :$extension:");
  524:   foreach my $file (</home/httpd/html/res/adm/includes/templates/*.$extension>) {
  525:     my $name=&Apache::lonnet::metadata($file,'title');
  526:     if ($namewanted && ($name eq $namewanted)) {
  527:       $result=$file;
  528:       last;
  529:     } else {
  530: 	if ($name) { push (@allnames, $name); }
  531:     }
  532:   }
  533:   if (@allnames && !$result) {
  534:     $result="<option>Select a $extension template</option>\n<option>".
  535:       join('</option><option>',sort(@allnames)).'</option>';
  536:   }
  537:   return $result;
  538: }
  539: 
  540: sub newproblem {
  541:     my ($request) = @_;
  542:     my $extension=$request->uri;
  543:     $extension=~s:^.*\.([\w]+)$:$1:;
  544:     &Apache::lonxml::debug("Looking for :$extension:");
  545:     if ($ENV{'form.template'} &&
  546: 	$ENV{'form.template'} ne "Select a $extension type") {
  547: 	use File::Copy;
  548: 	my $file = &get_template_list($ENV{'form.template'},$extension);
  549: 	my $dest = &Apache::lonnet::filelocation("",$request->uri);
  550: 	copy($file,$dest);
  551: 	&renderpage($request,$dest);
  552:     } elsif($ENV{'form.newfile'}) {
  553: 	# I don't like hard-coded filenames but for now, this will work.
  554: 	use File::Copy;
  555: 	my $templatefilename = 
  556: 	    $request->dir_config('lonIncludes').'/templates/blank.problem';
  557: 	&Apache::lonxml::debug("$templatefilename");
  558: 	my $dest = &Apache::lonnet::filelocation("",$request->uri);
  559: 	copy($templatefilename,$dest);
  560: 	&renderpage($request,$dest);
  561:     } else {
  562: 	my $templatelist=&get_template_list('',$extension);
  563: 	my $url=$request->uri;
  564: 	my $dest = &Apache::lonnet::filelocation("",$request->uri);
  565: 	my $instructions;
  566: 	if ($templatelist) { $instructions=", select a template from the pull-down menu below.<br />Then";}
  567: 	$request->print(<<ENDNEWPROBLEM);
  568: <body bgcolor="#FFFFFF">
  569: <h1>Creating a new $extension resource</h1>
  570: The requested file <tt>$url</tt> currently does not exist.
  571: <p>
  572: To create a new $extension$instructions click on the "Create $extension" button.
  573: </p>
  574: <p><form action="$url" method="POST">
  575: ENDNEWPROBLEM
  576: 	if (defined($templatelist)) {
  577: 	    $request->print("<select name=\"template\">$templatelist</select>");
  578: 	}
  579: 	$request->print("<br /><input type=\"submit\" name=\"newfile\" value=\"Create $extension\" />");
  580: 	$request->print("</form></p></body>");
  581:     }
  582:     return '';
  583: }
  584: 
  585: sub view_or_edit_menu {
  586:   my ($request) = @_;
  587:   my $url=$request->uri;
  588:   $request->print(<<EDITMENU);
  589: <body bgcolor="#FFFFFF">
  590: <form action="$url" method="POST">
  591: Would you like to <input type="submit" name="problemmode" value="View"> or
  592: <input type="submit" name="problemmode" value="Edit"> the problem.
  593: </form>
  594: </body>
  595: EDITMENU
  596: }
  597: 
  598: sub handler {
  599:   #my $t0 = [&gettimeofday()];
  600:   my $request=$_[0];
  601: 
  602: #  if ( $ENV{'user.name'} eq 'albertel' ) {$Apache::lonxml::debug=1;}
  603:   $Apache::lonxml::debug=$ENV{'user.debug'};
  604: 
  605:   if (&setupheader($request)) { return OK; }
  606:   $ENV{'request.uri'}=$request->uri;
  607: 
  608:   #setup permissions
  609:   $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
  610:   $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
  611:   &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:");
  612:   # some times multiple problemmodes are submitted, need to select
  613:   # the last one
  614:   &Apache::lonxml::debug("Problem Mode ".$ENV{'form.problemmode'});
  615:   if ( defined($ENV{'form.problemmode'}) &&
  616:        ref($ENV{'form.problemmode'}) ) {
  617:     &Apache::lonxml::debug("Problem Mode ".join(",",@$ENV{'form.problemmode'}));
  618:     my $mode=$ENV{'form.problemmode'}->[-1];
  619:     undef $ENV{'form.problemmode'};
  620:     $ENV{'form.problemmode'}=$mode;
  621:   }
  622:   &Apache::lonxml::debug("Problem Mode ".$ENV{'form.problemmode'});
  623:   my $file=&Apache::lonnet::filelocation("",$request->uri);
  624: 
  625:   #check if we know where we are
  626:   if ($ENV{'request.course.fn'} && !&Apache::lonnet::symbread()) { 
  627:     # if we are browsing we might not be able to know where we are
  628:     if ($Apache::lonhomework::browse ne 'F') {
  629:       #should know where we are, so ask
  630:       $request->internal_redirect('/adm/ambiguous'); return;
  631:     }
  632:   }
  633: 
  634:   if ($ENV{'request.state'} eq "construct") {
  635:     if ($ENV{'form.resetdata'} eq 'Reset Submissions') {
  636:       my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
  637:       &Apache::lonnet::tmpreset($symb,'',$domain,$name);
  638:     }
  639:     if ( -e $file ) {
  640:       if (!(defined $ENV{'form.problemmode'})) {
  641: 	#first visit to problem in construction space
  642: 	#&view_or_edit_menu($request);
  643: 	$ENV{'form.problemmode'}='View';
  644: 	&renderpage($request,$file);
  645:       } elsif ($ENV{'form.problemmode'} eq 'EditXML') {
  646: 	&editxmlmode($request,$file);
  647:       } elsif ($ENV{'form.problemmode'} eq 'Answer Distribution') {
  648: 	&analyze($request,$file);
  649:       } else {
  650: 	&renderpage($request,$file);
  651:       }
  652:     } else {
  653:       # requested file doesn't exist in contruction space
  654:       &newproblem($request);
  655:     }
  656:   } else {
  657:     # just render the page normally outside of construction space
  658:     &Apache::lonxml::debug("not construct");
  659:     &renderpage($request,$file);
  660:   }
  661:   #my $td=&tv_interval($t0);
  662:   #&Apache::lonxml::debug("Spent $td seconds processing");
  663:   # &Apache::lonhomework::send_footer($request);
  664:   # always turn off debug messages
  665:   $Apache::lonxml::debug=0;
  666:   return OK;
  667: 
  668: }
  669: 
  670: 1;
  671: __END__

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