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