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

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

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