--- loncom/homework/lonhomework.pm 2005/05/10 20:29:26 1.207 +++ loncom/homework/lonhomework.pm 2006/04/21 15:27:30 1.233.2.2 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # The LON-CAPA Homework handler # -# $Id: lonhomework.pm,v 1.207 2005/05/10 20:29:26 albertel Exp $ +# $Id: lonhomework.pm,v 1.233.2.2 2006/04/21 15:27:30 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -124,10 +124,10 @@ sub get_target { if ( $env{'form.submit'} eq &mt('Submit Changes and View') ) { return ('modified','web','answer'); } else { - return ('modified','edit'); + return ('modified','no_output_web','edit'); } } else { - return ('edit'); + return ('no_output_web','edit'); } } else { return ('web'); @@ -166,26 +166,40 @@ sub send_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 ) { + my ($slot_name,$slot,$type)=@_; + my @possible_proctors=split(",",$slot->{'proctor'}); + + my $key; + if ($type eq 'Task') { + my $version=$Apache::lonhomework::history{'resource.0.version'}; + $key ="resource.$version.0.checkedin"; + } elsif ($type eq 'problem') { + $key ='resource.0.checkedin'; + } + + foreach my $possible (@possible_proctors) { + if ($Apache::lonhomework::history{$key} eq $possible + && $Apache::lonhomework::history{$key.'.slot'} eq $slot_name) { return 1; } } + return 0; } $Apache::lonxml::browse=''; sub check_ip_acc { my ($acc)=@_; - if (!defined($acc) || $acc =~ /^\s*$/) { return 1; } + &Apache::lonxml::debug("acc is $acc"); + if (!defined($acc) || $acc =~ /^\s*$/ || $acc =~/^\s*no\s*$/i) { + return 1; + } my $allowed=0; my $ip=$ENV{'REMOTE_ADDR'}; my $name; foreach my $pattern (split(',',$acc)) { + $pattern =~ s/^\s*//; + $pattern =~ s/\s*$//; if ($pattern =~ /\*$/) { #35.8.* $pattern=~s/\*//; @@ -225,13 +239,19 @@ sub check_ip_acc { return $allowed; } -sub check_task_access { +sub check_slot_access { + my ($id,$type)=@_; + # does it pass normal muster - # yes we really do want the default args passing - my ($status,$datemsg)=&check_access; + my ($status,$datemsg)=&check_access($id); + + my $useslots = &Apache::lonnet::EXT("resource.$id.useslots"); + if ($useslots ne 'resource') { + return ($status,$datemsg); + } + if ($status eq 'SHOW_ANSWER' || $status eq 'CLOSED' || - $status eq 'CANNOT_ANSWER' || $status eq 'INVALID_ACCESS' || $status eq 'UNAVAILABLE') { return ($status,$datemsg); @@ -240,20 +260,25 @@ sub check_task_access { return ($status,$datemsg); } - if ($Apache::lonhomework::history{'resource.checkedin'}) { - if ($Apache::lonhomework::history{'resource.status'} eq 'pass' || - $Apache::lonhomework::history{'resource.status'} eq 'fail') { + if ($type eq 'Task') { + my $version=$Apache::lonhomework::history{'resource.version'}; + if ($Apache::lonhomework::history{"resource.$version.0.checkedin"} && + $Apache::lonhomework::history{"resource.$version.0.status"} eq 'pass') { return ('SHOW_ANSWER'); } } - my ($id)=@_; - my @slots=split(':',&Apache::lonnet::EXT("resource.$id.available")); + + my @slots= + (split(':',&Apache::lonnet::EXT("resource.$id.availablestudent")), + 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)) { + foreach my $slot (@slots) { + $slot =~ s/(^\s*|\s*$)//g; &Apache::lonxml::debug("getting $slot"); my %slot=&Apache::lonnet::get_slot($slot); &Apache::lonhomework::showhash(%slot); @@ -268,14 +293,47 @@ sub check_task_access { } } if ($slotstatus eq 'NEEDS_CHECKIN' && - &proctor_checked_in($slot_name,$returned_slot)) { + &proctor_checked_in($slot_name,$returned_slot,$type)) { &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'); + + my ($is_correct,$got_grade,$checkedin); + if ($type eq 'Task') { + my $version=$Apache::lonhomework::history{'resource.0.version'}; + $got_grade = + ($Apache::lonhomework::history{"resource.$version.0.status"} + =~ /^(?:pass|fail)$/); + $is_correct = + ($Apache::lonhomework::history{"resource.$version.0.status"} eq 'pass' + || $Apache::lonhomework::history{"resource.0.solved"} =~ /^correct_/ ); + $checkedin = + $Apache::lonhomework::history{"resource.$version.0.checkedin"}; + } elsif ($type eq 'problem') { + $got_grade = 1; + $checkedin = $Apache::lonhomework::history{"resource.0.checkedin"}; + } + + &Apache::lonxml::debug(" slot is $slotstatus checkedin ($checkedin) got_grade ($got_grade) is_correct ($is_correct)"); + + if ($slotstatus eq 'NOT_IN_A_SLOT' + && $checkedin ) { + + if ($got_grade) { + return ('SHOW_ANSWER'); + } else { + return ('WAITING_FOR_GRADE'); + } + + } + if ( $is_correct) { + return ('SHOW_ANSWER'); + } + if ( $status eq 'CANNOT_ANSWER' && + ($slotstatus ne 'NEEDS_CHECKIN' && $slotstatus ne 'NOT_IN_A_SLOT')) { + return ($status,$datemsg); } + return ($slotstatus,$datemsg,$slot_name,$returned_slot); } @@ -287,7 +345,6 @@ sub check_access { my $status; my $datemsg = ''; my $lastdate = ''; - my $temp; my $type; my $passed; @@ -313,6 +370,9 @@ sub check_access { &Apache::lonxml::debug("checking for part :$id:"); &Apache::lonxml::debug("time:".time); + my ($symb)=&Apache::lonxml::whichuser(); + &Apache::lonxml::debug("symb:".$symb); + #if ($env{'request.state'} ne "construct" && $symb ne '') { if ($env{'request.state'} ne "construct") { my $allowed=&check_ip_acc(&Apache::lonnet::EXT("resource.$id.acc")); if (!$allowed && ($Apache::lonhomework::browse ne 'F')) { @@ -321,7 +381,7 @@ sub check_access { return($status,$date); } - foreach $temp ("opendate","duedate","answerdate") { + foreach my $temp ("opendate","duedate","answerdate") { $lastdate = $date; $date = &Apache::lonnet::EXT("resource.$id.$temp"); my $thistype = &Apache::lonnet::EXT("resource.$id.$temp.type"); @@ -365,7 +425,8 @@ sub check_access { $datemsg = &mt("was due on")." $lastdate".&mt(", and answers will be available on")." $date"; } } - if ($status eq 'CAN_ANSWER') { + if ($status eq 'CAN_ANSWER' || + (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED'))) { #check #tries, and if correct. my $tries = $Apache::lonhomework::history{"resource.$id.tries"}; my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries"); @@ -567,10 +628,8 @@ sub handle_save_or_undo { sub analyze_header { my ($request) = @_; - my $bodytag=''; - if ($env{'environment.remote'} eq 'off') { - $bodytag=&Apache::loncommon::bodytag(); - } + my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef, + ($env{'environment.remote'} ne 'off')); my $html=&Apache::lonxml::xmlbegin(); my $result.=$html.' '.&mt("Analyzing a problem").' @@ -601,6 +660,7 @@ sub analyze { &Apache::lonxml::debug("Analyze"); my $result; my %overall; + my %seedexample; my %allparts; my $rndseed=$env{'form.rndseed'}; &analyze_header($request); @@ -613,9 +673,10 @@ sub analyze { &Apache::lonhtmlcommon::Increment_PrgWin($request,\%prog_state, &mt('last problem')); if (&Apache::loncommon::connection_aborted($request)) { return; } + my $thisseed=$i+$rndseed; my $subresult=&Apache::lonnet::ssi($request->uri, ('grade_target' => 'analyze'), - ('rndseed' => $i+$rndseed)); + ('rndseed' => $thisseed)); (my $garbage,$subresult)=split(/_HASH_REF__/,$subresult,2); my %analyze=&Apache::lonnet::str2hash($subresult); my @parts; @@ -627,8 +688,13 @@ sub analyze { if ($analyze{$part.'.type'} eq 'numericalresponse' || $analyze{$part.'.type'} eq 'stringresponse' || $analyze{$part.'.type'} eq 'formularesponse' ) { + my $concatanswer=join("\0",@{ $analyze{$part.'.answer'} }); + if (($concatanswer eq '') || ($concatanswer=~/^\@/)) { + @{$analyze{$part.'.answer'}}=(''.&mt('Error').''); + } push( @{ $overall{$part.'.answer'} }, [@{ $analyze{$part.'.answer'} }]); + $seedexample{join("\0",@{ $analyze{$part.'.answer'}})}=$thisseed; } } } @@ -638,17 +704,18 @@ sub analyze { foreach my $part (sort(keys(%allparts))) { if (defined(@{ $overall{$part.'.answer'} })) { my $num_cols=scalar(@{ $overall{$part.'.answer'}->[0] }); - $request->print(''); + $request->print('
'.&mt('Part').' '.$part.'
'); my %frequency; foreach my $answer (sort {$a->[0] <=> $b->[0]} (@{ $overall{$part.'.answer'} })) { $frequency{join("\0",@{ $answer })}++; } - $request->print(''); + $request->print(''); foreach my $answer (sort {(split("\0",$a))[0] <=> (split("\0",$b))[0]} (keys(%frequency))) { - $request->print(''); + $request->print(''); } $request->print('
'.&mt('Part').' '.$part.'
'.&mt('Answer').''.&mt('Frequency').'
'.&mt('Answer').''.&mt('Frequency').'
(' + .&mt('click for example').')
'. - join('',split("\0",$answer)). - '('.$frequency{$answer}. - ')
'. + join('',split("\0",$answer)). + ''.$frequency{$answer}. + '
'); } else { @@ -657,7 +724,7 @@ sub analyze { } } if (scalar(keys(%allparts)) == 0 ) { - $request->print('

