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

1 : sh002i 449 ################################################################################
2 : sh002i 1663 # WeBWorK Online Homework Delivery System
3 :     # Copyright © 2000-2003 The WeBWorK Project, http://openwebwork.sf.net/
4 : sh002i 2738 # $CVSHeader$
5 : sh002i 1663 #
6 :     # This program is free software; you can redistribute it and/or modify it under
7 :     # the terms of either: (a) the GNU General Public License as published by the
8 :     # Free Software Foundation; either version 2, or (at your option) any later
9 :     # version, or (b) the "Artistic License" which comes with this package.
10 :     #
11 :     # This program is distributed in the hope that it will be useful, but WITHOUT
12 :     # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 :     # FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the
14 :     # Artistic License for more details.
15 : sh002i 449 ################################################################################
16 :    
17 : malsyned 353 package WeBWorK::ContentGenerator::Problem;
18 : sh002i 818 use base qw(WeBWorK::ContentGenerator);
19 : malsyned 396
20 : sh002i 455 =head1 NAME
21 : gage 1039
22 : sh002i 455 WeBWorK::ContentGenerator::Problem - Allow a student to interact with a problem.
23 :    
24 :     =cut
25 : sh002i 1536
26 : malsyned 396 use strict;
27 :     use warnings;
28 : sh002i 449 use CGI qw();
29 : sh002i 919 use File::Path qw(rmtree);
30 : sh002i 455 use WeBWorK::Form;
31 :     use WeBWorK::PG;
32 : sh002i 1234 use WeBWorK::PG::ImageGenerator;
33 : sh002i 623 use WeBWorK::PG::IO;
34 : gage 1387 use WeBWorK::Utils qw(writeLog writeCourseLog encodeAnswers decodeAnswers ref2string makeTempDirectory);
35 : gage 997 use WeBWorK::DB::Utils qw(global2user user2global findDefaults);
36 : gage 1190 use WeBWorK::Timing;
37 : sh002i 1223
38 : jj 2415 use WeBWorK::Utils::Tasks qw(fake_set fake_problem);
39 : sh002i 1536
40 : sh002i 2505 ################################################################################
41 :     # CGI param interface to this module (up-to-date as of v1.153)
42 :     ################################################################################
43 :    
44 :     # Standard params:
45 : sh002i 449 #
46 : sh002i 2505 # user - user ID of real user
47 :     # key - session key
48 :     # effectiveUser - user ID of effective user
49 : sh002i 415 #
50 : sh002i 2505 # Integration with PGProblemEditor:
51 : sh002i 415 #
52 : sh002i 2505 # editMode - if set, indicates alternate problem source location.
53 :     # can be "temporaryFile" or "savedFile".
54 : sh002i 425 #
55 : sh002i 2505 # sourceFilePath - path to file to be edited
56 :     # problemSeed - force problem seed to value
57 :     # success - success message to display
58 :     # failure - failure message to display
59 : toenail 2398 #
60 : sh002i 2505 # Rendering options:
61 :     #
62 :     # displayMode - name of display mode to use
63 :     #
64 :     # showOldAnswers - request that last entered answer be shown (if allowed)
65 :     # showCorrectAnswers - request that correct answers be shown (if allowed)
66 :     # showHints - request that hints be shown (if allowed)
67 :     # showSolutions - request that solutions be shown (if allowed)
68 :     #
69 :     # Problem interaction:
70 :     #
71 :     # AnSwEr# - answer blanks in problem
72 :     #
73 :     # redisplay - name of the "Redisplay Problem" button
74 :     # submitAnswers - name of "Submit Answers" button
75 :     # checkAnswers - name of the "Check Answers" button
76 :     # previewAnswers - name of the "Preview Answers" button
77 : gage 392
78 : sh002i 2505 ################################################################################
79 :     # "can" methods
80 :     ################################################################################
81 : malsyned 1426
82 : sh002i 2505 # Subroutines to determine if a user "can" perform an action. Each subroutine is
83 :     # called with the following arguments:
84 :     #
85 : sh002i 2762 # ($self, $User, $EffectiveUser, $Set, $Problem)
86 : sh002i 2505
87 :     sub can_showOldAnswers {
88 : sh002i 2762 #my ($self, $User, $EffectiveUser, $Set, $Problem) = @_;
89 : sh002i 2505
90 :     return 1;
91 :     }
92 :    
93 :     sub can_showCorrectAnswers {
94 : sh002i 2762 my ($self, $User, $EffectiveUser, $Set, $Problem) = @_;
95 : sh002i 2505 my $authz = $self->r->authz;
96 :    
97 :     return
98 :     after($Set->answer_date)
99 :     ||
100 :     $authz->hasPermissions($User->user_id, "show_correct_answers_before_answer_date")
101 :     ;
102 :     }
103 :    
104 :     sub can_showHints {
105 : sh002i 2762 #my ($self, $User, $EffectiveUser, $Set, $Problem) = @_;
106 : sh002i 2505
107 :     return 1;
108 :     }
109 :    
110 :     sub can_showSolutions {
111 : sh002i 2762 my ($self, $User, $EffectiveUser, $Set, $Problem) = @_;
112 : sh002i 2505 my $authz = $self->r->authz;
113 :    
114 :     return
115 :     after($Set->answer_date)
116 :     ||
117 :     $authz->hasPermissions($User->user_id, "show_solutions_before_answer_date")
118 :     ;
119 :     }
120 :    
121 :     sub can_recordAnswers {
122 : sh002i 2762 my ($self, $User, $EffectiveUser, $Set, $Problem, $submitAnswers) = @_;
123 : sh002i 2505 my $authz = $self->r->authz;
124 : jj 2685 my $thisAttempt = $submitAnswers ? 1 : 0;
125 : sh002i 2505 if ($User->user_id ne $EffectiveUser->user_id) {
126 :     return $authz->hasPermissions($User->user_id, "record_answers_when_acting_as_student");
127 :     }
128 :     if (before($Set->open_date)) {
129 :     return $authz->hasPermissions($User->user_id, "record_answers_before_open_date");
130 :     } elsif (between($Set->open_date, $Set->due_date)) {
131 :     my $max_attempts = $Problem->max_attempts;
132 : jj 2685 my $attempts_used = $Problem->num_correct + $Problem->num_incorrect + $thisAttempt;
133 : sh002i 2505 if ($max_attempts == -1 or $attempts_used < $max_attempts) {
134 :     return $authz->hasPermissions($User->user_id, "record_answers_after_open_date_with_attempts");
135 :     } else {
136 :     return $authz->hasPermissions($User->user_id, "record_answers_after_open_date_without_attempts");
137 :     }
138 : sh002i 2512 } elsif (between($Set->due_date, $Set->answer_date)) {
139 : sh002i 2505 return $authz->hasPermissions($User->user_id, "record_answers_after_due_date");
140 : sh002i 2512 } elsif (after($Set->answer_date)) {
141 : sh002i 2505 return $authz->hasPermissions($User->user_id, "record_answers_after_answer_date");
142 :     }
143 :     }
144 :    
145 :     sub can_checkAnswers {
146 : sh002i 2762 my ($self, $User, $EffectiveUser, $Set, $Problem, $submitAnswers) = @_;
147 : sh002i 2505 my $authz = $self->r->authz;
148 : jj 2685 my $thisAttempt = $submitAnswers ? 1 : 0;
149 : sh002i 2505
150 :     if (before($Set->open_date)) {
151 :     return $authz->hasPermissions($User->user_id, "check_answers_before_open_date");
152 :     } elsif (between($Set->open_date, $Set->due_date)) {
153 :     my $max_attempts = $Problem->max_attempts;
154 : jj 2685 my $attempts_used = $Problem->num_correct + $Problem->num_incorrect + $thisAttempt;
155 : sh002i 2505 if ($max_attempts == -1 or $attempts_used < $max_attempts) {
156 :     return $authz->hasPermissions($User->user_id, "check_answers_after_open_date_with_attempts");
157 :     } else {
158 :     return $authz->hasPermissions($User->user_id, "check_answers_after_open_date_without_attempts");
159 :     }
160 : sh002i 2512 } elsif (between($Set->due_date, $Set->answer_date)) {
161 : sh002i 2505 return $authz->hasPermissions($User->user_id, "check_answers_after_due_date");
162 : sh002i 2512 } elsif (after($Set->answer_date)) {
163 : sh002i 2505 return $authz->hasPermissions($User->user_id, "check_answers_after_answer_date");
164 :     }
165 :     }
166 :    
167 :     # Helper functions for calculating times
168 : jj 2539 sub before { return time <= $_[0] }
169 :     sub after { return time >= $_[0] }
170 : sh002i 2505 sub between { my $t = time; return $t > $_[0] && $t < $_[1] }
171 :    
172 :     ################################################################################
173 :     # output utilities
174 :     ################################################################################
175 :    
176 :     sub attemptResults {
177 :     my $self = shift;
178 :     my $pg = shift;
179 :     my $showAttemptAnswers = shift;
180 :     my $showCorrectAnswers = shift;
181 :     my $showAttemptResults = $showAttemptAnswers && shift;
182 :     my $showSummary = shift;
183 :     my $showAttemptPreview = shift || 0;
184 :    
185 :     my $ce = $self->r->ce;
186 :    
187 :     my $problemResult = $pg->{result}; # the overall result of the problem
188 :     my @answerNames = @{ $pg->{flags}->{ANSWER_ENTRY_ORDER} };
189 :    
190 :     my $showMessages = $showAttemptAnswers && grep { $pg->{answers}->{$_}->{ans_message} } @answerNames;
191 :    
192 :     my $basename = "equation-" . $self->{set}->psvn. "." . $self->{problem}->problem_id . "-preview";
193 :    
194 :     # to make grabbing these options easier, we'll pull them out now...
195 :     my %imagesModeOptions = %{$ce->{pg}->{displayModeOptions}->{images}};
196 :    
197 :     my $imgGen = WeBWorK::PG::ImageGenerator->new(
198 :     tempDir => $ce->{webworkDirs}->{tmp},
199 :     latex => $ce->{externalPrograms}->{latex},
200 :     dvipng => $ce->{externalPrograms}->{dvipng},
201 :     useCache => 1,
202 :     cacheDir => $ce->{webworkDirs}->{equationCache},
203 :     cacheURL => $ce->{webworkURLs}->{equationCache},
204 :     cacheDB => $ce->{webworkFiles}->{equationCacheDB},
205 :     dvipng_align => $imagesModeOptions{dvipng_align},
206 :     dvipng_depth_db => $imagesModeOptions{dvipng_depth_db},
207 :     );
208 :    
209 :     my $header;
210 :     #$header .= CGI::th("Part");
211 :     $header .= $showAttemptAnswers ? CGI::th("Entered") : "";
212 :     $header .= $showAttemptPreview ? CGI::th("Answer Preview") : "";
213 :     $header .= $showCorrectAnswers ? CGI::th("Correct") : "";
214 :     $header .= $showAttemptResults ? CGI::th("Result") : "";
215 :     $header .= $showMessages ? CGI::th("Messages") : "";
216 : sh002i 2761 my $fully = '';
217 : sh002i 2505 my @tableRows = ( $header );
218 :     my $numCorrect = 0;
219 :     foreach my $name (@answerNames) {
220 :     my $answerResult = $pg->{answers}->{$name};
221 :     my $studentAnswer = $answerResult->{student_ans}; # original_student_ans
222 :     my $preview = ($showAttemptPreview
223 :     ? $self->previewAnswer($answerResult, $imgGen)
224 :     : "");
225 :     my $correctAnswer = $answerResult->{correct_ans};
226 :     my $answerScore = $answerResult->{score};
227 :     my $answerMessage = $showMessages ? $answerResult->{ans_message} : "";
228 : sh002i 2761 $numCorrect += $answerScore >= 1;
229 :     my $resultString = $answerScore >= 1 ? "correct" :
230 :     $answerScore > 0 ? int($answerScore*100)."% correct" :
231 :     "incorrect";
232 :     $fully = 'completely ' if $answerScore >0 and $answerScore < 1;
233 : sh002i 2505
234 :     # get rid of the goofy prefix on the answer names (supposedly, the format
235 :     # of the answer names is changeable. this only fixes it for "AnSwEr"
236 :     #$name =~ s/^AnSwEr//;
237 :    
238 :     my $row;
239 :     #$row .= CGI::td($name);
240 :     $row .= $showAttemptAnswers ? CGI::td($self->nbsp($studentAnswer)) : "";
241 :     $row .= $showAttemptPreview ? CGI::td($self->nbsp($preview)) : "";
242 :     $row .= $showCorrectAnswers ? CGI::td($self->nbsp($correctAnswer)) : "";
243 :     $row .= $showAttemptResults ? CGI::td($self->nbsp($resultString)) : "";
244 :     $row .= $showMessages ? CGI::td($self->nbsp($answerMessage)) : "";
245 :     push @tableRows, $row;
246 :     }
247 :    
248 :     # render equation images
249 :     $imgGen->render(refresh => 1);
250 :    
251 :     # my $numIncorrectNoun = scalar @answerNames == 1 ? "question" : "questions";
252 :     my $scorePercent = sprintf("%.0f%%", $problemResult->{score} * 100);
253 :     # FIXME -- I left the old code in in case we have to back out.
254 :     # my $summary = "On this attempt, you answered $numCorrect out of "
255 :     # . scalar @answerNames . " $numIncorrectNoun correct, for a score of $scorePercent.";
256 :     my $summary = "";
257 :     if (scalar @answerNames == 1) {
258 :     if ($numCorrect == scalar @answerNames) {
259 :     $summary .= CGI::div({class=>"ResultsWithoutError"},"The above answer is correct.");
260 :     } else {
261 : sh002i 2761 $summary .= CGI::div({class=>"ResultsWithError"},"The above answer is NOT ${fully}correct.");
262 : sh002i 2505 }
263 :     } else {
264 :     if ($numCorrect == scalar @answerNames) {
265 :     $summary .= CGI::div({class=>"ResultsWithoutError"},"All of the above answers are correct.");
266 :     } else {
267 : sh002i 2761 $summary .= CGI::div({class=>"ResultsWithError"},"At least one of the above answers is NOT ${fully}correct.");
268 : sh002i 2505 }
269 :     }
270 :    
271 :     return
272 :     CGI::table({-class=>"attemptResults"}, CGI::Tr(\@tableRows))
273 :     . ($showSummary ? CGI::p({class=>'emphasis'},$summary) : "");
274 :     }
275 :    
276 :     sub viewOptions {
277 :     my ($self) = @_;
278 :     my $ce = $self->r->ce;
279 :    
280 :     # don't show options if we don't have anything to show
281 :     return if $self->{invalidSet} or $self->{invalidProblem};
282 :     return unless $self->{isOpen};
283 :    
284 :     my $displayMode = $self->{displayMode};
285 :     my %must = %{ $self->{must} };
286 :     my %can = %{ $self->{can} };
287 :     my %will = %{ $self->{will} };
288 :    
289 :     my $optionLine;
290 :     $can{showOldAnswers} and $optionLine .= join "",
291 :     "Show: &nbsp;".CGI::br(),
292 :     CGI::checkbox(
293 :     -name => "showOldAnswers",
294 :     -checked => $will{showOldAnswers},
295 :     -label => "Saved answers",
296 :     ), "&nbsp;&nbsp;".CGI::br();
297 :    
298 :     $optionLine and $optionLine .= join "", CGI::br();
299 :    
300 :     my %display_modes = %{WeBWorK::PG::DISPLAY_MODES()};
301 :     my @active_modes = grep { exists $display_modes{$_} }
302 :     @{$ce->{pg}->{displayModes}};
303 :     my $modeLine = (scalar(@active_modes) > 1) ?
304 :     "View&nbsp;equations&nbsp;as:&nbsp;&nbsp;&nbsp;&nbsp;".CGI::br().
305 :     CGI::radio_group(
306 :     -name => "displayMode",
307 :     -values => \@active_modes,
308 :     -default => $displayMode,
309 :     -linebreak=>'true',
310 :     -labels => {
311 :     plainText => "plain",
312 :     formattedText => "formatted",
313 :     images => "images",
314 :     jsMath => "jsMath",
315 :     asciimath => "asciimath",
316 :     },
317 :     ). CGI::br().CGI::hr() : '';
318 :    
319 :     return CGI::div({-style=>"border: thin groove; padding: 1ex; margin: 2ex align: left"},
320 :     $modeLine,
321 :     $optionLine,
322 :     CGI::submit(-name=>"redisplay", -label=>"Apply Options"),
323 :     );
324 :     }
325 :    
326 :     sub previewAnswer {
327 :     my ($self, $answerResult, $imgGen) = @_;
328 :     my $ce = $self->r->ce;
329 :     my $effectiveUser = $self->{effectiveUser};
330 :     my $set = $self->{set};
331 :     my $problem = $self->{problem};
332 :     my $displayMode = $self->{displayMode};
333 :    
334 :     # note: right now, we have to do things completely differently when we are
335 :     # rendering math from INSIDE the translator and from OUTSIDE the translator.
336 :     # so we'll just deal with each case explicitly here. there's some code
337 :     # duplication that can be dealt with later by abstracting out tth/dvipng/etc.
338 :    
339 :     my $tex = $answerResult->{preview_latex_string};
340 :    
341 :     return "" unless defined $tex and $tex ne "";
342 :    
343 :     if ($displayMode eq "plainText") {
344 :     return $tex;
345 :     } elsif ($displayMode eq "formattedText") {
346 :     my $tthCommand = $ce->{externalPrograms}->{tth}
347 :     . " -L -f5 -r 2> /dev/null <<END_OF_INPUT; echo > /dev/null\n"
348 :     . "\\(".$tex."\\)\n"
349 :     . "END_OF_INPUT\n";
350 :    
351 :     # call tth
352 :     my $result = `$tthCommand`;
353 :     if ($?) {
354 :     return "<b>[tth failed: $? $@]</b>";
355 :     } else {
356 :     return $result;
357 :     }
358 :     } elsif ($displayMode eq "images") {
359 :     $imgGen->add($tex);
360 :     } elsif ($displayMode eq "jsMath") {
361 :     return '<DIV CLASS="math">'.$tex.'</DIV>' ;
362 :     }
363 :     }
364 :    
365 :     ################################################################################
366 :     # Template escape implementations
367 :     ################################################################################
368 :    
369 : sh002i 476 sub pre_header_initialize {
370 : sh002i 1841 my ($self) = @_;
371 : sh002i 1908 my $r = $self->r;
372 :     my $ce = $r->ce;
373 :     my $db = $r->db;
374 : toenail 2349 my $authz = $r->authz;
375 : sh002i 1908 my $urlpath = $r->urlpath;
376 :    
377 :     my $setName = $urlpath->arg("setID");
378 : sh002i 1841 my $problemNumber = $r->urlpath->arg("problemID");
379 : sh002i 1908 my $userName = $r->param('user');
380 :     my $effectiveUserName = $r->param('effectiveUser');
381 :     my $key = $r->param('key');
382 : gage 1007
383 : sh002i 1636 my $user = $db->getUser($userName); # checked
384 :     die "record for user $userName (real user) does not exist."
385 :     unless defined $user;
386 :    
387 :     my $effectiveUser = $db->getUser($effectiveUserName); # checked
388 :     die "record for user $effectiveUserName (effective user) does not exist."
389 :     unless defined $effectiveUser;
390 :    
391 : sh002i 1197 # obtain the merged set for $effectiveUser
392 : sh002i 1636 my $set = $db->getMergedSet($effectiveUserName, $setName); # checked
393 : sh002i 2738
394 :     # Database fix (in case of undefined published values)
395 :     # this is only necessary because some people keep holding to ww1.9 which did not have a published field
396 :     # make sure published is set to 0 or 1
397 :     if ( $set and $set->published ne "0" and $set->published ne "1") {
398 :     my $globalSet = $db->getGlobalSet($set->set_id);
399 :     $globalSet->published("1"); # defaults to published
400 :     $db->putGlobalSet($globalSet);
401 :     $set = $db->getMergedSet($effectiveUserName, $setName);
402 :     } else {
403 :     # don't do anything just yet, maybe we're a professor and we're
404 :     # fabricating a set or haven't assigned it to ourselves just yet
405 :     }
406 : gage 992
407 : sh002i 1197 # obtain the merged problem for $effectiveUser
408 : sh002i 1636 my $problem = $db->getMergedProblem($effectiveUserName, $setName, $problemNumber); # checked
409 : toenail 2097
410 : sh002i 1197 my $editMode = $r->param("editMode");
411 : gage 992
412 : toenail 2349 if ($authz->hasPermissions($userName, "modify_problem_sets")) {
413 : sh002i 1197 # professors are allowed to fabricate sets and problems not
414 :     # assigned to them (or anyone). this allows them to use the
415 :     # editor to
416 : sh002i 2738
417 :     # if a User Set does not exist for this user and this set
418 :     # then we check the Global Set
419 :     # if that does not exist we create a fake set
420 :     # if it does, we add fake user data
421 : sh002i 1197 unless (defined $set) {
422 :     my $userSetClass = $db->{set_user}->{record};
423 : sh002i 1636 my $globalSet = $db->getGlobalSet($setName); # checked
424 : sh002i 2738
425 :     if (not defined $globalSet) {
426 : jj 2020 $set = fake_set($db);
427 :     } else {
428 :     $set = global2user($userSetClass, $globalSet);
429 :     $set->psvn(0);
430 :     }
431 : sh002i 1197 }
432 :    
433 :     # if that is not yet defined obtain the global problem,
434 :     # convert it to a user problem, and add fake user data
435 :     unless (defined $problem) {
436 :     my $userProblemClass = $db->{problem_user}->{record};
437 : sh002i 1636 my $globalProblem = $db->getGlobalProblem($setName, $problemNumber); # checked
438 :     # if the global problem doesn't exist either, bail!
439 : jj 2020 if(not defined $globalProblem) {
440 :     my $sourceFilePath = $r->param("sourceFilePath");
441 : jj 2421 # These are problems from setmaker. If declared invalid, they won't come up
442 : jj 2422 $self->{invalidProblem} = $self->{invalidSet} = 1 unless defined $sourceFilePath;
443 : toenail 2398 # die "Problem $problemNumber in set $setName does not exist" unless defined $sourceFilePath;
444 : jj 2020 $problem = fake_problem($db);
445 :     $problem->problem_id(1);
446 :     $problem->source_file($sourceFilePath);
447 :     $problem->user_id($effectiveUserName);
448 :     } else {
449 :     $problem = global2user($userProblemClass, $globalProblem);
450 :     $problem->user_id($effectiveUserName);
451 :     $problem->problem_seed(0);
452 :     $problem->status(0);
453 :     $problem->attempted(0);
454 :     $problem->last_answer("");
455 :     $problem->num_correct(0);
456 :     $problem->num_incorrect(0);
457 :     }
458 : sh002i 1197 }
459 :    
460 :     # now we're sure we have valid UserSet and UserProblem objects
461 :     # yay!
462 :    
463 :     # now deal with possible editor overrides:
464 :    
465 :     # if the caller is asking to override the source file, and
466 :     # editMode calls for a temporary file, do so
467 :     my $sourceFilePath = $r->param("sourceFilePath");
468 : jj 2020 if (defined $sourceFilePath and
469 :     (not defined $editMode or $editMode eq "temporaryFile")) {
470 : sh002i 1197 $problem->source_file($sourceFilePath);
471 :     }
472 :    
473 : toenail 2398 # if the problem does not have a source file or no source file has been passed in
474 :     # then this is really an invalid problem (probably from a bad URL)
475 :     $self->{invalidProblem} = not (defined $sourceFilePath or $problem->source_file);
476 :    
477 : sh002i 1197 # if the caller is asking to override the problem seed, do so
478 :     my $problemSeed = $r->param("problemSeed");
479 :     if (defined $problemSeed) {
480 :     $problem->problem_seed($problemSeed);
481 :     }
482 : toenail 2097
483 : toenail 2244 my $publishedClass = ($set->published) ? "Published" : "Unpublished";
484 : toenail 2278 my $publishedText = ($set->published) ? "visible to students." : "hidden from students.";
485 : toenail 2244 $self->addmessage(CGI::p("This set is " . CGI::font({class=>$publishedClass}, $publishedText)));
486 : sh002i 1197 } else {
487 : toenail 2099
488 : toenail 2097 # A set is valid if it exists and if it is either published or the user is privileged.
489 : sh002i 2738 $self->{invalidSet} = !(defined $set and ($set->published || $authz->hasPermissions($userName, "view_unpublished_sets")));
490 :     $self->{invalidProblem} = !(defined $problem and ($set->published || $authz->hasPermissions($userName, "view_unpublished_sets")));
491 :    
492 : toenail 2398 $self->addbadmessage(CGI::p("This problem will not count towards your grade.")) if $problem and not $problem->value and not $self->{invalidProblem};
493 : sh002i 1197 }
494 : toenail 2398
495 : sh002i 1197 $self->{userName} = $userName;
496 :     $self->{effectiveUserName} = $effectiveUserName;
497 :     $self->{user} = $user;
498 :     $self->{effectiveUser} = $effectiveUser;
499 :     $self->{set} = $set;
500 :     $self->{problem} = $problem;
501 :     $self->{editMode} = $editMode;
502 : sh002i 429
503 :     ##### form processing #####
504 :    
505 : sh002i 425 # set options from form fields (see comment at top of file for names)
506 : sh002i 1908 my $displayMode = $r->param("displayMode") || $ce->{pg}->{options}->{displayMode};
507 : sh002i 425 my $redisplay = $r->param("redisplay");
508 : sh002i 429 my $submitAnswers = $r->param("submitAnswers");
509 : sh002i 719 my $checkAnswers = $r->param("checkAnswers");
510 : sh002i 623 my $previewAnswers = $r->param("previewAnswers");
511 : gage 388
512 : sh002i 449 my $formFields = { WeBWorK::Form->new_from_paramable($r)->Vars };
513 :    
514 : sh002i 738 $self->{displayMode} = $displayMode;
515 :     $self->{redisplay} = $redisplay;
516 :     $self->{submitAnswers} = $submitAnswers;
517 :     $self->{checkAnswers} = $checkAnswers;
518 :     $self->{previewAnswers} = $previewAnswers;
519 :     $self->{formFields} = $formFields;
520 : toenail 2011
521 : toenail 2093 # get result and send to message
522 :     my $success = $r->param("sucess");
523 :     my $failure = $r->param("failure");
524 : toenail 2398 $self->addbadmessage(CGI::p($failure)) if $failure;
525 :     $self->addgoodmessage(CGI::p($success)) if $success;
526 : toenail 2093
527 : toenail 2011 # now that we've set all the necessary variables quit out if the set or problem is invalid
528 :     return if $self->{invalidSet} || $self->{invalidProblem};
529 : sh002i 738
530 : sh002i 449 ##### permissions #####
531 :    
532 : sh002i 738 # are we allowed to view this problem?
533 : sh002i 2505 $self->{isOpen} = after($set->open_date) || $authz->hasPermissions($userName, "view_unopened_sets");
534 : sh002i 738 return unless $self->{isOpen};
535 :    
536 : sh002i 449 # what does the user want to do?
537 : sh002i 431 my %want = (
538 : sh002i 1908 showOldAnswers => $r->param("showOldAnswers") || $ce->{pg}->{options}->{showOldAnswers},
539 :     showCorrectAnswers => $r->param("showCorrectAnswers") || $ce->{pg}->{options}->{showCorrectAnswers},
540 :     showHints => $r->param("showHints") || $ce->{pg}->{options}->{showHints},
541 :     showSolutions => $r->param("showSolutions") || $ce->{pg}->{options}->{showSolutions},
542 : sh002i 719 recordAnswers => $submitAnswers,
543 : sh002i 747 checkAnswers => $checkAnswers,
544 : jj 2685 getSubmitButton => 1,
545 : sh002i 431 );
546 : sh002i 429
547 : sh002i 431 # are certain options enforced?
548 :     my %must = (
549 :     showOldAnswers => 0,
550 :     showCorrectAnswers => 0,
551 :     showHints => 0,
552 :     showSolutions => 0,
553 : sh002i 2505 recordAnswers => ! $authz->hasPermissions($userName, "avoid_recording_answers"),
554 : sh002i 747 checkAnswers => 0,
555 : jj 2685 getSubmitButton => 0,
556 : sh002i 431 );
557 :    
558 : sh002i 429 # does the user have permission to use certain options?
559 : sh002i 2762 my @args = ($user, $effectiveUser, $set, $problem);
560 : sh002i 431 my %can = (
561 : sh002i 2505 showOldAnswers => $self->can_showOldAnswers(@args),
562 :     showCorrectAnswers => $self->can_showCorrectAnswers(@args),
563 :     showHints => $self->can_showHints(@args),
564 :     showSolutions => $self->can_showSolutions(@args),
565 : jj 2685 recordAnswers => $self->can_recordAnswers(@args, 0),
566 :     checkAnswers => $self->can_checkAnswers(@args, $submitAnswers),
567 :     getSubmitButton => $self->can_recordAnswers(@args, $submitAnswers),
568 : sh002i 431 );
569 : sh002i 1908
570 : sh002i 429 # final values for options
571 : sh002i 431 my %will;
572 : sh002i 617 foreach (keys %must) {
573 : sh002i 431 $will{$_} = $can{$_} && ($want{$_} || $must{$_});
574 :     }
575 : sh002i 429
576 :     ##### sticky answers #####
577 :    
578 : sh002i 1234 if (not ($submitAnswers or $previewAnswers or $checkAnswers) and $will{showOldAnswers}) {
579 : sh002i 431 # do this only if new answers are NOT being submitted
580 : sh002i 429 my %oldAnswers = decodeAnswers($problem->last_answer);
581 :     $formFields->{$_} = $oldAnswers{$_} foreach keys %oldAnswers;
582 :     }
583 :    
584 :     ##### translation #####
585 : gage 1202
586 : gage 1665 $WeBWorK::timer->continue("begin pg processing") if defined($WeBWorK::timer);
587 : sh002i 424 my $pg = WeBWorK::PG->new(
588 : sh002i 1908 $ce,
589 : malsyned 704 $effectiveUser,
590 : gage 1038 $key,
591 : sh002i 502 $set,
592 :     $problem,
593 : sh002i 1197 $set->psvn, # FIXME: this field should be removed
594 : sh002i 502 $formFields,
595 : sh002i 424 { # translation options
596 : sh002i 434 displayMode => $displayMode,
597 :     showHints => $will{showHints},
598 :     showSolutions => $will{showSolutions},
599 :     refreshMath2img => $will{showHints} || $will{showSolutions},
600 : sh002i 684 processAnswers => 1,
601 : sh002i 424 },
602 :     );
603 : gage 392
604 : gage 1665 $WeBWorK::timer->continue("end pg processing") if defined($WeBWorK::timer);
605 : sh002i 2505
606 : sh002i 684 ##### fix hint/solution options #####
607 :    
608 : gage 1582 $can{showHints} &&= $pg->{flags}->{hintExists}
609 :     &&= $pg->{flags}->{showHintLimit}<=$pg->{state}->{num_of_incorrect_ans};
610 : sh002i 684 $can{showSolutions} &&= $pg->{flags}->{solutionExists};
611 :    
612 : sh002i 449 ##### store fields #####
613 :    
614 :     $self->{want} = \%want;
615 :     $self->{must} = \%must;
616 :     $self->{can} = \%can;
617 :     $self->{will} = \%will;
618 :     $self->{pg} = $pg;
619 :     }
620 :    
621 : sh002i 558 sub if_errors($$) {
622 :     my ($self, $arg) = @_;
623 : sh002i 1908
624 :     if ($self->{isOpen}) {
625 :     return $self->{pg}->{flags}->{error_flag} ? $arg : !$arg;
626 :     } else {
627 :     return !$arg;
628 :     }
629 : sh002i 558 }
630 :    
631 : sh002i 562 sub head {
632 : sh002i 1908 my ($self) = @_;
633 :    
634 : sh002i 738 return "" unless $self->{isOpen};
635 : sh002i 555 return $self->{pg}->{head_text} if $self->{pg}->{head_text};
636 :     }
637 : sh002i 476
638 : sh002i 1131 sub options {
639 : sh002i 1908 my ($self) = @_;
640 :    
641 : toenail 2011 return "" if $self->{invalidProblem};
642 : jj 2231 my $sourceFilePathfield = '';
643 :     if($self->r->param("sourceFilePath")) {
644 :     $sourceFilePathfield = CGI::hidden(-name => "sourceFilePath",
645 :     -value => $self->r->param("sourceFilePath"));
646 :     }
647 : toenail 2011
648 : sh002i 1131 return join("",
649 :     CGI::start_form("POST", $self->{r}->uri),
650 :     $self->hidden_authen_fields,
651 : jj 2231 $sourceFilePathfield,
652 : sh002i 1131 CGI::hr(),
653 :     CGI::start_div({class=>"viewOptions"}),
654 :     $self->viewOptions(),
655 :     CGI::end_div(),
656 :     CGI::end_form()
657 :     );
658 :     }
659 :    
660 : sh002i 476 sub siblings {
661 : sh002i 1908 my ($self) = @_;
662 :     my $r = $self->r;
663 :     my $db = $r->db;
664 :     my $urlpath = $r->urlpath;
665 : sh002i 476
666 : toenail 2011 # can't show sibling problems if the set is invalid
667 :     return "" if $self->{invalidSet};
668 :    
669 : sh002i 1908 my $courseID = $urlpath->arg("courseID");
670 :     my $setID = $self->{set}->set_id;
671 :     my $eUserID = $r->param("effectiveUser");
672 :     my @problemIDs = sort { $a <=> $b } $db->listUserProblems($eUserID, $setID);
673 : sh002i 526
674 : sh002i 1908 print CGI::start_ul({class=>"LinksMenu"});
675 :     print CGI::start_li();
676 : jj 1949 print CGI::span({style=>"font-size:larger"}, "Problems");
677 : sh002i 1908 print CGI::start_ul();
678 : toenail 2211
679 : sh002i 1908 foreach my $problemID (@problemIDs) {
680 :     my $problemPage = $urlpath->newFromModule("WeBWorK::ContentGenerator::Problem",
681 :     courseID => $courseID, setID => $setID, problemID => $problemID);
682 : toenail 2211 print CGI::li(CGI::a({href=>$self->systemLink($problemPage, params=>{displayMode => $self->{displayMode}})}, "Problem $problemID"));
683 : sh002i 476 }
684 : sh002i 1908
685 :     print CGI::end_ul();
686 :     print CGI::end_li();
687 :     print CGI::end_ul();
688 :    
689 : sh002i 1223 return "";
690 : sh002i 476 }
691 :    
692 :     sub nav {
693 : sh002i 1908 my ($self, $args) = @_;
694 :     my $r = $self->r;
695 :     my $db = $r->db;
696 :     my $urlpath = $r->urlpath;
697 : sh002i 476
698 : sh002i 1908 my $courseID = $urlpath->arg("courseID");
699 : toenail 2011 my $setID = $self->{set}->set_id if !($self->{invalidSet});
700 :     my $problemID = $self->{problem}->problem_id if !($self->{invalidProblem});
701 : sh002i 1908 my $eUserID = $r->param("effectiveUser");
702 : sh002i 476
703 : sh002i 1908 my ($prevID, $nextID);
704 : toenail 2011
705 :     if (!$self->{invalidProblem}) {
706 :     my @problemIDs = $db->listUserProblems($eUserID, $setID);
707 :     foreach my $id (@problemIDs) {
708 :     $prevID = $id if $id < $problemID
709 :     and (not defined $prevID or $id > $prevID);
710 :     $nextID = $id if $id > $problemID
711 :     and (not defined $nextID or $id < $nextID);
712 :     }
713 : sh002i 1223 }
714 : jj 2020
715 : sh002i 1908 my @links;
716 : jj 2020
717 : sh002i 1908 if ($prevID) {
718 :     my $prevPage = $urlpath->newFromModule(__PACKAGE__,
719 :     courseID => $courseID, setID => $setID, problemID => $prevID);
720 :     push @links, "Previous Problem", $r->location . $prevPage->path, "navPrev";
721 :     } else {
722 :     push @links, "Previous Problem", "", "navPrev";
723 :     }
724 : jj 2020
725 : sh002i 1908 push @links, "Problem List", $r->location . $urlpath->parent->path, "navProbList";
726 : jj 2020
727 : sh002i 1908 if ($nextID) {
728 :     my $nextPage = $urlpath->newFromModule(__PACKAGE__,
729 :     courseID => $courseID, setID => $setID, problemID => $nextID);
730 :     push @links, "Next Problem", $r->location . $nextPage->path, "navNext";
731 :     } else {
732 :     push @links, "Next Problem", "", "navNext";
733 :     }
734 : jj 2020
735 : sh002i 1908 my $tail = "&displayMode=".$self->{displayMode};
736 :     return $self->navMacro($args, $tail, @links);
737 : sh002i 476 }
738 :    
739 : sh002i 449 sub title {
740 : sh002i 1908 my ($self) = @_;
741 : toenail 2011
742 :     # using the url arguments won't break if the set/problem are invalid
743 :     my $setID = $self->r->urlpath->arg("setID");
744 :     my $problemID = $self->r->urlpath->arg("problemID");
745 : sh002i 476
746 : sh002i 1908 return "$setID : $problemID";
747 : sh002i 449 }
748 :    
749 :     sub body {
750 :     my $self = shift;
751 : sh002i 1908 my $r = $self->r;
752 :     my $ce = $r->ce;
753 :     my $db = $r->db;
754 : toenail 2349 my $authz = $r->authz;
755 : sh002i 1908 my $urlpath = $r->urlpath;
756 : toenail 2349 my $user = $r->param('user');
757 : sh002i 2507 my $effectiveUser = $r->param('effectiveUser');
758 : toenail 2011
759 :     if ($self->{invalidSet}) {
760 :     return CGI::div({class=>"ResultsWithError"},
761 :     CGI::p("The selected problem set (" . $urlpath->arg("setID") . ") is not a valid set for " . $r->param("effectiveUser") . "."));
762 :     }
763 : sh002i 449
764 : toenail 2011 if ($self->{invalidProblem}) {
765 :     return CGI::div({class=>"ResultsWithError"},
766 :     CGI::p("The selected problem (" . $urlpath->arg("problemID") . ") is not a valid problem for set " . $self->{set}->set_id . "."));
767 :     }
768 :    
769 : sh002i 1908 unless ($self->{isOpen}) {
770 :     return CGI::div({class=>"ResultsWithError"},
771 :     CGI::p("This problem is not available because the problem set that contains it is not yet open."));
772 :     }
773 : sh002i 449 # unpack some useful variables
774 : sh002i 476 my $set = $self->{set};
775 :     my $problem = $self->{problem};
776 : sh002i 1197 my $editMode = $self->{editMode};
777 : sh002i 476 my $submitAnswers = $self->{submitAnswers};
778 : sh002i 719 my $checkAnswers = $self->{checkAnswers};
779 : sh002i 623 my $previewAnswers = $self->{previewAnswers};
780 : sh002i 719 my %want = %{ $self->{want} };
781 : sh002i 1197 my %can = %{ $self->{can} };
782 : sh002i 719 my %must = %{ $self->{must} };
783 : sh002i 476 my %will = %{ $self->{will} };
784 :     my $pg = $self->{pg};
785 : sh002i 449
786 : sh002i 1908 my $courseName = $urlpath->arg("courseID");
787 : gage 1592
788 : sh002i 2505 # FIXME: move editor link to top, next to problem number.
789 :     # format as "[edit]" like we're doing with course info file, etc.
790 :     # add edit link for set as well.
791 : sh002i 1908 my $editorLink = "";
792 : jj 2020 # if we are here without a real problem set, carry that through
793 :     my $forced_field = [];
794 :     $forced_field = ['sourceFilePath' => $r->param("sourceFilePath")] if
795 :     ($set->set_id eq 'Undefined_Set');
796 : toenail 2398 if ($authz->hasPermissions($user, "modify_problem_sets")) {
797 : sh002i 1908 my $editorPage = $urlpath->newFromModule("WeBWorK::ContentGenerator::Instructor::PGProblemEditor",
798 :     courseID => $courseName, setID => $set->set_id, problemID => $problem->problem_id);
799 : jj 2020 my $editorURL = $self->systemLink($editorPage, params=>$forced_field);
800 : sh002i 1908 $editorLink = CGI::a({href=>$editorURL}, "Edit this problem");
801 : gage 1592 }
802 : sh002i 1908
803 : sh002i 449 ##### translation errors? #####
804 :    
805 : sh002i 425 if ($pg->{flags}->{error_flag}) {
806 : sh002i 1908 print $self->errorOutput($pg->{errors}, $pg->{body_text});
807 :     print $editorLink;
808 :     return "";
809 : sh002i 425 }
810 : sh002i 415
811 : sh002i 429 ##### answer processing #####
812 : gage 1665 $WeBWorK::timer->continue("begin answer processing") if defined($WeBWorK::timer);
813 : sh002i 429 # if answers were submitted:
814 : sh002i 1227 my $scoreRecordedMessage;
815 : jjholt 2156 my $pureProblem;
816 : sh002i 429 if ($submitAnswers) {
817 : sh002i 818 # get a "pure" (unmerged) UserProblem to modify
818 : gage 1007 # this will be undefined if the problem has not been assigned to this user
819 : jjholt 2156 $pureProblem = $db->getUserProblem($problem->user_id, $problem->set_id, $problem->problem_id); # checked
820 : sh002i 1227 if (defined $pureProblem) {
821 :     # store answers in DB for sticky answers
822 :     my %answersToStore;
823 :     my %answerHash = %{ $pg->{answers} };
824 : gage 1539 $answersToStore{$_} = $self->{formFields}->{$_} #$answerHash{$_}->{original_student_ans} -- this may have been modified for fields with multiple values. Don't use it!!
825 : sh002i 1227 foreach (keys %answerHash);
826 : sh002i 1908
827 : gage 1507 # There may be some more answers to store -- one which are auxiliary entries to a primary answer. Evaluating
828 :     # matrices works in this way, only the first answer triggers an answer evaluator, the rest are just inputs
829 :     # however we need to store them. Fortunately they are still in the input form.
830 :     my @extra_answer_names = @{ $pg->{flags}->{KEPT_EXTRA_ANSWERS}};
831 : gage 1539 $answersToStore{$_} = $self->{formFields}->{$_} foreach (@extra_answer_names);
832 : gage 1507
833 :     # Now let's encode these answers to store them -- append the extra answers to the end of answer entry order
834 :     my @answer_order = (@{$pg->{flags}->{ANSWER_ENTRY_ORDER}}, @extra_answer_names);
835 : sh002i 1227 my $answerString = encodeAnswers(%answersToStore,
836 : gage 1507 @answer_order);
837 : sh002i 1227
838 :     # store last answer to database
839 :     $problem->last_answer($answerString);
840 : gage 1007 $pureProblem->last_answer($answerString);
841 :     $db->putUserProblem($pureProblem);
842 : sh002i 1227
843 : gage 1007 # store state in DB if it makes sense
844 :     if ($will{recordAnswers}) {
845 :     $problem->status($pg->{state}->{recorded_score});
846 :     $problem->attempted(1);
847 :     $problem->num_correct($pg->{state}->{num_of_correct_ans});
848 :     $problem->num_incorrect($pg->{state}->{num_of_incorrect_ans});
849 :     $pureProblem->status($pg->{state}->{recorded_score});
850 :     $pureProblem->attempted(1);
851 :     $pureProblem->num_correct($pg->{state}->{num_of_correct_ans});
852 :     $pureProblem->num_incorrect($pg->{state}->{num_of_incorrect_ans});
853 : sh002i 1227 if ($db->putUserProblem($pureProblem)) {
854 :     $scoreRecordedMessage = "Your score was recorded.";
855 :     } else {
856 :     $scoreRecordedMessage = "Your score was not recorded because there was a failure in storing the problem record to the database.";
857 :     }
858 : gage 1007 # write to the transaction log, just to make sure
859 :     writeLog($self->{ce}, "transaction",
860 :     $problem->problem_id."\t".
861 :     $problem->set_id."\t".
862 :     $problem->user_id."\t".
863 :     $problem->source_file."\t".
864 :     $problem->value."\t".
865 :     $problem->max_attempts."\t".
866 :     $problem->problem_seed."\t".
867 :     $pureProblem->status."\t".
868 :     $pureProblem->attempted."\t".
869 :     $pureProblem->last_answer."\t".
870 :     $pureProblem->num_correct."\t".
871 :     $pureProblem->num_incorrect
872 :     );
873 : sh002i 1227 } else {
874 : sh002i 2505 if (before($set->open_date) or after($set->due_date)) {
875 : sh002i 1227 $scoreRecordedMessage = "Your score was not recorded because this problem set is closed.";
876 :     } else {
877 :     $scoreRecordedMessage = "Your score was not recorded.";
878 :     }
879 : gage 1007 }
880 : sh002i 1227 } else {
881 : sh002i 2505 $scoreRecordedMessage = "Your score was not recorded because this problem has not been assigned to you.";
882 : sh002i 431 }
883 : sh002i 425 }
884 : sh002i 1227
885 : gage 794 # logging student answers
886 : gage 1387
887 :     my $answer_log = $self->{ce}->{courseFiles}->{logs}->{'answer_log'};
888 : jjholt 2156 if ( defined($answer_log ) and defined($pureProblem)) {
889 : dpvc 2735 if ($submitAnswers && !$authz->hasPermissions($effectiveUser, "dont_log_past_answers")) {
890 :     my $answerString = ""; my $scores = "";
891 : gage 794 my %answerHash = %{ $pg->{answers} };
892 : gage 1775 # FIXME this is the line 552 error. make sure original student ans is defined.
893 :     # The fact that it is not defined is probably due to an error in some answer evaluator.
894 :     # But I think it is useful to suppress this error message in the log.
895 : gage 1776 foreach (sort keys %answerHash) {
896 : sh002i 2635 my $orig_ans = $answerHash{$_}->{original_student_ans};
897 :     my $student_ans = defined $orig_ans ? $orig_ans : '';
898 : dpvc 2735 $answerString .= $student_ans."\t";
899 :     $scores .= $answerHash{$_}->{score} >= 1 ? "1" : "0";
900 : gage 1776 }
901 :     $answerString = '' unless defined($answerString); # insure string is defined.
902 : gage 1387 writeCourseLog($self->{ce}, "answer_log",
903 :     join("",
904 :     '|', $problem->user_id,
905 :     '|', $problem->set_id,
906 :     '|', $problem->problem_id,
907 : dpvc 2735 '|', $scores, "\t",
908 : gage 1387 time(),"\t",
909 :     $answerString,
910 :     ),
911 :     );
912 :    
913 : gage 794 }
914 : sh002i 1223 }
915 :    
916 : gage 1665 $WeBWorK::timer->continue("end answer processing") if defined($WeBWorK::timer);
917 : sh002i 1223
918 : sh002i 429 ##### output #####
919 : sh002i 1131
920 : malsyned 755 print CGI::start_div({class=>"problemHeader"});
921 : sh002i 1131
922 : sh002i 1197 # custom message for editor
923 : toenail 2349 if ($authz->hasPermissions($user, "modify_problem_sets") and defined $editMode) {
924 : sh002i 1197 if ($editMode eq "temporaryFile") {
925 :     print CGI::p(CGI::i("Editing temporary file: ", $problem->source_file));
926 :     } elsif ($editMode eq "savedFile") {
927 : toenail 2349 # taken care of in the initialization phase
928 : sh002i 1197 }
929 :     }
930 : toenail 2349
931 : sh002i 431 # attempt summary
932 : gage 1480 #FIXME -- the following is a kludge: if showPartialCorrectAnswers is negative don't show anything.
933 :     # until after the due date
934 : toenail 2398 # do I need to check $will{showCorrectAnswers} to make preflight work??
935 : sh002i 2505 if (($pg->{flags}->{showPartialCorrectAnswers} >= 0 and $submitAnswers) ) {
936 : sh002i 431 # print this if user submitted answers OR requested correct answers
937 : gage 1480
938 : gage 1507 print $self->attemptResults($pg, 1,
939 : sh002i 719 $will{showCorrectAnswers},
940 : gage 939 $pg->{flags}->{showPartialCorrectAnswers}, 1, 1);
941 : sh002i 719 } elsif ($checkAnswers) {
942 :     # print this if user previewed answers
943 : gage 2062 print CGI::div({class=>'ResultsWithError'},"ANSWERS ONLY CHECKED -- ",CGI::br(),"ANSWERS NOT RECORDED", CGI::br() );
944 : gage 1507 print $self->attemptResults($pg, 1, $will{showCorrectAnswers}, 1, 1, 1);
945 : sh002i 719 # show attempt answers
946 : gage 1507 # show correct answers if asked
947 : sh002i 719 # show attempt results (correctness)
948 : gage 1507 # show attempt previews
949 : sh002i 623 } elsif ($previewAnswers) {
950 :     # print this if user previewed answers
951 : gage 2062 print CGI::div({class=>'ResultsWithError'},"PREVIEW ONLY -- NOT RECORDED"),CGI::br(),$self->attemptResults($pg, 1, 0, 0, 0, 1);
952 : sh002i 719 # show attempt answers
953 : sh002i 623 # don't show correct answers
954 : sh002i 719 # don't show attempt results (correctness)
955 :     # show attempt previews
956 : sh002i 431 }
957 : gage 392
958 : malsyned 755 print CGI::end_div();
959 :    
960 : sh002i 2505 # main form
961 :     print CGI::startform("POST", $r->uri);
962 :     print $self->hidden_authen_fields;
963 :    
964 : malsyned 755 print CGI::start_div({class=>"problem"});
965 : sh002i 2505 print CGI::p($pg->{body_text});
966 :     print CGI::p(CGI::b("Note: "), CGI::i($pg->{result}->{msg})) if $pg->{result}->{msg};
967 :     print CGI::end_div();
968 :    
969 :     print CGI::start_p();
970 :    
971 :     if ($can{showCorrectAnswers}) {
972 :     print CGI::checkbox(
973 :     -name => "showCorrectAnswers",
974 :     -checked => $will{showCorrectAnswers},
975 :     -label => "Show correct answers",
976 : malsyned 755 );
977 : sh002i 2505 }
978 :     if ($can{showHints}) {
979 :     print CGI::div({style=>"color:red"},
980 :     CGI::checkbox(
981 :     -name => "showHints",
982 :     -checked => $will{showHints},
983 :     -label => "Show Hints",
984 :     )
985 :     );
986 :     }
987 :     if ($can{showSolutions}) {
988 :     print CGI::checkbox(
989 :     -name => "showSolutions",
990 :     -checked => $will{showSolutions},
991 :     -label => "Show Solutions",
992 :     );
993 :     }
994 :    
995 :     if ($can{showCorrectAnswers} or $can{showHints} or $can{showSolutions}) {
996 :     print CGI::br();
997 :     }
998 :    
999 :     print CGI::submit(-name=>"previewAnswers", -label=>"Preview Answers");
1000 :     if ($can{checkAnswers}) {
1001 :     print CGI::submit(-name=>"checkAnswers", -label=>"Check Answers");
1002 :     }
1003 : jj 2685 if ($can{getSubmitButton}) {
1004 : sh002i 2507 if ($user ne $effectiveUser) {
1005 :     # if acting as a student, make it clear that answer submissions will
1006 :     # apply to the student's records, not the professor's.
1007 :     print CGI::submit(-name=>"submitAnswers", -label=>"Submit Answers for $effectiveUser");
1008 :     } else {
1009 : sh002i 2505 print CGI::submit(-name=>"submitAnswers", -label=>"Submit Answers");
1010 : sh002i 2507 }
1011 : sh002i 2505 }
1012 :    
1013 :     print CGI::end_p();
1014 :    
1015 : gage 794 print CGI::start_div({class=>"scoreSummary"});
1016 : sh002i 1131
1017 : sh002i 431 # score summary
1018 :     my $attempts = $problem->num_correct + $problem->num_incorrect;
1019 :     my $attemptsNoun = $attempts != 1 ? "times" : "time";
1020 : malsyned 1034 my $lastScore = sprintf("%.0f%%", $problem->status * 100); # Round to whole number
1021 : sh002i 431 my ($attemptsLeft, $attemptsLeftNoun);
1022 :     if ($problem->max_attempts == -1) {
1023 :     # unlimited attempts
1024 :     $attemptsLeft = "unlimited";
1025 :     $attemptsLeftNoun = "attempts";
1026 :     } else {
1027 :     $attemptsLeft = $problem->max_attempts - $attempts;
1028 :     $attemptsLeftNoun = $attemptsLeft == 1 ? "attempt" : "attempts";
1029 :     }
1030 : malsyned 755
1031 :     my $setClosed = 0;
1032 : sh002i 476 my $setClosedMessage;
1033 : sh002i 2505 if (before($set->open_date) or after($set->due_date)) {
1034 : malsyned 755 $setClosed = 1;
1035 : sh002i 476 $setClosedMessage = "This problem set is closed.";
1036 : toenail 2398 if ($authz->hasPermissions($user, "view_answers")) {
1037 : sh002i 1131 $setClosedMessage .= " However, since you are a privileged user, additional attempts will be recorded.";
1038 : sh002i 476 } else {
1039 :     $setClosedMessage .= " Additional attempts will not be recorded.";
1040 :     }
1041 :     }
1042 : toenail 2099
1043 :     my $notCountedMessage = ($problem->value) ? "" : "(This problem will not count towards your grade.)";
1044 : sh002i 449 print CGI::p(
1045 : sh002i 1227 $submitAnswers ? $scoreRecordedMessage . CGI::br() : "",
1046 : sh002i 449 "You have attempted this problem $attempts $attemptsNoun.", CGI::br(),
1047 : sh002i 431 $problem->attempted
1048 : toenail 2099 ? "Your recorded score is $lastScore. $notCountedMessage" . CGI::br()
1049 : sh002i 431 : "",
1050 : malsyned 755 $setClosed ? $setClosedMessage : "You have $attemptsLeft $attemptsLeftNoun remaining."
1051 : sh002i 431 );
1052 : gage 794 print CGI::end_div();
1053 : malsyned 755
1054 : sh002i 1131 # save state for viewOptions
1055 : gage 1591 print CGI::hidden(
1056 :     -name => "showOldAnswers",
1057 :     -value => $will{showOldAnswers}
1058 :     ),
1059 : gage 1479
1060 : gage 1591 CGI::hidden(
1061 :     -name => "displayMode",
1062 :     -value => $self->{displayMode}
1063 :     );
1064 :     print( CGI::hidden(
1065 :     -name => 'editMode',
1066 :     -value => $self->{editMode},
1067 :     )
1068 :     ) if defined($self->{editMode}) and $self->{editMode} eq 'temporaryFile';
1069 :     print( CGI::hidden(
1070 :     -name => 'sourceFilePath',
1071 :     -value => $self->{problem}->{source_file}
1072 :     )) if defined($self->{problem}->{source_file});
1073 : jj 2251
1074 :     print( CGI::hidden(
1075 :     -name => 'problemSeed',
1076 :     -value => $r->param("problemSeed")
1077 :     )) if defined($r->param("problemSeed"));
1078 : gage 1591
1079 : sh002i 1131 # end of main form
1080 :     print CGI::endform();
1081 :    
1082 :     print CGI::start_div({class=>"problemFooter"});
1083 :    
1084 : sh002i 1908 ## arguments for answer inspection button
1085 :     #my $prof_url = $ce->{webworkURLs}->{oldProf};
1086 :     #my $webworkURL = $ce->{webworkURLs}->{root};
1087 :     #my $cgi_url = $prof_url;
1088 :     #$cgi_url=~ s|/[^/]*$||; # clip profLogin.pl
1089 :     #my $authen_args = $self->url_authen_args();
1090 :     #my $showPastAnswersURL = "$webworkURL/$courseName/instructor/show_answers/";
1091 : gage 794
1092 : sh002i 1908 my $pastAnswersPage = $urlpath->newFromModule("WeBWorK::ContentGenerator::Instructor::ShowAnswers",
1093 :     courseID => $courseName);
1094 :     my $showPastAnswersURL = $self->systemLink($pastAnswersPage, authen => 0); # no authen info for form action
1095 :    
1096 : gage 794 # print answer inspection button
1097 : toenail 2398 if ($authz->hasPermissions($user, "view_answers")) {
1098 : gage 794 print "\n",
1099 :     CGI::start_form(-method=>"POST",-action=>$showPastAnswersURL,-target=>"information"),"\n",
1100 : sh002i 1131 $self->hidden_authen_fields,"\n",
1101 : gage 1937 CGI::hidden(-name => 'courseID', -value=>$courseName), "\n",
1102 :     CGI::hidden(-name => 'problemID', -value=>$problem->problem_id), "\n",
1103 :     CGI::hidden(-name => 'setID', -value=>$problem->set_id), "\n",
1104 : gage 1395 CGI::hidden(-name => 'studentUser', -value=>$problem->user_id), "\n",
1105 : sh002i 1131 CGI::p( {-align=>"left"},
1106 :     CGI::submit(-name => 'action', -value=>'Show Past Answers')
1107 :     ), "\n",
1108 :     CGI::endform();
1109 :     }
1110 : sh002i 667
1111 : sh002i 1908 # feedback form url
1112 :     my $feedbackPage = $urlpath->newFromModule("WeBWorK::ContentGenerator::Feedback",
1113 :     courseID => $courseName);
1114 :     my $feedbackURL = $self->systemLink($feedbackPage, authen => 0); # no authen info for form action
1115 : gage 794
1116 : sh002i 1131 #print feedback form
1117 : gage 940 print
1118 :     CGI::start_form(-method=>"POST", -action=>$feedbackURL),"\n",
1119 :     $self->hidden_authen_fields,"\n",
1120 :     CGI::hidden("module", __PACKAGE__),"\n",
1121 :     CGI::hidden("set", $set->set_id),"\n",
1122 :     CGI::hidden("problem", $problem->problem_id),"\n",
1123 :     CGI::hidden("displayMode", $self->{displayMode}),"\n",
1124 :     CGI::hidden("showOldAnswers", $will{showOldAnswers}),"\n",
1125 :     CGI::hidden("showCorrectAnswers", $will{showCorrectAnswers}),"\n",
1126 :     CGI::hidden("showHints", $will{showHints}),"\n",
1127 :     CGI::hidden("showSolutions", $will{showSolutions}),"\n",
1128 :     CGI::p({-align=>"left"},
1129 : gage 1471 CGI::submit(-name=>"feedbackForm", -label=>"Email instructor")
1130 : gage 940 ),
1131 :     CGI::endform(),"\n";
1132 : sh002i 1908
1133 : gage 903 # FIXME print editor link
1134 : sh002i 1908 print $editorLink; #empty unless it is appropriate to have an editor link.
1135 : gage 906
1136 : sh002i 1131 print CGI::end_div();
1137 : gage 940
1138 : sh002i 424 # debugging stuff
1139 : sh002i 747 if (0) {
1140 : sh002i 738 print
1141 :     CGI::hr(),
1142 :     CGI::h2("debugging information"),
1143 :     CGI::h3("form fields"),
1144 :     ref2string($self->{formFields}),
1145 :     CGI::h3("user object"),
1146 :     ref2string($self->{user}),
1147 :     CGI::h3("set object"),
1148 :     ref2string($set),
1149 :     CGI::h3("problem object"),
1150 :     ref2string($problem),
1151 :     CGI::h3("PG object"),
1152 :     ref2string($pg, {'WeBWorK::PG::Translator' => 1});
1153 :     }
1154 : gage 392
1155 : sh002i 424 return "";
1156 : malsyned 353 }
1157 :    
1158 : jj 2539 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9