--- loncom/homework/response.pm	2003/05/16 20:16:18	1.78
+++ loncom/homework/response.pm	2003/08/11 19:56:45	1.83
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # various response type definitons response definition
 #
-# $Id: response.pm,v 1.78 2003/05/16 20:16:18 albertel Exp $
+# $Id: response.pm,v 1.83 2003/08/11 19:56:45 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -52,7 +52,7 @@ sub start_response {
 }
 
 sub end_response {
-    pop @Apache::inputtags::response;
+    #pop @Apache::inputtags::response;
     @Apache::inputtags::inputlist=();
     return '';
 }
@@ -62,6 +62,7 @@ sub start_hintresponse {
     my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
     if ($id eq '') { $id = $Apache::lonxml::curdepth; }
     push (@Apache::inputtags::response,$id);
+    push (@Apache::inputtags::responselist,$id);
     push (@Apache::inputtags::paramstack,[%Apache::inputtags::params]);
     return $id;
 }
@@ -92,10 +93,13 @@ sub setrandomnumber {
        $rndmod+=&Apache::lonnet::numval($Apache::inputtags::response[-1]);
     }
     if ($rndseed =~/,/) {
-	my ($num1,$num2)=split(/,/,$rndseed);
-	$num1+=$rndmod;
-	$num2+=$rndmod;
-	$rndseed="$num1,$num2";
+	{
+	    use integer;
+	    my ($num1,$num2)=split(/,/,$rndseed);
+	    $num1+=$rndmod;
+	    $num2+=$rndmod;
+	    $rndseed="$num1,$num2";
+	}
     } else {
 	$rndseed+=$rndmod;
     }
@@ -274,6 +278,24 @@ sub end_dataresponse {
     return $result;
 }
 
+sub decide_package {
+    my ($tagstack)=@_;
+    my $package;
+    if ($$tagstack[-1] eq 'parameter') {
+	$package='part';
+    } else {
+	my $i=-1;
+	while (defined($$tagstack[$i])) {
+	    if ($$tagstack[$i] =~ /(response|hint)$/) {
+		$package=$$tagstack[$i];
+		last;
+	    }
+	    $i--;
+	}
+    }
+    return $package;
+}
+
 sub start_responseparam {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
     my $result='';
@@ -284,6 +306,19 @@ sub start_responseparam {
 					$token->[2]->{'description'});
     } elsif ($target eq 'edit') {
 	$result.=&Apache::edit::tag_start($target,$token);
+	my $optionlist;
+	my $package=&decide_package($tagstack);
+	foreach my $key (sort(keys(%Apache::lonnet::packagetab))) {
+	    if ($key =~ /^\Q$package\E&(.*)&display$/) {
+		$optionlist.='<option value="'.$1.'">'.
+		    $Apache::lonnet::packagetab{$key}.'</option>';
+	    }
+	}
+	if (defined($optionlist)) {
+	    $result.='Use template: <select name="'.
+		&Apache::edit::html_element_name('parameter_package').'">'.
+		    '<option value=""></option>'.$optionlist.'</select><br />';
+	}
 	$result.=&Apache::edit::text_arg('Name:','name',$token).
 	    &Apache::edit::text_arg('Type:','type',$token).
 		&Apache::edit::text_arg('Description:','description',$token).
@@ -291,9 +326,22 @@ sub start_responseparam {
 			"</td></tr>";
 	$result.=&Apache::edit::end_table;
     } elsif ($target eq 'modified') {
-	my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
-						     'name','type','description',
-						     'default');
+	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
+						     $safeeval,'name','type',
+						     'description','default');
+	my $element=&Apache::edit::html_element_name('parameter_package');
+	if (defined($ENV{"form.$element"}) && $ENV{"form.$element"} ne '') {
+	    my $name=$ENV{"form.$element"};
+	    my $tag=&decide_package($tagstack);
+	    $token->[2]->{'name'}=$name;
+	    $token->[2]->{'type'}=
+		$Apache::lonnet::packagetab{"$tag&$name&type"};
+	    $token->[2]->{'description'}=
+		$Apache::lonnet::packagetab{"$tag&$name&display"};
+	    $token->[2]->{'default'}=
+		$Apache::lonnet::packagetab{"$tag&$name&default"};
+	    $constructtag=1;
+	}
 	if ($constructtag) {
 	    $result = &Apache::edit::rebuild_tag($token);
 	    $result.=&Apache::edit::handle_insert();
@@ -367,13 +415,8 @@ sub answer_header {
 	$result = ' \vskip 0 mm \begin{tabular}{|c|}\hline Answer for Part: '.
                   $Apache::inputtags::part.' \\\\ \hline ';
     } else {
-	if ($type eq 'optionresponse' || $type eq 'radiobuttonresponse' ) {
-	    $result = '<table border="1"><tr><th>Answer for Part:'.
-		$Apache::inputtags::part. '</th></tr><tr>'."\n";
-	} else {
-	    $result = '<table border="1"><tr><td>Answer for Part:'.
-		$Apache::inputtags::part. '</td>'."\n";
-	}
+	$result = '<table border="1"><tr><td>Answer for Part:'.
+	    $Apache::inputtags::part. '</td>'."\n";
     }
     return $result;
 }
@@ -382,13 +425,9 @@ sub answer_part {
     my ($type,$answer) = @_;
     my $result;
     if ($ENV{'form.answer_output_mode'} eq 'tex') {
-	$result = ' '.$answer.'\\\\ \hline ';
+	$result = ' \verb|'.$answer.'|\\\\ \hline ';
     } else {
-	if ($type eq 'optionresponse' || $type eq 'radiobuttonresponse') {
-	    $result = '<td>'.$answer.'</td>';
-	} else {
-	    $result = '<td>'.$answer.'</td>';
-	}
+	$result = '<td>'.$answer.'</td>';
     }
     return $result;
 }
@@ -399,11 +438,7 @@ sub answer_footer {
     if ($ENV{'form.answer_output_mode'} eq 'tex') {
 	$result = ' \end{tabular} \vskip 0 mm ';
     } else {
-	if ($type eq 'optionresponse' || $type eq 'radiobuttonresponse') {
-	    $result = '</tr></table>';
-	} else {
-	    $result = '</tr></table>';
-	}
+	$result = '</tr></table>';
     }
     return $result;
 }