File:  [LON-CAPA] / loncom / homework / lonhomework.pm
Revision 1.120: download - view: text, annotated - select for diffs
Wed Apr 30 21:27:57 2003 UTC (21 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
 trying to set document encoding of homework documents to utf8, this might break things, let me know

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

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