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

1.63      albertel    1: # The LearningOnline Network with CAPA
1.52      albertel    2: # The LON-CAPA Homework handler
1.63      albertel    3: #
1.91    ! albertel    4: # $Id: lonhomework.pm,v 1.90 2002/09/24 12:39:42 sakharuk 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: 
                    142: sub check_access {
1.52      albertel  143:   my ($id) = @_;
                    144:   my $date ='';
                    145:   my $status = '';
                    146:   my $datemsg = '';
                    147:   my $lastdate = '';
                    148:   my $temp;
                    149:   my $type;
                    150:   my $passed;
                    151:   &Apache::lonxml::debug("checking for part :$id:");
1.73      albertel  152:   &Apache::lonxml::debug("time:".time);
1.52      albertel  153:   foreach $temp ("opendate","duedate","answerdate") {
                    154:     $lastdate = $date;
                    155:     $date = &Apache::lonnet::EXT("resource.$id.$temp");
1.87      www       156:     my $thistype = &Apache::lonnet::EXT("resource.$id.$temp.type");
                    157:     if ($thistype eq 'date_interval') {
                    158: 	if ($temp eq 'opendate') {
                    159:            $date=&Apache::lonnet::EXT("resource.$id.duedate")-$date;
                    160:         }
                    161:         if ($temp eq 'answerdate') {
                    162:            $date=&Apache::lonnet::EXT("resource.$id.duedate")+$date;
                    163:         }
                    164:     }
1.52      albertel  165:     &Apache::lonxml::debug("found :$date: for :$temp:");
                    166:     if ($date eq '') {
                    167:       $date = "an unknown date"; $passed = 0;
                    168:     } elsif ($date eq 'con_lost') {
                    169:       $date = "an indeterminate date"; $passed = 0;
                    170:     } else {
                    171:       if (time < $date) { $passed = 0; } else { $passed = 1; }
                    172:       $date = localtime $date;
1.51      harris41  173:     }
1.52      albertel  174:     if (!$passed) { $type=$temp; last; }
                    175:   }
                    176:   &Apache::lonxml::debug("have :$type:$passed:");
                    177:   if ($passed) {
                    178:     $status='SHOW_ANSWER';
                    179:     $datemsg=$date;
                    180:   } elsif ($type eq 'opendate') {
                    181:     $status='CLOSED';
                    182:     $datemsg = "will open on $date";
                    183:   } elsif ($type eq 'duedate') {
                    184:     $status='CAN_ANSWER';
                    185:     $datemsg = "is due at $date";
                    186:   } elsif ($type eq 'answerdate') {
                    187:     $status='CLOSED';
                    188:     $datemsg = "was due on $lastdate, and answers will be available on $date";
                    189:   }
                    190:   if ($status eq 'CAN_ANSWER') {
                    191:     #check #tries
                    192:     my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
                    193:     my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
                    194:     if ( $tries eq '' ) { $tries = '0'; }
                    195:     if ( $maxtries eq '' ) { $maxtries = '2'; } 
                    196:     if ($tries >= $maxtries) { $status = 'CANNOT_ANSWER'; } 
                    197:   }
1.54      www       198: 
1.56      www       199:   if (($status ne 'CLOSED') && ($Apache::lonhomework::type eq 'exam') &&
                    200:       (!$Apache::lonhomework::history{"resource.0.outtoken"})) {
1.54      www       201:       return ('UNCHECKEDOUT','needs to be checked out');
                    202:   }
                    203: 
                    204: 
1.52      albertel  205:   &Apache::lonxml::debug("sending back :$status:$datemsg:");
                    206:   if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) {
1.91    ! albertel  207:     &Apache::lonxml::debug("should be allowed to browse a resource when closed");
1.52      albertel  208:     $status='CAN_ANSWER';
                    209:     $datemsg='is closed but you are allowed to view it';
                    210:   }
                    211:   if ($ENV{'request.state'} eq "construct") {
                    212:     &Apache::lonxml::debug("in construction ignoring dates");
                    213:     $status='CAN_ANSWER';
                    214:     $datemsg='is in under construction';
                    215:   }
                    216:   return ($status,$datemsg);
