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

1.63      albertel    1: # The LearningOnline Network with CAPA
1.52      albertel    2: # The LON-CAPA Homework handler
1.63      albertel    3: #
1.210   ! albertel    4: # $Id: lonhomework.pm,v 1.209 2005/05/20 18:07:29 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'});
                     89: 	}
                     90: 	if ($env{'form.webgrade'} &&
                     91: 	    $Apache::lonhomework::modifygrades eq 'F') {
                     92: 	    return ('grade','webgrade');
1.145     albertel   93: 	}
1.204     albertel   94: 	if ( defined($env{'form.submitted'}) &&
                     95: 	     ( !defined($env{'form.resetdata'})) &&
                     96: 	     ( !defined($env{'form.newrandomization'}))) {
1.145     albertel   97: 	    return ('grade', 'web');
                     98: 	} else {
                     99: 	    return ('web');
                    100: 	}
1.204     albertel  101:     } elsif ($env{'request.state'} eq "construct") {
                    102: 	if ( defined($env{'form.grade_target'}) ) {
                    103: 	    return ($env{'form.grade_target'});
1.145     albertel  104: 	}
1.204     albertel  105: 	if ( defined($env{'form.preview'})) {
                    106: 	    if ( defined($env{'form.submitted'})) {
1.145     albertel  107: 		return ('grade', 'web');
                    108: 	    } else {
                    109: 		return ('web');
                    110: 	    }
                    111: 	} else {
1.204     albertel  112: 	    if ( $env{'form.problemmode'} eq &mt('View') ||
                    113: 		 $env{'form.problemmode'} eq &mt('Discard Edits and View')) {
                    114: 		if ( defined($env{'form.submitted'}) &&
                    115: 		     (!defined($env{'form.resetdata'})) &&
                    116: 		     (!defined($env{'form.newrandomization'}))) {
1.145     albertel  117: 		    return ('grade', 'web','answer');
                    118: 		} else {
                    119: 		    return ('web','answer');
                    120: 		}
1.204     albertel  121: 	    } elsif ( $env{'form.problemmode'} eq &mt('Edit') ||
                    122: 		      $env{'form.problemmode'} eq 'Edit') {
                    123: 		if ( $env{'form.submitted'} eq 'edit' ) {
                    124: 		    if ( $env{'form.submit'} eq &mt('Submit Changes and View') ) {
1.145     albertel  125: 			return ('modified','web','answer');
                    126: 		    } else {
                    127: 			return ('modified','edit');
                    128: 		    }
                    129: 		} else {
                    130: 		    return ('edit');
                    131: 		}
                    132: 	    } else {
                    133: 		return ('web');
                    134: 	    }
                    135: 	}
1.15      albertel  136:     }
1.145     albertel  137:     return ();
1.5       albertel  138: }
                    139: 
