| … | |
… | |
| 2 | # WeBWorK mod_perl (c) 2000-2002 WeBWorK Project |
2 | # WeBWorK mod_perl (c) 2000-2002 WeBWorK Project |
| 3 | # $Id$ |
3 | # $Id$ |
| 4 | ################################################################################ |
4 | ################################################################################ |
| 5 | |
5 | |
| 6 | package WeBWorK::ContentGenerator::Problem; |
6 | package WeBWorK::ContentGenerator::Problem; |
|
|
7 | use base qw(WeBWorK::ContentGenerator); |
| 7 | |
8 | |
| 8 | =head1 NAME |
9 | =head1 NAME |
| 9 | |
10 | |
| 10 | WeBWorK::ContentGenerator::Problem - Allow a student to interact with a problem. |
11 | WeBWorK::ContentGenerator::Problem - Allow a student to interact with a problem. |
| 11 | |
12 | |
| 12 | =cut |
13 | =cut |
| 13 | |
14 | |
| 14 | use strict; |
15 | use strict; |
| 15 | use warnings; |
16 | use warnings; |
| 16 | use base qw(WeBWorK::ContentGenerator); |
|
|
| 17 | use CGI qw(); |
17 | use CGI qw(); |
|
|
18 | use File::Path qw(rmtree); |
| 18 | use File::Temp qw(tempdir); |
19 | use File::Temp qw(tempdir); |
| 19 | use WeBWorK::Form; |
20 | use WeBWorK::Form; |
| 20 | use WeBWorK::PG; |
21 | use WeBWorK::PG; |
| 21 | use WeBWorK::PG::IO; |
22 | use WeBWorK::PG::IO; |
| 22 | use WeBWorK::Utils qw(writeLog encodeAnswers decodeAnswers ref2string); |
23 | use WeBWorK::Utils qw(writeLog encodeAnswers decodeAnswers ref2string); |
|
|
24 | use WeBWorK::DB::Utils qw(global2user user2global findDefaults); |
| 23 | |
25 | |
| 24 | ############################################################ |
26 | ############################################################ |
| 25 | # |
27 | # |
| 26 | # user |
28 | # user |
|
|
29 | # effectiveUser |
| 27 | # key |
30 | # key |
| 28 | # |
31 | # |
| 29 | # displayMode |
32 | # displayMode |
| 30 | # showOldAnswers |
33 | # showOldAnswers |
| 31 | # showCorrectAnswers |
34 | # showCorrectAnswers |
| … | |
… | |
| 34 | # |
37 | # |
| 35 | # AnSwEr# - answer blanks in problem |
38 | # AnSwEr# - answer blanks in problem |
| 36 | # |
39 | # |
| 37 | # redisplay - name of the "Redisplay Problem" button |
40 | # redisplay - name of the "Redisplay Problem" button |
| 38 | # submitAnswers - name of "Submit Answers" button |
41 | # submitAnswers - name of "Submit Answers" button |
|
|
42 | # checkAnswers - name of the "Check Answers" button |
|
|
43 | # previewAnswers - name of the "Preview Answers" button |
| 39 | # |
44 | # |
| 40 | ############################################################ |
45 | ############################################################ |
| 41 | |
46 | |
| 42 | sub pre_header_initialize { |
47 | sub pre_header_initialize { |
| 43 | my ($self, $setName, $problemNumber) = @_; |
48 | my ($self, $setName, $problemNumber) = @_; |
| 44 | my $courseEnv = $self->{courseEnvironment}; |
|
|
| 45 | my $r = $self->{r}; |
49 | my $r = $self->{r}; |
|
|
50 | my $courseEnv = $self->{ce}; |
|
|
51 | my $db = $self->{db}; |
| 46 | my $userName = $r->param('user'); |
52 | my $userName = $r->param('user'); |
|
|
53 | my $effectiveUserName = $r->param('effectiveUser'); |
| 47 | |
54 | |
| 48 | ##### database setup ##### |
|
|
| 49 | |
|
|
| 50 | my $cldb = WeBWorK::DB::Classlist->new($courseEnv); |
|
|
| 51 | my $wwdb = WeBWorK::DB::WW->new($courseEnv); |
|
|
| 52 | my $authdb = WeBWorK::DB::Auth->new($courseEnv); |
|
|
| 53 | |
|
|
| 54 | my $user = $cldb->getUser($userName); |
55 | my $user = $db->getUser($userName); |
|
|
56 | my $effectiveUser = $db->getUser($effectiveUserName); |
|
|
57 | |
|
|
58 | # obtain the effective user set, or if that is not yet defined obtain global set |
| 55 | my $set = $wwdb->getSet($userName, $setName); |
59 | my $set = $db->getGlobalUserSet($effectiveUserName, $setName); |
|
|
60 | unless (defined $set) { |
|
|
61 | my $userSetClass = $courseEnv->{dbLayout}->{set_user}->{record}; |
|
|
62 | $set = global2user($userSetClass, $db->getGlobalSet($setName)); |
|
|
63 | $set->psvn('000'); |
|
|
64 | } |
|
|
65 | my $psvn = $set->psvn(); |
|
|
66 | |
|
|
67 | # obtain the effective user problem, or if that is not yet defined obtain global problem |
| 56 | my $problem = $wwdb->getProblem($userName, $setName, $problemNumber); |
68 | my $problem = $db->getGlobalUserProblem($effectiveUserName, $setName, $problemNumber); |
| 57 | my $psvn = $wwdb->getPSVN($userName, $setName); |
69 | unless (defined $problem) { |
|
|
70 | my $userProblemClass = $courseEnv->{dbLayout}->{problem_user}->{record}; |
|
|
71 | $problem = global2user($userProblemClass, $db->getGlobalProblem($setName,$problemNumber)); |
|
|
72 | |
|
|
73 | # $problem->max_attempts(-1); # default is infinite number of attempts |
|
|
74 | # $problem->value; |
|
|
75 | # $problem->problem_seed; |
|
|
76 | # $problem->source_file; |
|
|
77 | $problem->user_id($userName); # is this the right value for this unassigned problem? FIXME |
|
|
78 | $problem->status(0); |
|
|
79 | $problem->attempted(0); |
|
|
80 | $problem->num_correct(0); |
|
|
81 | $problem->num_incorrect(0); |
|
|
82 | $problem->last_answer(' '); |
|
|
83 | } |
|
|
84 | #$problem = $db->getGlobalProblem($setName, $problemNumber) unless defined($problem); |
|
|
85 | # FIXME |
|
|
86 | # a better solution at this point would be to take set and problem, convert them to global_user type |
|
|
87 | # so that they have the right methods. |
|
|
88 | # Stuff the local copy of $set and $problem with default data where it won't have been defined |
|
|
89 | # Make sure that nothing bad is stored back in the database. |
|
|
90 | # It would be nice to store lastAnswer somewhere -- perhaps that could be done as a special case. |
|
|
91 | |
|
|
92 | |
|
|
93 | # This supplies a psvn if $set doesn't have it. Unfortunately the problem is called on to provide |
|
|
94 | # data in many places and it might not even have methods defined. |
|
|
95 | |
|
|
96 | # global sets will not have a defined psvn |
|
|
97 | # my $psvn; |
|
|
98 | # if ($set->can('psvn') ) { |
|
|
99 | # $psvn = $set->psvn(); |
|
|
100 | # } else { # we are viewing an unassigned problem set, psvn's are irrelevant |
|
|
101 | # $psvn = '0000'; |
|
|
102 | # } |
|
|
103 | |
| 58 | my $permissionLevel = $authdb->getPermissions($userName); |
104 | my $permissionLevel = $db->getPermissionLevel($userName)->permission(); |
|
|
105 | |
|
|
106 | $self->{userName} = $userName; |
|
|
107 | $self->{user} = $user; |
|
|
108 | $self->{effectiveUser} = $effectiveUser; |
|
|
109 | $self->{set} = $set; |
|
|
110 | $self->{problem} = $problem; |
|
|
111 | $self->{permissionLevel} = $permissionLevel; |
| 59 | |
112 | |
| 60 | ##### form processing ##### |
113 | ##### form processing ##### |
| 61 | |
114 | |
| 62 | # set options from form fields (see comment at top of file for names) |
115 | # set options from form fields (see comment at top of file for names) |
| 63 | my $displayMode = $r->param("displayMode") || $courseEnv->{pg}->{options}->{displayMode}; |
116 | my $displayMode = $r->param("displayMode") || $courseEnv->{pg}->{options}->{displayMode}; |
| 64 | my $redisplay = $r->param("redisplay"); |
117 | my $redisplay = $r->param("redisplay"); |
| 65 | my $submitAnswers = $r->param("submitAnswers"); |
118 | my $submitAnswers = $r->param("submitAnswers"); |
|
|
119 | my $checkAnswers = $r->param("checkAnswers"); |
| 66 | my $previewAnswers = $r->param("previewAnswers"); |
120 | my $previewAnswers = $r->param("previewAnswers"); |
| 67 | |
121 | |
|
|
122 | # fields which may be defined when using Problem Editor |
|
|
123 | my $override_seed = ($permissionLevel>=10) ? $r->param('problemSeed') : undef; |
|
|
124 | my $override_problem_source = ($permissionLevel>=10) ? $r->param('sourceFilePath') : undef; |
|
|
125 | my $editMode = undef; |
|
|
126 | my $submit_button = $r->param('submit_button'); |
|
|
127 | if ( defined($submit_button ) ) { |
|
|
128 | $editMode = "temporaryFile" if $submit_button eq 'Refresh'; |
|
|
129 | $editMode = 'savedFile' if $submit_button eq 'Save'; |
|
|
130 | } |
|
|
131 | |
|
|
132 | #override using the source file data from the form field |
|
|
133 | $problem->source_file($override_problem_source) if defined($override_problem_source); |
|
|
134 | $problem->problem_seed($override_seed) if defined($override_seed); |
|
|
135 | |
|
|
136 | # store path to source file for title. |
|
|
137 | $self->{problem_source_name} = $problem->source_file; |
|
|
138 | $self->{edit_mode} = $editMode; |
|
|
139 | # $self->{current_problem_source} = (defined($override_problem_source) ) ? |
|
|
140 | # $override_problem_source : |
|
|
141 | # $problem->source_file; |
| 68 | # coerce form fields into CGI::Vars format |
142 | # coerce form fields into CGI::Vars format |
| 69 | my $formFields = { WeBWorK::Form->new_from_paramable($r)->Vars }; |
143 | my $formFields = { WeBWorK::Form->new_from_paramable($r)->Vars }; |
| 70 | |
144 | |
|
|
145 | $self->{displayMode} = $displayMode; |
|
|
146 | $self->{redisplay} = $redisplay; |
|
|
147 | $self->{submitAnswers} = $submitAnswers; |
|
|
148 | $self->{checkAnswers} = $checkAnswers; |
|
|
149 | $self->{previewAnswers} = $previewAnswers; |
|
|
150 | $self->{formFields} = $formFields; |
|
|
151 | |
|
|
152 | |
| 71 | ##### permissions ##### |
153 | ##### permissions ##### |
|
|
154 | |
|
|
155 | # are we allowed to view this problem? |
|
|
156 | $self->{isOpen} = time >= $set->open_date || $permissionLevel > 0; |
|
|
157 | return unless $self->{isOpen}; |
| 72 | |
158 | |
| 73 | # what does the user want to do? |
159 | # what does the user want to do? |
| 74 | my %want = ( |
160 | my %want = ( |
| 75 | showOldAnswers => $r->param("showOldAnswers") || $courseEnv->{pg}->{options}->{showOldAnswers}, |
161 | showOldAnswers => $r->param("showOldAnswers") || $courseEnv->{pg}->{options}->{showOldAnswers}, |
| 76 | showCorrectAnswers => $r->param("showCorrectAnswers") || $courseEnv->{pg}->{options}->{showCorrectAnswers}, |
162 | showCorrectAnswers => $r->param("showCorrectAnswers") || $courseEnv->{pg}->{options}->{showCorrectAnswers}, |
| 77 | showHints => $r->param("showHints") || $courseEnv->{pg}->{options}->{showHints}, |
163 | showHints => $r->param("showHints") || $courseEnv->{pg}->{options}->{showHints}, |
| 78 | showSolutions => $r->param("showSolutions") || $courseEnv->{pg}->{options}->{showSolutions}, |
164 | showSolutions => $r->param("showSolutions") || $courseEnv->{pg}->{options}->{showSolutions}, |
| 79 | recordAnswers => $r->param("recordAnswers") || 1, |
165 | recordAnswers => $submitAnswers, |
|
|
166 | checkAnswers => $checkAnswers, |
| 80 | ); |
167 | ); |
| 81 | |
168 | |
| 82 | # are certain options enforced? |
169 | # are certain options enforced? |
| 83 | my %must = ( |
170 | my %must = ( |
| 84 | showOldAnswers => 0, |
171 | showOldAnswers => 0, |
| 85 | showCorrectAnswers => 0, |
172 | showCorrectAnswers => 0, |
| 86 | showHints => 0, |
173 | showHints => 0, |
| 87 | showSolutions => 0, |
174 | showSolutions => 0, |
| 88 | recordAnswers => mustRecordAnswers($permissionLevel), |
175 | recordAnswers => mustRecordAnswers($permissionLevel), |
|
|
176 | checkAnswers => 0, |
| 89 | ); |
177 | ); |
| 90 | |
178 | |
| 91 | # does the user have permission to use certain options? |
179 | # does the user have permission to use certain options? |
| 92 | my %can = ( |
180 | my %can = ( |
| 93 | showOldAnswers => 1, |
181 | showOldAnswers => 1, |
| 94 | showCorrectAnswers => canShowCorrectAnswers($permissionLevel, $set->answer_date), |
182 | showCorrectAnswers => canShowCorrectAnswers($permissionLevel, $set->answer_date), |
| 95 | showHints => 1, |
183 | showHints => 1, |
| 96 | showSolutions => canShowSolutions($permissionLevel, $set->answer_date), |
184 | showSolutions => canShowSolutions($permissionLevel, $set->answer_date), |
| 97 | recordAnswers => canRecordAnswers($permissionLevel, $set->open_date, $set->due_date, |
185 | recordAnswers => canRecordAnswers($permissionLevel, $set->open_date, $set->due_date, |
| 98 | $problem->max_attempts, $problem->num_correct + $problem->num_incorrect + 1), |
186 | $problem->max_attempts, $problem->num_correct + $problem->num_incorrect + 1), |
| 99 | # num_correct+num_incorrect+1 -- as this happens before updating $problem |
187 | # attempts=num_correct+num_incorrect+1, as this happens before updating $problem |
|
|
188 | checkAnswers => canCheckAnswers($permissionLevel, $set->answer_date), |
| 100 | ); |
189 | ); |
| 101 | |
190 | |
| 102 | # final values for options |
191 | # final values for options |
| 103 | my %will; |
192 | my %will; |
| 104 | foreach (keys %must) { |
193 | foreach (keys %must) { |
| … | |
… | |
| 115 | |
204 | |
| 116 | ##### translation ##### |
205 | ##### translation ##### |
| 117 | |
206 | |
| 118 | my $pg = WeBWorK::PG->new( |
207 | my $pg = WeBWorK::PG->new( |
| 119 | $courseEnv, |
208 | $courseEnv, |
| 120 | $user, |
209 | $effectiveUser, |
| 121 | $r->param('key'), |
210 | $r->param('key'), |
| 122 | $set, |
211 | $set, |
| 123 | $problem, |
212 | $problem, |
| 124 | $psvn, |
213 | $psvn, |
| 125 | $formFields, |
214 | $formFields, |
| 126 | { # translation options |
215 | { # translation options |
| 127 | displayMode => $displayMode, |
216 | displayMode => $displayMode, |
|
|
217 | override_seed => $override_seed, |
|
|
218 | override_problem_source =>$override_problem_source, |
| 128 | showHints => $will{showHints}, |
219 | showHints => $will{showHints}, |
| 129 | showSolutions => $will{showSolutions}, |
220 | showSolutions => $will{showSolutions}, |
| 130 | refreshMath2img => $will{showHints} || $will{showSolutions}, |
221 | refreshMath2img => $will{showHints} || $will{showSolutions}, |
| 131 | processAnswers => 1, |
222 | processAnswers => 1, |
| 132 | }, |
223 | }, |
| … | |
… | |
| 137 | $can{showHints} &&= $pg->{flags}->{hintExists}; |
228 | $can{showHints} &&= $pg->{flags}->{hintExists}; |
| 138 | $can{showSolutions} &&= $pg->{flags}->{solutionExists}; |
229 | $can{showSolutions} &&= $pg->{flags}->{solutionExists}; |
| 139 | |
230 | |
| 140 | ##### store fields ##### |
231 | ##### store fields ##### |
| 141 | |
232 | |
| 142 | $self->{cldb} = $cldb; |
|
|
| 143 | $self->{wwdb} = $wwdb; |
|
|
| 144 | $self->{authdb} = $authdb; |
|
|
| 145 | |
|
|
| 146 | $self->{user} = $user; |
|
|
| 147 | $self->{set} = $set; |
|
|
| 148 | $self->{problem} = $problem; |
|
|
| 149 | $self->{permissionLevel} = $permissionLevel; |
|
|
| 150 | |
|
|
| 151 | $self->{displayMode} = $displayMode; |
|
|
| 152 | $self->{redisplay} = $redisplay; |
|
|
| 153 | $self->{submitAnswers} = $submitAnswers; |
|
|
| 154 | $self->{previewAnswers} = $previewAnswers; |
|
|
| 155 | $self->{formFields} = $formFields; |
|
|
| 156 | |
|
|
| 157 | $self->{want} = \%want; |
233 | $self->{want} = \%want; |
| 158 | $self->{must} = \%must; |
234 | $self->{must} = \%must; |
| 159 | $self->{can} = \%can; |
235 | $self->{can} = \%can; |
| 160 | $self->{will} = \%will; |
236 | $self->{will} = \%will; |
| 161 | |
237 | |
| 162 | $self->{pg} = $pg; |
238 | $self->{pg} = $pg; |
| 163 | } |
239 | } |
| 164 | |
240 | |
| 165 | sub if_warnings($$) { |
241 | sub if_warnings($$) { |
| 166 | my ($self, $arg) = @_; |
242 | my ($self, $arg) = @_; |
|
|
243 | return 0 unless $self->{isOpen}; |
| 167 | return $self->{pg}->{warnings} ne ""; |
244 | return $self->{pg}->{warnings} ne ""; |
| 168 | } |
245 | } |
| 169 | |
246 | |
| 170 | sub if_errors($$) { |
247 | sub if_errors($$) { |
| 171 | my ($self, $arg) = @_; |
248 | my ($self, $arg) = @_; |
|
|
249 | return 0 unless $self->{isOpen}; |
| 172 | return $self->{pg}->{flags}->{error_flag}; |
250 | return $self->{pg}->{flags}->{error_flag}; |
| 173 | } |
251 | } |
| 174 | |
252 | |
| 175 | sub head { |
253 | sub head { |
| 176 | my $self = shift; |
254 | my $self = shift; |
| 177 | |
255 | return "" unless $self->{isOpen}; |
| 178 | return $self->{pg}->{head_text} if $self->{pg}->{head_text}; |
256 | return $self->{pg}->{head_text} if $self->{pg}->{head_text}; |
| 179 | } |
257 | } |
| 180 | |
258 | |
| 181 | sub path { |
259 | sub path { |
| 182 | my $self = shift; |
260 | my $self = shift; |
| 183 | my $args = $_[-1]; |
261 | my $args = $_[-1]; |
| 184 | my $setName = $self->{set}->id; |
262 | my $setName = $self->{set}->set_id; |
| 185 | my $problemNumber = $self->{problem}->id; |
263 | my $problemNumber = $self->{problem}->problem_id; |
| 186 | |
264 | |
| 187 | my $ce = $self->{courseEnvironment}; |
265 | my $ce = $self->{ce}; |
| 188 | my $root = $ce->{webworkURLs}->{root}; |
266 | my $root = $ce->{webworkURLs}->{root}; |
| 189 | my $courseName = $ce->{courseName}; |
267 | my $courseName = $ce->{courseName}; |
| 190 | return $self->pathMacro($args, |
268 | return $self->pathMacro($args, |
| 191 | "Home" => "$root", |
269 | "Home" => "$root", |
| 192 | $courseName => "$root/$courseName", |
270 | $courseName => "$root/$courseName", |
| … | |
… | |
| 195 | ); |
273 | ); |
| 196 | } |
274 | } |
| 197 | |
275 | |
| 198 | sub siblings { |
276 | sub siblings { |
| 199 | my $self = shift; |
277 | my $self = shift; |
| 200 | my $setName = $self->{set}->id; |
278 | my $setName = $self->{set}->set_id; |
| 201 | my $problemNumber = $self->{problem}->id; |
279 | my $problemNumber = $self->{problem}->problem_id; |
| 202 | |
280 | |
| 203 | my $ce = $self->{courseEnvironment}; |
281 | my $ce = $self->{ce}; |
|
|
282 | my $db = $self->{db}; |
| 204 | my $root = $ce->{webworkURLs}->{root}; |
283 | my $root = $ce->{webworkURLs}->{root}; |
| 205 | my $courseName = $ce->{courseName}; |
284 | my $courseName = $ce->{courseName}; |
| 206 | |
285 | |
| 207 | print CGI::strong("Problems"), CGI::br(); |
286 | print CGI::strong("Problems"), CGI::br(); |
| 208 | |
287 | |
| 209 | my $wwdb = $self->{wwdb}; |
|
|
| 210 | my $user = $self->{r}->param("user"); |
288 | my $effectiveUser = $self->{r}->param("effectiveUser"); |
| 211 | my @problems; |
289 | my @problems; |
| 212 | push @problems, $wwdb->getProblem($user, $setName, $_) |
290 | push @problems, $db->getGlobalUserProblem($effectiveUser, $setName, $_) |
| 213 | foreach ($wwdb->getProblems($user, $setName)); |
291 | foreach ($db->listUserProblems($effectiveUser, $setName)); |
| 214 | foreach my $problem (sort { $a->id <=> $b->id } @problems) { |
292 | foreach my $problem (sort { $a->problem_id <=> $b->problem_id } @problems) { |
| 215 | print CGI::a({-href=>"$root/$courseName/$setName/".$problem->id."/?" |
293 | print CGI::a({-href=>"$root/$courseName/$setName/".$problem->problem_id."/?" |
| 216 | . $self->url_authen_args}, "Problem ".$problem->id), CGI::br(); |
294 | . $self->url_authen_args . "&displayMode=" . $self->{displayMode}}, |
|
|
295 | "Problem ".$problem->problem_id), CGI::br(); |
| 217 | } |
296 | } |
| 218 | } |
297 | } |
| 219 | |
298 | |
| 220 | sub nav { |
299 | sub nav { |
| 221 | my $self = shift; |
300 | my $self = shift; |
| 222 | my $args = $_[-1]; |
301 | my $args = $_[-1]; |
| 223 | my $setName = $self->{set}->id; |
302 | my $setName = $self->{set}->set_id; |
| 224 | my $problemNumber = $self->{problem}->id; |
303 | my $problemNumber = $self->{problem}->problem_id; |
| 225 | |
304 | |
| 226 | my $ce = $self->{courseEnvironment}; |
305 | my $ce = $self->{ce}; |
|
|
306 | my $db = $self->{db}; |
| 227 | my $root = $ce->{webworkURLs}->{root}; |
307 | my $root = $ce->{webworkURLs}->{root}; |
| 228 | my $courseName = $ce->{courseName}; |
308 | my $courseName = $ce->{courseName}; |
| 229 | |
309 | |
| 230 | my $wwdb = $self->{wwdb}; |
310 | my $wwdb = $self->{wwdb}; |
| 231 | my $user = $self->{r}->param("user"); |
311 | my $effectiveUser = $self->{r}->param("effectiveUser"); |
|
|
312 | my $tail = "&displayMode=".$self->{displayMode}; |
| 232 | |
313 | |
| 233 | my @links = ("Problem List" => "$root/$courseName/$setName"); |
314 | my @links = ("Problem List" , "$root/$courseName/$setName", "navProbList"); |
| 234 | |
315 | |
| 235 | my $prevProblem = $wwdb->getProblem($user, $setName, $problemNumber-1); |
316 | my $prevProblem = $db->getGlobalUserProblem($effectiveUser, $setName, $problemNumber-1); |
| 236 | my $nextProblem = $wwdb->getProblem($user, $setName, $problemNumber+1); |
317 | my $nextProblem = $db->getGlobalUserProblem($effectiveUser, $setName, $problemNumber+1); |
| 237 | unshift @links, "Previous Problem" => $prevProblem |
318 | unshift @links, "Previous Problem" , ($prevProblem |
| 238 | ? "$root/$courseName/$setName/".$prevProblem->id |
319 | ? "$root/$courseName/$setName/".$prevProblem->problem_id |
| 239 | : ""; |
320 | : "") , "navPrev"; |
| 240 | push @links, "Next Problem" => $nextProblem |
321 | push @links, "Next Problem" , ($nextProblem |
| 241 | ? "$root/$courseName/$setName/".$nextProblem->id |
322 | ? "$root/$courseName/$setName/".$nextProblem->problem_id |
| 242 | : ""; |
323 | : "") , "navNext"; |
| 243 | |
324 | |
| 244 | return $self->navMacro($args, @links); |
325 | return $self->navMacro($args, $tail, @links); |
| 245 | } |
326 | } |
| 246 | |
327 | |
| 247 | sub title { |
328 | sub title { |
| 248 | my $self = shift; |
329 | my $self = shift; |
| 249 | my $setName = $self->{set}->id; |
330 | my $setName = $self->{set}->set_id; |
| 250 | my $problemNumber = $self->{problem}->id; |
331 | |
|
|
332 | my $file_action; |
|
|
333 | my $edit_mode = $self->{edit_mode}; |
|
|
334 | if ( not defined($edit_mode) ) { |
|
|
335 | $file_action = ''; |
|
|
336 | } elsif ( $edit_mode eq 'temporaryFile') { |
|
|
337 | $file_action .= 'Editing temporary file : '. CGI::br() . $self->{problem_source_name}; |
|
|
338 | } elsif ( $edit_mode eq 'savedFile' ){ |
|
|
339 | $file_action .= 'Problem saved to : '. CGI::br() . $self->{problem_source_name}; |
|
|
340 | } |
|
|
341 | my $problemNumber = $self->{problem}->problem_id . " : " . $file_action; |
| 251 | |
342 | |
| 252 | return "$setName : Problem $problemNumber"; |
343 | return "$setName : Problem $problemNumber"; |
| 253 | } |
344 | } |
| 254 | |
345 | |
| 255 | sub body { |
346 | sub body { |
| 256 | my $self = shift; |
347 | my $self = shift; |
| 257 | |
348 | |
|
|
349 | return CGI::p(CGI::font({-color=>"red"}, "This problem is not available because the problem set that contains it is not yet open.")) |
|
|
350 | unless $self->{isOpen}; |
|
|
351 | |
| 258 | # unpack some useful variables |
352 | # unpack some useful variables |
| 259 | my $r = $self->{r}; |
353 | my $r = $self->{r}; |
| 260 | my $wwdb = $self->{wwdb}; |
354 | my $db = $self->{db}; |
| 261 | my $set = $self->{set}; |
355 | my $set = $self->{set}; |
| 262 | my $problem = $self->{problem}; |
356 | my $problem = $self->{problem}; |
| 263 | my $permissionLevel = $self->{permissionLevel}; |
357 | my $permissionLevel = $self->{permissionLevel}; |
| 264 | my $submitAnswers = $self->{submitAnswers}; |
358 | my $submitAnswers = $self->{submitAnswers}; |
|
|
359 | my $checkAnswers = $self->{checkAnswers}; |
| 265 | my $previewAnswers = $self->{previewAnswers}; |
360 | my $previewAnswers = $self->{previewAnswers}; |
|
|
361 | my %want = %{ $self->{want} }; |
|
|
362 | my %can = %{ $self->{can} }; |
|
|
363 | my %must = %{ $self->{must} }; |
| 266 | my %will = %{ $self->{will} }; |
364 | my %will = %{ $self->{will} }; |
| 267 | my $pg = $self->{pg}; |
365 | my $pg = $self->{pg}; |
| 268 | |
366 | |
| 269 | ##### translation errors? ##### |
367 | ##### translation errors? ##### |
| 270 | |
368 | |
| 271 | if ($pg->{flags}->{error_flag}) { |
369 | if ($pg->{flags}->{error_flag}) { |
| 272 | return translationError($pg->{errors}, $pg->{body_text}); |
370 | return $self->errorOutput($pg->{errors}, $pg->{body_text}); |
| 273 | } |
371 | } |
| 274 | |
372 | |
| 275 | ##### answer processing ##### |
373 | ##### answer processing ##### |
| 276 | |
374 | |
| 277 | # if answers were submitted: |
375 | # if answers were submitted: |
| 278 | if ($submitAnswers) { |
376 | if ($submitAnswers) { |
|
|
377 | # get a "pure" (unmerged) UserProblem to modify |
|
|
378 | # this will be undefined if the problem has not been assigned to this user |
|
|
379 | my $pureProblem = $db->getUserProblem($problem->user_id, $problem->set_id, $problem->problem_id); |
| 279 | # store answers in DB for sticky answers |
380 | # store answers in DB for sticky answers |
| 280 | my %answersToStore; |
381 | my %answersToStore; |
| 281 | my %answerHash = %{ $pg->{answers} }; |
382 | my %answerHash = %{ $pg->{answers} }; |
| 282 | $answersToStore{$_} = $answerHash{$_}->{original_student_ans} |
383 | $answersToStore{$_} = $answerHash{$_}->{original_student_ans} |
| 283 | foreach (keys %answerHash); |
384 | foreach (keys %answerHash); |
| 284 | my $answerString = encodeAnswers(%answersToStore, |
385 | my $answerString = encodeAnswers(%answersToStore, |
| 285 | @{ $pg->{flags}->{ANSWER_ENTRY_ORDER} }); |
386 | @{ $pg->{flags}->{ANSWER_ENTRY_ORDER} }); |
|
|
387 | |
| 286 | $problem->last_answer($answerString); |
388 | $problem->last_answer($answerString); |
|
|
389 | # if $pureProblem is defined ( there is a user ) then record the last answer. |
|
|
390 | # FIXME (we're assuming that not being able to get a pure problem is enough to determine |
|
|
391 | # whether or not the problem database needs to be updated. This should be thought through |
|
|
392 | # more carefully to be sure. |
|
|
393 | if ( defined($pureProblem) ) { |
|
|
394 | $pureProblem->last_answer($answerString); |
| 287 | $wwdb->setProblem($problem); |
395 | $db->putUserProblem($pureProblem); |
|
|
396 | #die "pureProblem = ", defined($pureProblem); |
| 288 | |
397 | |
|
|
398 | |
| 289 | # store state in DB if it makes sense |
399 | # store state in DB if it makes sense |
| 290 | if ($will{recordAnswers}) { |
400 | if ($will{recordAnswers}) { |
| 291 | $problem->attempted(1); |
|
|
| 292 | $problem->status($pg->{state}->{recorded_score}); |
401 | $problem->status($pg->{state}->{recorded_score}); |
|
|
402 | $problem->attempted(1); |
| 293 | $problem->num_correct($pg->{state}->{num_of_correct_ans}); |
403 | $problem->num_correct($pg->{state}->{num_of_correct_ans}); |
| 294 | $problem->num_incorrect($pg->{state}->{num_of_incorrect_ans}); |
404 | $problem->num_incorrect($pg->{state}->{num_of_incorrect_ans}); |
|
|
405 | $pureProblem->status($pg->{state}->{recorded_score}); |
|
|
406 | $pureProblem->attempted(1); |
|
|
407 | $pureProblem->num_correct($pg->{state}->{num_of_correct_ans}); |
|
|
408 | $pureProblem->num_incorrect($pg->{state}->{num_of_incorrect_ans}); |
| 295 | $wwdb->setProblem($problem); |
409 | $db->putUserProblem($pureProblem); |
| 296 | # write to the transaction log, just to make sure |
410 | # write to the transaction log, just to make sure |
| 297 | writeLog($self->{courseEnvironment}, "transaction", |
411 | writeLog($self->{ce}, "transaction", |
| 298 | $problem->id."\t". |
412 | $problem->problem_id."\t". |
| 299 | $problem->set_id."\t". |
413 | $problem->set_id."\t". |
| 300 | $problem->login_id."\t". |
414 | $problem->user_id."\t". |
| 301 | $problem->source_file."\t". |
415 | $problem->source_file."\t". |
| 302 | $problem->value."\t". |
416 | $problem->value."\t". |
| 303 | $problem->max_attempts."\t". |
417 | $problem->max_attempts."\t". |
| 304 | $problem->problem_seed."\t". |
418 | $problem->problem_seed."\t". |
| 305 | $problem->status."\t". |
419 | $pureProblem->status."\t". |
| 306 | $problem->attempted."\t". |
420 | $pureProblem->attempted."\t". |
| 307 | $problem->last_answer."\t". |
421 | $pureProblem->last_answer."\t". |
| 308 | $problem->num_correct."\t". |
422 | $pureProblem->num_correct."\t". |
| 309 | $problem->num_incorrect |
423 | $pureProblem->num_incorrect |
| 310 | ); |
424 | ); |
|
|
425 | } |
| 311 | } |
426 | } |
| 312 | } |
427 | } |
|
|
428 | # logging student answers |
|
|
429 | my $pastAnswerLog = undef; |
|
|
430 | if (defined( $self->{ce}->{webworkFiles}->{logs}->{'pastAnswerList'} )) { |
|
|
431 | |
|
|
432 | $pastAnswerLog = $self->{ce}->{webworkFiles}->{logs}->{'pastAnswerList'}; |
|
|
433 | |
|
|
434 | if ($submitAnswers and defined($pastAnswerLog) ) { |
|
|
435 | my $answerString = ""; |
|
|
436 | my %answerHash = %{ $pg->{answers} }; |
|
|
437 | $answerString = $answerString . $answerHash{$_}->{original_student_ans}."\t" |
|
|
438 | foreach (sort keys %answerHash); |
|
|
439 | $answerString = '' unless defined($answerString); # insure string is defined. |
|
|
440 | writeLog($self->{ce}, "pastAnswerList", |
|
|
441 | '|'.$problem->user_id. |
|
|
442 | '|'.$problem->set_id. |
|
|
443 | '|'.$problem->problem_id.'|'."\t". |
|
|
444 | time()."\t". |
|
|
445 | $answerString, |
|
|
446 | |
|
|
447 | ); |
|
|
448 | |
|
|
449 | } |
|
|
450 | |
|
|
451 | } |
|
|
452 | # end logging student answers |
| 313 | |
453 | |
| 314 | ##### output ##### |
454 | ##### output ##### |
| 315 | |
455 | print CGI::start_div({class=>"problemHeader"}); |
| 316 | # attempt summary |
456 | # attempt summary |
| 317 | if ($submitAnswers or $will{showCorrectAnswers}) { |
457 | if ($submitAnswers or $will{showCorrectAnswers}) { |
| 318 | # print this if user submitted answers OR requested correct answers |
458 | # print this if user submitted answers OR requested correct answers |
| 319 | print $self->attemptResults($pg, $submitAnswers, $will{showCorrectAnswers}, |
459 | print $self->attemptResults($pg, $submitAnswers, |
|
|
460 | $will{showCorrectAnswers}, |
| 320 | $pg->{flags}->{showPartialCorrectAnswers}, 0); |
461 | $pg->{flags}->{showPartialCorrectAnswers}, 1, 1); |
|
|
462 | } elsif ($checkAnswers) { |
|
|
463 | # print this if user previewed answers |
|
|
464 | print $self->attemptResults($pg, 1, 0, 1, 1, 1); |
|
|
465 | # show attempt answers |
|
|
466 | # don't show correct answers |
|
|
467 | # show attempt results (correctness) |
|
|
468 | # don't show attempt previews |
| 321 | } elsif ($previewAnswers) { |
469 | } elsif ($previewAnswers) { |
| 322 | # print this if user previewed answers |
470 | # print this if user previewed answers |
| 323 | print $self->attemptResults($pg, 1, 0, 0, 1); |
471 | print $self->attemptResults($pg, 1, 0, 0, 0, 1); |
| 324 | # don't show correctness |
472 | # show attempt answers |
| 325 | # don't show correct answers |
473 | # don't show correct answers |
|
|
474 | # don't show attempt results (correctness) |
|
|
475 | # show attempt previews |
| 326 | } |
476 | } |
| 327 | |
477 | |
|
|
478 | print CGI::end_div(); |
|
|
479 | |
|
|
480 | print CGI::start_div({class=>"problem"}); |
|
|
481 | #print CGI::hr(); |
|
|
482 | # main form |
|
|
483 | print |
|
|
484 | CGI::startform("POST", $r->uri), |
|
|
485 | $self->hidden_authen_fields, |
|
|
486 | CGI::p($pg->{body_text}), |
|
|
487 | CGI::p($pg->{result}->{msg} ? CGI::b("Note: ") : "", CGI::i($pg->{result}->{msg})), |
|
|
488 | CGI::p( |
|
|
489 | ($can{recordAnswers} |
|
|
490 | ? CGI::submit(-name=>"submitAnswers", |
|
|
491 | -label=>"Submit Answers") |
|
|
492 | : ""), |
|
|
493 | ($can{checkAnswers} |
|
|
494 | ? CGI::submit(-name=>"checkAnswers", |
|
|
495 | -label=>"Check Answers") |
|
|
496 | : ""), |
|
|
497 | CGI::submit(-name=>"previewAnswers", |
|
|
498 | -label=>"Preview Answers"), |
|
|
499 | ); |
|
|
500 | print CGI::end_div(); |
|
|
501 | |
|
|
502 | print CGI::start_div({class=>"scoreSummary"}); |
| 328 | # score summary |
503 | # score summary |
| 329 | my $attempts = $problem->num_correct + $problem->num_incorrect; |
504 | my $attempts = $problem->num_correct + $problem->num_incorrect; |
| 330 | my $attemptsNoun = $attempts != 1 ? "times" : "time"; |
505 | my $attemptsNoun = $attempts != 1 ? "times" : "time"; |
| 331 | my $lastScore = int ($problem->status * 100) . "%"; |
506 | my $lastScore = int ($problem->status * 100) . "%"; |
| 332 | my ($attemptsLeft, $attemptsLeftNoun); |
507 | my ($attemptsLeft, $attemptsLeftNoun); |
| … | |
… | |
| 336 | $attemptsLeftNoun = "attempts"; |
511 | $attemptsLeftNoun = "attempts"; |
| 337 | } else { |
512 | } else { |
| 338 | $attemptsLeft = $problem->max_attempts - $attempts; |
513 | $attemptsLeft = $problem->max_attempts - $attempts; |
| 339 | $attemptsLeftNoun = $attemptsLeft == 1 ? "attempt" : "attempts"; |
514 | $attemptsLeftNoun = $attemptsLeft == 1 ? "attempt" : "attempts"; |
| 340 | } |
515 | } |
|
|
516 | |
|
|
517 | my $setClosed = 0; |
| 341 | my $setClosedMessage; |
518 | my $setClosedMessage; |
| 342 | if (time < $set->open_date or time > $set->due_date) { |
519 | if (time < $set->open_date or time > $set->due_date) { |
|
|
520 | $setClosed = 1; |
| 343 | $setClosedMessage = "This problem set is closed."; |
521 | $setClosedMessage = "This problem set is closed."; |
| 344 | if ($permissionLevel > 0) { |
522 | if ($permissionLevel > 0) { |
| 345 | $setClosedMessage .= " Since you are a privileged user, additional attempts will be recorded."; |
523 | $setClosedMessage .= " Since you are a privileged user, additional attempts will be recorded."; |
| 346 | } else { |
524 | } else { |
| 347 | $setClosedMessage .= " Additional attempts will not be recorded."; |
525 | $setClosedMessage .= " Additional attempts will not be recorded."; |
| … | |
… | |
| 350 | print CGI::p( |
528 | print CGI::p( |
| 351 | "You have attempted this problem $attempts $attemptsNoun.", CGI::br(), |
529 | "You have attempted this problem $attempts $attemptsNoun.", CGI::br(), |
| 352 | $problem->attempted |
530 | $problem->attempted |
| 353 | ? "Your recorded score is $lastScore." . CGI::br() |
531 | ? "Your recorded score is $lastScore." . CGI::br() |
| 354 | : "", |
532 | : "", |
| 355 | "You have $attemptsLeft $attemptsLeftNoun remaining.", CGI::br(), |
533 | $setClosed ? $setClosedMessage : "You have $attemptsLeft $attemptsLeftNoun remaining." |
| 356 | $setClosedMessage, |
|
|
| 357 | ); |
534 | ); |
| 358 | |
|
|
| 359 | print CGI::hr(); |
535 | print CGI::end_div(); |
| 360 | |
536 | # print CGI::hr(), CGI::start_div({class=>"viewOptions"}); |
| 361 | # main form |
537 | # print $self->viewOptions(),CGI::end_div(), |
| 362 | print |
538 | # save state for viewOptions |
| 363 | CGI::startform("POST", $r->uri), |
539 | print CGI::hidden(-name => "showOldAnswers", |
| 364 | $self->hidden_authen_fields, |
540 | -value => $will{showOldAnswers}, |
| 365 | CGI::p(CGI::i($pg->{result}->{msg})), |
|
|
| 366 | CGI::p($pg->{body_text}), |
|
|
| 367 | CGI::p( |
|
|
| 368 | CGI::submit(-name=>"submitAnswers", -label=>"Submit Answers"), |
|
|
| 369 | CGI::submit(-name=>"previewAnswers", -label=>"Preview Answers"), |
|
|
| 370 | ), |
541 | ), |
| 371 | $self->viewOptions(), |
542 | CGI::hidden(-name => "showCorrectAnswers", |
|
|
543 | -value => $will{showCorrectAnswers}, |
|
|
544 | ), |
|
|
545 | CGI::hidden(-name => "showHints", |
|
|
546 | -value => $will{showHints}, |
|
|
547 | ), |
|
|
548 | CGI::hidden(-name => "showSolutions", |
|
|
549 | -value => $will{showSolutions}, |
|
|
550 | ), |
|
|
551 | CGI::hidden(-name => "displayMode", |
|
|
552 | -value => $self->{displayMode} |
|
|
553 | ); |
| 372 | CGI::endform(); |
554 | print CGI::endform(); |
| 373 | |
555 | |
|
|
556 | print CGI::start_div({class=>"problemFooter"}); |
| 374 | # feedback form |
557 | # feedback form |
| 375 | my $ce = $self->{courseEnvironment}; |
558 | my $ce = $self->{ce}; |
| 376 | my $root = $ce->{webworkURLs}->{root}; |
559 | my $root = $ce->{webworkURLs}->{root}; |
| 377 | my $courseName = $ce->{courseName}; |
560 | my $courseName = $ce->{courseName}; |
| 378 | my $feedbackURL = "$root/$courseName/feedback/"; |
561 | my $feedbackURL = "$root/$courseName/feedback/"; |
|
|
562 | |
|
|
563 | # arguments for answer inspection button |
|
|
564 | my $prof_url = $ce->{webworkURLs}->{oldProf}; |
|
|
565 | my $cgi_url = $prof_url; |
|
|
566 | $cgi_url=~ s|/[^/]*$||; # clip profLogin.pl |
|
|
567 | my $authen_args = $self->url_authen_args(); |
|
|
568 | my $showPastAnswersURL = "$cgi_url/showPastAnswers.pl"; |
|
|
569 | |
|
|
570 | |
|
|
571 | print CGI::end_div(); |
|
|
572 | print CGI::start_div(); |
|
|
573 | # print answer inspection button |
|
|
574 | if ($self->{permissionLevel} >0) { |
|
|
575 | |
|
|
576 | |
|
|
577 | print "\n", |
|
|
578 | CGI::start_form(-method=>"POST",-action=>$showPastAnswersURL,-target=>"information"),"\n", |
|
|
579 | $self->hidden_authen_fields,"\n", |
|
|
580 | CGI::hidden(-name => 'course', -value=>$courseName), "\n", |
|
|
581 | CGI::hidden(-name => 'probNum', -value=>$problem->problem_id), "\n", |
|
|
582 | CGI::hidden(-name => 'setNum', -value=>$problem->set_id), "\n", |
|
|
583 | CGI::hidden(-name => 'User', -value=>$problem->user_id), "\n", |
|
|
584 | CGI::p( {-align=>"left"}, |
|
|
585 | CGI::submit(-name => 'action', -value=>'Show Past Answers') |
|
|
586 | ), "\n", |
|
|
587 | CGI::endform(); |
|
|
588 | |
|
|
589 | |
|
|
590 | |
|
|
591 | } #print feedback form |
|
|
592 | |
|
|
593 | |
| 379 | print |
594 | print |
| 380 | CGI::startform("POST", $feedbackURL), |
595 | CGI::start_form(-method=>"POST", -action=>$feedbackURL),"\n", |
| 381 | $self->hidden_authen_fields, |
596 | $self->hidden_authen_fields,"\n", |
| 382 | CGI::hidden("module", __PACKAGE__), |
597 | CGI::hidden("module", __PACKAGE__),"\n", |
| 383 | CGI::hidden("set", $set->id), |
598 | CGI::hidden("set", $set->set_id),"\n", |
| 384 | CGI::hidden("problem", $problem->id), |
599 | CGI::hidden("problem", $problem->problem_id),"\n", |
| 385 | CGI::hidden("displayMode", $self->{displayMode}), |
600 | CGI::hidden("displayMode", $self->{displayMode}),"\n", |
| 386 | CGI::hidden("showOldAnswers", $will{showOldAnswers}), |
601 | CGI::hidden("showOldAnswers", $will{showOldAnswers}),"\n", |
| 387 | CGI::hidden("showCorrectAnswers", $will{showCorrectAnswers}), |
602 | CGI::hidden("showCorrectAnswers", $will{showCorrectAnswers}),"\n", |
| 388 | CGI::hidden("showHints", $will{showHints}), |
603 | CGI::hidden("showHints", $will{showHints}),"\n", |
| 389 | CGI::hidden("showSolutions", $will{showSolutions}), |
604 | CGI::hidden("showSolutions", $will{showSolutions}),"\n", |
| 390 | CGI::p({-align=>"right"}, |
605 | CGI::p({-align=>"left"}, |
| 391 | CGI::submit(-name=>"feedbackForm", -label=>"Send Feedback") |
606 | CGI::submit(-name=>"feedbackForm", -label=>"Contact instructor") |
| 392 | ), |
607 | ), |
| 393 | CGI::endform(); |
608 | CGI::endform(),"\n"; |
|
|
609 | |
|
|
610 | # FIXME print editor link |
|
|
611 | # print editor link if the user is an instructor AND the file is not in temporary editing mode |
|
|
612 | if ($self->{permissionLevel}>=10 and ( (not defined($self->{edit_mode})) or $self->{edit_mode} eq 'savedFile') ) { |
|
|
613 | print CGI::a({-href=>"/webwork/$courseName/instructor/pgProblemEditor/".$set->set_id. |
|
|
614 | '/'.$problem->problem_id.'?'.$self->url_authen_args},'Edit this problem'); |
|
|
615 | } |
| 394 | |
616 | |
|
|
617 | print CGI::end_div(); |
|
|
618 | |
|
|
619 | # end answer inspection button |
| 395 | # warning output |
620 | # warning output |
| 396 | if ($pg->{warnings} ne "") { |
621 | if ($pg->{warnings} ne "") { |
| 397 | print CGI::hr(), warningOutput($pg->{warnings}); |
622 | print CGI::hr(), $self->warningOutput($pg->{warnings}); |
| 398 | } |
623 | } |
| 399 | |
624 | |
| 400 | # debugging stuff |
625 | # debugging stuff |
|
|
626 | if (0) { |
| 401 | print |
627 | print |
| 402 | CGI::hr(), |
628 | CGI::hr(), |
| 403 | CGI::h2("debugging information"), |
629 | CGI::h2("debugging information"), |
| 404 | CGI::h3("form fields"), |
630 | CGI::h3("form fields"), |
| 405 | ref2string($self->{formFields}), |
631 | ref2string($self->{formFields}), |
| 406 | CGI::h3("user object"), |
632 | CGI::h3("user object"), |
| 407 | ref2string($self->{user}), |
633 | ref2string($self->{user}), |
| 408 | CGI::h3("set object"), |
634 | CGI::h3("set object"), |
| 409 | ref2string($set), |
635 | ref2string($set), |
| 410 | CGI::h3("problem object"), |
636 | CGI::h3("problem object"), |
| 411 | ref2string($problem), |
637 | ref2string($problem), |
| 412 | CGI::h3("PG object"), |
638 | CGI::h3("PG object"), |
| 413 | ref2string($pg, {'WeBWorK::PG::Translator' => 1}); |
639 | ref2string($pg, {'WeBWorK::PG::Translator' => 1}); |
|
|
640 | } |
| 414 | |
641 | |
| 415 | return ""; |
642 | return ""; |
| 416 | } |
643 | } |
| 417 | |
644 | |
| 418 | ##### output utilities ##### |
645 | ##### output utilities ##### |
| 419 | |
646 | |
| 420 | # this is used by ProblemSet.pm too, so don't fuck it up |
|
|
| 421 | sub translationError($$) { |
|
|
| 422 | my ($error, $details) = @_; |
|
|
| 423 | return |
|
|
| 424 | CGI::h2("Software Error"), |
|
|
| 425 | CGI::p(<<EOF), |
|
|
| 426 | WeBWorK has encountered a software error while attempting to process this problem. |
|
|
| 427 | It is likely that there is an error in the problem itself. |
|
|
| 428 | If you are a student, contact your professor to have the error corrected. |
|
|
| 429 | If you are a professor, please consut the error output below for more informaiton. |
|
|
| 430 | EOF |
|
|
| 431 | CGI::h3("Error messages"), CGI::blockquote(CGI::pre($error)), |
|
|
| 432 | CGI::h3("Error context"), CGI::blockquote(CGI::pre($details)); |
|
|
| 433 | } |
|
|
| 434 | |
|
|
| 435 | # this is used by ProblemSet.pm too, so don't fuck it up |
|
|
| 436 | sub warningOutput($) { |
|
|
| 437 | my $warnings = shift; |
|
|
| 438 | |
|
|
| 439 | return |
|
|
| 440 | CGI::h2("Software Warnings"), |
|
|
| 441 | CGI::p(<<EOF), |
|
|
| 442 | WeBWorK has encountered warnings while attempting to process this problem. |
|
|
| 443 | It is likely that this indicates an error or ambiguity in the problem itself. |
|
|
| 444 | If you are a student, contact your professor to have the problem corrected. |
|
|
| 445 | If you are a professor, please consut the error output below for more informaiton. |
|
|
| 446 | EOF |
|
|
| 447 | CGI::h3("Warning messages"), |
|
|
| 448 | CGI::blockquote(CGI::pre($warnings)), |
|
|
| 449 | ; |
|
|
| 450 | } |
|
|
| 451 | |
|
|
| 452 | sub attemptResults($$$$$) { |
647 | sub attemptResults($$$$$$) { |
| 453 | my $self = shift; |
648 | my $self = shift; |
| 454 | my $pg = shift; |
649 | my $pg = shift; |
| 455 | my $showAttemptAnswers = shift; |
650 | my $showAttemptAnswers = shift; |
| 456 | my $showCorrectAnswers = shift; |
651 | my $showCorrectAnswers = shift; |
| 457 | my $showAttemptResults = $showAttemptAnswers && shift; |
652 | my $showAttemptResults = $showAttemptAnswers && shift; |
|
|
653 | my $showSummary = shift; |
| 458 | my $showAttemptPreview = shift || 0; |
654 | my $showAttemptPreview = shift || 0; |
| 459 | my $problemResult = $pg->{result}; # the overall result of the problem |
655 | my $problemResult = $pg->{result}; # the overall result of the problem |
| 460 | my @answerNames = @{ $pg->{flags}->{ANSWER_ENTRY_ORDER} }; |
656 | my @answerNames = @{ $pg->{flags}->{ANSWER_ENTRY_ORDER} }; |
| 461 | |
657 | |
| 462 | my $showMessages = $showAttemptAnswers && grep { $pg->{answers}->{$_}->{ans_message} } @answerNames; |
658 | my $showMessages = $showAttemptAnswers && grep { $pg->{answers}->{$_}->{ans_message} } @answerNames; |
| 463 | |
659 | |
| 464 | my $header = CGI::th("part"); |
660 | my $header = CGI::th("Part"); |
| 465 | $header .= $showAttemptAnswers ? CGI::th("entered") : ""; |
661 | $header .= $showAttemptAnswers ? CGI::th("Entered") : ""; |
| 466 | $header .= $showAttemptPreview ? CGI::th("preview") : ""; |
662 | $header .= $showAttemptPreview ? CGI::th("Answer Preview") : ""; |
| 467 | $header .= $showCorrectAnswers ? CGI::th("correct") : ""; |
663 | $header .= $showCorrectAnswers ? CGI::th("Correct") : ""; |
| 468 | $header .= $showAttemptResults ? CGI::th("result") : ""; |
664 | $header .= $showAttemptResults ? CGI::th("Result") : ""; |
| 469 | $header .= $showMessages ? CGI::th("messages") : ""; |
665 | $header .= $showMessages ? CGI::th("messages") : ""; |
| 470 | my @tableRows = ( $header ); |
666 | my @tableRows = ( $header ); |
| 471 | my $numCorrect; |
667 | my $numCorrect; |
| 472 | foreach my $name (@answerNames) { |
668 | foreach my $name (@answerNames) { |
| 473 | my $answerResult = $pg->{answers}->{$name}; |
669 | my $answerResult = $pg->{answers}->{$name}; |
| … | |
… | |
| 493 | $row .= $showAttemptResults ? CGI::td($resultString) : ""; |
689 | $row .= $showAttemptResults ? CGI::td($resultString) : ""; |
| 494 | $row .= $answerMessage ? CGI::td($answerMessage) : ""; |
690 | $row .= $answerMessage ? CGI::td($answerMessage) : ""; |
| 495 | push @tableRows, $row; |
691 | push @tableRows, $row; |
| 496 | } |
692 | } |
| 497 | |
693 | |
| 498 | my $numCorrectNoun = $numCorrect == 1 ? "question" : "questions"; |
694 | my $numIncorrectNoun = scalar @answerNames == 1 ? "question" : "questions"; |
| 499 | my $scorePercent = int ($problemResult->{score} * 100) . "\%"; |
695 | my $scorePercent = int ($problemResult->{score} * 100) . "\%"; |
| 500 | my $summary = "On this attempt, you answered $numCorrect $numCorrectNoun out of " |
696 | my $summary = "On this attempt, you answered $numCorrect out of " |
| 501 | . scalar @answerNames . " correct, for a score of $scorePercent."; |
697 | . scalar @answerNames . " $numIncorrectNoun correct, for a score of $scorePercent."; |
| 502 | return CGI::table({-border=>1}, CGI::Tr(\@tableRows)) . CGI::p($summary); |
698 | return CGI::table({-class=>"attemptResults"}, CGI::Tr(\@tableRows)) . ($showSummary ? CGI::p($summary) : ""); |
| 503 | } |
699 | } |
| 504 | |
700 | |
| 505 | sub viewOptions($) { |
701 | sub viewOptions($) { |
| 506 | my $self = shift; |
702 | my $self = shift; |
| 507 | my $displayMode = $self->{displayMode}; |
703 | my $displayMode = $self->{displayMode}; |
| … | |
… | |
| 509 | my %can = %{ $self->{can} }; |
705 | my %can = %{ $self->{can} }; |
| 510 | my %will = %{ $self->{will} }; |
706 | my %will = %{ $self->{will} }; |
| 511 | |
707 | |
| 512 | my $optionLine; |
708 | my $optionLine; |
| 513 | $can{showOldAnswers} and $optionLine .= join "", |
709 | $can{showOldAnswers} and $optionLine .= join "", |
| 514 | "Show: ", |
710 | "Show: ".CGI::br(), |
| 515 | CGI::checkbox( |
711 | CGI::checkbox( |
| 516 | -name => "showOldAnswers", |
712 | -name => "showOldAnswers", |
| 517 | -checked => $will{showOldAnswers}, |
713 | -checked => $will{showOldAnswers}, |
| 518 | -label => "Saved answers", |
714 | -label => "Saved answers", |
| 519 | ), " "; |
715 | ), " ".CGI::br(); |
| 520 | $can{showCorrectAnswers} and $optionLine .= join "", |
716 | $can{showCorrectAnswers} and $optionLine .= join "", |
| 521 | CGI::checkbox( |
717 | CGI::checkbox( |
| 522 | -name => "showCorrectAnswers", |
718 | -name => "showCorrectAnswers", |
| 523 | -checked => $will{showCorrectAnswers}, |
719 | -checked => $will{showCorrectAnswers}, |
| 524 | -label => "Correct answers", |
720 | -label => "Correct answers", |
| 525 | ), " "; |
721 | ), " ".CGI::br(); |
| 526 | $can{showHints} and $optionLine .= join "", |
722 | $can{showHints} and $optionLine .= join "", |
| 527 | CGI::checkbox( |
723 | CGI::checkbox( |
| 528 | -name => "showHints", |
724 | -name => "showHints", |
| 529 | -checked => $will{showHints}, |
725 | -checked => $will{showHints}, |
| 530 | -label => "Hints", |
726 | -label => "Hints", |
| 531 | ), " "; |
727 | ), " ".CGI::br(); |
| 532 | $can{showSolutions} and $optionLine .= join "", |
728 | $can{showSolutions} and $optionLine .= join "", |
| 533 | CGI::checkbox( |
729 | CGI::checkbox( |
| 534 | -name => "showSolutions", |
730 | -name => "showSolutions", |
| 535 | -checked => $will{showSolutions}, |
731 | -checked => $will{showSolutions}, |
| 536 | -label => "Solutions", |
732 | -label => "Solutions", |
| 537 | ), " "; |
733 | ), " ".CGI::br(); |
| 538 | $optionLine and $optionLine .= join "", CGI::br(); |
734 | $optionLine and $optionLine .= join "", CGI::br(); |
| 539 | |
735 | |
| 540 | return CGI::div({-style=>"border: thin groove; padding: 1ex; margin: 2ex"}, |
736 | return CGI::div({-style=>"border: thin groove; padding: 1ex; margin: 2ex align: left"}, |
| 541 | "View equations as: ", |
737 | "View equations as: ".CGI::br(), |
| 542 | CGI::radio_group( |
738 | CGI::radio_group( |
| 543 | -name => "displayMode", |
739 | -name => "displayMode", |
| 544 | -values => ['plainText', 'formattedText', 'images'], |
740 | -values => ['plainText', 'formattedText', 'images'], |
| 545 | -default => $displayMode, |
741 | -default => $displayMode, |
|
|
742 | -linebreak=>'true', |
| 546 | -labels => { |
743 | -labels => { |
| 547 | plainText => "plain text", |
744 | plainText => "plain", |
| 548 | formattedText => "formatted text", |
745 | formattedText => "formatted", |
| 549 | images => "images", |
746 | images => "images", |
| 550 | } |
747 | } |
| 551 | ), CGI::br(), |
748 | ), CGI::br(),CGI::hr(), |
| 552 | $optionLine, |
749 | $optionLine, |
| 553 | CGI::submit(-name=>"redisplay", -label=>"Redisplay Problem"), |
750 | CGI::submit(-name=>"redisplay", -label=>"Save Options"), |
| 554 | ); |
751 | ); |
| 555 | } |
752 | } |
| 556 | |
753 | |
| 557 | sub previewAnswer($$) { |
754 | sub previewAnswer($$) { |
| 558 | my ($self, $answerResult) = @_; |
755 | my ($self, $answerResult) = @_; |
| 559 | my $ce = $self->{courseEnvironment}; |
|
|
| 560 | my $user = $self->{user}; |
756 | my $ce = $self->{ce}; |
|
|
757 | my $effectiveUser = $self->{effectiveUser}; |
| 561 | my $set = $self->{set}; |
758 | my $set = $self->{set}; |
| 562 | my $problem = $self->{problem}; |
759 | my $problem = $self->{problem}; |
| 563 | my $displayMode = $self->{displayMode}; |
760 | my $displayMode = $self->{displayMode}; |
| 564 | |
761 | |
| 565 | # note: right now, we have to do things completely differently when we are |
762 | # note: right now, we have to do things completely differently when we are |
| 566 | # rendering math from INSIDE the translator and from OUTSIDE the translator. |
763 | # rendering math from INSIDE the translator and from OUTSIDE the translator. |
| 567 | # so we'll just deal with each case explicitly here. there's some code |
764 | # so we'll just deal with each case explicitly here. there's some code |
| 568 | # duplication that can be dealt with later by abstracting out tth/dvipng/etc. |
765 | # duplication that can be dealt with later by abstracting out tth/dvipng/etc. |
| 569 | |
766 | |
| 570 | my $tex = $answerResult->{preview_latex_string}; |
767 | my $tex = $answerResult->{preview_latex_string}; |
|
|
768 | |
|
|
769 | return "" if $tex eq ""; |
| 571 | |
770 | |
| 572 | if ($displayMode eq "plainText") { |
771 | if ($displayMode eq "plainText") { |
| 573 | return $tex; |
772 | return $tex; |
| 574 | } elsif ($displayMode eq "formattedText") { |
773 | } elsif ($displayMode eq "formattedText") { |
| 575 | my $tthCommand = $ce->{externalPrograms}->{tth} |
774 | my $tthCommand = $ce->{externalPrograms}->{tth} |
| 576 | . " -L -f5 -r 2> /dev/null <<END_OF_INPUT; echo > /dev/null\n" |
775 | . " -L -f5 -r 2> /dev/null <<END_OF_INPUT; echo > /dev/null\n" |
| 577 | . "\\($tex\\)\n" |
776 | . "\\(".$tex."\\)\n" |
| 578 | . "END_OF_INPUT\n"; |
777 | . "END_OF_INPUT\n"; |
| 579 | |
|
|
| 580 | |
778 | |
| 581 | # call tth |
779 | # call tth |
| 582 | my $result = `$tthCommand`; |
780 | my $result = `$tthCommand`; |
| 583 | if ($?) { |
781 | if ($?) { |
| 584 | return "<b>[tth failed: $? $@]</b>"; |
782 | return "<b>[tth failed: $? $@]</b>"; |
| 585 | } |
783 | } |
| 586 | return $result; |
784 | return $result; |
| 587 | } elsif ($displayMode eq "images") { |
785 | } elsif ($displayMode eq "images") { |
| 588 | # how are we going to name this? |
786 | # how are we going to name this? |
| 589 | my $targetPathCommon = "/png/" |
787 | my $targetPathCommon = "/png/" |
| 590 | . $user->id . "." |
788 | . $effectiveUser->user_id . "." |
| 591 | . $set->id . "." |
789 | . $set->set_id . "." |
| 592 | . $problem->id . "." |
790 | . $problem->problem_id . "." |
| 593 | . $answerResult->{ans_name} . ".png"; |
791 | . $answerResult->{ans_name} . ".png"; |
| 594 | |
792 | |
| 595 | # figure out where to put things |
793 | # figure out where to put things |
| 596 | my $wd = tempdir("webwork-dvipng-XXXXXXXX", DIR => $ce->{courseDirs}->{html_temp}); |
794 | my $wd = tempdir("webwork-dvipng-XXXXXXXX", DIR => $ce->{courseDirs}->{html_temp}); |
| 597 | my $latex = $ce->{externalPrograms}->{latex}; |
795 | my $latex = $ce->{externalPrograms}->{latex}; |
| … | |
… | |
| 601 | # isolate it from the problem enivronment first |
799 | # isolate it from the problem enivronment first |
| 602 | my $targetURL = $ce->{courseURLs}->{html_temp} . $targetPathCommon; |
800 | my $targetURL = $ce->{courseURLs}->{html_temp} . $targetPathCommon; |
| 603 | |
801 | |
| 604 | # call dvipng to generate a preview |
802 | # call dvipng to generate a preview |
| 605 | dvipng($wd, $latex, $dvipng, $tex, $targetPath); |
803 | dvipng($wd, $latex, $dvipng, $tex, $targetPath); |
|
|
804 | rmtree($wd, 0, 0); |
| 606 | if (-e $targetPath) { |
805 | if (-e $targetPath) { |
| 607 | return "<img src=\"$targetURL\" alt=\"$tex\" />"; |
806 | return "<img src=\"$targetURL\" alt=\"$tex\" />"; |
| 608 | } else { |
807 | } else { |
| 609 | return "<b>[math2img failed]</b>"; |
808 | return "<b>[math2img failed]</b>"; |
| 610 | } |
809 | } |
| 611 | } |
810 | } |
| 612 | } |
811 | } |
|
|
812 | |
|
|
813 | sub options { |
|
|
814 | my $self=shift; |
|
|
815 | my $out; |
|
|
816 | $out .=join("", |
|
|
817 | CGI::startform("POST", $self->{r}->uri), |
|
|
818 | $self->hidden_authen_fields, |
|
|
819 | CGI::hr(), |
|
|
820 | CGI::start_div({class=>"viewOptions"}), |
|
|
821 | $self->viewOptions(),CGI::end_div(), |
|
|
822 | ); |
|
|
823 | return $out; |
|
|
824 | |
|
|
825 | } |
|
|
826 | ##### logging subroutine #### |
|
|
827 | |
|
|
828 | |
| 613 | |
829 | |
| 614 | ##### permission queries ##### |
830 | ##### permission queries ##### |
| 615 | |
831 | |
| 616 | # this stuff should be abstracted out into the permissions system |
832 | # this stuff should be abstracted out into the permissions system |
| 617 | # however, the permission system only knows about things in the |
833 | # however, the permission system only knows about things in the |
| … | |
… | |
| 638 | my $attemptsOK = $maxAttempts == -1 || $attempts <= $maxAttempts; |
854 | my $attemptsOK = $maxAttempts == -1 || $attempts <= $maxAttempts; |
| 639 | my $recordAnswers = $permHigh || ($timeOK && $attemptsOK); |
855 | my $recordAnswers = $permHigh || ($timeOK && $attemptsOK); |
| 640 | return $recordAnswers; |
856 | return $recordAnswers; |
| 641 | } |
857 | } |
| 642 | |
858 | |
|
|
859 | sub canCheckAnswers($$) { |
|
|
860 | my ($permissionLevel, $answerDate) = @_; |
|
|
861 | my $permHigh = $permissionLevel > 0; |
|
|
862 | my $timeOK = time >= $answerDate; |
|
|
863 | my $recordAnswers = $permHigh || $timeOK; |
|
|
864 | return $recordAnswers; |
|
|
865 | } |
|
|
866 | |
| 643 | sub mustRecordAnswers($) { |
867 | sub mustRecordAnswers($) { |
| 644 | my ($permissionLevel) = @_; |
868 | my ($permissionLevel) = @_; |
| 645 | return $permissionLevel == 0; |
869 | return $permissionLevel == 0; |
| 646 | } |
870 | } |
| 647 | |
871 | |