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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9