--- loncom/homework/inputtags.pm 2007/05/29 21:14:45 1.226 +++ loncom/homework/inputtags.pm 2009/09/14 16:39:21 1.248.4.1 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # input definitons # -# $Id: inputtags.pm,v 1.226 2007/05/29 21:14:45 albertel Exp $ +# $Id: inputtags.pm,v 1.248.4.1 2009/09/14 16:39:21 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -31,7 +31,6 @@ use strict; use Apache::loncommon; use Apache::lonlocal; use Apache::lonnet; -use lib '/home/httpd/lib/perl/'; use LONCAPA; @@ -116,8 +115,7 @@ sub check_for_duplicate_ids { sub start_input { my ($parstack,$safeeval)=@_; - my $id = &Apache::lonxml::get_param('id',$parstack,$safeeval); - if ($id eq '') { $id = $Apache::lonxml::curdepth; } + my $id = &Apache::lonxml::get_id($parstack,$safeeval); push (@Apache::inputtags::input,$id); push (@Apache::inputtags::inputlist,$id); return $id; @@ -308,7 +306,9 @@ sub start_textline { $oldresponse = $oldresponse->[$#Apache::inputtags::inputlist]; } $oldresponse = &HTML::Entities::encode($oldresponse,'<>&"'); - + $oldresponse =~ s/^\s+//; + $oldresponse =~ s/\s+$//; + $oldresponse =~ s/\s+/ /g; if ($Apache::lonhomework::type ne 'exam') { my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval); $result=''; @@ -358,8 +358,15 @@ sub start_textline { && $Apache::lonhomework::type ne 'exam') { my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval); if ($size != 0) {$size=$size*2; $size.=' mm';} else {$size='40 mm';} - $result='\framebox['.$size.'][s]{\tiny\strut}'; - + if ($env{'form.pdfFormFields'} eq 'yes') { + my $fieldname = $env{'request.symb'}. + '&part_'. $Apache::inputtags::part. + '&textresponse'. + '&HWVAL_' . $Apache::inputtags::response['-1']; + $result="\n\\\\\n".'\textField{'.$fieldname.'}{'.$size.'}{12 bp}'; + } else { + $result='\framebox['.$size.'][s]{\tiny\strut}'; + } } elsif ($target eq 'tex' && $Apache::lonhomework::type eq 'exam' && &needs_exam_box($tagstack)) { @@ -420,13 +427,14 @@ sub end_hiddenline { # $part -> partid # $id -> responseid # $uploadefiletypes -> comma seperated list of extensions allowed or * for any -# $which -> 'uploadedonly' -> only newly uploaded files +# $which -> 'uploadonly' -> only newly uploaded files # 'portfolioonly' -> only allow files from portfolio # 'both' -> allow files from either location # $extratext -> additional text to go between the link and the input box +# $maxfilesize -> maximum cumulative filesize for submitted files (in MB). # returns a table row sub file_selector { - my ($part,$id,$uploadedfiletypes,$which,$extratext)=@_; + my ($part,$id,$uploadedfiletypes,$which,$extratext,$maxfilesize)=@_; if (!$uploadedfiletypes) { return ''; } my $jspart=$part; @@ -435,9 +443,16 @@ sub file_selector { my $result; $result.=''; - if ($uploadedfiletypes ne '*') { - $result.= - &mt('Allowed filetypes: [_1]',$uploadedfiletypes).'
'; + if (($uploadedfiletypes ne '*') || ($maxfilesize)) { + if ($uploadedfiletypes ne '*') { + $result.= + &mt('Allowed filetypes: [_1]',''.$uploadedfiletypes.'').'
'; + } + if ($maxfilesize) { + $result.=&mt('Combined size of files not to exceed: [_1] MB[_2].', + ''.$maxfilesize.'').'
'; + } + $result .= '
'; } if ($which eq 'uploadonly' || $which eq 'both') { $result.=&mt('Submit a file: (only one file can be uploaded)'). @@ -449,12 +464,11 @@ sub file_selector { $result.='
'.''.&mt('OR:').'
'; } if ($which eq 'portfolioonly' || $which eq 'both') { - $result.=$extratext.''. + $result.=$extratext.''. &mt('Select Portfolio Files').'
'. ''. '
'; $result .= &show_past_portfile_submission($part,$id); - } $result.=''; return $result; @@ -516,9 +530,10 @@ sub valid_award { 'UNIT_INVALID_STUDENT', 'UNIT_IRRECONCIBLE', 'UNIT_FAIL', 'NO_UNIT', 'UNIT_NOTNEEDED', 'WANTED_NUMERIC', - 'BAD_FORMULA', 'SIG_FAIL', 'INCORRECT', + 'BAD_FORMULA', 'INTERNAL_ERROR', 'SIG_FAIL', 'INCORRECT', 'MISORDERED_RANK', 'INVALID_FILETYPE', - 'DRAFT', 'SUBMITTED', 'ASSIGNED_SCORE', + 'EXCESS_FILESIZE', 'DRAFT', + 'SUBMITTED', 'ASSIGNED_SCORE', 'APPROX_ANS', 'EXACT_ANS','COMMA_FAIL') { if ($award eq $possibleaward) { return 1; } } @@ -530,10 +545,10 @@ sub valid_award { 'TOO_LONG', 'UNIT_INVALID_INSTRUCTOR', 'UNIT_INVALID_STUDENT', 'UNIT_IRRECONCIBLE', 'UNIT_FAIL', 'NO_UNIT', - 'UNIT_NOTNEEDED', 'WANTED_NUMERIC', 'BAD_FORMULA', + 'UNIT_NOTNEEDED', 'WANTED_NUMERIC', 'BAD_FORMULA', 'INTERNAL_ERROR', 'COMMA_FAIL', 'SIG_FAIL', 'INCORRECT', 'MISORDERED_RANK', - 'INVALID_FILETYPE', 'DRAFT', 'SUBMITTED', 'ASSIGNED_SCORE', - 'APPROX_ANS', 'EXACT_ANS'); + 'INVALID_FILETYPE', 'EXCESS_FILESIZE', 'DRAFT', 'SUBMITTED', + 'ASSIGNED_SCORE', 'APPROX_ANS', 'EXACT_ANS'); my $i=0; my %fwd_awards = map { ($_,$i++) } @awards; my $max=scalar(@awards); @@ -541,8 +556,29 @@ sub valid_award { $i=0; my %rev_awards = map { ($_,$i++) } @awards; +sub awarddetail_to_awarded { + my ($awarddetail) = @_; + if ($awarddetail eq 'EXACT_ANS' + || $awarddetail eq 'APPROX_ANS') { + return 1; + } + return 0; +} + +sub hide_award { + my ($award) = @_; + if (&Apache::lonhomework::show_no_problem_status()) { + return 1; + } + if ($award =~ + /^(?:EXACT_ANS|APPROX_ANS|SUBMITTED|ASSIGNED_SCORE|INCORRECT)/) { + return 1; + } + return 0; +} + sub finalizeawards { - my ($awardref,$msgref,$nameref,$reverse)=@_; + my ($awardref,$msgref,$nameref,$reverse,$final_scantron)=@_; my $result; if ($#$awardref == -1) { $result = "NO_RESPONSE"; } if ($result eq '' ) { @@ -553,10 +589,32 @@ sub finalizeawards { $blankcount++; } } - if ($blankcount == ($#$awardref + 1)) { $result = 'NO_RESPONSE'; } + if ($blankcount == ($#$awardref + 1)) { + return ('NO_RESPONSE'); + } } - if (defined($result)) { return ($result); } + if ($Apache::lonxml::internal_error) { $result='INTERNAL_ERROR'; } + + if (!$final_scantron && defined($result)) { return ($result); } + + # if in scantron mode, if the award for any response is + # assigned score, then the part gets an assigned score + if ($final_scantron + && grep {$_ eq 'ASSIGNED_SCORE'} (@$awardref)) { + return ('ASSIGNED_SCORE'); + } + + # if in scantron mode, if the award for any response is + # correct and there are non-correct responses, + # then the part gets an assigned score + if ($final_scantron + && (grep { $_ eq 'EXACT_ANS' || + $_ eq 'APPROX_ANS' } (@$awardref)) + && (grep { $_ ne 'EXACT_ANS' && + $_ ne 'APPROX_ANS' } (@$awardref))) { + return ('ASSIGNED_SCORE'); + } # these awards are ordered from most important error through best correct my $awards = (!$reverse) ? \%fwd_awards : \%rev_awards ; @@ -570,6 +628,7 @@ sub finalizeawards { } $j++; } + if (defined($which)) { if (ref($nameref)) { return ($$awardref[$which],$$msgref[$which],$$nameref[$which]); @@ -597,8 +656,8 @@ sub decideoutput { ); my $part = $Apache::inputtags::part; - my $handgrade = - ('yes' eq lc(&Apache::lonnet::EXT("resource.$part.handgrade"))); + my $tohandgrade = &Apache::lonnet::EXT("resource.$part.handgrade"); + my $handgrade = ('yes' eq lc($tohandgrade)); my $computer = ($handgrade)? '' : " ".&mt("Computer's answer now shown above."); @@ -628,14 +687,18 @@ sub decideoutput { $message.= $computer; } $added_computer_text=1; - my ($symb) = &Apache::lonnet::whichuser(); - if (($symb ne '') - && - ($env{'course.'.$env{'request.course.id'}. - '.disable_receipt_display'} ne 'yes')) { - $message.=(($target eq 'web')?'
':' '). - &mt('Your receipt is').' '.&Apache::lonnet::receipt($Apache::inputtags::part). - (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):''); + if ($awarded > 0) { + my ($symb) = &Apache::lonnet::whichuser(); + if (($symb ne '') + && + ($env{'course.'.$env{'request.course.id'}. + '.disable_receipt_display'} ne 'yes') && + ($Apache::lonhomework::type ne 'practice')) { + $message.=(($target eq 'web')?'
':' '). + &mt('Your receipt is [_1]', + (&Apache::lonnet::receipt($Apache::inputtags::part). + (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):''))); + } } } $button=0; @@ -662,12 +725,14 @@ sub decideoutput { $message.= $computer; } $added_computer_text=1; - unless ($env{'course.'. + if ($awarded > 0 + && $env{'course.'. $env{'request.course.id'}. - '.disable_receipt_display'} eq 'yes') { + '.disable_receipt_display'} ne 'yes') { $message.=(($target eq 'web')?'
':' '). - 'Your receipt is '.&Apache::lonnet::receipt($Apache::inputtags::part). - (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):''); + &mt('Your receipt is [_1]', + (&Apache::lonnet::receipt($Apache::inputtags::part). + (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):''))); } $css_class=$possible_class{'correct'}; $button=0; @@ -683,10 +748,13 @@ sub decideoutput { $button = 1; } elsif ($award eq 'MISSING_ANSWER') { $message = &mt('Some items were not submitted.'); + if ($target ne 'tex') { + $message .= &Apache::loncommon::help_open_topic('Some_Items_Were_Not_Submitted'); + } $css_class=$possible_class{'not_charged_try'}; $button = 1; } elsif ($award eq 'ERROR') { - $message = &mt('An error occured while grading your answer.'); + $message = &mt('An error occurred while grading your answer.'); $css_class=$possible_class{'not_charged_try'}; $button = 1; } elsif ($award eq 'TOO_LONG') { @@ -698,12 +766,16 @@ sub decideoutput { $css_class=$possible_class{'not_charged_try'}; $button=1; } elsif ($award eq 'MISORDERED_RANK') { - $message = &mt('You have provided an invalid ranking'); - if ($target ne 'tex') { - $message.=', '.&mt('please refer to').' '.&Apache::loncommon::help_open_topic('Ranking_Problems','help on ranking problems'); - } + $message = &mt('You have provided an invalid ranking.'); + if ($target ne 'tex') { + $message.=' '.&mt('Please refer to [_1]',&Apache::loncommon::help_open_topic('Ranking_Problems',&mt('help on ranking problems'))); + } $css_class=$possible_class{'not_charged_try'}; $button=1; + } elsif ($award eq 'EXCESS_FILESIZE') { + $message = &mt('Submission won\'t be graded. The combined size of submitted files exceeded the amount allowed.'); + $css_class=$possible_class{'not_charged_try'}; + $button=1; } elsif ($award eq 'INVALID_FILETYPE') { $message = &mt('Submission won\'t be graded. The type of file submitted is not allowed.'); $css_class=$possible_class{'not_charged_try'}; @@ -743,9 +815,14 @@ sub decideoutput { $css_class=$possible_class{'not_charged_try'}; $button=1; } elsif ($award eq 'BAD_FORMULA') { - $message = &mt("Unable to understand formula"); + $message = &mt("Unable to understand formula").'.'; + if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Formula_Answers')}; $css_class=$possible_class{'not_charged_try'}; $button=1; + } elsif ($award eq 'INTERNAL_ERROR') { + $message = &mt("An internal error occurred while processing your answer. Please try again later."); + $css_class=$possible_class{'not_charged_try'}; + $button=1; } elsif ($award eq 'INCORRECT') { $message = &mt("Incorrect").'.'; $css_class=$possible_class{'charged_try'}; @@ -777,19 +854,33 @@ sub decideoutput { my (undef,undef,$domain,$user)=&Apache::lonnet::whichuser(); foreach my $resid(@Apache::inputtags::response){ if ($Apache::lonhomework::history{"resource.$part.$resid.handback"}) { - $message.='
'; + if ($target eq 'tex') { + $message.= "\\\\\n"; + } else { + $message.='
'; + } my @files = split(/\s*,\s*/, $Apache::lonhomework::history{"resource.$part.$resid.handback"}); my $file_msg; foreach my $file (@files) { - $file_msg.= '
'.$file.''; + if ($target eq 'tex') { + $file_msg.= "\\\\\n".$file; + } else { + $file_msg.= '
'.$file.''; + } } $message .= &mt('Returned file(s): [_1]',$file_msg); + if ($target eq 'tex') { + $message.= "\\\\\n"; + } else { + $message.='
'; + } } } - if (lc($Apache::lonhomework::problemstatus) eq 'no' && - $Apache::inputtags::status[-1] ne 'SHOW_ANSWER') { + if (&Apache::lonhomework::hide_problem_status() + && $Apache::inputtags::status[-1] ne 'SHOW_ANSWER' + && &hide_award($award)) { $message = &mt("Answer Submitted: Your final submission will be graded after the due date."); $css_class=$possible_class{'no_grade'}; $button=1; @@ -799,6 +890,15 @@ sub decideoutput { $message.= $computer; $added_computer_text=1; } + if ($Apache::lonhomework::type eq 'practice') { + if ($target eq 'web') { + $message .= '
'; + } else { + $message .= ' '; + } + $message.=&mt('Submissions to practice problems are not permanently recorded.'); + } + return ($button,$css_class,$message,$previousmsg); } @@ -852,8 +952,9 @@ sub setgradedata { $Apache::lonhomework::results{"resource.$id.afterduedate"}=$award; return ''; } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~ - /^correct/ || $Apache::lonhomework::scantronmode || - lc($Apache::lonhomework::problemstatus) eq 'no') { + /^correct/ + || $Apache::lonhomework::scantronmode + || &Apache::lonhomework::hide_problem_status() ) { # the student doesn't already have it correct, # or we are in a mode (scantron orno problem status) where a correct # can become incorrect @@ -875,8 +976,13 @@ sub setgradedata { my $numawards=scalar(@Apache::inputtags::response); $Apache::lonhomework::results{"resource.$id.awarded"} = 0; foreach my $res (@Apache::inputtags::response) { - $Apache::lonhomework::results{"resource.$id.awarded"}+= - $Apache::lonhomework::results{"resource.$id.$res.awarded"}; + if (defined($Apache::lonhomework::results{"resource.$id.$res.awarded"})) { + $Apache::lonhomework::results{"resource.$id.awarded"}+= + $Apache::lonhomework::results{"resource.$id.$res.awarded"}; + } else { + $Apache::lonhomework::results{"resource.$id.awarded"}+= + &awarddetail_to_awarded($Apache::lonhomework::results{"resource.$id.$res.awarddetail"}); + } } if ($numawards > 0) { $Apache::lonhomework::results{"resource.$id.awarded"}/= @@ -891,8 +997,8 @@ sub setgradedata { } elsif ( $award eq 'INCORRECT' ) { $Apache::lonhomework::results{"resource.$id.tries"} = $Apache::lonhomework::history{"resource.$id.tries"} + 1; - if (lc($Apache::lonhomework::problemstatus) eq 'no' || - $Apache::lonhomework::scantronmode) { + if (&Apache::lonhomework::hide_problem_status() + || $Apache::lonhomework::scantronmode) { $Apache::lonhomework::results{"resource.$id.awarded"} = 0; } $Apache::lonhomework::results{"resource.$id.solved"} = @@ -911,12 +1017,18 @@ sub setgradedata { } else { $Apache::lonhomework::results{"resource.$id.solved"} = 'incorrect_attempted'; - if (lc($Apache::lonhomework::problemstatus) eq 'no' || - $Apache::lonhomework::scantronmode) { + if (&Apache::lonhomework::show_no_problem_status() + || $Apache::lonhomework::scantronmode) { $Apache::lonhomework::results{"resource.$id.tries"} = $Apache::lonhomework::history{"resource.$id.tries"} + 1; $Apache::lonhomework::results{"resource.$id.awarded"} = 0; } + + if (&Apache::lonhomework::show_some_problem_status()) { + # clear out the awarded if they had gotten it wrong/right + # and are now in an error mode + $Apache::lonhomework::results{"resource.$id.awarded"} = ''; + } } if (defined($msg)) { $Apache::lonhomework::results{"resource.$id.awardmsg"} = $msg; @@ -930,7 +1042,7 @@ sub setgradedata { # check if this was a previous submission if it was delete the # unneeded data and update the previously_used attribute if ( $previously_used eq 'PREVIOUSLY_USED') { - if (lc($Apache::lonhomework::problemstatus) ne 'no') { + if (&Apache::lonhomework::show_problem_status()) { delete($Apache::lonhomework::results{"resource.$id.tries"}); $Apache::lonhomework::results{"resource.$id.previous"} = '1'; } @@ -1008,13 +1120,15 @@ sub grade { &Apache::lonxml::debug("got message $value from $response for $id"); push (@msgs,$value); } - my ($finalaward,$msg) = &finalizeawards(\@awards,\@msgs); + my ($finalaward,$msg) = + &finalizeawards(\@awards,\@msgs,undef,undef, + $Apache::lonhomework::scantronmode); my $previously_used; if ( $#Apache::inputtags::previous eq $#awards ) { my ($match) = &previous_match(\@Apache::inputtags::previous_version, scalar(@Apache::inputtags::response)); - + if ($match) { $previously_used = 'PREVIOUSLY_LAST'; foreach my $value (@Apache::inputtags::previous) { @@ -1082,7 +1196,7 @@ sub get_grade_messages { $trystr = '\vskip 0 mm '; } } else { - $trystr = "".$tries_text." $tries"; + $trystr = ''.$tries_text." $tries"; if ($Apache::lonhomework::parsing_a_task) { } elsif($env{'request.state'} ne 'construct') { $trystr.="/$maxtries"; @@ -1091,7 +1205,7 @@ sub get_grade_messages { $trystr.="/".$Apache::inputtags::params{'maxtries'}; } } - $trystr.=""; + $trystr.=""; } } @@ -1133,7 +1247,12 @@ sub gradestatus { } if ( $showbutton ) { if ($target ne 'tex') { - $button = ''; + $button = + ''; } } @@ -1182,16 +1301,16 @@ sub previous_tries { my $previous = $count_lookup{$which}; $message =~ s{()}{ as submission \# $previous $1}; } elsif ($Apache::lonhomework::history{"$prefix.tries"}) { - if (!(lc($Apache::lonhomework::problemstatus) eq 'no' + if (!(&Apache::lonhomework::hide_problem_status() && $Apache::inputtags::status[-1] ne 'SHOW_ANSWER') && $Apache::lonhomework::history{"$prefix.solved"} =~/^correct/ ) { + my $txt_correct = &mt('Correct'); $message =~ s{()(.*?)()} - {$1 Correct. $3}s; + {$1 $txt_correct. $3}s; } - my $trystr = "(Try ". - $Apache::lonhomework::history{"$prefix.tries"}.')'; + my $trystr = "(".&mt('Try [_1]',$Apache::lonhomework::history{"$prefix.tries"}).")"; $message =~ s{()}{ $trystr $1}; } my ($class) = ($message =~ m{'; $output =''.$headers.$output.'
'; #return $output; - $output=~s/\\/\\\\/g; - $output=~s/\'/\\\'/g; - $output=~s/\s+/ /g; + $output = &Apache::loncommon::js_ready($output); $output.='
'; my $windowopen=&Apache::lonhtmlcommon::javascript_docopen(); my $start_page = &Apache::loncommon::start_page('Previous Tries', undef, - {'only_body' => 1, - 'bgcolor' => '#FFFFFF', - 'js_ready' => 1,}); + {'only_body' => 1, + 'bgcolor' => '#FFFFFF', + 'js_ready' => 1, + 'inherit_jsmath' => 1, }); my $end_page = &Apache::loncommon::end_page({'js_ready' => 1,}); - + my $prefix = $env{'form.request.prefix'}; + $prefix =~ tr{.}{_}; + my $function_name = "LONCAPA_previous_tries_".$prefix. + $Apache::lonxml::curdepth.'_'.$env{'form.counter'}; my $result ="".&mt("Previous Tries")."
"; +".&mt("Previous Tries")."
"; #use Data::Dumper; #&Apache::lonnet::logthis(&Dumper(\%Apache::inputtags::submission_display)); return $result;