File:  [LON-CAPA] / loncom / homework / essayresponse.pm
Revision 1.121: download - view: text, annotated - select for diffs
Sat Dec 6 17:46:40 2014 UTC (9 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Move some code in start_essayresponse() and end_essayresponse to
  subroutines within essayresponse.pm to facilitate reuse by
  externalresponse.pm
Work in progress.

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

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