--- loncom/homework/inputtags.pm 2008/11/25 13:16:17 1.249 +++ loncom/homework/inputtags.pm 2010/06/21 02:15:37 1.265 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # input definitons # -# $Id: inputtags.pm,v 1.249 2008/11/25 13:16:17 jms Exp $ +# $Id: inputtags.pm,v 1.265 2010/06/21 02:15:37 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -53,6 +53,7 @@ package Apache::inputtags; use HTML::Entities(); use strict; use Apache::loncommon; +use Apache::lonhtmlcommon; use Apache::lonlocal; use Apache::lonnet; use LONCAPA; @@ -388,12 +389,13 @@ 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';} - if ($env{'form.pdfFormFields'} eq 'yes') { + if ($env{'form.pdfFormFields'} eq 'yes' + && $Apache::inputtags::status[-1] eq 'CAN_ANSWER') { my $fieldname = $env{'request.symb'}. '&part_'. $Apache::inputtags::part. '&textresponse'. '&HWVAL_' . $Apache::inputtags::response['-1']; - $result="\n\\\\\n".'\textField{'.$fieldname.'}{'.$size.'}{12 bp}'; + $result='\textField{'.$fieldname.'}{'.$size.'}{12 bp}'; } else { $result='\framebox['.$size.'][s]{\tiny\strut}'; } @@ -461,95 +463,179 @@ 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 =cut sub file_selector { - my ($part,$id,$uploadedfiletypes,$which,$extratext)=@_; + my ($part,$id,$uploadedfiletypes,$which,$extratext,$maxfilesize)=@_; if (!$uploadedfiletypes) { return ''; } my $jspart=$part; $jspart=~s/\./_/g; my $result; - - $result.=''; + my $current_files_display = ¤t_file_submissions($part,$id); + my $addfiles; + if ($current_files_display) { + $result .= &Apache::lonhtmlcommon::row_title(&mt('Currently submitted files:')). + $current_files_display. + &Apache::lonhtmlcommon::row_closure(); + $addfiles = &mt('Submit other file(s)'); + } else { + $addfiles = &mt('Choose file(s) to submit'); + } + $result .= &Apache::lonhtmlcommon::row_title($addfiles); + my $constraints; if ($uploadedfiletypes ne '*') { - $result.= - &mt('Allowed filetypes: [_1]',$uploadedfiletypes).'
'; + $constraints = + &mt('Allowed filetypes: [_1]',''.$uploadedfiletypes.'').'
'; + } + if ($maxfilesize) { + $constraints .= &mt('Combined size of all files not to exceed: [_1] MB[_2].', + ''.$maxfilesize.'').'
'; + } + if ($constraints) { + $result .= $constraints.'
'; } if ($which eq 'uploadonly' || $which eq 'both') { - $result.=&mt('Submit a file: (only one file can be uploaded)'). + $result.=&mt('Submit a file: (only one file per submission)'). '