1.3       albertel  140: sub setup_vars {
1.145     albertel  141:     my ($target) = @_;
                    142:     return ';'
1.11      albertel  143: #  return ';$external::target='.$target.';';
1.2       albertel  144: }
                    145: 
                    146: sub send_header {
1.145     albertel  147:     my ($request)= @_;
                    148:     $request->print(&Apache::lontexconvert::header());
1.204     albertel  149: #  $request->print('<form name='.$env{'form.request.prefix'}.'lonhomework method="POST" action="'.$request->uri.'">');
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.2       albertel  162: sub send_footer {
1.145     albertel  163:     my ($request)= @_;
1.16      albertel  164: #  $request->print('</form>');
1.145     albertel  165:     $request->print(&Apache::lontexconvert::footer());
1.2       albertel  166: }
                    167: 
1.200     albertel  168: sub proctor_checked_in {
1.207     albertel  169:     my ($slot_name,$slot)=@_;
1.202     albertel  170:     my @allowed=split(",",$slot->{'proctor'});
1.208     albertel  171:     my $version=$Apache::lonhomework::history{'resource.version'};
1.202     albertel  172:     foreach my $possible (@allowed) { 
1.208     albertel  173: 	if ($Apache::lonhomework::history{"resource.$version.checkedin"} eq
                    174: 	    $possible &&
                    175: 	    $Apache::lonhomework::history{"resource.$version.checkedin.slot"}
                    176: 	       eq $slot_name) {
1.202     albertel  177: 	    return 1;
                    178: 	}
                    179:     }
1.200     albertel  180:     return 0;
                    181: }
                    182: 
1.52      albertel  183: $Apache::lonxml::browse='';
1.152     albertel  184: sub check_ip_acc {
                    185:     my ($acc)=@_;
1.154     albertel  186:     if (!defined($acc) || $acc =~ /^\s*$/) { return 1; }
1.152     albertel  187:     my $allowed=0;
                    188:     my $ip=$ENV{'REMOTE_ADDR'};
                    189:     my $name;
                    190:     foreach my $pattern (split(',',$acc)) {
                    191: 	if ($pattern =~ /\*$/) {
                    192: 	    #35.8.*
                    193: 	    $pattern=~s/\*//;
                    194: 	    if ($ip =~ /^\Q$pattern\E/) { $allowed=1; }
                    195: 	} elsif ($pattern =~ /(\d+\.\d+\.\d+)\.\[(\d+)-(\d+)\]$/) {    
                    196: 	    #35.8.3.[34-56]
                    197: 	    my $low=$2;
                    198: 	    my $high=$3;
                    199: 	    $pattern=$1;
                    200: 	    if ($ip =~ /^\Q$pattern\E/) { 
                    201: 		my $last=(split(/\./,$ip))[3];
                    202: 		if ($last <=$high && $last >=$low) { $allowed=1; }
                    203: 	    }
                    204: 	} elsif ($pattern =~ /^\*/) {
                    205: 	    #*.msu.edu
                    206: 	    $pattern=~s/\*//;
                    207: 	    if (!defined($name)) {
                    208: 		use Socket;
                    209: 		my $netaddr=inet_aton($ip);
                    210: 		($name)=gethostbyaddr($netaddr,AF_INET);
1.158     albertel  211: 	    }
1.152     albertel  212: 	    if ($name =~ /\Q$pattern\E$/i) { $allowed=1; }
                    213: 	} elsif ($pattern =~ /\d+\.\d+\.\d+\.\d+/) {
                    214: 	    #127.0.0.1
                    215: 	    if ($ip =~ /^\Q$pattern\E/) { $allowed=1; }
                    216: 	} else {
                    217: 	    #some.name.com
                    218: 	    if (!defined($name)) {
                    219: 		use Socket;
                    220: 		my $netaddr=inet_aton($ip);
                    221: 		($name)=gethostbyaddr($netaddr,AF_INET);
                    222: 	    }
                    223: 	    if ($name =~ /\Q$pattern\E$/i) { $allowed=1; }
                    224: 	}
                    225: 	if ($allowed) { last; }
                    226:     }
                    227:     return $allowed;
                    228: }
1.198     albertel  229: 
                    230: sub check_task_access {
1.207     albertel  231:     # does it pass normal muster
                    232:     # yes we really do want the default args passing
1.200     albertel  233:     my ($status,$datemsg)=&check_access;
                    234:     if ($status eq 'SHOW_ANSWER' ||
                    235: 	$status eq 'CLOSED' ||
                    236: 	$status eq 'CANNOT_ANSWER' ||
                    237: 	$status eq 'INVALID_ACCESS' ||
                    238: 	$status eq 'UNAVAILABLE') {
                    239: 	return ($status,$datemsg);
                    240:     }
1.204     albertel  241:     if ($env{'request.state'} eq "construct") {
1.203     albertel  242: 	return ($status,$datemsg);
                    243:     }
1.208     albertel  244:     my $version=$Apache::lonhomework::history{'resource.version'};
1.209     albertel  245:     if ($Apache::lonhomework::history{"resource.$version.checkedin"} &&
                    246: 	$Apache::lonhomework::history{"resource.$version.status"} eq 'pass') {
                    247: 	return ('SHOW_ANSWER');
1.207     albertel  248:     }
1.200     albertel  249:     my ($id)=@_;
1.210   ! albertel  250:     my @slots=
        !           251: 	(split(':',&Apache::lonnet::EXT("resource.$id.availablestudent")),
        !           252: 	 split(':',&Apache::lonnet::EXT("resource.$id.available")));
        !           253: 
1.200     albertel  254: #    if (!@slots) {
                    255: #	return ($status,$datemsg);
                    256: #    }
                    257:     my $slotstatus='NOT_IN_A_SLOT';
1.206     albertel  258:     my ($returned_slot,$slot_name);
1.210   ! albertel  259:     foreach my $slot (@slots) {
1.201     albertel  260: 	&Apache::lonxml::debug("getting $slot");
1.200     albertel  261: 	my %slot=&Apache::lonnet::get_slot($slot);
1.201     albertel  262: 	&Apache::lonhomework::showhash(%slot);
1.200     albertel  263: 	if ($slot{'starttime'} < time &&
                    264: 	    $slot{'endtime'} > time &&
1.201     albertel  265: 	    &check_ip_acc($slot{'ip'})) {
1.202     albertel  266: 	    &Apache::lonxml::debug("$slot is good");
                    267: 	    $slotstatus='NEEDS_CHECKIN';
                    268: 	    $returned_slot=\%slot;
1.206     albertel  269: 	    $slot_name=$slot;
1.200     albertel  270: 	    last;
                    271: 	}
                    272:     }
1.202     albertel  273:     if ($slotstatus eq 'NEEDS_CHECKIN' &&
1.207     albertel  274: 	&proctor_checked_in($slot_name,$returned_slot)) {
1.202     albertel  275: 	&Apache::lonxml::debug("protoctor checked in");
1.200     albertel  276: 	$slotstatus='CAN_ANSWER';
                    277:     }
1.209     albertel  278:     if ( $slotstatus eq 'NOT_IN_A_SLOT' && 
1.208     albertel  279: 	$Apache::lonhomework::history{"resource.$version.checkedin"}) {
1.209     albertel  280: 	if ($Apache::lonhomework::history{"resource.$version.status"} eq 'fail') {
                    281: 	    return ('SHOW_ANSWER');
                    282: 	} else {
                    283: 	    return ('WAITING_FOR_GRADE');
                    284: 	}
1.207     albertel  285:     }
1.206     albertel  286:     return ($slotstatus,$datemsg,$slot_name,$returned_slot);
1.198     albertel  287: }
1.200     albertel  288: 
1.92      bowersj2  289: # JB, 9/24/2002: Any changes in this function may require a change
                    290: # in lonnavmaps::resource::getDateStatus.
1.53      www       291: sub check_access {
1.145     albertel  292:     my ($id) = @_;
                    293:     my $date ='';
                    294:     my $status;
                    295:     my $datemsg = '';
                    296:     my $lastdate = '';
                    297:     my $temp;
                    298:     my $type;
                    299:     my $passed;
                    300: 
1.204     albertel  301:     if ($env{'request.state'} eq "construct") {
                    302: 	if ($env{'form.problemstate'}) {
                    303: 	    if ($env{'form.problemstate'} =~ /^CANNOT_ANSWER/) {
                    304: 		if ( ! ($env{'form.problemstate'} eq 'CANNOT_ANSWER_correct' &&
1.167     albertel  305: 			lc($Apache::lonhomework::problemstatus) eq 'no')) {
1.168     albertel  306: 		    return ('CANNOT_ANSWER',
1.174     www       307: 			    &mt('is in this state due to author settings.'));
1.167     albertel  308: 		}
1.165     albertel  309: 	    } else {
1.204     albertel  310: 		return ($env{'form.problemstate'},
1.174     www       311: 			&mt('is in this state due to author settings.'));
1.165     albertel  312: 	    }
                    313: 	}
1.145     albertel  314: 	&Apache::lonxml::debug("in construction ignoring dates");
                    315: 	$status='CAN_ANSWER';
1.146     albertel  316: 	$datemsg=&mt('is in under construction');
1.163     albertel  317: #	return ($status,$datemsg);
1.145     albertel  318:     }
                    319: 
                    320:     &Apache::lonxml::debug("checking for part :$id:");
                    321:     &Apache::lonxml::debug("time:".time);
1.152     albertel  322: 
1.204     albertel  323:     if ($env{'request.state'} ne "construct") {
1.163     albertel  324: 	my $allowed=&check_ip_acc(&Apache::lonnet::EXT("resource.$id.acc"));
                    325: 	if (!$allowed && ($Apache::lonhomework::browse ne 'F')) {
                    326: 	    $status='INVALID_ACCESS';
                    327: 	    $date=&mt("can not be accessed from your location.");
1.145     albertel  328: 	    return($status,$date);
                    329: 	}
1.163     albertel  330: 	
                    331: 	foreach $temp ("opendate","duedate","answerdate") {
                    332: 	    $lastdate = $date;
                    333: 	    $date = &Apache::lonnet::EXT("resource.$id.$temp");
                    334: 	    my $thistype = &Apache::lonnet::EXT("resource.$id.$temp.type");
                    335: 	    if ($thistype =~ /^(con_lost|no_such_host)/ ||
                    336: 		$date     =~ /^(con_lost|no_such_host)/) {
                    337: 		$status='UNAVAILABLE';
                    338: 		$date=&mt("may open later.");
                    339: 		return($status,$date);
                    340: 	    }
                    341: 	    if ($thistype eq 'date_interval') {
                    342: 		if ($temp eq 'opendate') {
                    343: 		    $date=&Apache::lonnet::EXT("resource.$id.duedate")-$date;
                    344: 		}
                    345: 		if ($temp eq 'answerdate') {
                    346: 		    $date=&Apache::lonnet::EXT("resource.$id.duedate")+$date;
                    347: 		}
1.145     albertel  348: 	    }
1.163     albertel  349: 	    &Apache::lonxml::debug("found :$date: for :$temp:");
                    350: 	    if ($date eq '') {
                    351: 		$date = &mt("an unknown date"); $passed = 0;
                    352: 	    } elsif ($date eq 'con_lost') {
                    353: 		$date = &mt("an indeterminate date"); $passed = 0;
                    354: 	    } else {
                    355: 		if (time < $date) { $passed = 0; } else { $passed = 1; }
                    356: 		$date = localtime $date;
1.145     albertel  357: 	    }
1.163     albertel  358: 	    if (!$passed) { $type=$temp; last; }
1.145     albertel  359: 	}
1.163     albertel  360: 	&Apache::lonxml::debug("have :$type:$passed:");
                    361: 	if ($passed) {
                    362: 	    $status='SHOW_ANSWER';
                    363: 	    $datemsg=$date;
                    364: 	} elsif ($type eq 'opendate') {
                    365: 	    $status='CLOSED';
                    366: 	    $datemsg = &mt("will open on")." $date";
                    367: 	} elsif ($type eq 'duedate') {
                    368: 	    $status='CAN_ANSWER';
                    369: 	    $datemsg = &mt("is due at")." $date";
                    370: 	} elsif ($type eq 'answerdate') {
                    371: 	    $status='CLOSED';
                    372: 	    $datemsg = &mt("was due on")." $lastdate".&mt(", and answers will be available on")." $date";
1.145     albertel  373: 	}
                    374:     }
                    375:     if ($status eq 'CAN_ANSWER') {
                    376: 	#check #tries, and if correct.
                    377: 	my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
                    378: 	my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
                    379: 	if ( $tries eq '' ) { $tries = '0'; }
1.164     albertel  380: 	if ( $maxtries eq '' && 
1.204     albertel  381: 	     $env{'request.state'} ne 'construct') { $maxtries = '2'; } 
1.164     albertel  382: 	if ($maxtries && $tries >= $maxtries) { $status = 'CANNOT_ANSWER'; }
1.145     albertel  383: 	# if (correct and show prob status) or excused then CANNOT_ANSWER
                    384: 	if(($Apache::lonhomework::history{"resource.$id.solved"}=~/^correct/
                    385: 	    &&
                    386: 	    lc($Apache::lonhomework::problemstatus) ne 'no')
                    387: 	   ||
                    388: 	   $Apache::lonhomework::history{"resource.$id.solved"}=~/^excused/) {
                    389: 	    $status = 'CANNOT_ANSWER';
                    390: 	}
1.121     albertel  391:     }
1.181     albertel  392:     if ($status eq 'CAN_ANSWER' || $status eq 'CANNOT_ANSWER') {
1.177     albertel  393: 	my $interval=&Apache::lonnet::EXT("resource.$id.interval");
                    394: 	&Apache::lonxml::debug("looking for interval $interval");
                    395: 	if ($interval) {
                    396: 	    my $first_access=&Apache::lonnet::get_first_access('map');
                    397: 	    &Apache::lonxml::debug("looking for accesstime $first_access");
                    398: 	    if (!$first_access) {
                    399: 		$status='NOT_YET_VIEWED';
1.192     albertel  400: 		$datemsg=&seconds_to_human_length($interval);
1.177     albertel  401: 	    } else {
                    402: 		my $newdate=localtime($first_access+$interval);
                    403: 		if (time > ($first_access+$interval)) {
                    404: 		    $status='CLOSED';
                    405: 		    $datemsg = &mt("was due on")." $newdate".&mt(", and answers will be available on")." $date";
                    406: 		} else {
                    407: 		    $datemsg = &mt("is due at")." $newdate";
                    408: 		}
                    409: 	    }
                    410: 	}
                    411:     }
1.133     albertel  412:   #if (($status ne 'CLOSED') && ($Apache::lonhomework::type eq 'exam') &&
                    413:   #    (!$Apache::lonhomework::history{"resource.0.outtoken"})) {
                    414:   #    return ('UNCHECKEDOUT','needs to be checked out');
                    415:   #}
1.54      www       416: 
                    417: 
1.145     albertel  418:     &Apache::lonxml::debug("sending back :$status:$datemsg:");
                    419:     if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) {
                    420: 	&Apache::lonxml::debug("should be allowed to browse a resource when closed");
                    421: 	$status='CAN_ANSWER';
1.146     albertel  422: 	$datemsg=&mt('is closed but you are allowed to view it');
1.145     albertel  423:     }
1.106     albertel  424: 
1.145     albertel  425:     return ($status,$datemsg);
1.20      albertel  426: }
                    427: 
