[system] / trunk / webwork2 / lib / WeBWorK / ContentGenerator / Problem.pm Repository:
ViewVC logotype

Diff of /trunk/webwork2/lib/WeBWorK/ContentGenerator/Problem.pm

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

Revision 1744 Revision 2213
1################################################################################ 1################################################################################
2# WeBWorK Online Homework Delivery System 2# WeBWorK Online Homework Delivery System
3# Copyright © 2000-2003 The WeBWorK Project, http://openwebwork.sf.net/ 3# Copyright © 2000-2003 The WeBWorK Project, http://openwebwork.sf.net/
4# $CVSHeader: webwork-modperl/lib/WeBWorK/ContentGenerator/Problem.pm,v 1.111 2004/01/23 13:13:53 gage Exp $ 4# $CVSHeader: webwork-modperl/lib/WeBWorK/ContentGenerator/Problem.pm,v 1.136 2004/05/24 20:19:08 toenail 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.
33use WeBWorK::PG::IO; 33use WeBWorK::PG::IO;
34use WeBWorK::Utils qw(writeLog writeCourseLog encodeAnswers decodeAnswers ref2string makeTempDirectory); 34use WeBWorK::Utils qw(writeLog writeCourseLog encodeAnswers decodeAnswers ref2string makeTempDirectory);
35use WeBWorK::DB::Utils qw(global2user user2global findDefaults); 35use WeBWorK::DB::Utils qw(global2user user2global findDefaults);
36use WeBWorK::Timing; 36use WeBWorK::Timing;
37 37
38use WeBWorK::Utils::Tasks qw(fake_set fake_problem);
38 39
39############################################################ 40############################################################
40# 41#
41# user 42# user
42# effectiveUser 43# effectiveUser
63sub templateName { 64sub templateName {
64 "problem"; 65 "problem";
65} 66}
66 67
67sub pre_header_initialize { 68sub pre_header_initialize {
68 my ($self, $setName, $problemNumber) = @_; 69 my ($self) = @_;
69 my $r = $self->{r}; 70 my $r = $self->r;
70 my $courseEnv = $self->{ce}; 71 my $ce = $r->ce;
71 my $db = $self->{db}; 72 my $db = $r->db;
73 my $urlpath = $r->urlpath;
74
75 my $setName = $urlpath->arg("setID");
76 my $problemNumber = $r->urlpath->arg("problemID");
72 my $userName = $r->param('user'); 77 my $userName = $r->param('user');
73 my $effectiveUserName = $r->param('effectiveUser'); 78 my $effectiveUserName = $r->param('effectiveUser');
74 my $key = $r->param('key'); 79 my $key = $r->param('key');
75 80
76 my $user = $db->getUser($userName); # checked 81 my $user = $db->getUser($userName); # checked
77 die "record for user $userName (real user) does not exist." 82 die "record for user $userName (real user) does not exist."
78 unless defined $user; 83 unless defined $user;
79 84
89 # obtain the merged set for $effectiveUser 94 # obtain the merged set for $effectiveUser
90 my $set = $db->getMergedSet($effectiveUserName, $setName); # checked 95 my $set = $db->getMergedSet($effectiveUserName, $setName); # checked
91 96
92 # obtain the merged problem for $effectiveUser 97 # obtain the merged problem for $effectiveUser
93 my $problem = $db->getMergedProblem($effectiveUserName, $setName, $problemNumber); # checked 98 my $problem = $db->getMergedProblem($effectiveUserName, $setName, $problemNumber); # checked
94 99
95 my $editMode = $r->param("editMode"); 100 my $editMode = $r->param("editMode");
96 101
97 if ($permissionLevel > 0 and defined $editMode) { 102 if ($permissionLevel > 0) {
98 # professors are allowed to fabricate sets and problems not 103 # professors are allowed to fabricate sets and problems not
99 # assigned to them (or anyone). this allows them to use the 104 # assigned to them (or anyone). this allows them to use the
100 # editor to 105 # editor to
101 106
102 # if that is not yet defined obtain the global set, convert 107 # if that is not yet defined obtain the global set, convert
103 # it to a user set, and add fake user data 108 # it to a user set, and add fake user data
104 unless (defined $set) { 109 unless (defined $set) {
105 my $userSetClass = $db->{set_user}->{record}; 110 my $userSetClass = $db->{set_user}->{record};
106 my $globalSet = $db->getGlobalSet($setName); # checked 111 my $globalSet = $db->getGlobalSet($setName); # checked
107 # if the global set doesn't exist either, bail! 112 # if the global set doesn't exist either, bail!
108 die "Set $setName does not exist" 113 if(not defined $globalSet) {
109 unless defined $set; 114 $set = fake_set($db);
115 } else {
110 $set = global2user($userSetClass, $globalSet); 116 $set = global2user($userSetClass, $globalSet);
111 $set->psvn(0); 117 $set->psvn(0);
118
119 # FIXME: This is a temporary fix to fill in the database
120 # We want the published field to contain either 1 or 0 so if it has not been set to 0, default to 1
121 # this will fill in all the empty fields but not change anything that has been specifically set to 1 or 0
122 $globalSet->published("1") unless $globalSet->published eq "0";
123 $db->putGlobalSet($globalSet);
124 }
112 } 125 }
113 126
114 # if that is not yet defined obtain the global problem, 127 # if that is not yet defined obtain the global problem,
115 # convert it to a user problem, and add fake user data 128 # convert it to a user problem, and add fake user data
116 unless (defined $problem) { 129 unless (defined $problem) {
117 my $userProblemClass = $db->{problem_user}->{record}; 130 my $userProblemClass = $db->{problem_user}->{record};
118 my $globalProblem = $db->getGlobalProblem($setName, $problemNumber); # checked 131 my $globalProblem = $db->getGlobalProblem($setName, $problemNumber); # checked
119 # if the global problem doesn't exist either, bail! 132 # if the global problem doesn't exist either, bail!
133 if(not defined $globalProblem) {
134 my $sourceFilePath = $r->param("sourceFilePath");
120 die "Problem $problemNumber in set $setName does not exist" 135 die "Problem $problemNumber in set $setName does not exist"
121 unless defined $problem; 136 unless defined $sourceFilePath;
137 $problem = fake_problem($db);
138 $problem->problem_id(1);
139 $problem->source_file($sourceFilePath);
140 $problem->user_id($effectiveUserName);
141 } else {
122 $problem = global2user($userProblemClass, $globalProblem); 142 $problem = global2user($userProblemClass, $globalProblem);
123 $problem->user_id($effectiveUserName); 143 $problem->user_id($effectiveUserName);
124 $problem->problem_seed(0); 144 $problem->problem_seed(0);
125 $problem->status(0); 145 $problem->status(0);
126 $problem->attempted(0); 146 $problem->attempted(0);
127 $problem->last_answer(""); 147 $problem->last_answer("");
128 $problem->num_correct(0); 148 $problem->num_correct(0);
129 $problem->num_incorrect(0); 149 $problem->num_incorrect(0);
150 }
130 } 151 }
131 152
132 # now we're sure we have valid UserSet and UserProblem objects 153 # now we're sure we have valid UserSet and UserProblem objects
133 # yay! 154 # yay!
134 155
135 # now deal with possible editor overrides: 156 # now deal with possible editor overrides:
136 157
137 # if the caller is asking to override the source file, and 158 # if the caller is asking to override the source file, and
138 # editMode calls for a temporary file, do so 159 # editMode calls for a temporary file, do so
139 my $sourceFilePath = $r->param("sourceFilePath"); 160 my $sourceFilePath = $r->param("sourceFilePath");
140 if (defined $sourceFilePath and $editMode eq "temporaryFile") { 161 if (defined $sourceFilePath and
162 (not defined $editMode or $editMode eq "temporaryFile")) {
141 $problem->source_file($sourceFilePath); 163 $problem->source_file($sourceFilePath);
142 } 164 }
143 165
144 # if the caller is asking to override the problem seed, do so 166 # if the caller is asking to override the problem seed, do so
145 my $problemSeed = $r->param("problemSeed"); 167 my $problemSeed = $r->param("problemSeed");
146 if (defined $problemSeed) { 168 if (defined $problemSeed) {
147 $problem->problem_seed($problemSeed); 169 $problem->problem_seed($problemSeed);
148 } 170 }
171
172 my $published = ($set->published) ? "visable to students." : "hidden from students.";
173 $self->addmessage(CGI::p("This set is " . CGI::font({class=>$published}, $published)));
149 } else { 174 } else {
175
176 $self->addmessage(CGI::div({class=>"ResultsWithError"}, CGI::p("This problem will not count towards your grade."))) unless $problem->value;;
150 # students can't view problems not assigned to them 177 # students can't view problems not assigned to them
151 die "Set $setName is not assigned to $effectiveUserName" 178
152 unless defined $set; 179 # A set is valid if it exists and if it is either published or the user is privileged.
153 die "Problem $problemNumber in set $setName is not assigned to $effectiveUserName" 180 $self->{invalidSet} = (grep /$setName/, $db->listUserSets($effectiveUserName)) == 0 || !($set->published || $permissionLevel->permission > 0); # this is redundant because of the above if
154 unless defined $problem; 181 $self->{invalidProblem} = (grep /$problemNumber/, $db->listUserProblems($effectiveUserName, $setName)) == 0 || !($set->published || $permissionLevel->permission > 0);;
182
155 } 183 }
156 184
157 $self->{userName} = $userName; 185 $self->{userName} = $userName;
158 $self->{effectiveUserName} = $effectiveUserName; 186 $self->{effectiveUserName} = $effectiveUserName;
159 $self->{user} = $user; 187 $self->{user} = $user;
164 $self->{editMode} = $editMode; 192 $self->{editMode} = $editMode;
165 193
166 ##### form processing ##### 194 ##### form processing #####
167 195
168 # set options from form fields (see comment at top of file for names) 196 # set options from form fields (see comment at top of file for names)
169 my $displayMode = $r->param("displayMode") || $courseEnv->{pg}->{options}->{displayMode}; 197 my $displayMode = $r->param("displayMode") || $ce->{pg}->{options}->{displayMode};
170 my $redisplay = $r->param("redisplay"); 198 my $redisplay = $r->param("redisplay");
171 my $submitAnswers = $r->param("submitAnswers"); 199 my $submitAnswers = $r->param("submitAnswers");
172 my $checkAnswers = $r->param("checkAnswers"); 200 my $checkAnswers = $r->param("checkAnswers");
173 my $previewAnswers = $r->param("previewAnswers"); 201 my $previewAnswers = $r->param("previewAnswers");
174 202
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 }; 203 my $formFields = { WeBWorK::Form->new_from_paramable($r)->Vars };
196
197 204
198 $self->{displayMode} = $displayMode; 205 $self->{displayMode} = $displayMode;
199 $self->{redisplay} = $redisplay; 206 $self->{redisplay} = $redisplay;
200 $self->{submitAnswers} = $submitAnswers; 207 $self->{submitAnswers} = $submitAnswers;
201 $self->{checkAnswers} = $checkAnswers; 208 $self->{checkAnswers} = $checkAnswers;
202 $self->{previewAnswers} = $previewAnswers; 209 $self->{previewAnswers} = $previewAnswers;
203 $self->{formFields} = $formFields; 210 $self->{formFields} = $formFields;
211
212 # get result and send to message
213 my $success = $r->param("sucess");
214 my $failure = $r->param("failure");
215 $self->addmessage(CGI::div({class=>"ResultsWithError"}, CGI::p($failure))) if $failure;
216 $self->addmessage(CGI::div({class=>"ResultsWithoutError"}, CGI::p($success))) if $success;
217
218 # now that we've set all the necessary variables quit out if the set or problem is invalid
219 return if $self->{invalidSet} || $self->{invalidProblem};
204 220
205 ##### permissions ##### 221 ##### permissions #####
206 222
207 # are we allowed to view this problem? 223 # are we allowed to view this problem?
208 $self->{isOpen} = time >= $set->open_date || $permissionLevel > 0; 224 $self->{isOpen} = time >= $set->open_date || $permissionLevel > 0;
209 return unless $self->{isOpen}; 225 return unless $self->{isOpen};
210 226
211 # what does the user want to do? 227 # what does the user want to do?
212 my %want = ( 228 my %want = (
213 showOldAnswers => $r->param("showOldAnswers") || $courseEnv->{pg}->{options}->{showOldAnswers}, 229 showOldAnswers => $r->param("showOldAnswers") || $ce->{pg}->{options}->{showOldAnswers},
214 showCorrectAnswers => $r->param("showCorrectAnswers") || $courseEnv->{pg}->{options}->{showCorrectAnswers}, 230 showCorrectAnswers => $r->param("showCorrectAnswers") || $ce->{pg}->{options}->{showCorrectAnswers},
215 showHints => $r->param("showHints") || $courseEnv->{pg}->{options}->{showHints}, 231 showHints => $r->param("showHints") || $ce->{pg}->{options}->{showHints},
216 showSolutions => $r->param("showSolutions") || $courseEnv->{pg}->{options}->{showSolutions}, 232 showSolutions => $r->param("showSolutions") || $ce->{pg}->{options}->{showSolutions},
217 recordAnswers => $submitAnswers, 233 recordAnswers => $submitAnswers,
218 checkAnswers => $checkAnswers, 234 checkAnswers => $checkAnswers,
219 ); 235 );
220 236
221 # are certain options enforced? 237 # are certain options enforced?
237 recordAnswers => canRecordAnswers($permissionLevel, $set->open_date, $set->due_date, 253 recordAnswers => canRecordAnswers($permissionLevel, $set->open_date, $set->due_date,
238 $problem->max_attempts, $problem->num_correct + $problem->num_incorrect + 1), 254 $problem->max_attempts, $problem->num_correct + $problem->num_incorrect + 1),
239 # attempts=num_correct+num_incorrect+1, as this happens before updating $problem 255 # attempts=num_correct+num_incorrect+1, as this happens before updating $problem
240 checkAnswers => canCheckAnswers($permissionLevel, $set->answer_date), 256 checkAnswers => canCheckAnswers($permissionLevel, $set->answer_date),
241 ); 257 );
242 ######################################################### 258
243 # more complicated logic for showing check answer button: 259 # more complicated logic for showing check answer button:
244 #########################################################
245 # checkAnswers button shows up after due date -- once a student can't record anymore 260 # 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 261 # checkAnswers button always shows up when an instructor or TA is acting
247 # as someone else (the $user and $effectiveUserName aren't the same). 262 # as someone else (the $user and $effectiveUserName aren't the same).
248 $can{checkAnswers} = ($can{checkAnswers} && not $can{recordAnswers} ) || 263 $can{checkAnswers} = (
249 ( defined($userName) and defined($effectiveUserName) and 264 # $can{recordAnswers} will be false if the due date has passed OR the
250 ($userName ne $effectiveUserName) 265 # student has used up all of her attempts
251 ); 266 ($can{checkAnswers} and not $can{recordAnswers})
252 ######################################################### 267 or
268 (
269 # FIXME: this is not the right way to check for this.
270 # also, canCheckAnswers() will show this button if the permission
271 # level is positive, which is always true when an instructor is
272 # acting as a student
273 defined($userName)
274 and
275 defined($effectiveUserName)
276 and
277 ($userName ne $effectiveUserName)
278 )
279 );
280
253 # more complicated logif for showing "submit answer" button 281 # more complicated logif for showing "submit answer" button:
254 #########################################################
255 # We hide the submit answer button if someone is acting as a student 282 # 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 283 # This prevents errors where you accidently submit the answer for a student
257 # Not sure whether this a feature or a bug 284 # Not sure whether this a feature or a bug
285 $can{recordAnswers} = (
286 $can{recordAnswers}
287 and not
288 (
289 # FIXME: this is not the right way to check for this.
290 defined($userName)
291 and
292 defined($effectiveUserName)
293 and
294 ($userName ne $effectiveUserName)
295 )
296 );
258 297
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 298 # final values for options
265 my %will; 299 my %will;
266 foreach (keys %must) { 300 foreach (keys %must) {
267 $will{$_} = $can{$_} && ($want{$_} || $must{$_}); 301 $will{$_} = $can{$_} && ($want{$_} || $must{$_});
268 } 302 }
277 311
278 ##### translation ##### 312 ##### translation #####
279 313
280 $WeBWorK::timer->continue("begin pg processing") if defined($WeBWorK::timer); 314 $WeBWorK::timer->continue("begin pg processing") if defined($WeBWorK::timer);
281 my $pg = WeBWorK::PG->new( 315 my $pg = WeBWorK::PG->new(
282 $courseEnv, 316 $ce,
283 $effectiveUser, 317 $effectiveUser,
284 $key, 318 $key,
285 $set, 319 $set,
286 $problem, 320 $problem,
287 $set->psvn, # FIXME: this field should be removed 321 $set->psvn, # FIXME: this field should be removed
309 $self->{can} = \%can; 343 $self->{can} = \%can;
310 $self->{will} = \%will; 344 $self->{will} = \%will;
311 $self->{pg} = $pg; 345 $self->{pg} = $pg;
312} 346}
313 347
314#sub if_warnings($$) {
315# my ($self, $arg) = @_;
316# return 0 unless $self->{isOpen};
317# return $self->{pg}->{warnings} ne "";
318#}
319
320sub if_errors($$) { 348sub if_errors($$) {
321 my ($self, $arg) = @_; 349 my ($self, $arg) = @_;
322 return 0 unless $self->{isOpen}; 350
351 if ($self->{isOpen}) {
323 return $self->{pg}->{flags}->{error_flag}; 352 return $self->{pg}->{flags}->{error_flag} ? $arg : !$arg;
353 } else {
354 return !$arg;
355 }
324} 356}
325 357
326sub head { 358sub head {
327 my $self = shift; 359 my ($self) = @_;
360
328 return "" unless $self->{isOpen}; 361 return "" unless $self->{isOpen};
329 return $self->{pg}->{head_text} if $self->{pg}->{head_text}; 362 return $self->{pg}->{head_text} if $self->{pg}->{head_text};
330} 363}
331 364
332sub options { 365sub options {
333 my $self = shift; 366 my ($self) = @_;
367
368 return "" if $self->{invalidProblem};
369
334 return join("", 370 return join("",
335 CGI::start_form("POST", $self->{r}->uri), 371 CGI::start_form("POST", $self->{r}->uri),
336 $self->hidden_authen_fields, 372 $self->hidden_authen_fields,
337 CGI::hr(), 373 CGI::hr(),
338 CGI::start_div({class=>"viewOptions"}), 374 CGI::start_div({class=>"viewOptions"}),
340 CGI::end_div(), 376 CGI::end_div(),
341 CGI::end_form() 377 CGI::end_form()
342 ); 378 );
343} 379}
344 380
345sub path { 381#sub path {
346 my $self = shift; 382# my $self = shift;
347 my $args = $_[-1]; 383# my $args = $_[-1];
348 my $setName = $self->{set}->set_id; 384# my $setName = $self->{set}->set_id;
349 my $problemNumber = $self->{problem}->problem_id; 385# my $problemNumber = $self->{problem}->problem_id;
350 386#
351 my $ce = $self->{ce}; 387# my $ce = $self->{ce};
352 my $root = $ce->{webworkURLs}->{root}; 388# my $root = $ce->{webworkURLs}->{root};
353 my $courseName = $ce->{courseName}; 389# my $courseName = $ce->{courseName};
354 return $self->pathMacro($args, 390# return $self->pathMacro($args,
355 "Home" => "$root", 391# "Home" => "$root",
356 $courseName => "$root/$courseName", 392# $courseName => "$root/$courseName",
357 $setName => "$root/$courseName/$setName", 393# $setName => "$root/$courseName/$setName",
358 "Problem $problemNumber" => "", 394# "Problem $problemNumber" => "",
359 ); 395# );
360} 396#}
361 397
362sub siblings { 398sub siblings {
363 my $self = shift; 399 my ($self) = @_;
400 my $r = $self->r;
401 my $db = $r->db;
402 my $urlpath = $r->urlpath;
403
404 # can't show sibling problems if the set is invalid
405 return "" if $self->{invalidSet};
406
407 my $courseID = $urlpath->arg("courseID");
364 my $setName = $self->{set}->set_id; 408 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"); 409 my $eUserID = $r->param("effectiveUser");
374 my @problemIDs = $db->listUserProblems($effectiveUser, $setName); 410 my @problemIDs = sort { $a <=> $b } $db->listUserProblems($eUserID, $setID);
411
412 print CGI::start_ul({class=>"LinksMenu"});
413 print CGI::start_li();
414 print CGI::span({style=>"font-size:larger"}, "Problems");
415 print CGI::start_ul();
416
375 foreach my $problem (sort { $a <=> $b } @problemIDs) { 417 foreach my $problemID (@problemIDs) {
376 print '&nbsp;&nbsp;'.CGI::a({-href=>"$root/$courseName/$setName/".$problem."/?" 418 my $problemPage = $urlpath->newFromModule("WeBWorK::ContentGenerator::Problem",
377 . $self->url_authen_args . "&displayMode=" . $self->{displayMode}}, 419 courseID => $courseID, setID => $setID, problemID => $problemID);
378 "Problem ".$problem), CGI::br(); 420 print CGI::li(CGI::a({href=>$self->systemLink($problemPage, params=>{displayMode => $self->{displayMode}})}, "Problem $problemID"));
379 } 421 }
380 422
423 print CGI::end_ul();
424 print CGI::end_li();
425 print CGI::end_ul();
426
381 return ""; 427 return "";
382} 428}
383 429
384sub nav { 430sub nav {
385 $WeBWorK::timer->continue("begin nav subroutine") if defined($WeBWorK::timer); 431 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}; 432 my $r = $self->r;
392 my $db = $self->{db}; 433 my $db = $r->db;
393 my $root = $ce->{webworkURLs}->{root}; 434 my $urlpath = $r->urlpath;
394 my $courseName = $ce->{courseName};
395 435
396 my $wwdb = $self->{wwdb}; 436 my $courseID = $urlpath->arg("courseID");
437 my $setID = $self->{set}->set_id if !($self->{invalidSet});
438 my $problemID = $self->{problem}->problem_id if !($self->{invalidProblem});
397 my $effectiveUser = $self->{r}->param("effectiveUser"); 439 my $eUserID = $r->param("effectiveUser");
440
441 my ($prevID, $nextID);
442
443 if (!$self->{invalidProblem}) {
444 my @problemIDs = $db->listUserProblems($eUserID, $setID);
445 foreach my $id (@problemIDs) {
446 $prevID = $id if $id < $problemID
447 and (not defined $prevID or $id > $prevID);
448 $nextID = $id if $id > $problemID
449 and (not defined $nextID or $id < $nextID);
450 }
451 }
452
453 my @links;
454
455 if ($prevID) {
456 my $prevPage = $urlpath->newFromModule(__PACKAGE__,
457 courseID => $courseID, setID => $setID, problemID => $prevID);
458 push @links, "Previous Problem", $r->location . $prevPage->path, "navPrev";
459 } else {
460 push @links, "Previous Problem", "", "navPrev";
461 }
462
463 push @links, "Problem List", $r->location . $urlpath->parent->path, "navProbList";
464
465 if ($nextID) {
466 my $nextPage = $urlpath->newFromModule(__PACKAGE__,
467 courseID => $courseID, setID => $setID, problemID => $nextID);
468 push @links, "Next Problem", $r->location . $nextPage->path, "navNext";
469 } else {
470 push @links, "Next Problem", "", "navNext";
471 }
472
398 my $tail = "&displayMode=".$self->{displayMode}; 473 my $tail = "&displayMode=".$self->{displayMode};
399
400 my @links = ("Problem List" , "$root/$courseName/$setName", "navProbList");
401
402 my @problemIDs = $db->listUserProblems($effectiveUser, $setName);
403 my ($prevID, $nextID);
404 foreach my $id (@problemIDs) {
405 $prevID = $id if $id < $problemNumber
406 and (not defined $prevID or $id > $prevID);
407 $nextID = $id if $id > $problemNumber
408 and (not defined $nextID or $id < $nextID);
409 }
410 unshift @links, "Previous Problem" , ($prevID
411 ? "$root/$courseName/$setName/".$prevID
412 : "") , "navPrev";
413 push @links, "Next Problem" , ($nextID
414 ? "$root/$courseName/$setName/".$nextID
415 : "") , "navNext";
416
417 my $result = $self->navMacro($args, $tail, @links); 474 return $self->navMacro($args, $tail, @links);
418 $WeBWorK::timer->continue("end nav subroutine") if defined($WeBWorK::timer);
419 return $result;
420} 475}
421 476
422sub title { 477sub title {
423 my $self = shift; 478 my ($self) = @_;
479
480 # using the url arguments won't break if the set/problem are invalid
481 my $setID = $self->r->urlpath->arg("setID");
482 my $problemID = $self->r->urlpath->arg("problemID");
483
424 my $setName = $self->{set}->set_id; 484 #my $setID = $self->{set}->set_id;
425 my $problemNumber = $self->{problem}->problem_id; 485 #my $problemID = $self->{problem}->problem_id;
426 486
427 return "$setName : Problem $problemNumber"; 487 return "$setID : $problemID";
428} 488}
429 489
430sub body { 490sub body {
431 my $self = shift; 491 my $self = shift;
492 my $r = $self->r;
493 my $ce = $r->ce;
494 my $db = $r->db;
495 my $urlpath = $r->urlpath;
496
497 if ($self->{invalidSet}) {
498 return CGI::div({class=>"ResultsWithError"},
499 CGI::p("The selected problem set (" . $urlpath->arg("setID") . ") is not a valid set for " . $r->param("effectiveUser") . "."));
500 }
432 501
433 return CGI::p(CGI::font({-color=>"red"}, "This problem is not available because the problem set that contains it is not yet open.")) 502 if ($self->{invalidProblem}) {
503 return CGI::div({class=>"ResultsWithError"},
504 CGI::p("The selected problem (" . $urlpath->arg("problemID") . ") is not a valid problem for set " . $self->{set}->set_id . "."));
505 }
506
434 unless $self->{isOpen}; 507 unless ($self->{isOpen}) {
435 508 return CGI::div({class=>"ResultsWithError"},
509 CGI::p("This problem is not available because the problem set that contains it is not yet open."));
510 }
436 # unpack some useful variables 511 # 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}; 512 my $set = $self->{set};
443 my $problem = $self->{problem}; 513 my $problem = $self->{problem};
444 my $editMode = $self->{editMode}; 514 my $editMode = $self->{editMode};
445 my $permissionLevel = $self->{permissionLevel}; 515 my $permissionLevel = $self->{permissionLevel};
446 my $submitAnswers = $self->{submitAnswers}; 516 my $submitAnswers = $self->{submitAnswers};
450 my %can = %{ $self->{can} }; 520 my %can = %{ $self->{can} };
451 my %must = %{ $self->{must} }; 521 my %must = %{ $self->{must} };
452 my %will = %{ $self->{will} }; 522 my %will = %{ $self->{will} };
453 my $pg = $self->{pg}; 523 my $pg = $self->{pg};
454 524
455 525 #my $root = $ce->{webworkURLs}->{root};
526 my $courseName = $urlpath->arg("courseID");
456 527
457 #####create Editor link ##### 528 #####create Editor link #####
458 # print editor link if the user is an instructor AND the file is not in temporary editing mode 529 ## print editor link if the user is an instructor AND the file is not in temporary editing mode
459 my $editorLinkMessage = ''; 530 #my $editorLinkMessage = '';
460 # and ( (not defined($self->{editMode})) or $self->{editMode} eq 'savedFile') # FIXME is this needed? 531 ## and ( (not defined($self->{editMode})) or $self->{editMode} eq 'savedFile') # FIXME is this needed?
461 if ($self->{permissionLevel}>=10 ) { 532 #if ($self->{permissionLevel}>=10 ) {
462 $editorLinkMessage = CGI::a({-href=>$ce->{webworkURLs}->{root}."/$courseName/instructor/pgProblemEditor/". 533 # $editorLinkMessage = CGI::a({-href=>$ce->{webworkURLs}->{root}."/$courseName/instructor/pgProblemEditor/".
463 $set->set_id.'/'.$problem->problem_id.'?'.$self->url_authen_args},'Edit this problem'); 534 # $set->set_id.'/'.$problem->problem_id.'?'.$self->url_authen_args},'Edit this problem');
464 } 535 #}
536
537 my $editorLink = "";
538 # if we are here without a real problem set, carry that through
539 my $forced_field = [];
540 $forced_field = ['sourceFilePath' => $r->param("sourceFilePath")] if
541 ($set->set_id eq 'Undefined_Set');
542 if ($self->{permissionLevel}>=10) {
543 my $editorPage = $urlpath->newFromModule("WeBWorK::ContentGenerator::Instructor::PGProblemEditor",
544 courseID => $courseName, setID => $set->set_id, problemID => $problem->problem_id);
545 my $editorURL = $self->systemLink($editorPage, params=>$forced_field);
546 $editorLink = CGI::a({href=>$editorURL}, "Edit this problem");
547 }
548
465 ##### translation errors? ##### 549 ##### translation errors? #####
466 550
467 if ($pg->{flags}->{error_flag}) { 551 if ($pg->{flags}->{error_flag}) {
468 return $self->errorOutput($pg->{errors}, $pg->{body_text}.CGI::p($editorLinkMessage)); 552 print $self->errorOutput($pg->{errors}, $pg->{body_text});
553 print $editorLink;
554 return "";
469 } 555 }
470 556
471 ##### answer processing ##### 557 ##### answer processing #####
472 $WeBWorK::timer->continue("begin answer processing") if defined($WeBWorK::timer); 558 $WeBWorK::timer->continue("begin answer processing") if defined($WeBWorK::timer);
473 # if answers were submitted: 559 # if answers were submitted:
474 my $scoreRecordedMessage; 560 my $scoreRecordedMessage;
561 my $pureProblem;
475 if ($submitAnswers) { 562 if ($submitAnswers) {
476 # get a "pure" (unmerged) UserProblem to modify 563 # get a "pure" (unmerged) UserProblem to modify
477 # this will be undefined if the problem has not been assigned to this user 564 # 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 565 $pureProblem = $db->getUserProblem($problem->user_id, $problem->set_id, $problem->problem_id); # checked
479 if (defined $pureProblem) { 566 if (defined $pureProblem) {
480 # store answers in DB for sticky answers 567 # store answers in DB for sticky answers
481 my %answersToStore; 568 my %answersToStore;
482 my %answerHash = %{ $pg->{answers} }; 569 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!! 570 $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); 571 foreach (keys %answerHash);
572
485 # There may be some more answers to store -- one which are auxiliary entries to a primary answer. Evaluating 573 # 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 574 # 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. 575 # however we need to store them. Fortunately they are still in the input form.
488 my @extra_answer_names = @{ $pg->{flags}->{KEPT_EXTRA_ANSWERS}}; 576 my @extra_answer_names = @{ $pg->{flags}->{KEPT_EXTRA_ANSWERS}};
489
490 $answersToStore{$_} = $self->{formFields}->{$_} foreach (@extra_answer_names); 577 $answersToStore{$_} = $self->{formFields}->{$_} foreach (@extra_answer_names);
491 578
492 # Now let's encode these answers to store them -- append the extra answers to the end of answer entry order 579 # 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); 580 my @answer_order = (@{$pg->{flags}->{ANSWER_ENTRY_ORDER}}, @extra_answer_names);
494 my $answerString = encodeAnswers(%answersToStore, 581 my $answerString = encodeAnswers(%answersToStore,
542 } 629 }
543 630
544 # logging student answers 631 # logging student answers
545 632
546 my $answer_log = $self->{ce}->{courseFiles}->{logs}->{'answer_log'}; 633 my $answer_log = $self->{ce}->{courseFiles}->{logs}->{'answer_log'};
547 if ( defined($answer_log )) { 634 if ( defined($answer_log ) and defined($pureProblem)) {
548 if ($submitAnswers ) { 635 if ($submitAnswers ) {
549 my $answerString = ""; 636 my $answerString = "";
550 my %answerHash = %{ $pg->{answers} }; 637 my %answerHash = %{ $pg->{answers} };
551 $answerString = $answerString . $answerHash{$_}->{original_student_ans}."\t" 638 # FIXME this is the line 552 error. make sure original student ans is defined.
639 # The fact that it is not defined is probably due to an error in some answer evaluator.
640 # But I think it is useful to suppress this error message in the log.
552 foreach (sort keys %answerHash); 641 foreach (sort keys %answerHash) {
642 my $student_ans = $answerHash{$_}->{original_student_ans} ||'';
643 $answerString .= $student_ans."\t"
644 }
553 $answerString = '' unless defined($answerString); # insure string is defined. 645 $answerString = '' unless defined($answerString); # insure string is defined.
554 writeCourseLog($self->{ce}, "answer_log", 646 writeCourseLog($self->{ce}, "answer_log",
555 join("", 647 join("",
556 '|', $problem->user_id, 648 '|', $problem->user_id,
557 '|', $problem->set_id, 649 '|', $problem->set_id,
558 '|', $problem->problem_id, 650 '|', $problem->problem_id,
574 # custom message for editor 666 # custom message for editor
575 if ($permissionLevel >= 10 and defined $editMode) { 667 if ($permissionLevel >= 10 and defined $editMode) {
576 if ($editMode eq "temporaryFile") { 668 if ($editMode eq "temporaryFile") {
577 print CGI::p(CGI::i("Editing temporary file: ", $problem->source_file)); 669 print CGI::p(CGI::i("Editing temporary file: ", $problem->source_file));
578 } elsif ($editMode eq "savedFile") { 670 } elsif ($editMode eq "savedFile") {
671 # FIXME: this is all done automatically if submitError exists.
672 #if ( defined($r->param('submiterror')) and $r->param('submiterror') ) {
673 # FIXME The following line doesn't work because the submiterror hook has already been called.
674 # The actions below should take place during the initialization phase.
675 # $self->{submiterror} .= $r->param('submiterror');
676 # print CGI::p(CGI::div({class=>'ResultsWithError'},$self->{submiterror}));
677 #} else {
579 print CGI::p(CGI::i("Problem saved to: ", $problem->source_file)); 678 # print CGI::p(CGI::div({ class=>'ResultsWithoutError'}, "Problem saved to: ", $problem->source_file));
679 #}
580 } 680 }
581 } 681 }
582 682 #FIXME we need error messages here if the problem was really not saved.
583 # attempt summary 683 # attempt summary
584 #FIXME -- the following is a kludge: if showPartialCorrectAnswers is negative don't show anything. 684 #FIXME -- the following is a kludge: if showPartialCorrectAnswers is negative don't show anything.
585 # until after the due date 685 # until after the due date
586 # do I need to check $wills{howCorrectAnswers} to make preflight work?? 686 # do I need to check $wills{howCorrectAnswers} to make preflight work??
587 if (($pg->{flags}->{showPartialCorrectAnswers}>= 0 and $submitAnswers) ) { 687 if (($pg->{flags}->{showPartialCorrectAnswers}>= 0 and $submitAnswers) ) {
590 print $self->attemptResults($pg, 1, 690 print $self->attemptResults($pg, 1,
591 $will{showCorrectAnswers}, 691 $will{showCorrectAnswers},
592 $pg->{flags}->{showPartialCorrectAnswers}, 1, 1); 692 $pg->{flags}->{showPartialCorrectAnswers}, 1, 1);
593 } elsif ($checkAnswers) { 693 } elsif ($checkAnswers) {
594 # print this if user previewed answers 694 # print this if user previewed answers
595 print "ANSWERS ONLY CHECKED -- ",CGI::br(),"ANSWERS NOT RECORDED", CGI::br(); 695 print CGI::div({class=>'ResultsWithError'},"ANSWERS ONLY CHECKED -- ",CGI::br(),"ANSWERS NOT RECORDED", CGI::br() );
596 print $self->attemptResults($pg, 1, $will{showCorrectAnswers}, 1, 1, 1); 696 print $self->attemptResults($pg, 1, $will{showCorrectAnswers}, 1, 1, 1);
597 # show attempt answers 697 # show attempt answers
598 # show correct answers if asked 698 # show correct answers if asked
599 # show attempt results (correctness) 699 # show attempt results (correctness)
600 # show attempt previews 700 # show attempt previews
601 } elsif ($previewAnswers) { 701 } elsif ($previewAnswers) {
602 # print this if user previewed answers 702 # print this if user previewed answers
603 print "PREVIEW ONLY -- NOT RECORDED",CGI::br(),$self->attemptResults($pg, 1, 0, 0, 0, 1); 703 print CGI::div({class=>'ResultsWithError'},"PREVIEW ONLY -- NOT RECORDED"),CGI::br(),$self->attemptResults($pg, 1, 0, 0, 0, 1);
604 # show attempt answers 704 # show attempt answers
605 # don't show correct answers 705 # don't show correct answers
606 # don't show attempt results (correctness) 706 # don't show attempt results (correctness)
607 # show attempt previews 707 # show attempt previews
608 } 708 }
609 709
610 print CGI::end_div(); 710 print CGI::end_div();
611 711
612 print CGI::start_div({class=>"problem"}); 712 print CGI::start_div({class=>"problem"});
613 713
614 # main form 714 # main form
615 print 715 print
616 CGI::startform("POST", $r->uri), 716 CGI::startform("POST", $r->uri),
617 $self->hidden_authen_fields, 717 $self->hidden_authen_fields,
618 CGI::p($pg->{body_text}), 718 CGI::p($pg->{body_text}),
677 $setClosedMessage .= " However, since you are a privileged user, additional attempts will be recorded."; 777 $setClosedMessage .= " However, since you are a privileged user, additional attempts will be recorded.";
678 } else { 778 } else {
679 $setClosedMessage .= " Additional attempts will not be recorded."; 779 $setClosedMessage .= " Additional attempts will not be recorded.";
680 } 780 }
681 } 781 }
782
783 my $notCountedMessage = ($problem->value) ? "" : "(This problem will not count towards your grade.)";
682 print CGI::p( 784 print CGI::p(
683 $submitAnswers ? $scoreRecordedMessage . CGI::br() : "", 785 $submitAnswers ? $scoreRecordedMessage . CGI::br() : "",
684 "You have attempted this problem $attempts $attemptsNoun.", CGI::br(), 786 "You have attempted this problem $attempts $attemptsNoun.", CGI::br(),
685 $problem->attempted 787 $problem->attempted
686 ? "Your recorded score is $lastScore." . CGI::br() 788 ? "Your recorded score is $lastScore. $notCountedMessage" . CGI::br()
687 : "", 789 : "",
688 $setClosed ? $setClosedMessage : "You have $attemptsLeft $attemptsLeftNoun remaining." 790 $setClosed ? $setClosedMessage : "You have $attemptsLeft $attemptsLeftNoun remaining."
689 ); 791 );
690 print CGI::end_div(); 792 print CGI::end_div();
691 793
710 )) if defined($self->{problem}->{source_file}); 812 )) if defined($self->{problem}->{source_file});
711 813
712 # end of main form 814 # end of main form
713 print CGI::endform(); 815 print CGI::endform();
714 816
715
716 print CGI::start_div({class=>"problemFooter"}); 817 print CGI::start_div({class=>"problemFooter"});
717 818
718 # arguments for answer inspection button 819 ## arguments for answer inspection button
719 my $prof_url = $ce->{webworkURLs}->{oldProf}; 820 #my $prof_url = $ce->{webworkURLs}->{oldProf};
720 my $webworkURL = $ce->{webworkURLs}->{root}; 821 #my $webworkURL = $ce->{webworkURLs}->{root};
721 my $cgi_url = $prof_url; 822 #my $cgi_url = $prof_url;
722 $cgi_url=~ s|/[^/]*$||; # clip profLogin.pl 823 #$cgi_url=~ s|/[^/]*$||; # clip profLogin.pl
723 my $authen_args = $self->url_authen_args(); 824 #my $authen_args = $self->url_authen_args();
724 my $showPastAnswersURL = "$webworkURL/$courseName/instructor/show_answers/"; 825 #my $showPastAnswersURL = "$webworkURL/$courseName/instructor/show_answers/";
725 826
827 my $pastAnswersPage = $urlpath->newFromModule("WeBWorK::ContentGenerator::Instructor::ShowAnswers",
828 courseID => $courseName);
829 my $showPastAnswersURL = $self->systemLink($pastAnswersPage, authen => 0); # no authen info for form action
830
726 # print answer inspection button 831 # print answer inspection button
727 if ($self->{permissionLevel} > 0) { 832 if ($self->{permissionLevel} > 0) {
728 print "\n", 833 print "\n",
729 CGI::start_form(-method=>"POST",-action=>$showPastAnswersURL,-target=>"information"),"\n", 834 CGI::start_form(-method=>"POST",-action=>$showPastAnswersURL,-target=>"information"),"\n",
730 $self->hidden_authen_fields,"\n", 835 $self->hidden_authen_fields,"\n",
731 CGI::hidden(-name => 'course', -value=>$courseName), "\n", 836 CGI::hidden(-name => 'courseID', -value=>$courseName), "\n",
732 CGI::hidden(-name => 'problemNumber', -value=>$problem->problem_id), "\n", 837 CGI::hidden(-name => 'problemID', -value=>$problem->problem_id), "\n",
733 CGI::hidden(-name => 'setName', -value=>$problem->set_id), "\n", 838 CGI::hidden(-name => 'setID', -value=>$problem->set_id), "\n",
734 CGI::hidden(-name => 'studentUser', -value=>$problem->user_id), "\n", 839 CGI::hidden(-name => 'studentUser', -value=>$problem->user_id), "\n",
735 CGI::p( {-align=>"left"}, 840 CGI::p( {-align=>"left"},
736 CGI::submit(-name => 'action', -value=>'Show Past Answers') 841 CGI::submit(-name => 'action', -value=>'Show Past Answers')
737 ), "\n", 842 ), "\n",
738 CGI::endform(); 843 CGI::endform();
739 } 844 }
740 845
741 #print CGI::end_div(); 846 ## arguments for feedback form
847 #my $feedbackURL = "$root/$courseName/feedback/";
742 # 848 #
743 #print CGI::start_div(); 849 ##print feedback form
744 850 #print
745 # arguments for feedback form 851 # CGI::start_form(-method=>"POST", -action=>$feedbackURL),"\n",
746 my $feedbackURL = "$root/$courseName/feedback/"; 852 # $self->hidden_authen_fields,"\n",
853 # CGI::hidden("module", __PACKAGE__),"\n",
854 # CGI::hidden("set", $set->set_id),"\n",
855 # CGI::hidden("problem", $problem->problem_id),"\n",
856 # CGI::hidden("displayMode", $self->{displayMode}),"\n",
857 # CGI::hidden("showOldAnswers", $will{showOldAnswers}),"\n",
858 # CGI::hidden("showCorrectAnswers", $will{showCorrectAnswers}),"\n",
859 # CGI::hidden("showHints", $will{showHints}),"\n",
860 # CGI::hidden("showSolutions", $will{showSolutions}),"\n",
861 # CGI::p({-align=>"left"},
862 # CGI::submit(-name=>"feedbackForm", -label=>"Email instructor")
863 # ),
864 # CGI::endform(),"\n";
865
866 # feedback form url
867 my $feedbackPage = $urlpath->newFromModule("WeBWorK::ContentGenerator::Feedback",
868 courseID => $courseName);
869 my $feedbackURL = $self->systemLink($feedbackPage, authen => 0); # no authen info for form action
747 870
748 #print feedback form 871 #print feedback form
749 print 872 print
750 CGI::start_form(-method=>"POST", -action=>$feedbackURL),"\n", 873 CGI::start_form(-method=>"POST", -action=>$feedbackURL),"\n",
751 $self->hidden_authen_fields,"\n", 874 $self->hidden_authen_fields,"\n",
759 CGI::hidden("showSolutions", $will{showSolutions}),"\n", 882 CGI::hidden("showSolutions", $will{showSolutions}),"\n",
760 CGI::p({-align=>"left"}, 883 CGI::p({-align=>"left"},
761 CGI::submit(-name=>"feedbackForm", -label=>"Email instructor") 884 CGI::submit(-name=>"feedbackForm", -label=>"Email instructor")
762 ), 885 ),
763 CGI::endform(),"\n"; 886 CGI::endform(),"\n";
764 887
765 # FIXME print editor link 888 # FIXME print editor link
766 print $editorLinkMessage; #empty unless it is appropriate to have an editor link. 889 print $editorLink; #empty unless it is appropriate to have an editor link.
767 890
768 print CGI::end_div(); 891 print CGI::end_div();
769 892
770 # warning output 893 # warning output
771 #if ($pg->{warnings} ne "") { 894 #if ($pg->{warnings} ne "") {
792 return ""; 915 return "";
793} 916}
794 917
795##### output utilities ##### 918##### output utilities #####
796 919
797sub attemptResults($$$$$$) { 920sub attemptResults {
798 my $self = shift; 921 my $self = shift;
799 my $pg = shift; 922 my $pg = shift;
800 my $showAttemptAnswers = shift; 923 my $showAttemptAnswers = shift;
801 my $showCorrectAnswers = shift; 924 my $showCorrectAnswers = shift;
802 my $showAttemptResults = $showAttemptAnswers && shift; 925 my $showAttemptResults = $showAttemptAnswers && shift;
803 my $showSummary = shift; 926 my $showSummary = shift;
804 my $showAttemptPreview = shift || 0; 927 my $showAttemptPreview = shift || 0;
928
805 my $ce = $self->{ce}; 929 my $ce = $self->r->ce;
930
806 my $problemResult = $pg->{result}; # the overall result of the problem 931 my $problemResult = $pg->{result}; # the overall result of the problem
807 my @answerNames = @{ $pg->{flags}->{ANSWER_ENTRY_ORDER} }; 932 my @answerNames = @{ $pg->{flags}->{ANSWER_ENTRY_ORDER} };
808 933
809 my $showMessages = $showAttemptAnswers && grep { $pg->{answers}->{$_}->{ans_message} } @answerNames; 934 my $showMessages = $showAttemptAnswers && grep { $pg->{answers}->{$_}->{ans_message} } @answerNames;
810 935
823 #$header .= CGI::th("Part"); 948 #$header .= CGI::th("Part");
824 $header .= $showAttemptAnswers ? CGI::th("Entered") : ""; 949 $header .= $showAttemptAnswers ? CGI::th("Entered") : "";
825 $header .= $showAttemptPreview ? CGI::th("Answer Preview") : ""; 950 $header .= $showAttemptPreview ? CGI::th("Answer Preview") : "";
826 $header .= $showCorrectAnswers ? CGI::th("Correct") : ""; 951 $header .= $showCorrectAnswers ? CGI::th("Correct") : "";
827 $header .= $showAttemptResults ? CGI::th("Result") : ""; 952 $header .= $showAttemptResults ? CGI::th("Result") : "";
828 $header .= $showMessages ? CGI::th("messages") : ""; 953 $header .= $showMessages ? CGI::th("Messages") : "";
829 my @tableRows = ( $header ); 954 my @tableRows = ( $header );
830 my $numCorrect; 955 my $numCorrect;
831 foreach my $name (@answerNames) { 956 foreach my $name (@answerNames) {
832 my $answerResult = $pg->{answers}->{$name}; 957 my $answerResult = $pg->{answers}->{$name};
833 my $studentAnswer = $answerResult->{student_ans}; # original_student_ans 958 my $studentAnswer = $answerResult->{student_ans}; # original_student_ans
845 # of the answer names is changeable. this only fixes it for "AnSwEr" 970 # of the answer names is changeable. this only fixes it for "AnSwEr"
846 #$name =~ s/^AnSwEr//; 971 #$name =~ s/^AnSwEr//;
847 972
848 my $row; 973 my $row;
849 #$row .= CGI::td($name); 974 #$row .= CGI::td($name);
850 $row .= $showAttemptAnswers ? CGI::td(nbsp($studentAnswer)) : ""; 975 $row .= $showAttemptAnswers ? CGI::td($self->nbsp($studentAnswer)) : "";
851 $row .= $showAttemptPreview ? CGI::td(nbsp($preview)) : ""; 976 $row .= $showAttemptPreview ? CGI::td($self->nbsp($preview)) : "";
852 $row .= $showCorrectAnswers ? CGI::td(nbsp($correctAnswer)) : ""; 977 $row .= $showCorrectAnswers ? CGI::td($self->nbsp($correctAnswer)) : "";
853 $row .= $showAttemptResults ? CGI::td(nbsp($resultString)) : ""; 978 $row .= $showAttemptResults ? CGI::td($self->nbsp($resultString)) : "";
854 $row .= $showMessages ? CGI::td(nbsp($answerMessage)) : ""; 979 $row .= $showMessages ? CGI::td($self->nbsp($answerMessage)) : "";
855 push @tableRows, $row; 980 push @tableRows, $row;
856 } 981 }
857 982
858 # render equation images 983 # render equation images
859 $imgGen->render(refresh => 1); 984 $imgGen->render(refresh => 1);
864# my $summary = "On this attempt, you answered $numCorrect out of " 989# my $summary = "On this attempt, you answered $numCorrect out of "
865# . scalar @answerNames . " $numIncorrectNoun correct, for a score of $scorePercent."; 990# . scalar @answerNames . " $numIncorrectNoun correct, for a score of $scorePercent.";
866 my $summary = ""; 991 my $summary = "";
867 if (scalar @answerNames == 1) { 992 if (scalar @answerNames == 1) {
868 if ($numCorrect == scalar @answerNames) { 993 if ($numCorrect == scalar @answerNames) {
869 $summary .= "The above answer is correct."; 994 $summary .= CGI::div({class=>"ResultsWithoutError"},"The above answer is correct.");
870 } else { 995 } else {
871 $summary .= "The above answer is NOT correct."; 996 $summary .= CGI::div({class=>"ResultsWithError"},"The above answer is NOT correct.");
872 } 997 }
873 } else { 998 } else {
874 if ($numCorrect == scalar @answerNames) { 999 if ($numCorrect == scalar @answerNames) {
875 $summary .= "All of the above answers are correct."; 1000 $summary .= CGI::div({class=>"ResultsWithoutError"},"All of the above answers are correct.");
876 } else { 1001 } else {
877 $summary .= "At least one of the above answers is NOT correct."; 1002 $summary .= CGI::div({class=>"ResultsWithError"},"At least one of the above answers is NOT correct.");
878 } 1003 }
879 } 1004 }
880 #FIXME there must be a better way to force refresh. 1005
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 1006 return
887 CGI::table({-class=>"attemptResults"}, CGI::Tr(\@tableRows)) 1007 CGI::table({-class=>"attemptResults"}, CGI::Tr(\@tableRows))
888 . ($showSummary ? CGI::p({class=>'emphasis'},$summary) : ""); 1008 . ($showSummary ? CGI::p({class=>'emphasis'},$summary) : "");
889} 1009}
1010
890sub nbsp { 1011#sub nbsp {
891 my $str = shift; 1012# my $str = shift;
892 ($str =~/\S/) ? $str : '&nbsp;' ; # returns non-breaking space for empty strings 1013# ($str =~/\S/) ? $str : '&nbsp;' ; # returns non-breaking space for empty strings
893 # tricky cases: $str =0; 1014# # tricky cases: $str =0;
894 # $str is a complex number 1015# # $str is a complex number
895} 1016#}
1017
896sub viewOptions($) { 1018sub viewOptions {
897 my $self = shift; 1019 my ($self) = @_;
1020 my $ce = $self->r->ce;
1021
898 my $displayMode = $self->{displayMode}; 1022 my $displayMode = $self->{displayMode};
899 my %must = %{ $self->{must} }; 1023 my %must = %{ $self->{must} };
900 my %can = %{ $self->{can} }; 1024 my %can = %{ $self->{can} };
901 my %will = %{ $self->{will} }; 1025 my %will = %{ $self->{will} };
902 1026
909 -label => "Saved answers", 1033 -label => "Saved answers",
910 ), "&nbsp;&nbsp;".CGI::br(); 1034 ), "&nbsp;&nbsp;".CGI::br();
911 1035
912 $optionLine and $optionLine .= join "", CGI::br(); 1036 $optionLine and $optionLine .= join "", CGI::br();
913 1037
1038 my %display_modes = %{WeBWorK::PG::DISPLAY_MODES()};
1039 my @active_modes = grep { exists $display_modes{$_} }
1040 @{$ce->{pg}->{displayModes}};
1041
914 return CGI::div({-style=>"border: thin groove; padding: 1ex; margin: 2ex align: left"}, 1042 return CGI::div({-style=>"border: thin groove; padding: 1ex; margin: 2ex align: left"},
915 "View&nbsp;equations&nbsp;as:&nbsp;&nbsp;&nbsp;&nbsp;".CGI::br(), 1043 "View&nbsp;equations&nbsp;as:&nbsp;&nbsp;&nbsp;&nbsp;".CGI::br(),
916 CGI::radio_group( 1044 CGI::radio_group(
917 -name => "displayMode", 1045 -name => "displayMode",
918 -values => ['plainText', 'formattedText', 'images'], 1046 -values => \@active_modes,
919 -default => $displayMode, 1047 -default => $displayMode,
920 -linebreak=>'true', 1048 -linebreak=>'true',
921 -labels => { 1049 -labels => {
922 plainText => "plain", 1050 plainText => "plain",
923 formattedText => "formatted", 1051 formattedText => "formatted",
924 images => "images", 1052 images => "images",
1053 jsMath => "jsMath",
1054 asciimath => "asciimath",
925 } 1055 },
926 ), CGI::br(),CGI::hr(), 1056 ), CGI::br(),CGI::hr(),
927 $optionLine, 1057 $optionLine,
928 CGI::submit(-name=>"redisplay", -label=>"Save Options"), 1058 CGI::submit(-name=>"redisplay", -label=>"Save Options"),
929 ); 1059 );
930} 1060}
931 1061
932sub previewAnswer($$) { 1062sub previewAnswer {
933 my ($self, $answerResult, $imgGen) = @_; 1063 my ($self, $answerResult, $imgGen) = @_;
934 my $ce = $self->{ce}; 1064 my $ce = $self->r->ce;
935 my $effectiveUser = $self->{effectiveUser}; 1065 my $effectiveUser = $self->{effectiveUser};
936 my $set = $self->{set}; 1066 my $set = $self->{set};
937 my $problem = $self->{problem}; 1067 my $problem = $self->{problem};
938 my $displayMode = $self->{displayMode}; 1068 my $displayMode = $self->{displayMode};
939 1069
959 if ($?) { 1089 if ($?) {
960 return "<b>[tth failed: $? $@]</b>"; 1090 return "<b>[tth failed: $? $@]</b>";
961 } 1091 }
962 return $result; 1092 return $result;
963 } elsif ($displayMode eq "images") { 1093 } elsif ($displayMode eq "images") {
964 ## how are we going to name this? 1094 $imgGen->add($tex);
965 #my $targetPathCommon = "/m2i/" 1095 } elsif ($displayMode eq "jsMath") {
966 # . $effectiveUser->user_id . "." 1096
967 # . $set->set_id . "." 1097 return '<DIV CLASS="math">'.$tex.'</DIV>' ;
968 # . $problem->problem_id . "." 1098
969 # . $answerResult->{ans_name} . ".png"; 1099
970 # 1100
971 ## figure out where to put things 1101
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 } 1102 }
991} 1103}
992
993##### logging subroutine ####
994
995
996 1104
997##### permission queries ##### 1105##### permission queries #####
998 1106
999# this stuff should be abstracted out into the permissions system 1107# this stuff should be abstracted out into the permissions system
1000# however, the permission system only knows about things in the 1108# however, the permission system only knows about things in the
1001# course environment and the username. hmmm... 1109# course environment and the username. hmmm...
1002 1110
1003# also, i should fix these so that they have a consistent calling 1111# also, i should fix these so that they have a consistent calling
1004# format -- perhaps: 1112# format -- perhaps:
1005# canPERM($courseEnv, $user, $set, $problem, $permissionLevel) 1113# canPERM($ce, $user, $set, $problem, $permissionLevel)
1006 1114
1007sub canShowCorrectAnswers($$) { 1115sub canShowCorrectAnswers($$) {
1008 my ($permissionLevel, $answerDate) = @_; 1116 my ($permissionLevel, $answerDate) = @_;
1009 return $permissionLevel > 0 || time > $answerDate; 1117 return $permissionLevel > 0 || time > $answerDate;
1010} 1118}
1034sub mustRecordAnswers($) { 1142sub mustRecordAnswers($) {
1035 my ($permissionLevel) = @_; 1143 my ($permissionLevel) = @_;
1036 return $permissionLevel == 0; 1144 return $permissionLevel == 0;
1037} 1145}
1038 1146
1147
1148# FIXME: does this even get used?
1149sub submiterror {
1150 my $self = shift;
1151 my $submiterror = (defined($self->{submiterror}) ) ? $self->{submiterror} : '';
1152 $submiterror;
1153}
10391; 11541;

Legend:
Removed from v.1744  
changed lines
  Added in v.2213

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9