Annotation of loncom/homework/essayresponse.pm, revision 1.90

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # essay (ungraded) style responses
1.5       albertel    3: #
1.90    ! jms         4: # $Id: essayresponse.pm,v 1.89 2008/11/07 18:58:06 jms Exp $
1.5       albertel    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: #
1.33      albertel   28: 
1.89      jms        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: 
1.1       albertel   44: package Apache::essayresponse;
                     45: use strict;
1.33      albertel   46: use Apache::lonxml();
1.62      albertel   47: use Apache::lonnet;
1.33      albertel   48: use Apache::lonlocal;
1.88      raeburn    49: use LONCAPA qw(:DEFAULT :match);
1.72      www        50:  
1.1       albertel   51: 
1.6       harris41   52: BEGIN {
1.10      ng         53:     &Apache::lonxml::register('Apache::essayresponse',('essayresponse'));
1.1       albertel   54: }
                     55: 
                     56: sub start_essayresponse {
1.10      ng         57:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     58:     my $result;
1.14      albertel   59:     my $id = &Apache::response::start_response($parstack,$safeeval);
                     60:     if ($target eq 'meta') {
                     61: 	$result=&Apache::response::meta_package_write('essayresponse');
1.69      albertel   62:     } elsif ($target eq 'web' &&
                     63: 	     $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
1.10      ng         64: 	my $part= $Apache::inputtags::part;
                     65: 	my $ncol= &Apache::lonnet::EXT("resource.$part".'_'."$id.maxcollaborators");
1.34      albertel   66: 	my $coll= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.collaborators"},'<>&"');
1.17      www        67: 	my $uploadedfiletypes= &Apache::lonnet::EXT("resource.$part".'_'."$id.uploadedfiletypes");
1.21      www        68:         $uploadedfiletypes=~s/[^\w\,]//g;
1.75      albertel   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: 
1.10      ng         86: 	if ($ncol > 0) {
1.64      matthew    87: 	    $result .='<tr><td>'.'<label>'.
1.88      raeburn    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 />';
1.10      ng         95: 	    $result .= &check_collaborators($ncol,$coll) if ($coll =~ /\w+/);
1.13      ng         96: 	    $result .='</td></tr>';
1.10      ng         97: 	}
1.60      albertel   98: 	$result.=&Apache::inputtags::file_selector($part,$id,
                     99: 						   $uploadedfiletypes,'both');
1.22      www       100:         $result.='</table>';
1.74      albertel  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: 	}
1.10      ng        127:     }
                    128:     return $result;
