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

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

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