Annotation of loncom/homework/lonhomework.pm, revision 1.119

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

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