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

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # essay (ungraded) style responses
1.5       albertel    3: #
1.120   ! raeburn     4: # $Id: essayresponse.pm,v 1.119 2014/12/04 15:41:30 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.1       albertel   29: package Apache::essayresponse;
                     30: use strict;
1.33      albertel   31: use Apache::lonxml();
1.108     raeburn    32: use Apache::lonhtmlcommon;
                     33: use Apache::loncommon;
1.62      albertel   34: use Apache::lonnet;
1.108     raeburn    35: use Apache::lonnavmaps;
1.33      albertel   36: use Apache::lonlocal;
1.88      raeburn    37: use LONCAPA qw(:DEFAULT :match);
1.72      www        38:  
1.1       albertel   39: 
1.6       harris41   40: BEGIN {
1.10      ng         41:     &Apache::lonxml::register('Apache::essayresponse',('essayresponse'));
1.1       albertel   42: }
                     43: 
                     44: sub start_essayresponse {
1.10      ng         45:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     46:     my $result;
1.14      albertel   47:     my $id = &Apache::response::start_response($parstack,$safeeval);
                     48:     if ($target eq 'meta') {
                     49: 	$result=&Apache::response::meta_package_write('essayresponse');
1.69      albertel   50:     } elsif ($target eq 'web' &&
                     51: 	     $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
1.10      ng         52: 	my $part= $Apache::inputtags::part;
                     53: 	my $ncol= &Apache::lonnet::EXT("resource.$part".'_'."$id.maxcollaborators");
1.34      albertel   54: 	my $coll= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.collaborators"},'<>&"');
1.17      www        55: 	my $uploadedfiletypes= &Apache::lonnet::EXT("resource.$part".'_'."$id.uploadedfiletypes");
1.21      www        56:         $uploadedfiletypes=~s/[^\w\,]//g;
1.93      raeburn    57:         my $maxfilesize=&Apache::lonnet::EXT("resource.$part".'_'."$id.maxfilesize");
                     58:         if (!defined($maxfilesize)) {
                     59:             $maxfilesize = 10.0; #FIXME This should become a domain configuration 
                     60:         }
1.114     raeburn    61:         my $hiddendraft;
1.102     raeburn    62: 	if (($Apache::lonhomework::type eq 'survey') ||
                     63:             ($Apache::lonhomework::type eq 'surveycred') ||
                     64:             ($Apache::lonhomework::type eq 'anonsurvey') ||
                     65:             ($Apache::lonhomework::type eq 'anonsurveycred')) {
1.114     raeburn    66:             $hiddendraft = '<input type="hidden" name="HWDRAFT'.$part.'_'.$id.'" value="yes" />';
                     67:         } else {
                     68:             my $status_text = &mt('Submission type');
                     69:             if ($Apache::lonhomework::history{"resource.$part.award"} eq 'DRAFT') {
                     70:                 $status_text .= '<br />'.&mt('(Currently -- draft)');
                     71:             }
                     72:             $result = &Apache::lonhtmlcommon::row_title($status_text);
1.108     raeburn    73:             my $closure;
                     74:             unless ($ncol || $uploadedfiletypes) {
                     75:                 $closure = 1;
                     76:             }
                     77: 	    $result.= 
1.75      albertel   78: 		'<label>'.
1.108     raeburn    79: 		'<input type="radio" name="HWDRAFT'.$part.'_'.$id.'" value="yes" checked="checked" />&nbsp;'.
1.75      albertel   80: 		&mt('Submit entries below as answer to receive credit').
                     81: 		'</label> <br />'.
                     82: 		'<label>'.
1.108     raeburn    83: 		'<input type="radio" name="HWDRAFT'.$part.'_'.$id.'" value="no" />&nbsp;'.
1.101     riegler    84: 		&mt('Save entries below (not submitted for credit yet)').
1.75      albertel   85: 		'</label>'.
1.108     raeburn    86:                 &Apache::lonhtmlcommon::row_closure($closure);
1.75      albertel   87: 	}
                     88: 
1.10      ng         89: 	if ($ncol > 0) {
1.108     raeburn    90: 	    $result.= &Apache::lonhtmlcommon::row_title(&mt('Collaborators')).
                     91:                       '<label>'.
1.88      raeburn    92: 		      &mt('Collaborators:').' <input type="text" size="70" max="80" name="HWCOL'.
                     93: 		      $part.'_'.$id.'" value="'.$coll.'" /><br />'.
1.117     raeburn    94: 		      &mt('Enter a maximum of [quant,_1,collaborator] using username or username:domain, e.g. smithje or smithje:[_2].',$ncol,$env{'user.domain'});
1.88      raeburn    95:             if ($ncol > 1) {
                     96:                 $result .= '<br />'.&mt('If entering more than one, use spaces to separate the collaborators.');
                     97:             }
                     98:             $result .= '</label><br />';
1.10      ng         99: 	    $result .= &check_collaborators($ncol,$coll) if ($coll =~ /\w+/);
1.108     raeburn   100: 	    $result .= &Apache::lonhtmlcommon::row_closure();
1.10      ng        101: 	}
1.93      raeburn   102:         my $filesfrom = 'both';
                    103:         my $stuname = &Apache::lonnet::EXT('user.name');
                    104:         my $studom = &Apache::lonnet::EXT('user.domain');
                    105:         if (!&Apache::lonnet::usertools_access($stuname,$studom,'portfolio')) {
                    106:             $filesfrom = 'uploadonly';
                    107:         }
                    108: 	$result.=&Apache::inputtags::file_selector($part,$id,$uploadedfiletypes,
                    109: 						   $filesfrom,undef,$maxfilesize);
1.114     raeburn   110:         if ($result) {
                    111:             $result =
                    112:                   '<div>'.$hiddendraft.
                    113:                   &Apache::lonhtmlcommon::start_pick_box().
                    114:                   $result.
                    115:                   &Apache::lonhtmlcommon::end_pick_box().'</div>';
                    116:         } else {
                    117:             $result = $hiddendraft;
                    118:         }
1.74      albertel  119:     } elsif ($target eq 'web' &&
                    120: 	     $Apache::inputtags::status[-1] ne 'CAN_ANSWER') {
                    121: 	my $part= $Apache::inputtags::part;
                    122: 	my @msgs;
                    123: 	if ($Apache::lonhomework::history{"resource.$part.$id.collaborators"} =~ /\S/) {
                    124: 	    my $coll= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.collaborators"},'<>&"');
1.106     www       125: 	    $result .= '<td><i>'.&mt('Collaborated with [_1]',$coll).'</i></td>';
1.74      albertel  126: 	}
                    127: 
1.109     raeburn   128:         my $current_files_display = &Apache::inputtags::current_file_submissions($part,$id);
1.108     raeburn   129:         if ($current_files_display) {
                    130:             $result .= '<td><b>'.&mt('Submitted files:').'</b><br />'.
                    131:                        $current_files_display.'</td>';
                    132:         }
1.74      albertel  133: 
                    134: 	if ($result ne '') {
                    135: 	    $result = 
                    136: 		'<table class="LC_pastsubmission"><tr>'.$result.
                    137: 		'</tr></table>';
                    138: 	}
1.10      ng        139:     }
                    140:     return $result;
1.1       albertel  141: }
                    142: 
                    143: sub end_essayresponse {
1.10      ng        144:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.13      ng        145:     my $part          = $Apache::inputtags::part;
1.14      albertel  146:     my $id            = $Apache::inputtags::response[-1];
1.70      albertel  147:     my $increment     = &Apache::response::repetition();
1.15      albertel  148:     my $result;
1.10      ng        149:     if ( $target eq 'grade' ) {
1.106     www       150: # Deal with collaborators
                    151: 	my $collaborators = $env{'form.HWCOL'.$part.'_'.$id};
                    152: 	my $previous_list= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.collaborators"},'<>&"');
                    153: 	if ($collaborators ne $previous_list) { 
                    154: #           &Apache::lonnet::logthis("New collaborators [$collaborators] [$previous_list]");
                    155:            $Apache::lonhomework::results{"resource.$part.$id.collaborators"}=$collaborators; 
                    156:         }
                    157: # Scantron
1.58      albertel  158: 	if (  &Apache::response::submitted('scantron') ) {
1.31      albertel  159: 	    $increment=&Apache::response::scored_response($part,$id);
1.58      albertel  160: 	} elsif ( &Apache::response::submitted() ) {
1.62      albertel  161: 	    my $response      = $env{'form.HWVAL_'.$id};
1.110     raeburn   162:             my $jspart=$part;
                    163:             $jspart=~s/\./_/g;
                    164:             my $filename = $env{'form.HWFILE'.$jspart.'_'.$id.'.filename'} || 
1.91      raeburn   165:                            $env{'form.HWFILETOOBIG'.$part.'_'.$id};
1.110     raeburn   166:             my $portfiles = $env{'form.HWPORT'.$jspart.'_'.$id};
                    167:             my @deletions = &Apache::loncommon::get_env_multiple('form.HWFILE'.$jspart.'_'.$id.'_delete');
1.108     raeburn   168:             my ($is_submit,$was_draft);
                    169:             if ($env{'form.HWDRAFT'.$part.'_'.$id} eq 'yes') {
                    170:                  $is_submit = 1;
                    171:             }
                    172:             if ($Apache::lonhomework::history{"resource.$part.award"} eq 'DRAFT') {
                    173:                 $was_draft = 1;
                    174:             }
                    175: 	    if (($response =~ /[^\s]/) || ($filename =~ /[^\s]/) || ($portfiles =~ /[^\s]/) || 
                    176:                 (@deletions > 0) || ($was_draft && $is_submit)) {
1.61      albertel  177:  		my $award='DRAFT';
1.62      albertel  178:         	if ($env{'form.HWDRAFT'.$part.'_'.$id} eq 'yes') {
1.102     raeburn   179:                     if ($Apache::lonhomework::type eq 'anonsurvey') {
                    180:                         $award='ANONYMOUS';
                    181:                     } elsif ($Apache::lonhomework::type eq 'anonsurveycred') {
                    182:                         $award='ANONYMOUS_CREDIT';
                    183:                     } elsif ($Apache::lonhomework::type eq 'surveycred') {
                    184:                         $award='SUBMITTED_CREDIT';
                    185:                     } else {
                    186: 		        $award='SUBMITTED';
                    187:                     }
1.14      albertel  188: 		}
1.22      www       189:                 my $uploadedflag=0;
1.91      raeburn   190:                 my $totalsize=0;
1.108     raeburn   191:                 &file_submission($part,$id,\$award,\$uploadedflag,\$totalsize,\@deletions);
1.10      ng        192: 		$Apache::lonhomework::results{"resource.$part.$id.submission"}=$response;
1.14      albertel  193: 		$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$award;
1.10      ng        194: 		my %previous=&Apache::response::check_for_previous($response,$part,$id);
1.107     raeburn   195: 		if ($uploadedflag) {
                    196:                     if ($award eq 'FILENAME_INUSE') {
                    197:                         delete($Apache::lonhomework::results{"resource.$id.tries"});
                    198:                     }
                    199:                 } else {
                    200:                     &Apache::response::handle_previous(\%previous,$award);
                    201:                 }
1.32      www       202: #
                    203: # Store with resource author for similarity testing
                    204: #
                    205:                 if ($award eq 'SUBMITTED') {
                    206: 		    my ($symb,$crsid,$domain,$name)=
1.77      albertel  207: 			&Apache::lonnet::whichuser();
1.32      www       208: 		    if ($crsid) {
1.83      albertel  209: 			my $akey=join('.',&escape($name),&escape($domain),
                    210: 				      &escape($crsid));
1.32      www       211: 			my $essayurl=
                    212: 			    &Apache::lonnet::declutter($ENV{'REQUEST_URI'});
                    213: 			my ($adom,$aname,$apath)=
1.80      albertel  214: 			    ($essayurl=~/^($LONCAPA::domain_re)\/($LONCAPA::username_re)\/(.*)$/);
1.72      www       215:                         $apath=&escape($apath);
1.32      www       216: 			$apath=~s/\W/\_/gs;
                    217: 			&Apache::lonnet::put('nohist_essay_'.$apath,
                    218: 					 { $akey => $response },$adom,$aname);
                    219: 		    }
1.42      banghart  220:                 }
1.108     raeburn   221:             }
1.42      banghart  222: 	} 
1.15      albertel  223:     } elsif ($target eq 'edit') {
                    224: 	$result.=&Apache::edit::end_table();
1.71      albertel  225: 
                    226:     } elsif ($target eq 'tex'
                    227: 	     && $Apache::lonhomework::type eq 'exam') {
                    228: 	$result .= &Apache::inputtags::exam_score_line($target);
                    229: 
1.70      albertel  230:     } elsif ($target eq 'answer') {
1.78      albertel  231: 	$result.=&Apache::response::answer_header($$tagstack[-1]);
1.81      foxr      232: 	my $answer = &mt('Essay will be hand graded.');
1.78      albertel  233: 	$result.=&Apache::response::answer_part($$tagstack[-1],$answer,
                    234: 						{'no_verbatim' => 1});
                    235: 	$result.=&Apache::response::answer_footer($$tagstack[-1]);
1.10      ng        236:     }
1.82      albertel  237:     if ($target eq 'web') {
                    238: 	&Apache::response::setup_prior_tries_hash(\&format_prior_response,
                    239: 						  ['portfiles',
                    240: 						   'uploadedurl']);
                    241:     }
1.71      albertel  242: 
1.27      albertel  243:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
                    244: 	$target eq 'tex' || $target eq 'analyze') {
1.86      foxr      245: 	&Apache::lonxml::increment_counter($increment, "$part.$id");
1.85      foxr      246: 
                    247: 	if ($target eq 'analyze') {
1.87      raeburn   248:             $Apache::lonhomework::analyze{"$part.$id.type"} = 'essayresponse';
1.96      raeburn   249:             push (@{ $Apache::lonhomework::analyze{"parts"} },"$part.$id");
1.85      foxr      250: 	    &Apache::lonhomework::set_bubble_lines();
                    251: 	}
1.27      albertel  252:     }
1.10      ng        253:     &Apache::response::end_response;
1.42      banghart  254: 
1.15      albertel  255:     return $result;
1.10      ng        256: }
                    257: 
