File:  [LON-CAPA] / loncom / homework / lonhomework.pm
Revision 1.128: download - view: text, annotated - select for diffs
Fri May 16 17:41:17 2003 UTC (20 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- fixes #1449, selecting no problem template throws an error message and asks again.

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

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