File:  [LON-CAPA] / loncom / homework / lonhomework.pm
Revision 1.125: download - view: text, annotated - select for diffs
Tue May 13 01:18:37 2003 UTC (21 years ago) by www
Branches: MAIN
CVS tags: HEAD
Aargh ... mostly rewinding. We don't want the homework to be rendered on the
course's homeserver, or do we?

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

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