1.192     albertel  428: sub seconds_to_human_length {
                    429:     my ($length)=@_;
                    430: 
                    431:     my $seconds=$length%60; $length=int($length/60);
                    432:     my $minutes=$length%60; $length=int($length/60);
                    433:     my $hours=$length%24;   $length=int($length/24);
                    434:     my $days=$length;
                    435: 
                    436:     my $timestr;
                    437:     if ($days > 0) { $timestr.=&mt('[quant,_1,day]',$days); }
                    438:     if ($hours > 0) { $timestr.=($timestr?", ":"").
                    439: 			  &mt('[quant,_1,hour]',$hours); }
                    440:     if ($minutes > 0) { $timestr.=($timestr?", ":"").
                    441: 			    &mt('[quant,_1,minute]',$minutes); }
                    442:     if ($seconds > 0) { $timestr.=($timestr?", ":"").
                    443: 			    &mt('[quant,_1,second]',$seconds); }
                    444:     return $timestr;
                    445: }
                    446: 
1.41      albertel  447: sub showhash {
1.145     albertel  448:     my (%hash) = @_;
                    449:     &showhashsubset(\%hash,'.');
                    450:     return '';
1.79      albertel  451: }
                    452: 
1.106     albertel  453: sub showarray {
                    454:     my ($array)=@_;
                    455:     my $string="(";
                    456:     foreach my $elm (@{ $array }) {
1.193     albertel  457: 	if (ref($elm) eq 'ARRAY') {
                    458: 	    $string.=&showarray($elm);
                    459: 	} elsif (ref($elm) eq 'HASH') {
                    460: 	    $string.= "HASH --- \n<br />";
                    461: 	    $string.= &showhashsubset($elm,'.');
1.106     albertel  462: 	} else {
                    463: 	    $string.="$elm,"
                    464: 	}
                    465:     }
                    466:     chop($string);
                    467:     $string.=")";
                    468:     return $string;
                    469: }
                    470: 
