[LON-CAPA-cvs] cvs: loncom(version_1_1_X) /homework/caparesponse caparesponse.pm

albertel lon-capa-cvs@mail.lon-capa.org
Thu, 19 Feb 2004 19:24:38 -0000


albertel		Thu Feb 19 14:24:38 2004 EDT

  Modified files:              (Branch: version_1_1_X)
    /loncom/homework/caparesponse	caparesponse.pm 
  Log:
  - backport 1.128 and 1.130
  
  
Index: loncom/homework/caparesponse/caparesponse.pm
diff -u loncom/homework/caparesponse/caparesponse.pm:1.126 loncom/homework/caparesponse/caparesponse.pm:1.126.2.1
--- loncom/homework/caparesponse/caparesponse.pm:1.126	Thu Feb  5 13:41:06 2004
+++ loncom/homework/caparesponse/caparesponse.pm	Thu Feb 19 14:24:38 2004
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # caparesponse definition
 #
-# $Id: caparesponse.pm,v 1.126 2004/02/05 18:41:06 albertel Exp $
+# $Id: caparesponse.pm,v 1.126.2.1 2004/02/19 19:24:38 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -254,18 +254,18 @@
 		}
 		if ($tag eq 'numericalresponse') {
 		    my ($celllength,$number_of_tables,@table_range)=
-			&get_table_sizes($formats[0],$number_of_bubbles);
+			&get_table_sizes($number_of_bubbles,\@bubble_values);
 		    my $j=0;
 		    my $cou=0;
 		    $result.='\vskip -1 mm \noindent \begin{enumerate}\item[\textbf{'.$Apache::lonxml::counter.'}.]';
 		    for (my $i=0;$i<$number_of_tables;$i++) {
 			$result.='\vskip -1 mm \noindent \begin{tabular}{';
 			for (my $ind=0;$ind<$table_range[$j];$ind++) {
-			    $result.='lp{'.$celllength.' mm}';
+			    $result.='p{3 mm}p{'.$celllength.' mm}';
 			}
 			$result.='}';
 			for (my $ind=$cou;$ind<$cou+$table_range[$j];$ind++) {
-			    $result.='\hskip -3 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -2 mm & {\small '.$bubble_values[$ind].'} ';
+			    $result.='\hskip -4 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$ & \hskip -3 mm {\small '.$bubble_values[$ind].'} ';
 			    if ($ind != $cou+$table_range[$j]-1) {$result.=' & ';}
 			}
 			$cou += $table_range[$j];
@@ -376,23 +376,39 @@
 }
 
 sub get_table_sizes {
-    my ($format,$number_of_bubbles)=@_;
-    my $max_val = 0;
-    if ($format=~m/^(\d+)E([^\d]*)(\d*)$/) {
-	$max_val=$1+$2+4;
+    my ($number_of_bubbles,$rbubble_values)=@_;
+    my $scale=2; #mm for one digit
+    my $cell_width=0;
+    foreach my $member (@$rbubble_values) {
+	my $cell_width_real=0;
+	if ($member=~/(\d*)\.?(\d*)(E|e)(\+|-)?(\d*)/) {
+	    $cell_width_real=(length($1)+length($2)+length($5)+9)*$scale;
+	} elsif ($member=~/(\d*)\.?(\d*)/) {
+	    $cell_width_real=(length($1)+length($2)+2)*$scale;
+	} else {
+	    $cell_width_real=(length($member)+1)*$scale;
+	}
+	if ($cell_width_real>$cell_width) {$cell_width=$cell_width_real;}
+    }
+    $cell_width+=8;
+    my $textwidth;
+    if ($ENV{'form.textwidth'} ne '') {
+	$ENV{'form.textwidth'}=~/(\d+)/;
+	$textwidth=$1;
     } else {
-	$max_val=4;
+	$ENV{'textwidth'}=~/(\d+)/;
+	$textwidth=$1;
     }
-    $max_val = int(0.9*$ENV{'form.textwidth'}/(($max_val+6)*2));
-    my $celllength = 0.9*$ENV{'form.textwidth'}/$max_val-10;
+    my $bubbles_per_line=int($textwidth/$cell_width);
+    my $number_of_tables = int($number_of_bubbles/$bubbles_per_line);
     my @table_range = ();
-    my $number_of_tables = int($number_of_bubbles/$max_val);
-    for (my $i=0;$i<$number_of_tables;$i++) {push @table_range,$max_val;}
-    if ($number_of_bubbles % $max_val != 0) {
+    for (my $i=0;$i<$number_of_tables;$i++) {push @table_range,$bubbles_per_line;}
+    if ($number_of_bubbles % $bubbles_per_line) {
 	$number_of_tables++;
-	push @table_range,($number_of_bubbles % $max_val);
+	push @table_range,($number_of_bubbles % $bubbles_per_line);
     }
-    return ($celllength,$number_of_tables,@table_range);
+    $cell_width-=8;
+    return ($cell_width,$number_of_tables,@table_range);
 }
 
 sub format_number {