'; - $result .= &show_past_file_submission($part,$id); } - if ( $which eq 'both') { + if ( $which eq 'both') { $result.='
'.''.&mt('OR:').'
'; } if ($which eq 'portfolioonly' || $which eq 'both') { $result.=$extratext.''. - &mt('Select Portfolio Files').'
'. + &mt('Select Portfolio Files: (one or more files per submission)').'
'. ''. '
'; - $result .= &show_past_portfile_submission($part,$id); } - $result.=''; + $result.=&Apache::lonhtmlcommon::row_closure(1); return $result; } -sub show_past_file_submission { +sub current_file_submissions { my ($part,$id) = @_; - my $uploadedfile= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.uploadedfile"},'<>&"'); - - return if (!$uploadedfile); - - my $url=$Apache::lonhomework::history{"resource.$part.$id.uploadedurl"}; - &Apache::lonxml::extlink($url); - &Apache::lonnet::allowuploaded('/adm/essayresponse',$url); - my $icon=&Apache::loncommon::icon($url); - my $curfile=''.$uploadedfile.''; - return &mt('Currently submitted: [_1]',$curfile); - -} - -sub show_past_portfile_submission { - my ($part,$id) = @_; - if ($Apache::lonhomework::history{"resource.$part.$id.portfiles"}!~/[^\s]/){ - return; + my $jspart=$part; + $jspart=~s/\./_/g; + my $uploadedfile=&HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.uploadedfile"},'<>&"'); + my $portfiles=$Apache::lonhomework::history{"resource.$part.$id.portfiles"}; + return if (($uploadedfile eq '') && ($portfiles !~/[^\s]/)); + my $header = &Apache::loncommon::start_data_table(). + &Apache::loncommon::start_data_table_header_row(). + ''.&mt('Delete?').''. + ''.&mt('Name').''. + ''.&mt('Size (MB)').''. + ''.&mt('Last Modified').''. + &Apache::loncommon::end_data_table_header_row(); + my (undef,$crsid,$udom,$uname)=&Apache::lonnet::whichuser(); + my ($cdom,$cnum) = ($crsid =~ /^($LONCAPA::match_domain)_($LONCAPA::match_courseid)$/); + my ($result,$header_shown,%okfiles,%rows,@bad_file_list); + if ($uploadedfile) { + my $url=$Apache::lonhomework::history{"resource.$part.$id.uploadedurl"}; + my ($path,$name) = ($url =~ m{^/uploaded/\Q$cdom\E/\Q$cnum\E/(essayresponse/.+/)([^/]+))}); + my ($status,$hashref,$error) = + ¤t_file_info($url,$uploadedfile,$name,$path); + if ($status eq 'ok') { + push(@{$okfiles{$name}},$url); + $rows{$url} = $hashref; + &Apache::lonxml::extlink($url); + &Apache::lonnet::allowuploaded('/adm/essayresponse',$url); + } else { + push(@bad_file_list,$error); + } + } + if ($portfiles =~ /[^\s]/) { + my $prefix = "/uploaded/$udom/$uname/portfolio"; + foreach my $file (split(/\s*,\s*/,&unescape($portfiles))) { + my ($path,$name) = ($file =~ m{^(.*/)([^/]+)$}); + my $url = $prefix.$path.$name; + my $uploadedfile = &HTML::Entities::encode($url,'<>&"'); + my ($status,$hashref,$error) = + ¤t_file_info($url,$uploadedfile,$name,$path); + if ($status eq 'ok') { + push(@{$okfiles{$name}},$url); + $rows{$url} = $hashref; + } else { + push(@bad_file_list,$error); + } + } + } + foreach my $name (sort(keys(%okfiles))) { + if (ref($okfiles{$name}) eq 'ARRAY') { + foreach my $url (@{$okfiles{$name}}) { + if (ref($rows{$url}) eq 'HASH') { + my $link = $rows{$url}{link}; + my $portfile = $rows{$url}{path}.$rows{$url}{name}; + $portfile = &HTML::Entities::encode($portfile,'<>&"'); + if ($link) { + my $icon=&Apache::loncommon::icon($url); + unless ($header_shown) { + $result .= $header; + $header_shown = 1; + } + $result.= + &Apache::loncommon::start_data_table_row()."\n". + ''."\n". + ''.$name.''."\n". + ''.$rows{$url}{size}.''."\n". + ''.$rows{$url}{lastmodified}.''."\n". + &Apache::loncommon::end_data_table_row(); + } + } + } + } + } + if ($header_shown) { + $result .= &Apache::loncommon::end_data_table(); } - my (@file_list,@bad_file_list); - foreach my $file (split(/\s*,\s*/,&unescape($Apache::lonhomework::history{"resource.$part.$id.portfiles"}))) { - my (undef,undef,$domain,$user)=&Apache::lonnet::whichuser(); - my $url="/uploaded/$domain/$user/portfolio$file"; - my $icon=&Apache::loncommon::icon($url); - push(@file_list,''.$file.''); - if (! &Apache::lonnet::stat_file($url)) { - &Apache::lonnet::logthis("bad file is $url"); - push(@bad_file_list,''.$file.''); - } - } - my $files = ''. - join(', ',@file_list). - ''; - my $result = &mt("Portfolio files previously selected: [_1]",$files); if (@bad_file_list) { - my $bad_files = ''. - join(', ',@bad_file_list). - ''; - $result.='
'.&mt('These file(s) don\'t exist: [_1]',$bad_files); + my $bad_files = ''. + join(', ',@bad_file_list). + ''; + $result.='

