");
+ }
+ if (($env{'request.state'} eq "published") ||
+ ($env{'request.state'} eq "uploaded")) {
+ if ( defined($env{'form.grade_target'} )
+ && ($env{'form.grade_target'} eq 'tex')) {
+ return ($env{'form.grade_target'});
+ } elsif ( defined($env{'form.grade_target'} )
+ && ($Apache::lonhomework::viewgrades eq 'F' )) {
+ return ($env{'form.grade_target'});
+ }
+ if ($env{'form.webgrade'} &&
+ $Apache::lonhomework::modifygrades eq 'F') {
+ return ('grade','webgrade');
+ }
+ if ( defined($env{'form.submitted'}) &&
+ ( !defined($env{'form.resetdata'})) &&
+ ( !defined($env{'form.newrandomization'}))) {
+ return ('grade', 'web');
+ } else {
+ return ('web');
+ }
+ } elsif ($env{'request.state'} eq "construct") {
+ if ( defined($env{'form.grade_target'}) ) {
+ return ($env{'form.grade_target'});
+ }
+ if ( defined($env{'form.preview'})) {
+ if ( defined($env{'form.submitted'})) {
+ return ('grade', 'web');
+ } else {
+ return ('web');
+ }
+ } else {
+ if ( $env{'form.problemmode'} eq &mt('View') ||
+ $env{'form.problemmode'} eq &mt('Discard Edits and View')) {
+ if ( defined($env{'form.submitted'}) &&
+ (!defined($env{'form.resetdata'})) &&
+ (!defined($env{'form.newrandomization'}))) {
+ return ('grade', 'web','answer');
+ } else {
+ return ('web','answer');
+ }
+ } elsif ( $env{'form.problemmode'} eq &mt('Edit') ||
+ $env{'form.problemmode'} eq 'Edit') {
+ if ( $env{'form.submitted'} eq 'edit' ) {
+ if ( $env{'form.submit'} eq &mt('Submit Changes and View') ) {
+ return ('modified','web','answer');
+ } else {
+ return ('modified','edit');
+ }
+ } else {
+ return ('edit');
+ }
+ } else {
+ return ('web');
+ }
+ }
}
- }
+ return ();
}
sub setup_vars {
- my ($target) = @_;
- return ';'
+ my ($target) = @_;
+ return ';'
# return ';$external::target='.$target.';';
}
sub send_header {
- my ($request)= @_;
- $request->print(&Apache::lontexconvert::header());
- $request->print('');
- $request->print(&Apache::lontexconvert::footer());
-}
-
-sub getfilenothere {
- my ($filename) = @_;
- my $a="";
- if (! -e $filename ) {
- &Apache::lonnet::subscribe($filename);
- &Apache::lonnet::repcopy($filename);
- }
- {
- my $fh=Apache::File->new($filename);
+ my ($request)= @_;
+# $request->print('');
+ $request->print(&Apache::lontexconvert::footer());
+}
+
+sub proctor_checked_in {
+ my ($slot_name,$slot)=@_;
+ my @allowed=split(",",$slot->{'proctor'});
+ foreach my $possible (@allowed) {
+ if ($Apache::lonhomework::history{'resource.checkedin'} eq $possible &&
+ $Apache::lonhomework::history{'resource.checkedin.slot'} eq
+ $slot_name ) {
+ return 1;
+ }
+ }
+ return 0;
+}
+
+$Apache::lonxml::browse='';
+sub check_ip_acc {
+ my ($acc)=@_;
+ if (!defined($acc) || $acc =~ /^\s*$/) { return 1; }
+ my $allowed=0;
+ my $ip=$ENV{'REMOTE_ADDR'};
+ my $name;
+ foreach my $pattern (split(',',$acc)) {
+ if ($pattern =~ /\*$/) {
+ #35.8.*
+ $pattern=~s/\*//;
+ if ($ip =~ /^\Q$pattern\E/) { $allowed=1; }
+ } elsif ($pattern =~ /(\d+\.\d+\.\d+)\.\[(\d+)-(\d+)\]$/) {
+ #35.8.3.[34-56]
+ my $low=$2;
+ my $high=$3;
+ $pattern=$1;
+ if ($ip =~ /^\Q$pattern\E/) {
+ my $last=(split(/\./,$ip))[3];
+ if ($last <=$high && $last >=$low) { $allowed=1; }
+ }
+ } elsif ($pattern =~ /^\*/) {
+ #*.msu.edu
+ $pattern=~s/\*//;
+ if (!defined($name)) {
+ use Socket;
+ my $netaddr=inet_aton($ip);
+ ($name)=gethostbyaddr($netaddr,AF_INET);
+ }
+ if ($name =~ /\Q$pattern\E$/i) { $allowed=1; }
+ } elsif ($pattern =~ /\d+\.\d+\.\d+\.\d+/) {
+ #127.0.0.1
+ if ($ip =~ /^\Q$pattern\E/) { $allowed=1; }
+ } else {
+ #some.name.com
+ if (!defined($name)) {
+ use Socket;
+ my $netaddr=inet_aton($ip);
+ ($name)=gethostbyaddr($netaddr,AF_INET);
+ }
+ if ($name =~ /\Q$pattern\E$/i) { $allowed=1; }
+ }
+ if ($allowed) { last; }
+ }
+ return $allowed;
+}
+
+sub check_task_access {
+ # does it pass normal muster
+ # yes we really do want the default args passing
+ my ($status,$datemsg)=&check_access;
+ if ($status eq 'SHOW_ANSWER' ||
+ $status eq 'CLOSED' ||
+ $status eq 'CANNOT_ANSWER' ||
+ $status eq 'INVALID_ACCESS' ||
+ $status eq 'UNAVAILABLE') {
+ return ($status,$datemsg);
+ }
+ if ($env{'request.state'} eq "construct") {
+ return ($status,$datemsg);
+ }
+
+ if ($Apache::lonhomework::history{'resource.checkedin'}) {
+ if ($Apache::lonhomework::history{'resource.status'} eq 'pass' ||
+ $Apache::lonhomework::history{'resource.status'} eq 'fail') {
+ return ('SHOW_ANSWER');
+ }
+ }
+ my ($id)=@_;
+ my @slots=split(':',&Apache::lonnet::EXT("resource.$id.available"));
+# if (!@slots) {
+# return ($status,$datemsg);
+# }
+ my $slotstatus='NOT_IN_A_SLOT';
+ my ($returned_slot,$slot_name);
+ foreach my $slot (sort(@slots)) {
+ &Apache::lonxml::debug("getting $slot");
+ my %slot=&Apache::lonnet::get_slot($slot);
+ &Apache::lonhomework::showhash(%slot);
+ if ($slot{'starttime'} < time &&
+ $slot{'endtime'} > time &&
+ &check_ip_acc($slot{'ip'})) {
+ &Apache::lonxml::debug("$slot is good");
+ $slotstatus='NEEDS_CHECKIN';
+ $returned_slot=\%slot;
+ $slot_name=$slot;
+ last;
+ }
+ }
+ if ($slotstatus eq 'NEEDS_CHECKIN' &&
+ &proctor_checked_in($slot_name,$returned_slot)) {
+ &Apache::lonxml::debug("protoctor checked in");
+ $slotstatus='CAN_ANSWER';
+ }
+ if ( ($slotstatus eq 'NOT_IN_A_SLOT' || $slotstatus eq 'NEEDS_CHECKIN') &&
+ $Apache::lonhomework::history{'resource.checkedin'}) {
+ return ('WAITING_FOR_GRADE');
+ }
+ return ($slotstatus,$datemsg,$slot_name,$returned_slot);
+}
+
+# JB, 9/24/2002: Any changes in this function may require a change
+# in lonnavmaps::resource::getDateStatus.
+sub check_access {
+ my ($id) = @_;
+ my $date ='';
+ my $status;
+ my $datemsg = '';
+ my $lastdate = '';
+ my $temp;
+ my $type;
+ my $passed;
+
+ if ($env{'request.state'} eq "construct") {
+ if ($env{'form.problemstate'}) {
+ if ($env{'form.problemstate'} =~ /^CANNOT_ANSWER/) {
+ if ( ! ($env{'form.problemstate'} eq 'CANNOT_ANSWER_correct' &&
+ lc($Apache::lonhomework::problemstatus) eq 'no')) {
+ return ('CANNOT_ANSWER',
+ &mt('is in this state due to author settings.'));
+ }
+ } else {
+ return ($env{'form.problemstate'},
+ &mt('is in this state due to author settings.'));
+ }
+ }
+ &Apache::lonxml::debug("in construction ignoring dates");
+ $status='CAN_ANSWER';
+ $datemsg=&mt('is in under construction');
+# return ($status,$datemsg);
+ }
+
+ &Apache::lonxml::debug("checking for part :$id:");
+ &Apache::lonxml::debug("time:".time);
+
+ if ($env{'request.state'} ne "construct") {
+ my $allowed=&check_ip_acc(&Apache::lonnet::EXT("resource.$id.acc"));
+ if (!$allowed && ($Apache::lonhomework::browse ne 'F')) {
+ $status='INVALID_ACCESS';
+ $date=&mt("can not be accessed from your location.");
+ return($status,$date);
+ }
+
+ foreach $temp ("opendate","duedate","answerdate") {
+ $lastdate = $date;
+ $date = &Apache::lonnet::EXT("resource.$id.$temp");
+ my $thistype = &Apache::lonnet::EXT("resource.$id.$temp.type");
+ if ($thistype =~ /^(con_lost|no_such_host)/ ||
+ $date =~ /^(con_lost|no_such_host)/) {
+ $status='UNAVAILABLE';
+ $date=&mt("may open later.");
+ return($status,$date);
+ }
+ if ($thistype eq 'date_interval') {
+ if ($temp eq 'opendate') {
+ $date=&Apache::lonnet::EXT("resource.$id.duedate")-$date;
+ }
+ if ($temp eq 'answerdate') {
+ $date=&Apache::lonnet::EXT("resource.$id.duedate")+$date;
+ }
+ }
+ &Apache::lonxml::debug("found :$date: for :$temp:");
+ if ($date eq '') {
+ $date = &mt("an unknown date"); $passed = 0;
+ } elsif ($date eq 'con_lost') {
+ $date = &mt("an indeterminate date"); $passed = 0;
+ } else {
+ if (time < $date) { $passed = 0; } else { $passed = 1; }
+ $date = localtime $date;
+ }
+ if (!$passed) { $type=$temp; last; }
+ }
+ &Apache::lonxml::debug("have :$type:$passed:");
+ if ($passed) {
+ $status='SHOW_ANSWER';
+ $datemsg=$date;
+ } elsif ($type eq 'opendate') {
+ $status='CLOSED';
+ $datemsg = &mt("will open on")." $date";
+ } elsif ($type eq 'duedate') {
+ $status='CAN_ANSWER';
+ $datemsg = &mt("is due at")." $date";
+ } elsif ($type eq 'answerdate') {
+ $status='CLOSED';
+ $datemsg = &mt("was due on")." $lastdate".&mt(", and answers will be available on")." $date";
+ }
+ }
+ if ($status eq 'CAN_ANSWER') {
+ #check #tries, and if correct.
+ my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
+ my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
+ if ( $tries eq '' ) { $tries = '0'; }
+ if ( $maxtries eq '' &&
+ $env{'request.state'} ne 'construct') { $maxtries = '2'; }
+ if ($maxtries && $tries >= $maxtries) { $status = 'CANNOT_ANSWER'; }
+ # if (correct and show prob status) or excused then CANNOT_ANSWER
+ if(($Apache::lonhomework::history{"resource.$id.solved"}=~/^correct/
+ &&
+ lc($Apache::lonhomework::problemstatus) ne 'no')
+ ||
+ $Apache::lonhomework::history{"resource.$id.solved"}=~/^excused/) {
+ $status = 'CANNOT_ANSWER';
+ }
+ }
+ if ($status eq 'CAN_ANSWER' || $status eq 'CANNOT_ANSWER') {
+ my $interval=&Apache::lonnet::EXT("resource.$id.interval");
+ &Apache::lonxml::debug("looking for interval $interval");
+ if ($interval) {
+ my $first_access=&Apache::lonnet::get_first_access('map');
+ &Apache::lonxml::debug("looking for accesstime $first_access");
+ if (!$first_access) {
+ $status='NOT_YET_VIEWED';
+ $datemsg=&seconds_to_human_length($interval);
+ } else {
+ my $newdate=localtime($first_access+$interval);
+ if (time > ($first_access+$interval)) {
+ $status='CLOSED';
+ $datemsg = &mt("was due on")." $newdate".&mt(", and answers will be available on")." $date";
+ } else {
+ $datemsg = &mt("is due at")." $newdate";
+ }
+ }
+ }
+ }
+ #if (($status ne 'CLOSED') && ($Apache::lonhomework::type eq 'exam') &&
+ # (!$Apache::lonhomework::history{"resource.0.outtoken"})) {
+ # return ('UNCHECKEDOUT','needs to be checked out');
+ #}
+
+
+ &Apache::lonxml::debug("sending back :$status:$datemsg:");
+ if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) {
+ &Apache::lonxml::debug("should be allowed to browse a resource when closed");
+ $status='CAN_ANSWER';
+ $datemsg=&mt('is closed but you are allowed to view it');
+ }
+
+ return ($status,$datemsg);
+}
+
+sub seconds_to_human_length {
+ my ($length)=@_;
+
+ my $seconds=$length%60; $length=int($length/60);
+ my $minutes=$length%60; $length=int($length/60);
+ my $hours=$length%24; $length=int($length/24);
+ my $days=$length;
+
+ my $timestr;
+ if ($days > 0) { $timestr.=&mt('[quant,_1,day]',$days); }
+ if ($hours > 0) { $timestr.=($timestr?", ":"").
+ &mt('[quant,_1,hour]',$hours); }
+ if ($minutes > 0) { $timestr.=($timestr?", ":"").
+ &mt('[quant,_1,minute]',$minutes); }
+ if ($seconds > 0) { $timestr.=($timestr?", ":"").
+ &mt('[quant,_1,second]',$seconds); }
+ return $timestr;
+}
+
+sub showhash {
+ my (%hash) = @_;
+ &showhashsubset(\%hash,'.');
+ return '';
+}
+
+sub showarray {
+ my ($array)=@_;
+ my $string="(";
+ foreach my $elm (@{ $array }) {
+ if (ref($elm) eq 'ARRAY') {
+ $string.=&showarray($elm);
+ } elsif (ref($elm) eq 'HASH') {
+ $string.= "HASH --- \n
";
+ $string.= &showhashsubset($elm,'.');
+ } else {
+ $string.="$elm,"
+ }
+ }
+ chop($string);
+ $string.=")";
+ return $string;
+}
+
+sub showhashsubset {
+ my ($hash,$keyre) = @_;
+ my $resultkey;
+ foreach $resultkey (sort keys %$hash) {
+ if ($resultkey !~ /$keyre/) { next; }
+ if (ref($$hash{$resultkey}) eq 'ARRAY' ) {
+ &Apache::lonxml::debug("$resultkey ---- ".
+ &showarray($$hash{$resultkey}));
+ } elsif (ref($$hash{$resultkey}) eq 'HASH' ) {
+ &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
+ &showhashsubset($$hash{$resultkey},'.');
+ } else {
+ &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
+ }
+ }
+ &Apache::lonxml::debug("\n
restored values^\n");
+ return '';
+}
+
+sub setuppermissions {
+ $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$env{'request.filename'});
+ my $viewgrades = &Apache::lonnet::allowed('vgr',$env{'request.course.id'});
+ if (! $viewgrades &&
+ exists($env{'request.course.sec'}) &&
+ $env{'request.course.sec'} !~ /^\s*$/) {
+ $viewgrades = &Apache::lonnet::allowed('vgr',$env{'request.course.id'}.
+ '/'.$env{'request.course.sec'});
+ }
+ $Apache::lonhomework::viewgrades = $viewgrades; # File global variable...dirt.
+ if ($Apache::lonhomework::browse eq 'F' &&
+ $env{'form.devalidatecourseresdata'} eq 'on') {
+ my (undef,$courseid) = &Apache::lonxml::whichuser();
+ &Apache::lonnet::devalidatecourseresdata($env{"course.$courseid.num"},
+ $env{"course.$courseid.domain"});
+ }
+ my $modifygrades = &Apache::lonnet::allowed('mgr',$env{'request.course.id'});
+ if (! $modifygrades &&
+ exists($env{'request.course.sec'}) &&
+ $env{'request.course.sec'} !~ /^\s*$/) {
+ $modifygrades =
+ &Apache::lonnet::allowed('mgr',$env{'request.course.id'}.
+ '/'.$env{'request.course.sec'});
+ }
+ $Apache::lonhomework::modifygrades = $modifygrades;
+ return '';
+}
+
+sub setupheader {
+ my $request=$_[0];
+ &Apache::loncommon::content_type($request,'text/html');
+ if (!$Apache::lonxml::debug && ($ENV{'REQUEST_METHOD'} eq 'GET')) {
+ &Apache::loncommon::no_cache($request);
+ }
+# $request->set_last_modified(&Apache::lonnet::metadata($request->uri,
+# 'lastrevisiondate'));
+ $request->send_http_header;
+ return OK if $request->header_only;
+ return ''
+}
+
+sub handle_save_or_undo {
+ my ($request,$problem,$result) = @_;
+ my $file = &Apache::lonnet::filelocation("",$request->uri);
+ my $filebak =$file.".bak";
+ my $filetmp =$file.".tmp";
+ my $error=0;
+
+ &Apache::lonnet::correct_line_ends($result);
+
+ if ($env{'form.Undo'} eq &mt('undo')) {
+ my $error=0;
+ if (!copy($file,$filetmp)) { $error=1; }
+ if ((!$error) && (!copy($filebak,$file))) { $error=1; }
+ if ((!$error) && (!move($filetmp,$filebak))) { $error=1; }
+ if (!$error) {
+ &Apache::lonxml::info("".&mt("Undid changes, Switched")." $filebak ".&mt("and")." $file
");
+ } else {
+ &Apache::lonxml::info("".&mt("Unable to undo, unable to switch")." $filebak ".&mt("and")." $file
");
+ $error=1;
+ }
+ } else {
+ my $fs=Apache::File->new(">$filebak");
+ if (defined($fs)) {
+ print $fs $$problem;
+ &Apache::lonxml::info("".&mt("Making Backup to").
+ " $filebak");
+ } else {
+ &Apache::lonxml::info("".&mt("Unable to make backup")." $filebak");
+ $error=2;
+ }
+ my $fh=Apache::File->new(">$file");
+ if (defined($fh)) {
+ print $fh $$result;
+ &Apache::lonxml::info("".&mt("Saving Modifications to").
+ " $file");
+ } else {
+ &Apache::lonxml::info("".
+ &mt("Unable to write to")." $file");
+ $error|=4;
+ }
+ }
+ return $error;
+}
+
+sub analyze_header {
+ my ($request) = @_;
+ my $bodytag='';
+ if ($env{'environment.remote'} eq 'off') {
+ $bodytag=&Apache::loncommon::bodytag();
+ }
+ my $html=&Apache::lonxml::xmlbegin();
+ my $result.=$html.'
+ '.&mt("Analyzing a problem").'
+ '.$bodytag.&Apache::lonxml::message_location().'
+ ';
+ &Apache::lonxml::add_messages(\$result);
+ $request->print($result);
+ $request->rflush();
+}
+
+sub analyze_footer {
+ my ($request) = @_;
+ my $result='