1.20      albertel  217: }
                    218: 
1.41      albertel  219: sub showhash {
1.52      albertel  220:   my (%hash) = @_;
1.79      albertel  221:   &showhashsubset(\%hash,'');
                    222:   return '';
                    223: }
                    224: 
                    225: sub showhashsubset {
                    226:   my ($hash,$keyre) = @_;
1.52      albertel  227:   my $resultkey;
1.79      albertel  228:   foreach $resultkey (sort keys %$hash) {
                    229:     if ($resultkey =~ /$keyre/) {
                    230:       if (ref($$hash{$resultkey})) {
                    231: 	if ($$hash{$resultkey} =~ /ARRAY/ ) {
                    232: 	  my $string="$resultkey ---- (";
                    233: 	  foreach my $elm (@{ $$hash{$resultkey} }) {
                    234: 	    $string.="$elm,";
                    235: 	  }
                    236: 	  chop($string);
                    237: 	  &Apache::lonxml::debug("$string)");
                    238: 	} else {
                    239: 	  &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
1.73      albertel  240: 	}
                    241:       } else {
1.79      albertel  242: 	&Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
1.73      albertel  243:       }
                    244:     }
1.52      albertel  245:   }
                    246:   &Apache::lonxml::debug("\n<br />restored values^</br>\n");
                    247:   return '';
1.41      albertel  248: }
                    249: 
                    250: sub setuppermissions {
1.52      albertel  251:   $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
                    252:   $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
                    253:   return ''
1.41      albertel  254: }
                    255: 
                    256: sub setupheader {
1.52      albertel  257:   my $request=$_[0];
                    258:   if ($ENV{'browser.mathml'}) {
                    259:     $request->content_type('text/xml');
                    260:   } else {
                    261:     $request->content_type('text/html');
                    262:   }
1.68      albertel  263:   if (!$Apache::lonxml::debug && ($ENV{'REQUEST_METHOD'} eq 'GET')) {
                    264:     &Apache::loncommon::no_cache($request);
                    265:   }
1.52      albertel  266:   $request->send_http_header;
                    267:   return OK if $request->header_only;
                    268:   return ''
1.41      albertel  269: }
1.35      albertel  270: 
1.47      albertel  271: sub handle_save_or_undo {
1.52      albertel  272:   my ($request,$problem,$result) = @_;
1.70      albertel  273:   my $file    = &Apache::lonnet::filelocation("",$request->uri);
                    274:   my $filebak =$file.".bak";
                    275:   my $filetmp =$file.".tmp";
1.64      albertel  276:   my $error=0;
1.52      albertel  277: 
                    278:   if ($ENV{'form.Undo'} eq 'undo') {
1.70      albertel  279:     my $error=0;
                    280:     if (!copy($file,$filetmp)) { $error=1; }
                    281:     if ((!$error) && (!copy($filebak,$file))) { $error=1; }
                    282:     if ((!$error) && (!move($filetmp,$filebak))) { $error=1; }
                    283:     if (!$error) {
                    284:       $request->print("<p><b>Undid changes, Switched $filebak and $file</b></p>");
1.52      albertel  285:     } else {
1.70      albertel  286:       $request->print("<p><font color=\"red\" size=\"+1\"><b>Unable to undo, unable to switch $filebak and $file</b></font></p>");
1.64      albertel  287:       $error=1;
1.52      albertel  288:     }
                    289:   } else {
1.70      albertel  290:     my $fs=Apache::File->new(">$filebak");
1.52      albertel  291:     if (defined($fs)) {
                    292:       print $fs $$problem;
1.70      albertel  293:       $request->print("<b>Making Backup to $filebak</b><br />");
1.52      albertel  294:     } else {
1.70      albertel  295:       $request->print("<font color=\"red\" size=\"+1\"><b>Unable to make backup $filebak</b></font>");
1.64      albertel  296:       $error=2;
1.52      albertel  297:     }
1.70      albertel  298:     my $fh=Apache::File->new(">$file");
1.52      albertel  299:     if (defined($fh)) {
                    300:       print $fh $$result;
1.70      albertel  301:       $request->print("<b>Saving Modifications to $file</b><br />");
1.47      albertel  302:     } else {
1.70      albertel  303:       $request->print("<font color=\"red\" size=\"+1\"><b>Unable to write to $file</b></font>");
1.64      albertel  304:       $error|=4;
1.47      albertel  305:     }
1.52      albertel  306:   }
1.64      albertel  307:   return $error;
                    308: }
                    309: 
