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

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # essay (ungraded) style responses
1.5       albertel    3: #
1.89    ! jms         4: # $Id: essayresponse.pm,v 1.88 2008/08/28 00:14:42 raeburn 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: =head2 Subroutines
        !            43: 
        !            44: =cut
        !            45: 
1.1       albertel   46: package Apache::essayresponse;
                     47: use strict;
1.33      albertel   48: use Apache::lonxml();
1.62      albertel   49: use Apache::lonnet;
1.33      albertel   50: use Apache::lonlocal;
1.88      raeburn    51: use LONCAPA qw(:DEFAULT :match);
1.72      www        52:  
1.1       albertel   53: 
1.6       harris41   54: BEGIN {
1.10      ng         55:     &Apache::lonxml::register('Apache::essayresponse',('essayresponse'));
1.1       albertel   56: }
                     57: 
                     58: sub start_essayresponse {
1.10      ng         59:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     60:     my $result;
1.14      albertel   61:     my $id = &Apache::response::start_response($parstack,$safeeval);
                     62:     if ($target eq 'meta') {
                     63: 	$result=&Apache::response::meta_package_write('essayresponse');
1.69      albertel   64:     } elsif ($target eq 'web' &&
                     65: 	     $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
1.10      ng         66: 	my $part= $Apache::inputtags::part;
                     67: 	my $ncol= &Apache::lonnet::EXT("resource.$part".'_'."$id.maxcollaborators");
1.34      albertel   68: 	my $coll= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.collaborators"},'<>&"');
1.17      www        69: 	my $uploadedfiletypes= &Apache::lonnet::EXT("resource.$part".'_'."$id.uploadedfiletypes");
1.21      www        70:         $uploadedfiletypes=~s/[^\w\,]//g;
1.75      albertel   71: 	if ( $Apache::lonhomework::type eq 'survey' ) {
                     72: 	    $result.= '<input type="hidden" name="HWDRAFT'.$part.'_'.$id.'" value="yes" /> ';
                     73: 	}
                     74: 	$result.='<br /><table border="1">';
                     75: 	if ( $Apache::lonhomework::type ne 'survey' ) {
                     76: 	    $result.= '<tr><td>'.
                     77: 		'<label>'.
                     78: 		'<input type="radio" name="HWDRAFT'.$part.'_'.$id.'" value="yes" checked="checked" /> '.
                     79: 		&mt('Submit entries below as answer to receive credit').
                     80: 		'</label> <br />'.
                     81: 		'<label>'.
                     82: 		'<input type="radio" name="HWDRAFT'.$part.'_'.$id.'" value="no" /> '.
                     83: 		&mt('Save entries below as a draft answer (not submitting them for credit yet)').
                     84: 		'</label>'.
                     85: 		'</td></tr>';
                     86: 	}
                     87: 
1.10      ng         88: 	if ($ncol > 0) {
1.64      matthew    89: 	    $result .='<tr><td>'.'<label>'.
1.88      raeburn    90: 		      &mt('Collaborators:').' <input type="text" size="70" max="80" name="HWCOL'.
                     91: 		      $part.'_'.$id.'" value="'.$coll.'" /><br />'.
                     92: 		      &mt('(Enter a maximum of [quant,_1,collaborator] using username or username:domain, e.g. smithje or smithje:[_2].)',$ncol,$env{'user.domain'});
                     93:             if ($ncol > 1) {
                     94:                 $result .= '<br />'.&mt('If entering more than one, use spaces to separate the collaborators.');
                     95:             }
                     96:             $result .= '</label><br />';
1.10      ng         97: 	    $result .= &check_collaborators($ncol,$coll) if ($coll =~ /\w+/);
1.13      ng         98: 	    $result .='</td></tr>';
1.10      ng         99: 	}
1.60      albertel  100: 	$result.=&Apache::inputtags::file_selector($part,$id,
                    101: 						   $uploadedfiletypes,'both');
1.22      www       102:         $result.='</table>';
1.74      albertel  103:     } elsif ($target eq 'web' &&
                    104: 	     $Apache::inputtags::status[-1] ne 'CAN_ANSWER') {
                    105: 	my $part= $Apache::inputtags::part;
                    106: 	my @msgs;
                    107: 	if ($Apache::lonhomework::history{"resource.$part.$id.collaborators"} =~ /\S/) {
                    108: 	    my $coll= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.collaborators"},'<>&"');
                    109: 	    $result .= '<td>'.&mt('Collaborated with [_1]',$coll).'</td>';
                    110: 	}
                    111: 
                    112: 	my $file_submission = 
                    113: 	    &Apache::inputtags::show_past_file_submission($part,$id);
                    114: 	if ($file_submission) {
                    115: 	    $result .= '<td>'.$file_submission.'</td>';
                    116: 	}
                    117: 
                    118: 	my $port_submission = 
                    119: 	    &Apache::inputtags::show_past_portfile_submission($part,$id);
                    120: 	if ($port_submission) {
                    121: 	    $result .= '<td>'.$port_submission.'</td>';
                    122: 	}
                    123: 
                    124: 	if ($result ne '') {
                    125: 	    $result = 
                    126: 		'<table class="LC_pastsubmission"><tr>'.$result.
                    127: 		'</tr></table>';
                    128: 	}
1.10      ng        129:     }
                    130:     return $result;
1.1       albertel  131: }
                    132: 
                    133: sub end_essayresponse {
1.10      ng        134:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.13      ng        135:     my $part          = $Apache::inputtags::part;
1.14      albertel  136:     my $id            = $Apache::inputtags::response[-1];
1.70      albertel  137:     my $increment     = &Apache::response::repetition();
1.15      albertel  138:     my $result;
1.10      ng        139:     if ( $target eq 'grade' ) {
1.62      albertel  140: 	my $collaborators = $env{'form.HWCOL'.$part.'_'.$id};	
1.14      albertel  141: 	if ($collaborators =~ /[^\s]/) {
1.34      albertel  142: 	    my $previous_list= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.collaborators"},'<>&"');
1.14      albertel  143: 	    $Apache::lonhomework::results{"resource.$part.$id.collaborators"}=$collaborators
                    144: 		if ($collaborators ne $previous_list);
                    145: 	}
1.58      albertel  146: 	if (  &Apache::response::submitted('scantron') ) {
1.31      albertel  147: 	    $increment=&Apache::response::scored_response($part,$id);
1.58      albertel  148: 	} elsif ( &Apache::response::submitted() ) {
1.62      albertel  149: 	    my $response      = $env{'form.HWVAL_'.$id};
                    150:             my $filename= $env{'form.HWFILE'.$part.'_'.$id.'.filename'};
                    151:             my $portfiles = $env{'form.HWPORT'.$part.'_'.$id};
1.42      banghart  152: 	    if (( $response =~ /[^\s]/) || ($filename =~ /[^\s]/) || ($portfiles =~ /[^\s]/)) {
1.61      albertel  153:  		my $award='DRAFT';
1.62      albertel  154:         	if ($env{'form.HWDRAFT'.$part.'_'.$id} eq 'yes') {
1.14      albertel  155: 		    $award='SUBMITTED';
                    156: 		}
1.22      www       157:                 my $uploadedflag=0;
1.63      albertel  158: 		&file_submission($part,$id,'filename',\$award,\$uploadedflag);
                    159: 		&file_submission($part,$id,'portfiles',\$award,\$uploadedflag);
1.10      ng        160: 		$Apache::lonhomework::results{"resource.$part.$id.submission"}=$response;
1.14      albertel  161: 		$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$award;
1.10      ng        162: 		my %previous=&Apache::response::check_for_previous($response,$part,$id);
1.22      www       163: 		unless ($uploadedflag) { &Apache::response::handle_previous(\%previous,$award); }
1.32      www       164: #
                    165: # Store with resource author for similarity testing
                    166: #
                    167:                 if ($award eq 'SUBMITTED') {
                    168: 		    my ($symb,$crsid,$domain,$name)=
1.77      albertel  169: 			&Apache::lonnet::whichuser();
1.32      www       170: 		    if ($crsid) {
1.83      albertel  171: 			my $akey=join('.',&escape($name),&escape($domain),
                    172: 				      &escape($crsid));
1.32      www       173: 			my $essayurl=
                    174: 			    &Apache::lonnet::declutter($ENV{'REQUEST_URI'});
                    175: 			my ($adom,$aname,$apath)=
1.80      albertel  176: 			    ($essayurl=~/^($LONCAPA::domain_re)\/($LONCAPA::username_re)\/(.*)$/);
1.72      www       177:                         $apath=&escape($apath);
1.32      www       178: 			$apath=~s/\W/\_/gs;
                    179: 			&Apache::lonnet::put('nohist_essay_'.$apath,
                    180: 					 { $akey => $response },$adom,$aname);
                    181: 		    }
1.42      banghart  182:                 }
1.10      ng        183: 	    }
1.42      banghart  184: 	} 
1.15      albertel  185:     } elsif ($target eq 'edit') {
                    186: 	$result.=&Apache::edit::end_table();
1.71      albertel  187: 
                    188:     } elsif ($target eq 'tex'
                    189: 	     && $Apache::lonhomework::type eq 'exam') {
                    190: 	$result .= &Apache::inputtags::exam_score_line($target);
                    191: 
1.70      albertel  192:     } elsif ($target eq 'answer') {
1.78      albertel  193: 	$result.=&Apache::response::answer_header($$tagstack[-1]);
1.81      foxr      194: 	my $answer = &mt('Essay will be hand graded.');
1.78      albertel  195: 	$result.=&Apache::response::answer_part($$tagstack[-1],$answer,
                    196: 						{'no_verbatim' => 1});
                    197: 	$result.=&Apache::response::answer_footer($$tagstack[-1]);
1.10      ng        198:     }
1.82      albertel  199:     if ($target eq 'web') {
                    200: 	&Apache::response::setup_prior_tries_hash(\&format_prior_response,
                    201: 						  ['portfiles',
                    202: 						   'uploadedurl']);
                    203:     }
1.71      albertel  204: 
1.27      albertel  205:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
                    206: 	$target eq 'tex' || $target eq 'analyze') {
1.86      foxr      207: 	&Apache::lonxml::increment_counter($increment, "$part.$id");
1.85      foxr      208: 
                    209: 	if ($target eq 'analyze') {
1.87      raeburn   210:             $Apache::lonhomework::analyze{"$part.$id.type"} = 'essayresponse';
1.85      foxr      211: 	    &Apache::lonhomework::set_bubble_lines();
                    212: 	}
1.27      albertel  213:     }
1.10      ng        214:     &Apache::response::end_response;
1.42      banghart  215: 
1.15      albertel  216:     return $result;
1.10      ng        217: }
                    218: 
