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

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

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