1.79      albertel  471: sub showhashsubset {
1.145     albertel  472:     my ($hash,$keyre) = @_;
                    473:     my $resultkey;
                    474:     foreach $resultkey (sort keys %$hash) {
1.193     albertel  475: 	if ($resultkey !~ /$keyre/) { next; }
                    476: 	if (ref($$hash{$resultkey})  eq 'ARRAY' ) {
                    477: 	    &Apache::lonxml::debug("$resultkey ---- ".
                    478: 				   &showarray($$hash{$resultkey}));
                    479: 	} elsif (ref($$hash{$resultkey}) eq 'HASH' ) {
                    480: 	    &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
                    481: 	    &showhashsubset($$hash{$resultkey},'.');
                    482: 	} else {
                    483: 	    &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
1.145     albertel  484: 	}
                    485:     }
                    486:     &Apache::lonxml::debug("\n<br />restored values^</br>\n");
                    487:     return '';
1.41      albertel  488: }
                    489: 
                    490: sub setuppermissions {
1.204     albertel  491:     $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$env{'request.filename'});
                    492:     my $viewgrades = &Apache::lonnet::allowed('vgr',$env{'request.course.id'});
1.145     albertel  493:     if (! $viewgrades && 
1.204     albertel  494: 	exists($env{'request.course.sec'}) && 
                    495: 	$env{'request.course.sec'} !~ /^\s*$/) {
                    496: 	$viewgrades = &Apache::lonnet::allowed('vgr',$env{'request.course.id'}.
                    497:                                                '/'.$env{'request.course.sec'});
1.145     albertel  498:     }
1.188     foxr      499:     $Apache::lonhomework::viewgrades = $viewgrades; # File global variable...dirt.
1.185     albertel  500:     if ($Apache::lonhomework::browse eq 'F' && 
1.204     albertel  501: 	$env{'form.devalidatecourseresdata'} eq 'on') {
1.185     albertel  502: 	my (undef,$courseid) = &Apache::lonxml::whichuser();
1.204     albertel  503: 	&Apache::lonnet::devalidatecourseresdata($env{"course.$courseid.num"},
                    504: 					      $env{"course.$courseid.domain"});
1.185     albertel  505:     }
1.205     albertel  506:     my $modifygrades = &Apache::lonnet::allowed('mgr',$env{'request.course.id'});
                    507:     if (! $modifygrades && 
                    508: 	exists($env{'request.course.sec'}) && 
                    509: 	$env{'request.course.sec'} !~ /^\s*$/) {
                    510: 	$modifygrades = 
                    511: 	    &Apache::lonnet::allowed('mgr',$env{'request.course.id'}.
                    512: 				     '/'.$env{'request.course.sec'});
                    513:     }
                    514:     $Apache::lonhomework::modifygrades = $modifygrades;
                    515:     return '';
1.41      albertel  516: }
                    517: 
                    518: sub setupheader {
1.120     albertel  519:     my $request=$_[0];
1.197     albertel  520:     &Apache::loncommon::content_type($request,'text/html');
1.120     albertel  521:     if (!$Apache::lonxml::debug && ($ENV{'REQUEST_METHOD'} eq 'GET')) {
                    522: 	&Apache::loncommon::no_cache($request);
                    523:     }
1.196     albertel  524: #    $request->set_last_modified(&Apache::lonnet::metadata($request->uri,
                    525: #							  'lastrevisiondate'));
1.120     albertel  526:     $request->send_http_header;
                    527:     return OK if $request->header_only;
                    528:     return ''
1.41      albertel  529: }
1.35      albertel  530: 
1.47      albertel  531: sub handle_save_or_undo {
1.145     albertel  532:     my ($request,$problem,$result) = @_;
                    533:     my $file    = &Apache::lonnet::filelocation("",$request->uri);
                    534:     my $filebak =$file.".bak";
                    535:     my $filetmp =$file.".tmp";
                    536:     my $error=0;
1.156     albertel  537: 
                    538:     &Apache::lonnet::correct_line_ends($result);
1.52      albertel  539: 
1.204     albertel  540:     if ($env{'form.Undo'} eq &mt('undo')) {
1.145     albertel  541: 	my $error=0;
                    542: 	if (!copy($file,$filetmp)) { $error=1; }
                    543: 	if ((!$error) && (!copy($filebak,$file))) { $error=1; }
                    544: 	if ((!$error) && (!move($filetmp,$filebak))) { $error=1; }
                    545: 	if (!$error) {
1.171     albertel  546: 	    &Apache::lonxml::info("<p><b>".&mt("Undid changes, Switched")." $filebak ".&mt("and")." $file</b></p>");
1.145     albertel  547: 	} else {
1.171     albertel  548: 	    &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  549: 	    $error=1;
                    550: 	}
1.52      albertel  551:     } else {
1.145     albertel  552: 	my $fs=Apache::File->new(">$filebak");
                    553: 	if (defined($fs)) {
                    554: 	    print $fs $$problem;
1.171     albertel  555: 	    &Apache::lonxml::info("<b>".&mt("Making Backup to").
                    556: 				  " $filebak</b>");
1.145     albertel  557: 	} else {
1.171     albertel  558: 	    &Apache::lonxml::info("<font color=\"red\" size=\"+1\"><b>".&mt("Unable to make backup")." $filebak</b></font>");
1.145     albertel  559: 	    $error=2;
                    560: 	}
                    561: 	my $fh=Apache::File->new(">$file");
                    562: 	if (defined($fh)) {
                    563: 	    print $fh $$result;
1.171     albertel  564: 	    &Apache::lonxml::info("<b>".&mt("Saving Modifications to").
                    565: 				  " $file</b>");
1.145     albertel  566: 	} else {
1.171     albertel  567: 	    &Apache::lonxml::info("<font color=\"red\" size=\"+1\"><b>".
                    568: 				  &mt("Unable to write to")." $file</b></font>");
1.145     albertel  569: 	    $error|=4;
                    570: 	}
1.52      albertel  571:     }
1.145     albertel  572:     return $error;
1.64      albertel  573: }
                    574: 
