[system] / trunk / webwork2 / lib / WeBWorK / ContentGenerator / Problem.pm Repository:
ViewVC logotype

Annotation of /trunk/webwork2/lib/WeBWorK/ContentGenerator/Problem.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : sh002i 449 ################################################################################
2 : sh002i 494 # WeBWorK mod_perl (c) 2000-2002 WeBWorK Project
3 : sh002i 449 # $Id$
4 :     ################################################################################
5 :    
6 : malsyned 353 package WeBWorK::ContentGenerator::Problem;
7 : malsyned 396
8 : sh002i 455 =head1 NAME
9 :    
10 :     WeBWorK::ContentGenerator::Problem - Allow a student to interact with a problem.
11 :    
12 :     =cut
13 :    
14 : malsyned 396 use strict;
15 :     use warnings;
16 : sh002i 449 use base qw(WeBWorK::ContentGenerator);
17 :     use CGI qw();
18 : sh002i 623 use File::Temp qw(tempdir);
19 : sh002i 455 use WeBWorK::Form;
20 :     use WeBWorK::PG;
21 : sh002i 623 use WeBWorK::PG::IO;
22 : sh002i 562 use WeBWorK::Utils qw(writeLog encodeAnswers decodeAnswers ref2string);
23 : malsyned 353
24 : sh002i 449 ############################################################
25 :     #
26 : sh002i 424 # user
27 : malsyned 704 # effectiveUser
28 : sh002i 424 # key
29 : sh002i 415 #
30 : sh002i 424 # displayMode
31 :     # showOldAnswers
32 :     # showCorrectAnswers
33 :     # showHints
34 :     # showSolutions
35 : sh002i 415 #
36 : sh002i 425 # AnSwEr# - answer blanks in problem
37 :     #
38 : sh002i 429 # redisplay - name of the "Redisplay Problem" button
39 :     # submitAnswers - name of "Submit Answers" button
40 : sh002i 449 #
41 :     ############################################################
42 : gage 392
43 : sh002i 476 sub pre_header_initialize {
44 : sh002i 424 my ($self, $setName, $problemNumber) = @_;
45 :     my $courseEnv = $self->{courseEnvironment};
46 :     my $r = $self->{r};
47 : malsyned 704 my $userName = $r->param('user');
48 :     my $effectiveUserName = $r->param('effectiveUser');
49 : malsyned 353
50 : sh002i 429 ##### database setup #####
51 :    
52 : sh002i 449 my $cldb = WeBWorK::DB::Classlist->new($courseEnv);
53 :     my $wwdb = WeBWorK::DB::WW->new($courseEnv);
54 :     my $authdb = WeBWorK::DB::Auth->new($courseEnv);
55 : sh002i 429
56 : sh002i 449 my $user = $cldb->getUser($userName);
57 : malsyned 704 my $effectiveUser = $cldb->getUser($effectiveUserName);
58 :     my $set = $wwdb->getSet($effectiveUserName, $setName);
59 :     my $problem = $wwdb->getProblem($effectiveUserName, $setName, $problemNumber);
60 :     my $psvn = $wwdb->getPSVN($effectiveUserName, $setName);
61 :     my $permissionLevel = $authdb->getPermissions($userName);
62 : malsyned 396
63 : sh002i 429 ##### form processing #####
64 :    
65 : sh002i 425 # set options from form fields (see comment at top of file for names)
66 : sh002i 692 my $displayMode = $r->param("displayMode") || $courseEnv->{pg}->{options}->{displayMode};
67 : sh002i 425 my $redisplay = $r->param("redisplay");
68 : sh002i 429 my $submitAnswers = $r->param("submitAnswers");
69 : sh002i 719 my $checkAnswers = $r->param("checkAnswers");
70 : sh002i 623 my $previewAnswers = $r->param("previewAnswers");
71 : gage 388
72 : sh002i 449 # coerce form fields into CGI::Vars format
73 :     my $formFields = { WeBWorK::Form->new_from_paramable($r)->Vars };
74 :    
75 :     ##### permissions #####
76 :    
77 :     # what does the user want to do?
78 : sh002i 431 my %want = (
79 :     showOldAnswers => $r->param("showOldAnswers") || $courseEnv->{pg}->{options}->{showOldAnswers},
80 :     showCorrectAnswers => $r->param("showCorrectAnswers") || $courseEnv->{pg}->{options}->{showCorrectAnswers},
81 :     showHints => $r->param("showHints") || $courseEnv->{pg}->{options}->{showHints},
82 :     showSolutions => $r->param("showSolutions") || $courseEnv->{pg}->{options}->{showSolutions},
83 : sh002i 719 #recordAnswers => $r->param("recordAnswers") || 1,
84 :     recordAnswers => $submitAnswers,
85 : sh002i 431 );
86 : sh002i 429
87 : sh002i 431 # are certain options enforced?
88 :     my %must = (
89 :     showOldAnswers => 0,
90 :     showCorrectAnswers => 0,
91 :     showHints => 0,
92 :     showSolutions => 0,
93 :     recordAnswers => mustRecordAnswers($permissionLevel),
94 :     );
95 :    
96 : sh002i 429 # does the user have permission to use certain options?
97 : sh002i 431 my %can = (
98 :     showOldAnswers => 1,
99 :     showCorrectAnswers => canShowCorrectAnswers($permissionLevel, $set->answer_date),
100 :     showHints => 1,
101 :     showSolutions => canShowSolutions($permissionLevel, $set->answer_date),
102 :     recordAnswers => canRecordAnswers($permissionLevel, $set->open_date, $set->due_date,
103 :     $problem->max_attempts, $problem->num_correct + $problem->num_incorrect + 1),
104 :     # num_correct+num_incorrect+1 -- as this happens before updating $problem
105 :     );
106 : sh002i 429
107 :     # final values for options
108 : sh002i 431 my %will;
109 : sh002i 617 foreach (keys %must) {
110 : sh002i 431 $will{$_} = $can{$_} && ($want{$_} || $must{$_});
111 :     }
112 : sh002i 429
113 :     ##### sticky answers #####
114 :    
115 : sh002i 431 if (not $submitAnswers and $will{showOldAnswers}) {
116 :     # do this only if new answers are NOT being submitted
117 : sh002i 429 my %oldAnswers = decodeAnswers($problem->last_answer);
118 :     $formFields->{$_} = $oldAnswers{$_} foreach keys %oldAnswers;
119 :     }
120 :    
121 :     ##### translation #####
122 :    
123 : sh002i 424 my $pg = WeBWorK::PG->new(
124 :     $courseEnv,
125 : malsyned 704 $effectiveUser,
126 : sh002i 424 $r->param('key'),
127 : sh002i 502 $set,
128 :     $problem,
129 :     $psvn,
130 :     $formFields,
131 : sh002i 424 { # translation options
132 : sh002i 434 displayMode => $displayMode,
133 :     showHints => $will{showHints},
134 :     showSolutions => $will{showSolutions},
135 :     refreshMath2img => $will{showHints} || $will{showSolutions},
136 : sh002i 684 processAnswers => 1,
137 : sh002i 424 },
138 :     );
139 : gage 392
140 : sh002i 684 ##### fix hint/solution options #####
141 :    
142 :     $can{showHints} &&= $pg->{flags}->{hintExists};
143 :     $can{showSolutions} &&= $pg->{flags}->{solutionExists};
144 :    
145 : sh002i 449 ##### store fields #####
146 :    
147 :     $self->{cldb} = $cldb;
148 :     $self->{wwdb} = $wwdb;
149 :     $self->{authdb} = $authdb;
150 :    
151 : sh002i 692 $self->{userName} = $userName;
152 : sh002i 449 $self->{user} = $user;
153 : malsyned 704 $self->{effectiveUser} = $effectiveUser;
154 : sh002i 449 $self->{set} = $set;
155 :     $self->{problem} = $problem;
156 :     $self->{permissionLevel} = $permissionLevel;
157 :    
158 : sh002i 623 $self->{displayMode} = $displayMode;
159 :     $self->{redisplay} = $redisplay;
160 :     $self->{submitAnswers} = $submitAnswers;
161 : sh002i 719 $self->{checkAnswers} = $checkAnswers;
162 : sh002i 623 $self->{previewAnswers} = $previewAnswers;
163 :     $self->{formFields} = $formFields;
164 : sh002i 449
165 :     $self->{want} = \%want;
166 :     $self->{must} = \%must;
167 :     $self->{can} = \%can;
168 :     $self->{will} = \%will;
169 :    
170 :     $self->{pg} = $pg;
171 :     }
172 :    
173 : sh002i 558 sub if_warnings($$) {
174 :     my ($self, $arg) = @_;
175 :     return $self->{pg}->{warnings} ne "";
176 :     }
177 :    
178 :     sub if_errors($$) {
179 :     my ($self, $arg) = @_;
180 :     return $self->{pg}->{flags}->{error_flag};
181 :     }
182 :    
183 : sh002i 562 sub head {
184 : sh002i 555 my $self = shift;
185 :    
186 :     return $self->{pg}->{head_text} if $self->{pg}->{head_text};
187 :     }
188 : sh002i 476
189 :     sub path {
190 :     my $self = shift;
191 :     my $args = $_[-1];
192 :     my $setName = $self->{set}->id;
193 :     my $problemNumber = $self->{problem}->id;
194 :    
195 :     my $ce = $self->{courseEnvironment};
196 :     my $root = $ce->{webworkURLs}->{root};
197 :     my $courseName = $ce->{courseName};
198 :     return $self->pathMacro($args,
199 :     "Home" => "$root",
200 :     $courseName => "$root/$courseName",
201 : sh002i 555 $setName => "$root/$courseName/$setName",
202 : sh002i 476 "Problem $problemNumber" => "",
203 :     );
204 :     }
205 :    
206 :     sub siblings {
207 :     my $self = shift;
208 :     my $setName = $self->{set}->id;
209 :     my $problemNumber = $self->{problem}->id;
210 :    
211 :     my $ce = $self->{courseEnvironment};
212 :     my $root = $ce->{webworkURLs}->{root};
213 :     my $courseName = $ce->{courseName};
214 :    
215 : sh002i 526 print CGI::strong("Problems"), CGI::br();
216 :    
217 : sh002i 476 my $wwdb = $self->{wwdb};
218 : malsyned 704 my $effectiveUser = $self->{r}->param("effectiveUser");
219 : sh002i 476 my @problems;
220 : malsyned 704 push @problems, $wwdb->getProblem($effectiveUser, $setName, $_)
221 :     foreach ($wwdb->getProblems($effectiveUser, $setName));
222 : sh002i 476 foreach my $problem (sort { $a->id <=> $b->id } @problems) {
223 :     print CGI::a({-href=>"$root/$courseName/$setName/".$problem->id."/?"
224 : sh002i 692 . $self->url_authen_args . "&displayMode=" . $self->{displayMode}},
225 :     "Problem ".$problem->id), CGI::br();
226 : sh002i 476 }
227 :     }
228 :    
229 :     sub nav {
230 :     my $self = shift;
231 :     my $args = $_[-1];
232 :     my $setName = $self->{set}->id;
233 :     my $problemNumber = $self->{problem}->id;
234 :    
235 :     my $ce = $self->{courseEnvironment};
236 :     my $root = $ce->{webworkURLs}->{root};
237 :     my $courseName = $ce->{courseName};
238 :    
239 : malsyned 704 my $wwdb = $self->{wwdb};
240 :     my $effectiveUser = $self->{r}->param("effectiveUser");
241 : sh002i 692 my $tail = "&displayMode=".$self->{displayMode};
242 : sh002i 476
243 : sh002i 555 my @links = ("Problem List" => "$root/$courseName/$setName");
244 : sh002i 476
245 : malsyned 704 my $prevProblem = $wwdb->getProblem($effectiveUser, $setName, $problemNumber-1);
246 :     my $nextProblem = $wwdb->getProblem($effectiveUser, $setName, $problemNumber+1);
247 : sh002i 555 unshift @links, "Previous Problem" => $prevProblem
248 :     ? "$root/$courseName/$setName/".$prevProblem->id
249 :     : "";
250 :     push @links, "Next Problem" => $nextProblem
251 :     ? "$root/$courseName/$setName/".$nextProblem->id
252 :     : "";
253 : sh002i 476
254 : sh002i 692 return $self->navMacro($args, $tail, @links);
255 : sh002i 476 }
256 :    
257 : sh002i 449 sub title {
258 :     my $self = shift;
259 : sh002i 476 my $setName = $self->{set}->id;
260 :     my $problemNumber = $self->{problem}->id;
261 :    
262 :     return "$setName : Problem $problemNumber";
263 : sh002i 449 }
264 :    
265 :     sub body {
266 :     my $self = shift;
267 :    
268 :     # unpack some useful variables
269 : sh002i 476 my $r = $self->{r};
270 :     my $wwdb = $self->{wwdb};
271 :     my $set = $self->{set};
272 :     my $problem = $self->{problem};
273 :     my $permissionLevel = $self->{permissionLevel};
274 :     my $submitAnswers = $self->{submitAnswers};
275 : sh002i 719 my $checkAnswers = $self->{checkAnswers};
276 : sh002i 623 my $previewAnswers = $self->{previewAnswers};
277 : sh002i 719 my %want = %{ $self->{want} };
278 :     my %can = %{ $self->{can} };
279 :     my %must = %{ $self->{must} };
280 : sh002i 476 my %will = %{ $self->{will} };
281 :     my $pg = $self->{pg};
282 : sh002i 449
283 :     ##### translation errors? #####
284 :    
285 : sh002i 425 if ($pg->{flags}->{error_flag}) {
286 : sh002i 526 return translationError($pg->{errors}, $pg->{body_text});
287 : sh002i 425 }
288 : sh002i 415
289 : sh002i 429 ##### answer processing #####
290 :    
291 :     # if answers were submitted:
292 :     if ($submitAnswers) {
293 : sh002i 431 # store answers in DB for sticky answers
294 : sh002i 429 my %answersToStore;
295 :     my %answerHash = %{ $pg->{answers} };
296 :     $answersToStore{$_} = $answerHash{$_}->{original_student_ans}
297 :     foreach (keys %answerHash);
298 :     my $answerString = encodeAnswers(%answersToStore,
299 :     @{ $pg->{flags}->{ANSWER_ENTRY_ORDER} });
300 :     $problem->last_answer($answerString);
301 :     $wwdb->setProblem($problem);
302 :    
303 : sh002i 449 # store state in DB if it makes sense
304 : sh002i 431 if ($will{recordAnswers}) {
305 :     $problem->attempted(1);
306 :     $problem->status($pg->{state}->{recorded_score});
307 :     $problem->num_correct($pg->{state}->{num_of_correct_ans});
308 :     $problem->num_incorrect($pg->{state}->{num_of_incorrect_ans});
309 :     $wwdb->setProblem($problem);
310 : sh002i 562 # write to the transaction log, just to make sure
311 :     writeLog($self->{courseEnvironment}, "transaction",
312 :     $problem->id."\t".
313 :     $problem->set_id."\t".
314 :     $problem->login_id."\t".
315 :     $problem->source_file."\t".
316 :     $problem->value."\t".
317 :     $problem->max_attempts."\t".
318 :     $problem->problem_seed."\t".
319 :     $problem->status."\t".
320 :     $problem->attempted."\t".
321 :     $problem->last_answer."\t".
322 :     $problem->num_correct."\t".
323 :     $problem->num_incorrect
324 :     );
325 : sh002i 431 }
326 : sh002i 425 }
327 :    
328 : sh002i 429 ##### output #####
329 : sh002i 425
330 : sh002i 431 # attempt summary
331 :     if ($submitAnswers or $will{showCorrectAnswers}) {
332 :     # print this if user submitted answers OR requested correct answers
333 : sh002i 719 print $self->attemptResults($pg, $submitAnswers,
334 :     $will{showCorrectAnswers},
335 : sh002i 683 $pg->{flags}->{showPartialCorrectAnswers}, 0);
336 : sh002i 719 } elsif ($checkAnswers) {
337 :     # print this if user previewed answers
338 :     print $self->attemptResults($pg, 1, 0, 1, 0);
339 :     # show attempt answers
340 :     # don't show correct answers
341 :     # show attempt results (correctness)
342 :     # don't show attempt previews
343 : sh002i 623 } elsif ($previewAnswers) {
344 :     # print this if user previewed answers
345 : sh002i 683 print $self->attemptResults($pg, 1, 0, 0, 1);
346 : sh002i 719 # show attempt answers
347 : sh002i 623 # don't show correct answers
348 : sh002i 719 # don't show attempt results (correctness)
349 :     # show attempt previews
350 : sh002i 431 }
351 : gage 392
352 : sh002i 431 # score summary
353 :     my $attempts = $problem->num_correct + $problem->num_incorrect;
354 :     my $attemptsNoun = $attempts != 1 ? "times" : "time";
355 :     my $lastScore = int ($problem->status * 100) . "%";
356 :     my ($attemptsLeft, $attemptsLeftNoun);
357 :     if ($problem->max_attempts == -1) {
358 :     # unlimited attempts
359 :     $attemptsLeft = "unlimited";
360 :     $attemptsLeftNoun = "attempts";
361 :     } else {
362 :     $attemptsLeft = $problem->max_attempts - $attempts;
363 :     $attemptsLeftNoun = $attemptsLeft == 1 ? "attempt" : "attempts";
364 :     }
365 : sh002i 476 my $setClosedMessage;
366 :     if (time < $set->open_date or time > $set->due_date) {
367 :     $setClosedMessage = "This problem set is closed.";
368 :     if ($permissionLevel > 0) {
369 :     $setClosedMessage .= " Since you are a privileged user, additional attempts will be recorded.";
370 :     } else {
371 :     $setClosedMessage .= " Additional attempts will not be recorded.";
372 :     }
373 :     }
374 : sh002i 449 print CGI::p(
375 :     "You have attempted this problem $attempts $attemptsNoun.", CGI::br(),
376 : sh002i 431 $problem->attempted
377 : sh002i 449 ? "Your recorded score is $lastScore." . CGI::br()
378 : sh002i 431 : "",
379 : sh002i 476 "You have $attemptsLeft $attemptsLeftNoun remaining.", CGI::br(),
380 :     $setClosedMessage,
381 : sh002i 431 );
382 :    
383 : sh002i 449 print CGI::hr();
384 : sh002i 431
385 : sh002i 429 # main form
386 :     print
387 : sh002i 449 CGI::startform("POST", $r->uri),
388 : sh002i 429 $self->hidden_authen_fields,
389 : sh002i 449 CGI::p(CGI::i($pg->{result}->{msg})),
390 :     CGI::p($pg->{body_text}),
391 : sh002i 623 CGI::p(
392 : sh002i 719 ($can{recordAnswers}
393 :     ? CGI::submit(-name=>"submitAnswers",
394 :     -label=>"Submit Answers")
395 :     : ""),
396 :     ($can{recordAnswers}
397 :     ? CGI::submit(-name=>"checkAnswers",
398 :     -label=>"Check Answers")
399 :     : ""),
400 :     CGI::submit(-name=>"previewAnswers",
401 :     -label=>"Preview Answers"),
402 : sh002i 623 ),
403 : sh002i 671 $self->viewOptions(),
404 : sh002i 449 CGI::endform();
405 : sh002i 429
406 : sh002i 667 # feedback form
407 :     my $ce = $self->{courseEnvironment};
408 :     my $root = $ce->{webworkURLs}->{root};
409 :     my $courseName = $ce->{courseName};
410 :     my $feedbackURL = "$root/$courseName/feedback/";
411 :     print
412 :     CGI::startform("POST", $feedbackURL),
413 :     $self->hidden_authen_fields,
414 :     CGI::hidden("module", __PACKAGE__),
415 :     CGI::hidden("set", $set->id),
416 :     CGI::hidden("problem", $problem->id),
417 :     CGI::hidden("displayMode", $self->{displayMode}),
418 :     CGI::hidden("showOldAnswers", $will{showOldAnswers}),
419 :     CGI::hidden("showCorrectAnswers", $will{showCorrectAnswers}),
420 :     CGI::hidden("showHints", $will{showHints}),
421 :     CGI::hidden("showSolutions", $will{showSolutions}),
422 :     CGI::p({-align=>"right"},
423 :     CGI::submit(-name=>"feedbackForm", -label=>"Send Feedback")
424 :     ),
425 :     CGI::endform();
426 :    
427 : sh002i 558 # warning output
428 :     if ($pg->{warnings} ne "") {
429 :     print CGI::hr(), warningOutput($pg->{warnings});
430 :     }
431 :    
432 : sh002i 424 # debugging stuff
433 : sh002i 692 #print
434 :     # CGI::hr(),
435 :     # CGI::h2("debugging information"),
436 :     # CGI::h3("form fields"),
437 :     # ref2string($self->{formFields}),
438 :     # CGI::h3("user object"),
439 :     # ref2string($self->{user}),
440 :     # CGI::h3("set object"),
441 :     # ref2string($set),
442 :     # CGI::h3("problem object"),
443 :     # ref2string($problem),
444 :     # CGI::h3("PG object"),
445 :     # ref2string($pg, {'WeBWorK::PG::Translator' => 1});
446 : gage 392
447 : sh002i 424 return "";
448 : malsyned 353 }
449 :    
450 : sh002i 431 ##### output utilities #####
451 : sh002i 429
452 : sh002i 526 # this is used by ProblemSet.pm too, so don't fuck it up
453 : sh002i 429 sub translationError($$) {
454 :     my ($error, $details) = @_;
455 :     return
456 : sh002i 449 CGI::h2("Software Error"),
457 :     CGI::p(<<EOF),
458 : sh002i 429 WeBWorK has encountered a software error while attempting to process this problem.
459 :     It is likely that there is an error in the problem itself.
460 :     If you are a student, contact your professor to have the error corrected.
461 :     If you are a professor, please consut the error output below for more informaiton.
462 :     EOF
463 : sh002i 449 CGI::h3("Error messages"), CGI::blockquote(CGI::pre($error)),
464 :     CGI::h3("Error context"), CGI::blockquote(CGI::pre($details));
465 : sh002i 429 }
466 :    
467 : sh002i 558 # this is used by ProblemSet.pm too, so don't fuck it up
468 :     sub warningOutput($) {
469 :     my $warnings = shift;
470 :    
471 :     return
472 :     CGI::h2("Software Warnings"),
473 :     CGI::p(<<EOF),
474 :     WeBWorK has encountered warnings while attempting to process this problem.
475 :     It is likely that this indicates an error or ambiguity in the problem itself.
476 :     If you are a student, contact your professor to have the problem corrected.
477 :     If you are a professor, please consut the error output below for more informaiton.
478 :     EOF
479 :     CGI::h3("Warning messages"),
480 :     CGI::blockquote(CGI::pre($warnings)),
481 :     ;
482 :     }
483 :    
484 : sh002i 623 sub attemptResults($$$$$) {
485 :     my $self = shift;
486 : sh002i 425 my $pg = shift;
487 : sh002i 431 my $showAttemptAnswers = shift;
488 : sh002i 425 my $showCorrectAnswers = shift;
489 : sh002i 431 my $showAttemptResults = $showAttemptAnswers && shift;
490 : sh002i 683 my $showAttemptPreview = shift || 0;
491 : sh002i 425 my $problemResult = $pg->{result}; # the overall result of the problem
492 :     my @answerNames = @{ $pg->{flags}->{ANSWER_ENTRY_ORDER} };
493 :    
494 : sh002i 685 my $showMessages = $showAttemptAnswers && grep { $pg->{answers}->{$_}->{ans_message} } @answerNames;
495 :    
496 :     my $header = CGI::th("part");
497 :     $header .= $showAttemptAnswers ? CGI::th("entered") : "";
498 : sh002i 683 $header .= $showAttemptPreview ? CGI::th("preview") : "";
499 : sh002i 449 $header .= $showCorrectAnswers ? CGI::th("correct") : "";
500 :     $header .= $showAttemptResults ? CGI::th("result") : "";
501 : sh002i 685 $header .= $showMessages ? CGI::th("messages") : "";
502 : sh002i 425 my @tableRows = ( $header );
503 :     my $numCorrect;
504 :     foreach my $name (@answerNames) {
505 :     my $answerResult = $pg->{answers}->{$name};
506 : sh002i 429 my $studentAnswer = $answerResult->{student_ans}; # original_student_ans
507 : sh002i 683 my $preview = ($showAttemptPreview
508 :     ? $self->previewAnswer($answerResult)
509 :     : "");
510 : sh002i 425 my $correctAnswer = $answerResult->{correct_ans};
511 :     my $answerScore = $answerResult->{score};
512 : sh002i 685 my $answerMessage = $showMessages ? $answerResult->{ans_message} : "";
513 : sh002i 425
514 :     $numCorrect += $answerScore > 0;
515 : sh002i 555 my $resultString = $answerScore ? "correct" : "incorrect";
516 : sh002i 425
517 : sh002i 555 # get rid of the goofy prefix on the answer names (supposedly, the format
518 : sh002i 683 # of the answer names is changeable. this only fixes it for "AnSwEr"
519 : sh002i 555 $name =~ s/^AnSwEr//;
520 :    
521 : sh002i 449 my $row = CGI::td($name);
522 :     $row .= $showAttemptAnswers ? CGI::td($studentAnswer) : "";
523 : sh002i 683 $row .= $showAttemptPreview ? CGI::td($preview) : "";
524 : sh002i 449 $row .= $showCorrectAnswers ? CGI::td($correctAnswer) : "";
525 :     $row .= $showAttemptResults ? CGI::td($resultString) : "";
526 :     $row .= $answerMessage ? CGI::td($answerMessage) : "";
527 : sh002i 425 push @tableRows, $row;
528 :     }
529 :    
530 : sh002i 431 my $numCorrectNoun = $numCorrect == 1 ? "question" : "questions";
531 : sh002i 425 my $scorePercent = int ($problemResult->{score} * 100) . "\%";
532 : sh002i 431 my $summary = "On this attempt, you answered $numCorrect $numCorrectNoun out of "
533 : sh002i 425 . scalar @answerNames . " correct, for a score of $scorePercent.";
534 : sh002i 476 return CGI::table({-border=>1}, CGI::Tr(\@tableRows)) . CGI::p($summary);
535 : sh002i 425 }
536 :    
537 : sh002i 449 sub viewOptions($) {
538 :     my $self = shift;
539 :     my $displayMode = $self->{displayMode};
540 :     my %must = %{ $self->{must} };
541 :     my %can = %{ $self->{can} };
542 :     my %will = %{ $self->{will} };
543 : sh002i 431
544 :     my $optionLine;
545 :     $can{showOldAnswers} and $optionLine .= join "",
546 : sh002i 429 "Show: &nbsp;",
547 : sh002i 449 CGI::checkbox(
548 : sh002i 429 -name => "showOldAnswers",
549 : sh002i 431 -checked => $will{showOldAnswers},
550 :     -label => "Saved answers",
551 :     ), "&nbsp;&nbsp;";
552 :     $can{showCorrectAnswers} and $optionLine .= join "",
553 : sh002i 449 CGI::checkbox(
554 : sh002i 429 -name => "showCorrectAnswers",
555 : sh002i 431 -checked => $will{showCorrectAnswers},
556 : sh002i 429 -label => "Correct answers",
557 : sh002i 431 ), "&nbsp;&nbsp;";
558 :     $can{showHints} and $optionLine .= join "",
559 : sh002i 449 CGI::checkbox(
560 : sh002i 429 -name => "showHints",
561 : sh002i 431 -checked => $will{showHints},
562 : sh002i 429 -label => "Hints",
563 : sh002i 431 ), "&nbsp;&nbsp;";
564 :     $can{showSolutions} and $optionLine .= join "",
565 : sh002i 449 CGI::checkbox(
566 : sh002i 429 -name => "showSolutions",
567 : sh002i 431 -checked => $will{showSolutions},
568 : sh002i 429 -label => "Solutions",
569 : sh002i 431 ), "&nbsp;&nbsp;";
570 : sh002i 449 $optionLine and $optionLine .= join "", CGI::br();
571 : sh002i 431
572 : sh002i 449 return CGI::div({-style=>"border: thin groove; padding: 1ex; margin: 2ex"},
573 : sh002i 431 "View equations as: &nbsp;",
574 : sh002i 449 CGI::radio_group(
575 : sh002i 431 -name => "displayMode",
576 :     -values => ['plainText', 'formattedText', 'images'],
577 :     -default => $displayMode,
578 :     -labels => {
579 :     plainText => "plain text",
580 :     formattedText => "formatted text",
581 :     images => "images",
582 :     }
583 : sh002i 449 ), CGI::br(),
584 : sh002i 431 $optionLine,
585 : sh002i 449 CGI::submit(-name=>"redisplay", -label=>"Redisplay Problem"),
586 : sh002i 429 );
587 :     }
588 :    
589 : sh002i 623 sub previewAnswer($$) {
590 :     my ($self, $answerResult) = @_;
591 : malsyned 718 my $ce = $self->{courseEnvironment};
592 :     my $effectiveUser = $self->{effectiveUser};
593 :     my $set = $self->{set};
594 :     my $problem = $self->{problem};
595 :     my $displayMode = $self->{displayMode};
596 : sh002i 623
597 : sh002i 682 # note: right now, we have to do things completely differently when we are
598 :     # rendering math from INSIDE the translator and from OUTSIDE the translator.
599 :     # so we'll just deal with each case explicitly here. there's some code
600 :     # duplication that can be dealt with later by abstracting out tth/dvipng/etc.
601 : sh002i 623
602 :     my $tex = $answerResult->{preview_latex_string};
603 :    
604 : sh002i 682 if ($displayMode eq "plainText") {
605 :     return $tex;
606 :     } elsif ($displayMode eq "formattedText") {
607 :     my $tthCommand = $ce->{externalPrograms}->{tth}
608 :     . " -L -f5 -r 2> /dev/null <<END_OF_INPUT; echo > /dev/null\n"
609 :     . "\\($tex\\)\n"
610 :     . "END_OF_INPUT\n";
611 :    
612 :    
613 :     # call tth
614 :     my $result = `$tthCommand`;
615 :     if ($?) {
616 :     return "<b>[tth failed: $? $@]</b>";
617 :     }
618 :     return $result;
619 :     } elsif ($displayMode eq "images") {
620 :     # how are we going to name this?
621 :     my $targetPathCommon = "/png/"
622 : malsyned 717 . $effectiveUser->id . "."
623 : sh002i 682 . $set->id . "."
624 :     . $problem->id . "."
625 :     . $answerResult->{ans_name} . ".png";
626 :    
627 :     # figure out where to put things
628 :     my $wd = tempdir("webwork-dvipng-XXXXXXXX", DIR => $ce->{courseDirs}->{html_temp});
629 :     my $latex = $ce->{externalPrograms}->{latex};
630 :     my $dvipng = $ce->{externalPrograms}->{dvipng};
631 :     my $targetPath = $ce->{courseDirs}->{html_temp} . $targetPathCommon;
632 :     # should use surePathToTmpFile, but we have to
633 :     # isolate it from the problem enivronment first
634 :     my $targetURL = $ce->{courseURLs}->{html_temp} . $targetPathCommon;
635 :    
636 :     # call dvipng to generate a preview
637 :     dvipng($wd, $latex, $dvipng, $tex, $targetPath);
638 :     if (-e $targetPath) {
639 :     return "<img src=\"$targetURL\" alt=\"$tex\" />";
640 :     } else {
641 :     return "<b>[math2img failed]</b>";
642 :     }
643 : sh002i 623 }
644 :     }
645 :    
646 : sh002i 431 ##### permission queries #####
647 : sh002i 429
648 :     # this stuff should be abstracted out into the permissions system
649 :     # however, the permission system only knows about things in the
650 :     # course environment and the username. hmmm...
651 :    
652 : sh002i 431 # also, i should fix these so that they have a consistent calling
653 :     # format -- perhaps:
654 :     # canPERM($courseEnv, $user, $set, $problem, $permissionLevel)
655 :    
656 : sh002i 429 sub canShowCorrectAnswers($$) {
657 :     my ($permissionLevel, $answerDate) = @_;
658 :     return $permissionLevel > 0 || time > $answerDate;
659 :     }
660 :    
661 :     sub canShowSolutions($$) {
662 :     my ($permissionLevel, $answerDate) = @_;
663 :     return canShowCorrectAnswers($permissionLevel, $answerDate);
664 :     }
665 :    
666 : sh002i 431 sub canRecordAnswers($$$$$) {
667 :     my ($permissionLevel, $openDate, $dueDate, $maxAttempts, $attempts) = @_;
668 :     my $permHigh = $permissionLevel > 0;
669 :     my $timeOK = time >= $openDate && time <= $dueDate;
670 : sh002i 617 my $attemptsOK = $maxAttempts == -1 || $attempts <= $maxAttempts;
671 :     my $recordAnswers = $permHigh || ($timeOK && $attemptsOK);
672 :     return $recordAnswers;
673 : sh002i 429 }
674 :    
675 :     sub mustRecordAnswers($) {
676 :     my ($permissionLevel) = @_;
677 :     return $permissionLevel == 0;
678 :     }
679 :    
680 : sh002i 415 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9