[system] / trunk / webwork-modperl / lib / WeBWorK / ContentGenerator / Problem.pm Repository:
ViewVC logotype

Diff of /trunk/webwork-modperl/lib/WeBWorK/ContentGenerator/Problem.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.719  
changed lines
  Added in v.1038

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9