--- loncom/homework/lonhomework.pm 2005/05/20 18:07:29 1.209 +++ loncom/homework/lonhomework.pm 2006/05/09 19:03:02 1.243 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # The LON-CAPA Homework handler # -# $Id: lonhomework.pm,v 1.209 2005/05/20 18:07:29 albertel Exp $ +# $Id: lonhomework.pm,v 1.243 2006/05/09 19:03:02 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'); @@ -143,12 +143,6 @@ sub setup_vars { # return ';$external::target='.$target.';'; } -sub send_header { - my ($request)= @_; - $request->print(&Apache::lontexconvert::header()); -# $request->print('
'); -} - sub createmenu { my ($which,$request)=@_; if ($which eq 'grade') { @@ -159,35 +153,41 @@ sub createmenu { } } -sub send_footer { - my ($request)= @_; -# $request->print('
'); - $request->print(&Apache::lontexconvert::footer()); -} - sub proctor_checked_in { - my ($slot_name,$slot)=@_; - my @allowed=split(",",$slot->{'proctor'}); - my $version=$Apache::lonhomework::history{'resource.version'}; - foreach my $possible (@allowed) { - if ($Apache::lonhomework::history{"resource.$version.checkedin"} eq - $possible && - $Apache::lonhomework::history{"resource.$version.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/\*//; @@ -227,13 +227,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' && $useslots ne 'sequence') { + 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); @@ -241,19 +247,26 @@ sub check_task_access { if ($env{'request.state'} eq "construct") { return ($status,$datemsg); } - my $version=$Apache::lonhomework::history{'resource.version'}; - if ($Apache::lonhomework::history{"resource.$version.checkedin"} && - $Apache::lonhomework::history{"resource.$version.status"} eq 'pass') { - return ('SHOW_ANSWER'); + + 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,18 +281,55 @@ 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' && - $Apache::lonhomework::history{"resource.$version.checkedin"}) { - if ($Apache::lonhomework::history{"resource.$version.status"} eq 'fail') { + + 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)"); + + # no slot is currently open, and has been checked in for this version + # but hasn't got a grade, therefore must be awaiting a grade + if (!defined($slot_name) + && $checkedin + && !$got_grade) { + return ('WAITING_FOR_GRADE'); + } + + 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); } @@ -291,7 +341,6 @@ sub check_access { my $status; my $datemsg = ''; my $lastdate = ''; - my $temp; my $type; my $passed; @@ -317,6 +366,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')) { @@ -325,7 +377,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"); @@ -369,7 +421,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"); @@ -571,14 +624,11 @@ sub handle_save_or_undo { 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().' + my $result = + &Apache::loncommon::start_page('Analyzing a problem',undef); + + $result .= + &Apache::lonxml::message_location().'
'. &Apache::structuretags::remember_problem_state().' @@ -595,8 +645,7 @@ sub analyze_header { sub analyze_footer { my ($request) = @_; - my $result=''; - $request->print($result); + $request->print(&Apache::loncommon::end_page()); $request->rflush(); } @@ -605,6 +654,7 @@ sub analyze { &Apache::lonxml::debug("Analyze"); my $result; my %overall; + my %seedexample; my %allparts; my $rndseed=$env{'form.rndseed'}; &analyze_header($request); @@ -617,9 +667,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; @@ -631,8 +682,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; } } } @@ -642,17 +698,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 { @@ -661,7 +718,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); @@ -699,27 +756,30 @@ 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 $html=&Apache::lonxml::xmlbegin(); - $result.=$html.$bodytag.&Apache::lonxml::message_location().' - '. &Apache::structuretags::remember_problem_state().' - + +
-
' . $xml_help . ' - -
'; +
+ + + '.&Apache::loncommon::end_page(); &Apache::lonxml::add_messages(\$result); $request->print($result); } @@ -730,9 +790,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; @@ -745,22 +805,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); @@ -781,9 +847,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