File:  [LON-CAPA] / loncom / homework / lonhomework.pm
Revision 1.122: download - view: text, annotated - select for diffs
Fri May 9 17:01:08 2003 UTC (20 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
-chemresponse not ready to go

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

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