@@ -53,75 +53,55 @@ loadMacros(
5353################################################################
5454
5555sub custom_problem_grader {
56- my $rh_evaluated_answers = shift;
57- my $rh_problem_state = shift;
58- my %form_options = @_;
59- my %evaluated_answers = %{$rh_evaluated_answers};
60- # The hash $rh_evaluated_answers typically contains:
61- # 'answer1' => 34, 'answer2'=> 'Mozart', etc.
62-
63- # By default the old problem state is simply passed back out again.
64- my %problem_state = %$rh_problem_state;
65-
66-
67- # %form_options might include
68- # The user login name
69- # The permission level of the user
70- # The studentLogin name for this psvn.
71- # Whether the form is asking for a refresh or
72- # is submitting a new answer.
73-
74- # initial setup of the answer
75- my $total=0;
76- my %problem_result = ( score => 0,
77- errors => '',
78- type => 'custom_problem_grader',
79- msg => 'To get full credit, all answers must be correct. Having all but one correct is worth 50%. Two or more incorrect answers gives a score of 0%.',
80- );
56+ my $evaluated_answers = shift;
57+ my $problem_state = shift;
58+ my %form_options = @_;
59+
60+ # initial setup of the answer
61+ my $total = 0;
62+ my $problem_result = {
63+ score => 0,
64+ errors => '',
65+ type => 'custom_problem_grader',
66+ msg => 'To get full credit, all answers must be correct. Having all but one correct is worth 50%. Two or more incorrect answers gives a score of 0%.',
67+ };
8168
8269 # Return unless answers have been submitted
83- unless ($form_options{answers_submitted} == 1) {
84-
85- # Since this code is in a .pg file we must use double tildes
86- # instead of Perl's backslash on the next line.
87- return(~~%problem_result,~~%problem_state);
88- }
89- # Answers have been submitted -- process them.
90-
91- ########################################################
92- # Here's where we compute the score. The variable #
93- # $numright is the number of correct answers. #
94- ########################################################
95-
96- my $numright=0;
97-
98- $numright += ($evaluated_answers{'AnSwEr0001'}->{score});
99- $numright += ($evaluated_answers{'AnSwEr0002'}->{score});
100- $numright += ($evaluated_answers{'AnSwEr0003'}->{score});
101- $numright += ($evaluated_answers{'AnSwEr0004'}->{score});
102- $numright += ($evaluated_answers{'AnSwEr0005'}->{score});
103- $numright += ($evaluated_answers{'AnSwEr0006'}->{score});
104-
105- if ($numright == 6) {
106- $total = 1;
107- } elsif ($numright == 5) {
108- $total = 0.5;
109- } else {
110- $total = 0;
111- }
112-
113- $problem_result{score} = $total;
114- # increase recorded score if the current score is greater.
115- $problem_state{recorded_score} = $problem_result{score} if $problem_result{score} > $problem_state{recorded_score};
116-
117-
118- $problem_state{num_of_correct_ans}++ if $total == 1;
119- $problem_state{num_of_incorrect_ans}++ if $total < 1 ;
120-
121- # Since this code is in a .pg file we must use double tildes
122- # instead of Perl's backslash on the next line.
123- (~~%problem_result, ~~%problem_state);
70+ return($problem_result, $problem_state) unless ($form_options{answers_submitted} == 1);
71+
72+ # Answers have been submitted -- process them.
73+
74+ ########################################################
75+ # Here's where we compute the score. The variable #
76+ # $numright is the number of correct answers. #
77+ ########################################################
78+ my $numright=0;
79+
80+ foreach my $key (keys %$evaluated_answers) {
81+ # summing the scores is identical to checking each one is correct
82+ $numright += $evaluated_answers->{$key}{score};
83+ }
84+
85+ if ($numright == 6) {
86+ $problem_result->{msg} = 'This attempt scored 100%. Nice work.';
87+ $total = 1;
88+ } elsif ($numright == 5) {
89+ $problem_result->{msg} = 'This attempt scored 50%. You must answer each part correctly for full credit.';
90+ $total = 0.5;
91+ } else {
92+ # the default message will suffice
93+ $total = 0;
94+ }
95+
96+ $problem_result->{score} = $total;
97+
98+ # increase recorded score if the current score is greater.
99+ $problem_state->{recorded_score} = $problem_result->{score}
100+ if $problem_result->{score} > $problem_state->{recorded_score};
124101
102+ $problem_state{num_of_correct_ans}++ if $total == 1;
103+ $problem_state{num_of_incorrect_ans}++ if $total < 1;
104+ return ($problem_result, $problem_state);
125105}
126106
127107################################################################
0 commit comments