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

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

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