--- loncom/homework/matchresponse.pm	2011/11/08 01:33:14	1.84
+++ loncom/homework/matchresponse.pm	2016/01/22 22:42:51	1.93
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Full matching style response
 #
-# $Id: matchresponse.pm,v 1.84 2011/11/08 01:33:14 raeburn Exp $
+# $Id: matchresponse.pm,v 1.93 2016/01/22 22:42:51 damieng Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -34,6 +34,7 @@ use Apache::optionresponse();
 use Apache::lonlocal;
 use Apache::lonnet;
 use Apache::lonxml;
+use POSIX qw(ceil);
 
 BEGIN {
     &Apache::lonxml::register('Apache::matchresponse',('matchresponse'));
@@ -54,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>'
@@ -103,20 +104,25 @@ 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);
+        $result.=&Apache::edit::select_arg('Items Columns:',
+                                           'columns',
+                                           [['','default'],'1','2','3','4'],
+                                            $token);
 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
     } elsif ($target eq 'modified') {
 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
 						     $safeeval,'randomize',
-						     'location','direction');
+						     'location','direction',
+                                                     'columns');
 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
     } elsif ($target eq 'web' or $target eq 'tex') {
 	$Apache::matchresponse::itemtable{'location'}=
@@ -158,19 +164,51 @@ sub end_itemgroup {
     $Apache::response::itemgroup{'letter_name_map'}=\%letter_name_map;
     $Apache::response::itemgroup{'name_letter_map'}=\%name_letter_map;
     my $direction=&Apache::lonxml::get_param('direction',$parstack,$safeeval);
+    my $columns=&Apache::lonxml::get_param('columns',$parstack,$safeeval);
+    
+    unless ($columns =~ /^\d+$/) {
+        undef($columns);
+    }
     if ($target eq 'web') {
 	
-	my $table='<br /><table>'; # extra space to match what latex does.
-	my $i=0;
-	if ($direction eq 'horizontal') { $table .='<tr>';}
-	foreach my $name (@names) {
-	    if ($direction ne 'horizontal') { $table.='<tr>'; }
-	    $table.='<td>'.$alphabet[$i].'</td><td>'.
-		$Apache::response::itemgroup{$name.'.text'}.'</td>';
-	    if ($direction ne 'horizontal') { $table.='</tr>'; }
-	    $i++;
-	}
-	if ($direction eq 'horizontal') { $table .='</tr>';}
+	my $table='<table class="LC_itemgroup">'; # extra space to match what latex does.
+        if ((!$columns) || ($columns < 0)) {
+            if ($direction eq 'horizontal') {
+                if (@names > 0) { 
+                    $columns = scalar(@names);
+                } else {
+                    $columns = 1;
+                }
+            } else {
+                $columns = 1;
+            }
+        }
+        my $rows=ceil(scalar(@names)/$columns);
+        my $endloop = $columns*$rows;
+        for (my $i=0; $i<$endloop; $i++) {
+            my $label = '&nbsp;';
+            my $item = '&nbsp;';
+            my $index;
+            if ($direction eq 'horizontal') {
+                $index = $i;
+            } else {
+                $index = ($i % $columns)*$rows+int($i/$columns);
+            }
+            if ($index < scalar(@names)) {
+                $label = $alphabet[$index];
+                $item = $Apache::response::itemgroup{$names[$index].'.text'};
+            }
+            if ($i % $columns == 0) {
+                $table.='<tr>';
+            }
+            $table.= '<td>'.$label.'</td><td>'.$item.'</td>';
+            if ($columns > 1) {
+                $table .= '<td>&nbsp;</td>';
+            }
+            if ( ! (($i+1) % $columns) ) {
+                $table.='</tr>';
+            }
+        }
 	$table.='</table>';
 	$Apache::matchresponse::itemtable{'display'}=$table;
 	$Apache::lonxml::post_evaluate=0;
@@ -224,7 +262,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);
@@ -281,7 +319,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>'
@@ -462,7 +501,7 @@ sub grade_response {
 	}
     }
     my $part=$Apache::inputtags::part;
-    my $nonlenient=&Apache::optionresponse::is_nonlenient($part);
+    my $nonlenient=&Apache::optionresponse::grading_is_nonlenient($part);
     my $id = $Apache::inputtags::response['-1'];
     my $responsestr=&Apache::lonnet::hash2str(%responsehash);
     my $itemstr    =&Apache::lonnet::array2str(@items);
@@ -542,7 +581,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));
@@ -614,9 +656,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));