Parent Directory
|
Revision Log
turned off debugging in Problem.pm -sam
1 package WeBWorK::ContentGenerator::Problem; 2 use base qw(WeBWorK::ContentGenerator); 3 4 use strict; 5 use warnings; 6 use CGI qw(:html :form); 7 use WeBWorK::Utils qw(ref2string encodeAnswers decodeAnswers); 8 use WeBWorK::PG; 9 use WeBWorK::Form; 10 11 # user 12 # key 13 # 14 # displayMode 15 # showOldAnswers 16 # showCorrectAnswers 17 # showHints 18 # showSolutions 19 # 20 # AnSwEr# - answer blanks in problem 21 # 22 # redisplay - name of the "Redisplay Problem" button 23 # submitAnswers - name of "Submit Answers" button 24 25 sub title { 26 my ($self, $setName, $problemNumber) = @_; 27 my $userName = $self->{r}->param('user'); 28 return "Problem $problemNumber of problem set $setName for $userName"; 29 } 30 31 # TODO: 32 # :) enforce permissions for showCorrectAnswers and showSolutions 33 # (use $PRIV = $mustPRIV || ($canPRIV && $wantPRIV) -- cool syntax!) 34 # :) if answers were not submitted and there are student answers in the DB, 35 # decode them and put them into $formFields for the translator 36 # :) store submitted answers hash in database for sticky answers 37 # :) deal with the results of answer evaluation and grading :p 38 # :) introduce a recordAnswers option, which works on the same principle as 39 # the other permission-based options 40 # 7. make warnings work 41 42 sub body { 43 my ($self, $setName, $problemNumber) = @_; 44 my $courseEnv = $self->{courseEnvironment}; 45 my $r = $self->{r}; 46 my $userName = $r->param('user'); 47 48 # fix format of setName and problem 49 $setName =~ s/^set//; 50 $problemNumber =~ s/^prob//; 51 52 ##### database setup ##### 53 # this should probably go in initialize() or whatever it's called 54 55 my $classlist = WeBWorK::DB::Classlist->new($courseEnv); 56 my $wwdb = WeBWorK::DB::WW->new($courseEnv); 57 my $authdb = WeBWorK::DB::Auth->new($courseEnv); 58 59 my $user = $classlist->getUser($userName); 60 my $set = $wwdb->getSet($userName, $setName); 61 my $problem = $wwdb->getProblem($userName, $setName, $problemNumber); 62 my $permissionLevel = $authdb->getPermissions($userName); 63 64 ##### form processing ##### 65 66 # set options from form fields (see comment at top of file for names) 67 my $displayMode = $r->param("displayMode") || $courseEnv->{pg}->{options}->{displayMode}; 68 my $redisplay = $r->param("redisplay"); 69 my $submitAnswers = $r->param("submitAnswers"); 70 71 my %want = ( 72 showOldAnswers => $r->param("showOldAnswers") || $courseEnv->{pg}->{options}->{showOldAnswers}, 73 showCorrectAnswers => $r->param("showCorrectAnswers") || $courseEnv->{pg}->{options}->{showCorrectAnswers}, 74 showHints => $r->param("showHints") || $courseEnv->{pg}->{options}->{showHints}, 75 showSolutions => $r->param("showSolutions") || $courseEnv->{pg}->{options}->{showSolutions}, 76 recordAnswers => $r->param("recordAnswers") || 1, 77 ); 78 79 # coerce form fields into CGI::Vars format 80 my $formFields = { WeBWorK::Form->new_from_paramable($r)->Vars }; 81 82 ##### permissions ##### 83 84 # are certain options enforced? 85 my %must = ( 86 showOldAnswers => 0, 87 showCorrectAnswers => 0, 88 showHints => 0, 89 showSolutions => 0, 90 recordAnswers => mustRecordAnswers($permissionLevel), 91 ); 92 93 # does the user have permission to use certain options? 94 my %can = ( 95 showOldAnswers => 1, 96 showCorrectAnswers => canShowCorrectAnswers($permissionLevel, $set->answer_date), 97 showHints => 1, 98 showSolutions => canShowSolutions($permissionLevel, $set->answer_date), 99 recordAnswers => canRecordAnswers($permissionLevel, $set->open_date, $set->due_date, 100 $problem->max_attempts, $problem->num_correct + $problem->num_incorrect + 1), 101 # num_correct+num_incorrect+1 -- as this happens before updating $problem 102 ); 103 104 # final values for options 105 my %will; 106 foreach(keys %must) { 107 $will{$_} = $can{$_} && ($want{$_} || $must{$_}); 108 } 109 110 ##### sticky answers ##### 111 112 if (not $submitAnswers and $will{showOldAnswers}) { 113 # do this only if new answers are NOT being submitted 114 my %oldAnswers = decodeAnswers($problem->last_answer); 115 $formFields->{$_} = $oldAnswers{$_} foreach keys %oldAnswers; 116 } 117 118 ##### translation ##### 119 120 my $pg = WeBWorK::PG->new( 121 $courseEnv, 122 $r->param('user'), 123 $r->param('key'), 124 $setName, 125 $problemNumber, 126 { # translation options 127 displayMode => $displayMode, 128 showHints => $will{showHints}, 129 showSolutions => $will{showSolutions}, 130 refreshMath2img => $will{showHints} || $will{showSolutions}, 131 # try leaving processAnswers on all the time? 132 processAnswers => 1, #$submitAnswers ? 1 : 0, 133 }, 134 $formFields 135 ); 136 137 # handle any errors in translation 138 if ($pg->{flags}->{error_flag}) { 139 # there was an error in translation 140 print 141 h2("Software Error"), 142 translationError($pg->{errors}, $pg->{body_text}); 143 144 return ""; 145 } 146 147 ##### answer processing ##### 148 149 # if answers were submitted: 150 if ($submitAnswers) { 151 # store answers in DB for sticky answers 152 my %answersToStore; 153 my %answerHash = %{ $pg->{answers} }; 154 $answersToStore{$_} = $answerHash{$_}->{original_student_ans} 155 foreach (keys %answerHash); 156 my $answerString = encodeAnswers(%answersToStore, 157 @{ $pg->{flags}->{ANSWER_ENTRY_ORDER} }); 158 $problem->last_answer($answerString); 159 $wwdb->setProblem($problem); 160 161 # store score in DB if it makes sense 162 if ($will{recordAnswers}) { 163 # the grader makes a lot of decisions for us... 164 # all we have to do is update information from 165 # the 'state' hash in the $pg hash. 166 $problem->attempted(1); 167 $problem->status($pg->{state}->{recorded_score}); 168 $problem->num_correct($pg->{state}->{num_of_correct_ans}); 169 $problem->num_incorrect($pg->{state}->{num_of_incorrect_ans}); 170 #warn "Would have stored the following:\n", 171 # $problem->toString, "\n"; 172 $wwdb->setProblem($problem); 173 } else { 174 print p("Your score was not recorded for some reason. ;)"); 175 } 176 } 177 178 ##### output ##### 179 180 # attempt summary 181 if ($submitAnswers or $will{showCorrectAnswers}) { 182 # print this if user submitted answers OR requested correct answers 183 print attemptResults($pg, $submitAnswers, $will{showCorrectAnswers}, 184 $pg->{flags}->{showPartialCorrectAnswers}); 185 } 186 187 # score summary 188 my $attempts = $problem->num_correct + $problem->num_incorrect; 189 my $attemptsNoun = $attempts != 1 ? "times" : "time"; 190 my $lastScore = int ($problem->status * 100) . "%"; 191 my ($attemptsLeft, $attemptsLeftNoun); 192 if ($problem->max_attempts == -1) { 193 # unlimited attempts 194 $attemptsLeft = "unlimited"; 195 $attemptsLeftNoun = "attempts"; 196 } else { 197 $attemptsLeft = $problem->max_attempts - $attempts; 198 $attemptsLeftNoun = $attemptsLeft == 1 ? "attempt" : "attempts"; 199 } 200 print p( 201 "You have attempted this problem $attempts $attemptsNoun.", br(), 202 $problem->attempted 203 ? "Your recorded score is $lastScore." . br() 204 : "", 205 "You have $attemptsLeft $attemptsLeftNoun remaining." 206 ); 207 208 # BY THE WAY.......... 209 # we have to figure out some way to tell the student if their NEW answer, 210 # on THIS attempt, has been recorded. however, this is decided in part by 211 # the grader, so is there any way for us to know? we can rule out several 212 # cases where the answer is NOT being recorded, because of things decided 213 # in &canRecordAnswers... 214 215 print hr(); 216 217 # main form 218 print 219 startform("POST", $r->uri), 220 $self->hidden_authen_fields, 221 p(i($pg->{result}->{msg})), 222 p($pg->{body_text}), 223 p(submit(-name=>"submitAnswers", -label=>"Submit Answers")), 224 viewOptions($displayMode, \%must, \%can, \%will), 225 endform(); 226 227 # debugging stuff 228 #print 229 # hr(), 230 # h2("debugging information"), 231 # h3("form fields"), 232 # ref2string($formFields), 233 # h3("user object"), 234 # ref2string($user), 235 # h3("set object"), 236 # ref2string($set), 237 # h3("problem object"), 238 # ref2string($problem), 239 # h3("PG object"), 240 # ref2string($pg, {'WeBWorK::PG::Translator' => 1}); 241 242 return ""; 243 } 244 245 ##### output utilities ##### 246 247 sub translationError($$) { 248 my ($error, $details) = @_; 249 return 250 p(<<EOF), 251 WeBWorK has encountered a software error while attempting to process this problem. 252 It is likely that there is an error in the problem itself. 253 If you are a student, contact your professor to have the error corrected. 254 If you are a professor, please consut the error output below for more informaiton. 255 EOF 256 h3("Error messages"), blockquote(pre($error)), 257 h3("Error context"), blockquote(pre($details)); 258 } 259 260 sub attemptResults($$$) { 261 my $pg = shift; 262 my $showAttemptAnswers = shift; 263 my $showCorrectAnswers = shift; 264 my $showAttemptResults = $showAttemptAnswers && shift; 265 my $problemResult = $pg->{result}; # the overall result of the problem 266 my @answerNames = @{ $pg->{flags}->{ANSWER_ENTRY_ORDER} }; 267 268 my $header = th("answer"); 269 $header .= $showAttemptAnswers ? th("attempt") : ""; 270 $header .= $showCorrectAnswers ? th("correct") : ""; 271 $header .= $showAttemptResults ? th("result") : ""; 272 $header .= $showAttemptAnswers ? th("messages") : ""; 273 my @tableRows = ( $header ); 274 my $numCorrect; 275 foreach my $name (@answerNames) { 276 my $answerResult = $pg->{answers}->{$name}; 277 my $studentAnswer = $answerResult->{student_ans}; # original_student_ans 278 my $correctAnswer = $answerResult->{correct_ans}; 279 my $answerScore = $answerResult->{score}; 280 my $answerMessage = $showAttemptAnswers ? $answerResult->{ans_message} : ""; 281 282 $numCorrect += $answerScore > 0; 283 my $resultString = $answerScore ? "correct :^)" : "incorrect >:("; 284 285 my $row = td($name); 286 $row .= $showAttemptAnswers ? td($studentAnswer) : ""; 287 $row .= $showCorrectAnswers ? td($correctAnswer) : ""; 288 $row .= $showAttemptResults ? td($resultString) : ""; 289 $row .= $answerMessage ? td($answerMessage) : ""; 290 push @tableRows, $row; 291 } 292 293 my $numCorrectNoun = $numCorrect == 1 ? "question" : "questions"; 294 my $scorePercent = int ($problemResult->{score} * 100) . "\%"; 295 #my $message = i($problemResult->{msg}); 296 my $summary = "On this attempt, you answered $numCorrect $numCorrectNoun out of " 297 . scalar @answerNames . " correct, for a score of $scorePercent."; 298 #return table({-border=>1}, Tr(\@tableRows)) . p($message, br(), $summary); 299 return table({-border=>1}, Tr(\@tableRows)) . p($summary); 300 } 301 302 sub viewOptions($\%\%\%) { 303 my $displayMode = shift; 304 my %must = %{ shift() }; 305 my %can = %{ shift() }; 306 my %will = %{ shift() }; 307 308 my $optionLine; 309 $can{showOldAnswers} and $optionLine .= join "", 310 "Show: ", 311 checkbox( 312 -name => "showOldAnswers", 313 -checked => $will{showOldAnswers}, 314 -label => "Saved answers", 315 ), " "; 316 $can{showCorrectAnswers} and $optionLine .= join "", 317 checkbox( 318 -name => "showCorrectAnswers", 319 -checked => $will{showCorrectAnswers}, 320 -label => "Correct answers", 321 ), " "; 322 $can{showHints} and $optionLine .= join "", 323 checkbox( 324 -name => "showHints", 325 -checked => $will{showHints}, 326 -label => "Hints", 327 ), " "; 328 $can{showSolutions} and $optionLine .= join "", 329 checkbox( 330 -name => "showSolutions", 331 -checked => $will{showSolutions}, 332 -label => "Solutions", 333 ), " "; 334 $optionLine and $optionLine .= join "", br(); 335 336 return div({-style=>"border: thin groove; padding: 1ex; margin: 2ex"}, 337 "View equations as: ", 338 radio_group( 339 -name => "displayMode", 340 -values => ['plainText', 'formattedText', 'images'], 341 -default => $displayMode, 342 -labels => { 343 plainText => "plain text", 344 formattedText => "formatted text", 345 images => "images", 346 } 347 ), br(), 348 $optionLine, 349 submit(-name=>"redisplay", -label=>"Redisplay Problem"), 350 ); 351 } 352 353 ##### permission queries ##### 354 355 # this stuff should be abstracted out into the permissions system 356 # however, the permission system only knows about things in the 357 # course environment and the username. hmmm... 358 359 # also, i should fix these so that they have a consistent calling 360 # format -- perhaps: 361 # canPERM($courseEnv, $user, $set, $problem, $permissionLevel) 362 363 sub canShowCorrectAnswers($$) { 364 my ($permissionLevel, $answerDate) = @_; 365 return $permissionLevel > 0 || time > $answerDate; 366 } 367 368 sub canShowSolutions($$) { 369 my ($permissionLevel, $answerDate) = @_; 370 return canShowCorrectAnswers($permissionLevel, $answerDate); 371 } 372 373 sub canRecordAnswers($$$$$) { 374 my ($permissionLevel, $openDate, $dueDate, $maxAttempts, $attempts) = @_; 375 my $permHigh = $permissionLevel > 0; 376 my $timeOK = time >= $openDate && time <= $dueDate; 377 my $attemptsOK = $attempts <= $maxAttempts; 378 return $permHigh || ($timeOK && $attemptsOK); 379 } 380 381 sub mustRecordAnswers($) { 382 my ($permissionLevel) = @_; 383 return $permissionLevel == 0; 384 } 385 386 1;
| aubreyja at gmail dot com | ViewVC Help |
| Powered by ViewVC 1.0.9 |