|
|
1 | ################################################################################ |
|
|
2 | # WeBWorK Online Homework Delivery System |
|
|
3 | # Copyright © 2000-2003 The WeBWorK Project, http://openwebwork.sf.net/ |
|
|
4 | # $CVSHeader: webwork-modperl/lib/WeBWorK/PG.pm,v 1.46 2003/12/09 01:12:30 sh002i Exp $ |
|
|
5 | # |
|
|
6 | # This program is free software; you can redistribute it and/or modify it under |
|
|
7 | # the terms of either: (a) the GNU General Public License as published by the |
|
|
8 | # Free Software Foundation; either version 2, or (at your option) any later |
|
|
9 | # version, or (b) the "Artistic License" which comes with this package. |
|
|
10 | # |
|
|
11 | # This program is distributed in the hope that it will be useful, but WITHOUT |
|
|
12 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
|
13 | # FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the |
|
|
14 | # Artistic License for more details. |
|
|
15 | ################################################################################ |
|
|
16 | |
| 1 | package WeBWork::PG; |
17 | package WeBWorK::PG; |
| 2 | |
18 | |
| 3 | # hide PG::* from the not-yet-insane. |
19 | =head1 NAME |
|
|
20 | |
|
|
21 | WeBWorK::PG - Invoke one of several PG rendering methods using an easy-to-use |
|
|
22 | API. |
|
|
23 | |
|
|
24 | =cut |
| 4 | |
25 | |
| 5 | use strict; |
26 | use strict; |
| 6 | use warnings; |
27 | use warnings; |
| 7 | use WeBWorK::Utils qw(readFile formatDateTime); |
|
|
| 8 | use WeBWorK::DB::Classlist; |
|
|
| 9 | use WeBWorK::DB::WW; |
|
|
| 10 | use WeBWorK::PG::Translator; |
28 | use WeBWorK::PG::ImageGenerator; |
|
|
29 | use WeBWorK::Utils qw(runtime_use formatDateTime makeTempDirectory); |
| 11 | |
30 | |
| 12 | use base qw(Exporter); |
31 | sub new { |
| 13 | our @EXPORT = qw(init_translator); |
32 | shift; # throw away invocant -- we don't need it |
| 14 | our @EXPORT_OK = qw(); |
33 | my ($ce, $user, $key, $set, $problem, $psvn, $formFields, |
| 15 | |
34 | $translationOptions) = @_; |
| 16 | sub init_translator($$$$$) { |
|
|
| 17 | my $courseEnv = shift; |
|
|
| 18 | my $userName = shift; |
|
|
| 19 | my $setName = shift; |
|
|
| 20 | my $problemNumber = shift; |
|
|
| 21 | my $formData = shift; |
|
|
| 22 | |
35 | |
| 23 | # get database information |
36 | my $renderer = $ce->{pg}->{renderer}; |
| 24 | my $classlist = WeBWorK::DB::Classlist->new($courseEnv); |
|
|
| 25 | my $wwdb = WeBWorK::DB::WW->new($courseEnv); |
|
|
| 26 | my $user = $classlist->getUser($userName); |
|
|
| 27 | my $set = $wwdb->getSet($userName, $setName); |
|
|
| 28 | my $problem = $wwdb->getProblem($userName, $setName, $problemNumber); |
|
|
| 29 | my $psvn = $wwdb->getPSVN($userName, $setName); |
|
|
| 30 | |
37 | |
| 31 | # create a Translator |
38 | runtime_use $renderer; |
| 32 | my $translator = WeBWorK::PG::Translator->new; |
|
|
| 33 | |
39 | |
| 34 | # give it a directory hash |
40 | return $renderer->new(@_); |
| 35 | $translator->rh_directories({ |
|
|
| 36 | courseScriptsDirectory => $courseEnv->{webworkDirs}->{macros}, |
|
|
| 37 | macroDirectory => $courseEnv->{courseDirs}->{macros}, |
|
|
| 38 | templateDirectory => $courseEnv->{courseDirs}->{templates}, |
|
|
| 39 | tempDirectory => $courseEnv->{courseDirs}->{html_temp}, |
|
|
| 40 | }); |
|
|
| 41 | |
|
|
| 42 | # give it modules to evaluate |
|
|
| 43 | # give it "extra packages" to load |
|
|
| 44 | my $modules = $courseEnv->{pg}->{modules}; |
|
|
| 45 | foreach $module (keys %$modules) { |
|
|
| 46 | my $main_package_loaded = 0; |
|
|
| 47 | foreach $package (@{$modules->{$module}}) { |
|
|
| 48 | if ($package eq $module) { |
|
|
| 49 | # this is the main package |
|
|
| 50 | $translator->evaluate_modules($package); |
|
|
| 51 | $main_package_loaded = 1; |
|
|
| 52 | } else { |
|
|
| 53 | # this is an "extra" package |
|
|
| 54 | if ($main_package_loaded) { |
|
|
| 55 | $translator->load_extra_packages($package); |
|
|
| 56 | } else { |
|
|
| 57 | warn "Can't load extra package $package: module $module hasn't been evaluated."; |
|
|
| 58 | } |
|
|
| 59 | |
|
|
| 60 | } |
|
|
| 61 | } |
|
|
| 62 | } |
|
|
| 63 | |
|
|
| 64 | # give it an environment (from defineProblemEnvir) |
|
|
| 65 | $translator->environment( |
|
|
| 66 | defineProblemEnvir($courseEnv, $user, $set, $problem, $psvn, $formData) |
|
|
| 67 | ); |
|
|
| 68 | |
|
|
| 69 | # initialize it |
|
|
| 70 | $translator->initialize(); |
|
|
| 71 | |
|
|
| 72 | # have it "unrestricted load" PG.pl and dangerousMacros.pl |
|
|
| 73 | my $pg_pl = $courseEnv->{webworkDirs}->{macros} . "/PG.pl"; |
|
|
| 74 | my $dangerousMacros_pl = $courseEnv->{webworkDirs}->{macros} . "/dangerousMacros.pl" |
|
|
| 75 | my $err = $translator->unrestricted_load($pg_pl); |
|
|
| 76 | warn "Error while loading $pg_pl: $err" if $err; |
|
|
| 77 | $err = $translator->unrestricted_load($dangerousMacros_pl); |
|
|
| 78 | warn "Error while loading $dangerousMacros_pl: $err" if $err; |
|
|
| 79 | |
|
|
| 80 | # give it an opcode mask (using default values) |
|
|
| 81 | $translator->set_mask(); |
|
|
| 82 | |
|
|
| 83 | # give it the problem source |
|
|
| 84 | my $sourceFile = $courseEnv->{courseDirs}->{templates}."/".$problem->source_file; |
|
|
| 85 | $translator->source_string(readFile($sourceFile)); |
|
|
| 86 | |
|
|
| 87 | # install a safety filter (&safetyFilter) |
|
|
| 88 | $translator->rf_safety_filter(\&safetyFilter); |
|
|
| 89 | |
|
|
| 90 | # return the translator |
|
|
| 91 | return $translator; |
|
|
| 92 | } |
41 | } |
| 93 | |
42 | |
| 94 | # ----- |
|
|
| 95 | |
|
|
| 96 | sub defineProblemEnvir($$$$$$) { |
43 | sub defineProblemEnvir { |
| 97 | my $courseEnv = shift; |
44 | my ( |
| 98 | my $user = shift; |
45 | $self, |
| 99 | my $set = shift; |
46 | $ce, |
| 100 | my $problem = shift; |
47 | $user, |
| 101 | my $psvn = shift; |
48 | $key, |
| 102 | my $form = shift; |
49 | $set, |
|
|
50 | $problem, |
|
|
51 | $psvn, |
|
|
52 | $formFields, |
|
|
53 | $options, |
|
|
54 | ) = @_; |
| 103 | |
55 | |
| 104 | my %envir; |
56 | my %envir; |
|
|
57 | |
|
|
58 | # ---------------------------------------------------------------------- |
| 105 | |
59 | |
| 106 | # PG environment variables |
60 | # PG environment variables |
| 107 | # from docs/pglanguage/pgreference/environmentvariables as of 06/25/2002 |
61 | # from docs/pglanguage/pgreference/environmentvariables as of 06/25/2002 |
| 108 | # any changes are noted by "ADDED:" or "REMOVED:" |
62 | # any changes are noted by "ADDED:" or "REMOVED:" |
| 109 | |
63 | |
| 110 | # Vital state information |
64 | # Vital state information |
| 111 | # ADDED: displayHintsQ, displaySolutionsQ, externalTTHPath |
65 | # ADDED: displayHintsQ, displaySolutionsQ, refreshMath2img, |
|
|
66 | # texDisposition |
| 112 | |
67 | |
| 113 | $envir{psvn} = $psvn; |
68 | $envir{psvn} = $set->psvn; |
| 114 | $envir{psvnNumber} = $envir{psvn}; |
69 | $envir{psvnNumber} = $envir{psvn}; |
| 115 | $envir{probNum} = $problem->id; |
70 | $envir{probNum} = $problem->problem_id; |
| 116 | $envir{questionNumber} = $envir{probNum}; |
71 | $envir{questionNumber} = $envir{probNum}; |
| 117 | $envir{fileName} = $problem->source_file; |
72 | $envir{fileName} = $problem->source_file; |
| 118 | $envir{probFileName} = $envir{fileName}; |
73 | $envir{probFileName} = $envir{fileName}; |
| 119 | $envir{problemSeed} = $problem->problem_seed; |
74 | $envir{problemSeed} = $problem->problem_seed; |
| 120 | $envir{displayMode} = $form->param('Mode'); |
75 | $envir{displayMode} = translateDisplayModeNames($options->{displayMode}); |
| 121 | $envir{languageMode} = $envir{displayMode}; |
76 | $envir{languageMode} = $envir{displayMode}; |
| 122 | $envir{outputMode} = $envir{displayMode}; |
77 | $envir{outputMode} = $envir{displayMode}; |
| 123 | $envir{displayHintsQ} = $form->param('ShowHint'); |
78 | $envir{displayHintsQ} = $options->{showHints}; |
| 124 | $envir{displaySolutionsQ} = $form->param('ShowSol'); |
79 | $envir{displaySolutionsQ} = $options->{showSolutions}; |
| 125 | $envir{externalTTHPath} = $courseEnv->{externalPrograms}->{tth}; |
80 | $envir{texDisposition} = "pdf"; # in webwork2, we use pdflatex |
| 126 | |
81 | |
| 127 | # Problem Information |
82 | # Problem Information |
| 128 | # ADDED: courseName |
83 | # ADDED: courseName, formatedDueDate |
| 129 | |
84 | |
| 130 | $envir{openDate} = $set->open_date; |
85 | $envir{openDate} = $set->open_date; |
| 131 | $envir{formattedOpenDate} = formatDateTime $envir{openDate}; |
86 | $envir{formattedOpenDate} = formatDateTime($envir{openDate}); |
| 132 | $envir{dueDate} = $set->due_date; |
87 | $envir{dueDate} = $set->due_date; |
| 133 | $envir{formattedDueDate} = formatDateTime $envir{dueDate}; |
88 | $envir{formattedDueDate} = formatDateTime($envir{dueDate}); |
|
|
89 | $envir{formatedDueDate} = $envir{formattedDueDate}; # typo in many header files |
| 134 | $envir{answerDate} = $set->answer_date; |
90 | $envir{answerDate} = $set->answer_date; |
| 135 | $envir{formattedAnswerDate} = formatDateTime $envir{answerDate}; |
91 | $envir{formattedAnswerDate} = formatDateTime($envir{answerDate}); |
| 136 | $envir{numOfAttempts} = $problem->num_correct + $problem->num_incorrect; |
92 | $envir{numOfAttempts} = ($problem->num_correct || 0) + ($problem->num_incorrect || 0); |
| 137 | $envir{problemValue} = $problem->value; |
93 | $envir{problemValue} = $problem->value; |
| 138 | $envir{sessionKey} = $form->param('key'); |
94 | $envir{sessionKey} = $key; |
| 139 | $envir{courseName} = $courseEnv->{courseName}; |
95 | $envir{courseName} = $ce->{courseName}; |
| 140 | |
96 | |
| 141 | # Student Information |
97 | # Student Information |
| 142 | # ADDED: studentID |
98 | # ADDED: studentID |
| 143 | |
99 | |
| 144 | $envir{sectionName} = $user->section; |
100 | $envir{sectionName} = $user->section; |
| 145 | $envir{sectionNumber} = $envir{sectionName}; |
101 | $envir{sectionNumber} = $envir{sectionName}; |
| 146 | $envir{recitationName} = $user->recitation; |
102 | $envir{recitationName} = $user->recitation; |
| 147 | $envir{recitationNumber} = $envir{recitationName}; |
103 | $envir{recitationNumber} = $envir{recitationName}; |
| 148 | $envir{setNumber} = $set->id; |
104 | $envir{setNumber} = $set->set_id; |
| 149 | $envir{studentLogin} = $user->id; |
105 | $envir{studentLogin} = $user->user_id; |
| 150 | $envir{studentName} = $user->first_name . " " . $user->last_name; |
106 | $envir{studentName} = $user->first_name . " " . $user->last_name; |
| 151 | $envir{studentID} = $user->student_id |
107 | $envir{studentID} = $user->student_id; |
| 152 | |
108 | |
| 153 | # Answer Information |
109 | # Answer Information |
|
|
110 | # REMOVED: refSubmittedAnswers |
| 154 | |
111 | |
| 155 | $envir{inputs_ref} = {}; # *** keys like "Answer1" |
112 | $envir{inputs_ref} = $formFields; |
| 156 | $envir{refSubmittedAnswers} = {}; # *** keys like "AnSwEr1" |
|
|
| 157 | |
113 | |
| 158 | # Default values for evaluating answers |
114 | # External Programs |
|
|
115 | # ADDED: externalLaTeXPath, externalDvipngPath, |
|
|
116 | # externalGif2EpsPath, externalPng2EpsPath |
| 159 | |
117 | |
| 160 | my $ansEvalDefaults = $courseEnv->{pg}->{ansEvalDefaults}; |
118 | $envir{externalTTHPath} = $ce->{externalPrograms}->{tth}; |
| 161 | $envir{$_} = $ansEvalDefaults->{$_} foreach (keys %$ansEvalDefaults); |
119 | $envir{externalLaTeXPath} = $ce->{externalPrograms}->{latex}; |
|
|
120 | $envir{externalDvipngPath} = $ce->{externalPrograms}->{dvipng}; |
|
|
121 | $envir{externalGif2EpsPath} = $ce->{externalPrograms}->{gif2eps}; |
|
|
122 | $envir{externalPng2EpsPath} = $ce->{externalPrograms}->{png2eps}; |
|
|
123 | $envir{externalGif2PngPath} = $ce->{externalPrograms}->{gif2png}; |
| 162 | |
124 | |
| 163 | # Directories and URLs |
125 | # Directories and URLs |
| 164 | # REMOVED: courseName |
126 | # REMOVED: courseName |
|
|
127 | # ADDED: dvipngTempDir |
| 165 | |
128 | |
| 166 | $envir{cgiDirectory} = undef; |
129 | $envir{cgiDirectory} = undef; |
| 167 | $envir{cgiURL} = undef; |
130 | $envir{cgiURL} = undef; |
| 168 | $envir{classDirectory} = undef; |
131 | $envir{classDirectory} = undef; |
| 169 | $envir{courseScriptsDirectory} = $courseEnv->{webworkDirs}->{macros}; |
132 | $envir{courseScriptsDirectory} = $ce->{pg}->{directories}->{macros}."/"; |
| 170 | $envir{htmlDirectory} = $courseEnv->{courseDirs}->{html}; |
133 | $envir{htmlDirectory} = $ce->{courseDirs}->{html}."/"; |
| 171 | $envir{htmlURL} = $courseEnv->{courseURLs}->{html}; |
134 | $envir{htmlURL} = $ce->{courseURLs}->{html}."/"; |
| 172 | $envir{macroDirectory} = $courseEnv->{courseDirs}->{macros}; |
135 | $envir{macroDirectory} = $ce->{courseDirs}->{macros}."/"; |
| 173 | $envir{templateDirectory} = $courseEnv->{courseDirs}->{templates}; |
136 | $envir{templateDirectory} = $ce->{courseDirs}->{templates}."/"; |
| 174 | $envir{tempDirectory} = $courseEnv->{courseDirs}->{html_temp}; |
137 | $envir{tempDirectory} = $ce->{courseDirs}->{html_temp}."/"; |
| 175 | $envir{tempURL} = $courseEnv->{courseURLs}->{html_temp}; |
138 | $envir{tempURL} = $ce->{courseURLs}->{html_temp}."/"; |
| 176 | $envir{scriptDirectory} = undef; |
139 | $envir{scriptDirectory} = undef; |
| 177 | $envir{webworkDocsURL} = $courseEnv->{webworkURLs}->{docs}; |
140 | $envir{webworkDocsURL} = $ce->{webworkURLs}->{docs}."/"; |
|
|
141 | |
|
|
142 | # Information for sending mail |
|
|
143 | |
|
|
144 | $envir{mailSmtpServer} = $ce->{mail}->{smtpServer}; |
|
|
145 | $envir{mailSmtpSender} = $ce->{mail}->{smtpSender}; |
|
|
146 | $envir{ALLOW_MAIL_TO} = $ce->{mail}->{allowedRecipients}; |
|
|
147 | |
|
|
148 | # Default values for evaluating answers |
|
|
149 | |
|
|
150 | my $ansEvalDefaults = $ce->{pg}->{ansEvalDefaults}; |
|
|
151 | $envir{$_} = $ansEvalDefaults->{$_} foreach (keys %$ansEvalDefaults); |
|
|
152 | |
|
|
153 | # ---------------------------------------------------------------------- |
|
|
154 | |
|
|
155 | my $basename = "equation-$envir{psvn}.$envir{probNum}"; |
|
|
156 | $basename .= ".$envir{problemSeed}" if $envir{problemSeed}; |
|
|
157 | |
|
|
158 | # Object for generating equation images |
|
|
159 | $envir{imagegen} = WeBWorK::PG::ImageGenerator->new( |
|
|
160 | tempDir => $ce->{webworkDirs}->{tmp}, # global temp dir |
|
|
161 | latex => $envir{externalLaTeXPath}, |
|
|
162 | dvipng => $envir{externalDvipngPath}, |
|
|
163 | useCache => 1, |
|
|
164 | cacheDir => $ce->{webworkDirs}->{equationCache}, |
|
|
165 | cacheURL => $ce->{webworkURLs}->{equationCache}, |
|
|
166 | cacheDB => $ce->{webworkFiles}->{equationCacheDB}, |
|
|
167 | ); |
|
|
168 | |
|
|
169 | # Other things... |
|
|
170 | $envir{QUIZ_PREFIX} = $options->{QUIZ_PREFIX}; # used by quizzes |
|
|
171 | $envir{PROBLEM_GRADER_TO_USE} = $ce->{pg}->{options}->{grader}; |
|
|
172 | $envir{PRINT_FILE_NAMES_FOR} = $ce->{pg}->{specialPGEnvironmentVars}->{PRINT_FILE_NAMES_FOR}; |
|
|
173 | |
|
|
174 | # variables for interpreting capa problems. |
|
|
175 | $envir{CAPA_Tools} = $ce->{pg}->{specialPGEnvironmentVars}->{CAPA_Tools}; |
|
|
176 | $envir{CAPA_MCTools} = $ce->{pg}->{specialPGEnvironmentVars}->{CAPA_MCTools}; |
|
|
177 | $envir{CAPA_Graphics_URL} = $ce->{pg}->{specialPGEnvironmentVars}->{CAPA_Graphics_URL}; |
|
|
178 | $envir{CAPA_GraphicsDirectory} = $ce->{pg}->{specialPGEnvironmentVars}->{CAPA_GraphicsDirectory}; |
| 178 | |
179 | |
| 179 | return \%envir; |
180 | return \%envir; |
| 180 | } |
181 | } |
| 181 | |
182 | |
|
|
183 | sub translateDisplayModeNames($) { |
|
|
184 | my $name = shift; |
|
|
185 | return { |
|
|
186 | tex => "TeX", |
|
|
187 | plainText => "HTML", |
|
|
188 | formattedText => "HTML_tth", |
|
|
189 | images => "HTML_dpng", # "HTML_img", |
|
|
190 | }->{$name}; |
|
|
191 | } |
|
|
192 | |
| 182 | sub safetyFilter { |
193 | sub oldSafetyFilter { |
| 183 | my $answer = shift; # accepts one answer and checks it |
194 | my $answer = shift; # accepts one answer and checks it |
| 184 | my $submittedAnswer = $answer; |
195 | my $submittedAnswer = $answer; |
| 185 | $answer = '' unless defined $answer; |
196 | $answer = '' unless defined $answer; |
| 186 | my ($errorno); |
197 | my ($errorno); |
| 187 | $answer =~ tr/\000-\037/ /; |
198 | $answer =~ tr/\000-\037/ /; |
| 188 | # Return if answer field is empty |
199 | # Return if answer field is empty |
| … | |
… | |
| 191 | $errorno = 0; ## don't report blank answer as error |
202 | $errorno = 0; ## don't report blank answer as error |
| 192 | return ($answer,$errorno); |
203 | return ($answer,$errorno); |
| 193 | } |
204 | } |
| 194 | # replace ^ with ** (for exponentiation) |
205 | # replace ^ with ** (for exponentiation) |
| 195 | # $answer =~ s/\^/**/g; |
206 | # $answer =~ s/\^/**/g; |
| 196 | # Return if forbidden characters are found |
207 | # Return if forbidden characters are found |
| 197 | unless ($answer =~ /^[a-zA-Z0-9_\-\+ \t\/@%\*\.\n^\(\)]+$/ ) { |
208 | unless ($answer =~ /^[a-zA-Z0-9_\-\+ \t\/@%\*\.\n^\[\]\(\)\,\|]+$/ ) { |
| 198 | $answer =~ tr/a-zA-Z0-9_\-\+ \t\/@%\*\.\n^\(\)/#/c; |
209 | $answer =~ tr/a-zA-Z0-9_\-\+ \t\/@%\*\.\n^\(\)/#/c; |
| 199 | $errorno = "<BR>There are forbidden characters in your answer: $submittedAnswer<BR>"; |
210 | $errorno = "<BR>There are forbidden characters in your answer: $submittedAnswer<BR>"; |
| 200 | return ($answer,$errorno); |
211 | return ($answer,$errorno); |
| 201 | } |
212 | } |
| 202 | $errorno = 0; |
213 | $errorno = 0; |
| 203 | return($answer, $errorno); |
214 | return($answer, $errorno); |
| 204 | } |
215 | } |
| 205 | |
216 | |
|
|
217 | sub nullSafetyFilter { |
|
|
218 | return shift, 0; # no errors |
|
|
219 | } |
|
|
220 | |
| 206 | 1; |
221 | 1; |
|
|
222 | |
|
|
223 | __END__ |
|
|
224 | |
|
|
225 | =head1 SYNOPSIS |
|
|
226 | |
|
|
227 | $pg = WeBWorK::PG->new( |
|
|
228 | $ce, # a WeBWorK::CourseEnvironment object |
|
|
229 | $user, # a WeBWorK::DB::Record::User object |
|
|
230 | $sessionKey, |
|
|
231 | $set, # a WeBWorK::DB::Record::UserSet object |
|
|
232 | $problem, # a WeBWorK::DB::Record::UserProblem object |
|
|
233 | $psvn, |
|
|
234 | $formFields # in &WeBWorK::Form::Vars format |
|
|
235 | { # translation options |
|
|
236 | displayMode => "images", # (plainText|formattedText|images) |
|
|
237 | showHints => 1, # (0|1) |
|
|
238 | showSolutions => 0, # (0|1) |
|
|
239 | refreshMath2img => 0, # (0|1) |
|
|
240 | processAnswers => 1, # (0|1) |
|
|
241 | }, |
|
|
242 | ); |
|
|
243 | |
|
|
244 | $translator = $pg->{translator}; # WeBWorK::PG::Translator |
|
|
245 | $body = $pg->{body_text}; # text string |
|
|
246 | $header = $pg->{head_text}; # text string |
|
|
247 | $answerHash = $pg->{answers}; # WeBWorK::PG::AnswerHash |
|
|
248 | $result = $pg->{result}; # hash reference |
|
|
249 | $state = $pg->{state}; # hash reference |
|
|
250 | $errors = $pg->{errors}; # text string |
|
|
251 | $warnings = $pg->{warnings}; # text string |
|
|
252 | $flags = $pg->{flags}; # hash reference |
|
|
253 | |
|
|
254 | =head1 DESCRIPTION |
|
|
255 | |
|
|
256 | WeBWorK::PG is a factory for modules which use the WeBWorK::PG API. Notable |
|
|
257 | modules which use this API (and exist) are WeBWorK::PG::Local and |
|
|
258 | WeBWorK::PG::Remote. The course environment key $pg{renderer} is consulted to |
|
|
259 | determine which render to use. |
|
|
260 | |
|
|
261 | =head1 THE WEBWORK::PG API |
|
|
262 | |
|
|
263 | Modules which support this API must implement the following method: |
|
|
264 | |
|
|
265 | =over |
|
|
266 | |
|
|
267 | =item new ENVIRONMENT, USER, KEY, SET, PROBLEM, PSVN, FIELDS, OPTIONS |
|
|
268 | |
|
|
269 | The C<new> method creates a translator, initializes it using the parameters |
|
|
270 | specified, translates a PG file, and processes answers. It returns a reference |
|
|
271 | to a blessed hash containing the results of the translation process. |
|
|
272 | |
|
|
273 | =back |
|
|
274 | |
|
|
275 | =head2 Parameters |
|
|
276 | |
|
|
277 | =over |
|
|
278 | |
|
|
279 | =item ENVIRONMENT |
|
|
280 | |
|
|
281 | a WeBWorK::CourseEnvironment object |
|
|
282 | |
|
|
283 | =item USER |
|
|
284 | |
|
|
285 | a WeBWorK::User object |
|
|
286 | |
|
|
287 | =item KEY |
|
|
288 | |
|
|
289 | the session key of the current session |
|
|
290 | |
|
|
291 | =item SET |
|
|
292 | |
|
|
293 | a WeBWorK::Set object |
|
|
294 | |
|
|
295 | =item PROBLEM |
|
|
296 | |
|
|
297 | a WeBWorK::DB::Record::UserProblem object. The contents of the source_file |
|
|
298 | field can specify a PG file either by absolute path or path relative to the |
|
|
299 | "templates" directory. I<The caller should remove taint from this value before |
|
|
300 | passing!> |
|
|
301 | |
|
|
302 | =item PSVN |
|
|
303 | |
|
|
304 | the problem set version number |
|
|
305 | |
|
|
306 | =item FIELDS |
|
|
307 | |
|
|
308 | a reference to a hash (as returned by &WeBWorK::Form::Vars) containing form |
|
|
309 | fields submitted by a problem processor. The translator will look for fields |
|
|
310 | like "AnSwEr[0-9]" containing submitted student answers. |
|
|
311 | |
|
|
312 | =item OPTIONS |
|
|
313 | |
|
|
314 | a reference to a hash containing the following data: |
|
|
315 | |
|
|
316 | =over |
|
|
317 | |
|
|
318 | =item displayMode |
|
|
319 | |
|
|
320 | one of "plainText", "formattedText", or "images" |
|
|
321 | |
|
|
322 | =item showHints |
|
|
323 | |
|
|
324 | boolean, render hints |
|
|
325 | |
|
|
326 | =item showSolutions |
|
|
327 | |
|
|
328 | boolean, render solutions |
|
|
329 | |
|
|
330 | =item refreshMath2img |
|
|
331 | |
|
|
332 | boolean, force images created by math2img (in "images" mode) to be recreated, |
|
|
333 | even if the PG source has not been updated. FIXME: remove this option. |
|
|
334 | |
|
|
335 | =item processAnswers |
|
|
336 | |
|
|
337 | boolean, call answer evaluators and graders |
|
|
338 | |
|
|
339 | =back |
|
|
340 | |
|
|
341 | =back |
|
|
342 | |
|
|
343 | =head2 RETURN VALUE |
|
|
344 | |
|
|
345 | The C<new> method returns a blessed hash reference containing the following |
|
|
346 | fields. More information can be found in the documentation for |
|
|
347 | WeBWorK::PG::Translator. |
|
|
348 | |
|
|
349 | =over |
|
|
350 | |
|
|
351 | =item translator |
|
|
352 | |
|
|
353 | The WeBWorK::PG::Translator object used to render the problem. |
|
|
354 | |
|
|
355 | =item head_text |
|
|
356 | |
|
|
357 | HTML code for the E<lt>headE<gt> block of an resulting web page. Used for |
|
|
358 | JavaScript features. |
|
|
359 | |
|
|
360 | =item body_text |
|
|
361 | |
|
|
362 | HTML code for the E<lt>bodyE<gt> block of an resulting web page. |
|
|
363 | |
|
|
364 | =item answers |
|
|
365 | |
|
|
366 | An C<AnswerHash> object containing submitted answers, and results of answer |
|
|
367 | evaluation. |
|
|
368 | |
|
|
369 | =item result |
|
|
370 | |
|
|
371 | A hash containing the results of grading the problem. |
|
|
372 | |
|
|
373 | =item state |
|
|
374 | |
|
|
375 | A hash containing the new problem state. |
|
|
376 | |
|
|
377 | =item errors |
|
|
378 | |
|
|
379 | A string containing any errors encountered while rendering the problem. |
|
|
380 | |
|
|
381 | =item warnings |
|
|
382 | |
|
|
383 | A string containing any warnings encountered while rendering the problem. |
|
|
384 | |
|
|
385 | =item flags |
|
|
386 | |
|
|
387 | A hash containing PG_flags (see the Translator docs). |
|
|
388 | |
|
|
389 | =back |
|
|
390 | |
|
|
391 | =head1 METHODS PROVIDED BY THE BASE CLASS |
|
|
392 | |
|
|
393 | The following methods are provided for use by subclasses of WeBWorK::PG. |
|
|
394 | |
|
|
395 | =over |
|
|
396 | |
|
|
397 | =item defineProblemEnvir ENVIRONMENT, USER, KEY, SET, PROBLEM, PSVN, FIELDS, OPTIONS |
|
|
398 | |
|
|
399 | Generate a problem environment hash to pass to the renderer. |
|
|
400 | |
|
|
401 | =item translateDisplayModeNames NAME |
|
|
402 | |
|
|
403 | NAME contains |
|
|
404 | |
|
|
405 | =back |
|
|
406 | |
|
|
407 | =head1 AUTHOR |
|
|
408 | |
|
|
409 | Written by Sam Hathaway, sh002i (at) math.rochester.edu. |
|
|
410 | |
|
|
411 | =cut |