1.82      albertel  258: sub format_prior_response {
                    259:     my ($mode,$answer,$other_data) = @_;
                    260:     my $output;
                    261: 
                    262:     my (undef,undef,$udom,$uname) = &Apache::lonnet::whichuser();
                    263:     my $port_url = '/uploaded/'.$udom.'/'.$uname.'/portfolio/';
                    264: 
                    265:     my $file_list;
                    266: 
                    267:     foreach my $file (split(/\s*,\s*/,
                    268: 			    $other_data->[0].','.$other_data->[1])) {
                    269: 	next if ($file!~/\S/);
                    270: 	if ($file !~ m{^/uploaded/}) { $file=$port_url.$file; }
                    271: 	$file=~s|/+|/|g;
                    272: 	&Apache::lonnet::allowuploaded('/adm/essayresponse',$file);
                    273: 	$file_list.='<li><span class="LC_nobreak"><a href="'.$file.'?rawmode=1" target="lonGRDs"><img src="'.
                    274: 	    &Apache::loncommon::icon($file).'" alt="file icon" border="0" /> '.$file.
                    275: 	    '</a></span></li>'."\n";
                    276:     }
                    277:     if ($file_list) {
                    278: 	$output.= &mt('Submitted Files').'<ul>'.$file_list.'</ul>';
                    279:     }
                    280:     if ($answer =~ /\S/) {
                    281: 	$output.='<p>'.&mt('Submitted text').
1.118     kruse     282: 	    '<blockquote>'.&HTML::Entities::encode($answer, '"<>&').'</blockquote></p>';
1.82      albertel  283:     }
                    284: 
                    285:     return '<div class="LC_prior_essay">'.$output.'</div>';
                    286: }
                    287: 
