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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9