From: Sam H. v. a. <we...@ma...> - 2008-04-29 19:44:13
|
Log Message: ----------- added fake_user, cleaned up formatting, expanded functionality of renderProblems Modified Files: -------------- webwork2/lib/WeBWorK/Utils: Tasks.pm Revision Data ------------- Index: Tasks.pm =================================================================== RCS file: /webwork/cvs/system/webwork2/lib/WeBWorK/Utils/Tasks.pm,v retrieving revision 1.15 retrieving revision 1.16 diff -Llib/WeBWorK/Utils/Tasks.pm -Llib/WeBWorK/Utils/Tasks.pm -u -r1.15 -r1.16 --- lib/WeBWorK/Utils/Tasks.pm +++ lib/WeBWorK/Utils/Tasks.pm @@ -18,27 +18,26 @@ =head1 NAME -WeBWorK::Utils::Tasks - utilities for doing single tasks, like - rendering a problem by itself. +WeBWorK::Utils::Tasks - utilities for doing single tasks, like rendering a +problem by itself. =head1 SYNOPSIS - use WeBWorK::Utils::Tasks qw/renderProblems/; - + use WeBWorK::Utils::Tasks qw(renderProblems); =head1 DESCRIPTION -This module provides functions for rendering html from files outside the -normal context of being for a particular user in an existing problem set. +This module provides functions for rendering html from files outside the normal +context of being for a particular user in an existing problem set. -It also provides functions which are useful for taking problems which are -not part of any set and making live versions of them, or loading them into -the editor. +It also provides functions which are useful for taking problems which are not +part of any set and making live versions of them, or loading them into the +editor. =cut -# Ultimately, this may provide functions for turning problems into hardcopy -# or other tasks that can be separated out of specific content managers. +# Ultimately, this may provide functions for turning problems into hardcopy or +# other tasks that can be separated out of specific content managers. use strict; use warnings; @@ -53,142 +52,238 @@ renderProblems fake_set fake_problem + fake_user ); use constant fakeSetName => "Undefined_Set"; +use constant fakeUserName => "Undefined_User"; =head1 FUNCTIONS =over -=item fake_set($db) +=item fake_set + + fake_set($db); Given a database, make a temporary problem set for that database. =cut sub fake_set { - my $db = shift; + my $db = shift; - my $set = $db->newGlobalSet(); - $set = global2user($db->{set_user}->{record}, $set); - $set->psvn(123); - $set->set_id(fakeSetName); - $set->open_date(time()); - $set->due_date(time()); - $set->answer_date(time()); - $set->published(0); - $set->hardcopy_header(""); - return($set); + my $set = $db->newGlobalSet(); + $set = global2user($db->{set_user}->{record}, $set); + $set->psvn(123); + $set->set_id(fakeSetName); + $set->open_date(time()); + $set->due_date(time()); + $set->answer_date(time()); + $set->published(0); + $set->hardcopy_header(""); + return($set); } -=item fake_problem($db) +=item fake_problem + + fake_problem($db); + fake_problem($db, problem_seed=>$seed); -Make a temporary problem for the given database. +Make a temporary problem for the given database. If a problem seed is not +specified, 0 is used. =cut sub fake_problem { - my $db = shift; - my %options = @_; - my $problem = $db->newGlobalProblem(); - $problem = global2user($db->{problem_user}->{record}, $problem); - $problem->set_id(fakeSetName); - $problem->value(""); - $problem->max_attempts("-1"); + my $db = shift; + my %options = @_; + my $problem = $db->newGlobalProblem(); + $problem = global2user($db->{problem_user}->{record}, $problem); + $problem->set_id(fakeSetName); + $problem->value(""); + $problem->max_attempts("-1"); - $problem->problem_seed(0); - $problem->problem_seed($options{'problem_seed'}) - if(defined($options{'problem_seed'})); - - $problem->status(0); - $problem->attempted(0); - $problem->last_answer(""); - $problem->num_correct(0); - $problem->num_incorrect(0); - return($problem); -} + $problem->problem_seed(0); + $problem->problem_seed($options{'problem_seed'}) + if(defined($options{'problem_seed'})); + + $problem->status(0); + $problem->attempted(0); + $problem->last_answer(""); + $problem->num_correct(0); + $problem->num_incorrect(0); + return($problem); +} + +=item fake_user -=item render_problems(r => $r, user => $user, problem_list => \@problem_list) + fake_user($db); -Given an Apache request object, the current user, and a list of problem -files, return a list of pg objects which contain rendered versions of -the problems. +Make a temporary user for the given database. =cut -sub renderProblems { - my %args = @_; - my $r = $args{r}; - my $user = $args{user}; - my @problem_list = @{$args{problem_list}}; - - my $db = $r->db; - my $ce = $r->ce; - my $key = $r->param('key'); - my $set = $args{'this_set'} || fake_set($db); - my $problem_seed = $args{'problem_seed'} || $r->param('problem_seed') || 0; - my $displayMode = $args{displayMode} || - $r->param("displayMode") || $ce->{pg}->{options}->{displayMode}; - my $showHints = $args{showHints} || 0; - my $showSolutions = $args{showSolutions} || 0; - my $problemNumber= $args{'problem_number'} || 1; - $ce->{pg}->{specialPGEnvironmentVars}->{problemPreamble} = { - TeX=>'', - HTML=>''}; - $ce->{pg}->{specialPGEnvironmentVars}->{problemPostamble} = { - TeX=>'', - HTML=>''}; - - my @output = (); - my $onefile; - if($displayMode eq 'None') { - for $onefile (@problem_list) { - my $res = { body_text=>''}; - #my $res = { body_text=>'Click "Try it" to see the problem rendered'}; - push @output, $res; - } - } else { - my $problem = fake_problem($db, 'problem_seed'=>$problem_seed); - my $formFields = { WeBWorK::Form->new_from_paramable($r)->Vars }; - - for $onefile (@problem_list) { - $problem->problem_id($problemNumber++); - $problem->source_file($onefile); - my $pg = WeBWorK::PG->new( - $ce, - $user, - $key, - $set, - $problem, - 123, # $set->psvn, # FIXME: this field should be\ removed - $formFields, - { # translation options - displayMode => $displayMode, - showHints => $showHints, - showSolutions => $showSolutions, - refreshMath2img => 0, - processAnswers => 0, - } - ); - - push @output, $pg; - } - } - return(@output); -} +sub fake_user { + my ($db) = @_; + return $db->newUser( + user_id => fakeUserName, + first_name=>'', + last_name=>'', + email_address=>'', + student_id=>'', + section=>'', + recitation=>'', + comment=>'', + ); +} + +=item render_problems + + render_problems(r => $r, user => $User, problem_list => \@problem_list); + +Given an Apache request object, the current user, and a list of problem files, +return a list of WeBWorK::PG objects which contain rendered versions of the +problems. + +Options: + +=over + +=item r + +A WeBWorK::Request object. Required. + +=item problem_list + +A reference to an array of items render. Required. Each item can either be a +string, which is interpreted as a path to a PG file, or a reference to a string, +which is interpreted as a complete PG program. + +=item user + +A User record (e.g. a WeBWorK::DB::Record::User object). Optional. If not +specified, fake_user() will be used to generate a temporary user record. + +=item this_set + +A Set record (e.g. a WeBWorK::DB::Record::UserSet object). Optional. If not +specified, fake_set() will be used to generate a temporary set record. + +=item problem_seed + +The seed to use for randomization. Optional. If not specified, the Request +object will be checked for a problem_seed parameter. If found, that value is +used. Otherwise, 0 is used. + +=item displayMode + +The display mode to use. Optional. If not specified, the Request object will be +checked for a displayMode parameter. If found, that value is used. Otherwise, +the default display mode is used. + +If the value is 'None', then problems will not be rendered and "fake" +WeBWorK::PG objects will be returned. Each "fake" WeBWorK::PG object will look +like: + + {body_text=>''} + +=item showHints + +Whether to show hints in the problem. Optional. If not specified, hints are not +shown. + +=item showSolutions + +Whether to show solutions in the problem. Optional. If not specified, solutions +are not shown. + +=item problemNumber + +Each problem in @problem_list is given a problem ID starting with this value. +Optional. If not specified the problems are numbered from 1. =back =cut -=head1 AUTHOR +sub renderProblems { + my %args = @_; + my $r = $args{r}; + my $db = $r->db; + my $ce = $r->ce; + + my @problem_list = @{$args{problem_list}}; + my $displayMode = $args{displayMode} + || $r->param('displayMode') + || $ce->{pg}{options}{displayMode}; + + # special case for display mode 'None' -- we don't have to do anything + # FIXME i think this should be handled in SetMaker.pm + if ($displayMode eq 'None') { + return map { {body_text=>''} } @problem_list; + } + + my $user = $args{user} || fake_user($db); + my $set = $args{'this_set'} || fake_set($db); + my $problem_seed = $args{'problem_seed'} || $r->param('problem_seed') || 0; + my $showHints = $args{showHints} || 0; + my $showSolutions = $args{showSolutions} || 0; + my $problemNumber = $args{'problem_number'} || 1; + + my $key = $r->param('key'); + + # remove any pretty garbage around the problem + local $ce->{pg}{specialPGEnvironmentVars}{problemPreamble} = {TeX=>'',HTML=>''}; + local $ce->{pg}{specialPGEnvironmentVars}{problemPostamble} = {TeX=>'',HTML=>''}; + my $problem = fake_problem($db, 'problem_seed'=>$problem_seed); + my $formFields = { WeBWorK::Form->new_from_paramable($r)->Vars }; + + my @output; + + foreach my $onefile (@problem_list) { + my $translationOptions = { + displayMode => $displayMode, + showHints => $showHints, + showSolutions => $showSolutions, + refreshMath2img => 0, + processAnswers => 0, + }; + + $problem->problem_id($problemNumber++); + if (ref $onefile) { + $problem->source_file(''); + $translationOptions->{r_source} = $onefile; + } else { + $problem->source_file($onefile); + } + + my $pg = new WeBWorK::PG( + $ce, + $user, + $key, + $set, + $problem, + 123, # PSVN (practically unused in PG) + $formFields, + $translationOptions, + ); -Written by John Jones, jj (at) asu.edu. + push @output, $pg; + } + + return @output; +} + +=back =cut +=head1 AUTHOR +Written by John Jones, jj (at) asu.edu. + +=cut 1; |