[system] / trunk / pg / macros / PGsequentialmacros.pl Repository:
ViewVC logotype

Annotation of /trunk/pg/macros/PGsequentialmacros.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3387 - (view) (download) (as text)

1 : gage 3387
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 :    
60 :     Lists all variables submitted in the problem form. This is used for debugging.
61 :    
62 :     =cut
63 :    
64 : gage 3386 sub listFormVariables {
65 :     # Lists all of the variables filled out on the input form
66 :     # Useful for debugging
67 :     TEXT($HR,"Form variables", );
68 :     TEXT(pretty_print($inputs_ref));
69 :     TEXT("Environment",$BR);
70 :     TEXT(pretty_print(\%envir));
71 :     TEXT($HR);
72 :     }
73 : gage 3387
74 :     =head2 checkAnswer
75 :    
76 :    
77 :     checkAnswer($label);
78 :    
79 :     Checks the answer to the question labeled C<$label>. The result is 1 if the answer is completely correct.
80 :     0 if the answer is wrong or partially wrong and undefined if that question has not yet
81 :     been answered. (Specifically if no answer hash is produced when the answer is evaluated
82 :     by the corresponding answer evaluator.)
83 :    
84 :     =cut
85 :    
86 : gage 3386 sub checkAnswer {
87 :     # checks an answer on a given answer evaluator.
88 :     my $answerName = shift; # get the name of the answer
89 :     my $ans_eval = get_PG_ANSWERS_HASH($answerName),; # get the answer evaluator
90 :     my %options = @_;
91 :     my $debug =($options{debug})?1:0; # give debug information
92 :    
93 :     my $answer = $main::inputs_ref->{$answerName};
94 :     my $response = undef;
95 :     if (defined($answer) and defined($ans_eval) ) {
96 :     my $rh_ans_hash = $ans_eval->evaluate($answer);
97 :     $response = (defined($rh_ans_hash) and 1 == $rh_ans_hash->{score}) ? 1:0;
98 :     TEXT("result of evaluating $answerName",$BR, pretty_print($rh_ans_hash) ) if $debug;
99 :     } else {
100 :     warn "Answer evaluator for answer $answerName is not defined" unless defined($ans_eval);
101 :     # it's ok to have a blank answer.
102 :     }
103 :     return $response; # response is (undef => no answer, 1=> correct answer, 0 => not completely correct
104 :     }
105 : gage 3387
106 :     =head2 listQueuedAnswers
107 :    
108 :     listQueuedAnswers();
109 :    
110 :     Lists the labels of the answer blanks which have been printed so far.
111 :     The return value is a string which can be printed. This is mainly
112 :     used for debugging.
113 :    
114 :     =cut
115 :    
116 :    
117 : gage 3386 sub listQueuedAnswers {
118 :     # lists the names of the answer blanks so far;
119 :     my %pg_answers_hash = get_PG_ANSWERS_HASH();
120 :     join(" ", keys %pg_answers_hash);
121 :     }
122 : gage 3387
123 :     =head2 checkQueuedAnswers
124 :    
125 :     checkQueuedAnswers();
126 :    
127 :     Returns a hash whose key/value pairs are the labels of the questions
128 :     have been printed so far and the scores obtained by evaluating the
129 :     answers to these questions.
130 :    
131 :     =cut
132 :    
133 : gage 3386 sub checkQueuedAnswers {
134 :     # gather all of the answers submitted up to this time
135 :     my %options = @_;
136 :     my $debug = ($options{debug}) ? 1 :0;
137 :     my (%pg_answers_hash) = get_PG_ANSWERS_HASH();
138 :     my %scores=();
139 :     foreach $label (keys %pg_answers_hash) {
140 :     $scores{$label}=checkAnswer($label, debug=>$debug);
141 :     }
142 :     %scores;
143 :     }
144 : gage 3387
145 :     =head2 all_answers_are_correct
146 :    
147 :     all_answers_are_correct();
148 :    
149 :     Returns 1 if there is at least one answer and all of the questions
150 :     printed so far have been answered correctly.
151 :    
152 :     =cut
153 :    
154 : gage 3386 sub all_answers_are_correct{
155 :     # return 1 if all scores are 1, else it returns 0;
156 :     # returns 0 if no answers have been checked yet
157 :     my %scores = checkQueuedAnswers();
158 :     return 0 unless %scores;
159 :     my $result =1;
160 :     foreach my $label (keys %scores) { if (not defined($scores{$label}) or $scores{$label} <1) {$result=0; last;} };
161 :     $result;
162 :     }
163 : gage 3387
164 :     =head2 get_incorrect_answers
165 :    
166 :     get_incorrect_answers();
167 :    
168 :     Returns a list of labels of questions which have been printed and have
169 :     been answered incorrectly. This list does NOT include blank or undefined
170 :     answers. It's possible for the returned list to be empty AND for all_answers_are_correct()
171 :     to return false.
172 :    
173 :     =cut
174 :    
175 : gage 3386 sub get_incorrect_answers {
176 :     # returns only incorrect answers, not blank or undefined answers.
177 :     my %scores = checkQueuedAnswers();
178 :     my @incorrect = ();
179 :     foreach my $label (keys %scores) {push( @incorrect, $label)
180 :     unless (not defined($scores{$label}) or $scores{$label}==1 )
181 :     };
182 :     @incorrect;
183 :     }
184 :    
185 :     1;
186 :    
187 :    

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9