1.82      albertel  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: 
1.61      albertel  249: sub file_submission {
                    250:     my ($part,$id,$which,$award,$uploadedflag)=@_;
                    251:     my $files;
1.67      albertel  252:     my $jspart=$part;
                    253:     $jspart=~s/\./_/g;
                    254:     if ($which eq 'portfiles') { $files= $env{'form.HWPORT'.$jspart.'_'.$id}; }
1.61      albertel  255:     if ($which eq 'filename') {
1.67      albertel  256: 	$files = $env{'form.HWFILE'.$jspart.'_'.$id.'.filename'};
1.61      albertel  257:     }
                    258:     
                    259:     if ($files =~ /[^\s]/) {
                    260: 	$files =~s/,$//;
                    261: 	$Apache::lonhomework::results{"resource.$part.$id.$which"}=$files;
1.68      albertel  262: 	
                    263: 	my @submitted_files = ($files);
                    264: 	if ( $which eq 'portfiles' ) {
1.73      albertel  265: 	    @submitted_files = split(/\s*,\s*/,$files);
1.68      albertel  266: 	}
                    267: 
1.61      albertel  268: 	my $uploadedfiletypes= &Apache::lonnet::EXT("resource.$part".'_'."$id.uploadedfiletypes");
1.65      albertel  269: 	if ($uploadedfiletypes) {
                    270: 	    $uploadedfiletypes=~s/[^\w\,]//g;
                    271: 	    $uploadedfiletypes=','.$uploadedfiletypes.',';
                    272: 	    foreach my $file (@submitted_files) {
                    273: 		my ($extension)=($file=~/\.(\w+)$/);
1.84      albertel  274: 		unless ($uploadedfiletypes=~/\,\s*\Q$extension\E\s*\,/i) {
1.65      albertel  275: 		    $$award='INVALID_FILETYPE';
                    276: 		}
1.61      albertel  277: 	    }
                    278: 	}
1.63      albertel  279: 	if ($$award ne 'INVALID_FILETYPE' && ref($uploadedflag)) {
1.61      albertel  280: 	    $$uploadedflag=1;
                    281: 	}
1.63      albertel  282: 	if ($$award ne 'INVALID_FILETYPE' && $which eq 'portfiles') {
1.77      albertel  283: 	    my ($symb,$crsid,$domain,$name)=&Apache::lonnet::whichuser();
1.66      albertel  284: 	    &Apache::lonnet::unmark_as_readonly($domain,$name,[$symb,$crsid]);
                    285: 	    &Apache::lonnet::mark_as_readonly($domain,$name,\@submitted_files,[$symb,$crsid]);
1.61      albertel  286: 	    &Apache::lonnet::clear_selected_files($name);
                    287: 	}
1.63      albertel  288: 	if ($$award ne 'INVALID_FILETYPE' && $which eq 'filename') {
1.61      albertel  289: 	    $Apache::lonhomework::results{"resource.$part.$id.uploadedfile"}=
                    290: 		$files;
                    291: 	    $Apache::lonhomework::results{"resource.$part.$id.uploadedurl"}=
1.67      albertel  292: 		&Apache::lonnet::userfileupload('HWFILE'.$jspart.'_'.$id,undef,
1.61      albertel  293: 						'essayresponse');
                    294: 	}
                    295:     } elsif ($which eq 'portfiles' &&
                    296: 	     $Apache::lonhomework::history{"resource.$part.$id.$which"}) {
1.77      albertel  297: 	my ($symb,$crsid,$domain,$name)=&Apache::lonnet::whichuser();
1.66      albertel  298: 	&Apache::lonnet::unmark_as_readonly($domain,$name,[$symb,$crsid]);
1.61      albertel  299: 	$Apache::lonhomework::results{"resource.$part.$id.$which"}="";
                    300:     }
                    301: }
                    302: 
