[LON-CAPA-cvs] cvs: loncom /homework default_homework.lcpm hint.pm /homework/caparesponse caparesponse.pm /homework/templates numPrePro.problem numPrePro.problem.meta stringPrePro.problem stringPrePro.problem.meta doc/loncapafiles loncapafiles.lpml

www www@source.lon-capa.org
Wed, 08 Jun 2011 20:38:53 -0000


This is a MIME encoded message

--www1307565533
Content-Type: text/plain

www		Wed Jun  8 20:38:53 2011 EDT

  Added files:                 
    /loncom/homework/templates	numPrePro.problem numPrePro.problem.meta 
                              	stringPrePro.problem 
                              	stringPrePro.problem.meta 

  Modified files:              
    /doc/loncapafiles	loncapafiles.lpml 
    /loncom/homework	default_homework.lcpm hint.pm 
    /loncom/homework/caparesponse	caparesponse.pm 
  Log:
  Bug #2020: pre-processing of numerical, formula, and string response
  Bug #5100: degrees Fahrenheit, etc
  
  
--www1307565533
Content-Type: text/plain
Content-Disposition: attachment; filename="www-20110608203853.txt"

Index: doc/loncapafiles/loncapafiles.lpml
diff -u doc/loncapafiles/loncapafiles.lpml:1.753 doc/loncapafiles/loncapafiles.lpml:1.754
--- doc/loncapafiles/loncapafiles.lpml:1.753	Thu May 26 22:54:39 2011
+++ doc/loncapafiles/loncapafiles.lpml	Wed Jun  8 20:38:44 2011
@@ -2,7 +2,7 @@
  "http://lpml.sourceforge.net/DTD/lpml.dtd">
 <!-- loncapafiles.lpml -->
 
-<!-- $Id: loncapafiles.lpml,v 1.753 2011/05/26 22:54:39 raeburn Exp $ -->
+<!-- $Id: loncapafiles.lpml,v 1.754 2011/06/08 20:38:44 www Exp $ -->
 
 <!--
 
@@ -1392,6 +1392,8 @@
 man1.jpg.meta;
 numerical.problem;
 numerical.problem.meta;
+numPrePro.problem;
+numPrePro.problem.meta;
 Plot.problem.meta;
 Plot_curve.problem;
 Plot_curve.problem.meta;
@@ -1429,6 +1431,8 @@
 SimpleFormulaCAS.problem.meta;
 StringResponse.problem;
 StringResponse.problem.meta;
+stringPrePro.problem;
+stringPrePro.problem.meta;
 SimpleMatching.problem;
 SimpleMatching.problem.meta;
 SimpleMathResponse.problem;
Index: loncom/homework/default_homework.lcpm
diff -u loncom/homework/default_homework.lcpm:1.157 loncom/homework/default_homework.lcpm:1.158
--- loncom/homework/default_homework.lcpm:1.157	Wed Jun  8 01:39:28 2011
+++ loncom/homework/default_homework.lcpm	Wed Jun  8 20:38:47 2011
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA 
 # used by lonxml::xmlparse() as input variable $safeinit to Apache::run::run()
 #
-# $Id: default_homework.lcpm,v 1.157 2011/06/08 01:39:28 www Exp $
+# $Id: default_homework.lcpm,v 1.158 2011/06/08 20:38:47 www Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -337,7 +337,7 @@
           if ($preprocess=~/\S/) {
              if (defined(&$preprocess)) {
                 no strict 'refs';
-                $element=&$preprocess($element);
+                $element=&$preprocess($element,$unit);
                 use strict 'refs';
              }
           }
@@ -414,6 +414,17 @@
                         }  
                     }
                 }
+                # See if we have preprocessor
+                &LONCAPA_INTERNAL_DEBUG("Ordered preprocessor $preprocess");
+                if ($preprocess=~/\S/) {
+                    if (defined(&$preprocess)) {
+                        no strict 'refs';
+                        $response->[$j]=&$preprocess($response->[$j]);
+                        use strict 'refs';
+                        &LONCAPA_INTERNAL_DEBUG("Ordered processed: $response->[$j]");
+                    }
+                }
+
 		my ($award,$msg) = &caparesponse_check($answer->[$j],
 						       $response->[$j]);
                 if ($type eq 'cs' || $type eq 'ci') {
@@ -452,6 +463,17 @@
                                 }
                             }
                         }