'.&mt('Found no analyzable respones in this problem, currently only Numerical, Formula and String response styles are supported.').'

'); + $request->print('

'.&mt('Found no analyzable responses in this problem, currently only Numerical, Formula and String response styles are supported.').'

'); } &Apache::lonhtmlcommon::Close_PrgWin($request,\%prog_state); &analyze_footer($request); @@ -695,26 +762,31 @@ sub editxmlmode { if ($cols > 80) { $cols = 80; } if ($cols < 70) { $cols = 70; } if ($rows < 20) { $rows = 20; } - my $bodytag=''; - if ($env{'environment.remote'} eq 'off') { - $bodytag=&Apache::loncommon::bodytag(); - } + my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef, + ($env{'environment.remote'} ne 'off')); my $html=&Apache::lonxml::xmlbegin(); - $result.=$html.$bodytag.&Apache::lonxml::message_location().' -
EditXML $file". + &Apache::edit::js_change_detection().""; + + $result.=$html.$head.$bodytag. + &renderpage($request,$file,['no_output_web'],1). + &Apache::lonxml::message_location().' + '. &Apache::structuretags::remember_problem_state().' - + +
-
' . $xml_help . ' - +
+ +
'; &Apache::lonxml::add_messages(\$result); $request->print($result); @@ -726,9 +798,9 @@ sub editxmlmode { # Render the page in whatever target desired. # sub renderpage { - my ($request,$file) = @_; + my ($request,$file,$targets,$return_string) = @_; - my (@targets) = &get_target(); + my @targets = @{$targets || [&get_target()]}; &Apache::lonhomework::showhashsubset(\%env,'form.'); &Apache::lonxml::debug("Running targets ".join(':',@targets)); my $overall_result; @@ -741,22 +813,28 @@ sub renderpage { # $request->print(" You most likely shouldn't see me."); #} #my $t0 = [&gettimeofday()]; + my $output=1; + if ($target eq 'no_output_web') { + $target = 'web'; $output=0; + } my $problem=&Apache::lonnet::getfile($file); + my $result; if ($problem eq -1) { - &Apache::lonxml::error(" ".&mt('Unable to find')." $file"); + my $filename=(split('/',$file))[-1]; + $result.=" ".&mt('Unable to find')." $filename"; $problem=''; } my %mystyle; - my $result = ''; if ($target eq 'analyze') { %Apache::lonhomework::analyze=(); } if ($target eq 'answer') { &showhash(%Apache::lonhomework::history); } if ($target eq 'web') {&Apache::lonhomework::showhashsubset(\%env,'^form');} &Apache::lonxml::debug("Should be parsing now"); - $result = &Apache::lonxml::xmlparse($request, $target, $problem, - &setup_vars($target),%mystyle); + $result .= &Apache::lonxml::xmlparse($request, $target, $problem, + &setup_vars($target),%mystyle); undef($Apache::lonhomework::parsing_a_problem); + if (!$output) { $result = ''; } #$request->print("Result follows:"); if ($target eq 'modified') { &handle_save_or_undo($request,\$problem,\$result); @@ -777,9 +855,13 @@ sub renderpage { #$request->print(":Result ends"); #my $td=&tv_interval($t0); } - &Apache::lonxml::add_messages(\$overall_result); - $request->print($overall_result); - $request->rflush(); + if (!$return_string) { + &Apache::lonxml::add_messages(\$overall_result); + $request->print($overall_result); + $request->rflush(); + } else { + return $overall_result; + } } # with no arg it returns a HTML