1.1       albertel  129: }
                    130: 
                    131: sub end_essayresponse {
1.10      ng        132:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.13      ng        133:     my $part          = $Apache::inputtags::part;
1.14      albertel  134:     my $id            = $Apache::inputtags::response[-1];
1.70      albertel  135:     my $increment     = &Apache::response::repetition();
1.15      albertel  136:     my $result;
1.10      ng        137:     if ( $target eq 'grade' ) {
1.62      albertel  138: 	my $collaborators = $env{'form.HWCOL'.$part.'_'.$id};	
1.14      albertel  139: 	if ($collaborators =~ /[^\s]/) {
1.34      albertel  140: 	    my $previous_list= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.collaborators"},'<>&"');
1.14      albertel  141: 	    $Apache::lonhomework::results{"resource.$part.$id.collaborators"}=$collaborators
                    142: 		if ($collaborators ne $previous_list);
                    143: 	}
1.58      albertel  144: 	if (  &Apache::response::submitted('scantron') ) {
1.31      albertel  145: 	    $increment=&Apache::response::scored_response($part,$id);
1.58      albertel  146: 	} elsif ( &Apache::response::submitted() ) {
1.62      albertel  147: 	    my $response      = $env{'form.HWVAL_'.$id};
                    148:             my $filename= $env{'form.HWFILE'.$part.'_'.$id.'.filename'};
                    149:             my $portfiles = $env{'form.HWPORT'.$part.'_'.$id};
1.42      banghart  150: 	    if (( $response =~ /[^\s]/) || ($filename =~ /[^\s]/) || ($portfiles =~ /[^\s]/)) {
1.61      albertel  151:  		my $award='DRAFT';
1.62      albertel  152:         	if ($env{'form.HWDRAFT'.$part.'_'.$id} eq 'yes') {
1.14      albertel  153: 		    $award='SUBMITTED';
                    154: 		}
1.22      www       155:                 my $uploadedflag=0;
1.63      albertel  156: 		&file_submission($part,$id,'filename',\$award,\$uploadedflag);
                    157: 		&file_submission($part,$id,'portfiles',\$award,\$uploadedflag);
1.10      ng        158: 		$Apache::lonhomework::results{"resource.$part.$id.submission"}=$response;
1.14      albertel  159: 		$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$award;
1.10      ng        160: 		my %previous=&Apache::response::check_for_previous($response,$part,$id);
1.22      www       161: 		unless ($uploadedflag) { &Apache::response::handle_previous(\%previous,$award); }
1.32      www       162: #
                    163: # Store with resource author for similarity testing
                    164: #
                    165:                 if ($award eq 'SUBMITTED') {
                    166: 		    my ($symb,$crsid,$domain,$name)=
1.77      albertel  167: 			&Apache::lonnet::whichuser();
1.32      www       168: 		    if ($crsid) {
1.83      albertel  169: 			my $akey=join('.',&escape($name),&escape($domain),
                    170: 				      &escape($crsid));
1.32      www       171: 			my $essayurl=
                    172: 			    &Apache::lonnet::declutter($ENV{'REQUEST_URI'});
                    173: 			my ($adom,$aname,$apath)=
1.80      albertel  174: 			    ($essayurl=~/^($LONCAPA::domain_re)\/($LONCAPA::username_re)\/(.*)$/);
1.72      www       175:                         $apath=&escape($apath);
1.32      www       176: 			$apath=~s/\W/\_/gs;
                    177: 			&Apache::lonnet::put('nohist_essay_'.$apath,
                    178: 					 { $akey => $response },$adom,$aname);
                    179: 		    }
1.42      banghart  180:                 }
1.10      ng        181: 	    }
1.42      banghart  182: 	} 
1.15      albertel  183:     } elsif ($target eq 'edit') {
                    184: 	$result.=&Apache::edit::end_table();
1.71      albertel  185: 
                    186:     } elsif ($target eq 'tex'
                    187: 	     && $Apache::lonhomework::type eq 'exam') {
                    188: 	$result .= &Apache::inputtags::exam_score_line($target);
                    189: 
1.70      albertel  190:     } elsif ($target eq 'answer') {
1.78      albertel  191: 	$result.=&Apache::response::answer_header($$tagstack[-1]);
1.81      foxr      192: 	my $answer = &mt('Essay will be hand graded.');
1.78      albertel  193: 	$result.=&Apache::response::answer_part($$tagstack[-1],$answer,
                    194: 						{'no_verbatim' => 1});
                    195: 	$result.=&Apache::response::answer_footer($$tagstack[-1]);
1.10      ng        196:     }
1.82      albertel  197:     if ($target eq 'web') {
                    198: 	&Apache::response::setup_prior_tries_hash(\&format_prior_response,
                    199: 						  ['portfiles',
                    200: 						   'uploadedurl']);
                    201:     }
1.71      albertel  202: 
1.27      albertel  203:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
                    204: 	$target eq 'tex' || $target eq 'analyze') {
1.86      foxr      205: 	&Apache::lonxml::increment_counter($increment, "$part.$id");
1.85      foxr      206: 
                    207: 	if ($target eq 'analyze') {
1.87      raeburn   208:             $Apache::lonhomework::analyze{"$part.$id.type"} = 'essayresponse';
1.85      foxr      209: 	    &Apache::lonhomework::set_bubble_lines();
                    210: 	}
1.27      albertel  211:     }
1.10      ng        212:     &Apache::response::end_response;
1.42      banghart  213: 
1.15      albertel  214:     return $result;
1.10      ng        215: }
                    216: 
