| 1 | ################################################################################ |
1 | ################################################################################ |
| 2 | # WeBWorK mod_perl (c) 2000-2002 WeBWorK Project |
2 | # WeBWorK Online Homework Delivery System |
| 3 | # $Id$ |
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. |
| 4 | ################################################################################ |
15 | ################################################################################ |
| 5 | |
16 | |
| 6 | package WeBWorK::PG; |
17 | package WeBWorK::PG; |
| 7 | |
18 | |
| 8 | =head1 NAME |
19 | =head1 NAME |
| 9 | |
20 | |
| 10 | WeBWorK::PG - Wrap the action of the PG Translator in an easy-to-use API. |
21 | WeBWorK::PG - Invoke one of several PG rendering methods using an easy-to-use |
|
|
22 | API. |
| 11 | |
23 | |
| 12 | =cut |
24 | =cut |
| 13 | |
25 | |
| 14 | use strict; |
26 | use strict; |
| 15 | use warnings; |
27 | use warnings; |
| 16 | use File::Path qw(rmtree); |
|
|
| 17 | use File::Temp qw(tempdir); |
|
|
| 18 | use WeBWorK::PG::Translator; |
28 | use WeBWorK::PG::ImageGenerator; |
| 19 | use WeBWorK::Utils qw(readFile formatDateTime writeTimingLogEntry); |
29 | use WeBWorK::Utils qw(runtime_use formatDateTime makeTempDirectory); |
| 20 | |
30 | |
| 21 | sub new($$$$$$$$) { |
31 | sub new { |
| 22 | my $invocant = shift; |
32 | shift; # throw away invocant -- we don't need it |
| 23 | my $class = ref($invocant) || $invocant; |
33 | my ($ce, $user, $key, $set, $problem, $psvn, $formFields, |
|
|
34 | $translationOptions) = @_; |
|
|
35 | |
|
|
36 | my $renderer = $ce->{pg}->{renderer}; |
|
|
37 | |
|
|
38 | runtime_use $renderer; |
|
|
39 | |
|
|
40 | return $renderer->new(@_); |
|
|
41 | } |
|
|
42 | |
|
|
43 | sub defineProblemEnvir { |
| 24 | my ( |
44 | my ( |
| 25 | $courseEnv, |
|
|
| 26 | $user, |
|
|
| 27 | $key, |
|
|
| 28 | $set, |
45 | $self, |
| 29 | $problem, |
|
|
| 30 | $psvn, |
|
|
| 31 | $formFields, # in CGI::Vars format |
|
|
| 32 | $translationOptions, # hashref containing options for the |
|
|
| 33 | # translator, such as whether to show |
|
|
| 34 | # hints and the display mode to use |
|
|
| 35 | ) = @_; |
|
|
| 36 | |
|
|
| 37 | # write timing log entry |
|
|
| 38 | writeTimingLogEntry($courseEnv, "WeBWorK::PG::new", |
|
|
| 39 | "user=".$user->id.",problem=".$courseEnv->{courseName}."/".$set->set_id."/".$problem->problem_id.",mode=".$translationOptions->{displayMode}, |
|
|
| 40 | "begin"); |
|
|
| 41 | |
|
|
| 42 | # install a local warn handler to collect warnings |
|
|
| 43 | my $warnings = ""; |
|
|
| 44 | local $SIG{__WARN__} = sub { $warnings .= shift } |
|
|
| 45 | if $courseEnv->{pg}->{options}->{catchWarnings}; |
|
|
| 46 | |
|
|
| 47 | # create a Translator |
|
|
| 48 | #warn "PG: creating a Translator\n"; |
|
|
| 49 | my $translator = WeBWorK::PG::Translator->new; |
|
|
| 50 | |
|
|
| 51 | # set the directory hash |
|
|
| 52 | #warn "PG: setting the directory hash\n"; |
|
|
| 53 | $translator->rh_directories({ |
|
|
| 54 | courseScriptsDirectory => $courseEnv->{webworkDirs}->{macros}, |
|
|
| 55 | macroDirectory => $courseEnv->{courseDirs}->{macros}, |
|
|
| 56 | templateDirectory => $courseEnv->{courseDirs}->{templates}, |
|
|
| 57 | tempDirectory => $courseEnv->{courseDirs}->{html_temp}, |
|
|
| 58 | }); |
|
|
| 59 | |
|
|
| 60 | # evaluate modules and "extra packages" |
|
|
| 61 | #warn "PG: evaluating modules and \"extra packages\"\n"; |
|
|
| 62 | my @modules = @{ $courseEnv->{pg}->{modules} }; |
|
|
| 63 | foreach my $module_packages_ref (@modules) { |
|
|
| 64 | my ($module, @extra_packages) = @$module_packages_ref; |
|
|
| 65 | # the first item is the main package |
|
|
| 66 | $translator->evaluate_modules($module); |
|
|
| 67 | # the remaining items are "extra" packages |
|
|
| 68 | $translator->load_extra_packages(@extra_packages); |
|
|
| 69 | } |
|
|
| 70 | |
|
|
| 71 | # set the environment (from defineProblemEnvir) |
|
|
| 72 | #warn "PG: setting the environment (from defineProblemEnvir)\n"; |
|
|
| 73 | my $envir = defineProblemEnvir( |
|
|
| 74 | $courseEnv, |
|
|
| 75 | $user, |
|
|
| 76 | $key, |
46 | $ce, |
| 77 | $set, |
|
|
| 78 | $problem, |
|
|
| 79 | $psvn, |
|
|
| 80 | $formFields, |
|
|
| 81 | $translationOptions, |
|
|
| 82 | ); |
|
|
| 83 | $translator->environment($envir); |
|
|
| 84 | |
|
|
| 85 | # initialize the Translator |
|
|
| 86 | #warn "PG: initializing the Translator\n"; |
|
|
| 87 | $translator->initialize(); |
|
|
| 88 | |
|
|
| 89 | # load IO.pl, PG.pl, and dangerousMacros.pl using unrestricted_load |
|
|
| 90 | # i'd like to change this at some point to have the same sort of interface to global.conf |
|
|
| 91 | # that the module loading does -- have a list of macros to load unrestrictedly. |
|
|
| 92 | #warn "PG: loading IO.pl, PG.pl, and dangerousMacros.pl using unrestricted_load\n"; |
|
|
| 93 | foreach (qw(IO.pl PG.pl dangerousMacros.pl)) { |
|
|
| 94 | my $macroPath = $courseEnv->{webworkDirs}->{macros} . "/$_"; |
|
|
| 95 | my $err = $translator->unrestricted_load($macroPath); |
|
|
| 96 | warn "Error while loading $macroPath: $err" if $err; |
|
|
| 97 | } |
|
|
| 98 | |
|
|
| 99 | # set the opcode mask (using default values) |
|
|
| 100 | #warn "PG: setting the opcode mask (using default values)\n"; |
|
|
| 101 | $translator->set_mask(); |
|
|
| 102 | |
|
|
| 103 | # store the problem source |
|
|
| 104 | #warn "PG: storing the problem source\n"; |
|
|
| 105 | my $sourceFile = $problem->source_file; |
|
|
| 106 | $sourceFile = $courseEnv->{courseDirs}->{templates}."/".$sourceFile |
|
|
| 107 | unless ($sourceFile =~ /^\//); |
|
|
| 108 | eval { $translator->source_string(readFile($sourceFile)) }; |
|
|
| 109 | if ($@) { |
|
|
| 110 | # well, we couldn't get the problem source, for some reason. |
|
|
| 111 | return bless { |
|
|
| 112 | translator => $translator, |
|
|
| 113 | head_text => "", |
|
|
| 114 | body_text => <<EOF, |
|
|
| 115 | WeBWorK::Utils::readFile($sourceFile) says: |
|
|
| 116 | $@ |
|
|
| 117 | EOF |
|
|
| 118 | answers => {}, |
|
|
| 119 | result => {}, |
|
|
| 120 | state => {}, |
|
|
| 121 | errors => "Failed to read the problem source file.", |
|
|
| 122 | warnings => $warnings, |
|
|
| 123 | flags => {error_flag => 1}, |
|
|
| 124 | }, $class; |
|
|
| 125 | } |
|
|
| 126 | |
|
|
| 127 | # install a safety filter (&safetyFilter) |
|
|
| 128 | #warn "PG: installing a safety filter\n"; |
|
|
| 129 | $translator->rf_safety_filter(\&safetyFilter); |
|
|
| 130 | |
|
|
| 131 | # write timing log entry -- the translator is now all set up |
|
|
| 132 | writeTimingLogEntry($courseEnv, "WeBWorK::PG::new", |
|
|
| 133 | "initialized", |
|
|
| 134 | "intermediate"); |
|
|
| 135 | |
|
|
| 136 | # translate the PG source into text |
|
|
| 137 | #warn "PG: translating the PG source into text\n"; |
|
|
| 138 | $translator->translate(); |
|
|
| 139 | |
|
|
| 140 | # after we're done translating, we may have to clean up after the translator. |
|
|
| 141 | # for example, 'images' mode uses a tempdir for dvipng's temp files. We have |
|
|
| 142 | # to remove it. |
|
|
| 143 | if ($translationOptions->{displayMode} eq 'images' && $envir->{dvipngTempDir}) { |
|
|
| 144 | rmtree($envir->{dvipngTempDir}, 0, 0); |
|
|
| 145 | } |
|
|
| 146 | |
|
|
| 147 | my ($result, $state); # we'll need these on the other side of the if block! |
|
|
| 148 | if ($translationOptions->{processAnswers}) { |
|
|
| 149 | |
|
|
| 150 | # process student answers |
|
|
| 151 | #warn "PG: processing student answers\n"; |
|
|
| 152 | $translator->process_answers($formFields); |
|
|
| 153 | |
|
|
| 154 | # retrieve the problem state and give it to the translator |
|
|
| 155 | #warn "PG: retrieving the problem state and giving it to the translator\n"; |
|
|
| 156 | $translator->rh_problem_state({ |
|
|
| 157 | recorded_score => $problem->status, |
|
|
| 158 | num_of_correct_ans => $problem->num_correct, |
|
|
| 159 | num_of_incorrect_ans => $problem->num_incorrect, |
|
|
| 160 | }); |
|
|
| 161 | |
|
|
| 162 | # determine an entry order -- the ANSWER_ENTRY_ORDER flag is built by |
|
|
| 163 | # the PG macro package (PG.pl) |
|
|
| 164 | #warn "PG: determining an entry order\n"; |
|
|
| 165 | my @answerOrder = |
|
|
| 166 | $translator->rh_flags->{ANSWER_ENTRY_ORDER} |
|
|
| 167 | ? @{ $translator->rh_flags->{ANSWER_ENTRY_ORDER} } |
|
|
| 168 | : keys %{ $translator->rh_evaluated_answers }; |
|
|
| 169 | |
|
|
| 170 | # install a grader -- use the one specified in the problem, |
|
|
| 171 | # or fall back on the default from the course environment. |
|
|
| 172 | # (two magic strings are accepted, to avoid having to |
|
|
| 173 | # reference code when it would be difficult.) |
|
|
| 174 | #warn "PG: installing a grader\n"; |
|
|
| 175 | my $grader = $translator->rh_flags->{PROBLEM_GRADER_TO_USE} |
|
|
| 176 | || $courseEnv->{pg}->{options}->{grader}; |
|
|
| 177 | $grader = $translator->rf_std_problem_grader |
|
|
| 178 | if $grader eq "std_problem_grader"; |
|
|
| 179 | $grader = $translator->rf_avg_problem_grader |
|
|
| 180 | if $grader eq "avg_problem_grader"; |
|
|
| 181 | die "Problem grader $grader is not a CODE reference." |
|
|
| 182 | unless ref $grader eq "CODE"; |
|
|
| 183 | $translator->rf_problem_grader($grader); |
|
|
| 184 | |
|
|
| 185 | # grade the problem |
|
|
| 186 | #warn "PG: grading the problem\n"; |
|
|
| 187 | ($result, $state) = $translator->grade_problem( |
|
|
| 188 | answers_submitted => $translationOptions->{processAnswers}, |
|
|
| 189 | ANSWER_ENTRY_ORDER => \@answerOrder, |
|
|
| 190 | ); |
|
|
| 191 | |
|
|
| 192 | } |
|
|
| 193 | |
|
|
| 194 | # write timing log entry |
|
|
| 195 | writeTimingLogEntry($courseEnv, "WeBWorK::PG::new", "", "end"); |
|
|
| 196 | |
|
|
| 197 | # return an object which contains the translator and the results of |
|
|
| 198 | # the translation process. this is DIFFERENT from the "format expected |
|
|
| 199 | # by Webwork.pm (and I believe processProblem8, but check.)" |
|
|
| 200 | return bless { |
|
|
| 201 | translator => $translator, |
|
|
| 202 | head_text => ${ $translator->r_header }, |
|
|
| 203 | body_text => ${ $translator->r_text }, |
|
|
| 204 | answers => $translator->rh_evaluated_answers, |
|
|
| 205 | result => $result, |
|
|
| 206 | state => $state, |
|
|
| 207 | errors => $translator->errors, |
|
|
| 208 | warnings => $warnings, |
|
|
| 209 | flags => $translator->rh_flags, |
|
|
| 210 | }, $class; |
|
|
| 211 | } |
|
|
| 212 | |
|
|
| 213 | # ----- |
|
|
| 214 | |
|
|
| 215 | sub defineProblemEnvir($$$$$$$) { |
|
|
| 216 | my ( |
|
|
| 217 | $courseEnv, |
|
|
| 218 | $user, |
47 | $user, |
| 219 | $key, |
48 | $key, |
| 220 | $set, |
49 | $set, |
| 221 | $problem, |
50 | $problem, |
| 222 | $psvn, |
51 | $psvn, |
| … | |
… | |
| 224 | $options, |
53 | $options, |
| 225 | ) = @_; |
54 | ) = @_; |
| 226 | |
55 | |
| 227 | my %envir; |
56 | my %envir; |
| 228 | |
57 | |
|
|
58 | # ---------------------------------------------------------------------- |
|
|
59 | |
| 229 | # PG environment variables |
60 | # PG environment variables |
| 230 | # from docs/pglanguage/pgreference/environmentvariables as of 06/25/2002 |
61 | # from docs/pglanguage/pgreference/environmentvariables as of 06/25/2002 |
| 231 | # any changes are noted by "ADDED:" or "REMOVED:" |
62 | # any changes are noted by "ADDED:" or "REMOVED:" |
| 232 | |
63 | |
| 233 | # Vital state information |
64 | # Vital state information |
| 234 | # ADDED: displayHintsQ, displaySolutionsQ, refreshMath2img, |
65 | # ADDED: displayHintsQ, displaySolutionsQ, refreshMath2img, |
| 235 | # texDisposition |
66 | # texDisposition |
| 236 | |
67 | |
| 237 | $envir{psvn} = $psvn; |
68 | $envir{psvn} = $set->psvn; |
| 238 | $envir{psvnNumber} = $envir{psvn}; |
69 | $envir{psvnNumber} = $envir{psvn}; |
| 239 | $envir{probNum} = $problem->problem_id; |
70 | $envir{probNum} = $problem->problem_id; |
| 240 | $envir{questionNumber} = $envir{probNum}; |
71 | $envir{questionNumber} = $envir{probNum}; |
| 241 | $envir{fileName} = $problem->source_file; |
72 | $envir{fileName} = $problem->source_file; |
| 242 | $envir{probFileName} = $envir{fileName}; |
73 | $envir{probFileName} = $envir{fileName}; |
| 243 | $envir{problemSeed} = $problem->problem_seed; |
74 | $envir{problemSeed} = $problem->problem_seed; |
| 244 | $envir{displayMode} = translateDisplayModeNames($options->{displayMode}); |
75 | $envir{displayMode} = translateDisplayModeNames($options->{displayMode}); |
| 245 | $envir{languageMode} = $envir{displayMode}; |
76 | $envir{languageMode} = $envir{displayMode}; |
| 246 | $envir{outputMode} = $envir{displayMode}; |
77 | $envir{outputMode} = $envir{displayMode}; |
| 247 | $envir{displayHintsQ} = $options->{showHints}; |
78 | $envir{displayHintsQ} = $options->{showHints}; |
| 248 | $envir{displaySolutionsQ} = $options->{showSolutions}; |
79 | $envir{displaySolutionsQ} = $options->{showSolutions}; |
| 249 | $envir{refreshMath2img} = $options->{refreshMath2img}; |
|
|
| 250 | $envir{texDisposition} = "pdf"; # in webwork-modperl, we use pdflatex |
80 | $envir{texDisposition} = "pdf"; # in webwork2, we use pdflatex |
| 251 | |
81 | |
| 252 | # Problem Information |
82 | # Problem Information |
| 253 | # ADDED: courseName, formatedDueDate |
83 | # ADDED: courseName, formatedDueDate |
| 254 | |
84 | |
| 255 | $envir{openDate} = $set->open_date; |
85 | $envir{openDate} = $set->open_date; |
| … | |
… | |
| 260 | $envir{answerDate} = $set->answer_date; |
90 | $envir{answerDate} = $set->answer_date; |
| 261 | $envir{formattedAnswerDate} = formatDateTime($envir{answerDate}); |
91 | $envir{formattedAnswerDate} = formatDateTime($envir{answerDate}); |
| 262 | $envir{numOfAttempts} = ($problem->num_correct || 0) + ($problem->num_incorrect || 0); |
92 | $envir{numOfAttempts} = ($problem->num_correct || 0) + ($problem->num_incorrect || 0); |
| 263 | $envir{problemValue} = $problem->value; |
93 | $envir{problemValue} = $problem->value; |
| 264 | $envir{sessionKey} = $key; |
94 | $envir{sessionKey} = $key; |
| 265 | $envir{courseName} = $courseEnv->{courseName}; |
95 | $envir{courseName} = $ce->{courseName}; |
| 266 | |
96 | |
| 267 | # Student Information |
97 | # Student Information |
| 268 | # ADDED: studentID |
98 | # ADDED: studentID |
| 269 | |
99 | |
| 270 | $envir{sectionName} = $user->section; |
100 | $envir{sectionName} = $user->section; |
| 271 | $envir{sectionNumber} = $envir{sectionName}; |
101 | $envir{sectionNumber} = $envir{sectionName}; |
| 272 | $envir{recitationName} = $user->recitation; |
102 | $envir{recitationName} = $user->recitation; |
| 273 | $envir{recitationNumber} = $envir{recitationName}; |
103 | $envir{recitationNumber} = $envir{recitationName}; |
| 274 | $envir{setNumber} = $set->set_id; |
104 | $envir{setNumber} = $set->set_id; |
| 275 | $envir{studentLogin} = $user->id; |
105 | $envir{studentLogin} = $user->user_id; |
| 276 | $envir{studentName} = $user->first_name . " " . $user->last_name; |
106 | $envir{studentName} = $user->first_name . " " . $user->last_name; |
| 277 | $envir{studentID} = $user->student_id; |
107 | $envir{studentID} = $user->student_id; |
| 278 | |
108 | |
| 279 | # Answer Information |
109 | # Answer Information |
| 280 | # REMOVED: refSubmittedAnswers |
110 | # REMOVED: refSubmittedAnswers |
| … | |
… | |
| 283 | |
113 | |
| 284 | # External Programs |
114 | # External Programs |
| 285 | # ADDED: externalLaTeXPath, externalDvipngPath, |
115 | # ADDED: externalLaTeXPath, externalDvipngPath, |
| 286 | # externalGif2EpsPath, externalPng2EpsPath |
116 | # externalGif2EpsPath, externalPng2EpsPath |
| 287 | |
117 | |
| 288 | $envir{externalTTHPath} = $courseEnv->{externalPrograms}->{tth}; |
118 | $envir{externalTTHPath} = $ce->{externalPrograms}->{tth}; |
| 289 | $envir{externalLaTeXPath} = $courseEnv->{externalPrograms}->{latex}; |
119 | $envir{externalLaTeXPath} = $ce->{externalPrograms}->{latex}; |
| 290 | $envir{externalDvipngPath} = $courseEnv->{externalPrograms}->{dvipng}; |
120 | $envir{externalDvipngPath} = $ce->{externalPrograms}->{dvipng}; |
| 291 | $envir{externalGif2EpsPath} = $courseEnv->{externalPrograms}->{gif2eps}; |
121 | $envir{externalGif2EpsPath} = $ce->{externalPrograms}->{gif2eps}; |
| 292 | $envir{externalPng2EpsPath} = $courseEnv->{externalPrograms}->{png2eps}; |
122 | $envir{externalPng2EpsPath} = $ce->{externalPrograms}->{png2eps}; |
| 293 | $envir{externalGif2PngPath} = $courseEnv->{externalPrograms}->{gif2png}; |
123 | $envir{externalGif2PngPath} = $ce->{externalPrograms}->{gif2png}; |
| 294 | |
124 | |
| 295 | # Directories and URLs |
125 | # Directories and URLs |
| 296 | # REMOVED: courseName |
126 | # REMOVED: courseName |
| 297 | # ADDED: dvipngTempDir |
127 | # ADDED: dvipngTempDir |
| 298 | |
128 | |
| 299 | $envir{cgiDirectory} = undef; |
129 | $envir{cgiDirectory} = undef; |
| 300 | $envir{cgiURL} = undef; |
130 | $envir{cgiURL} = undef; |
| 301 | $envir{classDirectory} = undef; |
131 | $envir{classDirectory} = undef; |
| 302 | $envir{courseScriptsDirectory} = $courseEnv->{webworkDirs}->{macros}."/"; |
132 | $envir{courseScriptsDirectory} = $ce->{pg}->{directories}->{macros}."/"; |
| 303 | $envir{htmlDirectory} = $courseEnv->{courseDirs}->{html}."/"; |
133 | $envir{htmlDirectory} = $ce->{courseDirs}->{html}."/"; |
| 304 | $envir{htmlURL} = $courseEnv->{courseURLs}->{html}."/"; |
134 | $envir{htmlURL} = $ce->{courseURLs}->{html}."/"; |
| 305 | $envir{macroDirectory} = $courseEnv->{courseDirs}->{macros}."/"; |
135 | $envir{macroDirectory} = $ce->{courseDirs}->{macros}."/"; |
| 306 | $envir{templateDirectory} = $courseEnv->{courseDirs}->{templates}."/"; |
136 | $envir{templateDirectory} = $ce->{courseDirs}->{templates}."/"; |
| 307 | $envir{tempDirectory} = $courseEnv->{courseDirs}->{html_temp}."/"; |
137 | $envir{tempDirectory} = $ce->{courseDirs}->{html_temp}."/"; |
| 308 | $envir{tempURL} = $courseEnv->{courseURLs}->{html_temp}."/"; |
138 | $envir{tempURL} = $ce->{courseURLs}->{html_temp}."/"; |
| 309 | $envir{scriptDirectory} = undef; |
139 | $envir{scriptDirectory} = undef; |
| 310 | $envir{webworkDocsURL} = $courseEnv->{webworkURLs}->{docs}."/"; |
140 | $envir{webworkDocsURL} = $ce->{webworkURLs}->{docs}."/"; |
| 311 | $envir{dvipngTempDir} = $options->{displayMode} eq 'images' |
|
|
| 312 | ? tempdir("webwork-dvipng-XXXXXXXX", DIR => $envir{tempDirectory}) |
|
|
| 313 | : undef; |
|
|
| 314 | |
141 | |
| 315 | # Information for sending mail |
142 | # Information for sending mail |
| 316 | |
143 | |
| 317 | $envir{mailSmtpServer} = $courseEnv->{mail}->{smtpServer}; |
144 | $envir{mailSmtpServer} = $ce->{mail}->{smtpServer}; |
| 318 | $envir{mailSmtpSender} = $courseEnv->{mail}->{smtpSender}; |
145 | $envir{mailSmtpSender} = $ce->{mail}->{smtpSender}; |
| 319 | $envir{ALLOW_MAIL_TO} = $courseEnv->{mail}->{allowedRecipients}; |
146 | $envir{ALLOW_MAIL_TO} = $ce->{mail}->{allowedRecipients}; |
| 320 | |
147 | |
| 321 | # Default values for evaluating answers |
148 | # Default values for evaluating answers |
| 322 | |
149 | |
| 323 | my $ansEvalDefaults = $courseEnv->{pg}->{ansEvalDefaults}; |
150 | my $ansEvalDefaults = $ce->{pg}->{ansEvalDefaults}; |
| 324 | $envir{$_} = $ansEvalDefaults->{$_} foreach (keys %$ansEvalDefaults); |
151 | $envir{$_} = $ansEvalDefaults->{$_} foreach (keys %$ansEvalDefaults); |
| 325 | |
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 | |
| 326 | # Other things... |
169 | # Other things... |
| 327 | |
170 | $envir{QUIZ_PREFIX} = $options->{QUIZ_PREFIX}; # used by quizzes |
| 328 | $envir{PROBLEM_GRADER_TO_USE} = $courseEnv->{pg}->{options}->{grader}; |
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}; |
| 329 | |
179 | |
| 330 | return \%envir; |
180 | return \%envir; |
| 331 | } |
181 | } |
| 332 | |
182 | |
| 333 | sub translateDisplayModeNames($) { |
183 | sub translateDisplayModeNames($) { |
| 334 | my $name = shift; |
184 | my $name = shift; |
| 335 | return { |
185 | return { |
| 336 | tex => "TeX", |
186 | tex => "TeX", |
| 337 | plainText => "HTML", |
187 | plainText => "HTML", |
| 338 | formattedText => "HTML_tth", |
188 | formattedText => "HTML_tth", |
| 339 | images => "HTML_img" |
189 | images => "HTML_dpng", # "HTML_img", |
| 340 | }->{$name}; |
190 | }->{$name}; |
| 341 | } |
191 | } |
| 342 | |
192 | |
| 343 | sub safetyFilter { |
193 | sub oldSafetyFilter { |
| 344 | my $answer = shift; # accepts one answer and checks it |
194 | my $answer = shift; # accepts one answer and checks it |
| 345 | my $submittedAnswer = $answer; |
195 | my $submittedAnswer = $answer; |
| 346 | $answer = '' unless defined $answer; |
196 | $answer = '' unless defined $answer; |
| 347 | my ($errorno); |
197 | my ($errorno); |
| 348 | $answer =~ tr/\000-\037/ /; |
198 | $answer =~ tr/\000-\037/ /; |
| … | |
… | |
| 353 | return ($answer,$errorno); |
203 | return ($answer,$errorno); |
| 354 | } |
204 | } |
| 355 | # replace ^ with ** (for exponentiation) |
205 | # replace ^ with ** (for exponentiation) |
| 356 | # $answer =~ s/\^/**/g; |
206 | # $answer =~ s/\^/**/g; |
| 357 | # Return if forbidden characters are found |
207 | # Return if forbidden characters are found |
| 358 | unless ($answer =~ /^[a-zA-Z0-9_\-\+ \t\/@%\*\.\n^\(\)]+$/ ) { |
208 | unless ($answer =~ /^[a-zA-Z0-9_\-\+ \t\/@%\*\.\n^\[\]\(\)\,\|]+$/ ) { |
| 359 | $answer =~ tr/a-zA-Z0-9_\-\+ \t\/@%\*\.\n^\(\)/#/c; |
209 | $answer =~ tr/a-zA-Z0-9_\-\+ \t\/@%\*\.\n^\(\)/#/c; |
| 360 | $errorno = "<BR>There are forbidden characters in your answer: $submittedAnswer<BR>"; |
210 | $errorno = "<BR>There are forbidden characters in your answer: $submittedAnswer<BR>"; |
| 361 | return ($answer,$errorno); |
211 | return ($answer,$errorno); |
| 362 | } |
212 | } |
| 363 | $errorno = 0; |
213 | $errorno = 0; |
| 364 | return($answer, $errorno); |
214 | return($answer, $errorno); |
| 365 | } |
215 | } |
| 366 | |
216 | |
|
|
217 | sub nullSafetyFilter { |
|
|
218 | return shift, 0; # no errors |
|
|
219 | } |
|
|
220 | |
| 367 | 1; |
221 | 1; |
| 368 | |
222 | |
| 369 | __END__ |
223 | __END__ |
| 370 | |
224 | |
| 371 | =head1 SYNOPSIS |
225 | =head1 SYNOPSIS |
| 372 | |
226 | |
| 373 | $pg = WeBWorK::PG->new( |
227 | $pg = WeBWorK::PG->new( |
| 374 | $courseEnv, # a WeBWorK::CourseEnvironment object |
228 | $ce, # a WeBWorK::CourseEnvironment object |
| 375 | $user, # a WeBWorK::DB::Record::User object |
229 | $user, # a WeBWorK::DB::Record::User object |
| 376 | $sessionKey, |
230 | $sessionKey, |
| 377 | $set, # a WeBWorK::DB::Record::UserSet object |
231 | $set, # a WeBWorK::DB::Record::UserSet object |
| 378 | $problem, # a WeBWorK::DB::Record::UserProblem object |
232 | $problem, # a WeBWorK::DB::Record::UserProblem object |
| 379 | $psvn, |
233 | $psvn, |
| … | |
… | |
| 397 | $warnings = $pg->{warnings}; # text string |
251 | $warnings = $pg->{warnings}; # text string |
| 398 | $flags = $pg->{flags}; # hash reference |
252 | $flags = $pg->{flags}; # hash reference |
| 399 | |
253 | |
| 400 | =head1 DESCRIPTION |
254 | =head1 DESCRIPTION |
| 401 | |
255 | |
| 402 | WeBWorK::PG encapsulates the PG translation process, making multiple calls to |
256 | WeBWorK::PG is a factory for modules which use the WeBWorK::PG API. Notable |
| 403 | WeBWorK::PG::Translator. Much of the flexibility of the Translator is hidden, |
257 | modules which use this API (and exist) are WeBWorK::PG::Local and |
| 404 | instead making choices that are appropriate for the webwork-modperl system. |
258 | WeBWorK::PG::Remote. The course environment key $pg{renderer} is consulted to |
|
|
259 | determine which render to use. |
| 405 | |
260 | |
| 406 | =head1 CONSTRUCTION |
261 | =head1 THE WEBWORK::PG API |
|
|
262 | |
|
|
263 | Modules which support this API must implement the following method: |
| 407 | |
264 | |
| 408 | =over |
265 | =over |
| 409 | |
266 | |
| 410 | =item new (ENVIRONMENT, USER, KEY, SET, PROBLEM, PSVN, FIELDS, OPTIONS) |
267 | =item new ENVIRONMENT, USER, KEY, SET, PROBLEM, PSVN, FIELDS, OPTIONS |
| 411 | |
268 | |
| 412 | The C<new> method creates a translator, initializes it using the parameters |
269 | The C<new> method creates a translator, initializes it using the parameters |
| 413 | specified, translates a PG file, and processes answers. It returns a reference |
270 | specified, translates a PG file, and processes answers. It returns a reference |
| 414 | to a blessed hash containing the results of the translation process. |
271 | to a blessed hash containing the results of the translation process. |
| 415 | |
272 | |
| … | |
… | |
| 471 | boolean, render solutions |
328 | boolean, render solutions |
| 472 | |
329 | |
| 473 | =item refreshMath2img |
330 | =item refreshMath2img |
| 474 | |
331 | |
| 475 | boolean, force images created by math2img (in "images" mode) to be recreated, |
332 | boolean, force images created by math2img (in "images" mode) to be recreated, |
| 476 | even if the PG source has not been updated. |
333 | even if the PG source has not been updated. FIXME: remove this option. |
| 477 | |
334 | |
| 478 | =item processAnswers |
335 | =item processAnswers |
| 479 | |
336 | |
| 480 | boolean, call answer evaluators and graders |
337 | boolean, call answer evaluators and graders |
| 481 | |
338 | |
| … | |
… | |
| 529 | |
386 | |
| 530 | A hash containing PG_flags (see the Translator docs). |
387 | A hash containing PG_flags (see the Translator docs). |
| 531 | |
388 | |
| 532 | =back |
389 | =back |
| 533 | |
390 | |
| 534 | =head1 OPERATION |
391 | =head1 METHODS PROVIDED BY THE BASE CLASS |
| 535 | |
392 | |
| 536 | WeBWorK::PG goes through the following operations when constructed: |
393 | The following methods are provided for use by subclasses of WeBWorK::PG. |
| 537 | |
394 | |
| 538 | =over |
395 | =over |
| 539 | |
396 | |
| 540 | =item Get database information |
397 | =item defineProblemEnvir ENVIRONMENT, USER, KEY, SET, PROBLEM, PSVN, FIELDS, OPTIONS |
| 541 | |
398 | |
| 542 | Retrieve information about the current user, set, and problem from the |
399 | Generate a problem environment hash to pass to the renderer. |
| 543 | database. |
|
|
| 544 | |
400 | |
| 545 | =item Create a translator |
401 | =item translateDisplayModeNames NAME |
| 546 | |
402 | |
| 547 | Instantiate a WeBWorK::PG::Translator object. |
403 | NAME contains |
| 548 | |
|
|
| 549 | =item Set the directory hash |
|
|
| 550 | |
|
|
| 551 | Set the translator's directory hash (courseScripts, macros, templates, and temp |
|
|
| 552 | directories) from the course environment. |
|
|
| 553 | |
|
|
| 554 | =item Evaluate PG modules |
|
|
| 555 | |
|
|
| 556 | Using the module list from the course environment (pg->modules), perform a |
|
|
| 557 | "use"-like operation to evaluate modules at runtime. |
|
|
| 558 | |
|
|
| 559 | =item Set the problem environment |
|
|
| 560 | |
|
|
| 561 | Use data from the user, set, and problem, as well as the course environemnt and |
|
|
| 562 | translation options, to set the problem environment. |
|
|
| 563 | |
|
|
| 564 | =item Initialize the translator |
|
|
| 565 | |
|
|
| 566 | Call &WeBWorK::PG::Translator::initialize. What more do you want? |
|
|
| 567 | |
|
|
| 568 | =item Load PG.pl and dangerousMacros.pl |
|
|
| 569 | |
|
|
| 570 | These macros must be loaded without opcode masking, so they are loaded here. |
|
|
| 571 | |
|
|
| 572 | =item Set the opcode mask |
|
|
| 573 | |
|
|
| 574 | Set the opcode mask to the default specified by WeBWorK::PG::Translator. |
|
|
| 575 | |
|
|
| 576 | =item Load the problem source |
|
|
| 577 | |
|
|
| 578 | Give the problem source to the translator. |
|
|
| 579 | |
|
|
| 580 | =item Install a safety filter |
|
|
| 581 | |
|
|
| 582 | The safety filter is used to preprocess student input before evaluation. The |
|
|
| 583 | default safety filter, &WeBWorK::PG::safetyFilter, is used. |
|
|
| 584 | |
|
|
| 585 | =item Translate the problem source |
|
|
| 586 | |
|
|
| 587 | Call &WeBWorK::PG::Translator::translate to render the problem source into the |
|
|
| 588 | format given by the display mode. |
|
|
| 589 | |
|
|
| 590 | =item Process student answers |
|
|
| 591 | |
|
|
| 592 | Use form field inputs to evaluate student answers. |
|
|
| 593 | |
|
|
| 594 | =item Load the problem state |
|
|
| 595 | |
|
|
| 596 | Use values from the database to initialize the problem state, so that the |
|
|
| 597 | grader will have a point of reference. |
|
|
| 598 | |
|
|
| 599 | =item Determine an entry order |
|
|
| 600 | |
|
|
| 601 | Use the ANSWER_ENTRY_ORDER flag to determine the order of answers in the |
|
|
| 602 | problem. This is important for problems with dependancies among parts. |
|
|
| 603 | |
|
|
| 604 | =item Install a grader |
|
|
| 605 | |
|
|
| 606 | Use the PROBLEM_GRADER_TO_USE flag, or a default from the course environment, |
|
|
| 607 | to install a grader. |
|
|
| 608 | |
|
|
| 609 | =item Grade the problem |
|
|
| 610 | |
|
|
| 611 | Use the selected grader to grade the problem. |
|
|
| 612 | |
404 | |
| 613 | =back |
405 | =back |
| 614 | |
406 | |
| 615 | =head1 AUTHOR |
407 | =head1 AUTHOR |
| 616 | |
408 | |