1.74      albertel  310: sub analyze {
                    311:   my ($request,$file) = @_;
                    312:   &Apache::lonxml::debug("Analyze");
                    313:   my $result=&Apache::lonnet::ssi($request->uri,('grade_target' => 'analyze'));
                    314:   &Apache::lonxml::debug(":$result:");
                    315:   (my $garbage,$result)=split(/_HASH_REF__/,$result,2);
                    316:   &showhash(&Apache::lonnet::str2hash($result));
                    317:   return $result;
                    318: }
                    319: 
1.64      albertel  320: sub editxmlmode {
                    321:   my ($request,$file) = @_;
                    322:   my $result;
                    323:   my $problem=&Apache::lonnet::getfile($file);
                    324:   if ($problem == -1) {
                    325:     &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
                    326:     $problem='';
                    327:   }
                    328:   if (defined($ENV{'form.editxmltext'}) || defined($ENV{'form.Undo'})) {
                    329:     my $error=&handle_save_or_undo($request,\$problem,
                    330: 				   \$ENV{'form.editxmltext'});
                    331:     if (!$error) { $problem=&Apache::lonnet::getfile($file); }
                    332:   }
1.80      albertel  333:   &Apache::lonhomework::showhashsubset(\%ENV,'^form');
                    334:   if ( $ENV{'form.submit'} eq 'Submit Changes and View' ) {
                    335:     &Apache::lonhomework::showhashsubset(\%ENV,'^form');
                    336:     $ENV{'form.problemmode'}='View';
                    337:     &renderpage($request,$file);
                    338:   } else {
                    339:     my ($rows,$cols) = &Apache::edit::textarea_sizes(\$problem);
1.82      bowersj2  340:     my $xml_help = Apache::loncommon::help_open_topic("Problem_Editor_XML_Index");
1.80      albertel  341:     if ($cols > 80) { $cols = 80; }
1.89      albertel  342:     if ($cols < 70) { $cols = 70; }
                    343:     if ($rows < 20) { $rows = 20; }
1.80      albertel  344:     $result.='<html><body bgcolor="#FFFFFF">
1.64      albertel  345:             <form name="lonhomework" method="POST" action="'.
                    346: 	      $ENV{'request.uri'}.'">
                    347:             <input type="hidden" name="problemmode" value="EditXML" />
1.80      albertel  348:             <input type="submit" name="problemmode" value="Discard Edits and View" />
1.64      albertel  349:             <input type="submit" name="problemmode" value="Edit" />
                    350:             <hr />
                    351:             <input type="submit" name="submit" value="Submit Changes" />
1.80      albertel  352:             <input type="submit" name="submit" value="Submit Changes and View" />
1.64      albertel  353:             <input type="submit" name="Undo" value="undo" />
                    354:             <hr />
1.82      bowersj2  355:             ' . $xml_help . ' Problem Help<br>
1.64      albertel  356:             <textarea rows="'.$rows.'" cols="'.$cols.'" name="editxmltext">'.
1.73      albertel  357: 	      &HTML::Entities::encode($problem).'</textarea>
1.64      albertel  358:             </form></body></html>';
1.80      albertel  359:     $request->print($result);
                    360:   }
1.64      albertel  361:   return '';
1.47      albertel  362: }
                    363: 
