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

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

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