+                        # See if we have preprocessor
+                        &LONCAPA_INTERNAL_DEBUG("Unordered preprocessor $preprocess");
+                        if ($preprocess=~/\S/) {
+                            if (defined(&$preprocess)) {
+                                no strict 'refs';
+                                $response->[$j]=&$preprocess($response->[$j]);
+                                use strict 'refs';
+                                &LONCAPA_INTERNAL_DEBUG("Unordered processed: $response->[$j]");
+                            }
+                        }
+
 			my ($award,$msg) = &caparesponse_check($answer->[$j],
 							       $response->[$j]);
                         if ($type eq 'cs' || $type eq 'ci') {
Index: loncom/homework/hint.pm
diff -u loncom/homework/hint.pm:1.77 loncom/homework/hint.pm:1.78
--- loncom/homework/hint.pm:1.77	Sat Nov 13 03:22:58 2010
+++ loncom/homework/hint.pm	Wed Jun  8 20:38:47 2011
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA 
 # implements the tags that control the hints
 #
-# $Id: hint.pm,v 1.77 2010/11/13 03:22:58 www Exp $
+# $Id: hint.pm,v 1.78 2011/06/08 20:38:47 www Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -133,6 +133,8 @@
 					     $token,40).
 	        &Apache::loncommon::help_open_topic('Formula_Response_Sampling');
 	}
+        $result.=&Apache::edit::text_arg('Pre-Processor Subroutine:','preprocess',
+                                             $token,10);
 	$result.=&Apache::edit::end_row();
 	$result.=&Apache::edit::start_spanning_row();
     } elsif ($target eq 'modified') {
@@ -140,11 +142,11 @@
 	if ($token->[1] eq 'numericalhint') {
 	    $constructtag=&Apache::edit::get_new_args($token,$parstack,
 						     $safeeval,'name',
-						     'answer','unit','format');
+						     'answer','unit','format','preprocess');
 	} elsif ($token->[1] eq 'formulahint') {
 	    $constructtag=&Apache::edit::get_new_args($token,$parstack,
 						     $safeeval,'name','answer',
-						     'samples');
+						     'samples','preprocess');
 	}
 	if ($constructtag) {
 	    $result  = &Apache::edit::rebuild_tag($token);
@@ -381,13 +383,15 @@
 			 [['cs','Case Sensitive'],['ci','Case Insensitive'],
 			  ['mc','Case Insensitive, Any Order'],
 			  ['re','Regular Expression']],$token);
+        $result.=&Apache::edit::text_arg('Pre-Processor Subroutine:','preprocess',
+                                             $token,10);
 	$result.=&Apache::edit::end_row();
 	$result.=&Apache::edit::start_spanning_row();
     } elsif ($target eq 'modified') {
 	my $constructtag;
 	$constructtag=&Apache::edit::get_new_args($token,$parstack,
 						  $safeeval,'name','answer',
-						  'type');
+						  'type','preprocess');
 	$result  = &Apache::edit::rebuild_tag($token);
     } elsif ($target eq 'web') {
 	&Apache::response::reset_params();
Index: loncom/homework/caparesponse/caparesponse.pm
diff -u loncom/homework/caparesponse/caparesponse.pm:1.247 loncom/homework/caparesponse/caparesponse.pm:1.248
--- loncom/homework/caparesponse/caparesponse.pm:1.247	Wed Jun  8 01:39:32 2011
+++ loncom/homework/caparesponse/caparesponse.pm	Wed Jun  8 20:38:50 2011
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # caparesponse definition
 #
-# $Id: caparesponse.pm,v 1.247 2011/06/08 01:39:32 www Exp $
+# $Id: caparesponse.pm,v 1.248 2011/06/08 20:38:50 www Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -280,6 +280,8 @@
 					     $token,40).
 	      &Apache::loncommon::help_open_topic('Formula_Response_Sampling');
 	}