1.101     albertel  575: sub analyze_header {
                    576:     my ($request) = @_;
1.183     albertel  577:     my $bodytag='<body bgcolor="#ffffff">';
1.204     albertel  578:     if ($env{'environment.remote'} eq 'off') {
1.183     albertel  579: 	$bodytag=&Apache::loncommon::bodytag();
                    580:     }
1.197     albertel  581:     my $html=&Apache::lonxml::xmlbegin();
                    582:     my $result.=$html.'
1.146     albertel  583:             <head><title>'.&mt("Analyzing a problem").'</title></head>
1.183     albertel  584:             '.$bodytag.&Apache::lonxml::message_location().'
1.101     albertel  585:             <form name="lonhomework" method="POST" action="'.
1.204     albertel  586: 	    &HTML::Entities::encode($env{'request.uri'},'<>&"').'">'.
1.179     albertel  587: 	    &Apache::structuretags::remember_problem_state().'
1.146     albertel  588:             <input type="submit" name="problemmode" value="'.&mt("EditXML").'" />
                    589:             <input type="submit" name="problemmode" value="'.&mt('Edit').'" />
1.101     albertel  590:             <hr />
1.146     albertel  591:             <input type="submit" name="submit" value="'.&mt("View").'" />
1.101     albertel  592:             <hr />
                    593:             </form>';
1.171     albertel  594:     &Apache::lonxml::add_messages(\$result);
1.101     albertel  595:     $request->print($result);
                    596:     $request->rflush();
                    597: }
                    598: 
1.109     albertel  599: sub analyze_footer {
                    600:     my ($request) = @_;
                    601:     my $result='</body></html>';
                    602:     $request->print($result);
                    603:     $request->rflush();
                    604: }
                    605: 
1.74      albertel  606: sub analyze {
1.101     albertel  607:     my ($request,$file) = @_;
                    608:     &Apache::lonxml::debug("Analyze");
                    609:     my $result;
                    610:     my %overall;
                    611:     my %allparts;
1.204     albertel  612:     my $rndseed=$env{'form.rndseed'};
1.101     albertel  613:     &analyze_header($request);
1.114     albertel  614:     my %prog_state=
1.146     albertel  615: 	&Apache::lonhtmlcommon::Create_PrgWin($request,&mt('Analyze Progress'),
                    616: 					      &mt('Getting Problem Variants'),
1.204     albertel  617: 					      $env{'form.numtoanalyze'},
1.175     albertel  618: 					      'inline',undef);
1.204     albertel  619:     for(my $i=1;$i<$env{'form.numtoanalyze'}+1;$i++) {
1.114     albertel  620: 	&Apache::lonhtmlcommon::Increment_PrgWin($request,\%prog_state,
1.146     albertel  621: 						 &mt('last problem'));
1.182     albertel  622: 	if (&Apache::loncommon::connection_aborted($request)) { return; }
1.101     albertel  623: 	my $subresult=&Apache::lonnet::ssi($request->uri,
                    624: 					   ('grade_target' => 'analyze'),
1.130     albertel  625: 					   ('rndseed' => $i+$rndseed));
1.101     albertel  626: 	(my $garbage,$subresult)=split(/_HASH_REF__/,$subresult,2);
                    627: 	my %analyze=&Apache::lonnet::str2hash($subresult);
1.114     albertel  628: 	my @parts;
                    629: 	if (defined(@{ $analyze{'parts'} })) {
                    630: 	    @parts=@{ $analyze{'parts'} };
                    631: 	}
1.101     albertel  632: 	foreach my $part (@parts) {
                    633: 	    if (!exists($allparts{$part})) {$allparts{$part}=1;};
1.109     albertel  634: 	    if ($analyze{$part.'.type'} eq 'numericalresponse'	||
                    635: 		$analyze{$part.'.type'} eq 'stringresponse'	||
                    636: 		$analyze{$part.'.type'} eq 'formularesponse'   ) {
1.101     albertel  637: 		push( @{ $overall{$part.'.answer'} },
                    638: 		      [@{ $analyze{$part.'.answer'} }]);
                    639: 	    }
                    640: 	}
                    641:     }
1.114     albertel  642:     &Apache::lonhtmlcommon::Update_PrgWin($request,\%prog_state,
1.146     albertel  643: 					  &mt('Analyzing Results'));
1.175     albertel  644:     $request->print('<hr />'.&mt('List of possible answers').': ');
1.134     albertel  645:     foreach my $part (sort(keys(%allparts))) {
1.109     albertel  646: 	if (defined(@{ $overall{$part.'.answer'} })) {
1.132     albertel  647: 	    my $num_cols=scalar(@{ $overall{$part.'.answer'}->[0] });
1.184     albertel  648: 	    $request->print('<table><tr><th colspan="'.($num_cols+1).'">'.&mt('Part').' '.$part.'</th></tr>');
1.130     albertel  649: 	    my %frequency;
1.109     albertel  650: 	    foreach my $answer (sort {$a->[0] <=> $b->[0]} (@{ $overall{$part.'.answer'} })) {
1.132     albertel  651: 		$frequency{join("\0",@{ $answer })}++;
1.130     albertel  652: 	    }
1.184     albertel  653: 	    $request->print('<tr><th colspan="'.($num_cols).'">'.&mt('Answer').'</th><th>'.&mt('Frequency').'</th></tr>');
1.132     albertel  654: 	    foreach my $answer (sort {(split("\0",$a))[0] <=> (split("\0",$b))[0]} (keys(%frequency))) {
                    655: 		$request->print('<tr><td align="right">'.
                    656: 				join('</td><td align="right">',split("\0",$answer)).
1.130     albertel  657: 				'</td><td>('.$frequency{$answer}.
                    658: 				')</td></tr>');
1.109     albertel  659: 	    }
                    660: 	    $request->print('</table>');
                    661: 	} else {
1.162     albertel  662: 	    $request->print('<p>'.&mt('Response').' '.$part.' '.
1.146     albertel  663: 			    &mt('is not analyzable at this time').'</p>');
1.101     albertel  664: 	}
                    665:     }
1.130     albertel  666:     if (scalar(keys(%allparts)) == 0 ) {
1.162     albertel  667: 	$request->print('<p>'.&mt('Found no analyzable respones in this problem, currently only Numerical, Formula and String response styles are supported.').'</p>');
1.130     albertel  668:     }
1.114     albertel  669:     &Apache::lonhtmlcommon::Close_PrgWin($request,\%prog_state);
1.109     albertel  670:     &analyze_footer($request);
1.101     albertel  671:     &Apache::lonhomework::showhash(%overall);
                    672:     return $result;
1.74      albertel  673: }
                    674: 
