[system] / trunk / webwork-modperl / lib / WeBWorK / ContentGenerator / Problem.pm Repository:
ViewVC logotype

Annotation of /trunk/webwork-modperl/lib/WeBWorK/ContentGenerator/Problem.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 526 - (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 455 use WeBWorK::Form;
19 :     use WeBWorK::PG;
20 : sh002i 429 use WeBWorK::Utils qw(ref2string encodeAnswers decodeAnswers);
21 : malsyned 353
22 : sh002i 449 ############################################################
23 :     #
24 : sh002i 424 # user
25 :     # key
26 : sh002i 415 #
27 : sh002i 424 # displayMode
28 :     # showOldAnswers
29 :     # showCorrectAnswers
30 :     # showHints
31 :     # showSolutions
32 : sh002i 415 #
33 : sh002i 425 # AnSwEr# - answer blanks in problem
34 :     #
35 : sh002i 429 # redisplay - name of the "Redisplay Problem" button
36 :     # submitAnswers - name of "Submit Answers" button
37 : sh002i 449 #
38 :     ############################################################
39 : gage 392
40 : sh002i 476 sub pre_header_initialize {
41 : sh002i 424 my ($self, $setName, $problemNumber) = @_;
42 :     my $courseEnv = $self->{courseEnvironment};
43 :     my $r = $self->{r};
44 :     my $userName = $r->param('user');
45 : malsyned 353
46 : sh002i 424 # fix format of setName and problem
47 :     $setName =~ s/^set//;
48 :     $problemNumber =~ s/^prob//;
49 : gage 388
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 :     my $set = $wwdb->getSet($userName, $setName);
58 :     my $problem = $wwdb->getProblem($userName, $setName, $problemNumber);
59 : sh002i 502 my $psvn = $wwdb->getPSVN($userName, $setName);
60 : sh002i 429 my $permissionLevel = $authdb->getPermissions($userName);
61 : malsyned 396
62 : sh002i 429 ##### form processing #####
63 :    
64 : sh002i 425 # set options from form fields (see comment at top of file for names)
65 : sh002i 424 my $displayMode = $r->param("displayMode") || $courseEnv->{pg}->{options}->{displayMode};
66 : sh002i 425 my $redisplay = $r->param("redisplay");
67 : sh002i 429 my $submitAnswers = $r->param("submitAnswers");
68 : gage 388
69 : sh002i 449 # coerce form fields into CGI::Vars format
70 :     my $formFields = { WeBWorK::Form->new_from_paramable($r)->Vars };
71 :    
72 :     ##### permissions #####
73 :    
74 :     # what does the user want to do?
75 : sh002i 431 my %want = (
76 :     showOldAnswers => $r->param("showOldAnswers") || $courseEnv->{pg}->{options}->{showOldAnswers},
77 :     showCorrectAnswers => $r->param("showCorrectAnswers") || $courseEnv->{pg}->{options}->{showCorrectAnswers},
78 :     showHints => $r->param("showHints") || $courseEnv->{pg}->{options}->{showHints},
79 :     showSolutions => $r->param("showSolutions") || $courseEnv->{pg}->{options}->{showSolutions},
80 :     recordAnswers => $r->param("recordAnswers") || 1,
81 :     );
82 : sh002i 429
83 : sh002i 431 # are certain options enforced?
84 :     my %must = (
85 :     showOldAnswers => 0,
86 :     showCorrectAnswers => 0,
87 :     showHints => 0,
88 :     showSolutions => 0,
89 :     recordAnswers => mustRecordAnswers($permissionLevel),
90 :     );
91 :    
92 : sh002i 429 # does the user have permission to use certain options?
93 : sh002i 431 my %can = (
94 :     showOldAnswers => 1,
95 :     showCorrectAnswers => canShowCorrectAnswers($permissionLevel, $set->answer_date),
96 :     showHints => 1,
97 :     showSolutions => canShowSolutions($permissionLevel, $set->answer_date),
98 :     recordAnswers => canRecordAnswers($permissionLevel, $set->open_date, $set->due_date,
99 :     $problem->max_attempts, $problem->num_correct + $problem->num_incorrect + 1),
100 :     # num_correct+num_incorrect+1 -- as this happens before updating $problem
101 :     );
102 : sh002i 429
103 :     # final values for options
104 : sh002i 431 my %will;
105 :     foreach(keys %must) {
106 :     $will{$_} = $can{$_} && ($want{$_} || $must{$_});
107 :     }
108 : sh002i 429
109 :     ##### sticky answers #####
110 :    
111 : sh002i 431 if (not $submitAnswers and $will{showOldAnswers}) {
112 :     # do this only if new answers are NOT being submitted
113 : sh002i 429 my %oldAnswers = decodeAnswers($problem->last_answer);
114 :     $formFields->{$_} = $oldAnswers{$_} foreach keys %oldAnswers;
115 :     }
116 :    
117 :     ##### translation #####
118 :    
119 : sh002i 424 my $pg = WeBWorK::PG->new(
120 :     $courseEnv,
121 : sh002i 502 $user,
122 : sh002i 424 $r->param('key'),
123 : sh002i 502 $set,
124 :     $problem,
125 :     $psvn,
126 :     $formFields,
127 : sh002i 424 { # translation options
128 : sh002i 434 displayMode => $displayMode,
129 :     showHints => $will{showHints},
130 :     showSolutions => $will{showSolutions},
131 :     refreshMath2img => $will{showHints} || $will{showSolutions},
132 : sh002i 431 # try leaving processAnswers on all the time?
133 : sh002i 434 processAnswers => 1, #$submitAnswers ? 1 : 0,
134 : sh002i 424 },
135 :     );
136 : gage 392
137 : sh002i 449 ##### store fields #####
138 :    
139 :     $self->{cldb} = $cldb;
140 :     $self->{wwdb} = $wwdb;
141 :     $self->{authdb} = $authdb;
142 :    
143 :     $self->{user} = $user;
144 :     $self->{set} = $set;
145 :     $self->{problem} = $problem;
146 :     $self->{permissionLevel} = $permissionLevel;
147 :    
148 :     $self->{displayMode} = $displayMode;
149 :     $self->{redisplay} = $redisplay;
150 :     $self->{submitAnswers} = $submitAnswers;
151 :     $self->{formFields} = $formFields;
152 :    
153 :     $self->{want} = \%want;
154 :     $self->{must} = \%must;
155 :     $self->{can} = \%can;
156 :     $self->{will} = \%will;
157 :    
158 :     $self->{pg} = $pg;
159 :     }
160 :    
161 : sh002i 476 #sub header {
162 :     # # *** we need to print $pg->{header_text} here!
163 :     #}
164 :    
165 :     sub path {
166 :     my $self = shift;
167 :     my $args = $_[-1];
168 :     my $setName = $self->{set}->id;
169 :     my $problemNumber = $self->{problem}->id;
170 :    
171 :     my $ce = $self->{courseEnvironment};
172 :     my $root = $ce->{webworkURLs}->{root};
173 :     my $courseName = $ce->{courseName};
174 :     return $self->pathMacro($args,
175 :     "Home" => "$root",
176 :     $courseName => "$root/$courseName",
177 :     $setName => "$root/$courseName/set$setName",
178 :     "Problem $problemNumber" => "",
179 :     );
180 :     }
181 :    
182 :     sub siblings {
183 :     my $self = shift;
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 :    
191 : sh002i 526 print CGI::strong("Problems"), CGI::br();
192 :    
193 : sh002i 476 my $wwdb = $self->{wwdb};
194 :     my $user = $self->{r}->param("user");
195 :     my @problems;
196 :     push @problems, $wwdb->getProblem($user, $setName, $_)
197 :     foreach ($wwdb->getProblems($user, $setName));
198 :     foreach my $problem (sort { $a->id <=> $b->id } @problems) {
199 :     print CGI::a({-href=>"$root/$courseName/$setName/".$problem->id."/?"
200 :     . $self->url_authen_args}, "Problem ".$problem->id), CGI::br();
201 :     }
202 :     }
203 :    
204 :     sub nav {
205 :     my $self = shift;
206 :     my $args = $_[-1];
207 :     my $setName = $self->{set}->id;
208 :     my $problemNumber = $self->{problem}->id;
209 :    
210 :     my $ce = $self->{courseEnvironment};
211 :     my $root = $ce->{webworkURLs}->{root};
212 :     my $courseName = $ce->{courseName};
213 :    
214 :     my $wwdb = $self->{wwdb};
215 :     my $user = $self->{r}->param("user");
216 :    
217 :     my @links = ("Problem List" => "$root/$courseName/set$setName");
218 :    
219 :     my $prevProblem = $wwdb->getProblem($user, $setName, $problemNumber-1);
220 :     my $nextProblem = $wwdb->getProblem($user, $setName, $problemNumber+1);
221 :     unshift @links, "Previous Problem" => "$root/$courseName/set$setName/prob".$prevProblem->id
222 :     if $prevProblem;
223 :     push @links, "Next Problem" => "$root/$courseName/set$setName/prob".$nextProblem->id
224 :     if $nextProblem;
225 :    
226 :     return $self->navMacro($args, @links);
227 :     }
228 :    
229 : sh002i 449 sub title {
230 :     my $self = shift;
231 : sh002i 476 my $setName = $self->{set}->id;
232 :     my $problemNumber = $self->{problem}->id;
233 :    
234 :     return "$setName : Problem $problemNumber";
235 : sh002i 449 }
236 :    
237 :     sub body {
238 :     my $self = shift;
239 :    
240 : sh002i 476 #$self->prepare(@_);
241 : sh002i 449
242 :     # unpack some useful variables
243 : sh002i 476 my $r = $self->{r};
244 :     my $wwdb = $self->{wwdb};
245 :     my $set = $self->{set};
246 :     my $problem = $self->{problem};
247 :     my $permissionLevel = $self->{permissionLevel};
248 :     my $submitAnswers = $self->{submitAnswers};
249 :     my %will = %{ $self->{will} };
250 :     my $pg = $self->{pg};
251 : sh002i 449
252 :     ##### translation errors? #####
253 :    
254 : sh002i 425 if ($pg->{flags}->{error_flag}) {
255 : sh002i 526 return translationError($pg->{errors}, $pg->{body_text});
256 : sh002i 425 }
257 : sh002i 415
258 : sh002i 429 ##### answer processing #####
259 :    
260 :     # if answers were submitted:
261 :     if ($submitAnswers) {
262 : sh002i 431 # store answers in DB for sticky answers
263 : sh002i 429 my %answersToStore;
264 :     my %answerHash = %{ $pg->{answers} };
265 :     $answersToStore{$_} = $answerHash{$_}->{original_student_ans}
266 :     foreach (keys %answerHash);
267 :     my $answerString = encodeAnswers(%answersToStore,
268 :     @{ $pg->{flags}->{ANSWER_ENTRY_ORDER} });
269 :     $problem->last_answer($answerString);
270 :     $wwdb->setProblem($problem);
271 :    
272 : sh002i 449 # store state in DB if it makes sense
273 : sh002i 431 if ($will{recordAnswers}) {
274 :     $problem->attempted(1);
275 :     $problem->status($pg->{state}->{recorded_score});
276 :     $problem->num_correct($pg->{state}->{num_of_correct_ans});
277 :     $problem->num_incorrect($pg->{state}->{num_of_incorrect_ans});
278 :     $wwdb->setProblem($problem);
279 :     }
280 : sh002i 425 }
281 :    
282 : sh002i 429 ##### output #####
283 : sh002i 425
284 : sh002i 431 # attempt summary
285 :     if ($submitAnswers or $will{showCorrectAnswers}) {
286 :     # print this if user submitted answers OR requested correct answers
287 :     print attemptResults($pg, $submitAnswers, $will{showCorrectAnswers},
288 :     $pg->{flags}->{showPartialCorrectAnswers});
289 :     }
290 : gage 392
291 : sh002i 431 # score summary
292 :     my $attempts = $problem->num_correct + $problem->num_incorrect;
293 :     my $attemptsNoun = $attempts != 1 ? "times" : "time";
294 :     my $lastScore = int ($problem->status * 100) . "%";
295 :     my ($attemptsLeft, $attemptsLeftNoun);
296 :     if ($problem->max_attempts == -1) {
297 :     # unlimited attempts
298 :     $attemptsLeft = "unlimited";
299 :     $attemptsLeftNoun = "attempts";
300 :     } else {
301 :     $attemptsLeft = $problem->max_attempts - $attempts;
302 :     $attemptsLeftNoun = $attemptsLeft == 1 ? "attempt" : "attempts";
303 :     }
304 : sh002i 476 my $setClosedMessage;
305 :     if (time < $set->open_date or time > $set->due_date) {
306 :     $setClosedMessage = "This problem set is closed.";
307 :     if ($permissionLevel > 0) {
308 :     $setClosedMessage .= " Since you are a privileged user, additional attempts will be recorded.";
309 :     } else {
310 :     $setClosedMessage .= " Additional attempts will not be recorded.";
311 :     }
312 :     }
313 : sh002i 449 print CGI::p(
314 :     "You have attempted this problem $attempts $attemptsNoun.", CGI::br(),
315 : sh002i 431 $problem->attempted
316 : sh002i 449 ? "Your recorded score is $lastScore." . CGI::br()
317 : sh002i 431 : "",
318 : sh002i 476 "You have $attemptsLeft $attemptsLeftNoun remaining.", CGI::br(),
319 :     $setClosedMessage,
320 : sh002i 431 );
321 :    
322 :     # BY THE WAY..........
323 :     # we have to figure out some way to tell the student if their NEW answer,
324 :     # on THIS attempt, has been recorded. however, this is decided in part by
325 :     # the grader, so is there any way for us to know? we can rule out several
326 :     # cases where the answer is NOT being recorded, because of things decided
327 :     # in &canRecordAnswers...
328 :    
329 : sh002i 449 print CGI::hr();
330 : sh002i 431
331 : sh002i 429 # main form
332 :     print
333 : sh002i 449 CGI::startform("POST", $r->uri),
334 : sh002i 429 $self->hidden_authen_fields,
335 : sh002i 449 CGI::p(CGI::i($pg->{result}->{msg})),
336 :     CGI::p($pg->{body_text}),
337 :     CGI::p(CGI::submit(-name=>"submitAnswers", -label=>"Submit Answers")),
338 :     $self->viewOptions,
339 :     CGI::endform();
340 : sh002i 429
341 : sh002i 424 # debugging stuff
342 : sh002i 435 #print
343 :     # hr(),
344 :     # h2("debugging information"),
345 :     # h3("form fields"),
346 :     # ref2string($formFields),
347 :     # h3("user object"),
348 :     # ref2string($user),
349 :     # h3("set object"),
350 :     # ref2string($set),
351 :     # h3("problem object"),
352 :     # ref2string($problem),
353 :     # h3("PG object"),
354 :     # ref2string($pg, {'WeBWorK::PG::Translator' => 1});
355 : gage 392
356 : sh002i 424 return "";
357 : malsyned 353 }
358 :    
359 : sh002i 431 ##### output utilities #####
360 : sh002i 429
361 : sh002i 526 # this is used by ProblemSet.pm too, so don't fuck it up
362 : sh002i 429 sub translationError($$) {
363 :     my ($error, $details) = @_;
364 :     return
365 : sh002i 449 CGI::h2("Software Error"),
366 :     CGI::p(<<EOF),
367 : sh002i 429 WeBWorK has encountered a software error while attempting to process this problem.
368 :     It is likely that there is an error in the problem itself.
369 :     If you are a student, contact your professor to have the error corrected.
370 :     If you are a professor, please consut the error output below for more informaiton.
371 :     EOF
372 : sh002i 449 CGI::h3("Error messages"), CGI::blockquote(CGI::pre($error)),
373 :     CGI::h3("Error context"), CGI::blockquote(CGI::pre($details));
374 : sh002i 429 }
375 :    
376 :     sub attemptResults($$$) {
377 : sh002i 425 my $pg = shift;
378 : sh002i 431 my $showAttemptAnswers = shift;
379 : sh002i 425 my $showCorrectAnswers = shift;
380 : sh002i 431 my $showAttemptResults = $showAttemptAnswers && shift;
381 : sh002i 425 my $problemResult = $pg->{result}; # the overall result of the problem
382 :     my @answerNames = @{ $pg->{flags}->{ANSWER_ENTRY_ORDER} };
383 :    
384 : sh002i 449 my $header = CGI::th("answer");
385 :     $header .= $showAttemptAnswers ? CGI::th("attempt") : "";
386 :     $header .= $showCorrectAnswers ? CGI::th("correct") : "";
387 :     $header .= $showAttemptResults ? CGI::th("result") : "";
388 :     $header .= $showAttemptAnswers ? CGI::th("messages") : "";
389 : sh002i 425 my @tableRows = ( $header );
390 :     my $numCorrect;
391 :     foreach my $name (@answerNames) {
392 :     my $answerResult = $pg->{answers}->{$name};
393 : sh002i 429 my $studentAnswer = $answerResult->{student_ans}; # original_student_ans
394 : sh002i 425 my $correctAnswer = $answerResult->{correct_ans};
395 :     my $answerScore = $answerResult->{score};
396 : sh002i 431 my $answerMessage = $showAttemptAnswers ? $answerResult->{ans_message} : "";
397 : sh002i 425
398 :     $numCorrect += $answerScore > 0;
399 :     my $resultString = $answerScore ? "correct :^)" : "incorrect >:(";
400 :    
401 : sh002i 449 my $row = CGI::td($name);
402 :     $row .= $showAttemptAnswers ? CGI::td($studentAnswer) : "";
403 :     $row .= $showCorrectAnswers ? CGI::td($correctAnswer) : "";
404 :     $row .= $showAttemptResults ? CGI::td($resultString) : "";
405 :     $row .= $answerMessage ? CGI::td($answerMessage) : "";
406 : sh002i 425 push @tableRows, $row;
407 :     }
408 :    
409 : sh002i 431 my $numCorrectNoun = $numCorrect == 1 ? "question" : "questions";
410 : sh002i 425 my $scorePercent = int ($problemResult->{score} * 100) . "\%";
411 : sh002i 431 my $summary = "On this attempt, you answered $numCorrect $numCorrectNoun out of "
412 : sh002i 425 . scalar @answerNames . " correct, for a score of $scorePercent.";
413 : sh002i 476 return CGI::table({-border=>1}, CGI::Tr(\@tableRows)) . CGI::p($summary);
414 : sh002i 425 }
415 :    
416 : sh002i 449 sub viewOptions($) {
417 :     my $self = shift;
418 :     my $displayMode = $self->{displayMode};
419 :     my %must = %{ $self->{must} };
420 :     my %can = %{ $self->{can} };
421 :     my %will = %{ $self->{will} };
422 : sh002i 431
423 :     my $optionLine;
424 :     $can{showOldAnswers} and $optionLine .= join "",
425 : sh002i 429 "Show: &nbsp;",
426 : sh002i 449 CGI::checkbox(
427 : sh002i 429 -name => "showOldAnswers",
428 : sh002i 431 -checked => $will{showOldAnswers},
429 :     -label => "Saved answers",
430 :     ), "&nbsp;&nbsp;";
431 :     $can{showCorrectAnswers} and $optionLine .= join "",
432 : sh002i 449 CGI::checkbox(
433 : sh002i 429 -name => "showCorrectAnswers",
434 : sh002i 431 -checked => $will{showCorrectAnswers},
435 : sh002i 429 -label => "Correct answers",
436 : sh002i 431 ), "&nbsp;&nbsp;";
437 :     $can{showHints} and $optionLine .= join "",
438 : sh002i 449 CGI::checkbox(
439 : sh002i 429 -name => "showHints",
440 : sh002i 431 -checked => $will{showHints},
441 : sh002i 429 -label => "Hints",
442 : sh002i 431 ), "&nbsp;&nbsp;";
443 :     $can{showSolutions} and $optionLine .= join "",
444 : sh002i 449 CGI::checkbox(
445 : sh002i 429 -name => "showSolutions",
446 : sh002i 431 -checked => $will{showSolutions},
447 : sh002i 429 -label => "Solutions",
448 : sh002i 431 ), "&nbsp;&nbsp;";
449 : sh002i 449 $optionLine and $optionLine .= join "", CGI::br();
450 : sh002i 431
451 : sh002i 449 return CGI::div({-style=>"border: thin groove; padding: 1ex; margin: 2ex"},
452 : sh002i 431 "View equations as: &nbsp;",
453 : sh002i 449 CGI::radio_group(
454 : sh002i 431 -name => "displayMode",
455 :     -values => ['plainText', 'formattedText', 'images'],
456 :     -default => $displayMode,
457 :     -labels => {
458 :     plainText => "plain text",
459 :     formattedText => "formatted text",
460 :     images => "images",
461 :     }
462 : sh002i 449 ), CGI::br(),
463 : sh002i 431 $optionLine,
464 : sh002i 449 CGI::submit(-name=>"redisplay", -label=>"Redisplay Problem"),
465 : sh002i 429 );
466 :     }
467 :    
468 : sh002i 431 ##### permission queries #####
469 : sh002i 429
470 :     # this stuff should be abstracted out into the permissions system
471 :     # however, the permission system only knows about things in the
472 :     # course environment and the username. hmmm...
473 :    
474 : sh002i 431 # also, i should fix these so that they have a consistent calling
475 :     # format -- perhaps:
476 :     # canPERM($courseEnv, $user, $set, $problem, $permissionLevel)
477 :    
478 : sh002i 429 sub canShowCorrectAnswers($$) {
479 :     my ($permissionLevel, $answerDate) = @_;
480 :     return $permissionLevel > 0 || time > $answerDate;
481 :     }
482 :    
483 :     sub canShowSolutions($$) {
484 :     my ($permissionLevel, $answerDate) = @_;
485 :     return canShowCorrectAnswers($permissionLevel, $answerDate);
486 :     }
487 :    
488 : sh002i 431 sub canRecordAnswers($$$$$) {
489 :     my ($permissionLevel, $openDate, $dueDate, $maxAttempts, $attempts) = @_;
490 :     my $permHigh = $permissionLevel > 0;
491 :     my $timeOK = time >= $openDate && time <= $dueDate;
492 :     my $attemptsOK = $attempts <= $maxAttempts;
493 :     return $permHigh || ($timeOK && $attemptsOK);
494 : sh002i 429 }
495 :    
496 :     sub mustRecordAnswers($) {
497 :     my ($permissionLevel) = @_;
498 :     return $permissionLevel == 0;
499 :     }
500 :    
501 : sh002i 415 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9