Parent Directory
|
Revision Log
Made listVariables and listFormVariables synonyms for listing all variables available to the problem.
1 2 =head1 NAME 3 4 PGsequentialmacros.pl 5 6 Provides support for writing sequential problems, where certain parts 7 of the problem are hidden until earlier questions are answered correctly. 8 9 10 =head1 SYNPOSIS 11 12 The basic sequential problem structure: 13 14 DOCUMENT(); 15 loadMacros(.....); 16 ## first segment ## 17 BEGIN_TEXT 18 The first question: Enter \(sin(0) = \) \{ans_rule\}. 19 END_TEXT 20 ANS(num_cmp(0)); 21 if (@incorrect_answers = get_incorrect_answers( ) ) { 22 TEXT( "These answers are not correct ", join(" ",@incorrect_answers),$BR); 23 foreach my $label (@incorrect_answers) { 24 checkAnswer($label,debug=>1); 25 } 26 } 27 if (all_answers_are_correct() ) { 28 TEXT("$PAR Right! Now for the next part of the problem"); 29 } else { 30 STOP_RENDERING(); 31 } 32 ## second segment ## 33 .... 34 if (@incorrect_answers = get_incorrect_answers( ) ) { 35 TEXT( "These answers are not correct ", join(" ",@incorrect_answers),$BR); 36 foreach my $label (@incorrect_answers) { 37 checkAnswer($label,debug=>1); 38 } 39 } 40 if (all_answers_are_correct() ) { 41 TEXT("$PAR Right! Now for the next part of the problem"); 42 } else { 43 STOP_RENDERING(); 44 } 45 ## third segment ## 46 ENDDOCUMENT() # must be the last statement in the problem 47 48 49 50 =head1 DESCRIPTION 51 52 53 =cut 54 55 56 =head2 listFormVariables 57 58 listFormVariables(); 59 listVariables(); 60 61 Lists all variables submitted in the problem form and all variables in the 62 the Problem environment. This is used for debugging. 63 64 =cut 65 66 sub listVariables { 67 listFormVariables(@_); 68 } 69 70 sub listFormVariables { 71 # Lists all of the variables filled out on the input form 72 # Useful for debugging 73 TEXT($HR,"Form variables", ); 74 TEXT(pretty_print($inputs_ref)); 75 # list the environment variables 76 TEXT("Environment",$BR); 77 TEXT(pretty_print(\%envir)); 78 TEXT($HR); 79 } 80 81 =head2 checkAnswer 82 83 84 checkAnswer($label); 85 86 Checks the answer to the question labeled C<$label>. The result is 1 if the answer is completely correct. 87 0 if the answer is wrong or partially wrong and undefined if that question has not yet 88 been answered. (Specifically if no answer hash is produced when the answer is evaluated 89 by the corresponding answer evaluator.) 90 91 =cut 92 93 sub checkAnswer { 94 # checks an answer on a given answer evaluator. 95 my $answerName = shift; # get the name of the answer 96 my $ans_eval = get_PG_ANSWERS_HASH($answerName),; # get the answer evaluator 97 my %options = @_; 98 my $debug =($options{debug})?1:0; # give debug information 99 100 my $answer = $main::inputs_ref->{$answerName}; 101 my $response = undef; 102 if (defined($answer) and defined($ans_eval) ) { 103 my $rh_ans_hash = $ans_eval->evaluate($answer); 104 $response = (defined($rh_ans_hash) and 1 == $rh_ans_hash->{score}) ? 1:0; 105 TEXT("result of evaluating $answerName",$BR, pretty_print($rh_ans_hash) ) if $debug; 106 } else { 107 warn "Answer evaluator for answer $answerName is not defined" unless defined($ans_eval); 108 # it's ok to have a blank answer. 109 } 110 return $response; # response is (undef => no answer, 1=> correct answer, 0 => not completely correct 111 } 112 113 =head2 listQueuedAnswers 114 115 listQueuedAnswers(); 116 117 Lists the labels of the answer blanks which have been printed so far. 118 The return value is a string which can be printed. This is mainly 119 used for debugging. 120 121 =cut 122 123 124 sub listQueuedAnswers { 125 # lists the names of the answer blanks so far; 126 my %pg_answers_hash = get_PG_ANSWERS_HASH(); 127 join(" ", keys %pg_answers_hash); 128 } 129 130 =head2 checkQueuedAnswers 131 132 checkQueuedAnswers(); 133 134 Returns a hash whose key/value pairs are the labels of the questions 135 have been printed so far and the scores obtained by evaluating the 136 answers to these questions. 137 138 =cut 139 140 sub checkQueuedAnswers { 141 # gather all of the answers submitted up to this time 142 my %options = @_; 143 my $debug = ($options{debug}) ? 1 :0; 144 my (%pg_answers_hash) = get_PG_ANSWERS_HASH(); 145 my %scores=(); 146 foreach $label (keys %pg_answers_hash) { 147 $scores{$label}=checkAnswer($label, debug=>$debug); 148 } 149 %scores; 150 } 151 152 =head2 all_answers_are_correct 153 154 all_answers_are_correct(); 155 156 Returns 1 if there is at least one answer and all of the questions 157 printed so far have been answered correctly. 158 159 =cut 160 161 sub all_answers_are_correct{ 162 # return 1 if all scores are 1, else it returns 0; 163 # returns 0 if no answers have been checked yet 164 my %scores = checkQueuedAnswers(); 165 return 0 unless %scores; 166 my $result =1; 167 foreach my $label (keys %scores) { if (not defined($scores{$label}) or $scores{$label} <1) {$result=0; last;} }; 168 $result; 169 } 170 171 =head2 get_incorrect_answers 172 173 get_incorrect_answers(); 174 175 Returns a list of labels of questions which have been printed and have 176 been answered incorrectly. This list does NOT include blank or undefined 177 answers. It's possible for the returned list to be empty AND for all_answers_are_correct() 178 to return false. 179 180 =cut 181 182 sub get_incorrect_answers { 183 # returns only incorrect answers, not blank or undefined answers. 184 my %scores = checkQueuedAnswers(); 185 my @incorrect = (); 186 foreach my $label (keys %scores) {push( @incorrect, $label) 187 unless (not defined($scores{$label}) or $scores{$label}==1 ) 188 }; 189 @incorrect; 190 } 191 192 1; 193 194
| aubreyja at gmail dot com | ViewVC Help |
| Powered by ViewVC 1.0.9 |