1.61      albertel  288: sub file_submission {
1.119     raeburn   289:     my ($part,$id,$award,$uploadedflag,$totalsize,$deletions,$context,$info)=@_;
1.61      albertel  290:     my $files;
1.67      albertel  291:     my $jspart=$part;
                    292:     $jspart=~s/\./_/g;
1.108     raeburn   293:     my ($symb,$crsid,$udom,$uname) = &Apache::lonnet::whichuser();
                    294:     my %crsinfo = &Apache::lonnet::coursedescription($crsid);
                    295:     my $cdom = $crsinfo{'domain'};
                    296:     my $cnum = $crsinfo{'num'};
                    297:     my (@portfiles,$uploadedurl,@submitted_portfiles,$submitted_upload,
                    298:         @acceptable_portfiles,$acceptable_upload,@accepted_portfiles,
                    299:         $accepted_upload,@savedportfiles,$stored_upload,@tolock,
                    300:         %port_delete,$uploaded_delete);
                    301:     if ($Apache::lonhomework::history{"resource.$part.$id.portfiles"} ||
                    302:         $Apache::lonhomework::history{"resource.$part.$id.uploadedurl"}) {
                    303:         if ($Apache::lonhomework::history{"resource.$part.$id.portfiles"}) {
                    304:             @portfiles = split(/,/,$Apache::lonhomework::history{"resource.$part.$id.portfiles"});
                    305:         }
                    306:         $uploadedurl = $Apache::lonhomework::history{"resource.$part.$id.uploadedurl"};
                    307:         if (ref($deletions) eq 'ARRAY') {
                    308:             if (@{$deletions} > 0) {
                    309:                 foreach my $file (@{$deletions}) {
                    310:                     $file = &HTML::Entities::decode($file);
                    311:                     if (grep(/^\Q$file\E$/,@portfiles)) {
                    312:                         $port_delete{$file} = 1;
                    313:                     } elsif ($file =~ m{^/uploaded/\Q$udom\E/\Q$uname\E/essayresponse/\Q$cdom\E/\Q$cnum\E/}) {
                    314:                         $uploaded_delete = $file; 
1.112     raeburn   315:                     } elsif ($file =~ m{^/uploaded/\Q$udom\E/\Q$uname\E/essayresponse/[^/]+$}) {
                    316:                         $uploaded_delete = $file;
1.108     raeburn   317:                     }
                    318:                 }
                    319:             }
                    320:         }
                    321:         foreach my $current (@portfiles) {
                    322:             unless ($port_delete{$current}) {
                    323:                 push(@savedportfiles,$current);
                    324:             }
                    325:         }
                    326:         if ($uploaded_delete) {
1.112     raeburn   327:             if ($uploaded_delete eq $uploadedurl) {
                    328:                 $Apache::lonhomework::results{"resource.$part.$id.uploadedfile"} = "";
                    329:                 $Apache::lonhomework::results{"resource.$part.$id.uploadedurl"} = "";
                    330:             } else {
                    331:                 undef($uploaded_delete);
                    332:             }
1.108     raeburn   333:         }
                    334:     }
                    335:     if ($env{'form.HWPORT'.$jspart.'_'.$id} ne '') {
                    336:         my $newfiles= $env{'form.HWPORT'.$jspart.'_'.$id};
                    337:         $newfiles =~s/,$//;
                    338:         if ($newfiles =~ /[^\s]/) {
                    339:             foreach my $file (split(/\s*,\s*/,$newfiles)) {
                    340:                 if ($file =~ /[^\s]/) {
                    341:                     push(@submitted_portfiles,$file);
                    342:                 }
                    343:             }
                    344:         }
                    345:     }
1.110     raeburn   346:     if ($env{'form.HWFILETOOBIG'.$part.'_'.$id} ne '') {
1.108     raeburn   347:         $$award = 'EXCESS_FILESIZE';
                    348:     } elsif ($env{'form.HWFILE'.$jspart.'_'.$id.'.filename'} ne '') {
                    349:         my $newfile = $env{'form.HWFILE'.$jspart.'_'.$id.'.filename'};
                    350:         if ($newfile =~ /[^\s]/) {
                    351:             $submitted_upload = $newfile;
1.91      raeburn   352:         }
1.61      albertel  353:     }
1.108     raeburn   354:     if (@savedportfiles) {
                    355:         foreach my $file (reverse(@savedportfiles)) {
                    356:             unless(grep(/^\Q$file\E$/,@submitted_portfiles)) {
                    357:                 unshift(@submitted_portfiles,$file);
                    358:             }
1.91      raeburn   359:         }
1.108     raeburn   360:     }
                    361:     if (@submitted_portfiles || $submitted_upload) {
                    362:         my $uploadedfiletypes=
1.91      raeburn   363:             &Apache::lonnet::EXT("resource.$part".'_'."$id.uploadedfiletypes");
1.99      raeburn   364:         if ($uploadedfiletypes ne '') {
1.91      raeburn   365:             $uploadedfiletypes=~s/[^\w\,]//g;
                    366:             $uploadedfiletypes=','.$uploadedfiletypes.',';
1.108     raeburn   367:             if (@submitted_portfiles) {
                    368:                 foreach my $file (@submitted_portfiles) {
                    369:                     my ($extension)=($file=~/\.(\w+)$/);
                    370:                     if ($uploadedfiletypes=~/\,\s*\Q$extension\E\s*\,/i) {
                    371:                         push(@acceptable_portfiles,$file);
                    372:                     }
                    373:                 }
                    374:             }
                    375:             if ($submitted_upload) {
                    376:                 my ($upload_ext)=($submitted_upload=~/\.(\w+)$/);
                    377:                 if ($uploadedfiletypes=~/\,\s*\Q$upload_ext\E\s*\,/i) {
                    378:                     $acceptable_upload = $submitted_upload;
1.91      raeburn   379:                 } else {
                    380:                     $$award='INVALID_FILETYPE';
1.108     raeburn   381:                     &delete_form_items($jspart,$id);
1.91      raeburn   382:                 }
                    383:             }
1.99      raeburn   384:         } else {
1.108     raeburn   385:             @acceptable_portfiles = @submitted_portfiles;
                    386:             $acceptable_upload = $submitted_upload;
1.91      raeburn   387:         }
1.108     raeburn   388:     }
                    389:     if ((@acceptable_portfiles) || ($acceptable_upload ne '')) {
1.91      raeburn   390:         my $maxfilesize=&Apache::lonnet::EXT("resource.$part".'_'."$id.maxfilesize");
                    391:         if (!$maxfilesize) {
1.93      raeburn   392:             $maxfilesize = 10.0; #FIXME This should become a domain configuration
1.91      raeburn   393:         }
                    394:         my %dirlist;
1.108     raeburn   395:         if (@acceptable_portfiles) {
                    396:             foreach my $file (@acceptable_portfiles) {
1.97      raeburn   397:                 my ($path,$filename) = ($file =~ m{^(.*/)([^/]+)$});
1.91      raeburn   398:                 my $fullpath = '/userfiles/portfolio'.$path;
                    399:                 if (!exists($dirlist{$fullpath})) {
1.116     raeburn   400:                     my ($listref,$listerror) =
                    401:                         &Apache::lonnet::dirlist($fullpath,$udom,$uname,1);
                    402:                     if (ref($listref) eq 'ARRAY') {
                    403:                         $dirlist{$fullpath} = $listref;
                    404:                     }
1.97      raeburn   405:                 }
                    406:                 if (ref($dirlist{$fullpath}) eq 'ARRAY') {
                    407:                     foreach my $dir_line (@{$dirlist{$fullpath}}) {
1.91      raeburn   408:                         my ($fname,$dom,undef,$testdir,undef,undef,undef,undef,
1.108     raeburn   409:                             $size,undef,$mtime,undef,undef,undef,$obs,undef) =
1.91      raeburn   410:                         split(/\&/,$dir_line,16);
                    411:                         if ($filename eq $fname) {
                    412:                             my $mbsize = $size/(1024.0*1024.0);
                    413:                             if (ref($totalsize)) {
                    414:                                 $$totalsize += $mbsize;
                    415:                             }
                    416:                             last;
                    417:                         }
                    418:                     }
                    419:                 }
1.108     raeburn   420:                 if (ref($totalsize)) {
                    421:                     if ($$totalsize > $maxfilesize) {
                    422:                         $$award='EXCESS_FILESIZE';
                    423:                         &delete_form_items($jspart,$id);
                    424:                     } else {
                    425:                         push(@accepted_portfiles,$file);
                    426:                     }
                    427:                 } else {
                    428:                     push(@accepted_portfiles,$file);
                    429:                 }
1.91      raeburn   430:             }
1.108     raeburn   431:         }
                    432:         if ($acceptable_upload ne '') {
1.91      raeburn   433:             if (ref($totalsize)) {
1.108     raeburn   434:                 $$totalsize += $env{'form.HWFILESIZE'.$jspart.'_'.$id};
1.91      raeburn   435:                 if ($$totalsize > $maxfilesize) {
                    436:                     $$award='EXCESS_FILESIZE';
1.108     raeburn   437:                     delete($env{'form.HWFILE'.$jspart.'_'.$id});
1.94      raeburn   438:                 } else {
1.108     raeburn   439:                     $accepted_upload = $acceptable_upload;
1.91      raeburn   440:                 }
                    441:             } else {
1.108     raeburn   442:                 $accepted_upload = $acceptable_upload;
1.91      raeburn   443:             }
                    444:         }
1.108     raeburn   445:     }
                    446:     if ($accepted_upload ne '') {
1.115     raeburn   447:         my ($path,$multiresp) = 
                    448:             &Apache::loncommon::get_turnedin_filepath($symb,$uname,$udom,
                    449:                                                       'submission');
                    450:         if ($path eq '') {
                    451:             $$award = 'INTERNAL_ERROR';
                    452:         } else {
                    453:             if ($multiresp) {
                    454:                 $path .= '/'.$jspart.'_'.$id;
                    455:             }
                    456:             my $prefix = 'portfolio';
                    457:             my $formelement = 'HWFILE'.$jspart.'_'.$id;
                    458:             my $fname = &Apache::lonnet::clean_filename($env{'form.'.$formelement.'.filename'});
                    459:             my $url = '/uploaded/'.$udom.'/'.$uname.'/'.$prefix.$path.'/'.$fname;
                    460:             my @stat = &Apache::lonnet::stat_file($url);
                    461:             my $conflicts = 0;
                    462:             if (@stat && $stat[0] ne 'no_such_dir') {
                    463:                 my $current_permissions = 
                    464:                     &Apache::lonnet::get_portfile_permissions($udom,$uname);
                    465:                 if (ref($current_permissions) eq 'HASH') {
                    466:                     if (ref($current_permissions->{$path.'/'.$fname}) eq 'ARRAY') {
                    467:                         foreach my $record (@{$current_permissions->{$path.'/'.$fname}}) {
                    468:                             if (ref($record) eq 'ARRAY') {
                    469:                                 next if (($record->[0] eq $symb) && 
                    470:                                          ($record->[1] eq $crsid));
                    471:                                 $conflicts ++;
                    472:                             } 
1.107     raeburn   473:                         }
                    474:                     }
1.108     raeburn   475:                 }
1.115     raeburn   476:                 if ($conflicts) {
                    477:                     $$award = 'FILENAME_INUSE';
1.107     raeburn   478:                 }
1.95      raeburn   479:             }
1.115     raeburn   480:             unless ($conflicts) { 
                    481:                 my ($mode,%allfiles,%codebase);
                    482:                 my $result = &Apache::lonnet::userfileupload($formelement,'',
1.108     raeburn   483:                              $prefix.$path,$mode,\%allfiles,\%codebase);
1.115     raeburn   484:                 if ($result =~ m{^/uploaded/}) {
                    485:                     $stored_upload = $path.'/'.$fname;
                    486:                     unless (grep(/^\Q$stored_upload\E$/,@accepted_portfiles)) {
                    487:                         $Apache::lonhomework::results{"resource.$part.$id.portfiles"} = $stored_upload;
                    488:                         push(@tolock,$stored_upload);
                    489:                     }
                    490:                 } else {
                    491:                     $$award = 'INTERNAL_ERROR';
                    492:                 }
1.95      raeburn   493:             }
1.108     raeburn   494:         }
                    495:         delete($env{'form.HWFILE'.$jspart.'_'.$id});
                    496:     }
                    497:     if (@accepted_portfiles) {
1.115     raeburn   498:         if ($Apache::lonhomework::results{"resource.$part.$id.portfiles"}) {
1.108     raeburn   499:             $Apache::lonhomework::results{"resource.$part.$id.portfiles"} .= ',';
                    500:         }
                    501:         $Apache::lonhomework::results{"resource.$part.$id.portfiles"}.=join(',',@accepted_portfiles);
                    502:         push(@tolock,@accepted_portfiles);
                    503:     }
                    504:     if (!defined($Apache::lonhomework::results{"resource.$part.$id.portfiles"})) {
                    505:         if (keys(%port_delete) > 0) {
                    506:             $Apache::lonhomework::results{"resource.$part.$id.portfiles"} = "";
                    507:         }
                    508:     }
1.112     raeburn   509:     if (($Apache::lonhomework::history{"resource.$part.$id.portfiles"} ne  
                    510:          $Apache::lonhomework::results{"resource.$part.$id.portfiles"}) ||
                    511:         ($uploaded_delete)) {
1.108     raeburn   512:         if (ref($uploadedflag)) {
                    513:             $$uploadedflag=1;
                    514:         }
1.61      albertel  515:     }
1.119     raeburn   516:     if ($context eq 'externalresponse') {
1.120   ! raeburn   517:         my @todelete = map { "/uploaded/$udom/$uname/portfolio".$_;  } keys(%port_delete);
1.119     raeburn   518:         if (@tolock || @todelete) {
                    519:             if (ref($info) eq 'HASH') {
                    520:                 if ($info->{'ip'}) {
1.120   ! raeburn   521:                     my @adds;
        !           522:                     if (@tolock) {
        !           523:                         @adds = map { "/uploaded/$udom/$uname/portfolio".$_;  } @tolock;
        !           524:                     }
        !           525:                     &Apache::lonnet::automated_portfile_access('ip',\@adds,\@todelete,$info);
1.119     raeburn   526:                 }
                    527:             }
                    528:         }
                    529:     }
1.108     raeburn   530:     &Apache::lonnet::unmark_as_readonly($udom,$uname,[$symb,$crsid]);
                    531:     &Apache::lonnet::mark_as_readonly($udom,$uname,[@tolock],[$symb,$crsid]);
                    532:     &Apache::lonnet::clear_selected_files($uname);
                    533:     return;
1.61      albertel  534: }
                    535: 
