| … | |
… | |
| 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); |
| 23 | |
24 | |
| 24 | ############################################################ |
25 | ############################################################ |
| 25 | # |
26 | # |
| 26 | # user |
27 | # user |
|
|
28 | # effectiveUser |
| 27 | # key |
29 | # key |
| 28 | # |
30 | # |
| 29 | # displayMode |
31 | # displayMode |
| 30 | # showOldAnswers |
32 | # showOldAnswers |
| 31 | # showCorrectAnswers |
33 | # showCorrectAnswers |
| … | |
… | |
| 34 | # |
36 | # |
| 35 | # AnSwEr# - answer blanks in problem |
37 | # AnSwEr# - answer blanks in problem |
| 36 | # |
38 | # |
| 37 | # redisplay - name of the "Redisplay Problem" button |
39 | # redisplay - name of the "Redisplay Problem" button |
| 38 | # submitAnswers - name of "Submit Answers" button |
40 | # submitAnswers - name of "Submit Answers" button |
|
|
41 | # checkAnswers - name of the "Check Answers" button |
|
|
42 | # previewAnswers - name of the "Preview Answers" button |
| 39 | # |
43 | # |
| 40 | ############################################################ |
44 | ############################################################ |
| 41 | |
45 | |
| 42 | sub pre_header_initialize { |
46 | sub pre_header_initialize { |
| 43 | my ($self, $setName, $problemNumber) = @_; |
47 | my ($self, $setName, $problemNumber) = @_; |
| 44 | my $courseEnv = $self->{courseEnvironment}; |
|
|
| 45 | my $r = $self->{r}; |
48 | my $r = $self->{r}; |
|
|
49 | my $courseEnv = $self->{ce}; |
|
|
50 | my $db = $self->{db}; |
| 46 | my $userName = $r->param('user'); |
51 | my $userName = $r->param('user'); |
|
|
52 | my $effectiveUserName = $r->param('effectiveUser'); |
| 47 | |
53 | |
| 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); |
54 | my $user = $db->getUser($userName); |
|
|
55 | my $effectiveUser = $db->getUser($effectiveUserName); |
| 55 | my $set = $wwdb->getSet($userName, $setName); |
56 | my $set = $db->getGlobalUserSet($effectiveUserName, $setName); |
| 56 | my $problem = $wwdb->getProblem($userName, $setName, $problemNumber); |
57 | my $problem = $db->getGlobalUserProblem($effectiveUserName, $setName, $problemNumber); |
| 57 | my $psvn = $wwdb->getPSVN($userName, $setName); |
58 | my $psvn = $set->psvn(); |
| 58 | my $permissionLevel = $authdb->getPermissions($userName); |
59 | my $permissionLevel = $db->getPermissionLevel($userName)->permission(); |
|
|
60 | |
|
|
61 | $self->{userName} = $userName; |
|
|
62 | $self->{user} = $user; |
|
|
63 | $self->{effectiveUser} = $effectiveUser; |
|
|
64 | $self->{set} = $set; |
|
|
65 | $self->{problem} = $problem; |
|
|
66 | $self->{permissionLevel} = $permissionLevel; |
| 59 | |
67 | |
| 60 | ##### form processing ##### |
68 | ##### form processing ##### |
| 61 | |
69 | |
| 62 | # set options from form fields (see comment at top of file for names) |
70 | # set options from form fields (see comment at top of file for names) |
| 63 | my $displayMode = $r->param("displayMode") || $courseEnv->{pg}->{options}->{displayMode}; |
71 | my $displayMode = $r->param("displayMode") || $courseEnv->{pg}->{options}->{displayMode}; |
| 64 | my $redisplay = $r->param("redisplay"); |
72 | my $redisplay = $r->param("redisplay"); |
| 65 | my $submitAnswers = $r->param("submitAnswers"); |
73 | my $submitAnswers = $r->param("submitAnswers"); |
|
|
74 | my $checkAnswers = $r->param("checkAnswers"); |
| 66 | my $previewAnswers = $r->param("previewAnswers"); |
75 | my $previewAnswers = $r->param("previewAnswers"); |
| 67 | |
76 | |
| 68 | # coerce form fields into CGI::Vars format |
77 | # coerce form fields into CGI::Vars format |
| 69 | my $formFields = { WeBWorK::Form->new_from_paramable($r)->Vars }; |
78 | my $formFields = { WeBWorK::Form->new_from_paramable($r)->Vars }; |
| 70 | |
79 | |
|
|
80 | $self->{displayMode} = $displayMode; |
|
|
81 | $self->{redisplay} = $redisplay; |
|
|
82 | $self->{submitAnswers} = $submitAnswers; |
|
|
83 | $self->{checkAnswers} = $checkAnswers; |
|
|
84 | $self->{previewAnswers} = $previewAnswers; |
|
|
85 | $self->{formFields} = $formFields; |
|
|
86 | |
| 71 | ##### permissions ##### |
87 | ##### permissions ##### |
|
|
88 | |
|
|
89 | # are we allowed to view this problem? |
|
|
90 | $self->{isOpen} = time >= $set->open_date || $permissionLevel > 0; |
|
|
91 | return unless $self->{isOpen}; |
| 72 | |
92 | |
| 73 | # what does the user want to do? |
93 | # what does the user want to do? |
| 74 | my %want = ( |
94 | my %want = ( |
| 75 | showOldAnswers => $r->param("showOldAnswers") || $courseEnv->{pg}->{options}->{showOldAnswers}, |
95 | showOldAnswers => $r->param("showOldAnswers") || $courseEnv->{pg}->{options}->{showOldAnswers}, |
| 76 | showCorrectAnswers => $r->param("showCorrectAnswers") || $courseEnv->{pg}->{options}->{showCorrectAnswers}, |
96 | showCorrectAnswers => $r->param("showCorrectAnswers") || $courseEnv->{pg}->{options}->{showCorrectAnswers}, |
| 77 | showHints => $r->param("showHints") || $courseEnv->{pg}->{options}->{showHints}, |
97 | showHints => $r->param("showHints") || $courseEnv->{pg}->{options}->{showHints}, |
| 78 | showSolutions => $r->param("showSolutions") || $courseEnv->{pg}->{options}->{showSolutions}, |
98 | showSolutions => $r->param("showSolutions") || $courseEnv->{pg}->{options}->{showSolutions}, |
| 79 | recordAnswers => $r->param("recordAnswers") || 1, |
99 | recordAnswers => $submitAnswers, |
|
|
100 | checkAnswers => $checkAnswers, |
| 80 | ); |
101 | ); |
| 81 | |
102 | |
| 82 | # are certain options enforced? |
103 | # are certain options enforced? |
| 83 | my %must = ( |
104 | my %must = ( |
| 84 | showOldAnswers => 0, |
105 | showOldAnswers => 0, |
| 85 | showCorrectAnswers => 0, |
106 | showCorrectAnswers => 0, |
| 86 | showHints => 0, |
107 | showHints => 0, |
| 87 | showSolutions => 0, |
108 | showSolutions => 0, |
| 88 | recordAnswers => mustRecordAnswers($permissionLevel), |
109 | recordAnswers => mustRecordAnswers($permissionLevel), |
|
|
110 | checkAnswers => 0, |
| 89 | ); |
111 | ); |
| 90 | |
112 | |
| 91 | # does the user have permission to use certain options? |
113 | # does the user have permission to use certain options? |
| 92 | my %can = ( |
114 | my %can = ( |
| 93 | showOldAnswers => 1, |
115 | showOldAnswers => 1, |
| 94 | showCorrectAnswers => canShowCorrectAnswers($permissionLevel, $set->answer_date), |
116 | showCorrectAnswers => canShowCorrectAnswers($permissionLevel, $set->answer_date), |
| 95 | showHints => 1, |
117 | showHints => 1, |
| 96 | showSolutions => canShowSolutions($permissionLevel, $set->answer_date), |
118 | showSolutions => canShowSolutions($permissionLevel, $set->answer_date), |
| 97 | recordAnswers => canRecordAnswers($permissionLevel, $set->open_date, $set->due_date, |
119 | recordAnswers => canRecordAnswers($permissionLevel, $set->open_date, $set->due_date, |
| 98 | $problem->max_attempts, $problem->num_correct + $problem->num_incorrect + 1), |
120 | $problem->max_attempts, $problem->num_correct + $problem->num_incorrect + 1), |
| 99 | # num_correct+num_incorrect+1 -- as this happens before updating $problem |
121 | # attempts=num_correct+num_incorrect+1, as this happens before updating $problem |
|
|
122 | checkAnswers => canCheckAnswers($permissionLevel, $set->answer_date), |
| 100 | ); |
123 | ); |
| 101 | |
124 | |
| 102 | # final values for options |
125 | # final values for options |
| 103 | my %will; |
126 | my %will; |
| 104 | foreach (keys %must) { |
127 | foreach (keys %must) { |
| 105 | $will{$_} = $can{$_} && ($want{$_} || $must{$_}); |
128 | $will{$_} = $can{$_} && ($want{$_} || $must{$_}); |
| 106 | #warn "$_: can? $can{$_} want? $want{$_} must? $must{$_} will? $will{$_}\n"; |
|
|
| 107 | } |
129 | } |
| 108 | |
130 | |
| 109 | ##### sticky answers ##### |
131 | ##### sticky answers ##### |
| 110 | |
132 | |
| 111 | if (not $submitAnswers and $will{showOldAnswers}) { |
133 | if (not $submitAnswers and $will{showOldAnswers}) { |
| … | |
… | |
| 116 | |
138 | |
| 117 | ##### translation ##### |
139 | ##### translation ##### |
| 118 | |
140 | |
| 119 | my $pg = WeBWorK::PG->new( |
141 | my $pg = WeBWorK::PG->new( |
| 120 | $courseEnv, |
142 | $courseEnv, |
| 121 | $user, |
143 | $effectiveUser, |
| 122 | $r->param('key'), |
144 | $r->param('key'), |
| 123 | $set, |
145 | $set, |
| 124 | $problem, |
146 | $problem, |
| 125 | $psvn, |
147 | $psvn, |
| 126 | $formFields, |
148 | $formFields, |
| 127 | { # translation options |
149 | { # translation options |
| 128 | displayMode => $displayMode, |
150 | displayMode => $displayMode, |
| 129 | showHints => $will{showHints}, |
151 | showHints => $will{showHints}, |
| 130 | showSolutions => $will{showSolutions}, |
152 | showSolutions => $will{showSolutions}, |
| 131 | refreshMath2img => $will{showHints} || $will{showSolutions}, |
153 | refreshMath2img => $will{showHints} || $will{showSolutions}, |
| 132 | # try leaving processAnswers on all the time? |
154 | processAnswers => 1, |
| 133 | processAnswers => 1, #$submitAnswers ? 1 : 0, |
|
|
| 134 | }, |
155 | }, |
| 135 | ); |
156 | ); |
| 136 | |
157 | |
|
|
158 | ##### fix hint/solution options ##### |
|
|
159 | |
|
|
160 | $can{showHints} &&= $pg->{flags}->{hintExists}; |
|
|
161 | $can{showSolutions} &&= $pg->{flags}->{solutionExists}; |
|
|
162 | |
| 137 | ##### store fields ##### |
163 | ##### store fields ##### |
| 138 | |
|
|
| 139 | $self->{cldb} = $cldb; |
|
|
| 140 | $self->{wwdb} = $wwdb; |
|
|
| 141 | $self->{authdb} = $authdb; |
|
|
| 142 | |
|
|
| 143 | $self->{user} = $user; |
|
|
| 144 | $self->{set} = $set; |
|
|
| 145 | $self->{problem} = $problem; |
|
|
| 146 | $self->{permissionLevel} = $permissionLevel; |
|
|
| 147 | |
|
|
| 148 | $self->{displayMode} = $displayMode; |
|
|
| 149 | $self->{redisplay} = $redisplay; |
|
|
| 150 | $self->{submitAnswers} = $submitAnswers; |
|
|
| 151 | $self->{previewAnswers} = $previewAnswers; |
|
|
| 152 | $self->{formFields} = $formFields; |
|
|
| 153 | |
164 | |
| 154 | $self->{want} = \%want; |
165 | $self->{want} = \%want; |
| 155 | $self->{must} = \%must; |
166 | $self->{must} = \%must; |
| 156 | $self->{can} = \%can; |
167 | $self->{can} = \%can; |
| 157 | $self->{will} = \%will; |
168 | $self->{will} = \%will; |
| … | |
… | |
| 159 | $self->{pg} = $pg; |
170 | $self->{pg} = $pg; |
| 160 | } |
171 | } |
| 161 | |
172 | |
| 162 | sub if_warnings($$) { |
173 | sub if_warnings($$) { |
| 163 | my ($self, $arg) = @_; |
174 | my ($self, $arg) = @_; |
|
|
175 | return 0 unless $self->{isOpen}; |
| 164 | return $self->{pg}->{warnings} ne ""; |
176 | return $self->{pg}->{warnings} ne ""; |
| 165 | } |
177 | } |
| 166 | |
178 | |
| 167 | sub if_errors($$) { |
179 | sub if_errors($$) { |
| 168 | my ($self, $arg) = @_; |
180 | my ($self, $arg) = @_; |
|
|
181 | return 0 unless $self->{isOpen}; |
| 169 | return $self->{pg}->{flags}->{error_flag}; |
182 | return $self->{pg}->{flags}->{error_flag}; |
| 170 | } |
183 | } |
| 171 | |
184 | |
| 172 | sub head { |
185 | sub head { |
| 173 | my $self = shift; |
186 | my $self = shift; |
| 174 | |
187 | return "" unless $self->{isOpen}; |
| 175 | return $self->{pg}->{head_text} if $self->{pg}->{head_text}; |
188 | return $self->{pg}->{head_text} if $self->{pg}->{head_text}; |
| 176 | } |
189 | } |
| 177 | |
190 | |
| 178 | sub path { |
191 | sub path { |
| 179 | my $self = shift; |
192 | my $self = shift; |
| 180 | my $args = $_[-1]; |
193 | my $args = $_[-1]; |
| 181 | my $setName = $self->{set}->id; |
194 | my $setName = $self->{set}->set_id; |
| 182 | my $problemNumber = $self->{problem}->id; |
195 | my $problemNumber = $self->{problem}->problem_id; |
| 183 | |
196 | |
| 184 | my $ce = $self->{courseEnvironment}; |
197 | my $ce = $self->{ce}; |
| 185 | my $root = $ce->{webworkURLs}->{root}; |
198 | my $root = $ce->{webworkURLs}->{root}; |
| 186 | my $courseName = $ce->{courseName}; |
199 | my $courseName = $ce->{courseName}; |
| 187 | return $self->pathMacro($args, |
200 | return $self->pathMacro($args, |
| 188 | "Home" => "$root", |
201 | "Home" => "$root", |
| 189 | $courseName => "$root/$courseName", |
202 | $courseName => "$root/$courseName", |
| … | |
… | |
| 192 | ); |
205 | ); |
| 193 | } |
206 | } |
| 194 | |
207 | |
| 195 | sub siblings { |
208 | sub siblings { |
| 196 | my $self = shift; |
209 | my $self = shift; |
| 197 | my $setName = $self->{set}->id; |
210 | my $setName = $self->{set}->set_id; |
| 198 | my $problemNumber = $self->{problem}->id; |
211 | my $problemNumber = $self->{problem}->problem_id; |
| 199 | |
212 | |
| 200 | my $ce = $self->{courseEnvironment}; |
213 | my $ce = $self->{ce}; |
|
|
214 | my $db = $self->{db}; |
| 201 | my $root = $ce->{webworkURLs}->{root}; |
215 | my $root = $ce->{webworkURLs}->{root}; |
| 202 | my $courseName = $ce->{courseName}; |
216 | my $courseName = $ce->{courseName}; |
| 203 | |
217 | |
| 204 | print CGI::strong("Problems"), CGI::br(); |
218 | print CGI::strong("Problems"), CGI::br(); |
| 205 | |
219 | |
| 206 | my $wwdb = $self->{wwdb}; |
|
|
| 207 | my $user = $self->{r}->param("user"); |
220 | my $effectiveUser = $self->{r}->param("effectiveUser"); |
| 208 | my @problems; |
221 | my @problems; |
| 209 | push @problems, $wwdb->getProblem($user, $setName, $_) |
222 | push @problems, $db->getGlobalUserProblem($effectiveUser, $setName, $_) |
| 210 | foreach ($wwdb->getProblems($user, $setName)); |
223 | foreach ($db->listUserProblems($effectiveUser, $setName)); |
| 211 | foreach my $problem (sort { $a->id <=> $b->id } @problems) { |
224 | foreach my $problem (sort { $a->problem_id <=> $b->problem_id } @problems) { |
| 212 | print CGI::a({-href=>"$root/$courseName/$setName/".$problem->id."/?" |
225 | print CGI::a({-href=>"$root/$courseName/$setName/".$problem->problem_id."/?" |
| 213 | . $self->url_authen_args}, "Problem ".$problem->id), CGI::br(); |
226 | . $self->url_authen_args . "&displayMode=" . $self->{displayMode}}, |
|
|
227 | "Problem ".$problem->problem_id), CGI::br(); |
| 214 | } |
228 | } |
| 215 | } |
229 | } |
| 216 | |
230 | |
| 217 | sub nav { |
231 | sub nav { |
| 218 | my $self = shift; |
232 | my $self = shift; |
| 219 | my $args = $_[-1]; |
233 | my $args = $_[-1]; |
| 220 | my $setName = $self->{set}->id; |
234 | my $setName = $self->{set}->set_id; |
| 221 | my $problemNumber = $self->{problem}->id; |
235 | my $problemNumber = $self->{problem}->problem_id; |
| 222 | |
236 | |
| 223 | my $ce = $self->{courseEnvironment}; |
237 | my $ce = $self->{ce}; |
|
|
238 | my $db = $self->{db}; |
| 224 | my $root = $ce->{webworkURLs}->{root}; |
239 | my $root = $ce->{webworkURLs}->{root}; |
| 225 | my $courseName = $ce->{courseName}; |
240 | my $courseName = $ce->{courseName}; |
| 226 | |
241 | |
| 227 | my $wwdb = $self->{wwdb}; |
242 | my $wwdb = $self->{wwdb}; |
| 228 | my $user = $self->{r}->param("user"); |
243 | my $effectiveUser = $self->{r}->param("effectiveUser"); |
|
|
244 | my $tail = "&displayMode=".$self->{displayMode}; |
| 229 | |
245 | |
| 230 | my @links = ("Problem List" => "$root/$courseName/$setName"); |
246 | my @links = ("Problem List" , "$root/$courseName/$setName", "navProbList"); |
| 231 | |
247 | |
| 232 | my $prevProblem = $wwdb->getProblem($user, $setName, $problemNumber-1); |
248 | my $prevProblem = $db->getGlobalUserProblem($effectiveUser, $setName, $problemNumber-1); |
| 233 | my $nextProblem = $wwdb->getProblem($user, $setName, $problemNumber+1); |
249 | my $nextProblem = $db->getGlobalUserProblem($effectiveUser, $setName, $problemNumber+1); |
| 234 | unshift @links, "Previous Problem" => $prevProblem |
250 | unshift @links, "Previous Problem" , ($prevProblem |
| 235 | ? "$root/$courseName/$setName/".$prevProblem->id |
251 | ? "$root/$courseName/$setName/".$prevProblem->problem_id |
| 236 | : ""; |
252 | : "") , "navPrev"; |
| 237 | push @links, "Next Problem" => $nextProblem |
253 | push @links, "Next Problem" , ($nextProblem |
| 238 | ? "$root/$courseName/$setName/".$nextProblem->id |
254 | ? "$root/$courseName/$setName/".$nextProblem->problem_id |
| 239 | : ""; |
255 | : "") , "navNext"; |
| 240 | |
256 | |
| 241 | return $self->navMacro($args, @links); |
257 | return $self->navMacro($args, $tail, @links); |
| 242 | } |
258 | } |
| 243 | |
259 | |
| 244 | sub title { |
260 | sub title { |
| 245 | my $self = shift; |
261 | my $self = shift; |
| 246 | my $setName = $self->{set}->id; |
262 | my $setName = $self->{set}->set_id; |
| 247 | my $problemNumber = $self->{problem}->id; |
263 | my $problemNumber = $self->{problem}->problem_id; |
| 248 | |
264 | |
| 249 | return "$setName : Problem $problemNumber"; |
265 | return "$setName : Problem $problemNumber"; |
| 250 | } |
266 | } |
| 251 | |
267 | |
| 252 | sub body { |
268 | sub body { |
| 253 | my $self = shift; |
269 | my $self = shift; |
| 254 | |
270 | |
|
|
271 | return CGI::p(CGI::font({-color=>"red"}, "This problem is not available because the problem set that contains it is not yet open.")) |
|
|
272 | unless $self->{isOpen}; |
|
|
273 | |
| 255 | # unpack some useful variables |
274 | # unpack some useful variables |
| 256 | my $r = $self->{r}; |
275 | my $r = $self->{r}; |
| 257 | my $wwdb = $self->{wwdb}; |
276 | my $db = $self->{db}; |
| 258 | my $set = $self->{set}; |
277 | my $set = $self->{set}; |
| 259 | my $problem = $self->{problem}; |
278 | my $problem = $self->{problem}; |
| 260 | my $permissionLevel = $self->{permissionLevel}; |
279 | my $permissionLevel = $self->{permissionLevel}; |
| 261 | my $submitAnswers = $self->{submitAnswers}; |
280 | my $submitAnswers = $self->{submitAnswers}; |
|
|
281 | my $checkAnswers = $self->{checkAnswers}; |
| 262 | my $previewAnswers = $self->{previewAnswers}; |
282 | my $previewAnswers = $self->{previewAnswers}; |
|
|
283 | my %want = %{ $self->{want} }; |
|
|
284 | my %can = %{ $self->{can} }; |
|
|
285 | my %must = %{ $self->{must} }; |
| 263 | my %will = %{ $self->{will} }; |
286 | my %will = %{ $self->{will} }; |
| 264 | my $pg = $self->{pg}; |
287 | my $pg = $self->{pg}; |
| 265 | |
288 | |
| 266 | ##### translation errors? ##### |
289 | ##### translation errors? ##### |
| 267 | |
290 | |
| 268 | if ($pg->{flags}->{error_flag}) { |
291 | if ($pg->{flags}->{error_flag}) { |
| 269 | return translationError($pg->{errors}, $pg->{body_text}); |
292 | return $self->errorOutput($pg->{errors}, $pg->{body_text}); |
| 270 | } |
293 | } |
| 271 | |
294 | |
| 272 | ##### answer processing ##### |
295 | ##### answer processing ##### |
| 273 | |
296 | |
| 274 | # if answers were submitted: |
297 | # if answers were submitted: |
| 275 | if ($submitAnswers) { |
298 | if ($submitAnswers) { |
|
|
299 | # get a "pure" (unmerged) UserProblem to modify |
|
|
300 | my $pureProblem = $db->getUserProblem($problem->user_id, $problem->set_id, $problem->problem_id); |
| 276 | # store answers in DB for sticky answers |
301 | # store answers in DB for sticky answers |
| 277 | my %answersToStore; |
302 | my %answersToStore; |
| 278 | my %answerHash = %{ $pg->{answers} }; |
303 | my %answerHash = %{ $pg->{answers} }; |
| 279 | $answersToStore{$_} = $answerHash{$_}->{original_student_ans} |
304 | $answersToStore{$_} = $answerHash{$_}->{original_student_ans} |
| 280 | foreach (keys %answerHash); |
305 | foreach (keys %answerHash); |
| 281 | my $answerString = encodeAnswers(%answersToStore, |
306 | my $answerString = encodeAnswers(%answersToStore, |
| 282 | @{ $pg->{flags}->{ANSWER_ENTRY_ORDER} }); |
307 | @{ $pg->{flags}->{ANSWER_ENTRY_ORDER} }); |
|
|
308 | $pureProblem->last_answer($answerString); |
| 283 | $problem->last_answer($answerString); |
309 | $problem->last_answer($answerString); |
| 284 | $wwdb->setProblem($problem); |
310 | $db->putUserProblem($pureProblem); |
| 285 | |
311 | |
| 286 | # store state in DB if it makes sense |
312 | # store state in DB if it makes sense |
| 287 | if ($will{recordAnswers}) { |
313 | if ($will{recordAnswers}) { |
|
|
314 | $problem->status($pg->{state}->{recorded_score}); |
| 288 | $problem->attempted(1); |
315 | $problem->attempted(1); |
| 289 | $problem->status($pg->{state}->{recorded_score}); |
|
|
| 290 | $problem->num_correct($pg->{state}->{num_of_correct_ans}); |
316 | $problem->num_correct($pg->{state}->{num_of_correct_ans}); |
| 291 | $problem->num_incorrect($pg->{state}->{num_of_incorrect_ans}); |
317 | $problem->num_incorrect($pg->{state}->{num_of_incorrect_ans}); |
|
|
318 | $pureProblem->status($pg->{state}->{recorded_score}); |
|
|
319 | $pureProblem->attempted(1); |
|
|
320 | $pureProblem->num_correct($pg->{state}->{num_of_correct_ans}); |
|
|
321 | $pureProblem->num_incorrect($pg->{state}->{num_of_incorrect_ans}); |
| 292 | $wwdb->setProblem($problem); |
322 | $db->putUserProblem($pureProblem); |
| 293 | # write to the transaction log, just to make sure |
323 | # write to the transaction log, just to make sure |
| 294 | writeLog($self->{courseEnvironment}, "transaction", |
324 | writeLog($self->{ce}, "transaction", |
| 295 | $problem->id."\t". |
325 | $problem->problem_id."\t". |
| 296 | $problem->set_id."\t". |
326 | $problem->set_id."\t". |
| 297 | $problem->login_id."\t". |
327 | $problem->user_id."\t". |
| 298 | $problem->source_file."\t". |
328 | $problem->source_file."\t". |
| 299 | $problem->value."\t". |
329 | $problem->value."\t". |
| 300 | $problem->max_attempts."\t". |
330 | $problem->max_attempts."\t". |
| 301 | $problem->problem_seed."\t". |
331 | $problem->problem_seed."\t". |
| 302 | $problem->status."\t". |
332 | $pureProblem->status."\t". |
| 303 | $problem->attempted."\t". |
333 | $pureProblem->attempted."\t". |
| 304 | $problem->last_answer."\t". |
334 | $pureProblem->last_answer."\t". |
| 305 | $problem->num_correct."\t". |
335 | $pureProblem->num_correct."\t". |
| 306 | $problem->num_incorrect |
336 | $pureProblem->num_incorrect |
| 307 | ); |
337 | ); |
| 308 | } |
338 | } |
| 309 | } |
339 | } |
|
|
340 | # logging student answers |
|
|
341 | my $pastAnswerLog = undef; |
|
|
342 | if (defined( $self->{ce}->{webworkFiles}->{logs}->{'pastAnswerList'} )) { |
|
|
343 | |
|
|
344 | $pastAnswerLog = $self->{ce}->{webworkFiles}->{logs}->{'pastAnswerList'}; |
|
|
345 | |
|
|
346 | if ($submitAnswers and defined($pastAnswerLog) ) { |
|
|
347 | my $answerString = ""; |
|
|
348 | my %answerHash = %{ $pg->{answers} }; |
|
|
349 | $answerString = $answerString . $answerHash{$_}->{original_student_ans}."\t" |
|
|
350 | foreach (sort keys %answerHash); |
|
|
351 | writeLog($self->{ce}, "pastAnswerList", |
|
|
352 | '|'.$problem->user_id. |
|
|
353 | '|'.$problem->set_id. |
|
|
354 | '|'.$problem->problem_id.'|'."\t". |
|
|
355 | time()."\t". |
|
|
356 | $answerString, |
|
|
357 | |
|
|
358 | ); |
|
|
359 | |
|
|
360 | } |
|
|
361 | |
|
|
362 | } |
|
|
363 | # end logging student answers |
| 310 | |
364 | |
| 311 | ##### output ##### |
365 | ##### output ##### |
| 312 | |
366 | print CGI::start_div({class=>"problemHeader"}); |
| 313 | # attempt summary |
367 | # attempt summary |
| 314 | if ($submitAnswers or $will{showCorrectAnswers}) { |
368 | if ($submitAnswers or $will{showCorrectAnswers}) { |
| 315 | # print this if user submitted answers OR requested correct answers |
369 | # print this if user submitted answers OR requested correct answers |
| 316 | print $self->attemptResults($pg, $submitAnswers, $will{showCorrectAnswers}, |
370 | print $self->attemptResults($pg, $submitAnswers, |
|
|
371 | $will{showCorrectAnswers}, |
| 317 | $pg->{flags}->{showPartialCorrectAnswers}); |
372 | $pg->{flags}->{showPartialCorrectAnswers}, 1, 0); |
|
|
373 | } elsif ($checkAnswers) { |
|
|
374 | # print this if user previewed answers |
|
|
375 | print $self->attemptResults($pg, 1, 0, 1, 1, 0); |
|
|
376 | # show attempt answers |
|
|
377 | # don't show correct answers |
|
|
378 | # show attempt results (correctness) |
|
|
379 | # don't show attempt previews |
| 318 | } elsif ($previewAnswers) { |
380 | } elsif ($previewAnswers) { |
| 319 | # print this if user previewed answers |
381 | # print this if user previewed answers |
| 320 | print $self->attemptResults($pg, 1, 0, 0); |
382 | print $self->attemptResults($pg, 1, 0, 0, 0, 1); |
| 321 | # don't show correctness |
383 | # show attempt answers |
| 322 | # don't show correct answers |
384 | # don't show correct answers |
|
|
385 | # don't show attempt results (correctness) |
|
|
386 | # show attempt previews |
| 323 | } |
387 | } |
| 324 | |
388 | |
|
|
389 | print CGI::end_div(); |
|
|
390 | |
|
|
391 | print CGI::start_div({class=>"problem"}); |
|
|
392 | #print CGI::hr(); |
|
|
393 | # main form |
|
|
394 | print |
|
|
395 | CGI::startform("POST", $r->uri), |
|
|
396 | $self->hidden_authen_fields, |
|
|
397 | CGI::p($pg->{body_text}), |
|
|
398 | CGI::p($pg->{result}->{msg} ? CGI::b("Note: ") : "", CGI::i($pg->{result}->{msg})), |
|
|
399 | CGI::p( |
|
|
400 | ($can{recordAnswers} |
|
|
401 | ? CGI::submit(-name=>"submitAnswers", |
|
|
402 | -label=>"Submit Answers") |
|
|
403 | : ""), |
|
|
404 | ($can{checkAnswers} |
|
|
405 | ? CGI::submit(-name=>"checkAnswers", |
|
|
406 | -label=>"Check Answers") |
|
|
407 | : ""), |
|
|
408 | CGI::submit(-name=>"previewAnswers", |
|
|
409 | -label=>"Preview Answers"), |
|
|
410 | ); |
|
|
411 | print CGI::end_div(); |
|
|
412 | |
|
|
413 | print CGI::start_div({class=>"scoreSummary"}); |
| 325 | # score summary |
414 | # score summary |
| 326 | my $attempts = $problem->num_correct + $problem->num_incorrect; |
415 | my $attempts = $problem->num_correct + $problem->num_incorrect; |
| 327 | my $attemptsNoun = $attempts != 1 ? "times" : "time"; |
416 | my $attemptsNoun = $attempts != 1 ? "times" : "time"; |
| 328 | my $lastScore = int ($problem->status * 100) . "%"; |
417 | my $lastScore = int ($problem->status * 100) . "%"; |
| 329 | my ($attemptsLeft, $attemptsLeftNoun); |
418 | my ($attemptsLeft, $attemptsLeftNoun); |
| … | |
… | |
| 333 | $attemptsLeftNoun = "attempts"; |
422 | $attemptsLeftNoun = "attempts"; |
| 334 | } else { |
423 | } else { |
| 335 | $attemptsLeft = $problem->max_attempts - $attempts; |
424 | $attemptsLeft = $problem->max_attempts - $attempts; |
| 336 | $attemptsLeftNoun = $attemptsLeft == 1 ? "attempt" : "attempts"; |
425 | $attemptsLeftNoun = $attemptsLeft == 1 ? "attempt" : "attempts"; |
| 337 | } |
426 | } |
|
|
427 | |
|
|
428 | my $setClosed = 0; |
| 338 | my $setClosedMessage; |
429 | my $setClosedMessage; |
| 339 | if (time < $set->open_date or time > $set->due_date) { |
430 | if (time < $set->open_date or time > $set->due_date) { |
|
|
431 | $setClosed = 1; |
| 340 | $setClosedMessage = "This problem set is closed."; |
432 | $setClosedMessage = "This problem set is closed."; |
| 341 | if ($permissionLevel > 0) { |
433 | if ($permissionLevel > 0) { |
| 342 | $setClosedMessage .= " Since you are a privileged user, additional attempts will be recorded."; |
434 | $setClosedMessage .= " Since you are a privileged user, additional attempts will be recorded."; |
| 343 | } else { |
435 | } else { |
| 344 | $setClosedMessage .= " Additional attempts will not be recorded."; |
436 | $setClosedMessage .= " Additional attempts will not be recorded."; |
| … | |
… | |
| 347 | print CGI::p( |
439 | print CGI::p( |
| 348 | "You have attempted this problem $attempts $attemptsNoun.", CGI::br(), |
440 | "You have attempted this problem $attempts $attemptsNoun.", CGI::br(), |
| 349 | $problem->attempted |
441 | $problem->attempted |
| 350 | ? "Your recorded score is $lastScore." . CGI::br() |
442 | ? "Your recorded score is $lastScore." . CGI::br() |
| 351 | : "", |
443 | : "", |
| 352 | "You have $attemptsLeft $attemptsLeftNoun remaining.", CGI::br(), |
444 | $setClosed ? $setClosedMessage : "You have $attemptsLeft $attemptsLeftNoun remaining." |
| 353 | $setClosedMessage, |
|
|
| 354 | ); |
445 | ); |
| 355 | |
|
|
| 356 | print CGI::hr(); |
446 | print CGI::end_div(); |
| 357 | |
447 | print CGI::hr(), CGI::start_div({class=>"viewOptions"}); |
| 358 | # main form |
|
|
| 359 | print |
448 | print |
| 360 | CGI::startform("POST", $r->uri), |
|
|
| 361 | $self->hidden_authen_fields, |
|
|
| 362 | CGI::p(CGI::i($pg->{result}->{msg})), |
|
|
| 363 | CGI::p($pg->{body_text}), |
|
|
| 364 | CGI::p( |
|
|
| 365 | CGI::submit(-name=>"submitAnswers", -label=>"Submit Answers"), |
|
|
| 366 | CGI::submit(-name=>"previewAnswers", -label=>"Preview Answers"), |
|
|
| 367 | ), |
|
|
| 368 | $self->viewOptions(), |
449 | $self->viewOptions(),CGI::end_div(), |
| 369 | CGI::endform(); |
450 | CGI::endform(); |
| 370 | |
451 | |
|
|
452 | print CGI::start_div({class=>"problemFooter"}); |
| 371 | # feedback form |
453 | # feedback form |
| 372 | my $ce = $self->{courseEnvironment}; |
454 | my $ce = $self->{ce}; |
| 373 | my $root = $ce->{webworkURLs}->{root}; |
455 | my $root = $ce->{webworkURLs}->{root}; |
| 374 | my $courseName = $ce->{courseName}; |
456 | my $courseName = $ce->{courseName}; |
| 375 | my $feedbackURL = "$root/$courseName/feedback/"; |
457 | my $feedbackURL = "$root/$courseName/feedback/"; |
|
|
458 | |
|
|
459 | # arguments for answer inspection button |
|
|
460 | my $prof_url = $ce->{webworkURLs}->{oldProf}; |
|
|
461 | my $cgi_url = $prof_url; |
|
|
462 | $cgi_url=~ s|/[^/]*$||; # clip profLogin.pl |
|
|
463 | my $authen_args = $self->url_authen_args(); |
|
|
464 | my $showPastAnswersURL = "$cgi_url/showPastAnswers.pl"; |
|
|
465 | |
|
|
466 | #print feedback form |
| 376 | print |
467 | print |
| 377 | CGI::startform("POST", $feedbackURL), |
468 | CGI::start_form(-method=>"POST", -action=>$feedbackURL),"\n", |
| 378 | $self->hidden_authen_fields, |
469 | $self->hidden_authen_fields,"\n", |
| 379 | CGI::hidden("module", __PACKAGE__), |
470 | CGI::hidden("module", __PACKAGE__),"\n", |
| 380 | CGI::hidden("set", $set->id), |
471 | CGI::hidden("set", $set->set_id),"\n", |
| 381 | CGI::hidden("problem", $problem->id), |
472 | CGI::hidden("problem", $problem->problem_id),"\n", |
| 382 | CGI::hidden("displayMode", $self->{displayMode}), |
473 | CGI::hidden("displayMode", $self->{displayMode}),"\n", |
| 383 | CGI::hidden("showOldAnswers", $will{showOldAnswers}), |
474 | CGI::hidden("showOldAnswers", $will{showOldAnswers}),"\n", |
| 384 | CGI::hidden("showCorrectAnswers", $will{showCorrectAnswers}), |
475 | CGI::hidden("showCorrectAnswers", $will{showCorrectAnswers}),"\n", |
| 385 | CGI::hidden("showHints", $will{showHints}), |
476 | CGI::hidden("showHints", $will{showHints}),"\n", |
| 386 | CGI::hidden("showSolutions", $will{showSolutions}), |
477 | CGI::hidden("showSolutions", $will{showSolutions}),"\n", |
| 387 | CGI::p({-align=>"right"}, |
478 | CGI::p({-align=>"right"}, |
| 388 | CGI::submit(-name=>"feedbackForm", -label=>"Send Feedback") |
479 | CGI::submit(-name=>"feedbackForm", -label=>"Send Feedback") |
| 389 | ), |
480 | ), |
|
|
481 | CGI::endform(),"\n"; |
|
|
482 | # print answer inspection button |
|
|
483 | if ($self->{permissionLevel} >0) { |
|
|
484 | |
|
|
485 | |
|
|
486 | print "\n", |
|
|
487 | CGI::start_form(-method=>"POST",-action=>$showPastAnswersURL,-target=>"information"),"\n", |
|
|
488 | $self->hidden_authen_fields,"\n", |
|
|
489 | CGI::hidden(-name => 'course', -value=>$courseName), "\n", |
|
|
490 | CGI::hidden(-name => 'probNum', -value=>$problem->problem_id), "\n", |
|
|
491 | CGI::hidden(-name => 'setNum', -value=>$problem->set_id), "\n", |
|
|
492 | CGI::hidden(-name => 'User', -value=>$problem->user_id), "\n", |
|
|
493 | CGI::submit(-name => 'action', -value=>'Show Past Answers'), "\n", |
| 390 | CGI::endform(); |
494 | CGI::endform(); |
| 391 | |
495 | |
|
|
496 | |
|
|
497 | |
|
|
498 | } |
|
|
499 | # FIXME print editor link |
|
|
500 | if ($self->{permissionLevel}>=10 ) { |
|
|
501 | print CGI::a({-href=>"/webwork/$courseName/instructor/pgProblemEditor/".$set->set_id. |
|
|
502 | '/'.$problem->problem_id.'?'.$self->url_authen_args},'Edit this problem'); |
|
|
503 | } |
|
|
504 | print CGI::end_div(); |
|
|
505 | |
|
|
506 | # end answer inspection button |
| 392 | # warning output |
507 | # warning output |
| 393 | if ($pg->{warnings} ne "") { |
508 | if ($pg->{warnings} ne "") { |
| 394 | print CGI::hr(), warningOutput($pg->{warnings}); |
509 | print CGI::hr(), $self->warningOutput($pg->{warnings}); |
| 395 | } |
510 | } |
| 396 | |
511 | |
| 397 | # debugging stuff |
512 | # debugging stuff |
|
|
513 | if (0) { |
| 398 | #print |
514 | print |
| 399 | # CGI::hr(), |
515 | CGI::hr(), |
| 400 | # CGI::h2("debugging information"), |
516 | CGI::h2("debugging information"), |
| 401 | # CGI::h3("form fields"), |
517 | CGI::h3("form fields"), |
| 402 | # ref2string($self->{formFields}), |
518 | ref2string($self->{formFields}), |
| 403 | # CGI::h3("user object"), |
519 | CGI::h3("user object"), |
| 404 | # ref2string($self->{user}), |
520 | ref2string($self->{user}), |
| 405 | # CGI::h3("set object"), |
521 | CGI::h3("set object"), |
| 406 | # ref2string($set), |
522 | ref2string($set), |
| 407 | # CGI::h3("problem object"), |
523 | CGI::h3("problem object"), |
| 408 | # ref2string($problem), |
524 | ref2string($problem), |
| 409 | # CGI::h3("PG object"), |
525 | CGI::h3("PG object"), |
| 410 | # ref2string($pg, {'WeBWorK::PG::Translator' => 1}); |
526 | ref2string($pg, {'WeBWorK::PG::Translator' => 1}); |
|
|
527 | } |
| 411 | |
528 | |
| 412 | return ""; |
529 | return ""; |
| 413 | } |
530 | } |
| 414 | |
531 | |
| 415 | ##### output utilities ##### |
532 | ##### output utilities ##### |
| 416 | |
533 | |
| 417 | # this is used by ProblemSet.pm too, so don't fuck it up |
|
|
| 418 | sub translationError($$) { |
|
|
| 419 | my ($error, $details) = @_; |
|
|
| 420 | return |
|
|
| 421 | CGI::h2("Software Error"), |
|
|
| 422 | CGI::p(<<EOF), |
|
|
| 423 | WeBWorK has encountered a software error while attempting to process this problem. |
|
|
| 424 | It is likely that there is an error in the problem itself. |
|
|
| 425 | If you are a student, contact your professor to have the error corrected. |
|
|
| 426 | If you are a professor, please consut the error output below for more informaiton. |
|
|
| 427 | EOF |
|
|
| 428 | CGI::h3("Error messages"), CGI::blockquote(CGI::pre($error)), |
|
|
| 429 | CGI::h3("Error context"), CGI::blockquote(CGI::pre($details)); |
|
|
| 430 | } |
|
|
| 431 | |
|
|
| 432 | # this is used by ProblemSet.pm too, so don't fuck it up |
|
|
| 433 | sub warningOutput($) { |
|
|
| 434 | my $warnings = shift; |
|
|
| 435 | |
|
|
| 436 | return |
|
|
| 437 | CGI::h2("Software Warnings"), |
|
|
| 438 | CGI::p(<<EOF), |
|
|
| 439 | WeBWorK has encountered warnings while attempting to process this problem. |
|
|
| 440 | It is likely that this indicates an error or ambiguity in the problem itself. |
|
|
| 441 | If you are a student, contact your professor to have the problem corrected. |
|
|
| 442 | If you are a professor, please consut the error output below for more informaiton. |
|
|
| 443 | EOF |
|
|
| 444 | CGI::h3("Warning messages"), |
|
|
| 445 | CGI::blockquote(CGI::pre($warnings)), |
|
|
| 446 | ; |
|
|
| 447 | } |
|
|
| 448 | |
|
|
| 449 | sub attemptResults($$$$$) { |
534 | sub attemptResults($$$$$$) { |
| 450 | my $self = shift; |
535 | my $self = shift; |
| 451 | my $pg = shift; |
536 | my $pg = shift; |
| 452 | my $showAttemptAnswers = shift; |
537 | my $showAttemptAnswers = shift; |
| 453 | my $showCorrectAnswers = shift; |
538 | my $showCorrectAnswers = shift; |
| 454 | my $showAttemptResults = $showAttemptAnswers && shift; |
539 | my $showAttemptResults = $showAttemptAnswers && shift; |
|
|
540 | my $showSummary = shift; |
|
|
541 | my $showAttemptPreview = shift || 0; |
| 455 | my $problemResult = $pg->{result}; # the overall result of the problem |
542 | my $problemResult = $pg->{result}; # the overall result of the problem |
| 456 | my @answerNames = @{ $pg->{flags}->{ANSWER_ENTRY_ORDER} }; |
543 | my @answerNames = @{ $pg->{flags}->{ANSWER_ENTRY_ORDER} }; |
| 457 | |
544 | |
|
|
545 | my $showMessages = $showAttemptAnswers && grep { $pg->{answers}->{$_}->{ans_message} } @answerNames; |
|
|
546 | |
| 458 | my $header = CGI::th("answer"); |
547 | my $header = CGI::th("Part"); |
| 459 | $header .= $showAttemptAnswers ? CGI::th("attempt") : ""; |
548 | $header .= $showAttemptAnswers ? CGI::th("Entered") : ""; |
| 460 | $header .= $showAttemptAnswers ? CGI::th("preview") : ""; |
549 | $header .= $showAttemptPreview ? CGI::th("Answer Preview") : ""; |
| 461 | $header .= $showCorrectAnswers ? CGI::th("correct") : ""; |
550 | $header .= $showCorrectAnswers ? CGI::th("Correct") : ""; |
| 462 | $header .= $showAttemptResults ? CGI::th("result") : ""; |
551 | $header .= $showAttemptResults ? CGI::th("Result") : ""; |
| 463 | $header .= $showAttemptAnswers ? CGI::th("messages") : ""; |
552 | $header .= $showMessages ? CGI::th("messages") : ""; |
| 464 | my @tableRows = ( $header ); |
553 | my @tableRows = ( $header ); |
| 465 | my $numCorrect; |
554 | my $numCorrect; |
| 466 | foreach my $name (@answerNames) { |
555 | foreach my $name (@answerNames) { |
| 467 | my $answerResult = $pg->{answers}->{$name}; |
556 | my $answerResult = $pg->{answers}->{$name}; |
| 468 | my $studentAnswer = $answerResult->{student_ans}; # original_student_ans |
557 | my $studentAnswer = $answerResult->{student_ans}; # original_student_ans |
| 469 | my $preview = $self->previewAnswer($answerResult); |
558 | my $preview = ($showAttemptPreview |
|
|
559 | ? $self->previewAnswer($answerResult) |
|
|
560 | : ""); |
| 470 | my $correctAnswer = $answerResult->{correct_ans}; |
561 | my $correctAnswer = $answerResult->{correct_ans}; |
| 471 | my $answerScore = $answerResult->{score}; |
562 | my $answerScore = $answerResult->{score}; |
| 472 | my $answerMessage = $showAttemptAnswers ? $answerResult->{ans_message} : ""; |
563 | my $answerMessage = $showMessages ? $answerResult->{ans_message} : ""; |
| 473 | |
564 | |
| 474 | $numCorrect += $answerScore > 0; |
565 | $numCorrect += $answerScore > 0; |
| 475 | my $resultString = $answerScore ? "correct" : "incorrect"; |
566 | my $resultString = $answerScore ? "correct" : "incorrect"; |
| 476 | |
567 | |
| 477 | # get rid of the goofy prefix on the answer names (supposedly, the format |
568 | # get rid of the goofy prefix on the answer names (supposedly, the format |
| 478 | # of the answer names is changeable. this only fixes |
569 | # of the answer names is changeable. this only fixes it for "AnSwEr" |
| 479 | $name =~ s/^AnSwEr//; |
570 | $name =~ s/^AnSwEr//; |
| 480 | |
571 | |
| 481 | my $row = CGI::td($name); |
572 | my $row = CGI::td($name); |
| 482 | $row .= $showAttemptAnswers ? CGI::td($studentAnswer) : ""; |
573 | $row .= $showAttemptAnswers ? CGI::td($studentAnswer) : ""; |
| 483 | $row .= $showAttemptAnswers ? CGI::td($preview) : ""; |
574 | $row .= $showAttemptPreview ? CGI::td($preview) : ""; |
| 484 | $row .= $showCorrectAnswers ? CGI::td($correctAnswer) : ""; |
575 | $row .= $showCorrectAnswers ? CGI::td($correctAnswer) : ""; |
| 485 | $row .= $showAttemptResults ? CGI::td($resultString) : ""; |
576 | $row .= $showAttemptResults ? CGI::td($resultString) : ""; |
| 486 | $row .= $answerMessage ? CGI::td($answerMessage) : ""; |
577 | $row .= $answerMessage ? CGI::td($answerMessage) : ""; |
| 487 | push @tableRows, $row; |
578 | push @tableRows, $row; |
| 488 | } |
579 | } |
| 489 | |
580 | |
| 490 | my $numCorrectNoun = $numCorrect == 1 ? "question" : "questions"; |
581 | my $numIncorrectNoun = scalar @answerNames == 1 ? "question" : "questions"; |
| 491 | my $scorePercent = int ($problemResult->{score} * 100) . "\%"; |
582 | my $scorePercent = int ($problemResult->{score} * 100) . "\%"; |
| 492 | my $summary = "On this attempt, you answered $numCorrect $numCorrectNoun out of " |
583 | my $summary = "On this attempt, you answered $numCorrect out of " |
| 493 | . scalar @answerNames . " correct, for a score of $scorePercent."; |
584 | . scalar @answerNames . " $numIncorrectNoun correct, for a score of $scorePercent."; |
| 494 | return CGI::table({-border=>1}, CGI::Tr(\@tableRows)) . CGI::p($summary); |
585 | return CGI::table({-class=>"attemptResults"}, CGI::Tr(\@tableRows)) . ($showSummary ? CGI::p($summary) : ""); |
| 495 | } |
586 | } |
| 496 | |
587 | |
| 497 | sub viewOptions($) { |
588 | sub viewOptions($) { |
| 498 | my $self = shift; |
589 | my $self = shift; |
| 499 | my $displayMode = $self->{displayMode}; |
590 | my $displayMode = $self->{displayMode}; |
| … | |
… | |
| 546 | ); |
637 | ); |
| 547 | } |
638 | } |
| 548 | |
639 | |
| 549 | sub previewAnswer($$) { |
640 | sub previewAnswer($$) { |
| 550 | my ($self, $answerResult) = @_; |
641 | my ($self, $answerResult) = @_; |
| 551 | my $ce = $self->{courseEnvironment}; |
642 | my $ce = $self->{ce}; |
| 552 | my $user = $self->{user}; |
643 | my $effectiveUser = $self->{effectiveUser}; |
| 553 | my $set = $self->{set}; |
644 | my $set = $self->{set}; |
| 554 | my $problem = $self->{problem}; |
645 | my $problem = $self->{problem}; |
|
|
646 | my $displayMode = $self->{displayMode}; |
| 555 | |
647 | |
| 556 | # how are we going to name this? |
648 | # note: right now, we have to do things completely differently when we are |
| 557 | my $targetPathCommon = "/png/" |
649 | # rendering math from INSIDE the translator and from OUTSIDE the translator. |
| 558 | . $user->id . "." |
650 | # so we'll just deal with each case explicitly here. there's some code |
| 559 | . $set->id . "." |
651 | # duplication that can be dealt with later by abstracting out tth/dvipng/etc. |
| 560 | . $problem->id . "." |
|
|
| 561 | . $answerResult->{ans_name} . ".png"; |
|
|
| 562 | |
652 | |
| 563 | # figure out where to put things |
|
|
| 564 | my $wd = tempdir("webwork-dvipng-XXXXXXXX", DIR => $ce->{courseDirs}->{html_temp}); |
|
|
| 565 | my $latex = $ce->{externalPrograms}->{latex}; |
|
|
| 566 | my $dvipng = $ce->{externalPrograms}->{dvipng}; |
|
|
| 567 | my $tex = $answerResult->{preview_latex_string}; |
653 | my $tex = $answerResult->{preview_latex_string}; |
|
|
654 | |
|
|
655 | return "" if $tex eq ""; |
|
|
656 | |
|
|
657 | if ($displayMode eq "plainText") { |
|
|
658 | return $tex; |
|
|
659 | } elsif ($displayMode eq "formattedText") { |
|
|
660 | my $tthCommand = $ce->{externalPrograms}->{tth} |
|
|
661 | . " -L -f5 -r 2> /dev/null <<END_OF_INPUT; echo > /dev/null\n" |
|
|
662 | . "\\(".$tex."\\)\n" |
|
|
663 | . "END_OF_INPUT\n"; |
|
|
664 | |
|
|
665 | # call tth |
|
|
666 | my $result = `$tthCommand`; |
|
|
667 | if ($?) { |
|
|
668 | return "<b>[tth failed: $? $@]</b>"; |
|
|
669 | } |
|
|
670 | return $result; |
|
|
671 | } elsif ($displayMode eq "images") { |
|
|
672 | # how are we going to name this? |
|
|
673 | my $targetPathCommon = "/png/" |
|
|
674 | . $effectiveUser->user_id . "." |
|
|
675 | . $set->set_id . "." |
|
|
676 | . $problem->problem_id . "." |
|
|
677 | . $answerResult->{ans_name} . ".png"; |
|
|
678 | |
|
|
679 | # figure out where to put things |
|
|
680 | my $wd = tempdir("webwork-dvipng-XXXXXXXX", DIR => $ce->{courseDirs}->{html_temp}); |
|
|
681 | my $latex = $ce->{externalPrograms}->{latex}; |
|
|
682 | my $dvipng = $ce->{externalPrograms}->{dvipng}; |
| 568 | my $targetPath = $ce->{courseDirs}->{html_temp} . $targetPathCommon; |
683 | my $targetPath = $ce->{courseDirs}->{html_temp} . $targetPathCommon; |
| 569 | # should use surePathToTmpFile, but we have to |
684 | # should use surePathToTmpFile, but we have to |
| 570 | # isolate it from the problem enivronment first |
685 | # isolate it from the problem enivronment first |
| 571 | my $targetURL = $ce->{courseURLs}->{html_temp} . $targetPathCommon; |
686 | my $targetURL = $ce->{courseURLs}->{html_temp} . $targetPathCommon; |
| 572 | |
687 | |
| 573 | # call dvipng to generate a preview |
688 | # call dvipng to generate a preview |
| 574 | dvipng($wd, $latex, $dvipng, $tex, $targetPath); |
689 | dvipng($wd, $latex, $dvipng, $tex, $targetPath); |
|
|
690 | rmtree($envir->{dvipngTempDir}, 0, 0); |
| 575 | if (-e $targetPath) { |
691 | if (-e $targetPath) { |
| 576 | return "<img src=\"$targetURL\" alt=\"$tex\" />"; |
692 | return "<img src=\"$targetURL\" alt=\"$tex\" />"; |
| 577 | } else { |
693 | } else { |
| 578 | return "<b>[math2img failed]</b>"; |
694 | return "<b>[math2img failed]</b>"; |
| 579 | } |
695 | } |
|
|
696 | } |
| 580 | } |
697 | } |
|
|
698 | ##### logging subroutine #### |
|
|
699 | |
|
|
700 | |
| 581 | |
701 | |
| 582 | ##### permission queries ##### |
702 | ##### permission queries ##### |
| 583 | |
703 | |
| 584 | # this stuff should be abstracted out into the permissions system |
704 | # this stuff should be abstracted out into the permissions system |
| 585 | # however, the permission system only knows about things in the |
705 | # however, the permission system only knows about things in the |
| … | |
… | |
| 606 | my $attemptsOK = $maxAttempts == -1 || $attempts <= $maxAttempts; |
726 | my $attemptsOK = $maxAttempts == -1 || $attempts <= $maxAttempts; |
| 607 | my $recordAnswers = $permHigh || ($timeOK && $attemptsOK); |
727 | my $recordAnswers = $permHigh || ($timeOK && $attemptsOK); |
| 608 | return $recordAnswers; |
728 | return $recordAnswers; |
| 609 | } |
729 | } |
| 610 | |
730 | |
|
|
731 | sub canCheckAnswers($$) { |
|
|
732 | my ($permissionLevel, $answerDate) = @_; |
|
|
733 | my $permHigh = $permissionLevel > 0; |
|
|
734 | my $timeOK = time >= $answerDate; |
|
|
735 | my $recordAnswers = $permHigh || $timeOK; |
|
|
736 | return $recordAnswers; |
|
|
737 | } |
|
|
738 | |
| 611 | sub mustRecordAnswers($) { |
739 | sub mustRecordAnswers($) { |
| 612 | my ($permissionLevel) = @_; |
740 | my ($permissionLevel) = @_; |
| 613 | return $permissionLevel == 0; |
741 | return $permissionLevel == 0; |
| 614 | } |
742 | } |
| 615 | |
743 | |