File:  [LON-CAPA] / loncom / homework / essayresponse.pm
Revision 1.88: download - view: text, annotated - select for diffs
Thu Aug 28 00:14:42 2008 UTC (15 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_7_X, version_2_7_1, HEAD
- localization
- more sanity checking on collaborators to prevent ISEs
- instructions on how to enter multiple usernames
- replace @ with : in instructions about entering username and domain for collaborators
  (need to support usernames which are e-mail addresses).

    1: # The LearningOnline Network with CAPA
    2: # essay (ungraded) style responses
    3: #
    4: # $Id: essayresponse.pm,v 1.88 2008/08/28 00:14:42 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: package Apache::essayresponse;
   30: use strict;
   31: use Apache::lonxml();
   32: use Apache::lonnet;
   33: use Apache::lonlocal;
   34: use LONCAPA qw(:DEFAULT :match);
   35:  
   36: 
   37: BEGIN {
   38:     &Apache::lonxml::register('Apache::essayresponse',('essayresponse'));
   39: }
   40: 
   41: sub start_essayresponse {
   42:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   43:     my $result;
   44:     my $id = &Apache::response::start_response($parstack,$safeeval);
   45:     if ($target eq 'meta') {
   46: 	$result=&Apache::response::meta_package_write('essayresponse');
   47:     } elsif ($target eq 'web' &&
   48: 	     $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
   49: 	my $part= $Apache::inputtags::part;
   50: 	my $ncol= &Apache::lonnet::EXT("resource.$part".'_'."$id.maxcollaborators");
   51: 	my $coll= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.collaborators"},'<>&"');
   52: 	my $uploadedfiletypes= &Apache::lonnet::EXT("resource.$part".'_'."$id.uploadedfiletypes");
   53:         $uploadedfiletypes=~s/[^\w\,]//g;
   54: 	if ( $Apache::lonhomework::type eq 'survey' ) {
   55: 	    $result.= '<input type="hidden" name="HWDRAFT'.$part.'_'.$id.'" value="yes" /> ';
   56: 	}
   57: 	$result.='<br /><table border="1">';
   58: 	if ( $Apache::lonhomework::type ne 'survey' ) {
   59: 	    $result.= '<tr><td>'.
   60: 		'<label>'.
   61: 		'<input type="radio" name="HWDRAFT'.$part.'_'.$id.'" value="yes" checked="checked" /> '.
   62: 		&mt('Submit entries below as answer to receive credit').
   63: 		'</label> <br />'.
   64: 		'<label>'.
   65: 		'<input type="radio" name="HWDRAFT'.$part.'_'.$id.'" value="no" /> '.
   66: 		&mt('Save entries below as a draft answer (not submitting them for credit yet)').
   67: 		'</label>'.
   68: 		'</td></tr>';
   69: 	}
   70: 
   71: 	if ($ncol > 0) {
   72: 	    $result .='<tr><td>'.'<label>'.
   73: 		      &mt('Collaborators:').' <input type="text" size="70" max="80" name="HWCOL'.
   74: 		      $part.'_'.$id.'" value="'.$coll.'" /><br />'.
   75: 		      &mt('(Enter a maximum of [quant,_1,collaborator] using username or username:domain, e.g. smithje or smithje:[_2].)',$ncol,$env{'user.domain'});
   76:             if ($ncol > 1) {
   77:                 $result .= '<br />'.&mt('If entering more than one, use spaces to separate the collaborators.');
   78:             }
   79:             $result .= '</label><br />';
   80: 	    $result .= &check_collaborators($ncol,$coll) if ($coll =~ /\w+/);
   81: 	    $result .='</td></tr>';
   82: 	}
   83: 	$result.=&Apache::inputtags::file_selector($part,$id,
   84: 						   $uploadedfiletypes,'both');
   85:         $result.='</table>';
   86:     } elsif ($target eq 'web' &&
   87: 	     $Apache::inputtags::status[-1] ne 'CAN_ANSWER') {
   88: 	my $part= $Apache::inputtags::part;
   89: 	my @msgs;
   90: 	if ($Apache::lonhomework::history{"resource.$part.$id.collaborators"} =~ /\S/) {
   91: 	    my $coll= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.collaborators"},'<>&"');
   92: 	    $result .= '<td>'.&mt('Collaborated with [_1]',$coll).'</td>';
   93: 	}
   94: 
   95: 	my $file_submission = 
   96: 	    &Apache::inputtags::show_past_file_submission($part,$id);
   97: 	if ($file_submission) {
   98: 	    $result .= '<td>'.$file_submission.'</td>';
   99: 	}
  100: 
  101: 	my $port_submission = 
  102: 	    &Apache::inputtags::show_past_portfile_submission($part,$id);
  103: 	if ($port_submission) {
  104: 	    $result .= '<td>'.$port_submission.'</td>';
  105: 	}
  106: 
  107: 	if ($result ne '') {
  108: 	    $result = 
  109: 		'<table class="LC_pastsubmission"><tr>'.$result.
  110: 		'</tr></table>';
  111: 	}
  112:     }
  113:     return $result;
  114: }
  115: 
  116: sub end_essayresponse {
  117:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  118:     my $part          = $Apache::inputtags::part;
  119:     my $id            = $Apache::inputtags::response[-1];
  120:     my $increment     = &Apache::response::repetition();
  121:     my $result;
  122:     if ( $target eq 'grade' ) {
  123: 	my $collaborators = $env{'form.HWCOL'.$part.'_'.$id};	
  124: 	if ($collaborators =~ /[^\s]/) {
  125: 	    my $previous_list= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.collaborators"},'<>&"');
  126: 	    $Apache::lonhomework::results{"resource.$part.$id.collaborators"}=$collaborators
  127: 		if ($collaborators ne $previous_list);
  128: 	}
  129: 	if (  &Apache::response::submitted('scantron') ) {
  130: 	    $increment=&Apache::response::scored_response($part,$id);
  131: 	} elsif ( &Apache::response::submitted() ) {
  132: 	    my $response      = $env{'form.HWVAL_'.$id};
  133:             my $filename= $env{'form.HWFILE'.$part.'_'.$id.'.filename'};
  134:             my $portfiles = $env{'form.HWPORT'.$part.'_'.$id};
  135: 	    if (( $response =~ /[^\s]/) || ($filename =~ /[^\s]/) || ($portfiles =~ /[^\s]/)) {
  136:  		my $award='DRAFT';
  137:         	if ($env{'form.HWDRAFT'.$part.'_'.$id} eq 'yes') {
  138: 		    $award='SUBMITTED';
  139: 		}
  140:                 my $uploadedflag=0;
  141: 		&file_submission($part,$id,'filename',\$award,\$uploadedflag);
  142: 		&file_submission($part,$id,'portfiles',\$award,\$uploadedflag);
  143: 		$Apache::lonhomework::results{"resource.$part.$id.submission"}=$response;
  144: 		$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$award;
  145: 		my %previous=&Apache::response::check_for_previous($response,$part,$id);
  146: 		unless ($uploadedflag) { &Apache::response::handle_previous(\%previous,$award); }
  147: #
  148: # Store with resource author for similarity testing
  149: #
  150:                 if ($award eq 'SUBMITTED') {
  151: 		    my ($symb,$crsid,$domain,$name)=
  152: 			&Apache::lonnet::whichuser();
  153: 		    if ($crsid) {
  154: 			my $akey=join('.',&escape($name),&escape($domain),
  155: 				      &escape($crsid));
  156: 			my $essayurl=
  157: 			    &Apache::lonnet::declutter($ENV{'REQUEST_URI'});
  158: 			my ($adom,$aname,$apath)=
  159: 			    ($essayurl=~/^($LONCAPA::domain_re)\/($LONCAPA::username_re)\/(.*)$/);
  160:                         $apath=&escape($apath);
  161: 			$apath=~s/\W/\_/gs;
  162: 			&Apache::lonnet::put('nohist_essay_'.$apath,
  163: 					 { $akey => $response },$adom,$aname);
  164: 		    }
  165:                 }
  166: 	    }
  167: 	} 
  168:     } elsif ($target eq 'edit') {
  169: 	$result.=&Apache::edit::end_table();
  170: 
  171:     } elsif ($target eq 'tex'
  172: 	     && $Apache::lonhomework::type eq 'exam') {
  173: 	$result .= &Apache::inputtags::exam_score_line($target);
  174: 
  175:     } elsif ($target eq 'answer') {
  176: 	$result.=&Apache::response::answer_header($$tagstack[-1]);
  177: 	my $answer = &mt('Essay will be hand graded.');
  178: 	$result.=&Apache::response::answer_part($$tagstack[-1],$answer,
  179: 						{'no_verbatim' => 1});
  180: 	$result.=&Apache::response::answer_footer($$tagstack[-1]);
  181:     }
  182:     if ($target eq 'web') {
  183: 	&Apache::response::setup_prior_tries_hash(\&format_prior_response,
  184: 						  ['portfiles',
  185: 						   'uploadedurl']);
  186:     }
  187: 
  188:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
  189: 	$target eq 'tex' || $target eq 'analyze') {
  190: 	&Apache::lonxml::increment_counter($increment, "$part.$id");
  191: 
  192: 	if ($target eq 'analyze') {
  193:             $Apache::lonhomework::analyze{"$part.$id.type"} = 'essayresponse';
  194: 	    &Apache::lonhomework::set_bubble_lines();
  195: 	}
  196:     }
  197:     &Apache::response::end_response;
  198: 
  199:     return $result;
  200: }
  201: 
  202: sub format_prior_response {
  203:     my ($mode,$answer,$other_data) = @_;
  204:     my $output;
  205: 
  206:     my (undef,undef,$udom,$uname) = &Apache::lonnet::whichuser();
  207:     my $port_url = '/uploaded/'.$udom.'/'.$uname.'/portfolio/';
  208: 
  209:     my $file_list;
  210: 
  211:     foreach my $file (split(/\s*,\s*/,
  212: 			    $other_data->[0].','.$other_data->[1])) {
  213: 	next if ($file!~/\S/);
  214: 	if ($file !~ m{^/uploaded/}) { $file=$port_url.$file; }
  215: 	$file=~s|/+|/|g;
  216: 	&Apache::lonnet::allowuploaded('/adm/essayresponse',$file);
  217: 	$file_list.='<li><span class="LC_nobreak"><a href="'.$file.'?rawmode=1" target="lonGRDs"><img src="'.
  218: 	    &Apache::loncommon::icon($file).'" alt="file icon" border="0" /> '.$file.
  219: 	    '</a></span></li>'."\n";
  220:     }
  221:     if ($file_list) {
  222: 	$output.= &mt('Submitted Files').'<ul>'.$file_list.'</ul>';
  223:     }
  224:     if ($answer =~ /\S/) {
  225: 	$output.='<p>'.&mt('Submitted text').
  226: 	    '<blockquote>'.$answer.'</blockquote></p>';
  227:     }
  228: 
  229:     return '<div class="LC_prior_essay">'.$output.'</div>';
  230: }
  231: 
  232: sub file_submission {
  233:     my ($part,$id,$which,$award,$uploadedflag)=@_;
  234:     my $files;
  235:     my $jspart=$part;
  236:     $jspart=~s/\./_/g;
  237:     if ($which eq 'portfiles') { $files= $env{'form.HWPORT'.$jspart.'_'.$id}; }
  238:     if ($which eq 'filename') {
  239: 	$files = $env{'form.HWFILE'.$jspart.'_'.$id.'.filename'};
  240:     }
  241:     
  242:     if ($files =~ /[^\s]/) {
  243: 	$files =~s/,$//;
  244: 	$Apache::lonhomework::results{"resource.$part.$id.$which"}=$files;
  245: 	
  246: 	my @submitted_files = ($files);
  247: 	if ( $which eq 'portfiles' ) {
  248: 	    @submitted_files = split(/\s*,\s*/,$files);
  249: 	}
  250: 
  251: 	my $uploadedfiletypes= &Apache::lonnet::EXT("resource.$part".'_'."$id.uploadedfiletypes");
  252: 	if ($uploadedfiletypes) {
  253: 	    $uploadedfiletypes=~s/[^\w\,]//g;
  254: 	    $uploadedfiletypes=','.$uploadedfiletypes.',';
  255: 	    foreach my $file (@submitted_files) {
  256: 		my ($extension)=($file=~/\.(\w+)$/);
  257: 		unless ($uploadedfiletypes=~/\,\s*\Q$extension\E\s*\,/i) {
  258: 		    $$award='INVALID_FILETYPE';
  259: 		}
  260: 	    }
  261: 	}
  262: 	if ($$award ne 'INVALID_FILETYPE' && ref($uploadedflag)) {
  263: 	    $$uploadedflag=1;
  264: 	}
  265: 	if ($$award ne 'INVALID_FILETYPE' && $which eq 'portfiles') {
  266: 	    my ($symb,$crsid,$domain,$name)=&Apache::lonnet::whichuser();
  267: 	    &Apache::lonnet::unmark_as_readonly($domain,$name,[$symb,$crsid]);
  268: 	    &Apache::lonnet::mark_as_readonly($domain,$name,\@submitted_files,[$symb,$crsid]);
  269: 	    &Apache::lonnet::clear_selected_files($name);
  270: 	}
  271: 	if ($$award ne 'INVALID_FILETYPE' && $which eq 'filename') {
  272: 	    $Apache::lonhomework::results{"resource.$part.$id.uploadedfile"}=
  273: 		$files;
  274: 	    $Apache::lonhomework::results{"resource.$part.$id.uploadedurl"}=
  275: 		&Apache::lonnet::userfileupload('HWFILE'.$jspart.'_'.$id,undef,
  276: 						'essayresponse');
  277: 	}
  278:     } elsif ($which eq 'portfiles' &&
  279: 	     $Apache::lonhomework::history{"resource.$part.$id.$which"}) {
  280: 	my ($symb,$crsid,$domain,$name)=&Apache::lonnet::whichuser();
  281: 	&Apache::lonnet::unmark_as_readonly($domain,$name,[$symb,$crsid]);
  282: 	$Apache::lonhomework::results{"resource.$part.$id.$which"}="";
  283:     }
  284: }
  285: 
  286: sub check_collaborators {
  287:     my ($ncol,$coll) = @_;
  288:     my %classlist=&Apache::lonnet::dump('classlist',
  289: 					$env{'course.'.$env{'request.course.id'}.'.domain'},
  290: 					$env{'course.'.$env{'request.course.id'}.'.num'});
  291:     my (@badcollaborators,$result);
  292:   
  293:     my (@collaborators) = split(/,?\s+/,$coll);
  294:     foreach my $entry (@collaborators) {
  295:         my $collaborator;
  296: 	if ($entry =~ /:/) {
  297: 	    $collaborator = $entry;
  298: 	} else {
  299: 	    $collaborator = $entry.':'.$env{'user.domain'};
  300: 	}
  301:         if ($collaborator !~ /^$match_username:$match_domain$/) {
  302:             if (!grep(/^\Q$entry\E$/,@badcollaborators)) {
  303: 	        push(@badcollaborators,$entry);
  304:             }
  305:         } elsif (!grep(/^\Q$collaborator\E$/i,keys(%classlist))) {
  306:             if (!grep(/^\Q$entry\E$/,@badcollaborators)) {
  307:                 push(@badcollaborators,$entry);
  308:             }
  309:         }
  310:     }
  311:     
  312:     my $numbad = scalar(@badcollaborators);
  313:     if ($numbad) {
  314: 	$result = '<table border="0"><tr bgcolor="#ffbbbb"><td>';
  315:         if ($numbad == 1) {
  316:             $result .= &mt('The following user is invalid:');
  317:         } else {
  318:             $result .= &mt('The following [_1] users are invalid:',$numbad);
  319:         }
  320: 	$result .= ' '.join(', ',@badcollaborators).'. '.&mt('Please correct.').
  321:                    '</td></tr></table>';
  322:     }
  323:     my $toomany = scalar(@collaborators) - $ncol;
  324:     if ($toomany > 0) {
  325: 	$result .= '<table border="0"><tr bgcolor="#ffbbbb"><td>'.
  326: 	           &mt('You have too many collaborators.').' '.
  327:                    &mt('Please remove [quant,_1,collaborator].',$toomany).
  328: 	           '</td></tr></table>';
  329:     }
  330:     return $result;
  331: }
  332: 
  333: 1;
  334: __END__

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