[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 435 - (view) (download) (as text)

1 : malsyned 353 package WeBWorK::ContentGenerator::Problem;
2 : sh002i 415 use base qw(WeBWorK::ContentGenerator);
3 : malsyned 396
4 :     use strict;
5 :     use warnings;
6 : sh002i 424 use CGI qw(:html :form);
7 : sh002i 429 use WeBWorK::Utils qw(ref2string encodeAnswers decodeAnswers);
8 : sh002i 415 use WeBWorK::PG;
9 : sh002i 424 use WeBWorK::Form;
10 : malsyned 353
11 : sh002i 424 # user
12 :     # key
13 : sh002i 415 #
14 : sh002i 424 # displayMode
15 :     # showOldAnswers
16 :     # showCorrectAnswers
17 :     # showHints
18 :     # showSolutions
19 : sh002i 415 #
20 : sh002i 425 # AnSwEr# - answer blanks in problem
21 :     #
22 : sh002i 429 # redisplay - name of the "Redisplay Problem" button
23 :     # submitAnswers - name of "Submit Answers" button
24 : gage 392
25 : malsyned 353 sub title {
26 : sh002i 424 my ($self, $setName, $problemNumber) = @_;
27 :     my $userName = $self->{r}->param('user');
28 :     return "Problem $problemNumber of problem set $setName for $userName";
29 : malsyned 353 }
30 :    
31 : sh002i 429 # TODO:
32 :     # :) enforce permissions for showCorrectAnswers and showSolutions
33 :     # (use $PRIV = $mustPRIV || ($canPRIV && $wantPRIV) -- cool syntax!)
34 : sh002i 431 # :) if answers were not submitted and there are student answers in the DB,
35 : sh002i 429 # decode them and put them into $formFields for the translator
36 : sh002i 431 # :) store submitted answers hash in database for sticky answers
37 :     # :) deal with the results of answer evaluation and grading :p
38 : sh002i 429 # :) introduce a recordAnswers option, which works on the same principle as
39 :     # the other permission-based options
40 :     # 7. make warnings work
41 :    
42 : malsyned 353 sub body {
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 424 # fix format of setName and problem
49 :     $setName =~ s/^set//;
50 :     $problemNumber =~ s/^prob//;
51 : gage 388
52 : sh002i 429 ##### database setup #####
53 : sh002i 431 # this should probably go in initialize() or whatever it's called
54 : sh002i 429
55 : sh002i 424 my $classlist = WeBWorK::DB::Classlist->new($courseEnv);
56 : sh002i 429 my $wwdb = WeBWorK::DB::WW->new($courseEnv);
57 :     my $authdb = WeBWorK::DB::Auth->new($courseEnv);
58 :    
59 : sh002i 424 my $user = $classlist->getUser($userName);
60 :     my $set = $wwdb->getSet($userName, $setName);
61 :     my $problem = $wwdb->getProblem($userName, $setName, $problemNumber);
62 : sh002i 429 my $permissionLevel = $authdb->getPermissions($userName);
63 : malsyned 396
64 : sh002i 429 ##### form processing #####
65 :    
66 : sh002i 425 # set options from form fields (see comment at top of file for names)
67 : sh002i 424 my $displayMode = $r->param("displayMode") || $courseEnv->{pg}->{options}->{displayMode};
68 : sh002i 425 my $redisplay = $r->param("redisplay");
69 : sh002i 429 my $submitAnswers = $r->param("submitAnswers");
70 : gage 388
71 : sh002i 431 my %want = (
72 :     showOldAnswers => $r->param("showOldAnswers") || $courseEnv->{pg}->{options}->{showOldAnswers},
73 :     showCorrectAnswers => $r->param("showCorrectAnswers") || $courseEnv->{pg}->{options}->{showCorrectAnswers},
74 :     showHints => $r->param("showHints") || $courseEnv->{pg}->{options}->{showHints},
75 :     showSolutions => $r->param("showSolutions") || $courseEnv->{pg}->{options}->{showSolutions},
76 :     recordAnswers => $r->param("recordAnswers") || 1,
77 :     );
78 : sh002i 429
79 : sh002i 424 # coerce form fields into CGI::Vars format
80 :     my $formFields = { WeBWorK::Form->new_from_paramable($r)->Vars };
81 : gage 392
82 : sh002i 429 ##### permissions #####
83 : gage 392
84 : sh002i 431 # are certain options enforced?
85 :     my %must = (
86 :     showOldAnswers => 0,
87 :     showCorrectAnswers => 0,
88 :     showHints => 0,
89 :     showSolutions => 0,
90 :     recordAnswers => mustRecordAnswers($permissionLevel),
91 :     );
92 :    
93 : sh002i 429 # does the user have permission to use certain options?
94 : sh002i 431 my %can = (
95 :     showOldAnswers => 1,
96 :     showCorrectAnswers => canShowCorrectAnswers($permissionLevel, $set->answer_date),
97 :     showHints => 1,
98 :     showSolutions => canShowSolutions($permissionLevel, $set->answer_date),
99 :     recordAnswers => canRecordAnswers($permissionLevel, $set->open_date, $set->due_date,
100 :     $problem->max_attempts, $problem->num_correct + $problem->num_incorrect + 1),
101 :     # num_correct+num_incorrect+1 -- as this happens before updating $problem
102 :     );
103 : sh002i 429
104 :     # final values for options
105 : sh002i 431 my %will;
106 :     foreach(keys %must) {
107 :     $will{$_} = $can{$_} && ($want{$_} || $must{$_});
108 :     }
109 : sh002i 429
110 :     ##### sticky answers #####
111 :    
112 : sh002i 431 if (not $submitAnswers and $will{showOldAnswers}) {
113 :     # do this only if new answers are NOT being submitted
114 : sh002i 429 my %oldAnswers = decodeAnswers($problem->last_answer);
115 :     $formFields->{$_} = $oldAnswers{$_} foreach keys %oldAnswers;
116 :     }
117 :    
118 :     ##### translation #####
119 :    
120 : sh002i 424 my $pg = WeBWorK::PG->new(
121 :     $courseEnv,
122 :     $r->param('user'),
123 :     $r->param('key'),
124 :     $setName,
125 :     $problemNumber,
126 :     { # translation options
127 : sh002i 434 displayMode => $displayMode,
128 :     showHints => $will{showHints},
129 :     showSolutions => $will{showSolutions},
130 :     refreshMath2img => $will{showHints} || $will{showSolutions},
131 : sh002i 431 # try leaving processAnswers on all the time?
132 : sh002i 434 processAnswers => 1, #$submitAnswers ? 1 : 0,
133 : sh002i 424 },
134 :     $formFields
135 :     );
136 : gage 392
137 : sh002i 429 # handle any errors in translation
138 : sh002i 425 if ($pg->{flags}->{error_flag}) {
139 :     # there was an error in translation
140 : sh002i 429 print
141 :     h2("Software Error"),
142 :     translationError($pg->{errors}, $pg->{body_text});
143 :    
144 : sh002i 425 return "";
145 :     }
146 : sh002i 415
147 : sh002i 429 ##### answer processing #####
148 :    
149 :     # if answers were submitted:
150 :     if ($submitAnswers) {
151 : sh002i 431 # store answers in DB for sticky answers
152 : sh002i 429 my %answersToStore;
153 :     my %answerHash = %{ $pg->{answers} };
154 :     $answersToStore{$_} = $answerHash{$_}->{original_student_ans}
155 :     foreach (keys %answerHash);
156 :     my $answerString = encodeAnswers(%answersToStore,
157 :     @{ $pg->{flags}->{ANSWER_ENTRY_ORDER} });
158 :     $problem->last_answer($answerString);
159 :     $wwdb->setProblem($problem);
160 :    
161 : sh002i 431 # store score in DB if it makes sense
162 :     if ($will{recordAnswers}) {
163 :     # the grader makes a lot of decisions for us...
164 :     # all we have to do is update information from
165 :     # the 'state' hash in the $pg hash.
166 :     $problem->attempted(1);
167 :     $problem->status($pg->{state}->{recorded_score});
168 :     $problem->num_correct($pg->{state}->{num_of_correct_ans});
169 :     $problem->num_incorrect($pg->{state}->{num_of_incorrect_ans});
170 :     #warn "Would have stored the following:\n",
171 :     # $problem->toString, "\n";
172 :     $wwdb->setProblem($problem);
173 :     } else {
174 :     print p("Your score was not recorded for some reason. ;)");
175 :     }
176 : sh002i 425 }
177 :    
178 : sh002i 429 ##### output #####
179 : sh002i 425
180 : sh002i 431 # attempt summary
181 :     if ($submitAnswers or $will{showCorrectAnswers}) {
182 :     # print this if user submitted answers OR requested correct answers
183 :     print attemptResults($pg, $submitAnswers, $will{showCorrectAnswers},
184 :     $pg->{flags}->{showPartialCorrectAnswers});
185 :     }
186 : gage 392
187 : sh002i 431 # score summary
188 :     my $attempts = $problem->num_correct + $problem->num_incorrect;
189 :     my $attemptsNoun = $attempts != 1 ? "times" : "time";
190 :     my $lastScore = int ($problem->status * 100) . "%";
191 :     my ($attemptsLeft, $attemptsLeftNoun);
192 :     if ($problem->max_attempts == -1) {
193 :     # unlimited attempts
194 :     $attemptsLeft = "unlimited";
195 :     $attemptsLeftNoun = "attempts";
196 :     } else {
197 :     $attemptsLeft = $problem->max_attempts - $attempts;
198 :     $attemptsLeftNoun = $attemptsLeft == 1 ? "attempt" : "attempts";
199 :     }
200 :     print p(
201 :     "You have attempted this problem $attempts $attemptsNoun.", br(),
202 :     $problem->attempted
203 :     ? "Your recorded score is $lastScore." . br()
204 :     : "",
205 :     "You have $attemptsLeft $attemptsLeftNoun remaining."
206 :     );
207 :    
208 :     # BY THE WAY..........
209 :     # we have to figure out some way to tell the student if their NEW answer,
210 :     # on THIS attempt, has been recorded. however, this is decided in part by
211 :     # the grader, so is there any way for us to know? we can rule out several
212 :     # cases where the answer is NOT being recorded, because of things decided
213 :     # in &canRecordAnswers...
214 :    
215 :     print hr();
216 :    
217 : sh002i 429 # main form
218 :     print
219 :     startform("POST", $r->uri),
220 :     $self->hidden_authen_fields,
221 : sh002i 431 p(i($pg->{result}->{msg})),
222 : sh002i 429 p($pg->{body_text}),
223 :     p(submit(-name=>"submitAnswers", -label=>"Submit Answers")),
224 : sh002i 431 viewOptions($displayMode, \%must, \%can, \%will),
225 : sh002i 435 endform();
226 : sh002i 429
227 : sh002i 424 # debugging stuff
228 : sh002i 435 #print
229 :     # hr(),
230 :     # h2("debugging information"),
231 :     # h3("form fields"),
232 :     # ref2string($formFields),
233 :     # h3("user object"),
234 :     # ref2string($user),
235 :     # h3("set object"),
236 :     # ref2string($set),
237 :     # h3("problem object"),
238 :     # ref2string($problem),
239 :     # h3("PG object"),
240 :     # ref2string($pg, {'WeBWorK::PG::Translator' => 1});
241 : gage 392
242 : sh002i 424 return "";
243 : malsyned 353 }
244 :    
245 : sh002i 431 ##### output utilities #####
246 : sh002i 429
247 :     sub translationError($$) {
248 :     my ($error, $details) = @_;
249 :     return
250 :     p(<<EOF),
251 :     WeBWorK has encountered a software error while attempting to process this problem.
252 :     It is likely that there is an error in the problem itself.
253 :     If you are a student, contact your professor to have the error corrected.
254 :     If you are a professor, please consut the error output below for more informaiton.
255 :     EOF
256 :     h3("Error messages"), blockquote(pre($error)),
257 :     h3("Error context"), blockquote(pre($details));
258 :     }
259 :    
260 :     sub attemptResults($$$) {
261 : sh002i 425 my $pg = shift;
262 : sh002i 431 my $showAttemptAnswers = shift;
263 : sh002i 425 my $showCorrectAnswers = shift;
264 : sh002i 431 my $showAttemptResults = $showAttemptAnswers && shift;
265 : sh002i 425 my $problemResult = $pg->{result}; # the overall result of the problem
266 :     my @answerNames = @{ $pg->{flags}->{ANSWER_ENTRY_ORDER} };
267 :    
268 : sh002i 431 my $header = th("answer");
269 :     $header .= $showAttemptAnswers ? th("attempt") : "";
270 :     $header .= $showCorrectAnswers ? th("correct") : "";
271 :     $header .= $showAttemptResults ? th("result") : "";
272 :     $header .= $showAttemptAnswers ? th("messages") : "";
273 : sh002i 425 my @tableRows = ( $header );
274 :     my $numCorrect;
275 :     foreach my $name (@answerNames) {
276 :     my $answerResult = $pg->{answers}->{$name};
277 : sh002i 429 my $studentAnswer = $answerResult->{student_ans}; # original_student_ans
278 : sh002i 425 my $correctAnswer = $answerResult->{correct_ans};
279 :     my $answerScore = $answerResult->{score};
280 : sh002i 431 my $answerMessage = $showAttemptAnswers ? $answerResult->{ans_message} : "";
281 : sh002i 425
282 :     $numCorrect += $answerScore > 0;
283 :     my $resultString = $answerScore ? "correct :^)" : "incorrect >:(";
284 :    
285 : sh002i 431 my $row = td($name);
286 :     $row .= $showAttemptAnswers ? td($studentAnswer) : "";
287 : sh002i 425 $row .= $showCorrectAnswers ? td($correctAnswer) : "";
288 :     $row .= $showAttemptResults ? td($resultString) : "";
289 :     $row .= $answerMessage ? td($answerMessage) : "";
290 :     push @tableRows, $row;
291 :     }
292 :    
293 : sh002i 431 my $numCorrectNoun = $numCorrect == 1 ? "question" : "questions";
294 : sh002i 425 my $scorePercent = int ($problemResult->{score} * 100) . "\%";
295 : sh002i 431 #my $message = i($problemResult->{msg});
296 :     my $summary = "On this attempt, you answered $numCorrect $numCorrectNoun out of "
297 : sh002i 425 . scalar @answerNames . " correct, for a score of $scorePercent.";
298 : sh002i 431 #return table({-border=>1}, Tr(\@tableRows)) . p($message, br(), $summary);
299 :     return table({-border=>1}, Tr(\@tableRows)) . p($summary);
300 : sh002i 425 }
301 :    
302 : sh002i 431 sub viewOptions($\%\%\%) {
303 :     my $displayMode = shift;
304 :     my %must = %{ shift() };
305 :     my %can = %{ shift() };
306 :     my %will = %{ shift() };
307 :    
308 :     my $optionLine;
309 :     $can{showOldAnswers} and $optionLine .= join "",
310 : sh002i 429 "Show: &nbsp;",
311 :     checkbox(
312 :     -name => "showOldAnswers",
313 : sh002i 431 -checked => $will{showOldAnswers},
314 :     -label => "Saved answers",
315 :     ), "&nbsp;&nbsp;";
316 :     $can{showCorrectAnswers} and $optionLine .= join "",
317 : sh002i 429 checkbox(
318 :     -name => "showCorrectAnswers",
319 : sh002i 431 -checked => $will{showCorrectAnswers},
320 : sh002i 429 -label => "Correct answers",
321 : sh002i 431 ), "&nbsp;&nbsp;";
322 :     $can{showHints} and $optionLine .= join "",
323 : sh002i 429 checkbox(
324 :     -name => "showHints",
325 : sh002i 431 -checked => $will{showHints},
326 : sh002i 429 -label => "Hints",
327 : sh002i 431 ), "&nbsp;&nbsp;";
328 :     $can{showSolutions} and $optionLine .= join "",
329 : sh002i 429 checkbox(
330 :     -name => "showSolutions",
331 : sh002i 431 -checked => $will{showSolutions},
332 : sh002i 429 -label => "Solutions",
333 : sh002i 431 ), "&nbsp;&nbsp;";
334 :     $optionLine and $optionLine .= join "", br();
335 :    
336 :     return div({-style=>"border: thin groove; padding: 1ex; margin: 2ex"},
337 :     "View equations as: &nbsp;",
338 :     radio_group(
339 :     -name => "displayMode",
340 :     -values => ['plainText', 'formattedText', 'images'],
341 :     -default => $displayMode,
342 :     -labels => {
343 :     plainText => "plain text",
344 :     formattedText => "formatted text",
345 :     images => "images",
346 :     }
347 : sh002i 429 ), br(),
348 : sh002i 431 $optionLine,
349 : sh002i 429 submit(-name=>"redisplay", -label=>"Redisplay Problem"),
350 :     );
351 :     }
352 :    
353 : sh002i 431 ##### permission queries #####
354 : sh002i 429
355 :     # this stuff should be abstracted out into the permissions system
356 :     # however, the permission system only knows about things in the
357 :     # course environment and the username. hmmm...
358 :    
359 : sh002i 431 # also, i should fix these so that they have a consistent calling
360 :     # format -- perhaps:
361 :     # canPERM($courseEnv, $user, $set, $problem, $permissionLevel)
362 :    
363 : sh002i 429 sub canShowCorrectAnswers($$) {
364 :     my ($permissionLevel, $answerDate) = @_;
365 :     return $permissionLevel > 0 || time > $answerDate;
366 :     }
367 :    
368 :     sub canShowSolutions($$) {
369 :     my ($permissionLevel, $answerDate) = @_;
370 :     return canShowCorrectAnswers($permissionLevel, $answerDate);
371 :     }
372 :    
373 : sh002i 431 sub canRecordAnswers($$$$$) {
374 :     my ($permissionLevel, $openDate, $dueDate, $maxAttempts, $attempts) = @_;
375 :     my $permHigh = $permissionLevel > 0;
376 :     my $timeOK = time >= $openDate && time <= $dueDate;
377 :     my $attemptsOK = $attempts <= $maxAttempts;
378 :     return $permHigh || ($timeOK && $attemptsOK);
379 : sh002i 429 }
380 :    
381 :     sub mustRecordAnswers($) {
382 :     my ($permissionLevel) = @_;
383 :     return $permissionLevel == 0;
384 :     }
385 :    
386 : sh002i 415 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9