1.10      ng        303: sub check_collaborators {
1.11      ng        304:     my ($ncol,$coll) = @_;
1.10      ng        305:     my %classlist=&Apache::lonnet::dump('classlist',
1.62      albertel  306: 					$env{'course.'.$env{'request.course.id'}.'.domain'},
                    307: 					$env{'course.'.$env{'request.course.id'}.'.num'});
1.10      ng        308:     my (@badcollaborators,$result);
1.88      raeburn   309:   
                    310:     my (@collaborators) = split(/,?\s+/,$coll);
                    311:     foreach my $entry (@collaborators) {
                    312:         my $collaborator;
                    313: 	if ($entry =~ /:/) {
                    314: 	    $collaborator = $entry;
1.10      ng        315: 	} else {
1.88      raeburn   316: 	    $collaborator = $entry.':'.$env{'user.domain'};
1.10      ng        317: 	}
1.88      raeburn   318:         if ($collaborator !~ /^$match_username:$match_domain$/) {
                    319:             if (!grep(/^\Q$entry\E$/,@badcollaborators)) {
                    320: 	        push(@badcollaborators,$entry);
                    321:             }
                    322:         } elsif (!grep(/^\Q$collaborator\E$/i,keys(%classlist))) {
                    323:             if (!grep(/^\Q$entry\E$/,@badcollaborators)) {
                    324:                 push(@badcollaborators,$entry);
                    325:             }
                    326:         }
1.10      ng        327:     }
                    328:     
1.88      raeburn   329:     my $numbad = scalar(@badcollaborators);
                    330:     if ($numbad) {
                    331: 	$result = '<table border="0"><tr bgcolor="#ffbbbb"><td>';
                    332:         if ($numbad == 1) {
                    333:             $result .= &mt('The following user is invalid:');
                    334:         } else {
                    335:             $result .= &mt('The following [_1] users are invalid:',$numbad);
                    336:         }
                    337: 	$result .= ' '.join(', ',@badcollaborators).'. '.&mt('Please correct.').
                    338:                    '</td></tr></table>';
1.10      ng        339:     }
                    340:     my $toomany = scalar(@collaborators) - $ncol;
                    341:     if ($toomany > 0) {
                    342: 	$result .= '<table border="0"><tr bgcolor="#ffbbbb"><td>'.
1.88      raeburn   343: 	           &mt('You have too many collaborators.').' '.
                    344:                    &mt('Please remove [quant,_1,collaborator].',$toomany).
                    345: 	           '</td></tr></table>';
1.10      ng        346:     }
                    347:     return $result;
1.1       albertel  348: }
1.2       albertel  349: 
                    350: 1;
                    351: __END__

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