File:  [LON-CAPA] / loncom / homework / essayresponse.pm
Revision 1.91: download - view: text, annotated - select for diffs
Sun Nov 16 02:46:25 2008 UTC (15 years, 6 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Bug 5719 Size limits for File Upload in Essay response.
 - Max. cumulative size for submitted files applies to total for all files submitted to a particular essayresponse item. Default is 100 MB.
 - In course context, &lonacc:get_posted_cgi() will now no longer add file contents to %env if size exceeds max. for item.
 - Additional checks in essayresponse::file_submission() will assign award as "EXCESS_FILESIZE" if total of all files being submitted for sinle essayresponse item exceed max set via
PARM.
 - Modification to $Apache::lonhomework::results{"resource.$part.$id.$which"} to now include a comma separated list of only those submitted files which passed both the filetype and size tests.

    1: # The LearningOnline Network with CAPA
    2: # essay (ungraded) style responses
    3: #
    4: # $Id: essayresponse.pm,v 1.91 2008/11/16 02:46:25 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: =pod
   30: 
   31: =head1 NAME
   32: 
   33: Apache::easyresponse
   34: 
   35: =head1 SYNOPSIS
   36: 
   37: Handler to evaluate essay (ungraded) style responses.
   38: 
   39: This is part of the LearningOnline Network with CAPA project
   40: described at http://www.lon-capa.org.
   41: 
   42: =cut
   43: 
   44: package Apache::essayresponse;
   45: use strict;
   46: use Apache::lonxml();
   47: use Apache::lonnet;
   48: use Apache::lonlocal;
   49: use LONCAPA qw(:DEFAULT :match);
   50:  
   51: 
   52: BEGIN {
   53:     &Apache::lonxml::register('Apache::essayresponse',('essayresponse'));
   54: }
   55: 
   56: sub start_essayresponse {
   57:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   58:     my $result;
   59:     my $id = &Apache::response::start_response($parstack,$safeeval);
   60:     if ($target eq 'meta') {
   61: 	$result=&Apache::response::meta_package_write('essayresponse');
   62:     } elsif ($target eq 'web' &&
   63: 	     $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
   64: 	my $part= $Apache::inputtags::part;
   65: 	my $ncol= &Apache::lonnet::EXT("resource.$part".'_'."$id.maxcollaborators");
   66: 	my $coll= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.collaborators"},'<>&"');
   67: 	my $uploadedfiletypes= &Apache::lonnet::EXT("resource.$part".'_'."$id.uploadedfiletypes");
   68:         $uploadedfiletypes=~s/[^\w\,]//g;
   69: 	if ( $Apache::lonhomework::type eq 'survey' ) {
   70: 	    $result.= '<input type="hidden" name="HWDRAFT'.$part.'_'.$id.'" value="yes" /> ';
   71: 	}
   72: 	$result.='<br /><table border="1">';
   73: 	if ( $Apache::lonhomework::type ne 'survey' ) {
   74: 	    $result.= '<tr><td>'.
   75: 		'<label>'.
   76: 		'<input type="radio" name="HWDRAFT'.$part.'_'.$id.'" value="yes" checked="checked" /> '.
   77: 		&mt('Submit entries below as answer to receive credit').
   78: 		'</label> <br />'.
   79: 		'<label>'.
   80: 		'<input type="radio" name="HWDRAFT'.$part.'_'.$id.'" value="no" /> '.
   81: 		&mt('Save entries below as a draft answer (not submitting them for credit yet)').
   82: 		'</label>'.
   83: 		'</td></tr>';
   84: 	}
   85: 
   86: 	if ($ncol > 0) {
   87: 	    $result .='<tr><td>'.'<label>'.
   88: 		      &mt('Collaborators:').' <input type="text" size="70" max="80" name="HWCOL'.
   89: 		      $part.'_'.$id.'" value="'.$coll.'" /><br />'.
   90: 		      &mt('(Enter a maximum of [quant,_1,collaborator] using username or username:domain, e.g. smithje or smithje:[_2].)',$ncol,$env{'user.domain'});
   91:             if ($ncol > 1) {
   92:                 $result .= '<br />'.&mt('If entering more than one, use spaces to separate the collaborators.');
   93:             }
   94:             $result .= '</label><br />';
   95: 	    $result .= &check_collaborators($ncol,$coll) if ($coll =~ /\w+/);
   96: 	    $result .='</td></tr>';
   97: 	}
   98: 	$result.=&Apache::inputtags::file_selector($part,$id,
   99: 						   $uploadedfiletypes,'both');
  100:         $result.='</table>';
  101:     } elsif ($target eq 'web' &&
  102: 	     $Apache::inputtags::status[-1] ne 'CAN_ANSWER') {
  103: 	my $part= $Apache::inputtags::part;
  104: 	my @msgs;
  105: 	if ($Apache::lonhomework::history{"resource.$part.$id.collaborators"} =~ /\S/) {
  106: 	    my $coll= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.collaborators"},'<>&"');
  107: 	    $result .= '<td>'.&mt('Collaborated with [_1]',$coll).'</td>';
  108: 	}
  109: 
  110: 	my $file_submission = 
  111: 	    &Apache::inputtags::show_past_file_submission($part,$id);
  112: 	if ($file_submission) {
  113: 	    $result .= '<td>'.$file_submission.'</td>';
  114: 	}
  115: 
  116: 	my $port_submission = 
  117: 	    &Apache::inputtags::show_past_portfile_submission($part,$id);
  118: 	if ($port_submission) {
  119: 	    $result .= '<td>'.$port_submission.'</td>';
  120: 	}
  121: 
  122: 	if ($result ne '') {
  123: 	    $result = 
  124: 		'<table class="LC_pastsubmission"><tr>'.$result.
  125: 		'</tr></table>';
  126: 	}
  127:     }
  128:     return $result;
  129: }
  130: 
  131: sub end_essayresponse {
  132:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  133:     my $part          = $Apache::inputtags::part;
  134:     my $id            = $Apache::inputtags::response[-1];
  135:     my $increment     = &Apache::response::repetition();
  136:     my $result;
  137:     if ( $target eq 'grade' ) {
  138: 	my $collaborators = $env{'form.HWCOL'.$part.'_'.$id};	
  139: 	if ($collaborators =~ /[^\s]/) {
  140: 	    my $previous_list= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.collaborators"},'<>&"');
  141: 	    $Apache::lonhomework::results{"resource.$part.$id.collaborators"}=$collaborators
  142: 		if ($collaborators ne $previous_list);
  143: 	}
  144: 	if (  &Apache::response::submitted('scantron') ) {
  145: 	    $increment=&Apache::response::scored_response($part,$id);
  146: 	} elsif ( &Apache::response::submitted() ) {
  147: 	    my $response      = $env{'form.HWVAL_'.$id};
  148:             my $filename = $env{'form.HWFILE'.$part.'_'.$id.'.filename'} || 
  149:                            $env{'form.HWFILETOOBIG'.$part.'_'.$id};
  150:             my $portfiles = $env{'form.HWPORT'.$part.'_'.$id};
  151: 	    if (( $response =~ /[^\s]/) || ($filename =~ /[^\s]/) || ($portfiles =~ /[^\s]/)) {
  152:  		my $award='DRAFT';
  153:         	if ($env{'form.HWDRAFT'.$part.'_'.$id} eq 'yes') {
  154: 		    $award='SUBMITTED';
  155: 		}
  156:                 my $uploadedflag=0;
  157:                 my $totalsize=0;
  158: 		&file_submission($part,$id,'filename',\$award,\$uploadedflag,\$totalsize);
  159: 		&file_submission($part,$id,'portfiles',\$award,\$uploadedflag,\$totalsize);
  160: 		$Apache::lonhomework::results{"resource.$part.$id.submission"}=$response;
  161: 		$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$award;
  162: 		my %previous=&Apache::response::check_for_previous($response,$part,$id);
  163: 		unless ($uploadedflag) { &Apache::response::handle_previous(\%previous,$award); }
  164: #
  165: # Store with resource author for similarity testing
  166: #
  167:                 if ($award eq 'SUBMITTED') {
  168: 		    my ($symb,$crsid,$domain,$name)=
  169: 			&Apache::lonnet::whichuser();
  170: 		    if ($crsid) {
  171: 			my $akey=join('.',&escape($name),&escape($domain),
  172: 				      &escape($crsid));
  173: 			my $essayurl=
  174: 			    &Apache::lonnet::declutter($ENV{'REQUEST_URI'});
  175: 			my ($adom,$aname,$apath)=
  176: 			    ($essayurl=~/^($LONCAPA::domain_re)\/($LONCAPA::username_re)\/(.*)$/);
  177:                         $apath=&escape($apath);
  178: 			$apath=~s/\W/\_/gs;
  179: 			&Apache::lonnet::put('nohist_essay_'.$apath,
  180: 					 { $akey => $response },$adom,$aname);
  181: 		    }
  182:                 }
  183: 	    }
  184: 	} 
  185:     } elsif ($target eq 'edit') {
  186: 	$result.=&Apache::edit::end_table();
  187: 
  188:     } elsif ($target eq 'tex'
  189: 	     && $Apache::lonhomework::type eq 'exam') {
  190: 	$result .= &Apache::inputtags::exam_score_line($target);
  191: 
  192:     } elsif ($target eq 'answer') {
  193: 	$result.=&Apache::response::answer_header($$tagstack[-1]);
  194: 	my $answer = &mt('Essay will be hand graded.');
  195: 	$result.=&Apache::response::answer_part($$tagstack[-1],$answer,
  196: 						{'no_verbatim' => 1});
  197: 	$result.=&Apache::response::answer_footer($$tagstack[-1]);
  198:     }
  199:     if ($target eq 'web') {
  200: 	&Apache::response::setup_prior_tries_hash(\&format_prior_response,
  201: 						  ['portfiles',
  202: 						   'uploadedurl']);
  203:     }
  204: 
  205:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
  206: 	$target eq 'tex' || $target eq 'analyze') {
  207: 	&Apache::lonxml::increment_counter($increment, "$part.$id");
  208: 
  209: 	if ($target eq 'analyze') {
  210:             $Apache::lonhomework::analyze{"$part.$id.type"} = 'essayresponse';
  211: 	    &Apache::lonhomework::set_bubble_lines();
  212: 	}
  213:     }
  214:     &Apache::response::end_response;
  215: 
  216:     return $result;
  217: }
  218: 
  219: sub format_prior_response {
  220:     my ($mode,$answer,$other_data) = @_;
  221:     my $output;
  222: 
  223:     my (undef,undef,$udom,$uname) = &Apache::lonnet::whichuser();
  224:     my $port_url = '/uploaded/'.$udom.'/'.$uname.'/portfolio/';
  225: 
  226:     my $file_list;
  227: 
  228:     foreach my $file (split(/\s*,\s*/,
  229: 			    $other_data->[0].','.$other_data->[1])) {
  230: 	next if ($file!~/\S/);
  231: 	if ($file !~ m{^/uploaded/}) { $file=$port_url.$file; }
  232: 	$file=~s|/+|/|g;
  233: 	&Apache::lonnet::allowuploaded('/adm/essayresponse',$file);
  234: 	$file_list.='<li><span class="LC_nobreak"><a href="'.$file.'?rawmode=1" target="lonGRDs"><img src="'.
  235: 	    &Apache::loncommon::icon($file).'" alt="file icon" border="0" /> '.$file.
  236: 	    '</a></span></li>'."\n";
  237:     }
  238:     if ($file_list) {
  239: 	$output.= &mt('Submitted Files').'<ul>'.$file_list.'</ul>';
  240:     }
  241:     if ($answer =~ /\S/) {
  242: 	$output.='<p>'.&mt('Submitted text').
  243: 	    '<blockquote>'.$answer.'</blockquote></p>';
  244:     }
  245: 
  246:     return '<div class="LC_prior_essay">'.$output.'</div>';
  247: }
  248: 
  249: sub file_submission {
  250:     my ($part,$id,$which,$award,$uploadedflag,$totalsize)=@_;
  251:     my $files;
  252:     my $jspart=$part;
  253:     $jspart=~s/\./_/g;
  254:     if ($which eq 'portfiles') { 
  255:         $files= $env{'form.HWPORT'.$jspart.'_'.$id};
  256:     } elsif ($which eq 'filename') {
  257:         if ($env{'form.HWFILETOOBIG'.$jspart.'_'.$id} ne '') {
  258:             $$award = 'EXCESS_FILESIZE';
  259:             return;
  260:         } else {
  261:             $files = $env{'form.HWFILE'.$jspart.'_'.$id.'.filename'};
  262:         }
  263:     }
  264:     if ($files =~ /[^\s]/) {
  265: 	$files =~s/,$//;
  266:         my (@submitted_files,@acceptable_files,@accepted_files);
  267:         if ($which eq 'portfiles') {
  268:             @submitted_files = split(/\s*,\s*/,$files);
  269:         } else {
  270:             @submitted_files = ($files);
  271:         }
  272:         my $uploadedfiletypes= 
  273:             &Apache::lonnet::EXT("resource.$part".'_'."$id.uploadedfiletypes");
  274:         if ($uploadedfiletypes) {
  275:             $uploadedfiletypes=~s/[^\w\,]//g;
  276:             $uploadedfiletypes=','.$uploadedfiletypes.',';
  277:             foreach my $file (@submitted_files) {
  278:                 my ($extension)=($file=~/\.(\w+)$/);
  279:                 if ($uploadedfiletypes=~/\,\s*\Q$extension\E\s*\,/i) {
  280:                     push(@acceptable_files,$file);
  281:                 } else {
  282:                     $$award='INVALID_FILETYPE';
  283:                     if ($which eq 'filename') {
  284:                         &delete_form_items($jspart,$id);
  285:                     }
  286:                 }
  287:             }
  288:         }
  289:         my $maxfilesize=&Apache::lonnet::EXT("resource.$part".'_'."$id.maxfilesize");
  290:         if (!$maxfilesize) {
  291:             $maxfilesize = 100.0; #FIXME This should become a domain configuration
  292:         }
  293:         my %dirlist;
  294:         foreach my $file (@acceptable_files) {
  295:             if ($which eq 'filename') {
  296:                 if (ref($totalsize)) {
  297:                     $$totalsize += $env{'form.HWFILESIZE'.$jspart.'_'.$id};
  298:                 }
  299:             } else {
  300:                 my ($symb,$crsid,$udom,$uname) = &Apache::lonnet::whichuser();
  301:                 my ($path,$filename) = ($file =~ m{^(.+)/([^/]+)$});
  302:                 my $fullpath = '/userfiles/portfolio'.$path;
  303:                 if (!exists($dirlist{$fullpath})) {
  304:                     my @list = &Apache::lonnet::dirlist($fullpath,$udom,$uname,1);
  305:                     foreach my $dir_line (@list) {
  306:                         my ($fname,$dom,undef,$testdir,undef,undef,undef,undef,
  307:                             $size,undef,$mtime,undef,undef,undef,$obs,undef) = 
  308:                         split(/\&/,$dir_line,16);
  309:                         if ($filename eq $fname) {
  310:                             my $mbsize = $size/(1024.0*1024.0);
  311:                             if (ref($totalsize)) {
  312:                                 $$totalsize += $mbsize;
  313:                             }
  314:                             last;
  315:                         }
  316:                     }
  317:                     $dirlist{$fullpath} = \@list;
  318:                 }
  319:             }
  320:             if (ref($totalsize)) {
  321:                 if ($$totalsize > $maxfilesize) {
  322:                     $$award='EXCESS_FILESIZE';
  323:                     if ($which eq 'filename') {
  324:                         &delete_form_items($jspart,$id);
  325:                     }
  326:                 }
  327:             } else {
  328:                 push(@accepted_files,$file);
  329:             }
  330:         }
  331: 	my $uploadedfiletypes= &Apache::lonnet::EXT("resource.$part".'_'."$id.uploadedfiletypes");
  332:         $Apache::lonhomework::results{"resource.$part.$id.$which"}=join(',',@accepted_files);
  333:         if (($$award eq 'INVALID_FILETYPE') || ($award eq 'EXCESS_FILESIZE')) {
  334:             return;
  335:         }
  336: 	if (ref($uploadedflag)) {
  337: 	    $$uploadedflag=1;
  338: 	}
  339: 	if ($which eq 'portfiles') {
  340: 	    my ($symb,$crsid,$domain,$name)=&Apache::lonnet::whichuser();
  341: 	    &Apache::lonnet::unmark_as_readonly($domain,$name,[$symb,$crsid]);
  342: 	    &Apache::lonnet::mark_as_readonly($domain,$name,\@submitted_files,[$symb,$crsid]);
  343: 	    &Apache::lonnet::clear_selected_files($name);
  344: 	}
  345: 	if ($which eq 'filename') {
  346: 	    $Apache::lonhomework::results{"resource.$part.$id.uploadedfile"}=
  347: 		$files;
  348: 	    $Apache::lonhomework::results{"resource.$part.$id.uploadedurl"}=
  349: 		&Apache::lonnet::userfileupload('HWFILE'.$jspart.'_'.$id,undef,
  350: 						'essayresponse');
  351:             &Apache::lonnet::delenv($env{'form.HWFILE'.$jspart.'_'.$id});
  352: 	}
  353:     } elsif ($which eq 'portfiles' &&
  354: 	     $Apache::lonhomework::history{"resource.$part.$id.$which"}) {
  355: 	my ($symb,$crsid,$domain,$name)=&Apache::lonnet::whichuser();
  356: 	&Apache::lonnet::unmark_as_readonly($domain,$name,[$symb,$crsid]);
  357: 	$Apache::lonhomework::results{"resource.$part.$id.$which"}="";
  358:     }
  359: }
  360: 
  361: sub delete_form_items {
  362:     my ($jspart,$id) = @_;
  363:     &Apache::lonnet::delenv($env{'form.HWFILE'.$jspart.'_'.$id.'.filename'});
  364:     &Apache::lonnet::delenv($env{'form.HWFILE'.$jspart.'_'.$id.'.mimetype'});
  365:     &Apache::lonnet::delenv($env{'form.HWFILE'.$jspart.'_'.$id});
  366: }
  367: 
  368: 
  369: sub check_collaborators {
  370:     my ($ncol,$coll) = @_;
  371:     my %classlist=&Apache::lonnet::dump('classlist',
  372: 					$env{'course.'.$env{'request.course.id'}.'.domain'},
  373: 					$env{'course.'.$env{'request.course.id'}.'.num'});
  374:     my (@badcollaborators,$result);
  375:   
  376:     my (@collaborators) = split(/,?\s+/,$coll);
  377:     foreach my $entry (@collaborators) {
  378:         my $collaborator;
  379: 	if ($entry =~ /:/) {
  380: 	    $collaborator = $entry;
  381: 	} else {
  382: 	    $collaborator = $entry.':'.$env{'user.domain'};
  383: 	}
  384:         if ($collaborator !~ /^$match_username:$match_domain$/) {
  385:             if (!grep(/^\Q$entry\E$/,@badcollaborators)) {
  386: 	        push(@badcollaborators,$entry);
  387:             }
  388:         } elsif (!grep(/^\Q$collaborator\E$/i,keys(%classlist))) {
  389:             if (!grep(/^\Q$entry\E$/,@badcollaborators)) {
  390:                 push(@badcollaborators,$entry);
  391:             }
  392:         }
  393:     }
  394:     
  395:     my $numbad = scalar(@badcollaborators);
  396:     if ($numbad) {
  397: 	$result = '<table border="0"><tr bgcolor="#ffbbbb"><td>';
  398:         if ($numbad == 1) {
  399:             $result .= &mt('The following user is invalid:');
  400:         } else {
  401:             $result .= &mt('The following [_1] users are invalid:',$numbad);
  402:         }
  403: 	$result .= ' '.join(', ',@badcollaborators).'. '.&mt('Please correct.').
  404:                    '</td></tr></table>';
  405:     }
  406:     my $toomany = scalar(@collaborators) - $ncol;
  407:     if ($toomany > 0) {
  408: 	$result .= '<table border="0"><tr bgcolor="#ffbbbb"><td>'.
  409: 	           &mt('You have too many collaborators.').' '.
  410:                    &mt('Please remove [quant,_1,collaborator].',$toomany).
  411: 	           '</td></tr></table>';
  412:     }
  413:     return $result;
  414: }
  415: 
  416: 1;
  417: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>