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