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

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

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