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

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

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