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

Diff of /branches/gage_dev/webwork2/lib/WeBWorK/ContentGenerator/Problem.pm

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

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

Legend:
Removed from v.1738  
changed lines
  Added in v.2414

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9