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

albertel lon-capa-cvs@mail.lon-capa.org
Thu, 20 Nov 2003 22:06:39 -0000


This is a MIME encoded message

--albertel1069365999
Content-Type: text/plain

albertel		Thu Nov 20 17:06:39 2003 EDT

  Modified files:              
    /loncom/homework/caparesponse	caparesponse.pm 
  Log:
  - BUG#2373 adding Regular Expression support to <stringresponse>
  
  
  
--albertel1069365999
Content-Type: text/plain
Content-Disposition: attachment; filename="albertel-20031120170639.txt"

Index: loncom/homework/caparesponse/caparesponse.pm
diff -u loncom/homework/caparesponse/caparesponse.pm:1.121 loncom/homework/caparesponse/caparesponse.pm:1.122
--- loncom/homework/caparesponse/caparesponse.pm:1.121	Fri Oct 24 14:08:24 2003
+++ loncom/homework/caparesponse/caparesponse.pm	Thu Nov 20 17:06:39 2003
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # caparesponse definition
 #
-# $Id: caparesponse.pm,v 1.121 2003/10/24 18:08:24 sakharuk Exp $
+# $Id: caparesponse.pm,v 1.122 2003/11/20 22:06:39 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -41,17 +41,13 @@
     if ($target eq 'edit') {
 	$result.=&Apache::edit::tag_start($target,$token);
 	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
-	$result.=&Apache::edit::text_arg('Incorrect Answers:','incorrect',
-					 $token);
 	if ($token->[1] eq 'numericalresponse') {
+	    $result.=&Apache::edit::text_arg('Incorrect Answers:','incorrect',
+					     $token);
 	    $result.=&Apache::edit::text_arg('Unit:','unit',$token,5).
 		&Apache::loncommon::help_open_topic('Physical_Units');
 	    $result.=&Apache::edit::text_arg('Format:','format',$token,4).
 		&Apache::loncommon::help_open_topic('Numerical_Response_Format');
-	} elsif ($token->[1] eq 'stringresponse') {
-	    $result.=&Apache::edit::select_arg('Type:','type',
-			 [['cs','Case Sensitive'],['ci','Case Insensitive'],
-			  ['mc','Case Insensitive, Any Order']],$token);
 	} elsif ($token->[1] eq 'formularesponse') {
 	    $result.=&Apache::edit::text_arg('Sample Points:','samples',
 					     $token,40).
@@ -65,10 +61,6 @@
 						      $safeeval,'answer',
 						      'incorrect','unit',
 						      'format');
-	} elsif ($token->[1] eq 'stringresponse') {
-	    $constructtag=&Apache::edit::get_new_args($token,$parstack,
-						      $safeeval,'answer',
-						      'type');
 	} elsif ($token->[1] eq 'formularesponse') {
 	    $constructtag=&Apache::edit::get_new_args($token,$parstack,
 						      $safeeval,'answer',
@@ -109,8 +101,7 @@
 	my $partid = $Apache::inputtags::part;
 	my $id = $Apache::inputtags::response['-1'];
 	if ($Apache::lonhomework::type eq 'exam' && 
-	    ($$tagstack[-1] eq 'formularesponse' ||
-	     $$tagstack[-1] eq 'stringresponse')) {
+	    $$tagstack[-1] eq 'formularesponse') {
 	    $increment=&Apache::response::scored_response($partid,$id);
 	} else {
 	    my $response = &Apache::response::getresponse();
@@ -351,30 +342,6 @@
 		push (@{ $Apache::lonhomework::analyze{"$part_id.unit"} }, $unit);
 	    }
 	}
-	if ($type || $$tagstack[-1] eq 'stringresponse') {
-	    my $string='Case Insensitive';
-	    if ($type eq 'mc') {
-		$string='Multiple Choice';
-	    } elsif ($type eq 'cs') {
-		$string='Case Sensitive';
-	    } elsif ($type eq 'ci') {
-		$string='Case Insensitive';
-	    } elsif ($type eq 'fml') {
-		$string='Formula';
-	    }
-	    if ($target eq 'answer') {
-		if ($ENV{'form.answer_output_mode'} eq 'tex') {
-		    $result.=&Apache::response::answer_part($$tagstack[-1],
-							  "$string");
-		} else {
-		    $result.=&Apache::response::answer_part($$tagstack[-1],
-							    "<b>$string</b>");
-		}
-	    } elsif ($target eq 'analyze') {
-		push (@{ $Apache::lonhomework::analyze{"$part_id.str_type"} },
-		      $type);
-	    }
-	}
 	if ($$tagstack[-1] eq 'formularesponse' && $target eq 'answer') {
 	    my $samples=&Apache::lonxml::get_param('samples',$parstack,$safeeval);
 	    $result.=&Apache::response::answer_part($$tagstack[-1],$samples);
@@ -514,18 +481,169 @@
 sub start_stringresponse {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
     my $result;
+    my $id = &Apache::response::start_response($parstack,$safeeval);
     if ($target eq 'meta') {
 	&Apache::response::start_response($parstack,$safeeval);
 	$result=&Apache::response::meta_package_write('stringresponse');
 	&Apache::response::end_response();
-    } else {
-	$result.=&start_numericalresponse(@_);
+    } elsif ($target eq 'edit') {
+	$result.=&Apache::edit::tag_start($target,$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().&Apache::edit::start_spanning_row();
+    } elsif ($target eq 'modified') {
+	my $constructtag;
+	$constructtag=&Apache::edit::get_new_args($token,$parstack,
+						  $safeeval,'answer',
+						  'type');
+	if ($constructtag) {
+	    $result = &Apache::edit::rebuild_tag($token);
+	    $result.=&Apache::edit::handle_insert();
+	}
+    } elsif ($target eq 'answer' || $target eq 'grade') {
+	&Apache::response::reset_params();
     }
     return $result;
 }
 
 sub end_stringresponse {
-    return end_numericalresponse(@_);
+    my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
+    my $increment=1;
+    my $result = '';
+    my $part=$Apache::inputtags::part;
+    my $id=$Apache::inputtags::response[-1];
+    my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
+    my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
+    &Apache::lonxml::debug("current $answer ".$token->[2]);
+    if (!$Apache::lonxml::default_homework_loaded) {
+	&Apache::lonxml::default_homework_load($safeeval);
+    }
+    if ( $target eq 'grade' && defined($ENV{'form.submitted'})) {
+	&Apache::response::setup_params($$tagstack[-1]);
+	$safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
+	if ($Apache::lonhomework::type eq 'exam' ||
+	    $ENV{'form.submitted'} eq 'scantron') {
+	    $increment=&Apache::response::scored_response($part,$id);
+	} else {
+	    my $response = &Apache::response::getresponse();
+	    if ( $response =~ /[^\s]/) {
+		my %previous = &Apache::response::check_for_previous($response,
+								    $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;
+		if ($type eq 're' ) { 
+		    # if the RE wasn't in a var it likely got munged,
+                    # thus grab it from the var directly
+#		    my $testans=$token->[2]->{'answer'};
+#		    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);
+		    &Apache::lonxml::debug("current $response");
+		    &Apache::lonxml::debug("current $answer");
+		    $ad = ($result) ? 'APPROX_ANS' : 'INCORRECT';
+		} else {
+		    $response =~ s/\\/\\\\/g;
+		    $response =~ s/\'/\\\'/g;
+		    &Apache::lonxml::debug("current $response");
+		    my $expression="&caparesponse_check_list('".$response."','".
+			$$parstack[-1];
+		    foreach my $key (keys(%Apache::inputtags::params)) {
+			$expression.= ';my $'. #'
+			    $key.'="'.$Apache::inputtags::params{$key}.'"';
+		    }
+		    $expression.="');";
+		    &Apache::lonxml::debug('answer is'.join(':',$answer));
+		    @{$safeeval->varglob('CAPARESPONSE_CHECK_LIST_answer')}=($answer);
+		    $result = &Apache::run::run($expression,$safeeval);
+		    my ($awards) = split /:/ , $result;
+		    ($ad) = &Apache::inputtags::finalizeawards(split /,/ , $awards);
+		    &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.$part.$id.awarddetail"}=$ad;
+	    }
+	}
+    } elsif ($target eq 'web' || $target eq 'tex') {
+	my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
+	my $status = $Apache::inputtags::status['-1'];
+	if (  &Apache::response::show_answer() ) {
+	    if ($target eq 'web') {
+		$result="<br />The correct answer is ".$answer;
+#		    join(', ',@answers).".<br />";
+	    }
+	}
+	if ($Apache::lonhomework::type eq 'exam' && $target eq 'tex') {
+	    $result.='\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
+	    $increment = &Apache::response::repetition();
+	    $result.='\begin{enumerate}';
+	    for (my $i=0;$i<$increment;$i++) {
+		$result.='\item[\textbf{'.($Apache::lonxml::counter+$i).
+		    '}.]\textit{Leave blank on scoring form}\vskip 0 mm';
+	    }
+	    $result.= '\end{enumerate}';
+	}
+    } elsif ($target eq 'answer' || $target eq 'analyze') {
+	if ($target eq 'analyze') {
+	    push (@{ $Apache::lonhomework::analyze{"parts"} },"$part.$id");
+	    $Apache::lonhomework::analyze{"$part.$id.type"} = $$tagstack[-1];
+	}
+	&Apache::response::setup_params($$tagstack[-1]);
+	if ($target eq 'answer') {
+	    $result.=&Apache::response::answer_header($$tagstack[-1]);
+	}
+#	foreach my $ans (@answers) {
+	    if ($target eq 'answer') {
+		$result.=&Apache::response::answer_part($$tagstack[-1],$answer);
+	    } elsif ($target eq 'analyze') {
+		push (@{ $Apache::lonhomework::analyze{"$part.$id.answer"} },
+		      $answer);
+	    }
+#	}
+	my $string='Case Insensitive';
+	if ($type eq 'mc') {
+	    $string='Multiple Choice';
+	} elsif ($type eq 'cs') {
+	    $string='Case Sensitive';
+	} elsif ($type eq 'ci') {
+	    $string='Case Insensitive';
+	} elsif ($type eq 're') {
+	    $string='Regular Expression';
+	}
+	if ($target eq 'answer') {
+	    if ($ENV{'form.answer_output_mode'} eq 'tex') {
+		$result.=&Apache::response::answer_part($$tagstack[-1],
+							"$string");
+	    } else {
+		$result.=&Apache::response::answer_part($$tagstack[-1],
+							"<b>$string</b>");
+	    }
+	} elsif ($target eq 'analyze') {
+	    push (@{$Apache::lonhomework::analyze{"$part.$id.str_type"}},
+		  $type);
+	}
+	if ($target eq 'answer') {
+	    $result.=&Apache::response::answer_footer($$tagstack[-1]);
+	}
+    } elsif ($target eq 'edit') {
+	$result.='</td></tr>'.&Apache::edit::end_table;
+    }
+    if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
+	$target eq 'tex' || $target eq 'analyze') {
+	&Apache::lonxml::increment_counter($increment);
+    }
+    &Apache::response::end_response;
+    return $result;
 }
 
 sub start_formularesponse {

--albertel1069365999--