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

Annotation of /branches/gage_dev/webwork2/lib/WeBWorK/ContentGenerator/Problem.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1980 - (view) (download) (as text)
Original Path: trunk/webwork2/lib/WeBWorK/ContentGenerator/Problem.pm

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9