'. + &mt("These file(s) don't exist: [_1]",$bad_files). + '

'; } return $result; +} +sub current_file_info { + my ($url,$uploadedfile,$name,$path) = @_; + my ($status,$error,%info); + my @stat = &Apache::lonnet::stat_file($url); + if ((@stat) && ($stat[0] ne 'no_such_dir')) { + my ($lastmod,$size); + if ($stat[9] =~ /^\d+$/) { + $lastmod = &Apache::lonlocal::locallocaltime($stat[9]); + } + $size = $stat[7]/(1024*1024); + $size = sprintf("%.3f",$size); + %info = ( + link => $uploadedfile, + name => $name, + path => $path, + size => $size, + lastmodified => $lastmod, + ); + $status = 'ok'; + } else { + &Apache::lonnet::logthis("bad file is $url"); + my $icon=&Apache::loncommon::icon($url); + $error = ''.$uploadedfile.''; + } + return ($status,\%info,$error); } sub valid_award { @@ -560,11 +646,13 @@ 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', - 'EXCESS_FILESIZE', 'DRAFT', - 'SUBMITTED', 'ASSIGNED_SCORE', - 'APPROX_ANS', 'EXACT_ANS','COMMA_FAIL') { + 'EXCESS_FILESIZE', 'FILENAME_INUSE', + 'DRAFT', 'SUBMITTED', 'SUBMITTED_CREDIT', + 'ANONYMOUS', 'ANONYMOUS_CREDIT', + 'ASSIGNED_SCORE', 'APPROX_ANS', + 'EXACT_ANS','COMMA_FAIL') { if ($award eq $possibleaward) { return 1; } } return 0; @@ -575,9 +663,11 @@ 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', 'EXCESS_FILESIZE', 'DRAFT', 'SUBMITTED', + 'INVALID_FILETYPE', 'EXCESS_FILESIZE', 'FILENAME_INUSE', + 'DRAFT', 'SUBMITTED', + 'SUBMITTED_CREDIT', 'ANONYMOUS', 'ANONYMOUS_CREDIT', 'ASSIGNED_SCORE', 'APPROX_ANS', 'EXACT_ANS'); my $i=0; my %fwd_awards = map { ($_,$i++) } @awards; @@ -601,7 +691,7 @@ sub hide_award { return 1; } if ($award =~ - /^(?:EXACT_ANS|APPROX_ANS|SUBMITTED|ASSIGNED_SCORE|INCORRECT)/) { + /^(?:EXACT_ANS|APPROX_ANS|SUBMITTED|SUBMITTED_CREDIT|ANONYMOUS|ANONYMOUS_CREDIT|ASSIGNED_SCORE|INCORRECT)/) { return 1; } return 0; @@ -623,6 +713,9 @@ sub finalizeawards { return ('NO_RESPONSE'); } } + + 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 @@ -669,6 +762,7 @@ sub finalizeawards { sub decideoutput { my ($award,$awarded,$awardmsg,$solved,$previous,$target)=@_; + my $message=''; my $button=0; my $previousmsg; @@ -722,7 +816,7 @@ sub decideoutput { '.disable_receipt_display'} ne 'yes') && ($Apache::lonhomework::type ne 'practice')) { $message.=(($target eq 'web')?'
':' '). - &mt('Your receipt is [_1]', + &mt('Your receipt no. is [_1]', (&Apache::lonnet::receipt($Apache::inputtags::part). (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):''))); } @@ -803,15 +897,24 @@ sub decideoutput { $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 'FILENAME_INUSE') { + $message = &mt('You have already uploaded a file with that filename.'); + if ($target eq 'tex') { + $message.= "\\\\\n"; + } else { + $message .= '
'; + } + $message .= &mt('Please use a different file name.'); + $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.'); + $message = &mt("Submission won't be graded. The type of file submitted is not allowed."); $css_class=$possible_class{'not_charged_try'}; $button=1; } elsif ($award eq 'SIG_FAIL') { my ($used,$min,$max)=split(':',$awardmsg); my $word = ($used < $min) ? 'more' : 'fewer'; - $message = &mt("Submission not graded. Use $word digits.",$used); + $message = &mt("Submission not graded. Use $word digits.",$used); $css_class=$possible_class{'not_charged_try'}; $button=1; } elsif ($award eq 'UNIT_INVALID_INSTRUCTOR') { @@ -847,6 +950,10 @@ sub decideoutput { 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'}; @@ -855,8 +962,19 @@ sub decideoutput { $message = &mt("Your submission has been recorded."); $css_class=$possible_class{'no_grade'}; $button=1; + } elsif ($award eq 'SUBMITTED_CREDIT') { + $message = &mt("Your submission has been recorded, and credit awarded."); + $css_class=$possible_class{'correct'}; + $button=1; + } elsif ($award eq 'ANONYMOUS') { + $message = &mt("Your anonymous submission has been recorded."); + $css_class=$possible_class{'no_grade'}; + $button=1; + } elsif ($award eq 'ANONYMOUS_CREDIT') { + $message = &mt("Your anonymous submission has been recorded, and credit awarded."); + $css_class=$possible_class{'correct'}; } elsif ($award eq 'DRAFT') { - $message = &mt("A draft copy has been saved."); + $message = &mt("Copy saved but not submitted."); $css_class=$possible_class{'not_charged_try'}; $button=1; } elsif ($award eq 'ASSIGNED_SCORE') { @@ -878,14 +996,27 @@ 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.='
'; + } } } @@ -1005,6 +1136,18 @@ sub setgradedata { $Apache::lonhomework::results{"resource.$id.solved"} = $solvemsg; $Apache::lonhomework::results{"resource.$id.awarded"} = '1'; + } elsif ( $award eq 'SUBMITTED_CREDIT' ) { + $Apache::lonhomework::results{"resource.$id.tries"} = + $Apache::lonhomework::history{"resource.$id.tries"} + 1; + $Apache::lonhomework::results{"resource.$id.solved"} = + 'credit_attempted'; + $Apache::lonhomework::results{"resource.$id.awarded"} = '1'; + } elsif ( $award eq 'ANONYMOUS_CREDIT' ) { + $Apache::lonhomework::results{"resource.$id.tries"} = + $Apache::lonhomework::history{"resource.$id.tries"} + 1; + $Apache::lonhomework::results{"resource.$id.solved"} = + 'credit_attempted'; + $Apache::lonhomework::results{"resource.$id.awarded"} = '1'; } elsif ( $award eq 'INCORRECT' ) { $Apache::lonhomework::results{"resource.$id.tries"} = $Apache::lonhomework::history{"resource.$id.tries"} + 1; @@ -1019,6 +1162,11 @@ sub setgradedata { $Apache::lonhomework::history{"resource.$id.tries"} + 1; $Apache::lonhomework::results{"resource.$id.solved"} = 'ungraded_attempted'; + } elsif ( $award eq 'ANONYMOUS' ) { + $Apache::lonhomework::results{"resource.$id.tries"} = + $Apache::lonhomework::history{"resource.$id.tries"} + 1; + $Apache::lonhomework::results{"resource.$id.solved"} = + 'ungraded_attempted'; } elsif ( $award eq 'DRAFT' ) { $Apache::lonhomework::results{"resource.$id.solved"} = ''; } elsif ( $award eq 'NO_RESPONSE' ) { @@ -1078,6 +1226,10 @@ sub setgradedata { if ($award eq 'SUBMITTED') { &Apache::response::add_to_gradingqueue(); } + if (($Apache::lonhomework::type eq 'anonsurvey') || + ($Apache::lonhomework::type eq 'anonsurveycred')) { + $Apache::lonhomework::results{"resource.$id.type"} = $Apache::lonhomework::type; + } } sub find_which_previous { @@ -1190,12 +1342,7 @@ sub get_grade_messages { if ( $tries eq '' ) { $tries = '0'; } if ( $maxtries eq '' ) { $maxtries = '2'; } if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; } - my $tries_text=&mt('Tries'); - if ( $Apache::lonhomework::type eq 'survey' || - $Apache::lonhomework::parsing_a_task) { - $tries_text=&mt('Submissions'); - } - + my $tries_text= &get_tries_text();; if ($showbutton) { if ($target eq 'tex') { if ($env{'request.state'} ne "construct" @@ -1207,7 +1354,7 @@ sub get_grade_messages { $trystr = '\vskip 0 mm '; } } else { - $trystr = "".$tries_text." $tries"; + $trystr = ''.&mt($tries_text)." $tries"; if ($Apache::lonhomework::parsing_a_task) { } elsif($env{'request.state'} ne 'construct') { $trystr.="/$maxtries"; @@ -1216,7 +1363,7 @@ sub get_grade_messages { $trystr.="/".$Apache::inputtags::params{'maxtries'}; } } - $trystr.=""; + $trystr.=""; } } @@ -1259,11 +1406,9 @@ sub gradestatus { if ( $showbutton ) { if ($target ne 'tex') { $button = - ''; + ''; } } @@ -1296,7 +1441,13 @@ sub previous_tries { foreach my $i (1..$Apache::lonhomework::history{'version'}) { my $prefix = $i.":resource.$id"; - + my $is_anon; + if (defined($env{'form.grade_symb'})) { + if (($Apache::lonhomework::history{"$prefix.type"} eq 'anonsurvey') || + ($Apache::lonhomework::history{"$prefix.type"} eq 'anonsurveycred')) { + $is_anon = 1; + } + } next if (!exists($Apache::lonhomework::history{"$prefix.award"})); $count++; $count_lookup{$i} = $count; @@ -1341,7 +1492,11 @@ sub previous_tries { $submission = $Apache::lonhomework::history{"$prefix.submission"}; } - $output.=''.$submission.''; + if ($is_anon) { + $output.=''.&mt('(only shown to submitter)').''; + } else { + $output.=''.$submission.''; + } } else { $output.=''; } @@ -1359,8 +1514,9 @@ sub previous_tries { $output.='
'; my $windowopen=&Apache::lonhtmlcommon::javascript_docopen(); + my $tries_text = &get_tries_text('link'); my $start_page = - &Apache::loncommon::start_page('Previous Tries', undef, + &Apache::loncommon::start_page($tries_text, undef, {'only_body' => 1, 'bgcolor' => '#FFFFFF', 'js_ready' => 1, @@ -1375,12 +1531,34 @@ sub previous_tries { // -".&mt("Previous Tries")."
"; +".&mt($tries_text)."
"; #use Data::Dumper; #&Apache::lonnet::logthis(&Dumper(\%Apache::inputtags::submission_display)); return $result; } +sub get_tries_text { + my ($context) = @_; + my $tries_text; + if ($context eq 'link') { + $tries_text = 'Previous Tries'; + } else { + $tries_text = 'Tries'; + } + if ( $Apache::lonhomework::type eq 'survey' || + $Apache::lonhomework::type eq 'surveycred' || + $Apache::lonhomework::type eq 'anonsurvey' || + $Apache::lonhomework::type eq 'anonsurveycred' || + $Apache::lonhomework::parsing_a_task) { + if ($context eq 'link') { + $tries_text = 'Previous Submissions'; + } else { + $tries_text = 'Submissions'; + } + } + return $tries_text; +} + 1; __END__