[LON-CAPA-cvs] cvs: loncom /homework matchresponse.pm

raeburn raeburn at source.lon-capa.org
Fri Dec 28 20:21:10 EST 2012


raeburn		Sat Dec 29 01:21:10 2012 EDT

  Modified files:              
    /loncom/homework	matchresponse.pm 
  Log:
  - Bug 6595.
    - Derived from code suggested by Christian Knieling (KIT).
    - Additional attribute: "columns", works in tandem with existing attribute:
      "direction". If columns not set, defaults to number of items (i.e.,
      generates a single row, for direction=horizontal, or 1 - single column -
      otherwise).
    - If set to an integer (e.g., 3) will label items as:
      A B C
      D E F 
      for direction=horizontal
  
      or 
  
      A C E
      B D F
      for direction=vertical
  
  
Index: loncom/homework/matchresponse.pm
diff -u loncom/homework/matchresponse.pm:1.86 loncom/homework/matchresponse.pm:1.87
--- loncom/homework/matchresponse.pm:1.86	Thu Dec 15 01:21:28 2011
+++ loncom/homework/matchresponse.pm	Sat Dec 29 01:21:10 2012
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Full matching style response
 #
-# $Id: matchresponse.pm,v 1.86 2011/12/15 01:21:28 raeburn Exp $
+# $Id: matchresponse.pm,v 1.87 2012/12/29 01:21:10 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -34,6 +34,7 @@
 use Apache::lonlocal;
 use Apache::lonnet;
 use Apache::lonxml;
+use POSIX qw(ceil);
 
 BEGIN {
     &Apache::lonxml::register('Apache::matchresponse',('matchresponse'));
@@ -111,12 +112,17 @@
 	$result.=&Apache::edit::select_arg('Items Display Direction:',
 					   'direction',
 					   ['vertical','horizontal'],
-					   $token);
+					   $token).' 'x 3;
+        $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 @@
     $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>';}
+        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 = ' ';
+            my $item = ' ';
+            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> </td>';
+            }
+            if ( ! (($i+1) % $columns) ) {
+                $table.='</tr>';
+            }
+        }
 	$table.='</table>';
 	$Apache::matchresponse::itemtable{'display'}=$table;
 	$Apache::lonxml::post_evaluate=0;




More information about the LON-CAPA-cvs mailing list