1.64      albertel  675: sub editxmlmode {
1.145     albertel  676:     my ($request,$file) = @_;
                    677:     my $result;
                    678:     my $problem=&Apache::lonnet::getfile($file);
                    679:     if ($problem eq -1) {
1.146     albertel  680: 	&Apache::lonxml::error("<b> ".&mt('Unable to find').
                    681: 			       " <i>$file</i></b>");
1.145     albertel  682: 	$problem='';
                    683:     }
1.204     albertel  684:     if (defined($env{'form.editxmltext'}) || defined($env{'form.Undo'})) {
1.145     albertel  685: 	my $error=&handle_save_or_undo($request,\$problem,
1.204     albertel  686: 				       \$env{'form.editxmltext'});
1.145     albertel  687: 	if (!$error) { $problem=&Apache::lonnet::getfile($file); }
                    688:     }
1.204     albertel  689:     &Apache::lonhomework::showhashsubset(\%env,'^form');
                    690:     if ( $env{'form.submit'} eq &mt('Submit Changes and View') ) {
                    691: 	&Apache::lonhomework::showhashsubset(\%env,'^form');
                    692: 	$env{'form.problemmode'}='View';
1.145     albertel  693: 	&renderpage($request,$file);
                    694:     } else {
                    695: 	my ($rows,$cols) = &Apache::edit::textarea_sizes(\$problem);
1.160     www       696: 	my $xml_help = '<table><tr><td>'.
                    697: 	    &Apache::loncommon::helpLatexCheatsheet("Problem_Editor_XML_Index",
                    698: 						    "Problem Editing Help").
                    699: 						    '</td><td>'.
1.178     albertel  700:        &Apache::loncommon::help_open_menu('',undef,undef,undef,5,'Authoring').
                    701:                 '</td></tr></table>';
1.145     albertel  702: 	if ($cols > 80) { $cols = 80; }
                    703: 	if ($cols < 70) { $cols = 70; }
                    704: 	if ($rows < 20) { $rows = 20; }
1.183     albertel  705: 	my $bodytag='<body bgcolor="#ffffff">';
1.204     albertel  706: 	if ($env{'environment.remote'} eq 'off') {
1.183     albertel  707: 	    $bodytag=&Apache::loncommon::bodytag();
                    708: 	}
1.197     albertel  709: 	my $html=&Apache::lonxml::xmlbegin();
                    710: 	$result.=$html.$bodytag.&Apache::lonxml::message_location().'
1.64      albertel  711:             <form name="lonhomework" method="POST" action="'.
1.204     albertel  712: 	    &HTML::Entities::encode($env{'request.uri'},'<>&"').'">'.
1.179     albertel  713: 	    &Apache::structuretags::remember_problem_state().'
1.146     albertel  714:             <input type="hidden" name="problemmode" value="'.&mt('EditXML').'" />
1.170     matthew   715:             <input type="submit" name="problemmode" accesskey="d" value="'.&mt('Discard Edits and View').'" />
                    716:             <input type="submit" name="problemmode" accesskey="e" value="'.&mt('Edit').'" />
1.64      albertel  717:             <hr />
1.170     matthew   718:             <input type="submit" name="submit" accesskey="s" value="'.&mt('Submit Changes').'" />
                    719:             <input type="submit" name="submit" accesskey="v" value="'.&mt('Submit Changes and View').'" />
                    720:             <input type="submit" name="Undo" accesskey="u" value="'.&mt('undo').'" />
1.64      albertel  721:             <hr />
1.110     albertel  722:             ' . $xml_help . '
1.199     albertel  723:             <textarea style="width:100%" rows="'.$rows.'" cols="'.$cols.'" name="editxmltext">'.
1.172     albertel  724: 	    &HTML::Entities::encode($problem,'<>&"').'</textarea>
1.64      albertel  725:             </form></body></html>';
1.171     albertel  726: 	&Apache::lonxml::add_messages(\$result);
1.145     albertel  727: 	$request->print($result);
                    728:     }
                    729:     return '';
1.47      albertel  730: }
1.189     albertel  731: 
1.188     foxr      732: #
                    733: #    Render the page in whatever target desired.
                    734: #
