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

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

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