[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 2093 - (view) (download) (as text)

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9