File:  [LON-CAPA] / loncom / homework / essayresponse.pm
Revision 1.59: download - view: text, annotated - select for diffs
Thu Feb 10 22:43:15 2005 UTC (19 years, 2 months ago) by banghart
Branches: MAIN
CVS tags: HEAD
	Fix bug 3881: accept only specified file types in response to essay

    1: # The LearningOnline Network with CAPA
    2: # essay (ungraded) style responses
    3: #
    4: # $Id: essayresponse.pm,v 1.59 2005/02/10 22:43:15 banghart 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: package Apache::essayresponse;
   30: use strict;
   31: use Apache::lonxml();
   32: use Apache::lonnet();
   33: use Apache::lonlocal;
   34: 
   35: BEGIN {
   36:     &Apache::lonxml::register('Apache::essayresponse',('essayresponse'));
   37: }
   38: 
   39: sub start_essayresponse {
   40:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   41:     my $result;
   42:     my $id = &Apache::response::start_response($parstack,$safeeval);
   43:     if ($target eq 'meta') {
   44: 	$result=&Apache::response::meta_package_write('essayresponse');
   45:     } elsif ($target eq 'web') {
   46: 	my $part= $Apache::inputtags::part;
   47: 	my $ncol= &Apache::lonnet::EXT("resource.$part".'_'."$id.maxcollaborators");
   48: 	my $coll= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.collaborators"},'<>&"');
   49: 	my $uploadedfiletypes= &Apache::lonnet::EXT("resource.$part".'_'."$id.uploadedfiletypes");
   50:         $uploadedfiletypes=~s/[^\w\,]//g;
   51: 	my $uploadedfile= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.uploadedfile"},'<>&"');
   52: 	$result='<br /><table border="1">';
   53: 	$result.='<tr><td>'.
   54: 	    '<input type="radio" name="HWDRAFT'.$part.'_'.$id.'" value="yes" checked="checked" /> '.
   55: 	    &mt('Submit entries below as answer to receive credit').' <br />'.
   56: 	    '<input type="radio" name="HWDRAFT'.$part.'_'.$id.'" value="no" /> '.
   57: 	    &mt('Save entries below as a draft answer (not submitting them for credit yet)').
   58: 	    ' </td></tr>';
   59: 	if ($ncol > 0) {
   60: 	    $result .='<tr><td>'.
   61: 		'Collaborators: <input type="text" size="70" max="80" name="HWCOL'.
   62: 		$part.'_'.$id.'" value="'.$coll.'" /><br />'.
   63: 		&mt('(Enter maximum [_1] collaborators using username or username@domain, e.g. smithje or smithje@[_2].)',$ncol,$ENV{'user.domain'}).'<br />';
   64: 	    $result .= &check_collaborators($ncol,$coll) if ($coll =~ /\w+/);
   65: 	    $result .='</td></tr>';
   66: 	}
   67:         if ($uploadedfiletypes) {
   68: 	    $result.='<tr><td>'.&mt('Submit a file: (only one file can be uploaded)').
   69: 		' <br /><input type="file" size="50" name="HWFILE'.
   70: 		$part.'_'.$id.'" /><br />'.
   71: 		&mt('Allowed filetypes: <b>[_1]</b>',$uploadedfiletypes).'<br />'.
   72:                 '<strong>OR:</strong><br /><a href='."'".'javascript:void(window.open("/adm/portfolio?mode=selectfile&amp;fieldname=HWPORT'.$part.'_'.$id.'","cat","height=600,width=800,scrollbars=1,resizable=1,menubar=2,location=1"))'."'".'>Select Portfolio Files</a>'.
   73:                 '<br />'.
   74:                 '<input type="text" size="50" name="HWPORT'.$part.'_'.$id.'" value="" />'.
   75: 		'<br />';
   76: 	    if ($Apache::lonhomework::history{"resource.$part.$id.portfiles"}=~/[^\s]/){
   77: 	        $result.="Portfolio files previously selected: <strong>".&Apache::lonnet::unescape($Apache::lonhomework::history{"resource.$part.$id.portfiles"})."</strong><br />";
   78: 	    }
   79: 
   80:             if ($uploadedfile) {
   81: 	       my $url=$Apache::lonhomework::history{"resource.$part.$id.uploadedurl"};
   82: 	       push (@Apache::lonxml::extlinks,$url);
   83: 	       &Apache::lonnet::allowuploaded('/adm/essayresponse',$url);
   84: 	       my $curfile='<a href="'.$url.
   85: 		   '"><img src="/adm/lonIcons/unknown.gif" border=0" /> '.
   86: 		   $uploadedfile.'</a>';
   87: 	       $result.=&mt('Currently submitted: <tt>[_1]</tt>',$curfile);
   88:            } else {
   89:               $result.=&mt('(Hand in a file you have prepared on your computer)');
   90:            }
   91:            $result.='</td></tr>'; 
   92:         }
   93:         $result.='</table>';
   94:     }
   95:     return $result;
   96: }
   97: 
   98: sub end_essayresponse {
   99:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  100:     my $part          = $Apache::inputtags::part;
  101:     my $id            = $Apache::inputtags::response[-1];
  102:     my $increment     = 1;
  103:     my $result;
  104:     if ( $target eq 'grade' ) {
  105: 	my $collaborators = $ENV{'form.HWCOL'.$part.'_'.$id};	
  106: 	if ($collaborators =~ /[^\s]/) {
  107: 	    my $previous_list= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.collaborators"},'<>&"');
  108: 	    $Apache::lonhomework::results{"resource.$part.$id.collaborators"}=$collaborators
  109: 		if ($collaborators ne $previous_list);
  110: 	}
  111: 	if (  &Apache::response::submitted('scantron') ) {
  112: 	    $increment=&Apache::response::scored_response($part,$id);
  113: 	} elsif ( &Apache::response::submitted() ) {
  114: 	    my $response      = $ENV{'form.HWVAL_'.$id};
  115:             my $filename= $ENV{'form.HWFILE'.$part.'_'.$id.'.filename'};
  116:             my $portfiles = $ENV{'form.HWPORT'.$part.'_'.$id};
  117: 	    if (( $response =~ /[^\s]/) || ($filename =~ /[^\s]/) || ($portfiles =~ /[^\s]/)) {
  118:  		my $award;
  119:         	if ($ENV{'form.HWDRAFT'.$part.'_'.$id} eq 'yes') {
  120: 		    $award='SUBMITTED';
  121: 		} else {
  122: 		    $award='DRAFT';
  123: 		}
  124:                 my $uploadedflag=0;
  125:                 if ($filename =~ /[^\s]/) {
  126:  	            my $uploadedfiletypes= &Apache::lonnet::EXT("resource.$part".'_'."$id.uploadedfiletypes");
  127:                     $uploadedfiletypes=~s/[^\w\,]//g;
  128:                     $uploadedfiletypes=','.$uploadedfiletypes.',';
  129:                     my ($extension)=($filename=~/\.(\w+)$/);
  130: 	            if ($uploadedfiletypes=~/\,$extension\,/i) {
  131: 	                $Apache::lonhomework::results{"resource.$part.$id.uploadedfile"}=$filename;
  132:                         $Apache::lonhomework::results{"resource.$part.$id.uploadedurl"}=
  133:                         &Apache::lonnet::userfileupload('HWFILE'.$part.'_'.$id,undef,'essayresponse');
  134:                         $uploadedflag=1;
  135: 		    } else {
  136: 		        $award='INVALID_FILETYPE';
  137:                     }
  138: 	        } else {
  139: 	           $Apache::lonhomework::results{"resource.$part.$id.uploadedfile"}="";
  140:                    $Apache::lonhomework::results{"resource.$part.$id.uploadedurl"}="";
  141: 	        }
  142:                 if ($portfiles =~ /[^\s]/) {
  143:                     $portfiles =~s/,$//;
  144:                     $Apache::lonhomework::results{"resource.$part.$id.portfiles"}=$portfiles;
  145:                     my @submitted_files = split /,/,$portfiles;
  146:  	            my $uploadedfiletypes= &Apache::lonnet::EXT("resource.$part".'_'."$id.uploadedfiletypes");
  147:                     $uploadedfiletypes=~s/[^\w\,]//g;
  148:                     $uploadedfiletypes=','.$uploadedfiletypes.',';
  149:                     foreach (@submitted_files) {
  150:                         my ($extension)=($_=~/\.(\w+)$/);
  151: 	                unless ($uploadedfiletypes=~/\,$extension\,/i) {
  152: 	                    $award='INVALID_FILETYPE';
  153:                         }
  154:                     }
  155:                     if ($award ne 'INVALID_FILETYPE'){
  156:         		my ($dsymb,$crsid,$domain,$name)=&Apache::lonxml::whichuser();
  157:         		&Apache::lonnet::unmark_as_readonly($domain,$name,$dsymb.$crsid);
  158:                         &Apache::lonnet::mark_as_readonly($domain,$name,\@submitted_files,$dsymb.$crsid);
  159:                         &Apache::lonnet::clear_selected_files($name);
  160:                         $uploadedflag=1;
  161:                     }
  162:     	        } else {
  163: 		    my ($dsymb,$crsid,$domain,$name)=&Apache::lonxml::whichuser();
  164:     	            &Apache::lonnet::unmark_as_readonly($domain,$name,$dsymb.$crsid);
  165:     	            $Apache::lonhomework::results{"resource.$part.$id.portfiles"}="";
  166:     	        }
  167: 		$Apache::lonhomework::results{"resource.$part.$id.submission"}=$response;
  168: 		$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$award;
  169: 		my %previous=&Apache::response::check_for_previous($response,$part,$id);
  170: 		unless ($uploadedflag) { &Apache::response::handle_previous(\%previous,$award); }
  171: #
  172: # Store with resource author for similarity testing
  173: #
  174:                 if ($award eq 'SUBMITTED') {
  175: 		    my ($symb,$crsid,$domain,$name)=
  176: 			&Apache::lonxml::whichuser();
  177: 		    if ($crsid) {
  178: 			my $akey=$name.'.'.$domain.'.'.$crsid;
  179: 			my $essayurl=
  180: 			    &Apache::lonnet::declutter($ENV{'REQUEST_URI'});
  181: 			my ($adom,$aname,$apath)=
  182: 			    ($essayurl=~/^(\w+)\/(\w+)\/(.*)$/);
  183:                         $apath=&Apache::lonnet::escape($apath);
  184: 			$apath=~s/\W/\_/gs;
  185: 			&Apache::lonnet::put('nohist_essay_'.$apath,
  186: 					 { $akey => $response },$adom,$aname);
  187: 		    }
  188:                 }
  189: 	    }
  190: 	} 
  191:     } elsif ($target eq 'edit') {
  192: 	$result.=&Apache::edit::end_table();
  193:     } elsif ($target eq 'tex') {
  194: 	if ($Apache::lonhomework::type eq 'exam') {
  195: 	    my $repetition=&Apache::response::repetition();
  196: 	    $result.='\begin{enumerate}';
  197: 	    if ($ENV{'request.state'} eq "construct" ) {$result.='\item[\strut]';}
  198: 	    for (my $i=0;$i<$repetition;$i++) {
  199: 		$result.='\item[\textbf{'.($Apache::lonxml::counter+$i).
  200: 		    '}.]\textit{'.&mt('Leave blank on scoring form').
  201: 		    '}\vskip 0 mm';
  202: 	    }
  203: 	    $result.= '\end{enumerate}';
  204: 	    $increment=$repetition;
  205: 	}
  206:     }
  207:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
  208: 	$target eq 'tex' || $target eq 'analyze') {
  209: 	&Apache::lonxml::increment_counter($increment);
  210:     }
  211:     &Apache::response::end_response;
  212: 
  213:     return $result;
  214: }
  215: 
  216: sub check_collaborators {
  217:     my ($ncol,$coll) = @_;
  218:     my %classlist=&Apache::lonnet::dump('classlist',
  219: 					$ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  220: 					$ENV{'course.'.$ENV{'request.course.id'}.'.num'});
  221:     my (@badcollaborators,$result);
  222:     my (@collaborators) = split(/\,?\s+/,$coll);
  223:     foreach (@collaborators) {
  224: 	my $collaborator = $_;
  225: 	if (/@/) {
  226: 	    $collaborator =~ s/@/:/;
  227: 	} else {
  228: 	    $collaborator = $_.':'.$ENV{'user.domain'};
  229: 	}
  230: 	push @badcollaborators, $_ if (!grep /^$collaborator/i,keys %classlist);
  231:     }
  232:     
  233:     if (scalar(@badcollaborators)) {
  234: 	$result = '<table border="0"><tr bgcolor="#ffbbbb"><td> The following user'.
  235: 	    (scalar(@badcollaborators) > 1 ? 's are' : ' is').' invalid: '.
  236: 	    join(', ',@badcollaborators).'. Please correct.</td></tr></table>';
  237:     }
  238:     my $toomany = scalar(@collaborators) - $ncol;
  239:     if ($toomany > 0) {
  240: 	$result .= '<table border="0"><tr bgcolor="#ffbbbb"><td>'.
  241: 	    'You have too many collaborators. Please remove '.$toomany.' collaborator'.
  242: 	    ($toomany > 1 ? 's' :'').'.</td></tr></table>';
  243:     }
  244:     return $result;
  245: }
  246: 
  247: 1;
  248: __END__

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