--- loncom/homework/matchresponse.pm	2012/12/29 01:21:10	1.87
+++ loncom/homework/matchresponse.pm	2025/01/27 15:04:15	1.95
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Full matching style response
 #
-# $Id: matchresponse.pm,v 1.87 2012/12/29 01:21:10 raeburn Exp $
+# $Id: matchresponse.pm,v 1.95 2025/01/27 15:04:15 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -55,7 +55,7 @@ sub start_matchresponse {
 	$result=&Apache::response::meta_package_write('matchresponse');
     } elsif ($target eq 'edit' ) {
 	$result.=&Apache::edit::start_table($token).
-	    '<tr><td>'.&Apache::lonxml::description($token).'</td>'
+	    '<tr><td>'.&Apache::loncommon::insert_folding_button().&Apache::lonxml::description($token).'</td>'
            .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
 	   .&Apache::edit::deletelist($target,$token)
            .'</span></td>'
@@ -104,15 +104,15 @@ sub start_itemgroup {
     if ($target eq 'edit') {
 	$result=&Apache::edit::tag_start($target,$token);
 	$result.=&Apache::edit::select_arg('Randomize Order:','randomize',
-					   ['yes','no'],$token).'&nbsp;'x 3;
+					   ['yes','no'],$token);
 	$result.=&Apache::edit::select_arg('Items Display Location:',
 					   'location',
 					   ['top','bottom','left','right'],
-					   $token).'&nbsp;'x 3;
+					   $token);
 	$result.=&Apache::edit::select_arg('Items Display Direction:',
 					   'direction',
 					   ['vertical','horizontal'],
-					   $token).'&nbsp;'x 3;
+					   $token);
         $result.=&Apache::edit::select_arg('Items Columns:',
                                            'columns',
                                            [['','default'],'1','2','3','4'],
@@ -170,8 +170,7 @@ sub end_itemgroup {
         undef($columns);
     }
     if ($target eq 'web') {
-	
-	my $table='<br /><table>'; # extra space to match what latex does.
+	my $table='<table class="LC_itemgroup">'; # extra space to match what latex does.
         if ((!$columns) || ($columns < 0)) {
             if ($direction eq 'horizontal') {
                 if (@names > 0) { 
@@ -262,7 +261,7 @@ sub start_item {
 	$result=&Apache::edit::tag_start($target,$token,"Item");
 	$result.=&Apache::edit::text_arg('Name:','name',$token);
 	if ($randomize ne 'no') {
-	    $result.='&nbsp;'x 3 .
+	    $result.=
                      &Apache::edit::select_arg('Location:','location',
 					       ['random','top','bottom'],
 					       $token);
@@ -319,7 +318,8 @@ sub start_foilgroup {
     &Apache::response::pushrandomnumber(undef,$target);
     if ($target eq 'edit') {
 	$result.=&Apache::edit::start_table($token)
-	    .'<tr><td>'.&mt('Collection Of Foils').'</td>'
+	    .'<tr><td>'.&Apache::loncommon::insert_folding_button()
+            .&mt('Collection Of Foils').'</td>'
             .'<td><span class="LC_nobreak">'.&mt('Delete?')
 	    .&Apache::edit::deletelist($target,$token)
 	    .'</span></td>'
@@ -580,7 +580,10 @@ sub format_prior_answer {
     my $output;
     
     foreach my $name (@{ $foil_order }) {
-	my $item=shift(@items);
+	my $item = &HTML::Entities::encode(shift(@items),'<>&"');
+        if ($item eq '') {
+            $item = '&nbsp;';
+        }
 	$output .= '<tr><td>'.$item.'</td></tr>';
     }
     return if (!defined($output));
@@ -652,9 +655,19 @@ sub displayfoils {
                 $newvariation = 1;
             }
         }
-        unless ((($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurvey') || ($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurveycred') && (defined($env{'form.grade_symb'}))) || $newvariation) {
-	    $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
+        unless ($newvariation) {
+            if ((($env{'form.grade_username'} eq '') && ($env{'form.grade_domain'} eq '')) ||
+                (($env{'form.grade_username'} eq $env{'user.name'}) &&
+                 ($env{'form.grade_domain'} eq $env{'user.domain'}))) {
+                $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
+            } else {
+                unless (($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurvey') ||
+                        ($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurveycred')) {
+                    $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
+                }
+            }
         }
+
 	my %lastresponse=&Apache::lonnet::str2hash($lastresponse);    
 	my @alphabet=('A'..'Z');
 	my @used_letters=sort(keys(%letter_name_map));
@@ -671,7 +684,7 @@ sub displayfoils {
 	    my $last_letter=$name_letter_map{$lastopt};
 	    my $optionlist = '';
 	    if ($target ne 'tex') {
-		$optionlist="<option></option>\n";
+		$optionlist="<option value=\"\"></option>\n";
 	    } else {
 		if ($Apache::lonhomework::type ne 'exam') {
                     if($env{'form.pdfFormFields'} eq 'yes'
@@ -688,7 +701,7 @@ sub displayfoils {
 	    foreach $option (@used_letters) {
 		if ($option eq $last_letter) {
 		    if ($target ne 'tex') {
-                        $optionlist.="<option selected=\"selected\">$option</option>\n";
+                        $optionlist.="<option value=\"$option\" selected=\"selected\">$option</option>\n";
                     } elsif ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
                              && $Apache::inputtags::status['-1'] eq 'CAN_ANSWER'
                              && $Apache::lonhomework::type ne 'exam') {
@@ -696,7 +709,7 @@ sub displayfoils {
                     }
 		} else {
 		    if ($target ne 'tex') {
-                        $optionlist.="<option>$option</option>\n";
+                        $optionlist.="<option value=\"$option\">$option</option>\n";
                     } elsif ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
                              && $Apache::inputtags::status['-1'] eq 'CAN_ANSWER'
                              && $Apache::lonhomework::type ne 'exam') {
@@ -705,7 +718,7 @@ sub displayfoils {
 		}
 	    }
 	    if ($target ne 'tex' && $Apache::lonhomework::type ne 'exam') {
-	        $optionlist='<select onchange="javascript:setSubmittedPart(\''.
+	        $optionlist='<label><select onchange="javascript:setSubmittedPart(\''.
 		  $part.'\');" name="HWVAL_'.
 		    $Apache::inputtags::response[-1].':'.$temp.'">'.
 		        $optionlist."</select>\n";
@@ -732,7 +745,7 @@ sub displayfoils {
 	    }
 	    if ($target ne 'tex') {
 		if ($Apache::lonhomework::type ne 'exam') {
-		    $question.="<br />\n".$optionlist.$text;
+		    $question.="<br />\n".$optionlist.$text/'</label>';
 		} else {
 		    $question.="<br />\n".$text;
 		}
@@ -740,7 +753,7 @@ sub displayfoils {
 		    my @blank;
 		    $question.=&Apache::optionresponse::webbubbles(\@used_letters,\@blank,$temp,$last_letter);
 		}
-	    } else {		
+	    } else {
 		# Note that if left or right positioned, we must
 		# confine the bubbles to righttabsize:
 		#