| 1 | ################################################################################ |
1 | ################################################################################ |
| 2 | # WeBWorK mod_perl (c) 1995-2002 WeBWorK Team, Univeristy of Rochester |
2 | # WeBWorK mod_perl (c) 2000-2002 WeBWorK Project |
| 3 | # $Id$ |
3 | # $Id$ |
| 4 | ################################################################################ |
4 | ################################################################################ |
| 5 | |
5 | |
| 6 | package WeBWorK::ContentGenerator::Problem; |
6 | package WeBWorK::ContentGenerator::Problem; |
| 7 | |
7 | |
| … | |
… | |
| 13 | |
13 | |
| 14 | use strict; |
14 | use strict; |
| 15 | use warnings; |
15 | use warnings; |
| 16 | use base qw(WeBWorK::ContentGenerator); |
16 | use base qw(WeBWorK::ContentGenerator); |
| 17 | use CGI qw(); |
17 | use CGI qw(); |
|
|
18 | use File::Temp qw(tempdir); |
| 18 | use WeBWorK::Form; |
19 | use WeBWorK::Form; |
| 19 | use WeBWorK::PG; |
20 | use WeBWorK::PG; |
|
|
21 | use WeBWorK::PG::IO; |
| 20 | use WeBWorK::Utils qw(ref2string encodeAnswers decodeAnswers); |
22 | use WeBWorK::Utils qw(writeLog encodeAnswers decodeAnswers ref2string); |
| 21 | |
|
|
| 22 | # TODO: |
|
|
| 23 | # :) enforce permissions for showCorrectAnswers and showSolutions |
|
|
| 24 | # (use $PRIV = $canPRIV && ($wantPRIV || $mustPRIV) -- cool syntax!) |
|
|
| 25 | # :) if answers were not submitted and there are student answers in the DB, |
|
|
| 26 | # decode them and put them into $formFields for the translator |
|
|
| 27 | # :) store submitted answers hash in database for sticky answers |
|
|
| 28 | # :) deal with the results of answer evaluation and grading :p |
|
|
| 29 | # :) introduce a recordAnswers option, which works on the same principle as |
|
|
| 30 | # the other permission-based options |
|
|
| 31 | # 7. make warnings work |
|
|
| 32 | |
23 | |
| 33 | ############################################################ |
24 | ############################################################ |
| 34 | # |
25 | # |
| 35 | # user |
26 | # user |
| 36 | # key |
27 | # key |
| … | |
… | |
| 46 | # redisplay - name of the "Redisplay Problem" button |
37 | # redisplay - name of the "Redisplay Problem" button |
| 47 | # submitAnswers - name of "Submit Answers" button |
38 | # submitAnswers - name of "Submit Answers" button |
| 48 | # |
39 | # |
| 49 | ############################################################ |
40 | ############################################################ |
| 50 | |
41 | |
| 51 | sub prepare { |
42 | sub pre_header_initialize { |
| 52 | my ($self, $setName, $problemNumber) = @_; |
43 | my ($self, $setName, $problemNumber) = @_; |
| 53 | my $courseEnv = $self->{courseEnvironment}; |
44 | my $courseEnv = $self->{courseEnvironment}; |
| 54 | my $r = $self->{r}; |
45 | my $r = $self->{r}; |
| 55 | my $userName = $r->param('user'); |
46 | my $userName = $r->param('user'); |
| 56 | |
47 | |
| 57 | # fix format of setName and problem |
|
|
| 58 | $setName =~ s/^set//; |
|
|
| 59 | $problemNumber =~ s/^prob//; |
|
|
| 60 | |
|
|
| 61 | ##### database setup ##### |
48 | ##### database setup ##### |
| 62 | |
49 | |
| 63 | my $cldb = WeBWorK::DB::Classlist->new($courseEnv); |
50 | my $cldb = WeBWorK::DB::Classlist->new($courseEnv); |
| 64 | my $wwdb = WeBWorK::DB::WW->new($courseEnv); |
51 | my $wwdb = WeBWorK::DB::WW->new($courseEnv); |
| 65 | my $authdb = WeBWorK::DB::Auth->new($courseEnv); |
52 | my $authdb = WeBWorK::DB::Auth->new($courseEnv); |
| 66 | |
53 | |
| 67 | my $user = $cldb->getUser($userName); |
54 | my $user = $cldb->getUser($userName); |
| 68 | my $set = $wwdb->getSet($userName, $setName); |
55 | my $set = $wwdb->getSet($userName, $setName); |
| 69 | my $problem = $wwdb->getProblem($userName, $setName, $problemNumber); |
56 | my $problem = $wwdb->getProblem($userName, $setName, $problemNumber); |
|
|
57 | my $psvn = $wwdb->getPSVN($userName, $setName); |
| 70 | my $permissionLevel = $authdb->getPermissions($userName); |
58 | my $permissionLevel = $authdb->getPermissions($userName); |
| 71 | |
59 | |
| 72 | ##### form processing ##### |
60 | ##### form processing ##### |
| 73 | |
61 | |
| 74 | # set options from form fields (see comment at top of file for names) |
62 | # set options from form fields (see comment at top of file for names) |
| 75 | my $displayMode = $r->param("displayMode") || $courseEnv->{pg}->{options}->{displayMode}; |
63 | my $displayMode = $r->param("displayMode") || $courseEnv->{pg}->{options}->{displayMode}; |
| 76 | my $redisplay = $r->param("redisplay"); |
64 | my $redisplay = $r->param("redisplay"); |
| 77 | my $submitAnswers = $r->param("submitAnswers"); |
65 | my $submitAnswers = $r->param("submitAnswers"); |
|
|
66 | my $previewAnswers = $r->param("previewAnswers"); |
| 78 | |
67 | |
| 79 | # coerce form fields into CGI::Vars format |
68 | # coerce form fields into CGI::Vars format |
| 80 | my $formFields = { WeBWorK::Form->new_from_paramable($r)->Vars }; |
69 | my $formFields = { WeBWorK::Form->new_from_paramable($r)->Vars }; |
| 81 | |
70 | |
| 82 | ##### permissions ##### |
71 | ##### permissions ##### |
| … | |
… | |
| 110 | # num_correct+num_incorrect+1 -- as this happens before updating $problem |
99 | # num_correct+num_incorrect+1 -- as this happens before updating $problem |
| 111 | ); |
100 | ); |
| 112 | |
101 | |
| 113 | # final values for options |
102 | # final values for options |
| 114 | my %will; |
103 | my %will; |
| 115 | foreach(keys %must) { |
104 | foreach (keys %must) { |
| 116 | $will{$_} = $can{$_} && ($want{$_} || $must{$_}); |
105 | $will{$_} = $can{$_} && ($want{$_} || $must{$_}); |
|
|
106 | #warn "$_: can? $can{$_} want? $want{$_} must? $must{$_} will? $will{$_}\n"; |
| 117 | } |
107 | } |
| 118 | |
108 | |
| 119 | ##### sticky answers ##### |
109 | ##### sticky answers ##### |
| 120 | |
110 | |
| 121 | if (not $submitAnswers and $will{showOldAnswers}) { |
111 | if (not $submitAnswers and $will{showOldAnswers}) { |
| … | |
… | |
| 126 | |
116 | |
| 127 | ##### translation ##### |
117 | ##### translation ##### |
| 128 | |
118 | |
| 129 | my $pg = WeBWorK::PG->new( |
119 | my $pg = WeBWorK::PG->new( |
| 130 | $courseEnv, |
120 | $courseEnv, |
| 131 | $r->param('user'), |
121 | $user, |
| 132 | $r->param('key'), |
122 | $r->param('key'), |
| 133 | $setName, |
123 | $set, |
| 134 | $problemNumber, |
124 | $problem, |
|
|
125 | $psvn, |
|
|
126 | $formFields, |
| 135 | { # translation options |
127 | { # translation options |
| 136 | displayMode => $displayMode, |
128 | displayMode => $displayMode, |
| 137 | showHints => $will{showHints}, |
129 | showHints => $will{showHints}, |
| 138 | showSolutions => $will{showSolutions}, |
130 | showSolutions => $will{showSolutions}, |
| 139 | refreshMath2img => $will{showHints} || $will{showSolutions}, |
131 | refreshMath2img => $will{showHints} || $will{showSolutions}, |
| 140 | # try leaving processAnswers on all the time? |
132 | # try leaving processAnswers on all the time? |
| 141 | processAnswers => 1, #$submitAnswers ? 1 : 0, |
133 | processAnswers => 1, #$submitAnswers ? 1 : 0, |
| 142 | }, |
134 | }, |
| 143 | $formFields |
|
|
| 144 | ); |
135 | ); |
| 145 | |
136 | |
| 146 | ##### store fields ##### |
137 | ##### store fields ##### |
| 147 | |
138 | |
| 148 | $self->{cldb} = $cldb; |
139 | $self->{cldb} = $cldb; |
| … | |
… | |
| 152 | $self->{user} = $user; |
143 | $self->{user} = $user; |
| 153 | $self->{set} = $set; |
144 | $self->{set} = $set; |
| 154 | $self->{problem} = $problem; |
145 | $self->{problem} = $problem; |
| 155 | $self->{permissionLevel} = $permissionLevel; |
146 | $self->{permissionLevel} = $permissionLevel; |
| 156 | |
147 | |
| 157 | $self->{displayMode} = $displayMode; |
148 | $self->{displayMode} = $displayMode; |
| 158 | $self->{redisplay} = $redisplay; |
149 | $self->{redisplay} = $redisplay; |
| 159 | $self->{submitAnswers} = $submitAnswers; |
150 | $self->{submitAnswers} = $submitAnswers; |
|
|
151 | $self->{previewAnswers} = $previewAnswers; |
| 160 | $self->{formFields} = $formFields; |
152 | $self->{formFields} = $formFields; |
| 161 | |
153 | |
| 162 | $self->{want} = \%want; |
154 | $self->{want} = \%want; |
| 163 | $self->{must} = \%must; |
155 | $self->{must} = \%must; |
| 164 | $self->{can} = \%can; |
156 | $self->{can} = \%can; |
| 165 | $self->{will} = \%will; |
157 | $self->{will} = \%will; |
| 166 | |
158 | |
| 167 | $self->{pg} = $pg; |
159 | $self->{pg} = $pg; |
| 168 | } |
160 | } |
| 169 | |
161 | |
|
|
162 | sub if_warnings($$) { |
|
|
163 | my ($self, $arg) = @_; |
|
|
164 | return $self->{pg}->{warnings} ne ""; |
|
|
165 | } |
|
|
166 | |
|
|
167 | sub if_errors($$) { |
|
|
168 | my ($self, $arg) = @_; |
|
|
169 | return $self->{pg}->{flags}->{error_flag}; |
|
|
170 | } |
|
|
171 | |
|
|
172 | sub head { |
|
|
173 | my $self = shift; |
|
|
174 | |
|
|
175 | return $self->{pg}->{head_text} if $self->{pg}->{head_text}; |
|
|
176 | } |
|
|
177 | |
|
|
178 | sub path { |
|
|
179 | my $self = shift; |
|
|
180 | my $args = $_[-1]; |
|
|
181 | my $setName = $self->{set}->id; |
|
|
182 | my $problemNumber = $self->{problem}->id; |
|
|
183 | |
|
|
184 | my $ce = $self->{courseEnvironment}; |
|
|
185 | my $root = $ce->{webworkURLs}->{root}; |
|
|
186 | my $courseName = $ce->{courseName}; |
|
|
187 | return $self->pathMacro($args, |
|
|
188 | "Home" => "$root", |
|
|
189 | $courseName => "$root/$courseName", |
|
|
190 | $setName => "$root/$courseName/$setName", |
|
|
191 | "Problem $problemNumber" => "", |
|
|
192 | ); |
|
|
193 | } |
|
|
194 | |
|
|
195 | sub siblings { |
|
|
196 | my $self = shift; |
|
|
197 | my $setName = $self->{set}->id; |
|
|
198 | my $problemNumber = $self->{problem}->id; |
|
|
199 | |
|
|
200 | my $ce = $self->{courseEnvironment}; |
|
|
201 | my $root = $ce->{webworkURLs}->{root}; |
|
|
202 | my $courseName = $ce->{courseName}; |
|
|
203 | |
|
|
204 | print CGI::strong("Problems"), CGI::br(); |
|
|
205 | |
|
|
206 | my $wwdb = $self->{wwdb}; |
|
|
207 | my $user = $self->{r}->param("user"); |
|
|
208 | my @problems; |
|
|
209 | push @problems, $wwdb->getProblem($user, $setName, $_) |
|
|
210 | foreach ($wwdb->getProblems($user, $setName)); |
|
|
211 | foreach my $problem (sort { $a->id <=> $b->id } @problems) { |
|
|
212 | print CGI::a({-href=>"$root/$courseName/$setName/".$problem->id."/?" |
|
|
213 | . $self->url_authen_args}, "Problem ".$problem->id), CGI::br(); |
|
|
214 | } |
|
|
215 | } |
|
|
216 | |
|
|
217 | sub nav { |
|
|
218 | my $self = shift; |
|
|
219 | my $args = $_[-1]; |
|
|
220 | my $setName = $self->{set}->id; |
|
|
221 | my $problemNumber = $self->{problem}->id; |
|
|
222 | |
|
|
223 | my $ce = $self->{courseEnvironment}; |
|
|
224 | my $root = $ce->{webworkURLs}->{root}; |
|
|
225 | my $courseName = $ce->{courseName}; |
|
|
226 | |
|
|
227 | my $wwdb = $self->{wwdb}; |
|
|
228 | my $user = $self->{r}->param("user"); |
|
|
229 | |
|
|
230 | my @links = ("Problem List" => "$root/$courseName/$setName"); |
|
|
231 | |
|
|
232 | my $prevProblem = $wwdb->getProblem($user, $setName, $problemNumber-1); |
|
|
233 | my $nextProblem = $wwdb->getProblem($user, $setName, $problemNumber+1); |
|
|
234 | unshift @links, "Previous Problem" => $prevProblem |
|
|
235 | ? "$root/$courseName/$setName/".$prevProblem->id |
|
|
236 | : ""; |
|
|
237 | push @links, "Next Problem" => $nextProblem |
|
|
238 | ? "$root/$courseName/$setName/".$nextProblem->id |
|
|
239 | : ""; |
|
|
240 | |
|
|
241 | return $self->navMacro($args, @links); |
|
|
242 | } |
|
|
243 | |
| 170 | sub title { |
244 | sub title { |
| 171 | my $self = shift; |
245 | my $self = shift; |
| 172 | #return "Set " . $self->{set}->id . " problem " . $self->{problem}->id; |
246 | my $setName = $self->{set}->id; |
| 173 | return "hold on a sec"; |
247 | my $problemNumber = $self->{problem}->id; |
|
|
248 | |
|
|
249 | return "$setName : Problem $problemNumber"; |
| 174 | } |
250 | } |
| 175 | |
251 | |
| 176 | sub body { |
252 | sub body { |
| 177 | my $self = shift; |
253 | my $self = shift; |
| 178 | |
254 | |
| 179 | $self->prepare(@_); |
|
|
| 180 | |
|
|
| 181 | # unpack some useful variables |
255 | # unpack some useful variables |
| 182 | my $r = $self->{r}; |
256 | my $r = $self->{r}; |
| 183 | my $wwdb = $self->{wwdb}; |
257 | my $wwdb = $self->{wwdb}; |
| 184 | my $set = $self->{set}; |
258 | my $set = $self->{set}; |
| 185 | my $problem = $self->{problem}; |
259 | my $problem = $self->{problem}; |
|
|
260 | my $permissionLevel = $self->{permissionLevel}; |
| 186 | my $submitAnswers = $self->{submitAnswers}; |
261 | my $submitAnswers = $self->{submitAnswers}; |
|
|
262 | my $previewAnswers = $self->{previewAnswers}; |
| 187 | my %will = %{ $self->{will} }; |
263 | my %will = %{ $self->{will} }; |
| 188 | my $pg = $self->{pg}; |
264 | my $pg = $self->{pg}; |
| 189 | |
265 | |
| 190 | ##### translation errors? ##### |
266 | ##### translation errors? ##### |
| 191 | |
267 | |
| 192 | if ($pg->{flags}->{error_flag}) { |
268 | if ($pg->{flags}->{error_flag}) { |
| 193 | print translationError($pg->{errors}, $pg->{body_text}); |
269 | return translationError($pg->{errors}, $pg->{body_text}); |
| 194 | return ""; |
|
|
| 195 | } |
270 | } |
| 196 | |
271 | |
| 197 | ##### answer processing ##### |
272 | ##### answer processing ##### |
| 198 | |
273 | |
| 199 | # if answers were submitted: |
274 | # if answers were submitted: |
| … | |
… | |
| 212 | if ($will{recordAnswers}) { |
287 | if ($will{recordAnswers}) { |
| 213 | $problem->attempted(1); |
288 | $problem->attempted(1); |
| 214 | $problem->status($pg->{state}->{recorded_score}); |
289 | $problem->status($pg->{state}->{recorded_score}); |
| 215 | $problem->num_correct($pg->{state}->{num_of_correct_ans}); |
290 | $problem->num_correct($pg->{state}->{num_of_correct_ans}); |
| 216 | $problem->num_incorrect($pg->{state}->{num_of_incorrect_ans}); |
291 | $problem->num_incorrect($pg->{state}->{num_of_incorrect_ans}); |
| 217 | #warn "Would have stored the following:\n", |
|
|
| 218 | # $problem->toString, "\n"; |
|
|
| 219 | $wwdb->setProblem($problem); |
292 | $wwdb->setProblem($problem); |
|
|
293 | # write to the transaction log, just to make sure |
|
|
294 | writeLog($self->{courseEnvironment}, "transaction", |
|
|
295 | $problem->id."\t". |
|
|
296 | $problem->set_id."\t". |
|
|
297 | $problem->login_id."\t". |
|
|
298 | $problem->source_file."\t". |
|
|
299 | $problem->value."\t". |
|
|
300 | $problem->max_attempts."\t". |
|
|
301 | $problem->problem_seed."\t". |
|
|
302 | $problem->status."\t". |
|
|
303 | $problem->attempted."\t". |
|
|
304 | $problem->last_answer."\t". |
|
|
305 | $problem->num_correct."\t". |
|
|
306 | $problem->num_incorrect |
|
|
307 | ); |
| 220 | } |
308 | } |
| 221 | } |
309 | } |
| 222 | |
310 | |
| 223 | ##### output ##### |
311 | ##### output ##### |
| 224 | |
312 | |
| 225 | # attempt summary |
313 | # attempt summary |
| 226 | if ($submitAnswers or $will{showCorrectAnswers}) { |
314 | if ($submitAnswers or $will{showCorrectAnswers}) { |
| 227 | # print this if user submitted answers OR requested correct answers |
315 | # print this if user submitted answers OR requested correct answers |
| 228 | print attemptResults($pg, $submitAnswers, $will{showCorrectAnswers}, |
316 | print $self->attemptResults($pg, $submitAnswers, $will{showCorrectAnswers}, |
| 229 | $pg->{flags}->{showPartialCorrectAnswers}); |
317 | $pg->{flags}->{showPartialCorrectAnswers}); |
|
|
318 | } elsif ($previewAnswers) { |
|
|
319 | # print this if user previewed answers |
|
|
320 | print $self->attemptResults($pg, 1, 0, 0); |
|
|
321 | # don't show correctness |
|
|
322 | # don't show correct answers |
| 230 | } |
323 | } |
| 231 | |
324 | |
| 232 | # score summary |
325 | # score summary |
| 233 | my $attempts = $problem->num_correct + $problem->num_incorrect; |
326 | my $attempts = $problem->num_correct + $problem->num_incorrect; |
| 234 | my $attemptsNoun = $attempts != 1 ? "times" : "time"; |
327 | my $attemptsNoun = $attempts != 1 ? "times" : "time"; |
| … | |
… | |
| 240 | $attemptsLeftNoun = "attempts"; |
333 | $attemptsLeftNoun = "attempts"; |
| 241 | } else { |
334 | } else { |
| 242 | $attemptsLeft = $problem->max_attempts - $attempts; |
335 | $attemptsLeft = $problem->max_attempts - $attempts; |
| 243 | $attemptsLeftNoun = $attemptsLeft == 1 ? "attempt" : "attempts"; |
336 | $attemptsLeftNoun = $attemptsLeft == 1 ? "attempt" : "attempts"; |
| 244 | } |
337 | } |
|
|
338 | my $setClosedMessage; |
|
|
339 | if (time < $set->open_date or time > $set->due_date) { |
|
|
340 | $setClosedMessage = "This problem set is closed."; |
|
|
341 | if ($permissionLevel > 0) { |
|
|
342 | $setClosedMessage .= " Since you are a privileged user, additional attempts will be recorded."; |
|
|
343 | } else { |
|
|
344 | $setClosedMessage .= " Additional attempts will not be recorded."; |
|
|
345 | } |
|
|
346 | } |
| 245 | print CGI::p( |
347 | print CGI::p( |
| 246 | "You have attempted this problem $attempts $attemptsNoun.", CGI::br(), |
348 | "You have attempted this problem $attempts $attemptsNoun.", CGI::br(), |
| 247 | $problem->attempted |
349 | $problem->attempted |
| 248 | ? "Your recorded score is $lastScore." . CGI::br() |
350 | ? "Your recorded score is $lastScore." . CGI::br() |
| 249 | : "", |
351 | : "", |
| 250 | "You have $attemptsLeft $attemptsLeftNoun remaining." |
352 | "You have $attemptsLeft $attemptsLeftNoun remaining.", CGI::br(), |
|
|
353 | $setClosedMessage, |
| 251 | ); |
354 | ); |
| 252 | |
|
|
| 253 | # BY THE WAY.......... |
|
|
| 254 | # we have to figure out some way to tell the student if their NEW answer, |
|
|
| 255 | # on THIS attempt, has been recorded. however, this is decided in part by |
|
|
| 256 | # the grader, so is there any way for us to know? we can rule out several |
|
|
| 257 | # cases where the answer is NOT being recorded, because of things decided |
|
|
| 258 | # in &canRecordAnswers... |
|
|
| 259 | |
355 | |
| 260 | print CGI::hr(); |
356 | print CGI::hr(); |
| 261 | |
357 | |
| 262 | # main form |
358 | # main form |
| 263 | print |
359 | print |
| 264 | CGI::startform("POST", $r->uri), |
360 | CGI::startform("POST", $r->uri), |
| 265 | $self->hidden_authen_fields, |
361 | $self->hidden_authen_fields, |
| 266 | CGI::p(CGI::i($pg->{result}->{msg})), |
362 | CGI::p(CGI::i($pg->{result}->{msg})), |
| 267 | CGI::p($pg->{body_text}), |
363 | CGI::p($pg->{body_text}), |
|
|
364 | CGI::p( |
| 268 | CGI::p(CGI::submit(-name=>"submitAnswers", -label=>"Submit Answers")), |
365 | CGI::submit(-name=>"submitAnswers", -label=>"Submit Answers"), |
|
|
366 | CGI::submit(-name=>"previewAnswers", -label=>"Preview Answers"), |
|
|
367 | ), |
| 269 | $self->viewOptions, |
368 | $self->viewOptions(), |
| 270 | CGI::endform(); |
369 | CGI::endform(); |
|
|
370 | |
|
|
371 | # feedback form |
|
|
372 | my $ce = $self->{courseEnvironment}; |
|
|
373 | my $root = $ce->{webworkURLs}->{root}; |
|
|
374 | my $courseName = $ce->{courseName}; |
|
|
375 | my $feedbackURL = "$root/$courseName/feedback/"; |
|
|
376 | print |
|
|
377 | CGI::startform("POST", $feedbackURL), |
|
|
378 | $self->hidden_authen_fields, |
|
|
379 | CGI::hidden("module", __PACKAGE__), |
|
|
380 | CGI::hidden("set", $set->id), |
|
|
381 | CGI::hidden("problem", $problem->id), |
|
|
382 | CGI::hidden("displayMode", $self->{displayMode}), |
|
|
383 | CGI::hidden("showOldAnswers", $will{showOldAnswers}), |
|
|
384 | CGI::hidden("showCorrectAnswers", $will{showCorrectAnswers}), |
|
|
385 | CGI::hidden("showHints", $will{showHints}), |
|
|
386 | CGI::hidden("showSolutions", $will{showSolutions}), |
|
|
387 | CGI::p({-align=>"right"}, |
|
|
388 | CGI::submit(-name=>"feedbackForm", -label=>"Send Feedback") |
|
|
389 | ), |
|
|
390 | CGI::endform(); |
|
|
391 | |
|
|
392 | # warning output |
|
|
393 | if ($pg->{warnings} ne "") { |
|
|
394 | print CGI::hr(), warningOutput($pg->{warnings}); |
|
|
395 | } |
| 271 | |
396 | |
| 272 | # debugging stuff |
397 | # debugging stuff |
| 273 | #print |
398 | #print |
| 274 | # hr(), |
399 | # CGI::hr(), |
| 275 | # h2("debugging information"), |
400 | # CGI::h2("debugging information"), |
| 276 | # h3("form fields"), |
401 | # CGI::h3("form fields"), |
| 277 | # ref2string($formFields), |
402 | # ref2string($self->{formFields}), |
| 278 | # h3("user object"), |
403 | # CGI::h3("user object"), |
| 279 | # ref2string($user), |
404 | # ref2string($self->{user}), |
| 280 | # h3("set object"), |
405 | # CGI::h3("set object"), |
| 281 | # ref2string($set), |
406 | # ref2string($set), |
| 282 | # h3("problem object"), |
407 | # CGI::h3("problem object"), |
| 283 | # ref2string($problem), |
408 | # ref2string($problem), |
| 284 | # h3("PG object"), |
409 | # CGI::h3("PG object"), |
| 285 | # ref2string($pg, {'WeBWorK::PG::Translator' => 1}); |
410 | # ref2string($pg, {'WeBWorK::PG::Translator' => 1}); |
| 286 | |
411 | |
| 287 | return ""; |
412 | return ""; |
| 288 | } |
413 | } |
| 289 | |
414 | |
| 290 | ##### output utilities ##### |
415 | ##### output utilities ##### |
| 291 | |
416 | |
|
|
417 | # this is used by ProblemSet.pm too, so don't fuck it up |
| 292 | sub translationError($$) { |
418 | sub translationError($$) { |
| 293 | my ($error, $details) = @_; |
419 | my ($error, $details) = @_; |
| 294 | return |
420 | return |
| 295 | CGI::h2("Software Error"), |
421 | CGI::h2("Software Error"), |
| 296 | CGI::p(<<EOF), |
422 | CGI::p(<<EOF), |
| … | |
… | |
| 301 | EOF |
427 | EOF |
| 302 | CGI::h3("Error messages"), CGI::blockquote(CGI::pre($error)), |
428 | CGI::h3("Error messages"), CGI::blockquote(CGI::pre($error)), |
| 303 | CGI::h3("Error context"), CGI::blockquote(CGI::pre($details)); |
429 | CGI::h3("Error context"), CGI::blockquote(CGI::pre($details)); |
| 304 | } |
430 | } |
| 305 | |
431 | |
|
|
432 | # this is used by ProblemSet.pm too, so don't fuck it up |
|
|
433 | sub warningOutput($) { |
|
|
434 | my $warnings = shift; |
|
|
435 | |
|
|
436 | return |
|
|
437 | CGI::h2("Software Warnings"), |
|
|
438 | CGI::p(<<EOF), |
|
|
439 | WeBWorK has encountered warnings while attempting to process this problem. |
|
|
440 | It is likely that this indicates an error or ambiguity in the problem itself. |
|
|
441 | If you are a student, contact your professor to have the problem corrected. |
|
|
442 | If you are a professor, please consut the error output below for more informaiton. |
|
|
443 | EOF |
|
|
444 | CGI::h3("Warning messages"), |
|
|
445 | CGI::blockquote(CGI::pre($warnings)), |
|
|
446 | ; |
|
|
447 | } |
|
|
448 | |
| 306 | sub attemptResults($$$) { |
449 | sub attemptResults($$$$$) { |
|
|
450 | my $self = shift; |
| 307 | my $pg = shift; |
451 | my $pg = shift; |
| 308 | my $showAttemptAnswers = shift; |
452 | my $showAttemptAnswers = shift; |
| 309 | my $showCorrectAnswers = shift; |
453 | my $showCorrectAnswers = shift; |
| 310 | my $showAttemptResults = $showAttemptAnswers && shift; |
454 | my $showAttemptResults = $showAttemptAnswers && shift; |
| 311 | my $problemResult = $pg->{result}; # the overall result of the problem |
455 | my $problemResult = $pg->{result}; # the overall result of the problem |
| 312 | my @answerNames = @{ $pg->{flags}->{ANSWER_ENTRY_ORDER} }; |
456 | my @answerNames = @{ $pg->{flags}->{ANSWER_ENTRY_ORDER} }; |
| 313 | |
457 | |
| 314 | my $header = CGI::th("answer"); |
458 | my $header = CGI::th("answer"); |
| 315 | $header .= $showAttemptAnswers ? CGI::th("attempt") : ""; |
459 | $header .= $showAttemptAnswers ? CGI::th("attempt") : ""; |
|
|
460 | $header .= $showAttemptAnswers ? CGI::th("preview") : ""; |
| 316 | $header .= $showCorrectAnswers ? CGI::th("correct") : ""; |
461 | $header .= $showCorrectAnswers ? CGI::th("correct") : ""; |
| 317 | $header .= $showAttemptResults ? CGI::th("result") : ""; |
462 | $header .= $showAttemptResults ? CGI::th("result") : ""; |
| 318 | $header .= $showAttemptAnswers ? CGI::th("messages") : ""; |
463 | $header .= $showAttemptAnswers ? CGI::th("messages") : ""; |
| 319 | my @tableRows = ( $header ); |
464 | my @tableRows = ( $header ); |
| 320 | my $numCorrect; |
465 | my $numCorrect; |
| 321 | foreach my $name (@answerNames) { |
466 | foreach my $name (@answerNames) { |
| 322 | my $answerResult = $pg->{answers}->{$name}; |
467 | my $answerResult = $pg->{answers}->{$name}; |
| 323 | my $studentAnswer = $answerResult->{student_ans}; # original_student_ans |
468 | my $studentAnswer = $answerResult->{student_ans}; # original_student_ans |
|
|
469 | my $preview = $self->previewAnswer($answerResult); |
| 324 | my $correctAnswer = $answerResult->{correct_ans}; |
470 | my $correctAnswer = $answerResult->{correct_ans}; |
| 325 | my $answerScore = $answerResult->{score}; |
471 | my $answerScore = $answerResult->{score}; |
| 326 | my $answerMessage = $showAttemptAnswers ? $answerResult->{ans_message} : ""; |
472 | my $answerMessage = $showAttemptAnswers ? $answerResult->{ans_message} : ""; |
| 327 | |
473 | |
| 328 | $numCorrect += $answerScore > 0; |
474 | $numCorrect += $answerScore > 0; |
| 329 | my $resultString = $answerScore ? "correct :^)" : "incorrect >:("; |
475 | my $resultString = $answerScore ? "correct" : "incorrect"; |
|
|
476 | |
|
|
477 | # get rid of the goofy prefix on the answer names (supposedly, the format |
|
|
478 | # of the answer names is changeable. this only fixes |
|
|
479 | $name =~ s/^AnSwEr//; |
| 330 | |
480 | |
| 331 | my $row = CGI::td($name); |
481 | my $row = CGI::td($name); |
| 332 | $row .= $showAttemptAnswers ? CGI::td($studentAnswer) : ""; |
482 | $row .= $showAttemptAnswers ? CGI::td($studentAnswer) : ""; |
|
|
483 | $row .= $showAttemptAnswers ? CGI::td($preview) : ""; |
| 333 | $row .= $showCorrectAnswers ? CGI::td($correctAnswer) : ""; |
484 | $row .= $showCorrectAnswers ? CGI::td($correctAnswer) : ""; |
| 334 | $row .= $showAttemptResults ? CGI::td($resultString) : ""; |
485 | $row .= $showAttemptResults ? CGI::td($resultString) : ""; |
| 335 | $row .= $answerMessage ? CGI::td($answerMessage) : ""; |
486 | $row .= $answerMessage ? CGI::td($answerMessage) : ""; |
| 336 | push @tableRows, $row; |
487 | push @tableRows, $row; |
| 337 | } |
488 | } |
| 338 | |
489 | |
| 339 | my $numCorrectNoun = $numCorrect == 1 ? "question" : "questions"; |
490 | my $numCorrectNoun = $numCorrect == 1 ? "question" : "questions"; |
| 340 | my $scorePercent = int ($problemResult->{score} * 100) . "\%"; |
491 | my $scorePercent = int ($problemResult->{score} * 100) . "\%"; |
| 341 | my $summary = "On this attempt, you answered $numCorrect $numCorrectNoun out of " |
492 | my $summary = "On this attempt, you answered $numCorrect $numCorrectNoun out of " |
| 342 | . scalar @answerNames . " correct, for a score of $scorePercent."; |
493 | . scalar @answerNames . " correct, for a score of $scorePercent."; |
| 343 | return CGI::table({-border=>1}, CGI::tr(\@tableRows)) . CGI::p($summary); |
494 | return CGI::table({-border=>1}, CGI::Tr(\@tableRows)) . CGI::p($summary); |
| 344 | } |
495 | } |
| 345 | |
496 | |
| 346 | sub viewOptions($) { |
497 | sub viewOptions($) { |
| 347 | my $self = shift; |
498 | my $self = shift; |
| 348 | my $displayMode = $self->{displayMode}; |
499 | my $displayMode = $self->{displayMode}; |
| … | |
… | |
| 393 | $optionLine, |
544 | $optionLine, |
| 394 | CGI::submit(-name=>"redisplay", -label=>"Redisplay Problem"), |
545 | CGI::submit(-name=>"redisplay", -label=>"Redisplay Problem"), |
| 395 | ); |
546 | ); |
| 396 | } |
547 | } |
| 397 | |
548 | |
|
|
549 | sub previewAnswer($$) { |
|
|
550 | my ($self, $answerResult) = @_; |
|
|
551 | my $ce = $self->{courseEnvironment}; |
|
|
552 | my $user = $self->{user}; |
|
|
553 | my $set = $self->{set}; |
|
|
554 | my $problem = $self->{problem}; |
|
|
555 | |
|
|
556 | # how are we going to name this? |
|
|
557 | my $targetPathCommon = "/png/" |
|
|
558 | . $user->id . "." |
|
|
559 | . $set->id . "." |
|
|
560 | . $problem->id . "." |
|
|
561 | . $answerResult->{ans_name} . ".png"; |
|
|
562 | |
|
|
563 | # figure out where to put things |
|
|
564 | my $wd = tempdir("webwork-dvipng-XXXXXXXX", DIR => $ce->{courseDirs}->{html_temp}); |
|
|
565 | my $latex = $ce->{externalPrograms}->{latex}; |
|
|
566 | my $dvipng = $ce->{externalPrograms}->{dvipng}; |
|
|
567 | my $tex = $answerResult->{preview_latex_string}; |
|
|
568 | my $targetPath = $ce->{courseDirs}->{html_temp} . $targetPathCommon; |
|
|
569 | # should use surePathToTmpFile, but we have to |
|
|
570 | # isolate it from the problem enivronment first |
|
|
571 | my $targetURL = $ce->{courseURLs}->{html_temp} . $targetPathCommon; |
|
|
572 | |
|
|
573 | # call dvipng to generate a preview |
|
|
574 | dvipng($wd, $latex, $dvipng, $tex, $targetPath); |
|
|
575 | if (-e $targetPath) { |
|
|
576 | return "<img src=\"$targetURL\" alt=\"$tex\" />"; |
|
|
577 | } else { |
|
|
578 | return "<b>[math2img failed]</b>"; |
|
|
579 | } |
|
|
580 | } |
|
|
581 | |
| 398 | ##### permission queries ##### |
582 | ##### permission queries ##### |
| 399 | |
583 | |
| 400 | # this stuff should be abstracted out into the permissions system |
584 | # this stuff should be abstracted out into the permissions system |
| 401 | # however, the permission system only knows about things in the |
585 | # however, the permission system only knows about things in the |
| 402 | # course environment and the username. hmmm... |
586 | # course environment and the username. hmmm... |
| … | |
… | |
| 417 | |
601 | |
| 418 | sub canRecordAnswers($$$$$) { |
602 | sub canRecordAnswers($$$$$) { |
| 419 | my ($permissionLevel, $openDate, $dueDate, $maxAttempts, $attempts) = @_; |
603 | my ($permissionLevel, $openDate, $dueDate, $maxAttempts, $attempts) = @_; |
| 420 | my $permHigh = $permissionLevel > 0; |
604 | my $permHigh = $permissionLevel > 0; |
| 421 | my $timeOK = time >= $openDate && time <= $dueDate; |
605 | my $timeOK = time >= $openDate && time <= $dueDate; |
| 422 | my $attemptsOK = $attempts <= $maxAttempts; |
606 | my $attemptsOK = $maxAttempts == -1 || $attempts <= $maxAttempts; |
| 423 | return $permHigh || ($timeOK && $attemptsOK); |
607 | my $recordAnswers = $permHigh || ($timeOK && $attemptsOK); |
|
|
608 | return $recordAnswers; |
| 424 | } |
609 | } |
| 425 | |
610 | |
| 426 | sub mustRecordAnswers($) { |
611 | sub mustRecordAnswers($) { |
| 427 | my ($permissionLevel) = @_; |
612 | my ($permissionLevel) = @_; |
| 428 | return $permissionLevel == 0; |
613 | return $permissionLevel == 0; |