[LON-CAPA-cvs] cvs: loncom /homework default_homework.lcpm hint.pm /homework/caparesponse caparesponse.pm

albertel lon-capa-cvs@mail.lon-capa.org
Tue, 16 Mar 2004 19:47:48 -0000


This is a MIME encoded message

--albertel1079466468
Content-Type: text/plain

albertel		Tue Mar 16 14:47:48 2004 EDT

  Modified files:              
    /loncom/homework	default_homework.lcpm hint.pm 
    /loncom/homework/caparesponse	caparesponse.pm 
  Log:
  - Fixes Bug#2820 (internal vars were clashing with user vars)
  - Cleans up the way data is put into safe space, less escaping, names scaping etc issues
  - <stringhint> supports re mode now.
  
  
  
--albertel1079466468
Content-Type: text/plain
Content-Disposition: attachment; filename="albertel-20040316144748.txt"

Index: loncom/homework/default_homework.lcpm
diff -u loncom/homework/default_homework.lcpm:1.73 loncom/homework/default_homework.lcpm:1.74
--- loncom/homework/default_homework.lcpm:1.73	Tue Mar 16 10:31:52 2004
+++ loncom/homework/default_homework.lcpm	Tue Mar 16 14:47:47 2004
@@ -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.73 2004/03/16 15:31:52 albertel Exp $
+# $Id: default_homework.lcpm,v 1.74 2004/03/16 19:47:47 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -40,16 +40,20 @@
 $"=' ';
 
 sub caparesponse_check {
+    my ($answer,$response)=@_;
     #not properly used yet: calc
     #not to be used: $ans_fmt
-    my ($type,$tol,$sig,$ans_fmt,$unit,$calc,$samples) =
-	eval $_[2].
-	';return ($__LC__type,$__LC__tol,$__LC__sig,$__LC__ans_fmt,$__LC__unit,$__LC__calc,$__LC__samples);';
+    my $type=$LONCAPA::CAPAresponse_args{'type'};
+    my $tol=$LONCAPA::CAPAresponse_args{'tol'};
+    my $sig=$LONCAPA::CAPAresponse_args{'sig'};
+    my $ans_fmt=$LONCAPA::CAPAresponse_args{'ans_fmt'};
+    my $unit=$LONCAPA::CAPAresponse_args{'unit'};
+    my $calc=$LONCAPA::CAPAresponse_args{'calc'};
+    my $samples=$LONCAPA::CAPAresponse_args{'samples'};
     
     my $tol_type=''; # gets it's value from whether tol has a % or not done
     my $sig_lbound=''; #done
     my $sig_ubound=''; #done
-    my ($answer,$response,$expr)=@_;
 
 
     #type's definitons come from capaParser.h
@@ -68,7 +72,7 @@
     if (length($response) > 500) { return "TOO_LONG: Answer too long"; }
 
     if ($type eq '' ) {
-	$message .= "Didn't find a type :$type:$expr: defaulting\n";
+	$message .= "Didn't find a type :$type: defaulting\n";
 	if ( $answer eq ($answer *1.0)) { $type = 2;
 				      } else { $type = 3; }
     } else {
@@ -134,20 +138,21 @@
     elsif ($result =='12') { $result='WANTED_NUMERIC'; }
     else  {$result = "ERROR: Unknown Result:$result:$@:";}
 
-    return ("$result:\nRetError $reterror:\nError $error:\nAnswer $answer:\nResponse $response:\n type-$type|$tol|$tol_type|$sig:$sig_lbound:$sig_ubound|$unit|\n$message$expr",$reterror);
+    return ("$result:\nRetError $reterror:\nError $error:\nAnswer $answer:\nResponse $response:\n type-$type|$tol|$tol_type|$sig:$sig_lbound:$sig_ubound|$unit|\n$message",$reterror);
 }
 
 
 sub caparesponse_check_list {
-    my ($response,$expr)=@_;
+    my $response=$LONCAPA::CAPAresponse_args{'response'};
     my ($result,@list);
-    @list=@CAPARESPONSE_CHECK_LIST_answer;
+    @list=@LONCAPA::CAPAresponse_answer;
     my $aresult='';
     my $current_answer;
     my $answers=join(':',@list);
     $result.="Got response :$answers:\n";
+    &LONCAPA_INTERNAL_DEBUG("<blink>Yo!</blink> got ".join(':',%LONCAPA::CAPAresponse_args));
     my @responselist;
-    my $type =eval $expr.';return $__LC__type;';
+    my $type = $LONCAPA::CAPAresponse_args{'type'};
     $result.="Got type :$type:\n";
     if ($type ne '' && $#list > 0) {
 	(@responselist)=split /,/,$response;
@@ -175,12 +180,10 @@
 	my $thisanswer=$list[$i];
 	$result.="trying answer :$thisanswer:\n";
 	if ($unit eq '') {
-	    ($aresult,$msg)=&caparesponse_check($thisanswer,$responselist[$i],
-						$expr);
+	    ($aresult,$msg)=&caparesponse_check($thisanswer,$responselist[$i]);
 	} else {
 	    ($aresult,$msg)=&caparesponse_check($thisanswer,
-						$responselist[$i]." $unit",
-						$expr);
+						$responselist[$i]." $unit");
 	}
 	my ($temp)=split /:/, $aresult;
 	$awards.="$temp,";
Index: loncom/homework/hint.pm
diff -u loncom/homework/hint.pm:1.50 loncom/homework/hint.pm:1.51
--- loncom/homework/hint.pm:1.50	Fri Mar 12 19:36:11 2004
+++ loncom/homework/hint.pm	Tue Mar 16 14:47:47 2004
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA 
 # implements the tags that control the hints
 #
-# $Id: hint.pm,v 1.50 2004/03/13 00:36:11 albertel Exp $
+# $Id: hint.pm,v 1.51 2004/03/16 19:47:47 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -158,27 +158,32 @@
 	my $response = $Apache::lonhomework::history{
 			    "resource.$partid.$submitid.submission"};
 	&Apache::lonxml::debug("hintgroup is using $response<br />\n");
-	#build safe space expression
-	my $expression="&caparesponse_check_list('".$response."','".
-	    $$parstack[$#$parstack];
+	my $hideunit=&Apache::lonnet::EXT('resource.'.$submitid.'_'.$id.'.turnoffunit');
+	my $args_ref= \%{$safeeval->varglob('LONCAPA::CAPAresponse_args')};
+	$$args_ref{'response'}=$response;
 	#need to get all possible parms
+	foreach my $arg ('type','tol','sig','ans_fmt','unit','calc',
+			 'samples') {
+	    $$args_ref{$arg}=
+		&Apache::lonxml::get_param($arg,$parstack,$safeeval);
+	}
 	foreach my $key (keys(%Apache::inputtags::params)) {
-	    $expression.= ';my $__LC__'. #'
-		$key.'="'.$Apache::inputtags::params{$key}.'"';
+	    $$args_ref{$key}=$Apache::inputtags::params{$key};
 	}
+	if (lc($hideunit) eq "yes") { delete($$args_ref{'unit'}); }
 	if ($$tagstack[-1] eq 'formulahint') {
-	    $expression.=';my $__LC__type="fml";';
+	    $$args_ref{'type'}='fml';
 	} elsif ($$tagstack[-1] eq 'numericalhint') {
-	    $expression.=';my $__LC__type="float";';
+	    $$args_ref{'type'}='float';
 	}
-	$expression.="');";
 	my @answer=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
 	&Apache::lonxml::debug('answer is'.join(':',@answer));
-	@{$safeeval->varglob('CAPARESPONSE_CHECK_LIST_answer')}=@answer;
+	@{$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=@answer;
 		
-	($result,my @msgs) = &Apache::run::run($expression,$safeeval);
+	($result,my @msgs) = &Apache::run::run("&caparesponse_check_list()",
+						$safeeval);
 	&Apache::lonxml::debug('msgs are'.join(':',@msgs));
-	&Apache::lonxml::debug("$expression:result:$result:$Apache::lonxml::curdepth");
+	&Apache::lonxml::debug("result:$result:$Apache::lonxml::curdepth");
 	my ($awards)=split(/:/,$result);
 	my (@awards) = split(/,/,$awards);
 	my ($ad, $msg) = &Apache::inputtags::finalizeawards(\@awards,\@msgs);
@@ -193,20 +198,96 @@
     return $result;
 }
 
-sub start_stringhint {
+sub start_formulahint {
     return &start_numericalhint(@_);
 }
 
-sub end_stringhint {
+sub end_formulahint {
     return &end_numericalhint(@_);
 }
 
-sub start_formulahint {
-    return &start_numericalhint(@_);
+sub start_stringhint {
+    my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
+    #do everything in end, so intervening <responseparams> work
+    &Apache::response::start_hintresponse($parstack,$safeeval);
+    my $result;
+    if ($target eq 'edit') {
+	$result.=&Apache::edit::tag_start($target,$token);
+	$result.=&Apache::edit::text_arg('Name:','name',$token);
+	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
+	$result.=&Apache::edit::select_arg('Type:','type',
+			 [['cs','Case Sensitive'],['ci','Case Insensitive'],
+			  ['mc','Case Insensitive, Any Order'],
+			  ['re','Regular Expression']],$token);
+	$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');
+	$result  = &Apache::edit::rebuild_tag($token);
+	$result .= &Apache::edit::handle_insert();
+    } elsif ($target eq 'web') {
+	&Apache::response::reset_params();
+    }
+    return $result;
 }
 
-sub end_formulahint {
-    return end_numericalhint(@_);
+sub end_stringhint {
+    my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
+    my $result;
+    if ($target eq 'web') {
+	if (!$Apache::lonxml::default_homework_loaded) {
+	    &Apache::lonxml::default_homework_load($safeeval);
+	}
+	my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
+	$safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
+	my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
+	&Apache::response::setup_params('stringhint',$safeeval);
+	my $partid=$Apache::inputtags::part;
+	my $id=$Apache::inputtags::response['-1'];
+	#id submissions occured under
+	my $submitid=$Apache::inputtags::response['-2'];
+	my $response = $Apache::lonhomework::history{
+			    "resource.$partid.$submitid.submission"};
+	&Apache::lonxml::debug("hintgroup is using $response<br />\n");
+	my $args_ref= \%{$safeeval->varglob('LONCAPA::CAPAresponse_args')};
+	$$args_ref{'response'}=$response;
+	my $type=$$args_ref{'type'}=&Apache::lonxml::get_param('type',$parstack,$safeeval);
+	my ($ad,$msg);
+	if ($type eq 're' ) {
+	    ${$safeeval->varglob('LONCAPA::response')}=$response;
+	    $result = &Apache::run::run('return $LONCAPA::response=~m'.$answer,$safeeval);
+	    &Apache::lonxml::debug("current $response");
+	    &Apache::lonxml::debug("current $answer");
+	    $ad = ($result) ? 'APPROX_ANS' : 'INCORRECT';
+	} else { 
+	    foreach my $key (keys(%Apache::inputtags::params)) {
+		$$args_ref{$key}=$Apache::inputtags::params{$key};
+	    }
+	    &Apache::lonxml::debug('answer is'.$answer);
+	    @{$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=($answer);
+	    
+	    ($result,my @msgs)=&Apache::run::run("&caparesponse_check_list()",
+						   $safeeval);
+	    &Apache::lonxml::debug('msgs are'.join(':',@msgs));
+	    &Apache::lonxml::debug("result:$result:$Apache::lonxml::curdepth");
+	    my ($awards)=split(/:/,$result);
+	    my (@awards) = split(/,/,$awards);
+	    ($ad, $msg) = &Apache::inputtags::finalizeawards(\@awards,\@msgs);
+	}
+	if ($ad eq 'EXACT_ANS' || $ad eq 'APPROX_ANS') { 
+	    push (@Apache::hint::which,$name);
+	}
+	$result='';
+    } elsif ($target eq 'meta') {
+	$result=&Apache::response::meta_package_write($token->[1]);
+    } elsif ($target eq 'edit') {
+	$result.='</td></tr>'.&Apache::edit::end_table;
+    }
+    &Apache::response::end_hintresponse();
+    return $result;
 }
 
 # a part shows if it is on, if no specific parts are on, then default shows
Index: loncom/homework/caparesponse/caparesponse.pm
diff -u loncom/homework/caparesponse/caparesponse.pm:1.142 loncom/homework/caparesponse/caparesponse.pm:1.143
--- loncom/homework/caparesponse/caparesponse.pm:1.142	Fri Mar 12 19:36:11 2004
+++ loncom/homework/caparesponse/caparesponse.pm	Tue Mar 16 14:47:47 2004
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # caparesponse definition
 #
-# $Id: caparesponse.pm,v 1.142 2004/03/13 00:36:11 albertel Exp $
+# $Id: caparesponse.pm,v 1.143 2004/03/16 19:47:47 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -39,6 +39,7 @@
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
     my $id = &Apache::response::start_response($parstack,$safeeval);
     my $result;
+    undef %{$safeeval->varglob('LONCAPA::CAPAresponse_args')};
     if ($target eq 'edit') {
 	$result.=&Apache::edit::tag_start($target,$token);
 	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
@@ -122,44 +123,44 @@
 		    my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
 		    my @values=&make_numerical_bubbles($number_of_bubbles,$target,$answers[0],$formats[0],\@incorrect);
 		    $response=$values[$response];
-		} else {
-		    $response =~ s/\\/\\\\/g;
-		    $response =~ s/\'/\\\'/g;
 		}
 		$Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
-		&Apache::lonxml::debug("current $response");
-		my $expression="&caparesponse_check_list('".$response."','".
-		    $$parstack[-1];
+		my $args_ref= \%{$safeeval->varglob('LONCAPA::CAPAresponse_args')};
+		$$args_ref{'response'}=$response;
 		my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit');
 
+		foreach my $arg ('type','tol','sig','ans_fmt','unit','calc',
+				 'samples') {
+		    $$args_ref{$arg}=
+			&Apache::lonxml::get_param($arg,$parstack,$safeeval);
+		}
 		foreach my $key (keys(%Apache::inputtags::params)) {
-		    $expression.= ';my $__LC__'. #'
-			$key.'="'.$Apache::inputtags::params{$key}.'"';
+		    $$args_ref{$key}=$Apache::inputtags::params{$key};
 		}
 
 		#no way to enter units, with radio buttons
 		if ($Apache::lonhomework::type eq 'exam' ||
 		    lc($hideunit) eq "yes") {
-		    $expression.=';my $__LC__unit=undef;';
+		    delete($$args_ref{'unit'});
 		}
 		#sig fig don't make much sense either
 		if (($Apache::lonhomework::type eq 'exam' ||
 		     $ENV{'form.submitted'} eq 'scantron') &&
 		    $tag eq 'numericalresponse') {
-		    $expression.=';my $__LC__sig=undef;';
+		    delete($$args_ref{'sig'});
 		}
 
 		if ($tag eq 'formularesponse') {
-		    $expression.=';my $__LC__type="fml";';
+		    $$args_ref{'type'}='fml';
 		} elsif ($tag eq 'numericalresponse') {
-		    $expression.=';my $__LC__type="float";';
+		    $$args_ref{'type'}='float';
 		}
-		$expression.="');";
 		my @answer=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
 		&Apache::lonxml::debug('answer is'.join(':',@answer));
-		@{$safeeval->varglob('CAPARESPONSE_CHECK_LIST_answer')}=@answer;
+		@{$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=@answer;
 		
-		($result,my @msgs) = &Apache::run::run($expression,$safeeval);
+		($result,my @msgs) = 
+		    &Apache::run::run("&caparesponse_check_list()",$safeeval);
 		&Apache::lonxml::debug('msgs are'.join(':',@msgs));
 		my ($awards)=split(/:/,$result);
 		my (@awards) = split(/,/,$awards);
@@ -172,7 +173,6 @@
 		    &Apache::lonxml::debug("sigs bad $sig_u $sig_l ".
 					   $Apache::inputtags::params{'sig'});
 		}
-		&Apache::lonxml::debug("$expression");
 		&Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
 		&Apache::response::handle_previous(\%previous,$ad);
 		$Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
@@ -605,7 +605,6 @@
 								    $part,$id);
 		&Apache::lonxml::debug("submitted a $response<br>\n");
 		&Apache::lonxml::debug($$parstack[-1] . "\n<br>");
-		
 		$Apache::lonhomework::results{"resource.$part.$id.submission"}=
 		    $response;
 		my ($ad,$msg);
@@ -616,31 +615,29 @@
 #		    if ($testans !~ m/^\s*\$/) {
 #			$answer=$token->[2]->{'answer'};
 #		    }
-		    ${$safeeval->varglob('LONCAPA_INTERNAL_response')}=
-			$response;
-		    $result = &Apache::run::run('return $LONCAPA_INTERNAL_response=~m'.$answer,$safeeval);
+		    ${$safeeval->varglob('LONCAPA::response')}=$response;
+		    $result = &Apache::run::run('return $LONCAPA::response=~m'.$answer,$safeeval);
 		    &Apache::lonxml::debug("current $response");
 		    &Apache::lonxml::debug("current $answer");
 		    $ad = ($result) ? 'APPROX_ANS' : 'INCORRECT';
 		} else {
-		    $response =~ s/\\/\\\\/g;
-		    $response =~ s/\'/\\\'/g;
+		    my $args_ref= 
+			\%{$safeeval->varglob('LONCAPA::CAPAresponse_args')};
+		
+		    $$args_ref{'response'}=$response;
 		    &Apache::lonxml::debug("current $response");
-		    my $expression="&caparesponse_check_list('".$response."','".
-			$$parstack[-1];
+		    $$args_ref{'type'}=
+			&Apache::lonxml::get_param('type',$parstack,$safeeval);
 		    foreach my $key (keys(%Apache::inputtags::params)) {
-			$expression.= ';my $'. #'
-			    $key.'="'.$Apache::inputtags::params{$key}.'"';
+			$$args_ref{$key}=$Apache::inputtags::params{$key};
 		    }
-		    $expression.="');";
 		    &Apache::lonxml::debug('answer is'.join(':',$answer));
-		    @{$safeeval->varglob('CAPARESPONSE_CHECK_LIST_answer')}=($answer);
-		    ($result, my @msgs) = &Apache::run::run($expression,$safeeval);
+		    @{$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=($answer);
+		    ($result, my @msgs)=&Apache::run::run("&caparesponse_check_list()",$safeeval);
 		    &Apache::lonxml::debug('msgs are'.join(':',@msgs));
 		    my ($awards)=split(/:/,$result);
 		    my (@awards) = split(/,/,$awards);
 		    ($ad,$msg) = &Apache::inputtags::finalizeawards(\@awards,\@msgs);
-		    &Apache::lonxml::debug("$expression");
 		    &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
 		}
 		&Apache::response::handle_previous(\%previous,$ad);

--albertel1079466468--