--- loncom/homework/essayresponse.pm	2010/08/28 22:25:07	1.111
+++ loncom/homework/essayresponse.pm	2014/01/21 14:38:55	1.118
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # essay (ungraded) style responses
 #
-# $Id: essayresponse.pm,v 1.111 2010/08/28 22:25:07 raeburn Exp $
+# $Id: essayresponse.pm,v 1.118 2014/01/21 14:38:55 kruse Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -58,22 +58,18 @@ sub start_essayresponse {
         if (!defined($maxfilesize)) {
             $maxfilesize = 10.0; #FIXME This should become a domain configuration 
         }
+        my $hiddendraft;
 	if (($Apache::lonhomework::type eq 'survey') ||
             ($Apache::lonhomework::type eq 'surveycred') ||
             ($Apache::lonhomework::type eq 'anonsurvey') ||
             ($Apache::lonhomework::type eq 'anonsurveycred')) {
-	    $result.= '<input type="hidden" name="HWDRAFT'.$part.'_'.$id.'" value="yes" />';
-	}
-        my $status_text = &mt('Submission type');
-        if ($Apache::lonhomework::history{"resource.$part.award"} eq 'DRAFT') {
-            $status_text .= '<br />'.&mt('(Currently -- draft)');
-        }
-        $result.= '<p>'.&Apache::lonhtmlcommon::start_pick_box().
-                  &Apache::lonhtmlcommon::row_title($status_text);
-	if (($Apache::lonhomework::type ne 'survey') &&
-            ($Apache::lonhomework::type ne 'surveycred') &&
-            ($Apache::lonhomework::type ne 'anonsurvey') &&
-            ($Apache::lonhomework::type ne 'anonsurveycred')) {
+            $hiddendraft = '<input type="hidden" name="HWDRAFT'.$part.'_'.$id.'" value="yes" />';
+        } else {
+            my $status_text = &mt('Submission type');
+            if ($Apache::lonhomework::history{"resource.$part.award"} eq 'DRAFT') {
+                $status_text .= '<br />'.&mt('(Currently -- draft)');
+            }
+            $result = &Apache::lonhtmlcommon::row_title($status_text);
             my $closure;
             unless ($ncol || $uploadedfiletypes) {
                 $closure = 1;
@@ -95,7 +91,7 @@ sub start_essayresponse {
                       '<label>'.
 		      &mt('Collaborators:').' <input type="text" size="70" max="80" name="HWCOL'.
 		      $part.'_'.$id.'" value="'.$coll.'" /><br />'.
-		      &mt('(Enter a maximum of [quant,_1,collaborator] using username or username:domain, e.g. smithje or smithje:[_2].)',$ncol,$env{'user.domain'});
+		      &mt('Enter a maximum of [quant,_1,collaborator] using username or username:domain, e.g. smithje or smithje:[_2].',$ncol,$env{'user.domain'});
             if ($ncol > 1) {
                 $result .= '<br />'.&mt('If entering more than one, use spaces to separate the collaborators.');
             }
@@ -111,7 +107,15 @@ sub start_essayresponse {
         }
 	$result.=&Apache::inputtags::file_selector($part,$id,$uploadedfiletypes,
 						   $filesfrom,undef,$maxfilesize);
-        $result.=&Apache::lonhtmlcommon::end_pick_box().'</p>';
+        if ($result) {
+            $result =
+                  '<div>'.$hiddendraft.
+                  &Apache::lonhtmlcommon::start_pick_box().
+                  $result.
+                  &Apache::lonhtmlcommon::end_pick_box().'</div>';
+        } else {
+            $result = $hiddendraft;
+        }
     } elsif ($target eq 'web' &&
 	     $Apache::inputtags::status[-1] ne 'CAN_ANSWER') {
 	my $part= $Apache::inputtags::part;
@@ -275,7 +279,7 @@ sub format_prior_response {
     }
     if ($answer =~ /\S/) {
 	$output.='<p>'.&mt('Submitted text').
-	    '<blockquote>'.$answer.'</blockquote></p>';
+	    '<blockquote>'.&HTML::Entities::encode($answer, '"<>&').'</blockquote></p>';
     }
 
     return '<div class="LC_prior_essay">'.$output.'</div>';
@@ -308,6 +312,8 @@ sub file_submission {
                         $port_delete{$file} = 1;
                     } elsif ($file =~ m{^/uploaded/\Q$udom\E/\Q$uname\E/essayresponse/\Q$cdom\E/\Q$cnum\E/}) {
                         $uploaded_delete = $file; 
+                    } elsif ($file =~ m{^/uploaded/\Q$udom\E/\Q$uname\E/essayresponse/[^/]+$}) {
+                        $uploaded_delete = $file;
                     }
                 }
             }
@@ -318,8 +324,12 @@ sub file_submission {
             }
         }
         if ($uploaded_delete) {
-            $Apache::lonhomework::results{"resource.$part.$id.uploadedfile"} = "";
-            $Apache::lonhomework::results{"resource.$part.$id.uploadedurl"} = "";
+            if ($uploaded_delete eq $uploadedurl) {
+                $Apache::lonhomework::results{"resource.$part.$id.uploadedfile"} = "";
+                $Apache::lonhomework::results{"resource.$part.$id.uploadedurl"} = "";
+            } else {
+                undef($uploaded_delete);
+            }
         }
     }
     if ($env{'form.HWPORT'.$jspart.'_'.$id} ne '') {
@@ -387,8 +397,11 @@ sub file_submission {
                 my ($path,$filename) = ($file =~ m{^(.*/)([^/]+)$});
                 my $fullpath = '/userfiles/portfolio'.$path;
                 if (!exists($dirlist{$fullpath})) {
-                    my @list = &Apache::lonnet::dirlist($fullpath,$udom,$uname,1);
-                    $dirlist{$fullpath} = \@list;
+                    my ($listref,$listerror) =
+                        &Apache::lonnet::dirlist($fullpath,$udom,$uname,1);
+                    if (ref($listref) eq 'ARRAY') {
+                        $dirlist{$fullpath} = $listref;
+                    }
                 }
                 if (ref($dirlist{$fullpath}) eq 'ARRAY') {
                     foreach my $dir_line (@{$dirlist{$fullpath}}) {
@@ -431,81 +444,58 @@ sub file_submission {
         }
     }
     if ($accepted_upload ne '') {
-        my ($map,$resid,$resurl)=&Apache::lonnet::decode_symb($symb);
-        my $turnindir;
-        my %userhash = &Apache::lonnet::userenvironment($udom,$uname,'turnindir');
-        $turnindir = $userhash{'turnindir'};
-        if ($turnindir eq '') {
-            $turnindir = &mt('turned in');
-            $turnindir =~ s/\W+/_/g;
-            my %newhash = (
-                            'turnindir' => $turnindir,
-                          );
-            &Apache::lonnet::put('environment',\%newhash,$udom,$uname);
-        }
-        my $prefix = 'portfolio';
-        my $path = '/'.$turnindir.'/';
-        my $restitle=&Apache::lonnet::gettitle($symb);
-        $restitle =~ s/\W+/_/g;
-        if ($restitle eq '') {
-            $restitle = ($resurl =~ m{/[^/]+$});
-            if ($restitle eq '') {
-                $restitle = time;
-            }
-        }
-        my @pathitems;
-        my $navmap = Apache::lonnavmaps::navmap->new();
-        if (defined($navmap)) {
-            my $mapres = $navmap->getResourceByUrl($map);
-            if (ref($mapres)) {
-                my $pcslist = $mapres->map_hierarchy();
-                if ($pcslist ne '') {
-                    foreach my $pc (split(/,/,$pcslist)) {
-                        my $res = $navmap->getByMapPc($pc);
-                        if (ref($res)) {
-                            my $title = $res->compTitle();
-                            $title =~ s/\W+/_/g;
-                            if ($title ne '') {
-                                push(@pathitems,$title);
-                            }
+        my ($path,$multiresp) = 
+            &Apache::loncommon::get_turnedin_filepath($symb,$uname,$udom,
+                                                      'submission');
+        if ($path eq '') {
+            $$award = 'INTERNAL_ERROR';
+        } else {
+            if ($multiresp) {
+                $path .= '/'.$jspart.'_'.$id;
+            }
+            my $prefix = 'portfolio';
+            my $formelement = 'HWFILE'.$jspart.'_'.$id;
+            my $fname = &Apache::lonnet::clean_filename($env{'form.'.$formelement.'.filename'});
+            my $url = '/uploaded/'.$udom.'/'.$uname.'/'.$prefix.$path.'/'.$fname;
+            my @stat = &Apache::lonnet::stat_file($url);
+            my $conflicts = 0;
+            if (@stat && $stat[0] ne 'no_such_dir') {
+                my $current_permissions = 
+                    &Apache::lonnet::get_portfile_permissions($udom,$uname);
+                if (ref($current_permissions) eq 'HASH') {
+                    if (ref($current_permissions->{$path.'/'.$fname}) eq 'ARRAY') {
+                        foreach my $record (@{$current_permissions->{$path.'/'.$fname}}) {
+                            if (ref($record) eq 'ARRAY') {
+                                next if (($record->[0] eq $symb) && 
+                                         ($record->[1] eq $crsid));
+                                $conflicts ++;
+                            } 
                         }
                     }
                 }
-                my $maptitle = $mapres->compTitle();
-                $maptitle =~ s/\W+/_/g;
-                if ($maptitle ne '') {
-                    push(@pathitems,$maptitle);
+                if ($conflicts) {
+                    $$award = 'FILENAME_INUSE';
                 }
-            } else {
-                $$award = 'INTERNAL_ERROR';
             }
-        } else {
-            $$award = 'INTERNAL_ERROR';
-        }
-        push(@pathitems,$restitle);
-        $path .= join('/',@pathitems);
-        my $formelement = 'HWFILE'.$jspart.'_'.$id;
-        my $fname = &Apache::lonnet::clean_filename($env{'form.'.$formelement.'.filename'});
-        my $url = '/uploaded/'.$udom.'/'.$uname.'/'.$prefix.$path.'/'.$fname;
-        my @stat = &Apache::lonnet::stat_file($url);
-        if (@stat && $stat[0] ne 'no_such_dir') {
-            $$award = 'FILENAME_INUSE';
-        } else {
-            my ($mode,%allfiles,%codebase);
-            my $result = &Apache::lonnet::userfileupload($formelement,'',
+            unless ($conflicts) { 
+                my ($mode,%allfiles,%codebase);
+                my $result = &Apache::lonnet::userfileupload($formelement,'',
                              $prefix.$path,$mode,\%allfiles,\%codebase);
-            if ($result =~ m{^/uploaded/}) {
-                $stored_upload = $path.'/'.$fname;
-                $Apache::lonhomework::results{"resource.$part.$id.portfiles"} = $stored_upload;
-                push(@tolock,$stored_upload);
-            } else {
-                $$award = 'INTERNAL_ERROR';
+                if ($result =~ m{^/uploaded/}) {
+                    $stored_upload = $path.'/'.$fname;
+                    unless (grep(/^\Q$stored_upload\E$/,@accepted_portfiles)) {
+                        $Apache::lonhomework::results{"resource.$part.$id.portfiles"} = $stored_upload;
+                        push(@tolock,$stored_upload);
+                    }
+                } else {
+                    $$award = 'INTERNAL_ERROR';
+                }
             }
         }
         delete($env{'form.HWFILE'.$jspart.'_'.$id});
     }
     if (@accepted_portfiles) {
-        if ($stored_upload) {
+        if ($Apache::lonhomework::results{"resource.$part.$id.portfiles"}) {
             $Apache::lonhomework::results{"resource.$part.$id.portfiles"} .= ',';
         }
         $Apache::lonhomework::results{"resource.$part.$id.portfiles"}.=join(',',@accepted_portfiles);
@@ -516,7 +506,9 @@ sub file_submission {
             $Apache::lonhomework::results{"resource.$part.$id.portfiles"} = "";
         }
     }
-    if ($Apache::lonhomework::history{"resource.$part.$id.portfiles"} ne         $Apache::lonhomework::results{"resource.$part.$id.portfiles"}) {
+    if (($Apache::lonhomework::history{"resource.$part.$id.portfiles"} ne  
+         $Apache::lonhomework::results{"resource.$part.$id.portfiles"}) ||
+        ($uploaded_delete)) {
         if (ref($uploadedflag)) {
             $$uploadedflag=1;
         }