File:  [LON-CAPA] / loncom / homework / essayresponse.pm
Revision 1.125: download - view: text, annotated - select for diffs
Tue Nov 20 19:14:14 2018 UTC (5 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- "Check For Plagiarism" now supported for submissions to Simple Problem
(Essay-open ended). Similarity checking only for submissions in same course

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

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