1.41      albertel  735: sub renderpage {
1.145     albertel  736:     my ($request,$file) = @_;
1.52      albertel  737: 
1.145     albertel  738:     my (@targets) = &get_target();
1.204     albertel  739:     &Apache::lonhomework::showhashsubset(\%env,'form.');
1.145     albertel  740:     &Apache::lonxml::debug("Running targets ".join(':',@targets));
1.171     albertel  741:     my $overall_result;
1.145     albertel  742:     foreach my $target (@targets) {
1.183     albertel  743: 	# FIXME need to do something intelligent when a problem goes
                    744:         # from viewable to not viewable due to map conditions
                    745: 	#&setuppermissions();
                    746: 	#if (   $Apache::lonhomework::browse ne '2'
                    747: 	#    && $Apache::lonhomework::browse ne 'F' ) {
                    748: 	#    $request->print(" You most likely shouldn't see me.");
                    749: 	#}
1.145     albertel  750: 	#my $t0 = [&gettimeofday()];
                    751: 	my $problem=&Apache::lonnet::getfile($file);
                    752: 	if ($problem eq -1) {
1.146     albertel  753: 	    &Apache::lonxml::error("<b> ".&mt('Unable to find')." <i>$file</i></b>");
1.145     albertel  754: 	    $problem='';
                    755: 	}
1.52      albertel  756: 
1.145     albertel  757: 	my %mystyle;
                    758: 	my $result = '';
                    759: 	if ($target eq 'analyze') { %Apache::lonhomework::analyze=(); }
                    760: 	if ($target eq 'answer') { &showhash(%Apache::lonhomework::history); }
1.204     albertel  761: 	if ($target eq 'web') {&Apache::lonhomework::showhashsubset(\%env,'^form');}
1.145     albertel  762: 
                    763: 	&Apache::lonxml::debug("Should be parsing now");
                    764: 	$result = &Apache::lonxml::xmlparse($request, $target, $problem,
                    765: 					    &setup_vars($target),%mystyle);
                    766: 	undef($Apache::lonhomework::parsing_a_problem);
                    767: 	#$request->print("Result follows:");
                    768: 	if ($target eq 'modified') {
                    769: 	    &handle_save_or_undo($request,\$problem,\$result);
                    770: 	} else {
                    771: 	    if ($target eq 'analyze') {
                    772: 		$result=&Apache::lonnet::hashref2str(\%Apache::lonhomework::analyze);
                    773: 		undef(%Apache::lonhomework::analyze);
                    774: 	    }
                    775: 	    #my $td=&tv_interval($t0);
                    776: 	    #if ( $Apache::lonxml::debug) {
                    777: 	    #$result =~ s:</body>::;
                    778: 	    #$result.="<br />Spent $td seconds processing target $target\n</body>";
                    779: 	    #}
1.171     albertel  780: #	    $request->print($result);
                    781: 	    $overall_result.=$result;
                    782: #	    $request->rflush();
1.145     albertel  783: 	}
                    784: 	#$request->print(":Result ends");
                    785: 	#my $td=&tv_interval($t0);
1.52      albertel  786:     }
1.171     albertel  787:     &Apache::lonxml::add_messages(\$overall_result);
                    788:     $request->print($overall_result);   
                    789:     $request->rflush();   
1.41      albertel  790: }
                    791: 
