[system] / branches / rel-2-1-a1 / webwork-modperl / lib / WeBWorK / ContentGenerator / Problem.pm Repository:
ViewVC logotype

Annotation of /branches/rel-2-1-a1/webwork-modperl/lib/WeBWorK/ContentGenerator/Problem.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2156 - (view) (download) (as text)
Original Path: trunk/webwork-modperl/lib/WeBWorK/ContentGenerator/Problem.pm

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9