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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9