
=head1 NAME

PGmiscevaluators.pl - Some miscellaneous answer macros.

=head1 DESCRIPTION

Currently contains answer evaluators for radio buttons and checkboxes.


The L<parserRadioButtons.pl> and L<parserCheckboxList.pl> macros that manage
display and checking of radio-button and checkbox based answers. It is
recommended that you use those, instead of using the methods in this file.

=cut

BEGIN { strict->import; }
sub _PGmiscevaluators_init { }

=head1 FUNCTIONS

=head2 checkbox_cmp

	ANS(checkbox_cmp($correctAnswer))

$correctAnswer is a string containing the names of the correct boxes, e.g.
"ACD". Note that this means that individual checkbox names can only be one
character. Internally, this is largely the same as unordered_cs_str_cmp().

Note: See L<parserCheckboxList.pl> for an alternative to this.

=cut

# added 6/14/2000 by David Etlinger
# because of the conversion of the answer
# string to an array, I thought it better not
# to force STR_CMP() to work with this

#added 2/26/2003 by Mike Gage
# handled the case where multiple answers are passed as an array reference
# rather than as a \0 delimited string.
sub checkbox_cmp {
	my $correctAnswer = shift @_;
	my %options       = @_;
	assign_option_aliases(\%options,);
	set_default_options(
		\%options,
		'debug' => 0,
		'type'  => 'checkbox_cmp',
	);
	my $answer_evaluator = new AnswerEvaluator(
		correct_ans => $correctAnswer,
		type        => $options{type},
	);
	# pass along debug requests
	$answer_evaluator->{debug} = $options{debug};

	# join student answer array into a single string if necessary
	$answer_evaluator->install_pre_filter(sub {
		my $rh_ans = shift;
		$rh_ans->{_filter_name} = 'convert student_ans to string';
		$rh_ans->{student_ans}  = join("", @{ $rh_ans->{student_ans} })
			if ref($rh_ans->{student_ans}) =~ /ARRAY/i;
		$rh_ans;
	});
	# ignore order of check boxes
	$answer_evaluator->install_pre_filter(\&ignore_order);
	# compare as strings
	$answer_evaluator->install_evaluator(sub {
		my $rh_ans = shift;
		$rh_ans->{_filter_name} = 'compare strings generated by checked boxes';
		$rh_ans->{score}        = ($rh_ans->{student_ans} eq $rh_ans->{correct_ans}) ? 1 : 0;
		$rh_ans;
	});
	# fix up preview displays
	$answer_evaluator->install_post_filter(sub {
		my $rh_ans = shift;
		$rh_ans->{_filter_name}        = 'adjust preview strings';
		$rh_ans->{type}                = $options{type};
		$rh_ans->{preview_text_string} = '\\text{' . $rh_ans->{student_ans} . '}',
			$rh_ans->{preview_latex_string} = '\\text{' . $rh_ans->{student_ans} . '}',
			$rh_ans;

	});

	return $answer_evaluator;
}

=head2 radio_cmp

	ANS(radio_cmp($correctAnswer))

$correctAnswer	is a string containing the name of the correct radio button,
e.g. "Choice1". This is case sensitive and whitespace sensitive, so the correct
answer must match the name of the radio button exactly.

Note: See L<parserRadioButtons.pl> for an alternative to this.

=cut

#added 6/28/2000 by David Etlinger
#exactly the same as strict_str_cmp,
#but more intuitive to the user

# check that answer is really a string and not an array
# also use ordinary string compare
sub radio_cmp {
	#strict_str_cmp( @_ );
	my $response = shift;    # there should be only one item.
	warn "Multiple choices -- this should not happen with radio buttons. Have
	you used checkboxes perhaps?" if ref($response);    #triggered if an ARRAY is passed
	str_cmp($response);
}

=head1 SEE ALSO

L<PGanswermacros.pl>, L<UsingMathObjects>.

=cut

1;
