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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9