File:  [LON-CAPA] / loncom / homework / lonhomework.pm
Revision 1.252: download - view: text, annotated - select for diffs
Tue Jun 20 05:10:00 2006 UTC (17 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- multipart problems work correctly now
- handles 'student never did any work' in a correct manner now

    1: # The LearningOnline Network with CAPA
    2: # The LON-CAPA Homework handler
    3: #
    4: # $Id: lonhomework.pm,v 1.252 2006/06/20 05:10:00 albertel Exp $
    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: 
   28: 
   29: package Apache::lonhomework;
   30: use strict;
   31: use Apache::style();
   32: use Apache::lonxml();
   33: use Apache::lonnet;
   34: use Apache::lonplot();
   35: use Apache::inputtags();
   36: use Apache::structuretags();
   37: use Apache::randomlabel();
   38: use Apache::response();
   39: use Apache::hint();
   40: use Apache::outputtags();
   41: use Apache::caparesponse();
   42: use Apache::radiobuttonresponse();
   43: use Apache::optionresponse();
   44: use Apache::imageresponse();
   45: use Apache::essayresponse();
   46: use Apache::externalresponse();
   47: use Apache::rankresponse();
   48: use Apache::matchresponse();
   49: use Apache::chemresponse();
   50: use Apache::drawimage();
   51: use Apache::Constants qw(:common);
   52: use HTML::Entities();
   53: use Apache::loncommon();
   54: use Apache::lonlocal;
   55: use Time::HiRes qw( gettimeofday tv_interval );
   56: use Apache::lonnet();
   57: 
   58: # FIXME - improve commenting
   59: 
   60: 
   61: BEGIN {
   62:     &Apache::lonxml::register_insert();
   63: }
   64: 
   65: 
   66: #
   67: # Decides what targets to render for.
   68: # Implicit inputs:
   69: #   Various session environment variables:
   70: #      request.state -  published  - is a /res/ resource
   71: #                       uploaded   - is a /uploaded/ resource
   72: #                       contruct   - is a /priv/ resource
   73: #      form.grade_target - a form parameter requesting a specific target
   74: sub get_target {
   75:     &Apache::lonxml::debug("request.state = $env{'request.state'}");
   76:     if( defined($env{'form.grade_target'})) {
   77: 	&Apache::lonxml::debug("form.grade_target= $env{'form.grade_target'}");
   78:     } else {
   79: 	&Apache::lonxml::debug("form.grade_target <undefined>");
   80:     }
   81:     if (($env{'request.state'} eq "published") ||
   82: 	($env{'request.state'} eq "uploaded")) {
   83: 	if ( defined($env{'form.grade_target'}  ) 
   84: 	     && ($env{'form.grade_target'} eq 'tex')) {
   85: 	    return ($env{'form.grade_target'});
   86: 	} elsif ( defined($env{'form.grade_target'}  ) 
   87: 		  && ($Apache::lonhomework::viewgrades eq 'F' )) {
   88: 	    return ($env{'form.grade_target'});
   89: 	} elsif ( $env{'form.grade_target'} eq 'webgrade'
   90: 		  && ($Apache::lonhomework::queuegrade eq 'F' )) {
   91: 	    return ($env{'form.grade_target'});
   92: 	}
   93: 	if ($env{'form.webgrade'} &&
   94: 	    ($Apache::lonhomework::modifygrades eq 'F'
   95: 	     || $Apache::lonhomework::queuegrade eq 'F' )) {
   96: 	    return ('grade','webgrade');
   97: 	}
   98: 	if ( defined($env{'form.submitted'}) &&
   99: 	     ( !defined($env{'form.resetdata'})) &&
  100: 	     ( !defined($env{'form.newrandomization'}))) {
  101: 	    return ('grade', 'web');
  102: 	} else {
  103: 	    return ('web');
  104: 	}
  105:     } elsif ($env{'request.state'} eq "construct") {
  106: 	if ( defined($env{'form.grade_target'}) ) {
  107: 	    return ($env{'form.grade_target'});
  108: 	}
  109: 	if ( defined($env{'form.preview'})) {
  110: 	    if ( defined($env{'form.submitted'})) {
  111: 		return ('grade', 'web');
  112: 	    } else {
  113: 		return ('web');
  114: 	    }
  115: 	} else {
  116: 	    if ( $env{'form.problemmode'} eq &mt('View') ||
  117: 		 $env{'form.problemmode'} eq &mt('Discard Edits and View')) {
  118: 		if ( defined($env{'form.submitted'}) &&
  119: 		     (!defined($env{'form.resetdata'})) &&
  120: 		     (!defined($env{'form.newrandomization'}))) {
  121: 		    return ('grade', 'web','answer');
  122: 		} else {
  123: 		    return ('web','answer');
  124: 		}
  125: 	    } elsif ( $env{'form.problemmode'} eq &mt('Edit') ||
  126: 		      $env{'form.problemmode'} eq 'Edit') {
  127: 		if ( $env{'form.submitted'} eq 'edit' ) {
  128: 		    if ( $env{'form.submit'} eq &mt('Submit Changes and View') ) {
  129: 			return ('modified','web','answer');
  130: 		    } else {
  131: 			return ('modified','no_output_web','edit');
  132: 		    }
  133: 		} else {
  134: 		    return ('no_output_web','edit');
  135: 		}
  136: 	    } else {
  137: 		return ('web');
  138: 	    }
  139: 	}
  140:     }
  141:     return ();
  142: }
  143: 
  144: sub setup_vars {
  145:     my ($target) = @_;
  146:     return ';'
  147: #  return ';$external::target='.$target.';';
  148: }
  149: 
  150: sub createmenu {
  151:     my ($which,$request)=@_;
  152:     if ($which eq 'grade') {
  153: 	$request->print('<script language="JavaScript"> 
  154:           hwkmenu=window.open("/res/adm/pages/homeworkmenu.html","homeworkremote",
  155:                  "height=350,width=150,menubar=no");
  156:           </script>');
  157:     }
  158: }
  159: 
  160: sub proctor_checked_in {
  161:     my ($slot_name,$slot,$type)=@_;
  162:     my @possible_proctors=split(",",$slot->{'proctor'});
  163:     
  164:     return 1 if (!@possible_proctors);
  165: 
  166:     my $key;
  167:     if ($type eq 'Task') {
  168: 	my $version=$Apache::lonhomework::history{'resource.0.version'};
  169: 	$key ="resource.$version.0.checkedin";
  170:     } elsif ($type eq 'problem') {
  171: 	$key ='resource.0.checkedin';
  172:     }
  173:     # backward compatability, used to be username@domain, 
  174:     # now is username:domain
  175:     my $who = $Apache::lonhomework::history{$key};
  176:     if ($who !~ /:/) {
  177: 	$who =~ tr/@/:/;
  178:     }     
  179:     foreach my $possible (@possible_proctors) { 
  180: 	if ($who eq $possible
  181: 	    && $Apache::lonhomework::history{$key.'.slot'} eq $slot_name) {
  182: 	    return 1;
  183: 	}
  184:     }
  185:     
  186:     return 0;
  187: }
  188: 
  189: $Apache::lonxml::browse='';
  190: sub check_ip_acc {
  191:     my ($acc)=@_;
  192:     &Apache::lonxml::debug("acc is $acc");
  193:     if (!defined($acc) || $acc =~ /^\s*$/ || $acc =~/^\s*no\s*$/i) { 
  194: 	return 1;
  195:     }
  196:     my $allowed=0;
  197:     my $ip=$ENV{'REMOTE_ADDR'};
  198:     my $name;
  199:     foreach my $pattern (split(',',$acc)) {
  200: 	$pattern =~ s/^\s*//;
  201: 	$pattern =~ s/\s*$//;
  202: 	if ($pattern =~ /\*$/) {
  203: 	    #35.8.*
  204: 	    $pattern=~s/\*//;
  205: 	    if ($ip =~ /^\Q$pattern\E/) { $allowed=1; }
  206: 	} elsif ($pattern =~ /(\d+\.\d+\.\d+)\.\[(\d+)-(\d+)\]$/) {    
  207: 	    #35.8.3.[34-56]
  208: 	    my $low=$2;
  209: 	    my $high=$3;
  210: 	    $pattern=$1;
  211: 	    if ($ip =~ /^\Q$pattern\E/) { 
  212: 		my $last=(split(/\./,$ip))[3];
  213: 		if ($last <=$high && $last >=$low) { $allowed=1; }
  214: 	    }
  215: 	} elsif ($pattern =~ /^\*/) {
  216: 	    #*.msu.edu
  217: 	    $pattern=~s/\*//;
  218: 	    if (!defined($name)) {
  219: 		use Socket;
  220: 		my $netaddr=inet_aton($ip);
  221: 		($name)=gethostbyaddr($netaddr,AF_INET);
  222: 	    }
  223: 	    if ($name =~ /\Q$pattern\E$/i) { $allowed=1; }
  224: 	} elsif ($pattern =~ /\d+\.\d+\.\d+\.\d+/) {
  225: 	    #127.0.0.1
  226: 	    if ($ip =~ /^\Q$pattern\E/) { $allowed=1; }
  227: 	} else {
  228: 	    #some.name.com
  229: 	    if (!defined($name)) {
  230: 		use Socket;
  231: 		my $netaddr=inet_aton($ip);
  232: 		($name)=gethostbyaddr($netaddr,AF_INET);
  233: 	    }
  234: 	    if ($name =~ /\Q$pattern\E$/i) { $allowed=1; }
  235: 	}
  236: 	if ($allowed) { last; }
  237:     }
  238:     return $allowed;
  239: }
  240: 
  241: sub check_slot_access {
  242:     my ($id,$type)=@_;
  243: 
  244:     # does it pass normal muster
  245:     my ($status,$datemsg)=&check_access($id);
  246:     
  247:     my $useslots = &Apache::lonnet::EXT("resource.0.useslots");
  248:     if ($useslots ne 'resource' && $useslots ne 'map' 
  249: 	&& $useslots ne 'map_map') {
  250: 	return ($status,$datemsg);
  251:     }
  252: 
  253:     if ($status eq 'SHOW_ANSWER' ||
  254: 	$status eq 'CLOSED' ||
  255: 	$status eq 'INVALID_ACCESS' ||
  256: 	$status eq 'UNAVAILABLE') {
  257: 	return ($status,$datemsg);
  258:     }
  259:     if ($env{'request.state'} eq "construct") {
  260: 	return ($status,$datemsg);
  261:     }
  262:     
  263:     if ($type eq 'Task') {
  264: 	my $version=$Apache::lonhomework::history{'resource.version'};
  265: 	if ($Apache::lonhomework::history{"resource.$version.0.checkedin"} &&
  266: 	    $Apache::lonhomework::history{"resource.$version.0.status"} eq 'pass') {
  267: 	    return ('SHOW_ANSWER');
  268: 	}
  269:     }
  270: 
  271:     my @slots=
  272: 	(split(':',&Apache::lonnet::EXT("resource.0.availablestudent")),
  273: 	 split(':',&Apache::lonnet::EXT("resource.0.available")));
  274: 
  275: #    if (!@slots) {
  276: #	return ($status,$datemsg);
  277: #    }
  278:     my $slotstatus='NOT_IN_A_SLOT';
  279:     my ($returned_slot,$slot_name);
  280:     foreach my $slot (@slots) {
  281: 	$slot =~ s/(^\s*|\s*$)//g;
  282: 	&Apache::lonxml::debug("getting $slot");
  283: 	my %slot=&Apache::lonnet::get_slot($slot);
  284: 	&Apache::lonhomework::showhash(%slot);
  285: 	if ($slot{'starttime'} < time &&
  286: 	    $slot{'endtime'} > time &&
  287: 	    &check_ip_acc($slot{'ip'})) {
  288: 	    &Apache::lonxml::debug("$slot is good");
  289: 	    $slotstatus='NEEDS_CHECKIN';
  290: 	    $returned_slot=\%slot;
  291: 	    $slot_name=$slot;
  292: 	    last;
  293: 	}
  294:     }
  295:     if ($slotstatus eq 'NEEDS_CHECKIN' &&
  296: 	&proctor_checked_in($slot_name,$returned_slot,$type)) {
  297: 	&Apache::lonxml::debug("protoctor checked in");
  298: 	$slotstatus='CAN_ANSWER';
  299:     }
  300: 
  301:     my ($is_correct,$got_grade,$checkedin);
  302:     if ($type eq 'Task') {
  303: 	my $version=$Apache::lonhomework::history{'resource.0.version'};
  304: 	$got_grade = 
  305: 	    ($Apache::lonhomework::history{"resource.$version.0.status"} 
  306: 	     =~ /^(?:pass|fail)$/);
  307: 	$is_correct =  
  308: 	    ($Apache::lonhomework::history{"resource.$version.0.status"} eq 'pass'
  309: 	     || $Apache::lonhomework::history{"resource.0.solved"} =~ /^correct_/ );
  310: 	$checkedin =
  311: 	    $Apache::lonhomework::history{"resource.$version.0.checkedin"};
  312:     } elsif ($type eq 'problem') {
  313: 	$got_grade  = 1;
  314: 	$checkedin  = $Apache::lonhomework::history{"resource.0.checkedin"};
  315: 	$is_correct =
  316: 	    ($Apache::lonhomework::history{"resource.0.solved"} =~/^correct_/);
  317:     }
  318:     
  319:     &Apache::lonxml::debug(" slot is $slotstatus checkedin ($checkedin) got_grade ($got_grade) is_correct ($is_correct)");
  320:     
  321:     # no slot is currently open, and has been checked in for this version
  322:     # but hasn't got a grade, therefore must be awaiting a grade
  323:     if (!defined($slot_name)
  324: 	&& $checkedin 
  325: 	&& !$got_grade) {
  326: 	return ('WAITING_FOR_GRADE');
  327:     }
  328: 
  329:     # no slot is currently open, and has been checked in for this version
  330:     # previous slot is therefore CLOSED, so therefore the problem is
  331:     if (!defined($slot_name)
  332: 	&& $checkedin 
  333: 	&& $type eq 'problem') {
  334: 	return ('CLOSED',$datemsg);
  335:     }
  336: 
  337:     if ($slotstatus eq 'NOT_IN_A_SLOT' 
  338: 	&& $checkedin ) {
  339: 
  340: 	if ($got_grade) {
  341: 	    return ('SHOW_ANSWER');
  342: 	} else {
  343: 	    return ('WAITING_FOR_GRADE');
  344: 	}
  345: 
  346:     }
  347:     if ( $is_correct) {
  348: 	return ('SHOW_ANSWER');
  349:     }
  350:     if ( $status eq 'CANNOT_ANSWER' && 
  351: 	 ($slotstatus ne 'NEEDS_CHECKIN' && $slotstatus ne 'NOT_IN_A_SLOT')) {
  352: 	return ($status,$datemsg);
  353:     }
  354: 
  355:     return ($slotstatus,$datemsg,$slot_name,$returned_slot);
  356: }
  357: 
  358: # JB, 9/24/2002: Any changes in this function may require a change
  359: # in lonnavmaps::resource::getDateStatus.
  360: sub check_access {
  361:     my ($id) = @_;
  362:     my $date ='';
  363:     my $status;
  364:     my $datemsg = '';
  365:     my $lastdate = '';
  366:     my $type;
  367:     my $passed;
  368: 
  369:     if ($env{'request.state'} eq "construct") {
  370: 	if ($env{'form.problemstate'}) {
  371: 	    if ($env{'form.problemstate'} =~ /^CANNOT_ANSWER/) {
  372: 		if ( ! ($env{'form.problemstate'} eq 'CANNOT_ANSWER_correct' &&
  373: 			lc($Apache::lonhomework::problemstatus) eq 'no')) {
  374: 		    return ('CANNOT_ANSWER',
  375: 			    &mt('is in this state due to author settings.'));
  376: 		}
  377: 	    } else {
  378: 		return ($env{'form.problemstate'},
  379: 			&mt('is in this state due to author settings.'));
  380: 	    }
  381: 	}
  382: 	&Apache::lonxml::debug("in construction ignoring dates");
  383: 	$status='CAN_ANSWER';
  384: 	$datemsg=&mt('is in under construction');
  385: #	return ($status,$datemsg);
  386:     }
  387: 
  388:     &Apache::lonxml::debug("checking for part :$id:");
  389:     &Apache::lonxml::debug("time:".time);
  390: 
  391:     my ($symb)=&Apache::lonxml::whichuser();
  392:     &Apache::lonxml::debug("symb:".$symb);
  393:     #if ($env{'request.state'} ne "construct" && $symb ne '') {
  394:     if ($env{'request.state'} ne "construct") {
  395: 	my $allowed=&check_ip_acc(&Apache::lonnet::EXT("resource.$id.acc"));
  396: 	if (!$allowed && ($Apache::lonhomework::browse ne 'F')) {
  397: 	    $status='INVALID_ACCESS';
  398: 	    $date=&mt("can not be accessed from your location.");
  399: 	    return($status,$date);
  400: 	}
  401: 	
  402: 	foreach my $temp ("opendate","duedate","answerdate") {
  403: 	    $lastdate = $date;
  404: 	    if ($temp eq 'duedate') {
  405: 		$date = &due_date($id);
  406: 	    } else {
  407: 		$date = &Apache::lonnet::EXT("resource.$id.$temp");
  408: 	    }
  409: 	    
  410: 	    my $thistype = &Apache::lonnet::EXT("resource.$id.$temp.type");
  411: 	    if ($thistype =~ /^(con_lost|no_such_host)/ ||
  412: 		$date     =~ /^(con_lost|no_such_host)/) {
  413: 		$status='UNAVAILABLE';
  414: 		$date=&mt("may open later.");
  415: 		return($status,$date);
  416: 	    }
  417: 	    if ($thistype eq 'date_interval') {
  418: 		if ($temp eq 'opendate') {
  419: 		    $date=&Apache::lonnet::EXT("resource.$id.duedate")-$date;
  420: 		}
  421: 		if ($temp eq 'answerdate') {
  422: 		    $date=&Apache::lonnet::EXT("resource.$id.duedate")+$date;
  423: 		}
  424: 	    }
  425: 	    &Apache::lonxml::debug("found :$date: for :$temp:");
  426: 	    if ($date eq '') {
  427: 		$date = &mt("an unknown date"); $passed = 0;
  428: 	    } elsif ($date eq 'con_lost') {
  429: 		$date = &mt("an indeterminate date"); $passed = 0;
  430: 	    } else {
  431: 		if (time < $date) { $passed = 0; } else { $passed = 1; }
  432: 		$date = localtime $date;
  433: 	    }
  434: 	    if (!$passed) { $type=$temp; last; }
  435: 	}
  436: 	&Apache::lonxml::debug("have :$type:$passed:");
  437: 	if ($passed) {
  438: 	    $status='SHOW_ANSWER';
  439: 	    $datemsg=$date;
  440: 	} elsif ($type eq 'opendate') {
  441: 	    $status='CLOSED';
  442: 	    $datemsg = &mt("will open on")." $date";
  443: 	} elsif ($type eq 'duedate') {
  444: 	    $status='CAN_ANSWER';
  445: 	    $datemsg = &mt("is due at")." $date";
  446: 	} elsif ($type eq 'answerdate') {
  447: 	    $status='CLOSED';
  448: 	    $datemsg = &mt("was due on")." $lastdate".&mt(", and answers will be available on")." $date";
  449: 	}
  450:     }
  451:     if ($status eq 'CAN_ANSWER' ||
  452: 	(($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED'))) {
  453: 	#check #tries, and if correct.
  454: 	my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
  455: 	my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
  456: 	if ( $tries eq '' ) { $tries = '0'; }
  457: 	if ( $maxtries eq '' && 
  458: 	     $env{'request.state'} ne 'construct') { $maxtries = '2'; } 
  459: 	if ($maxtries && $tries >= $maxtries) { $status = 'CANNOT_ANSWER'; }
  460: 	# if (correct and show prob status) or excused then CANNOT_ANSWER
  461: 	if(($Apache::lonhomework::history{"resource.$id.solved"}=~/^correct/
  462: 	    &&
  463: 	    lc($Apache::lonhomework::problemstatus) ne 'no')
  464: 	   ||
  465: 	   $Apache::lonhomework::history{"resource.$id.solved"}=~/^excused/) {
  466: 	    $status = 'CANNOT_ANSWER';
  467: 	}
  468:     }
  469:     if ($status eq 'CAN_ANSWER' || $status eq 'CANNOT_ANSWER') {
  470: 	my $interval=&Apache::lonnet::EXT("resource.$id.interval");
  471: 	&Apache::lonxml::debug("looking for interval $interval");
  472: 	if ($interval) {
  473: 	    my $first_access=&Apache::lonnet::get_first_access('map');
  474: 	    &Apache::lonxml::debug("looking for accesstime $first_access");
  475: 	    if (!$first_access) {
  476: 		$status='NOT_YET_VIEWED';
  477: 		my $due_date = &due_date($id);
  478: 		$datemsg=&seconds_to_human_length($due_date-time);
  479: 	    }
  480: 	}
  481:     }
  482: 
  483:   #if (($status ne 'CLOSED') && ($Apache::lonhomework::type eq 'exam') &&
  484:   #    (!$Apache::lonhomework::history{"resource.0.outtoken"})) {
  485:   #    return ('UNCHECKEDOUT','needs to be checked out');
  486:   #}
  487: 
  488: 
  489:     &Apache::lonxml::debug("sending back :$status:$datemsg:");
  490:     if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) {
  491: 	&Apache::lonxml::debug("should be allowed to browse a resource when closed");
  492: 	$status='CAN_ANSWER';
  493: 	$datemsg=&mt('is closed but you are allowed to view it');
  494:     }
  495: 
  496:     return ($status,$datemsg);
  497: }
  498: # this should work exactly like the copy in lonnavmaps.pm
  499: sub due_date {
  500:     my ($part_id,$symb,$udom,$uname)=@_;
  501:     my $date;
  502:     my $interval= &Apache::lonnet::EXT("resource.$part_id.interval",$symb,
  503: 				       $udom,$uname);
  504:     &Apache::lonxml::debug("looking for interval $part_id $symb $interval");
  505:     my $due_date= &Apache::lonnet::EXT("resource.$part_id.duedate",$symb,
  506: 				       $udom,$uname);
  507:     &Apache::lonxml::debug("looking for due_date $part_id $symb $due_date");
  508:     if (defined($interval)) {
  509: 	my $first_access=&Apache::lonnet::get_first_access('map',$symb);
  510: 	if (defined($first_access)) {
  511: 	    $interval = $first_access+$interval;
  512: 	    $date = ($interval < $due_date)? $interval : $due_date;
  513: 	} else {
  514: 	    $date = $due_date;
  515: 	}
  516:     } else {
  517: 	$date = $due_date;
  518:     }
  519:     return $date
  520: }
  521: 
  522: sub seconds_to_human_length {
  523:     my ($length)=@_;
  524: 
  525:     my $seconds=$length%60; $length=int($length/60);
  526:     my $minutes=$length%60; $length=int($length/60);
  527:     my $hours=$length%24;   $length=int($length/24);
  528:     my $days=$length;
  529: 
  530:     my $timestr;
  531:     if ($days > 0) { $timestr.=&mt('[quant,_1,day]',$days); }
  532:     if ($hours > 0) { $timestr.=($timestr?", ":"").
  533: 			  &mt('[quant,_1,hour]',$hours); }
  534:     if ($minutes > 0) { $timestr.=($timestr?", ":"").
  535: 			    &mt('[quant,_1,minute]',$minutes); }
  536:     if ($seconds > 0) { $timestr.=($timestr?", ":"").
  537: 			    &mt('[quant,_1,second]',$seconds); }
  538:     return $timestr;
  539: }
  540: 
  541: sub showhash {
  542:     my (%hash) = @_;
  543:     &showhashsubset(\%hash,'.');
  544:     return '';
  545: }
  546: 
  547: sub showarray {
  548:     my ($array)=@_;
  549:     my $string="(";
  550:     foreach my $elm (@{ $array }) {
  551: 	if (ref($elm) eq 'ARRAY') {
  552: 	    $string.=&showarray($elm);
  553: 	} elsif (ref($elm) eq 'HASH') {
  554: 	    $string.= "HASH --- \n<br />";
  555: 	    $string.= &showhashsubset($elm,'.');
  556: 	} else {
  557: 	    $string.="$elm,"
  558: 	}
  559:     }
  560:     chop($string);
  561:     $string.=")";
  562:     return $string;
  563: }
  564: 
  565: sub showhashsubset {
  566:     my ($hash,$keyre) = @_;
  567:     my $resultkey;
  568:     foreach $resultkey (sort keys %$hash) {
  569: 	if ($resultkey !~ /$keyre/) { next; }
  570: 	if (ref($$hash{$resultkey})  eq 'ARRAY' ) {
  571: 	    &Apache::lonxml::debug("$resultkey ---- ".
  572: 				   &showarray($$hash{$resultkey}));
  573: 	} elsif (ref($$hash{$resultkey}) eq 'HASH' ) {
  574: 	    &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
  575: 	    &showhashsubset($$hash{$resultkey},'.');
  576: 	} else {
  577: 	    &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
  578: 	}
  579:     }
  580:     &Apache::lonxml::debug("\n<br />restored values^</br>\n");
  581:     return '';
  582: }
  583: 
  584: sub setuppermissions {
  585:     $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$env{'request.filename'});
  586:     my $viewgrades = &Apache::lonnet::allowed('vgr',$env{'request.course.id'});
  587:     if (! $viewgrades && 
  588: 	exists($env{'request.course.sec'}) && 
  589: 	$env{'request.course.sec'} !~ /^\s*$/) {
  590: 	$viewgrades = &Apache::lonnet::allowed('vgr',$env{'request.course.id'}.
  591:                                                '/'.$env{'request.course.sec'});
  592:     }
  593:     $Apache::lonhomework::viewgrades = $viewgrades;
  594: 
  595:     if ($Apache::lonhomework::browse eq 'F' && 
  596: 	$env{'form.devalidatecourseresdata'} eq 'on') {
  597: 	my (undef,$courseid) = &Apache::lonxml::whichuser();
  598: 	&Apache::lonnet::devalidatecourseresdata($env{"course.$courseid.num"},
  599: 					      $env{"course.$courseid.domain"});
  600:     }
  601: 
  602:     my $modifygrades = &Apache::lonnet::allowed('mgr',$env{'request.course.id'});
  603:     if (! $modifygrades && 
  604: 	exists($env{'request.course.sec'}) && 
  605: 	$env{'request.course.sec'} !~ /^\s*$/) {
  606: 	$modifygrades = 
  607: 	    &Apache::lonnet::allowed('mgr',$env{'request.course.id'}.
  608: 				     '/'.$env{'request.course.sec'});
  609:     }
  610:     $Apache::lonhomework::modifygrades = $modifygrades;
  611: 
  612:     my $queuegrade = &Apache::lonnet::allowed('mqg',$env{'request.course.id'});
  613:     if (! $queuegrade && 
  614: 	exists($env{'request.course.sec'}) && 
  615: 	$env{'request.course.sec'} !~ /^\s*$/) {
  616: 	$queuegrade = 
  617: 	    &Apache::lonnet::allowed('qgr',$env{'request.course.id'}.
  618: 				     '/'.$env{'request.course.sec'});
  619:     }
  620:     $Apache::lonhomework::queuegrade = $queuegrade;
  621:     return '';
  622: }
  623: 
  624: sub setupheader {
  625:     my $request=$_[0];
  626:     &Apache::loncommon::content_type($request,'text/html');
  627:     if (!$Apache::lonxml::debug && ($ENV{'REQUEST_METHOD'} eq 'GET')) {
  628: 	&Apache::loncommon::no_cache($request);
  629:     }
  630: #    $request->set_last_modified(&Apache::lonnet::metadata($request->uri,
  631: #							  'lastrevisiondate'));
  632:     $request->send_http_header;
  633:     return OK if $request->header_only;
  634:     return ''
  635: }
  636: 
  637: sub handle_save_or_undo {
  638:     my ($request,$problem,$result) = @_;
  639:     my $file    = &Apache::lonnet::filelocation("",$request->uri);
  640:     my $filebak =$file.".bak";
  641:     my $filetmp =$file.".tmp";
  642:     my $error=0;
  643: 
  644:     &Apache::lonnet::correct_line_ends($result);
  645: 
  646:     if ($env{'form.Undo'} eq &mt('undo')) {
  647: 	my $error=0;
  648: 	if (!copy($file,$filetmp)) { $error=1; }
  649: 	if ((!$error) && (!copy($filebak,$file))) { $error=1; }
  650: 	if ((!$error) && (!move($filetmp,$filebak))) { $error=1; }
  651: 	if (!$error) {
  652: 	    &Apache::lonxml::info("<p><b>".&mt("Undid changes, Switched")." $filebak ".&mt("and")." $file</b></p>");
  653: 	} else {
  654: 	    &Apache::lonxml::info("<p><font color=\"red\" size=\"+1\"><b>".&mt("Unable to undo, unable to switch")." $filebak ".&mt("and")." $file</b></font></p>");
  655: 	    $error=1;
  656: 	}
  657:     } else {
  658: 	my $fs=Apache::File->new(">$filebak");
  659: 	if (defined($fs)) {
  660: 	    print $fs $$problem;
  661: 	    &Apache::lonxml::info("<b>".&mt("Making Backup to").
  662: 				  " $filebak</b>");
  663: 	} else {
  664: 	    &Apache::lonxml::info("<font color=\"red\" size=\"+1\"><b>".&mt("Unable to make backup")." $filebak</b></font>");
  665: 	    $error=2;
  666: 	}
  667: 	my $fh=Apache::File->new(">$file");
  668: 	if (defined($fh)) {
  669: 	    print $fh $$result;
  670: 	    &Apache::lonxml::info("<b>".&mt("Saving Modifications to").
  671: 				  " $file</b>");
  672: 	} else {
  673: 	    &Apache::lonxml::info("<font color=\"red\" size=\"+1\"><b>".
  674: 				  &mt("Unable to write to")." $file</b></font>");
  675: 	    $error|=4;
  676: 	}
  677:     }
  678:     return $error;
  679: }
  680: 
  681: sub analyze_header {
  682:     my ($request) = @_;
  683:     my $result =
  684: 	&Apache::loncommon::start_page('Analyzing a problem',undef);
  685: 
  686:     $result .= 
  687: 	&Apache::lonxml::message_location().'
  688:             <form name="lonhomework" method="POST" action="'.
  689: 	    &HTML::Entities::encode($env{'request.uri'},'<>&"').'">'.
  690: 	    &Apache::structuretags::remember_problem_state().'
  691:             <input type="submit" name="problemmode" value="'.&mt("EditXML").'" />
  692:             <input type="submit" name="problemmode" value="'.&mt('Edit').'" />
  693:             <hr />
  694:             <input type="submit" name="submit" value="'.&mt("View").'" />
  695:             <hr />
  696:             </form>';
  697:     &Apache::lonxml::add_messages(\$result);
  698:     $request->print($result);
  699:     $request->rflush();
  700: }
  701: 
  702: sub analyze_footer {
  703:     my ($request) = @_;
  704:     $request->print(&Apache::loncommon::end_page());
  705:     $request->rflush();
  706: }
  707: 
  708: sub analyze {
  709:     my ($request,$file) = @_;
  710:     &Apache::lonxml::debug("Analyze");
  711:     my $result;
  712:     my %overall;
  713:     my %seedexample;
  714:     my %allparts;
  715:     my $rndseed=$env{'form.rndseed'};
  716:     &analyze_header($request);
  717:     my %prog_state=
  718: 	&Apache::lonhtmlcommon::Create_PrgWin($request,&mt('Analyze Progress'),
  719: 					      &mt('Getting Problem Variants'),
  720: 					      $env{'form.numtoanalyze'},
  721: 					      'inline',undef);
  722:     for(my $i=1;$i<$env{'form.numtoanalyze'}+1;$i++) {
  723: 	&Apache::lonhtmlcommon::Increment_PrgWin($request,\%prog_state,
  724: 						 &mt('last problem'));
  725: 	if (&Apache::loncommon::connection_aborted($request)) { return; }
  726:         my $thisseed=$i+$rndseed;
  727: 	my $subresult=&Apache::lonnet::ssi($request->uri,
  728: 					   ('grade_target' => 'analyze'),
  729: 					   ('rndseed' => $thisseed));
  730: 	(my $garbage,$subresult)=split(/_HASH_REF__/,$subresult,2);
  731: 	my %analyze=&Apache::lonnet::str2hash($subresult);
  732: 	my @parts;
  733: 	if (defined(@{ $analyze{'parts'} })) {
  734: 	    @parts=@{ $analyze{'parts'} };
  735: 	}
  736: 	foreach my $part (@parts) {
  737: 	    if (!exists($allparts{$part})) {$allparts{$part}=1;};
  738: 	    if ($analyze{$part.'.type'} eq 'numericalresponse'	||
  739: 		$analyze{$part.'.type'} eq 'stringresponse'	||
  740: 		$analyze{$part.'.type'} eq 'formularesponse'   ) {
  741: 		my $concatanswer=join("\0",@{ $analyze{$part.'.answer'} });
  742: 		if (($concatanswer eq '') || ($concatanswer=~/^\@/)) {
  743: 		    @{$analyze{$part.'.answer'}}=('<font color="red">'.&mt('Error').'</font>');
  744: 		}
  745: 		push( @{ $overall{$part.'.answer'} },
  746: 		      [@{ $analyze{$part.'.answer'} }]);
  747:                 $seedexample{join("\0",@{ $analyze{$part.'.answer'}})}=$thisseed;
  748: 	    }
  749: 	}
  750:     }
  751:     &Apache::lonhtmlcommon::Update_PrgWin($request,\%prog_state,
  752: 					  &mt('Analyzing Results'));
  753:     $request->print('<hr />'.&mt('List of possible answers').': ');
  754:     foreach my $part (sort(keys(%allparts))) {
  755: 	if (defined(@{ $overall{$part.'.answer'} })) {
  756: 	    my $num_cols=scalar(@{ $overall{$part.'.answer'}->[0] });
  757: 	    $request->print('<table class="thinborder"><tr><th colspan="'.($num_cols+1).'">'.&mt('Part').' '.$part.'</th></tr>');
  758: 	    my %frequency;
  759: 	    foreach my $answer (sort {$a->[0] <=> $b->[0]} (@{ $overall{$part.'.answer'} })) {
  760: 		$frequency{join("\0",@{ $answer })}++;
  761: 	    }
  762: 	    $request->print('<tr><th colspan="'.($num_cols).'">'.&mt('Answer').'</th><th>'.&mt('Frequency').'<br />('
  763: 			    .&mt('click for example').')</th></tr>');
  764: 	    foreach my $answer (sort {(split("\0",$a))[0] <=> (split("\0",$b))[0]} (keys(%frequency))) {
  765: 		$request->print('<tr><td class="center">'.
  766: 				join('</td><td class="center">',split("\0",$answer)).
  767: 				'</td><td class="center"><a href="'.$request->uri.'?rndseed='.$seedexample{$answer}.'">'.$frequency{$answer}.
  768: 				'</a></td></tr>');
  769: 	    }
  770: 	    $request->print('</table>');
  771: 	} else {
  772: 	    $request->print('<p>'.&mt('Response').' '.$part.' '.
  773: 			    &mt('is not analyzable at this time').'</p>');
  774: 	}
  775:     }
  776:     if (scalar(keys(%allparts)) == 0 ) {
  777: 	$request->print('<p>'.&mt('Found no analyzable responses in this problem, currently only Numerical, Formula and String response styles are supported.').'</p>');
  778:     }
  779:     &Apache::lonhtmlcommon::Close_PrgWin($request,\%prog_state);
  780:     &analyze_footer($request);
  781:     &Apache::lonhomework::showhash(%overall);
  782:     return $result;
  783: }
  784: 
  785: sub editxmlmode {
  786:     my ($request,$file) = @_;
  787:     my $result;
  788:     my $problem=&Apache::lonnet::getfile($file);
  789:     if ($problem eq -1) {
  790: 	&Apache::lonxml::error("<b> ".&mt('Unable to find').
  791: 			       " <i>$file</i></b>");
  792: 	$problem='';
  793:     }
  794:     if (defined($env{'form.editxmltext'}) || defined($env{'form.Undo'})) {
  795: 	my $error=&handle_save_or_undo($request,\$problem,
  796: 				       \$env{'form.editxmltext'});
  797: 	if (!$error) { $problem=&Apache::lonnet::getfile($file); }
  798:     }
  799:     &Apache::lonhomework::showhashsubset(\%env,'^form');
  800:     if ( $env{'form.submit'} eq &mt('Submit Changes and View') ) {
  801: 	&Apache::lonhomework::showhashsubset(\%env,'^form');
  802: 	$env{'form.problemmode'}='View';
  803: 	&renderpage($request,$file);
  804:     } else {
  805: 	my ($rows,$cols) = &Apache::edit::textarea_sizes(\$problem);
  806: 	my $xml_help = '<table><tr><td>'.
  807: 	    &Apache::loncommon::helpLatexCheatsheet("Problem_Editor_XML_Index",
  808: 						    "Problem Editing Help").
  809: 						    '</td><td>'.
  810:        &Apache::loncommon::help_open_menu('',undef,undef,undef,5,'Authoring').
  811:                 '</td></tr></table>';
  812: 	if ($cols > 80) { $cols = 80; }
  813: 	if ($cols < 70) { $cols = 70; }
  814: 	if ($rows < 20) { $rows = 20; }
  815: 	my $start_page = 
  816: 	    &Apache::loncommon::start_page("EditXML $file",
  817: 					   &Apache::edit::js_change_detection());
  818: 
  819: 	$result.=$start_page.
  820: 	    &renderpage($request,$file,['no_output_web'],1).
  821: 	    &Apache::lonxml::message_location().'
  822:             <form '.&Apache::edit::form_change_detection().' name="lonhomework" method="POST" action="'.
  823: 	    &HTML::Entities::encode($env{'request.uri'},'<>&"').'">'.
  824: 	    &Apache::structuretags::remember_problem_state().'
  825:             <input type="hidden" name="problemmode" value="'.&mt('EditXML').'" />
  826:             <input type="submit" name="problemmode" accesskey="d" value="'.&mt('Discard Edits and View').'" />
  827:             <input type="submit" '.&Apache::edit::submit_ask_anyway().'name="problemmode" accesskey="e" value="'.&mt('Edit').'" />
  828:             <input type="submit" name="Undo" accesskey="u" value="'.&mt('undo').'" />
  829:             <hr />
  830:             <input type="submit" name="submit" accesskey="s" value="'.&mt('Submit Changes').'" />
  831:             <input type="submit" name="submit" accesskey="v" value="'.&mt('Submit Changes and View').'" />
  832:             <hr />
  833:             ' . $xml_help . '
  834:             <textarea '.&Apache::edit::element_change_detection().' style="width:100%" rows="'.$rows.'" cols="'.$cols.'" name="editxmltext">'.
  835: 	    &HTML::Entities::encode($problem,'<>&"').'</textarea><br />
  836:             <input type="submit" name="submit" accesskey="s" value="'.&mt('Submit Changes').'" />
  837:             <input type="submit" name="submit" accesskey="v" value="'.&mt('Submit Changes and View').'" />
  838:             </form>'.&Apache::loncommon::end_page();
  839: 	&Apache::lonxml::add_messages(\$result);
  840: 	$request->print($result);
  841:     }
  842:     return '';
  843: }
  844: 
  845: #
  846: #    Render the page in whatever target desired.
  847: #
  848: sub renderpage {
  849:     my ($request,$file,$targets,$return_string) = @_;
  850: 
  851:     my @targets = @{$targets || [&get_target()]};
  852:     &Apache::lonhomework::showhashsubset(\%env,'form.');
  853:     &Apache::lonxml::debug("Running targets ".join(':',@targets));
  854:     my $overall_result;
  855:     foreach my $target (@targets) {
  856: 	# FIXME need to do something intelligent when a problem goes
  857:         # from viewable to not viewable due to map conditions
  858: 	#&setuppermissions();
  859: 	#if (   $Apache::lonhomework::browse ne '2'
  860: 	#    && $Apache::lonhomework::browse ne 'F' ) {
  861: 	#    $request->print(" You most likely shouldn't see me.");
  862: 	#}
  863: 	#my $t0 = [&gettimeofday()];
  864: 	my $output=1;
  865: 	if ($target eq 'no_output_web') {
  866: 	    $target = 'web'; $output=0;
  867: 	}
  868: 	my $problem=&Apache::lonnet::getfile($file);
  869: 	my $result;
  870: 	if ($problem eq -1) {
  871: 	    my $filename=(split('/',$file))[-1];
  872: 	    $result.="<b> ".&mt('Unable to find')." <i>$filename</i></b>";
  873: 	    $problem='';
  874: 	}
  875: 
  876: 	my %mystyle;
  877: 	if ($target eq 'analyze') { %Apache::lonhomework::analyze=(); }
  878: 	if ($target eq 'answer') { &showhash(%Apache::lonhomework::history); }
  879: 	if ($target eq 'web') {&Apache::lonhomework::showhashsubset(\%env,'^form');}
  880: 
  881: 	&Apache::lonxml::debug("Should be parsing now");
  882: 	$result .= &Apache::lonxml::xmlparse($request, $target, $problem,
  883: 					     &setup_vars($target),%mystyle);
  884: 	undef($Apache::lonhomework::parsing_a_problem);
  885: 	if (!$output) { $result = ''; }
  886: 	#$request->print("Result follows:");
  887: 	if ($target eq 'modified') {
  888: 	    &handle_save_or_undo($request,\$problem,\$result);
  889: 	} else {
  890: 	    if ($target eq 'analyze') {
  891: 		$result=&Apache::lonnet::hashref2str(\%Apache::lonhomework::analyze);
  892: 		undef(%Apache::lonhomework::analyze);
  893: 	    }
  894: 	    #my $td=&tv_interval($t0);
  895: 	    #if ( $Apache::lonxml::debug) {
  896: 	    #$result =~ s:</body>::;
  897: 	    #$result.="<br />Spent $td seconds processing target $target\n</body>";
  898: 	    #}
  899: #	    $request->print($result);
  900: 	    $overall_result.=$result;
  901: #	    $request->rflush();
  902: 	}
  903: 	#$request->print(":Result ends");
  904: 	#my $td=&tv_interval($t0);
  905:     }
  906:     if (!$return_string) {
  907: 	&Apache::lonxml::add_messages(\$overall_result);
  908: 	$request->print($overall_result);   
  909: 	$request->rflush();   
  910:     } else {
  911: 	return $overall_result;
  912:     }
  913: }
  914: 
  915: # with no arg it returns a HTML <option> list of the template titles
  916: # with one arg it returns the filename associated with the arg passed
  917: sub get_template_list {
  918:     my ($namewanted,$extension) = @_;
  919:     my $result;
  920:     my @allnames;
  921:     &Apache::lonxml::debug("Looking for :$extension:");
  922:     foreach my $file (</home/httpd/html/res/adm/includes/templates/*.$extension>) {
  923: 	my $name=&Apache::lonnet::metadata($file,'title');
  924: 	if ($namewanted && ($name eq $namewanted)) {
  925: 	    $result=$file;
  926: 	    last;
  927: 	} else {
  928: 	    if ($name) { push (@allnames, $name); }
  929: 	}
  930:     }
  931:     if (@allnames && !$result) {
  932: 	$result="<option>".&mt("Select a")." $extension ".&mt('template')."</option>\n<option>".
  933: 	    join('</option><option>',sort(@allnames)).'</option>';
  934:     }
  935:     return $result;
  936: }
  937: 
  938: sub newproblem {
  939:     my ($request) = @_;
  940:     my $extension=$request->uri;
  941:     $extension=~s:^.*\.([\w]+)$:$1:;
  942:     &Apache::lonxml::debug("Looking for :$extension:");
  943:     my $templatelist=&get_template_list('',$extension);
  944:     if ($env{'form.template'} &&
  945: 	$env{'form.template'} ne "Select a $extension template") {
  946: 	use File::Copy;
  947: 	my $file = &get_template_list($env{'form.template'},$extension);
  948: 	my $dest = &Apache::lonnet::filelocation("",$request->uri);
  949: 	copy($file,$dest);
  950: 	&renderpage($request,$dest);
  951:     } elsif($env{'form.newfile'} && !$templatelist) {
  952: 	# I don't like hard-coded filenames but for now, this will work.
  953: 	use File::Copy;
  954: 	my $templatefilename =
  955: 	    $request->dir_config('lonIncludes').'/templates/blank.problem';
  956: 	&Apache::lonxml::debug("$templatefilename");
  957: 	my $dest = &Apache::lonnet::filelocation("",$request->uri);
  958: 	copy($templatefilename,$dest);
  959: 	&renderpage($request,$dest);
  960:     } else {
  961: 	my $url=&HTML::Entities::encode($request->uri,'<>&"');
  962: 	my $shownurl=$url;	
  963: 	$shownurl=~s-^/~-/priv/-;
  964: 	my $dest = &Apache::lonnet::filelocation("",$request->uri);
  965: 	my $errormsg;
  966: 	if ($env{'form.newfile'}) {
  967: 	    $errormsg='<p><font color="red">'.&mt('You did not select a template.').'</font></p>'."\n";
  968: 	}
  969: 	my $instructions;
  970: 	my $start_page = 
  971: 	    &Apache::loncommon::start_page("Create New $extension");
  972: 	if ($templatelist) { $instructions=&mt(", select a template from the pull-down menu below.").'<br />'.&mt("Then");}
  973: 	my %lt=&Apache::lonlocal::texthash( 'create' => 'Creating a new',
  974: 			  'resource' => 'resource',
  975: 			  'requested' => 'The requested file',
  976: 			  'not exist' => 'currently does not exist',
  977: 			  'createnew' => 'To create a new',
  978: 			  'click' => 'click on the',
  979: 			  'Create' => 'Create',
  980: 			  'button' => 'button');
  981: 	$request->print(<<ENDNEWPROBLEM);
  982: $start_page
  983: <h1>$lt{'create'} $extension $lt{'resource'}</h1>
  984: $errormsg
  985: $lt{'requested'} <tt>$shownurl</tt> $lt{'not exist'}.
  986: <p>
  987: <b>$lt{'createnew'} $extension$instructions $lt{'click'} "$lt{'Create'} $extension" $lt{'button'}.</b>
  988: </p>
  989: <p><form action="$url" method="POST">
  990: ENDNEWPROBLEM
  991: 	if (defined($templatelist)) {
  992: 	    $request->print("<select name=\"template\">$templatelist</select>");
  993: 	}
  994: 	$request->print("<br /><input type=\"submit\" name=\"newfile\" value=\"".&mt('Create')." $extension\" />");
  995: 	$request->print("</form></p>".&Apache::loncommon::end_page());
  996:     }
  997:     return '';
  998: }
  999: 
 1000: sub handler {
 1001:     #my $t0 = [&gettimeofday()];
 1002:     my $request=$_[0];
 1003:     $Apache::lonxml::request=$request;
 1004:     $Apache::lonxml::debug=$env{'user.debug'};
 1005:     $env{'request.uri'}=$request->uri;
 1006:     &setuppermissions();
 1007:     # some times multiple problemmodes are submitted, need to select
 1008:     # the last one
 1009:     if ( defined($env{'form.problemmode'}) && ref($env{'form.problemmode'}) ) {
 1010: 	my $mode=$env{'form.problemmode'}->[-1];
 1011: 	undef $env{'form.problemmode'};
 1012: 	$env{'form.problemmode'}=$mode;
 1013:     }
 1014: 
 1015:     my $file=&Apache::lonnet::filelocation("",$request->uri);
 1016: 
 1017:     #check if we know where we are
 1018:     if ($env{'request.course.fn'} && !&Apache::lonnet::symbread()) { 
 1019: 	# if we are browsing we might not be able to know where we are
 1020: 	if ($Apache::lonhomework::browse ne 'F' && 
 1021: 	    $env{'request.state'} ne "construct") {
 1022: 	    #should know where we are, so ask
 1023: 	    $request->internal_redirect('/adm/ambiguous'); return OK;
 1024: 	}
 1025:     }
 1026:     if (&setupheader($request)) { return OK; }
 1027:     &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:$Apache::lonhomework::modifygrades:$Apache::lonhomework::queuegrade");
 1028:     &Apache::lonxml::debug("Problem Mode ".$env{'form.problemmode'});
 1029:     my ($symb) = &Apache::lonxml::whichuser();
 1030:     &Apache::lonxml::debug('symb is '.$symb);
 1031:     if ($env{'request.state'} eq "construct") {
 1032: 	if ( -e $file ) {
 1033: 	    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 1034: 						    ['problemmode']);
 1035: 	    if (!(defined $env{'form.problemmode'})) {
 1036: 		#first visit to problem in construction space
 1037: 		$env{'form.problemmode'}='View';
 1038: 		&renderpage($request,$file);
 1039: 	    } elsif ($env{'form.problemmode'} eq &mt('EditXML') ||
 1040: 		     $env{'form.problemmode'} eq 'EditXML') {
 1041: 		&editxmlmode($request,$file);
 1042: 	    } elsif ($env{'form.problemmode'} eq &mt('Calculate answers')) {
 1043: 		&analyze($request,$file);
 1044: 	    } else {
 1045: 		&renderpage($request,$file);
 1046: 	    }
 1047: 	} else {
 1048: 	    # requested file doesn't exist in contruction space
 1049: 	    &newproblem($request);
 1050: 	}
 1051:     } else {
 1052: 	# just render the page normally outside of construction space
 1053: 	&Apache::lonxml::debug("not construct");
 1054: 	&renderpage($request,$file);
 1055:     }
 1056:     #my $td=&tv_interval($t0);
 1057:     #&Apache::lonxml::debug("Spent $td seconds processing");
 1058:     # always turn off debug messages
 1059:     $Apache::lonxml::debug=0;
 1060:     return OK;
 1061: 
 1062: }
 1063: 
 1064: 1;
 1065: __END__

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