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