+        $result.=&Apache::edit::text_arg('Pre-Processor Subroutine:','preprocess',
+                                             $token,10);
 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
     } elsif ($target eq 'modified') {
 	my $constructtag;
@@ -287,11 +289,11 @@
 	    $constructtag=&Apache::edit::get_new_args($token,$parstack,
 						      $safeeval,'answer',
  						      'incorrect','unit',
-						      'format');
+						      'format','preprocess');
 	} elsif ($token->[1] eq 'formularesponse') {
 	    $constructtag=&Apache::edit::get_new_args($token,$parstack,
 						      $safeeval,'answer',
-						      'samples');
+						      'samples','preproces');
 	}
 	if ($constructtag) {
 	    $result = &Apache::edit::rebuild_tag($token);
@@ -1148,12 +1150,14 @@
 			  ['re','Regular Expression']],$token);
 	$result.=&Apache::edit::text_arg('String to display for answer:',
 					 'answerdisplay',$token);
+        $result.=&Apache::edit::text_arg('Pre-Processor Subroutine:','preprocess',
+                                             $token,10);
 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
     } elsif ($target eq 'modified') {
 	my $constructtag;
 	$constructtag=&Apache::edit::get_new_args($token,$parstack,
 						  $safeeval,'answer',
-						  'type','answerdisplay');
+						  'type','answerdisplay','preprocess');
 	if ($constructtag) {
 	    $result = &Apache::edit::rebuild_tag($token);
 	    $result.=&Apache::edit::handle_insert();
@@ -1213,12 +1217,17 @@
 #			$answer=$token->[2]->{'answer'};
 #		    }
 		    ${$safeeval->varglob('LONCAPA::response')}=$response;
+                    my $preprocess=&Apache::lonxml::get_param('preprocess',$parstack,$safeeval);
+                    $preprocess=~s/^\&//;
+                    if (defined($preprocess)) {
+                        &Apache::run::run('$LONCAPA::response=&'.$preprocess.'($LONCAPA::response);',$safeeval);
+                    }
 		    $result = &Apache::run::run('if ($LONCAPA::response=~m'.$answer.') { return 1; } else { return 0; }',$safeeval);
 		    &Apache::lonxml::debug("current $response");
 		    &Apache::lonxml::debug("current $answer");
 		    $ad = ($result) ? 'APPROX_ANS' : 'INCORRECT';
 		} else {
-		    my @args = ('type');
+		    my @args = ('type','preprocess');
 		    my $args_ref = &setup_capa_args($safeeval,$parstack,
 						    \@args,$response);
                     if ($$args_ref{'type'} eq '') {

Index: loncom/homework/templates/numPrePro.problem
+++ loncom/homework/templates/numPrePro.problem
<problem>

<script type="loncapa/perl">
#Enter the computations here
$idx=&random(0,2,1);
$season=('nice spring','hot summer','cool fall')[$idx];
$temp=273+(20,30,10)[$idx];

sub abstemp {
    my ($answer,$unit)=@_;
    if ($unit=~/degC/) { $answer+=273; }
    if ($unit=~/degF/) { $answer+=460; }
    return $answer;
}
</script>

<startouttext />
What is the temperature in Germany on a $season day?
<endouttext />

<numericalresponse answer="$temp" id="11" preprocess="abstemp" unit="K">
    <responseparam name="tol" type="tolerance" default="5" description="Numerical Tolerance" />
    <textline readonly="no" />
</numericalresponse>
</problem>





Index: loncom/homework/templates/numPrePro.problem.meta
+++ loncom/homework/templates/numPrePro.problem.meta
<title>Numerical Response with Pre-Processing</title>
<category>Numerical Problems</category>

Index: loncom/homework/templates/stringPrePro.problem
+++ loncom/homework/templates/stringPrePro.problem
<problem>
<script type="loncapa/perl">$idx=&random(0,2,1);
$word=('Maus','Horn','Huhn')[$idx];
$plural=('Mäuse','Hörner','Hühner')[$idx];

sub umlaute {
    my $answer=shift;
    $answer=~s/ae/ä/g;
    $answer=~s/oe/ö/g;
    $answer=~s/ue/ü/g;
    return $answer;
}</script>

<startouttext />
What is the plural of German "$word"?
<endouttext />

<stringresponse answer="$plural" type="cs" preprocess="umlaute">
    <textline readonly="no" addchars="Ä,Ö,Ü,ä,ö,ü,ß"></textline>
    
</stringresponse>

</problem>

Index: loncom/homework/templates/stringPrePro.problem.meta
+++ loncom/homework/templates/stringPrePro.problem.meta
<title>String Response with Pre-Processing</title>
<category>Free Form Problems</category>
<help>String_Response_Problems</help>

--www1307565533--