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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9