1.41      albertel  364: sub renderpage {
1.52      albertel  365:   my ($request,$file) = @_;
                    366: 
                    367:   my (@targets) = &get_target();
1.70      albertel  368:   &Apache::lonxml::debug("Running targets ".join(':',@targets));
1.52      albertel  369:   foreach my $target (@targets) {
                    370:     #my $t0 = [&gettimeofday()];
                    371:     my $problem=&Apache::lonnet::getfile($file);
                    372:     if ($problem == -1) {
                    373:       &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
                    374:       $problem='';
                    375:     }
                    376: 
                    377:     my %mystyle;
                    378:     my $result = '';
                    379:     &Apache::inputtags::initialize_inputtags;
                    380:     &Apache::edit::initialize_edit;
1.74      albertel  381:     if ($target eq 'analyze') { %Apache::lonhomework::anaylze=(); }
1.52      albertel  382:     if ($target eq 'web') {
1.76      albertel  383:       my ($symb)=&Apache::lonxml::whichuser();
                    384:       if ($symb eq '') {
1.52      albertel  385: 	if ($ENV{'request.state'} eq "construct") {
1.36      albertel  386: 	} else {
1.84      bowersj2  387:           my $help = Apache::loncommon::help_open_topic("Ambiguous_Reference");
                    388: 	  $request->print("Browsing or <a href=\"/adm/ambiguous\">ambiguous</a> reference, submissions ignored $help<br />");
1.51      harris41  389: 	}
1.52      albertel  390:       }
                    391:       #if ($Apache::lonhomework::viewgrades eq 'F') {&createmenu('grade',$request); }
                    392:     }
1.85      albertel  393:     if ($target eq 'answer') { &showhash(%Apache::lonhomework::history); }
                    394:     if ($target eq 'web') {&Apache::lonhomework::showhashsubset(\%ENV,'^form');}
1.52      albertel  395: 
1.91    ! albertel  396:     my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
1.52      albertel  397:     if ($default == -1) {
1.91    ! albertel  398:       &Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
1.52      albertel  399:       $default='';
                    400:     }
1.70      albertel  401:     &Apache::lonxml::debug("Should be parsing now");
1.78      albertel  402:     $result = &Apache::lonxml::xmlparse($request, $target, $problem,
1.52      albertel  403: 			$default.&setup_vars($target),%mystyle);
                    404: 
                    405:     #$request->print("Result follows:");
                    406:     if ($target eq 'modified') {
                    407:       &handle_save_or_undo($request,\$problem,\$result);
                    408:     } else {
1.74      albertel  409:       if ($target eq 'analyze') {
                    410: 	$result=&Apache::lonnet::hashref2str(\%Apache::lonhomework::analyze);
1.75      albertel  411: 	undef(%Apache::lonhomework::analyze);
1.74      albertel  412:       }
1.52      albertel  413:       #my $td=&tv_interval($t0);
                    414:       #if ( $Apache::lonxml::debug) {
                    415: 	#$result =~ s:</body>::;
                    416: 	#$result.="<br />Spent $td seconds processing target $target\n</body>";
                    417:       #}
                    418:       $request->print($result);
                    419:     }
                    420:     #$request->print(":Result ends");
                    421:     #my $td=&tv_interval($t0);
                    422:   }
1.41      albertel  423: }
                    424: 