1.82      albertel  217: sub format_prior_response {
                    218:     my ($mode,$answer,$other_data) = @_;
                    219:     my $output;
                    220: 
                    221:     my (undef,undef,$udom,$uname) = &Apache::lonnet::whichuser();
                    222:     my $port_url = '/uploaded/'.$udom.'/'.$uname.'/portfolio/';
                    223: 
                    224:     my $file_list;
                    225: 
                    226:     foreach my $file (split(/\s*,\s*/,
                    227: 			    $other_data->[0].','.$other_data->[1])) {
                    228: 	next if ($file!~/\S/);
                    229: 	if ($file !~ m{^/uploaded/}) { $file=$port_url.$file; }
                    230: 	$file=~s|/+|/|g;
                    231: 	&Apache::lonnet::allowuploaded('/adm/essayresponse',$file);
                    232: 	$file_list.='<li><span class="LC_nobreak"><a href="'.$file.'?rawmode=1" target="lonGRDs"><img src="'.
                    233: 	    &Apache::loncommon::icon($file).'" alt="file icon" border="0" /> '.$file.
                    234: 	    '</a></span></li>'."\n";
                    235:     }
                    236:     if ($file_list) {
                    237: 	$output.= &mt('Submitted Files').'<ul>'.$file_list.'</ul>';
                    238:     }
                    239:     if ($answer =~ /\S/) {
                    240: 	$output.='<p>'.&mt('Submitted text').
                    241: 	    '<blockquote>'.$answer.'</blockquote></p>';
                    242:     }
                    243: 
                    244:     return '<div class="LC_prior_essay">'.$output.'</div>';
                    245: }
                    246: 
1.61      albertel  247: sub file_submission {
                    248:     my ($part,$id,$which,$award,$uploadedflag)=@_;
                    249:     my $files;
1.67      albertel  250:     my $jspart=$part;
                    251:     $jspart=~s/\./_/g;
                    252:     if ($which eq 'portfiles') { $files= $env{'form.HWPORT'.$jspart.'_'.$id}; }
1.61      albertel  253:     if ($which eq 'filename') {
1.67      albertel  254: 	$files = $env{'form.HWFILE'.$jspart.'_'.$id.'.filename'};
1.61      albertel  255:     }
                    256:     
                    257:     if ($files =~ /[^\s]/) {
                    258: 	$files =~s/,$//;
                    259: 	$Apache::lonhomework::results{"resource.$part.$id.$which"}=$files;
1.68      albertel  260: 	
                    261: 	my @submitted_files = ($files);
                    262: 	if ( $which eq 'portfiles' ) {
1.73      albertel  263: 	    @submitted_files = split(/\s*,\s*/,$files);
1.68      albertel  264: 	}
                    265: 
1.61      albertel  266: 	my $uploadedfiletypes= &Apache::lonnet::EXT("resource.$part".'_'."$id.uploadedfiletypes");
1.65      albertel  267: 	if ($uploadedfiletypes) {
                    268: 	    $uploadedfiletypes=~s/[^\w\,]//g;
                    269: 	    $uploadedfiletypes=','.$uploadedfiletypes.',';
                    270: 	    foreach my $file (@submitted_files) {
                    271: 		my ($extension)=($file=~/\.(\w+)$/);
1.84      albertel  272: 		unless ($uploadedfiletypes=~/\,\s*\Q$extension\E\s*\,/i) {
1.65      albertel  273: 		    $$award='INVALID_FILETYPE';
                    274: 		}
1.61      albertel  275: 	    }
                    276: 	}
1.63      albertel  277: 	if ($$award ne 'INVALID_FILETYPE' && ref($uploadedflag)) {
1.61      albertel  278: 	    $$uploadedflag=1;
                    279: 	}
1.63      albertel  280: 	if ($$award ne 'INVALID_FILETYPE' && $which eq 'portfiles') {
1.77      albertel  281: 	    my ($symb,$crsid,$domain,$name)=&Apache::lonnet::whichuser();
1.66      albertel  282: 	    &Apache::lonnet::unmark_as_readonly($domain,$name,[$symb,$crsid]);
                    283: 	    &Apache::lonnet::mark_as_readonly($domain,$name,\@submitted_files,[$symb,$crsid]);
1.61      albertel  284: 	    &Apache::lonnet::clear_selected_files($name);
                    285: 	}
1.63      albertel  286: 	if ($$award ne 'INVALID_FILETYPE' && $which eq 'filename') {
1.61      albertel  287: 	    $Apache::lonhomework::results{"resource.$part.$id.uploadedfile"}=
                    288: 		$files;
                    289: 	    $Apache::lonhomework::results{"resource.$part.$id.uploadedurl"}=
1.67      albertel  290: 		&Apache::lonnet::userfileupload('HWFILE'.$jspart.'_'.$id,undef,
1.61      albertel  291: 						'essayresponse');
                    292: 	}
                    293:     } elsif ($which eq 'portfiles' &&
                    294: 	     $Apache::lonhomework::history{"resource.$part.$id.$which"}) {
1.77      albertel  295: 	my ($symb,$crsid,$domain,$name)=&Apache::lonnet::whichuser();
1.66      albertel  296: 	&Apache::lonnet::unmark_as_readonly($domain,$name,[$symb,$crsid]);
1.61      albertel  297: 	$Apache::lonhomework::results{"resource.$part.$id.$which"}="";
                    298:     }
                    299: }
                    300: 
