################################################################################ # WeBWorK mod_perl (c) 1995-2002 WeBWorK Team, Univeristy of Rochester # $Id$ ################################################################################ package WeBWorK::ContentGenerator::Problem; use strict; use warnings; use base qw(WeBWorK::ContentGenerator); use CGI qw(); use WeBWorK::Utils qw(ref2string encodeAnswers decodeAnswers); use WeBWorK::PG; use WeBWorK::Form; # TODO: # :) enforce permissions for showCorrectAnswers and showSolutions # (use $PRIV = $canPRIV && ($wantPRIV || $mustPRIV) -- cool syntax!) # :) if answers were not submitted and there are student answers in the DB, # decode them and put them into $formFields for the translator # :) store submitted answers hash in database for sticky answers # :) deal with the results of answer evaluation and grading :p # :) introduce a recordAnswers option, which works on the same principle as # the other permission-based options # 7. make warnings work ############################################################ # # user # key # # displayMode # showOldAnswers # showCorrectAnswers # showHints # showSolutions # # AnSwEr# - answer blanks in problem # # redisplay - name of the "Redisplay Problem" button # submitAnswers - name of "Submit Answers" button # ############################################################ sub initialize { my ($self, $setName, $problemNumber) = @_; my $courseEnv = $self->{courseEnvironment}; my $r = $self->{r}; my $userName = $r->param('user'); # fix format of setName and problem $setName =~ s/^set//; $problemNumber =~ s/^prob//; ##### database setup ##### my $cldb = WeBWorK::DB::Classlist->new($courseEnv); my $wwdb = WeBWorK::DB::WW->new($courseEnv); my $authdb = WeBWorK::DB::Auth->new($courseEnv); my $user = $cldb->getUser($userName); my $set = $wwdb->getSet($userName, $setName); my $problem = $wwdb->getProblem($userName, $setName, $problemNumber); my $permissionLevel = $authdb->getPermissions($userName); ##### form processing ##### # set options from form fields (see comment at top of file for names) my $displayMode = $r->param("displayMode") || $courseEnv->{pg}->{options}->{displayMode}; my $redisplay = $r->param("redisplay"); my $submitAnswers = $r->param("submitAnswers"); # coerce form fields into CGI::Vars format my $formFields = { WeBWorK::Form->new_from_paramable($r)->Vars }; ##### permissions ##### # what does the user want to do? my %want = ( showOldAnswers => $r->param("showOldAnswers") || $courseEnv->{pg}->{options}->{showOldAnswers}, showCorrectAnswers => $r->param("showCorrectAnswers") || $courseEnv->{pg}->{options}->{showCorrectAnswers}, showHints => $r->param("showHints") || $courseEnv->{pg}->{options}->{showHints}, showSolutions => $r->param("showSolutions") || $courseEnv->{pg}->{options}->{showSolutions}, recordAnswers => $r->param("recordAnswers") || 1, ); # are certain options enforced? my %must = ( showOldAnswers => 0, showCorrectAnswers => 0, showHints => 0, showSolutions => 0, recordAnswers => mustRecordAnswers($permissionLevel), ); # does the user have permission to use certain options? my %can = ( showOldAnswers => 1, showCorrectAnswers => canShowCorrectAnswers($permissionLevel, $set->answer_date), showHints => 1, showSolutions => canShowSolutions($permissionLevel, $set->answer_date), recordAnswers => canRecordAnswers($permissionLevel, $set->open_date, $set->due_date, $problem->max_attempts, $problem->num_correct + $problem->num_incorrect + 1), # num_correct+num_incorrect+1 -- as this happens before updating $problem ); # final values for options my %will; foreach(keys %must) { $will{$_} = $can{$_} && ($want{$_} || $must{$_}); } ##### sticky answers ##### if (not $submitAnswers and $will{showOldAnswers}) { # do this only if new answers are NOT being submitted my %oldAnswers = decodeAnswers($problem->last_answer); $formFields->{$_} = $oldAnswers{$_} foreach keys %oldAnswers; } ##### translation ##### my $pg = WeBWorK::PG->new( $courseEnv, $r->param('user'), $r->param('key'), $setName, $problemNumber, { # translation options displayMode => $displayMode, showHints => $will{showHints}, showSolutions => $will{showSolutions}, refreshMath2img => $will{showHints} || $will{showSolutions}, # try leaving processAnswers on all the time? processAnswers => 1, #$submitAnswers ? 1 : 0, }, $formFields ); ##### store fields ##### $self->{cldb} = $cldb; $self->{wwdb} = $wwdb; $self->{authdb} = $authdb; $self->{user} = $user; $self->{set} = $set; $self->{problem} = $problem; $self->{permissionLevel} = $permissionLevel; $self->{displayMode} = $displayMode; $self->{redisplay} = $redisplay; $self->{submitAnswers} = $submitAnswers; $self->{formFields} = $formFields; $self->{want} = \%want; $self->{must} = \%must; $self->{can} = \%can; $self->{will} = \%will; $self->{pg} = $pg; } sub title { my $self = shift; #return "Set " . $self->{set}->id . " problem " . $self->{problem}->id; return "hold on a sec"; } sub body { my $self = shift; #$self->prepare(@_); # unpack some useful variables my $r = $self->{r}; my $wwdb = $self->{wwdb}; my $set = $self->{set}; my $problem = $self->{problem}; my $submitAnswers = $self->{submitAnswers}; my %will = %{ $self->{will} }; my $pg = $self->{pg}; ##### translation errors? ##### if ($pg->{flags}->{error_flag}) { print translationError($pg->{errors}, $pg->{body_text}); return ""; } ##### answer processing ##### # if answers were submitted: if ($submitAnswers) { # store answers in DB for sticky answers my %answersToStore; my %answerHash = %{ $pg->{answers} }; $answersToStore{$_} = $answerHash{$_}->{original_student_ans} foreach (keys %answerHash); my $answerString = encodeAnswers(%answersToStore, @{ $pg->{flags}->{ANSWER_ENTRY_ORDER} }); $problem->last_answer($answerString); $wwdb->setProblem($problem); # store state in DB if it makes sense if ($will{recordAnswers}) { $problem->attempted(1); $problem->status($pg->{state}->{recorded_score}); $problem->num_correct($pg->{state}->{num_of_correct_ans}); $problem->num_incorrect($pg->{state}->{num_of_incorrect_ans}); #warn "Would have stored the following:\n", # $problem->toString, "\n"; $wwdb->setProblem($problem); } } ##### output ##### # attempt summary if ($submitAnswers or $will{showCorrectAnswers}) { # print this if user submitted answers OR requested correct answers print attemptResults($pg, $submitAnswers, $will{showCorrectAnswers}, $pg->{flags}->{showPartialCorrectAnswers}); } # score summary my $attempts = $problem->num_correct + $problem->num_incorrect; my $attemptsNoun = $attempts != 1 ? "times" : "time"; my $lastScore = int ($problem->status * 100) . "%"; my ($attemptsLeft, $attemptsLeftNoun); if ($problem->max_attempts == -1) { # unlimited attempts $attemptsLeft = "unlimited"; $attemptsLeftNoun = "attempts"; } else { $attemptsLeft = $problem->max_attempts - $attempts; $attemptsLeftNoun = $attemptsLeft == 1 ? "attempt" : "attempts"; } print CGI::p( "You have attempted this problem $attempts $attemptsNoun.", CGI::br(), $problem->attempted ? "Your recorded score is $lastScore." . CGI::br() : "", "You have $attemptsLeft $attemptsLeftNoun remaining." ); # BY THE WAY.......... # we have to figure out some way to tell the student if their NEW answer, # on THIS attempt, has been recorded. however, this is decided in part by # the grader, so is there any way for us to know? we can rule out several # cases where the answer is NOT being recorded, because of things decided # in &canRecordAnswers... print CGI::hr(); # main form print CGI::startform("POST", $r->uri), $self->hidden_authen_fields, CGI::p(CGI::i($pg->{result}->{msg})), CGI::p($pg->{body_text}), CGI::p(CGI::submit(-name=>"submitAnswers", -label=>"Submit Answers")), $self->viewOptions, CGI::endform(); # debugging stuff #print # hr(), # h2("debugging information"), # h3("form fields"), # ref2string($formFields), # h3("user object"), # ref2string($user), # h3("set object"), # ref2string($set), # h3("problem object"), # ref2string($problem), # h3("PG object"), # ref2string($pg, {'WeBWorK::PG::Translator' => 1}); return ""; } ##### output utilities ##### sub translationError($$) { my ($error, $details) = @_; return CGI::h2("Software Error"), CGI::p(<{result}; # the overall result of the problem my @answerNames = @{ $pg->{flags}->{ANSWER_ENTRY_ORDER} }; my $header = CGI::th("answer"); $header .= $showAttemptAnswers ? CGI::th("attempt") : ""; $header .= $showCorrectAnswers ? CGI::th("correct") : ""; $header .= $showAttemptResults ? CGI::th("result") : ""; $header .= $showAttemptAnswers ? CGI::th("messages") : ""; my @tableRows = ( $header ); my $numCorrect; foreach my $name (@answerNames) { my $answerResult = $pg->{answers}->{$name}; my $studentAnswer = $answerResult->{student_ans}; # original_student_ans my $correctAnswer = $answerResult->{correct_ans}; my $answerScore = $answerResult->{score}; my $answerMessage = $showAttemptAnswers ? $answerResult->{ans_message} : ""; $numCorrect += $answerScore > 0; my $resultString = $answerScore ? "correct :^)" : "incorrect >:("; my $row = CGI::td($name); $row .= $showAttemptAnswers ? CGI::td($studentAnswer) : ""; $row .= $showCorrectAnswers ? CGI::td($correctAnswer) : ""; $row .= $showAttemptResults ? CGI::td($resultString) : ""; $row .= $answerMessage ? CGI::td($answerMessage) : ""; push @tableRows, $row; } my $numCorrectNoun = $numCorrect == 1 ? "question" : "questions"; my $scorePercent = int ($problemResult->{score} * 100) . "\%"; my $summary = "On this attempt, you answered $numCorrect $numCorrectNoun out of " . scalar @answerNames . " correct, for a score of $scorePercent."; return CGI::table({-border=>1}, CGI::tr(\@tableRows)) . CGI::p($summary); } sub viewOptions($) { my $self = shift; my $displayMode = $self->{displayMode}; my %must = %{ $self->{must} }; my %can = %{ $self->{can} }; my %will = %{ $self->{will} }; my $optionLine; $can{showOldAnswers} and $optionLine .= join "", "Show:  ", CGI::checkbox( -name => "showOldAnswers", -checked => $will{showOldAnswers}, -label => "Saved answers", ), "  "; $can{showCorrectAnswers} and $optionLine .= join "", CGI::checkbox( -name => "showCorrectAnswers", -checked => $will{showCorrectAnswers}, -label => "Correct answers", ), "  "; $can{showHints} and $optionLine .= join "", CGI::checkbox( -name => "showHints", -checked => $will{showHints}, -label => "Hints", ), "  "; $can{showSolutions} and $optionLine .= join "", CGI::checkbox( -name => "showSolutions", -checked => $will{showSolutions}, -label => "Solutions", ), "  "; $optionLine and $optionLine .= join "", CGI::br(); return CGI::div({-style=>"border: thin groove; padding: 1ex; margin: 2ex"}, "View equations as:  ", CGI::radio_group( -name => "displayMode", -values => ['plainText', 'formattedText', 'images'], -default => $displayMode, -labels => { plainText => "plain text", formattedText => "formatted text", images => "images", } ), CGI::br(), $optionLine, CGI::submit(-name=>"redisplay", -label=>"Redisplay Problem"), ); } ##### permission queries ##### # this stuff should be abstracted out into the permissions system # however, the permission system only knows about things in the # course environment and the username. hmmm... # also, i should fix these so that they have a consistent calling # format -- perhaps: # canPERM($courseEnv, $user, $set, $problem, $permissionLevel) sub canShowCorrectAnswers($$) { my ($permissionLevel, $answerDate) = @_; return $permissionLevel > 0 || time > $answerDate; } sub canShowSolutions($$) { my ($permissionLevel, $answerDate) = @_; return canShowCorrectAnswers($permissionLevel, $answerDate); } sub canRecordAnswers($$$$$) { my ($permissionLevel, $openDate, $dueDate, $maxAttempts, $attempts) = @_; my $permHigh = $permissionLevel > 0; my $timeOK = time >= $openDate && time <= $dueDate; my $attemptsOK = $attempts <= $maxAttempts; return $permHigh || ($timeOK && $attemptsOK); } sub mustRecordAnswers($) { my ($permissionLevel) = @_; return $permissionLevel == 0; } 1;