File:  [LON-CAPA] / loncom / homework / lonhomework.pm
Revision 1.129: download - view: text, annotated - select for diffs
Fri May 16 19:01:27 2003 UTC (20 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- if the student is excused, it should not allow one to answer the problem (BUG# 1443)

    1: # The LearningOnline Network with CAPA
    2: # The LON-CAPA Homework handler
    3: #
    4: # $Id: lonhomework.pm,v 1.129 2003/05/16 19:01:27 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 (correct and show prob status) or excused then CANNOT_ANSWER
  219:     if(($Apache::lonhomework::history{"resource.$id.solved"}=~/^correct/
  220: 	&&
  221: 	lc($Apache::lonhomework::problemstatus) ne 'no')
  222:        ||
  223:        $Apache::lonhomework::history{"resource.$id.solved"}=~/^excused/) {
  224: 	$status = 'CANNOT_ANSWER';
  225:     }
  226:   }
  227: 
  228:   if (($status ne 'CLOSED') && ($Apache::lonhomework::type eq 'exam') &&
  229:       (!$Apache::lonhomework::history{"resource.0.outtoken"})) {
  230:       return ('UNCHECKEDOUT','needs to be checked out');
  231:   }
  232: 
  233: 
  234:   &Apache::lonxml::debug("sending back :$status:$datemsg:");
  235:   if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) {
  236:     &Apache::lonxml::debug("should be allowed to browse a resource when closed");
  237:     $status='CAN_ANSWER';
  238:     $datemsg='is closed but you are allowed to view it';
  239:   }
  240: 
  241:   return ($status,$datemsg);
  242: }
  243: 
  244: sub showhash {
  245:   my (%hash) = @_;
  246:   &showhashsubset(\%hash,'.');
  247:   return '';
  248: }
  249: 
  250: sub showarray {
  251:     my ($array)=@_;
  252:     my $string="(";
  253:     foreach my $elm (@{ $array }) {
  254: 	if (ref($elm)) {
  255: 	    if ($elm =~ /ARRAY/ ) {
  256: 		$string.=&showarray($elm);
  257: 	    }
  258: 	} else {
  259: 	    $string.="$elm,"
  260: 	}
  261:     }
  262:     chop($string);
  263:     $string.=")";
  264:     return $string;
  265: }
  266: 
  267: sub showhashsubset {
  268:   my ($hash,$keyre) = @_;
  269:   my $resultkey;
  270:   foreach $resultkey (sort keys %$hash) {
  271:     if ($resultkey =~ /$keyre/) {
  272:       if (ref($$hash{$resultkey})) {
  273: 	if ($$hash{$resultkey} =~ /ARRAY/ ) {
  274: 	    &Apache::lonxml::debug("$resultkey ---- ".
  275: 				   &showarray($$hash{$resultkey}));
  276: 	} elsif ($$hash{$resultkey} =~ /HASH/ ) {
  277: 	    &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
  278: 	    &showhashsubset($$hash{$resultkey},'.');
  279: 	} else {
  280: 	    &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
  281: 	}
  282:       } else {
  283: 	&Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
  284:       }
  285:     }
  286:   }
  287:   &Apache::lonxml::debug("\n<br />restored values^</br>\n");
  288:   return '';
  289: }
  290: 
  291: sub setuppermissions {
  292:   $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
  293:   my $viewgrades = &Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
  294:   if (! $viewgrades && 
  295:       exists($ENV{'request.course.sec'}) && 
  296:       $ENV{'request.course.sec'} !~ /^\s*$/) {
  297:       $viewgrades = &Apache::lonnet::allowed('vgr',$ENV{'request.course.id'}.
  298:                                                '/'.$ENV{'request.course.sec'});
  299:   }
  300:   $Apache::lonhomework::viewgrades = $viewgrades;
  301:   return ''
  302: }
  303: 
  304: sub setupheader {
  305:     my $request=$_[0];
  306:     if ($ENV{'browser.mathml'}) {
  307: 	$request->content_type('text/xml');
  308:     } else {
  309: 	$request->content_type('text/html');
  310:     }
  311:     $request->content_encoding('UTF-8');
  312:     if (!$Apache::lonxml::debug && ($ENV{'REQUEST_METHOD'} eq 'GET')) {
  313: 	&Apache::loncommon::no_cache($request);
  314:     }
  315:     $request->send_http_header;
  316:     return OK if $request->header_only;
  317:     return ''
  318: }
  319: 
  320: sub handle_save_or_undo {
  321:   my ($request,$problem,$result) = @_;
  322:   my $file    = &Apache::lonnet::filelocation("",$request->uri);
  323:   my $filebak =$file.".bak";
  324:   my $filetmp =$file.".tmp";
  325:   my $error=0;
  326: 
  327:   if ($ENV{'form.Undo'} eq 'undo') {
  328:     my $error=0;
  329:     if (!copy($file,$filetmp)) { $error=1; }
  330:     if ((!$error) && (!copy($filebak,$file))) { $error=1; }
  331:     if ((!$error) && (!move($filetmp,$filebak))) { $error=1; }
  332:     if (!$error) {
  333:       $request->print("<p><b>Undid changes, Switched $filebak and $file</b></p>");
  334:     } else {
  335:       $request->print("<p><font color=\"red\" size=\"+1\"><b>Unable to undo, unable to switch $filebak and $file</b></font></p>");
  336:       $error=1;
  337:     }
  338:   } else {
  339:     my $fs=Apache::File->new(">$filebak");
  340:     if (defined($fs)) {
  341:       print $fs $$problem;
  342:       $request->print("<b>Making Backup to $filebak</b><br />");
  343:     } else {
  344:       $request->print("<font color=\"red\" size=\"+1\"><b>Unable to make backup $filebak</b></font>");
  345:       $error=2;
  346:     }
  347:     my $fh=Apache::File->new(">$file");
  348:     if (defined($fh)) {
  349:       print $fh $$result;
  350:       $request->print("<b>Saving Modifications to $file</b><br />");
  351:     } else {
  352:       $request->print("<font color=\"red\" size=\"+1\"><b>Unable to write to $file</b></font>");
  353:       $error|=4;
  354:     }
  355:   }
  356:   return $error;
  357: }
  358: 
  359: sub analyze_header {
  360:     my ($request) = @_;
  361:     my $result.='<html>
  362:             <head><title>Analyzing a problem</title></head>
  363:             <body bgcolor="#FFFFFF">
  364:             <form name="lonhomework" method="POST" action="'.
  365: 	      $ENV{'request.uri'}.'">
  366:             <input type="submit" name="problemmode" value="EditXML" />
  367:             <input type="submit" name="problemmode" value="Edit" />
  368:             <hr />
  369:             <input type="submit" name="submit" value="View" />
  370:             <hr />
  371:             List of possible answers:
  372:             </form>';
  373:     $request->print($result);
  374:     $request->rflush();
  375: }
  376: 
  377: sub analyze_footer {
  378:     my ($request) = @_;
  379:     my $result='</body></html>';
  380:     $request->print($result);
  381:     $request->rflush();
  382: }
  383: 
  384: sub analyze {
  385:     my ($request,$file) = @_;
  386:     &Apache::lonxml::debug("Analyze");
  387:     my $result;
  388:     my %overall;
  389:     my %allparts;
  390:     my $rndseed=$ENV{'form.rndseed'};
  391:     &analyze_header($request);
  392:     my %prog_state=
  393: 	&Apache::lonhtmlcommon::Create_PrgWin($request,'Analyze Progress',
  394: 					      'Getting Problem Variants',
  395: 					      $ENV{'form.numtoanalyze'});
  396:     for(my $i=1;$i<$ENV{'form.numtoanalyze'}+1;$i++) {
  397: 	&Apache::lonhtmlcommon::Increment_PrgWin($request,\%prog_state,
  398: 						 'last problem');
  399: 	my $subresult=&Apache::lonnet::ssi($request->uri,
  400: 					   ('grade_target' => 'analyze'),
  401: 					   ('rndseed' => $i));
  402: 	&Apache::lonxml::debug(":$subresult:");
  403: 	(my $garbage,$subresult)=split(/_HASH_REF__/,$subresult,2);
  404: 	my %analyze=&Apache::lonnet::str2hash($subresult);
  405: 	my @parts;
  406: 	if (defined(@{ $analyze{'parts'} })) {
  407: 	    @parts=@{ $analyze{'parts'} };
  408: 	}
  409: 	foreach my $part (@parts) {
  410: 	    if (!exists($allparts{$part})) {$allparts{$part}=1;};
  411: 	    if ($analyze{$part.'.type'} eq 'numericalresponse'	||
  412: 		$analyze{$part.'.type'} eq 'stringresponse'	||
  413: 		$analyze{$part.'.type'} eq 'formularesponse'   ) {
  414: 		push( @{ $overall{$part.'.answer'} },
  415: 		      [@{ $analyze{$part.'.answer'} }]);
  416: 	    }
  417: 	}
  418:     }
  419:     &Apache::lonhtmlcommon::Update_PrgWin($request,\%prog_state,
  420: 					  'Analyzing Results');
  421:     foreach my $part (keys(%allparts)) {
  422: 	if (defined(@{ $overall{$part.'.answer'} })) {
  423: 	    $request->print('<table><tr><td>Part '.$part.'</td></tr>');
  424: 	    foreach my $answer (sort {$a->[0] <=> $b->[0]} (@{ $overall{$part.'.answer'} })) {
  425: 		$request->print('<tr><td>'.join('</td><td>',@{ $answer }).
  426: 				'</td></tr>');
  427: 	    }
  428: 	    $request->print('</table>');
  429: 	} else {
  430: 	    $request->print('<p>Part '.$part.
  431: 			    ' is not analyzabale at this time</p>');
  432: 	}
  433:     }
  434:     &Apache::lonhtmlcommon::Close_PrgWin($request,\%prog_state);
  435:     &analyze_footer($request);
  436:     &Apache::lonhomework::showhash(%overall);
  437:     return $result;
  438: }
  439: 
  440: sub editxmlmode {
  441:   my ($request,$file) = @_;
  442:   my $result;
  443:   my $problem=&Apache::lonnet::getfile($file);
  444:   if ($problem eq -1) {
  445:     &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
  446:     $problem='';
  447:   }
  448:   if (defined($ENV{'form.editxmltext'}) || defined($ENV{'form.Undo'})) {
  449:     my $error=&handle_save_or_undo($request,\$problem,
  450: 				   \$ENV{'form.editxmltext'});
  451:     if (!$error) { $problem=&Apache::lonnet::getfile($file); }
  452:   }
  453:   &Apache::lonhomework::showhashsubset(\%ENV,'^form');
  454:   if ( $ENV{'form.submit'} eq 'Submit Changes and View' ) {
  455:     &Apache::lonhomework::showhashsubset(\%ENV,'^form');
  456:     $ENV{'form.problemmode'}='View';
  457:     &renderpage($request,$file);
  458:   } else {
  459:     my ($rows,$cols) = &Apache::edit::textarea_sizes(\$problem);
  460:     my $xml_help = '<table><tr><td>'.
  461: 	&Apache::loncommon::help_open_topic("Problem_Editor_XML_Index",'Problem Editing Help')
  462: 	    .'</td><td>'.
  463: 	&Apache::loncommon::help_open_topic("Greek_Symbols",'Greek Symbols',
  464: 					    undef,undef,600)
  465: 	    .'</td><td>'.
  466:         &Apache::loncommon::help_open_topic("Other_Symbols",'Other Symbols',
  467: 					    undef,undef,600)
  468: 	    .'</td></tr></table>';
  469:     if ($cols > 80) { $cols = 80; }
  470:     if ($cols < 70) { $cols = 70; }
  471:     if ($rows < 20) { $rows = 20; }
  472:     $result.='<html><body bgcolor="#FFFFFF">
  473:             <form name="lonhomework" method="POST" action="'.
  474: 	      $ENV{'request.uri'}.'">
  475:             <input type="hidden" name="problemmode" value="EditXML" />
  476:             <input type="submit" name="problemmode" value="Discard Edits and View" />
  477:             <input type="submit" name="problemmode" value="Edit" />
  478:             <hr />
  479:             <input type="submit" name="submit" value="Submit Changes" />
  480:             <input type="submit" name="submit" value="Submit Changes and View" />
  481:             <input type="submit" name="Undo" value="undo" />
  482:             <hr />
  483:             ' . $xml_help . '
  484:             <textarea rows="'.$rows.'" cols="'.$cols.'" name="editxmltext">'.
  485: 	      &HTML::Entities::encode($problem).'</textarea>
  486:             </form></body></html>';
  487:     $request->print($result);
  488:   }
  489:   return '';
  490: }
  491: 
  492: sub renderpage {
  493:   my ($request,$file) = @_;
  494: 
  495:   my (@targets) = &get_target();
  496:   &Apache::lonxml::debug("Running targets ".join(':',@targets));
  497:   foreach my $target (@targets) {
  498:     #my $t0 = [&gettimeofday()];
  499:     my $problem=&Apache::lonnet::getfile($file);
  500:     if ($problem eq -1) {
  501:       &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
  502:       $problem='';
  503:     }
  504: 
  505:     my %mystyle;
  506:     my $result = '';
  507:     if ($target eq 'analyze') { %Apache::lonhomework::analyze=(); }
  508:     if ($target eq 'answer') { &showhash(%Apache::lonhomework::history); }
  509:     if ($target eq 'web') {&Apache::lonhomework::showhashsubset(\%ENV,'^form');}
  510: 
  511:     &Apache::lonxml::debug("Should be parsing now");
  512:     $result = &Apache::lonxml::xmlparse($request, $target, $problem,
  513: 			&setup_vars($target),%mystyle);
  514:     undef($Apache::lonhomework::parsing_a_problem);
  515:     #$request->print("Result follows:");
  516:     if ($target eq 'modified') {
  517:       &handle_save_or_undo($request,\$problem,\$result);
  518:     } else {
  519:       if ($target eq 'analyze') {
  520: 	$result=&Apache::lonnet::hashref2str(\%Apache::lonhomework::analyze);
  521: 	undef(%Apache::lonhomework::analyze);
  522:       }
  523:       #my $td=&tv_interval($t0);
  524:       #if ( $Apache::lonxml::debug) {
  525: 	#$result =~ s:</body>::;
  526: 	#$result.="<br />Spent $td seconds processing target $target\n</body>";
  527:       #}
  528:       $request->print($result);
  529:       $request->rflush();
  530:     }
  531:     #$request->print(":Result ends");
  532:     #my $td=&tv_interval($t0);
  533:   }
  534: }
  535: 
  536: # with no arg it returns a HTML <option> list of the template titles
  537: # with one arg it returns the filename associated with the arg passed
  538: sub get_template_list {
  539:   my ($namewanted,$extension) = @_;
  540:   my $result;
  541:   my @allnames;
  542:   &Apache::lonxml::debug("Looking for :$extension:");
  543:   foreach my $file (</home/httpd/html/res/adm/includes/templates/*.$extension>) {
  544:     my $name=&Apache::lonnet::metadata($file,'title');
  545:     if ($namewanted && ($name eq $namewanted)) {
  546:       $result=$file;
  547:       last;
  548:     } else {
  549: 	if ($name) { push (@allnames, $name); }
  550:     }
  551:   }
  552:   if (@allnames && !$result) {
  553:     $result="<option>Select a $extension template</option>\n<option>".
  554: 	join('</option><option>',sort(@allnames)).'</option>';
  555:   }
  556:   return $result;
  557: }
  558: 
  559: sub newproblem {
  560:     my ($request) = @_;
  561:     my $extension=$request->uri;
  562:     $extension=~s:^.*\.([\w]+)$:$1:;
  563:     &Apache::lonxml::debug("Looking for :$extension:");
  564:     if ($ENV{'form.template'} &&
  565: 	$ENV{'form.template'} ne "Select a $extension template") {
  566: 	use File::Copy;
  567: 	my $file = &get_template_list($ENV{'form.template'},$extension);
  568: 	my $dest = &Apache::lonnet::filelocation("",$request->uri);
  569: 	copy($file,$dest);
  570: 	&renderpage($request,$dest);
  571:     } else {
  572: 	my $templatelist=&get_template_list('',$extension);
  573: 	my $url=$request->uri;
  574: 	my $dest = &Apache::lonnet::filelocation("",$request->uri);
  575: 	my $errormsg;
  576: 	if ($ENV{'form.newfile'}) {
  577: 	    $errormsg='<p><font color="red">You did not select a template.</font></p>'."\n";
  578: 	}
  579: 	my $instructions;
  580: 	if ($templatelist) { $instructions=", select a template from the pull-down menu below.<br />Then";}
  581: 	$request->print(<<ENDNEWPROBLEM);
  582: <body bgcolor="#FFFFFF">
  583: <h1>Creating a new $extension resource</h1>
  584: $errormsg
  585: The requested file <tt>$url</tt> currently does not exist.
  586: <p>
  587: <b>To create a new $extension$instructions click on the "Create $extension" button.</b>
  588: </p>
  589: <p><form action="$url" method="POST">
  590: ENDNEWPROBLEM
  591: 	if (defined($templatelist)) {
  592: 	    $request->print("<select name=\"template\">$templatelist</select>");
  593: 	}
  594: 	$request->print("<br /><input type=\"submit\" name=\"newfile\" value=\"Create $extension\" />");
  595: 	$request->print("</form></p></body>");
  596:     }
  597:     return '';
  598: }
  599: 
  600: sub view_or_edit_menu {
  601:   my ($request) = @_;
  602:   my $url=$request->uri;
  603:   $request->print(<<EDITMENU);
  604: <body bgcolor="#FFFFFF">
  605: <form action="$url" method="POST">
  606: Would you like to <input type="submit" name="problemmode" value="View"> or
  607: <input type="submit" name="problemmode" value="Edit"> the problem.
  608: </form>
  609: </body>
  610: EDITMENU
  611: }
  612: 
  613: sub handler {
  614:   #my $t0 = [&gettimeofday()];
  615:   my $request=$_[0];
  616: 
  617:   $Apache::lonxml::debug=$ENV{'user.debug'};
  618: 
  619:   if (&setupheader($request)) { return OK; }
  620:   $ENV{'request.uri'}=$request->uri;
  621: 
  622:   #setup permissions
  623:   $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
  624:   $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
  625:   &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:");
  626:   # some times multiple problemmodes are submitted, need to select
  627:   # the last one
  628:   &Apache::lonxml::debug("Problem Mode ".$ENV{'form.problemmode'});
  629:   if ( defined($ENV{'form.problemmode'}) &&
  630:        ref($ENV{'form.problemmode'}) ) {
  631:     &Apache::lonxml::debug("Problem Mode ".join(",",@$ENV{'form.problemmode'}));
  632:     my $mode=$ENV{'form.problemmode'}->[-1];
  633:     undef $ENV{'form.problemmode'};
  634:     $ENV{'form.problemmode'}=$mode;
  635:   }
  636:   &Apache::lonxml::debug("Problem Mode ".$ENV{'form.problemmode'});
  637:   my $file=&Apache::lonnet::filelocation("",$request->uri);
  638: 
  639:   #check if we know where we are
  640:   if ($ENV{'request.course.fn'} && !&Apache::lonnet::symbread()) { 
  641:     # if we are browsing we might not be able to know where we are
  642:     if ($Apache::lonhomework::browse ne 'F') {
  643:       #should know where we are, so ask
  644:       $request->internal_redirect('/adm/ambiguous'); return;
  645:     }
  646:   }
  647: 
  648:   my ($symb) = &Apache::lonxml::whichuser();
  649:   &Apache::lonxml::debug('symb is '.$symb);
  650:   if ($ENV{'request.state'} eq "construct" || $symb eq '') {
  651:       if ($ENV{'form.resetdata'} eq 'Reset Submissions' ||
  652: 	  $ENV{'form.resetdata'} eq 'New Problem Variation' ) {
  653: 	  my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
  654: 	  &Apache::lonnet::tmpreset($symb,'',$domain,$name);
  655:       }
  656:   }
  657:   if ($ENV{'request.state'} eq "construct") {
  658:     if ( -e $file ) {
  659:       if (!(defined $ENV{'form.problemmode'})) {
  660: 	#first visit to problem in construction space
  661: 	#&view_or_edit_menu($request);
  662: 	$ENV{'form.problemmode'}='View';
  663: 	&renderpage($request,$file);
  664:       } elsif ($ENV{'form.problemmode'} eq 'EditXML') {
  665: 	&editxmlmode($request,$file);
  666:       } elsif ($ENV{'form.problemmode'} eq 'Answer Distribution') {
  667: 	&analyze($request,$file);
  668:       } else {
  669: 	&renderpage($request,$file);
  670:       }
  671:     } else {
  672:       # requested file doesn't exist in contruction space
  673:       &newproblem($request);
  674:     }
  675:   } else {
  676:     # just render the page normally outside of construction space
  677:     &Apache::lonxml::debug("not construct");
  678:     &renderpage($request,$file);
  679:   }
  680:   #my $td=&tv_interval($t0);
  681:   #&Apache::lonxml::debug("Spent $td seconds processing");
  682:   # &Apache::lonhomework::send_footer($request);
  683:   # always turn off debug messages
  684:   $Apache::lonxml::debug=0;
  685:   return OK;
  686: 
  687: }
  688: 
  689: 1;
  690: __END__

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