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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.455  
changed lines
  Added in v.692

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9