1.10      ng        301: sub check_collaborators {
1.11      ng        302:     my ($ncol,$coll) = @_;
1.10      ng        303:     my %classlist=&Apache::lonnet::dump('classlist',
1.62      albertel  304: 					$env{'course.'.$env{'request.course.id'}.'.domain'},
                    305: 					$env{'course.'.$env{'request.course.id'}.'.num'});
1.10      ng        306:     my (@badcollaborators,$result);
1.88      raeburn   307:   
                    308:     my (@collaborators) = split(/,?\s+/,$coll);
                    309:     foreach my $entry (@collaborators) {
                    310:         my $collaborator;
                    311: 	if ($entry =~ /:/) {
                    312: 	    $collaborator = $entry;
1.10      ng        313: 	} else {
1.88      raeburn   314: 	    $collaborator = $entry.':'.$env{'user.domain'};
1.10      ng        315: 	}
1.88      raeburn   316:         if ($collaborator !~ /^$match_username:$match_domain$/) {
                    317:             if (!grep(/^\Q$entry\E$/,@badcollaborators)) {
                    318: 	        push(@badcollaborators,$entry);
                    319:             }
                    320:         } elsif (!grep(/^\Q$collaborator\E$/i,keys(%classlist))) {
                    321:             if (!grep(/^\Q$entry\E$/,@badcollaborators)) {
                    322:                 push(@badcollaborators,$entry);
                    323:             }
                    324:         }
1.10      ng        325:     }
                    326:     
1.88      raeburn   327:     my $numbad = scalar(@badcollaborators);
                    328:     if ($numbad) {
                    329: 	$result = '<table border="0"><tr bgcolor="#ffbbbb"><td>';
                    330:         if ($numbad == 1) {
                    331:             $result .= &mt('The following user is invalid:');
                    332:         } else {
                    333:             $result .= &mt('The following [_1] users are invalid:',$numbad);
                    334:         }
                    335: 	$result .= ' '.join(', ',@badcollaborators).'. '.&mt('Please correct.').
                    336:                    '</td></tr></table>';
1.10      ng        337:     }
                    338:     my $toomany = scalar(@collaborators) - $ncol;
                    339:     if ($toomany > 0) {
                    340: 	$result .= '<table border="0"><tr bgcolor="#ffbbbb"><td>'.
1.88      raeburn   341: 	           &mt('You have too many collaborators.').' '.
                    342:                    &mt('Please remove [quant,_1,collaborator].',$toomany).
                    343: 	           '</td></tr></table>';
1.10      ng        344:     }
                    345:     return $result;
1.1       albertel  346: }
1.2       albertel  347: 
                    348: 1;
                    349: __END__

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