1.42      albertel  425: # with no arg it returns a HTML <option> list of the template titles
                    426: # with one arg it returns the filename associated with the arg passed
                    427: sub get_template_list {
1.52      albertel  428:   my ($namewanted,$extension) = @_;
                    429:   my $result;
1.85      albertel  430:   my @allnames;
1.52      albertel  431:   &Apache::lonxml::debug("Looking for :$extension:");
1.91    ! albertel  432:   foreach my $file (</home/httpd/html/res/adm/includes/templates/*.$extension>) {
1.52      albertel  433:     my $name=&Apache::lonnet::metadata($file,'title');
                    434:     if ($namewanted && ($name eq $namewanted)) {
                    435:       $result=$file;
                    436:       last;
                    437:     } else {
1.85      albertel  438:       push (@allnames, $name);
1.42      albertel  439:     }
1.52      albertel  440:   }
1.86      albertel  441:   if (@allnames && !$result) {
1.85      albertel  442:     $result="<option>Select a $extension type</option>\n<option>".
                    443:       join('</option><option>',sort(@allnames)).'</option>';
                    444:   }
1.52      albertel  445:   return $result;
1.42      albertel  446: }
                    447: 
                    448: sub newproblem {
1.65      matthew   449:     my ($request) = @_;
                    450:     my $extension=$request->uri;
                    451:     $extension=~s:^.*\.([\w]+)$:$1:;
                    452:     &Apache::lonxml::debug("Looking for :$extension:");
1.85      albertel  453:     if ($ENV{'form.template'} &&
                    454: 	$ENV{'form.template'} ne "Select a $extension type") {
1.65      matthew   455: 	use File::Copy;
                    456: 	my $file = &get_template_list($ENV{'form.template'},$extension);
                    457: 	my $dest = &Apache::lonnet::filelocation("",$request->uri);
                    458: 	copy($file,$dest);
                    459: 	&renderpage($request,$dest);
                    460:     } elsif($ENV{'form.newfile'}) {
                    461: 	# I don't like hard-coded filenames but for now, this will work.
                    462: 	use File::Copy;
                    463: 	my $templatefilename = 
1.66      albertel  464: 	    $request->dir_config('lonIncludes').'/templates/blank.problem';
                    465: 	&Apache::lonxml::debug("$templatefilename");
1.65      matthew   466: 	my $dest = &Apache::lonnet::filelocation("",$request->uri);
                    467: 	copy($templatefilename,$dest);
                    468: 	&renderpage($request,$dest);
1.85      albertel  469:     } else {
1.65      matthew   470: 	my $templatelist=&get_template_list('',$extension);
                    471: 	my $url=$request->uri;
                    472: 	my $dest = &Apache::lonnet::filelocation("",$request->uri);
1.85      albertel  473: 	my $instructions;
                    474: 	if ($templatelist) { $instructions=", select a template from the pull-down menu below. Then";}
1.65      matthew   475: 	$request->print(<<ENDNEWPROBLEM);
1.42      albertel  476: <body bgcolor="#FFFFFF">
1.85      albertel  477: The requested file $url doesn\'t exist. <br />
                    478: To create a new $extension$instructions click on the Create $extension button.
1.42      albertel  479: <form action="$url" method="POST">
                    480: ENDNEWPROBLEM
1.85      albertel  481: 	if (defined($templatelist)) {
                    482: 	    $request->print("<select name=\"template\">$templatelist</select>");
                    483: 	}
                    484: 	$request->print("<br /><input type=\"submit\" name=\"newfile\" value=\"Create $extension\" />");
                    485: 	$request->print("</form></body>");
1.65      matthew   486:     }
                    487:     return '';
1.42      albertel  488: }
                    489: 
                    490: sub view_or_edit_menu {
1.52      albertel  491:   my ($request) = @_;
                    492:   my $url=$request->uri;
                    493:   $request->print(<<EDITMENU);
1.42      albertel  494: <body bgcolor="#FFFFFF">
                    495: <form action="$url" method="POST">
                    496: Would you like to <input type="submit" name="problemmode" value="View"> or
                    497: <input type="submit" name="problemmode" value="Edit"> the problem.
                    498: </form>
                    499: </body>
                    500: EDITMENU
                    501: }
                    502: 
1.41      albertel  503: sub handler {
1.52      albertel  504:   #my $t0 = [&gettimeofday()];
                    505:   my $request=$_[0];
1.41      albertel  506: 
1.91    ! albertel  507: #  if ( $ENV{'user.name'} eq 'albertel' ) {$Apache::lonxml::debug=1;}
1.41      albertel  508: 
                    509:   if (&setupheader($request)) { return OK; }
1.52      albertel  510:   $ENV{'request.uri'}=$request->uri;
1.41      albertel  511: 
                    512:   #setup permissions
1.52      albertel  513:   $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
                    514:   $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
                    515:   &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:");
1.71      albertel  516:   # some times multiple problemmodes are submitted, need to select
                    517:   # the last one
1.80      albertel  518:   &Apache::lonxml::debug("Problem Mode ".$ENV{'form.problemmode'});
1.72      albertel  519:   if ( defined($ENV{'form.problemmode'}) &&
1.77      albertel  520:        ref($ENV{'form.problemmode'}) ) {
1.80      albertel  521:     &Apache::lonxml::debug("Problem Mode ".join(",",@$ENV{'form.problemmode'}));
1.72      albertel  522:     my $mode=$ENV{'form.problemmode'}->[-1];
1.71      albertel  523:     undef $ENV{'form.problemmode'};
1.72      albertel  524:     $ENV{'form.problemmode'}=$mode;
1.71      albertel  525:   }
1.64      albertel  526:   &Apache::lonxml::debug("Problem Mode ".$ENV{'form.problemmode'});
1.52      albertel  527:   my $file=&Apache::lonnet::filelocation("",$request->uri);
                    528: 
                    529:   #check if we know where we are
                    530:   if ($ENV{'request.course.fn'} && !&Apache::lonnet::symbread()) { 
                    531:     # if we are browsing we might not be able to know where we are
                    532:     if ($Apache::lonhomework::browse ne 'F') {
                    533:       #should know where we are, so ask
                    534:       $request->internal_redirect('/adm/ambiguous'); return;
1.41      albertel  535:     }
1.52      albertel  536:   }
1.41      albertel  537: 
1.52      albertel  538:   if ($ENV{'request.state'} eq "construct") {
1.62      albertel  539:     if ($ENV{'form.resetdata'} eq 'Reset Submissions') {
                    540:       my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
                    541:       &Apache::lonnet::tmpreset($symb,'',$domain,$name);
                    542:     }
1.52      albertel  543:     if ( -e $file ) {
                    544:       if (!(defined $ENV{'form.problemmode'})) {
                    545: 	#first visit to problem in construction space
1.64      albertel  546: 	#&view_or_edit_menu($request);
                    547: 	$ENV{'form.problemmode'}='View';
                    548: 	&renderpage($request,$file);
                    549:       } elsif ($ENV{'form.problemmode'} eq 'EditXML') {
                    550: 	&editxmlmode($request,$file);
1.74      albertel  551:       } elsif ($ENV{'form.problemmode'} eq 'Answer Distribution') {
                    552: 	&analyze($request,$file);
1.52      albertel  553:       } else {
                    554: 	&renderpage($request,$file);
1.41      albertel  555:       }
                    556:     } else {
1.52      albertel  557:       # requested file doesn't exist in contruction space
                    558:       &newproblem($request);
1.41      albertel  559:     }
1.52      albertel  560:   } else {
                    561:     # just render the page normally outside of construction space
1.74      albertel  562:     &Apache::lonxml::debug("not construct");
1.52      albertel  563:     &renderpage($request,$file);
                    564:   }
                    565:   #my $td=&tv_interval($t0);
                    566:   #&Apache::lonxml::debug("Spent $td seconds processing");
                    567:   # &Apache::lonhomework::send_footer($request);
                    568:   # always turn off debug messages
                    569:   $Apache::lonxml::debug=0;
                    570:   return OK;
                    571: 
1.1       albertel  572: }
                    573: 
                    574: 1;
                    575: __END__

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