1.91      raeburn   536: sub delete_form_items {
                    537:     my ($jspart,$id) = @_;
1.98      raeburn   538:     delete($env{'form.HWFILE'.$jspart.'_'.$id.'.filename'});
                    539:     delete($env{'form.HWFILE'.$jspart.'_'.$id.'.mimetype'});
                    540:     delete($env{'form.HWFILE'.$jspart.'_'.$id});
1.91      raeburn   541: }
                    542: 
1.10      ng        543: sub check_collaborators {
1.11      ng        544:     my ($ncol,$coll) = @_;
1.10      ng        545:     my %classlist=&Apache::lonnet::dump('classlist',
1.62      albertel  546: 					$env{'course.'.$env{'request.course.id'}.'.domain'},
                    547: 					$env{'course.'.$env{'request.course.id'}.'.num'});
1.10      ng        548:     my (@badcollaborators,$result);
1.88      raeburn   549:   
1.106     www       550:     my (@collaborators) = split(/[,;\s]+/,$coll);
1.88      raeburn   551:     foreach my $entry (@collaborators) {
                    552:         my $collaborator;
                    553: 	if ($entry =~ /:/) {
                    554: 	    $collaborator = $entry;
1.10      ng        555: 	} else {
1.88      raeburn   556: 	    $collaborator = $entry.':'.$env{'user.domain'};
1.10      ng        557: 	}
1.88      raeburn   558:         if ($collaborator !~ /^$match_username:$match_domain$/) {
                    559:             if (!grep(/^\Q$entry\E$/,@badcollaborators)) {
                    560: 	        push(@badcollaborators,$entry);
                    561:             }
                    562:         } elsif (!grep(/^\Q$collaborator\E$/i,keys(%classlist))) {
                    563:             if (!grep(/^\Q$entry\E$/,@badcollaborators)) {
                    564:                 push(@badcollaborators,$entry);
                    565:             }
                    566:         }
1.10      ng        567:     }
                    568:     
1.88      raeburn   569:     my $numbad = scalar(@badcollaborators);
                    570:     if ($numbad) {
                    571: 	$result = '<table border="0"><tr bgcolor="#ffbbbb"><td>';
                    572:         if ($numbad == 1) {
                    573:             $result .= &mt('The following user is invalid:');
                    574:         } else {
                    575:             $result .= &mt('The following [_1] users are invalid:',$numbad);
                    576:         }
                    577: 	$result .= ' '.join(', ',@badcollaborators).'. '.&mt('Please correct.').
                    578:                    '</td></tr></table>';
1.10      ng        579:     }
                    580:     my $toomany = scalar(@collaborators) - $ncol;
                    581:     if ($toomany > 0) {
                    582: 	$result .= '<table border="0"><tr bgcolor="#ffbbbb"><td>'.
1.88      raeburn   583: 	           &mt('You have too many collaborators.').' '.
                    584:                    &mt('Please remove [quant,_1,collaborator].',$toomany).
                    585: 	           '</td></tr></table>';
1.10      ng        586:     }
                    587:     return $result;
1.1       albertel  588: }
1.2       albertel  589: 
                    590: 1;
                    591: __END__
1.92      jms       592: 
                    593: 
                    594: =pod
                    595: 
                    596: =head1 NAME
                    597: 
1.106     www       598: Apache::essayresponse
1.92      jms       599: 
                    600: =head1 SYNOPSIS
                    601: 
                    602: Handler to evaluate essay (ungraded) style responses.
                    603: 
                    604: This is part of the LearningOnline Network with CAPA project
                    605: described at http://www.lon-capa.org.
                    606: 
                    607: =head1 SUBROUTINES
                    608: 
                    609: =over 
                    610: 
                    611: =item start_essayresponse()
                    612: 
                    613: =item end_essayresponse()
                    614: 
                    615: =item format_prior_response()
                    616: 
                    617: =item file_submission()
                    618: 
                    619: =item delete_form_items()
                    620: 
                    621: =item check_collaborators()
                    622: 
                    623: =back
                    624: 
1.93      raeburn   625: =cut

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