1.42      albertel  792: # with no arg it returns a HTML <option> list of the template titles
                    793: # with one arg it returns the filename associated with the arg passed
                    794: sub get_template_list {
1.145     albertel  795:     my ($namewanted,$extension) = @_;
                    796:     my $result;
                    797:     my @allnames;
                    798:     &Apache::lonxml::debug("Looking for :$extension:");
                    799:     foreach my $file (</home/httpd/html/res/adm/includes/templates/*.$extension>) {
                    800: 	my $name=&Apache::lonnet::metadata($file,'title');
                    801: 	if ($namewanted && ($name eq $namewanted)) {
                    802: 	    $result=$file;
                    803: 	    last;
                    804: 	} else {
                    805: 	    if ($name) { push (@allnames, $name); }
                    806: 	}
                    807:     }
                    808:     if (@allnames && !$result) {
1.146     albertel  809: 	$result="<option>".&mt("Select a")." $extension ".&mt('template')."</option>\n<option>".
1.145     albertel  810: 	    join('</option><option>',sort(@allnames)).'</option>';
1.42      albertel  811:     }
1.145     albertel  812:     return $result;
1.42      albertel  813: }
                    814: 
                    815: sub newproblem {
1.65      matthew   816:     my ($request) = @_;
                    817:     my $extension=$request->uri;
                    818:     $extension=~s:^.*\.([\w]+)$:$1:;
                    819:     &Apache::lonxml::debug("Looking for :$extension:");
1.131     albertel  820:     my $templatelist=&get_template_list('',$extension);
1.204     albertel  821:     if ($env{'form.template'} &&
                    822: 	$env{'form.template'} ne "Select a $extension template") {
1.65      matthew   823: 	use File::Copy;
1.204     albertel  824: 	my $file = &get_template_list($env{'form.template'},$extension);
1.65      matthew   825: 	my $dest = &Apache::lonnet::filelocation("",$request->uri);
                    826: 	copy($file,$dest);
                    827: 	&renderpage($request,$dest);
1.204     albertel  828:     } elsif($env{'form.newfile'} && !$templatelist) {
1.131     albertel  829: 	# I don't like hard-coded filenames but for now, this will work.
                    830: 	use File::Copy;
                    831: 	my $templatefilename =
                    832: 	    $request->dir_config('lonIncludes').'/templates/blank.problem';
                    833: 	&Apache::lonxml::debug("$templatefilename");
                    834: 	my $dest = &Apache::lonnet::filelocation("",$request->uri);
                    835: 	copy($templatefilename,$dest);
                    836: 	&renderpage($request,$dest);
1.85      albertel  837:     } else {
1.176     albertel  838: 	my $url=&HTML::Entities::encode($request->uri,'<>&"');
                    839: 	my $shownurl=$url;	
1.157     albertel  840: 	$shownurl=~s-^/~-/priv/-;
1.65      matthew   841: 	my $dest = &Apache::lonnet::filelocation("",$request->uri);
1.128     albertel  842: 	my $errormsg;
1.204     albertel  843: 	if ($env{'form.newfile'}) {
1.146     albertel  844: 	    $errormsg='<p><font color="red">'.&mt('You did not select a template.').'</font></p>'."\n";
1.128     albertel  845: 	}
1.85      albertel  846: 	my $instructions;
1.186     albertel  847: 	my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,
1.204     albertel  848: 					($env{'environment.remote'} ne 'off'));
1.146     albertel  849: 	if ($templatelist) { $instructions=&mt(", select a template from the pull-down menu below.").'<br />'.&mt("Then");}
1.147     albertel  850: 	my %lt=&Apache::lonlocal::texthash( 'create' => 'Creating a new',
1.146     albertel  851: 			  'resource' => 'resource',
                    852: 			  'requested' => 'The requested file',
                    853: 			  'not exist' => 'currently does not exist',
                    854: 			  'createnew' => 'To create a new',
                    855: 			  'click' => 'click on the',
                    856: 			  'Create' => 'Create',
                    857: 			  'button' => 'button');
1.65      matthew   858: 	$request->print(<<ENDNEWPROBLEM);
1.159     www       859: $bodytag
1.148     albertel  860: <h1>$lt{'create'} $extension $lt{'resource'}</h1>
1.128     albertel  861: $errormsg
1.157     albertel  862: $lt{'requested'} <tt>$shownurl</tt> $lt{'not exist'}.
1.105     www       863: <p>
1.146     albertel  864: <b>$lt{'createnew'} $extension$instructions $lt{'click'} "$lt{'Create'} $extension" $lt{'button'}.</b>
1.105     www       865: </p>
                    866: <p><form action="$url" method="POST">
1.42      albertel  867: ENDNEWPROBLEM
1.85      albertel  868: 	if (defined($templatelist)) {
                    869: 	    $request->print("<select name=\"template\">$templatelist</select>");
                    870: 	}
1.146     albertel  871: 	$request->print("<br /><input type=\"submit\" name=\"newfile\" value=\"".&mt('Create')." $extension\" />");
1.105     www       872: 	$request->print("</form></p></body>");
1.65      matthew   873:     }
                    874:     return '';
1.42      albertel  875: }
                    876: 
                    877: sub view_or_edit_menu {
1.145     albertel  878:     my ($request) = @_;
1.176     albertel  879:     my $url=&HTML::Entities::encode($request->uri,'<>&"');
1.147     albertel  880:     my %lt=&Apache::lonlocal::texthash( 'would' => 'Would you like to',
1.146     albertel  881: 		      'view' => 'View',
                    882: 		      'Edit' => 'edit',
                    883: 		      'or' => 'or',
                    884: 		      'the problem' => 'the problem');
1.145     albertel  885:     $request->print(<<EDITMENU);
1.42      albertel  886: <body bgcolor="#FFFFFF">
                    887: <form action="$url" method="POST">
1.170     matthew   888: $lt{'would'} <input type="submit" name="problemmode" accesskey="v" value="&lt{'view'}">
                    889: &lt{'or'} <input type="submit" name="problemmode" accesskey="e" value="&lt{'Edit'}">
1.146     albertel  890: &lt{'the problem'}.
1.42      albertel  891: </form>
                    892: </body>
                    893: EDITMENU
                    894: }
                    895: 
1.41      albertel  896: sub handler {
1.145     albertel  897:     #my $t0 = [&gettimeofday()];
                    898:     my $request=$_[0];
                    899:     
1.204     albertel  900:     $Apache::lonxml::debug=$env{'user.debug'};
                    901:     $env{'request.uri'}=$request->uri;
1.180     albertel  902:     &setuppermissions();
1.145     albertel  903:     # some times multiple problemmodes are submitted, need to select
                    904:     # the last one
1.204     albertel  905:     if ( defined($env{'form.problemmode'}) && ref($env{'form.problemmode'}) ) {
                    906: 	my $mode=$env{'form.problemmode'}->[-1];
                    907: 	undef $env{'form.problemmode'};
                    908: 	$env{'form.problemmode'}=$mode;
1.145     albertel  909:     }
1.193     albertel  910: 
1.145     albertel  911:     my $file=&Apache::lonnet::filelocation("",$request->uri);
                    912: 
                    913:     #check if we know where we are
1.204     albertel  914:     if ($env{'request.course.fn'} && !&Apache::lonnet::symbread()) { 
1.145     albertel  915: 	# if we are browsing we might not be able to know where we are
1.173     albertel  916: 	if ($Apache::lonhomework::browse ne 'F' && 
1.204     albertel  917: 	    $env{'request.state'} ne "construct") {
1.145     albertel  918: 	    #should know where we are, so ask
                    919: 	    if ( &Apache::lonnet::mod_perl_version() == 2 ) {
                    920: 		&Apache::lonnet::cleanenv();
                    921: 	    }
1.190     albertel  922: 	    &Apache::lonnet::logthis(&Apache::lonnet::mod_perl_version());
                    923: 	    $request->internal_redirect('/adm/ambiguous'); return OK;
1.145     albertel  924: 	}
                    925:     }
1.187     albertel  926:     if (&setupheader($request)) { return OK; }
1.193     albertel  927:     &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:");
1.204     albertel  928:     &Apache::lonxml::debug("Problem Mode ".$env{'form.problemmode'});
1.145     albertel  929:     my ($symb) = &Apache::lonxml::whichuser();
                    930:     &Apache::lonxml::debug('symb is '.$symb);
1.204     albertel  931:     if ($env{'request.state'} eq "construct" || $symb eq '') {
                    932: 	if ($env{'form.resetdata'} eq &mt('Reset Submissions') ||
                    933: 	    $env{'form.resetdata'} eq &mt('New Problem Variation') ||
                    934: 	    $env{'form.newrandomization'} eq &mt('New Randomization')) {
1.145     albertel  935: 	    my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
                    936: 	    &Apache::lonnet::tmpreset($symb,'',$domain,$name);
                    937: 	    &Apache::lonxml::debug("Attempt reset");
                    938: 	}
                    939:     }
1.204     albertel  940:     if ($env{'request.state'} eq "construct") {
1.145     albertel  941: 	if ( -e $file ) {
                    942: 	    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                    943: 						    ['problemmode']);
1.204     albertel  944: 	    if (!(defined $env{'form.problemmode'})) {
1.145     albertel  945: 		#first visit to problem in construction space
                    946: 		#&view_or_edit_menu($request);
1.204     albertel  947: 		$env{'form.problemmode'}='View';
1.145     albertel  948: 		&renderpage($request,$file);
1.204     albertel  949: 	    } elsif ($env{'form.problemmode'} eq &mt('EditXML') ||
                    950: 		     $env{'form.problemmode'} eq 'EditXML') {
1.145     albertel  951: 		&editxmlmode($request,$file);
1.204     albertel  952: 	    } elsif ($env{'form.problemmode'} eq &mt('Calculate answers')) {
1.145     albertel  953: 		&analyze($request,$file);
                    954: 	    } else {
                    955: 		&renderpage($request,$file);
                    956: 	    }
                    957: 	} else {
                    958: 	    # requested file doesn't exist in contruction space
                    959: 	    &newproblem($request);
                    960: 	}
                    961:     } else {
                    962: 	# just render the page normally outside of construction space
                    963: 	&Apache::lonxml::debug("not construct");
1.52      albertel  964: 	&renderpage($request,$file);
1.41      albertel  965:     }
1.145     albertel  966:     #my $td=&tv_interval($t0);
                    967:     #&Apache::lonxml::debug("Spent $td seconds processing");
                    968:     # &Apache::lonhomework::send_footer($request);
                    969:     # always turn off debug messages
                    970:     $Apache::lonxml::debug=0;
                    971:     return OK;
1.52      albertel  972: 
1.1       albertel  973: }
                    974: 
                    975: 1;
                    976: __END__

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