$pg = WeBWorK::PG->new(
displayMode => 'MathJax', # (images|MathJax)
showHints => 1,
showSolutions => 0,
processAnswers => 1,
isInstructor => 1,
useMathQuill => 1,
templateDirectory => '/opt/webwork/pg/',
problemSeed => 1234,
inputs_ref => $formFields,
sourceFilePath =>
'/opt/webwork/libraries/webwork-open-problem-library/OpenProblemLibrary/Michigan/Chap8Sec4/Q21.pg'
);
$translator = $pg->{translator}; # WeBWorK::PG::Translator
$body = $pg->{body_text}; # text string
$header = $pg->{head_text}; # text string
$post_header_text = $pg->{post_header_text}; # text string
$answerHash = $pg->{answers}; # WeBWorK::PG::AnswerHash
$result = $pg->{result}; # hash reference
$state = $pg->{state}; # hash reference
$errors = $pg->{errors}; # text string
$warnings = $pg->{warnings}; # text string
$flags = $pg->{flags}; # hash reference
$pgcore = $pg->{pgcore} # PGcore
WeBWorK::PG is a module that provides a convenient API for rendering a PG problem.
The constructor can be passed the following options. One of the r_source or sourceFilePath options described below must be provided. In addition, any of the translator environment variables may be passed as options in this argument. Furthermore, any keys in the passed hash not documented below will be copied into the translator environment.
The source of the pg problem to render provided as a reference to a string. If this is given it will be used for the problem source instead of the sourceFilePath
Location of the pg problem file to render. It must either be provided with an absoute path, or a path relative to the given templateDirectory.
Either a readable location containing the problem file and all static assets for the problem, or the empty string. WARNING: If this is the empty string then any static assets for the problem may not be found.
Seed to use for the problem.
The PG display mode to use, e.g., 'tex', 'plainText', 'images', 'MathJax', or 'PTX'.
This determines if hints that may be encoded in the problem will be rendered.
This determines if solutionas that may be encoded in the problem will be rendered.
If set to 1, then all scaffolds will be allowed to be opened.
Determines if the set containing the problem is open for the user to work. (This is only used by problemPanic.pl)
Determines if the problem is past due. (This is only used by answerDiscussion.pl, problemPanic.pl, and problemRandomize.pl)
Determines if the problem answers are available. (This is only used by scaffold.pl)
This is used to generate unique identifiers for resources. The caller should ensure that this is sufficiently unique for the purposes of the system. Generally that means it should be unique for each course, user, set, and problem number.
Problem set version number. This is also incorporated into the unique identifiers used for resources.
Problem number. This will eventually be removed from pg.
If 1, force images created by math2img (in "images" mode) to be recreated.
If 1, call answer evaluators and graders.
Determines if the user is an instructor (certain restrictions are removed for these users).
Hash containing all input values in the form containing the problem. Most importantly this contains answers.
This may contain the following keys (example values are shown)
functAbsTolDefault: 0.001
functLLimitDefault: 0.0000001
functMaxConstantOfIntegration: 1E8
functNumOfPoints: 3
functRelPercentTolDefault: 0.1
functULimitDefault: 0.9999999
functVarDefault: x
functZeroLevelDefault: 1E-14
functZeroLevelTolDefault: 1E-12
numAbsTolDefault: 0.001
numFormatDefault: ''
numRelPercentTolDefault: 0.1
numZeroLevelDefault: 1E-14
numZeroLevelTolDefault: 1E-12
useBaseTenLog: 0
defaultDisplayMatrixStyle: '[s]' # left delimiter, middle line delimiters, right delimiter
Determines if feedback will be shown for answers in the problem. Note that feedback will be shown if forceShowAttemptResults is true regardless of the value of this option.
Determines if the student's evaluated (i.e. "Entered") answers will be shown in feedback.
Determines if the student's answer previews will be shown in feedback.
Determines if attempt results will be revealed in feedback. In other words, if the student's answers are correct, incorrect, or partially correct. This honors the value of the PG showPartialCorrectAnswers
flag. If that flag is false, then attempt results will still not be shown.
If this is true, then a summary of results will also be generated. The summary will be returned in the summary
key of the result
hash.
If this is true then feedback will be shown with attempt results. This ignores the PG showPartialCorrectAnswers
flag and shows attempt results in any case. The summary will also be generated if this is true.
Determines if any messages generated in answer evaluation will be shown.
Determines if correct answers will be shown. If 0, then correct answers are not shown. If set to 1, then correct answers are shown but hidden, and a "Reveal" button is shown at first. If that button is clicked, then the answer is shown. If set to 2, then correct answers are shown immediately.
There is one special case that needs extra explanation. If this is true (greater than zero), forceShowAttemptResults
is true, forceScaffoldsOpen
is true, and showAttemptAnswers
, showAttemptPreviews
, and showMessages
are all false, then correct answers will be shown with no other content in the feedback popover except a close button, and the popover will open automatically on page load.
A prefix to prepend to all answer labels. Note that other prefixes may be prepended in front of this one, so it is not safe to assume that it is at the beginning of all answer labels. For example, the parserMultiAnswer.pl macro does this. Also note that in the actual PGcore environment this is QUIZ_PREFIX.
The default grader to use. This can be overridden by the problem.
Determines which entry assist method to use. If useMathQuill is 1, then MathQuill will be used. Otherwise, if useMathView is 1, then MathView will be used. If both are 0, then basic html inputs will be used.
The language for the problem.
Language subroutine that will be used for translations (the maketext method).
An array of paths to search for macros that may be loaded by the problem.
Paths to search for auxiliary html files. Note that this array may contain the special value of '.' which means the directory the problem file is contained in.
Paths to search for auxiliarly image files. Note that this array may contain the special value of '.' which means the directory the problem file is contained in.
Html directory that may contain additional static resources that may be used in problems. Usually the course's html directory.
Public html address of the htmlDirectory above.
Location to place generated resources. This directory must be writable.
Public html address of the tempDirectory above.
Public html address for the PG help files.
Site prefix prepended to image and video url's inserted into problems by the PGbasicmacros.pl methods "image" and "video".
If set to 1, then "%%IDPREFIX%%" will be prepended to answer labels.
A hash that should contain key value pairs for the keys 'root', 'pg', and 'tmpl'. These are used to shorten filenames in error messages.
A hash that can contain any of the keys described int conf/pg_config.dist.yml.
A hash that may contain key value pairs for the keys show_resource_info, view_problem_debugging_info, show_pg_info, show_answer_hash_info, show_answer_group_info. The keys enable the things describe in the key name.
These options are still used in some places in PG (mostly by macros that will eventually be deprecated and removed), but eventually that will all be fixed and these will no longer be needed. Note that webwork2 still needs to pass the courseName so the relevant course values (course html directories and urls) can be optained from the WeBWorK::CourseEnvironment.
The new
method returns a blessed hash reference containing the following fields. More information can be found in the documentation for WeBWorK::PG::Translator.
The WeBWorK::PG::Translator object used to render the problem.
HTML code to be injected into the <head> tag of the web page containing the problem.
HTML code to be injected into the <body> tag before the form containing the problem.
HTML code to be inserted into the <body> tag of the web page to show the problem.
An AnswerHash
object containing submitted answers, and results of answer evaluation.
A hash containing the results of grading the problem.
A hash containing the problem state.
A string containing any errors encountered while rendering the problem.
A string containing any warnings encountered while rendering the problem.
A hash containing PG_flags (see the Translator docs).
The PGcore object for the problem.