|
|
1 | ################################################################################ |
|
|
2 | # WeBWorK Online Homework Delivery System |
|
|
3 | # Copyright © 2000-2003 The WeBWorK Project, http://openwebwork.sf.net/ |
|
|
4 | # $CVSHeader: webwork2/lib/WeBWorK/PG.pm,v 1.61 2005/07/05 18:16:12 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 | sub new($$$$$$$$) { |
31 | use constant DISPLAY_MODES => { |
| 13 | my $invocant = shift; |
32 | # display name # mode name |
| 14 | my $class = ref($invocant) || $invocant; |
33 | tex => "TeX", |
|
|
34 | plainText => "HTML", |
|
|
35 | formattedText => "HTML_tth", |
|
|
36 | images => "HTML_dpng", |
|
|
37 | jsMath => "HTML_jsMath", |
|
|
38 | asciimath => "HTML_asciimath", |
|
|
39 | }; |
|
|
40 | |
|
|
41 | sub new { |
|
|
42 | shift; # throw away invocant -- we don't need it |
|
|
43 | my ($ce, $user, $key, $set, $problem, $psvn, $formFields, |
|
|
44 | $translationOptions) = @_; |
|
|
45 | |
|
|
46 | my $renderer = $ce->{pg}->{renderer}; |
|
|
47 | |
|
|
48 | runtime_use $renderer; |
|
|
49 | |
|
|
50 | return $renderer->new(@_); |
|
|
51 | } |
|
|
52 | |
|
|
53 | sub defineProblemEnvir { |
| 15 | my ( |
54 | my ( |
| 16 | $courseEnv, |
55 | $self, |
| 17 | $userName, |
|
|
| 18 | $key, |
56 | $ce, |
| 19 | $setName, |
|
|
| 20 | $problemNumber, |
|
|
| 21 | $translationOptions, # hashref containing options for the |
|
|
| 22 | # translator, such as whether to show |
|
|
| 23 | # hints and the display mode to use |
|
|
| 24 | $formFields, # in CGI::Vars format |
|
|
| 25 | ) = @_; |
|
|
| 26 | |
|
|
| 27 | # get database information |
|
|
| 28 | my $classlist = WeBWorK::DB::Classlist->new($courseEnv); |
|
|
| 29 | my $wwdb = WeBWorK::DB::WW->new($courseEnv); |
|
|
| 30 | my $user = $classlist->getUser($userName); |
|
|
| 31 | my $set = $wwdb->getSet($userName, $setName); |
|
|
| 32 | my $problem = $wwdb->getProblem($userName, $setName, $problemNumber); |
|
|
| 33 | my $psvn = $wwdb->getPSVN($userName, $setName); |
|
|
| 34 | |
|
|
| 35 | # create a Translator |
|
|
| 36 | warn "PG: creating a Translator\n"; |
|
|
| 37 | my $translator = WeBWorK::PG::Translator->new; |
|
|
| 38 | |
|
|
| 39 | # set the directory hash |
|
|
| 40 | warn "PG: setting the directory hash\n"; |
|
|
| 41 | $translator->rh_directories({ |
|
|
| 42 | courseScriptsDirectory => $courseEnv->{webworkDirs}->{macros}, |
|
|
| 43 | macroDirectory => $courseEnv->{courseDirs}->{macros}, |
|
|
| 44 | templateDirectory => $courseEnv->{courseDirs}->{templates}, |
|
|
| 45 | tempDirectory => $courseEnv->{courseDirs}->{html_temp}, |
|
|
| 46 | }); |
|
|
| 47 | |
|
|
| 48 | # evaluate modules and "extra packages" |
|
|
| 49 | warn "PG: evaluating modules and \"extra packages\"\n"; |
|
|
| 50 | my @modules = @{ $courseEnv->{pg}->{modules} }; |
|
|
| 51 | foreach my $module_packages (@modules) { |
|
|
| 52 | # the first item in $module_packages is the main package |
|
|
| 53 | $translator->evaluate_modules(shift @$module_packages); |
|
|
| 54 | # the remaining items are "extra" packages |
|
|
| 55 | $translator->load_extra_packages(@$module_packages); |
|
|
| 56 | } |
|
|
| 57 | |
|
|
| 58 | # set the environment (from defineProblemEnvir) |
|
|
| 59 | warn "PG: setting the environment (from defineProblemEnvir)\n"; |
|
|
| 60 | $translator->environment(defineProblemEnvir( |
|
|
| 61 | $courseEnv, $user, $key, $set, $problem, $psvn, $formFields, $translationOptions)); |
|
|
| 62 | |
|
|
| 63 | # initialize the Translator |
|
|
| 64 | warn "PG: initializing the Translator\n"; |
|
|
| 65 | $translator->initialize(); |
|
|
| 66 | |
|
|
| 67 | # load PG.pl and dangerousMacros.pl using unrestricted_load |
|
|
| 68 | warn "PG: loading PG.pl and dangerousMacros.pl using unrestricted_load\n"; |
|
|
| 69 | my $pg_pl = $courseEnv->{webworkDirs}->{macros} . "/PG.pl"; |
|
|
| 70 | my $dangerousMacros_pl = $courseEnv->{webworkDirs}->{macros} . "/dangerousMacros.pl"; |
|
|
| 71 | my $err = $translator->unrestricted_load($pg_pl); |
|
|
| 72 | warn "Error while loading $pg_pl: $err" if $err; |
|
|
| 73 | $err = $translator->unrestricted_load($dangerousMacros_pl); |
|
|
| 74 | warn "Error while loading $dangerousMacros_pl: $err" if $err; |
|
|
| 75 | |
|
|
| 76 | # set the opcode mask (using default values) |
|
|
| 77 | warn "PG: setting the opcode mask (using default values)\n"; |
|
|
| 78 | $translator->set_mask(); |
|
|
| 79 | |
|
|
| 80 | # store the problem source |
|
|
| 81 | warn "PG: storing the problem source\n"; |
|
|
| 82 | my $sourceFile = $courseEnv->{courseDirs}->{templates}."/".$problem->source_file; |
|
|
| 83 | $translator->source_string(readFile($sourceFile)); |
|
|
| 84 | |
|
|
| 85 | # install a safety filter (&safetyFilter) |
|
|
| 86 | warn "PG: installing a safety filter\n"; |
|
|
| 87 | $translator->rf_safety_filter(\&safetyFilter); |
|
|
| 88 | |
|
|
| 89 | # translate the PG source into text |
|
|
| 90 | warn "PG: translating the PG source into text\n"; |
|
|
| 91 | $translator->translate(); |
|
|
| 92 | |
|
|
| 93 | # [in Problem.pm and processProblem8.pl, "install a grader" is here] |
|
|
| 94 | |
|
|
| 95 | # process student answers |
|
|
| 96 | warn "PG: processing student answers\n"; |
|
|
| 97 | $translator->process_answers($formFields); |
|
|
| 98 | |
|
|
| 99 | # retrieve the problem state and give it to the translator |
|
|
| 100 | warn "PG: retrieving the problem state and giving it to the translator\n"; |
|
|
| 101 | $translator->rh_problem_state({ |
|
|
| 102 | recorded_score => $problem->status, |
|
|
| 103 | num_of_correct_ans => $problem->num_correct, |
|
|
| 104 | num_of_incorrect_ans => $problem->num_incorrect, |
|
|
| 105 | }); |
|
|
| 106 | |
|
|
| 107 | # determine an entry order -- the ANSWER_ENTRY_ORDER flag is built by |
|
|
| 108 | # the PG macro package (PG.pl) |
|
|
| 109 | warn "PG: determining an entry order\n"; |
|
|
| 110 | my @answerOrder = |
|
|
| 111 | $translator->rh_flags->{ANSWER_ENTRY_ORDER} |
|
|
| 112 | ? @{ $translator->rh_flags->{ANSWER_ENTRY_ORDER} } |
|
|
| 113 | : keys %{ $translator->rh_evaluated_answers }; |
|
|
| 114 | |
|
|
| 115 | # install a grader -- use the one specified in the problem, |
|
|
| 116 | # or fall back on the default from the course environment. |
|
|
| 117 | # (two magic strings are accepted, to avoid having to |
|
|
| 118 | # reference code when it would be difficult.) |
|
|
| 119 | warn "PG: installing a grader\n"; |
|
|
| 120 | my $grader = $translator->rh_flags->{PROBLEM_GRADER_TO_USE} |
|
|
| 121 | || $courseEnv->{pg}->{options}->{grader}; |
|
|
| 122 | $grader = $translator->rf_std_problem_grader |
|
|
| 123 | if $grader eq "std_problem_grader"; |
|
|
| 124 | $grader = $translator->rf_avg_problem_grader |
|
|
| 125 | if $grader eq "avg_problem_grader"; |
|
|
| 126 | die "Problem grader $grader is not a CODE reference." |
|
|
| 127 | unless ref $grader eq "CODE"; |
|
|
| 128 | $translator->rf_problem_grader($grader); |
|
|
| 129 | |
|
|
| 130 | # grading the problem |
|
|
| 131 | warn "PG: grade the problem\n"; |
|
|
| 132 | my ($result, $state) = $translator->grade_problem( |
|
|
| 133 | answers_submitted => $translationOptions->{processAnswers}, |
|
|
| 134 | ANSWER_ENTRY_ORDER => \@answerOrder, |
|
|
| 135 | ); |
|
|
| 136 | |
|
|
| 137 | # return an object which contains the translator and the results of |
|
|
| 138 | # the translation process. this is DIFFERENT from the "format expected |
|
|
| 139 | # by Webwork.pm (and I believe processProblem8, but check.)" |
|
|
| 140 | return bless { |
|
|
| 141 | translator => $translator, |
|
|
| 142 | head_text => ${ $translator->r_header }, |
|
|
| 143 | body_text => ${ $translator->r_text }, |
|
|
| 144 | answers => $translator->rh_evaluated_answers, |
|
|
| 145 | result => $result, |
|
|
| 146 | state => $state, |
|
|
| 147 | errors => $translator->errors, # *** what is this doing? |
|
|
| 148 | warnings => undef, # *** gotta catch warnings eventually... |
|
|
| 149 | flags => $translator->rh_flags, |
|
|
| 150 | }, $class; |
|
|
| 151 | } |
|
|
| 152 | |
|
|
| 153 | # ----- |
|
|
| 154 | |
|
|
| 155 | sub defineProblemEnvir($$$$$$$) { |
|
|
| 156 | my ( |
|
|
| 157 | $courseEnv, |
|
|
| 158 | $user, |
57 | $user, |
| 159 | $key, |
58 | $key, |
| 160 | $set, |
59 | $set, |
| 161 | $problem, |
60 | $problem, |
| 162 | $psvn, |
61 | $psvn, |
| … | |
… | |
| 164 | $options, |
63 | $options, |
| 165 | ) = @_; |
64 | ) = @_; |
| 166 | |
65 | |
| 167 | my %envir; |
66 | my %envir; |
| 168 | |
67 | |
|
|
68 | # ---------------------------------------------------------------------- |
|
|
69 | |
| 169 | # PG environment variables |
70 | # PG environment variables |
| 170 | # from docs/pglanguage/pgreference/environmentvariables as of 06/25/2002 |
71 | # from docs/pglanguage/pgreference/environmentvariables as of 06/25/2002 |
| 171 | # any changes are noted by "ADDED:" or "REMOVED:" |
72 | # any changes are noted by "ADDED:" or "REMOVED:" |
| 172 | |
73 | |
| 173 | # Vital state information |
74 | # Vital state information |
| 174 | # ADDED: displayHintsQ, displaySolutionsQ, externalTTHPath |
75 | # ADDED: displayModeFailover, displayHintsQ, displaySolutionsQ, |
|
|
76 | # refreshMath2img, texDisposition |
| 175 | |
77 | |
| 176 | $envir{psvn} = $psvn; |
78 | $envir{psvn} = $set->psvn; |
| 177 | $envir{psvnNumber} = $envir{psvn}; |
79 | $envir{psvnNumber} = $envir{psvn}; |
| 178 | $envir{probNum} = $problem->id; |
80 | $envir{probNum} = $problem->problem_id; |
| 179 | $envir{questionNumber} = $envir{probNum}; |
81 | $envir{questionNumber} = $envir{probNum}; |
| 180 | $envir{fileName} = $problem->source_file; |
82 | $envir{fileName} = $problem->source_file; |
| 181 | $envir{probFileName} = $envir{fileName}; |
83 | $envir{probFileName} = $envir{fileName}; |
| 182 | $envir{problemSeed} = $problem->problem_seed; |
84 | $envir{problemSeed} = $problem->problem_seed; |
| 183 | $envir{displayMode} = translateDisplayModeNames($options->{displayMode}); |
85 | $envir{displayMode} = translateDisplayModeNames($options->{displayMode}); |
| 184 | $envir{languageMode} = $envir{displayMode}; |
86 | $envir{languageMode} = $envir{displayMode}; |
| 185 | $envir{outputMode} = $envir{displayMode}; |
87 | $envir{outputMode} = $envir{displayMode}; |
| 186 | $envir{displayHintsQ} = $options->{hints}; |
88 | $envir{displayHintsQ} = $options->{showHints}; |
| 187 | $envir{displaySolutionsQ} = $options->{solutions}; |
89 | $envir{displaySolutionsQ} = $options->{showSolutions}; |
| 188 | $envir{externalTTHPath} = $courseEnv->{externalPrograms}->{tth}; |
90 | $envir{texDisposition} = "pdf"; # in webwork2, we use pdflatex |
| 189 | |
91 | |
| 190 | # Problem Information |
92 | # Problem Information |
| 191 | # ADDED: courseName |
93 | # ADDED: courseName, formatedDueDate |
| 192 | |
94 | |
| 193 | $envir{openDate} = $set->open_date; |
95 | $envir{openDate} = $set->open_date; |
| 194 | $envir{formattedOpenDate} = formatDateTime($envir{openDate}); |
96 | $envir{formattedOpenDate} = formatDateTime($envir{openDate}, $ce->{siteDefaults}{timezone}); |
| 195 | $envir{dueDate} = $set->due_date; |
97 | $envir{dueDate} = $set->due_date; |
| 196 | $envir{formattedDueDate} = formatDateTime($envir{dueDate}); |
98 | $envir{formattedDueDate} = formatDateTime($envir{dueDate}, $ce->{siteDefaults}{timezone}); |
|
|
99 | $envir{formatedDueDate} = $envir{formattedDueDate}; # typo in many header files |
| 197 | $envir{answerDate} = $set->answer_date; |
100 | $envir{answerDate} = $set->answer_date; |
| 198 | $envir{formattedAnswerDate} = formatDateTime($envir{answerDate}); |
101 | $envir{formattedAnswerDate} = formatDateTime($envir{answerDate}, $ce->{siteDefaults}{timezone}); |
| 199 | $envir{numOfAttempts} = $problem->num_correct + $problem->num_incorrect; |
102 | $envir{numOfAttempts} = ($problem->num_correct || 0) + ($problem->num_incorrect || 0); |
| 200 | $envir{problemValue} = $problem->value; |
103 | $envir{problemValue} = $problem->value; |
| 201 | $envir{sessionKey} = $key; |
104 | $envir{sessionKey} = $key; |
| 202 | $envir{courseName} = $courseEnv->{courseName}; |
105 | $envir{courseName} = $ce->{courseName}; |
| 203 | |
106 | |
| 204 | # Student Information |
107 | # Student Information |
| 205 | # ADDED: studentID |
108 | # ADDED: studentID |
| 206 | |
109 | |
| 207 | $envir{sectionName} = $user->section; |
110 | $envir{sectionName} = $user->section; |
| 208 | $envir{sectionNumber} = $envir{sectionName}; |
111 | $envir{sectionNumber} = $envir{sectionName}; |
| 209 | $envir{recitationName} = $user->recitation; |
112 | $envir{recitationName} = $user->recitation; |
| 210 | $envir{recitationNumber} = $envir{recitationName}; |
113 | $envir{recitationNumber} = $envir{recitationName}; |
| 211 | $envir{setNumber} = $set->id; |
114 | $envir{setNumber} = $set->set_id; |
| 212 | $envir{studentLogin} = $user->id; |
115 | $envir{studentLogin} = $user->user_id; |
| 213 | $envir{studentName} = $user->first_name . " " . $user->last_name; |
116 | $envir{studentName} = $user->first_name . " " . $user->last_name; |
| 214 | $envir{studentID} = $user->student_id; |
117 | $envir{studentID} = $user->student_id; |
| 215 | |
118 | |
| 216 | # Answer Information |
119 | # Answer Information |
| 217 | # REMOVED: refSubmittedAnswers (alledgedly unused, causes errors) |
120 | # REMOVED: refSubmittedAnswers |
| 218 | |
121 | |
| 219 | $envir{inputs_ref} = $formFields; |
122 | $envir{inputs_ref} = $formFields; |
| 220 | |
123 | |
| 221 | # Default values for evaluating answers |
124 | # External Programs |
|
|
125 | # ADDED: externalLaTeXPath, externalDvipngPath, |
|
|
126 | # externalGif2EpsPath, externalPng2EpsPath |
| 222 | |
127 | |
| 223 | my $ansEvalDefaults = $courseEnv->{pg}->{ansEvalDefaults}; |
128 | $envir{externalTTHPath} = $ce->{externalPrograms}->{tth}; |
| 224 | $envir{$_} = $ansEvalDefaults->{$_} foreach (keys %$ansEvalDefaults); |
129 | $envir{externalLaTeXPath} = $ce->{externalPrograms}->{latex}; |
|
|
130 | $envir{externalDvipngPath} = $ce->{externalPrograms}->{dvipng}; |
|
|
131 | $envir{externalGif2EpsPath} = $ce->{externalPrograms}->{gif2eps}; |
|
|
132 | $envir{externalPng2EpsPath} = $ce->{externalPrograms}->{png2eps}; |
|
|
133 | $envir{externalGif2PngPath} = $ce->{externalPrograms}->{gif2png}; |
| 225 | |
134 | |
| 226 | # Directories and URLs |
135 | # Directories and URLs |
| 227 | # REMOVED: courseName |
136 | # REMOVED: courseName |
|
|
137 | # ADDED: dvipngTempDir |
|
|
138 | # ADDED: jsMathURL |
|
|
139 | # ADDED: asciimathURL |
|
|
140 | # ADDED: macrosPath |
|
|
141 | # REMOVED: macrosDirectory, courseScriptsDirectory |
| 228 | |
142 | |
| 229 | $envir{cgiDirectory} = undef; |
143 | $envir{cgiDirectory} = undef; |
| 230 | $envir{cgiURL} = undef; |
144 | $envir{cgiURL} = undef; |
| 231 | $envir{classDirectory} = undef; |
145 | $envir{classDirectory} = undef; |
| 232 | $envir{courseScriptsDirectory} = $courseEnv->{webworkDirs}->{macros}."/"; |
146 | $envir{macrosPath} = $ce->{pg}->{directories}{macrosPath}; |
| 233 | $envir{htmlDirectory} = $courseEnv->{courseDirs}->{html}."/"; |
147 | $envir{htmlDirectory} = $ce->{courseDirs}->{html}."/"; |
| 234 | $envir{htmlURL} = $courseEnv->{courseURLs}->{html}; |
148 | $envir{htmlURL} = $ce->{courseURLs}->{html}."/"; |
| 235 | $envir{macroDirectory} = $courseEnv->{courseDirs}->{macros}."/"; |
|
|
| 236 | $envir{templateDirectory} = $courseEnv->{courseDirs}->{templates}."/"; |
149 | $envir{templateDirectory} = $ce->{courseDirs}->{templates}."/"; |
| 237 | $envir{tempDirectory} = $courseEnv->{courseDirs}->{html_temp}."/"; |
150 | $envir{tempDirectory} = $ce->{courseDirs}->{html_temp}."/"; |
| 238 | $envir{tempURL} = $courseEnv->{courseURLs}->{html_temp}; |
151 | $envir{tempURL} = $ce->{courseURLs}->{html_temp}."/"; |
| 239 | $envir{scriptDirectory} = undef; |
152 | $envir{scriptDirectory} = undef; |
| 240 | $envir{webworkDocsURL} = $courseEnv->{webworkURLs}->{docs}; |
153 | $envir{webworkDocsURL} = $ce->{webworkURLs}->{docs}."/"; |
|
|
154 | $envir{localHelpURL} = $ce->{webworkURLs}->{local_help}."/"; |
|
|
155 | $envir{jsMathURL} = $ce->{webworkURLs}->{jsMath}; |
|
|
156 | $envir{asciimathURL} = $ce->{webworkURLs}->{asciimath}; |
|
|
157 | |
|
|
158 | # Information for sending mail |
|
|
159 | |
|
|
160 | $envir{mailSmtpServer} = $ce->{mail}->{smtpServer}; |
|
|
161 | $envir{mailSmtpSender} = $ce->{mail}->{smtpSender}; |
|
|
162 | $envir{ALLOW_MAIL_TO} = $ce->{mail}->{allowedRecipients}; |
|
|
163 | |
|
|
164 | # Default values for evaluating answers |
|
|
165 | |
|
|
166 | my $ansEvalDefaults = $ce->{pg}->{ansEvalDefaults}; |
|
|
167 | $envir{$_} = $ansEvalDefaults->{$_} foreach (keys %$ansEvalDefaults); |
|
|
168 | |
|
|
169 | # ---------------------------------------------------------------------- |
|
|
170 | |
|
|
171 | my $basename = "equation-$envir{psvn}.$envir{probNum}"; |
|
|
172 | $basename .= ".$envir{problemSeed}" if $envir{problemSeed}; |
|
|
173 | |
|
|
174 | # to make grabbing these options easier, we'll pull them out now... |
|
|
175 | my %imagesModeOptions = %{$ce->{pg}->{displayModeOptions}->{images}}; |
|
|
176 | |
|
|
177 | # Object for generating equation images |
|
|
178 | $envir{imagegen} = WeBWorK::PG::ImageGenerator->new( |
|
|
179 | tempDir => $ce->{webworkDirs}->{tmp}, # global temp dir |
|
|
180 | latex => $envir{externalLaTeXPath}, |
|
|
181 | dvipng => $envir{externalDvipngPath}, |
|
|
182 | useCache => 1, |
|
|
183 | cacheDir => $ce->{webworkDirs}->{equationCache}, |
|
|
184 | cacheURL => $ce->{webworkURLs}->{equationCache}, |
|
|
185 | cacheDB => $ce->{webworkFiles}->{equationCacheDB}, |
|
|
186 | useMarkers => ($imagesModeOptions{dvipng_align} && $imagesModeOptions{dvipng_align} eq 'mysql'), |
|
|
187 | dvipng_align => $imagesModeOptions{dvipng_align}, |
|
|
188 | dvipng_depth_db => $imagesModeOptions{dvipng_depth_db}, |
|
|
189 | ); |
|
|
190 | |
|
|
191 | # ADDED: jsMath options |
|
|
192 | $envir{jsMath} = {%{$ce->{pg}{displayModeOptions}{jsMath}}}; |
|
|
193 | |
|
|
194 | # Other things... |
|
|
195 | $envir{QUIZ_PREFIX} = $options->{QUIZ_PREFIX}; # used by quizzes |
|
|
196 | $envir{PROBLEM_GRADER_TO_USE} = $ce->{pg}->{options}->{grader}; |
|
|
197 | $envir{PRINT_FILE_NAMES_FOR} = $ce->{pg}->{specialPGEnvironmentVars}->{PRINT_FILE_NAMES_FOR}; |
|
|
198 | |
|
|
199 | # ADDED: __files__ |
|
|
200 | # an array for mapping (eval nnn) to filenames in error messages |
|
|
201 | $envir{__files__} = { |
|
|
202 | root => $ce->{webworkDirs}{root}, # used to shorten filenames |
|
|
203 | pg => $ce->{pg}{directories}{root}, # ditto |
|
|
204 | tmpl => $ce->{courseDirs}{templates}, # ditto |
|
|
205 | }; |
|
|
206 | |
|
|
207 | # variables for interpreting capa problems and other things to be |
|
|
208 | # seen in a pg file |
|
|
209 | my $specialPGEnvironmentVarHash = $ce->{pg}->{specialPGEnvironmentVars}; |
|
|
210 | for my $SPGEV (keys %{$specialPGEnvironmentVarHash}) { |
|
|
211 | $envir{$SPGEV} = $specialPGEnvironmentVarHash->{$SPGEV}; |
|
|
212 | } |
| 241 | |
213 | |
| 242 | return \%envir; |
214 | return \%envir; |
| 243 | } |
215 | } |
| 244 | |
216 | |
| 245 | sub translateDisplayModeNames($) { |
217 | sub translateDisplayModeNames($) { |
| 246 | my $name = shift; |
218 | my $name = shift; |
| 247 | return { |
219 | return DISPLAY_MODES()->{$name}; |
| 248 | plainText => "HTML", |
|
|
| 249 | formattedText => "HTML_tth", |
|
|
| 250 | images => "Latex2HTML" |
|
|
| 251 | }->{$name}; |
|
|
| 252 | } |
220 | } |
| 253 | |
221 | |
| 254 | sub safetyFilter { |
222 | sub oldSafetyFilter { |
| 255 | my $answer = shift; # accepts one answer and checks it |
223 | my $answer = shift; # accepts one answer and checks it |
| 256 | my $submittedAnswer = $answer; |
224 | my $submittedAnswer = $answer; |
| 257 | $answer = '' unless defined $answer; |
225 | $answer = '' unless defined $answer; |
| 258 | my ($errorno); |
226 | my ($errorno); |
| 259 | $answer =~ tr/\000-\037/ /; |
227 | $answer =~ tr/\000-\037/ /; |
| … | |
… | |
| 263 | $errorno = 0; ## don't report blank answer as error |
231 | $errorno = 0; ## don't report blank answer as error |
| 264 | return ($answer,$errorno); |
232 | return ($answer,$errorno); |
| 265 | } |
233 | } |
| 266 | # replace ^ with ** (for exponentiation) |
234 | # replace ^ with ** (for exponentiation) |
| 267 | # $answer =~ s/\^/**/g; |
235 | # $answer =~ s/\^/**/g; |
| 268 | # Return if forbidden characters are found |
236 | # Return if forbidden characters are found |
| 269 | unless ($answer =~ /^[a-zA-Z0-9_\-\+ \t\/@%\*\.\n^\(\)]+$/ ) { |
237 | unless ($answer =~ /^[a-zA-Z0-9_\-\+ \t\/@%\*\.\n^\[\]\(\)\,\|]+$/ ) { |
| 270 | $answer =~ tr/a-zA-Z0-9_\-\+ \t\/@%\*\.\n^\(\)/#/c; |
238 | $answer =~ tr/a-zA-Z0-9_\-\+ \t\/@%\*\.\n^\(\)/#/c; |
| 271 | $errorno = "<BR>There are forbidden characters in your answer: $submittedAnswer<BR>"; |
239 | $errorno = "<BR>There are forbidden characters in your answer: $submittedAnswer<BR>"; |
| 272 | return ($answer,$errorno); |
240 | return ($answer,$errorno); |
| 273 | } |
241 | } |
| 274 | $errorno = 0; |
242 | $errorno = 0; |
| 275 | return($answer, $errorno); |
243 | return($answer, $errorno); |
| 276 | } |
244 | } |
| 277 | |
245 | |
|
|
246 | sub nullSafetyFilter { |
|
|
247 | return shift, 0; # no errors |
|
|
248 | } |
|
|
249 | |
| 278 | 1; |
250 | 1; |
|
|
251 | |
|
|
252 | __END__ |
|
|
253 | |
|
|
254 | =head1 SYNOPSIS |
|
|
255 | |
|
|
256 | $pg = WeBWorK::PG->new( |
|
|
257 | $ce, # a WeBWorK::CourseEnvironment object |
|
|
258 | $user, # a WeBWorK::DB::Record::User object |
|
|
259 | $sessionKey, |
|
|
260 | $set, # a WeBWorK::DB::Record::UserSet object |
|
|
261 | $problem, # a WeBWorK::DB::Record::UserProblem object |
|
|
262 | $psvn, |
|
|
263 | $formFields # in &WeBWorK::Form::Vars format |
|
|
264 | { # translation options |
|
|
265 | displayMode => "images", # (plainText|formattedText|images) |
|
|
266 | showHints => 1, # (0|1) |
|
|
267 | showSolutions => 0, # (0|1) |
|
|
268 | refreshMath2img => 0, # (0|1) |
|
|
269 | processAnswers => 1, # (0|1) |
|
|
270 | }, |
|
|
271 | ); |
|
|
272 | |
|
|
273 | $translator = $pg->{translator}; # WeBWorK::PG::Translator |
|
|
274 | $body = $pg->{body_text}; # text string |
|
|
275 | $header = $pg->{head_text}; # text string |
|
|
276 | $answerHash = $pg->{answers}; # WeBWorK::PG::AnswerHash |
|
|
277 | $result = $pg->{result}; # hash reference |
|
|
278 | $state = $pg->{state}; # hash reference |
|
|
279 | $errors = $pg->{errors}; # text string |
|
|
280 | $warnings = $pg->{warnings}; # text string |
|
|
281 | $flags = $pg->{flags}; # hash reference |
|
|
282 | |
|
|
283 | =head1 DESCRIPTION |
|
|
284 | |
|
|
285 | WeBWorK::PG is a factory for modules which use the WeBWorK::PG API. Notable |
|
|
286 | modules which use this API (and exist) are WeBWorK::PG::Local and |
|
|
287 | WeBWorK::PG::Remote. The course environment key $pg{renderer} is consulted to |
|
|
288 | determine which render to use. |
|
|
289 | |
|
|
290 | =head1 THE WEBWORK::PG API |
|
|
291 | |
|
|
292 | Modules which support this API must implement the following method: |
|
|
293 | |
|
|
294 | =over |
|
|
295 | |
|
|
296 | =item new ENVIRONMENT, USER, KEY, SET, PROBLEM, PSVN, FIELDS, OPTIONS |
|
|
297 | |
|
|
298 | The C<new> method creates a translator, initializes it using the parameters |
|
|
299 | specified, translates a PG file, and processes answers. It returns a reference |
|
|
300 | to a blessed hash containing the results of the translation process. |
|
|
301 | |
|
|
302 | =back |
|
|
303 | |
|
|
304 | =head2 Parameters |
|
|
305 | |
|
|
306 | =over |
|
|
307 | |
|
|
308 | =item ENVIRONMENT |
|
|
309 | |
|
|
310 | a WeBWorK::CourseEnvironment object |
|
|
311 | |
|
|
312 | =item USER |
|
|
313 | |
|
|
314 | a WeBWorK::User object |
|
|
315 | |
|
|
316 | =item KEY |
|
|
317 | |
|
|
318 | the session key of the current session |
|
|
319 | |
|
|
320 | =item SET |
|
|
321 | |
|
|
322 | a WeBWorK::Set object |
|
|
323 | |
|
|
324 | =item PROBLEM |
|
|
325 | |
|
|
326 | a WeBWorK::DB::Record::UserProblem object. The contents of the source_file |
|
|
327 | field can specify a PG file either by absolute path or path relative to the |
|
|
328 | "templates" directory. I<The caller should remove taint from this value before |
|
|
329 | passing!> |
|
|
330 | |
|
|
331 | =item PSVN |
|
|
332 | |
|
|
333 | the problem set version number |
|
|
334 | |
|
|
335 | =item FIELDS |
|
|
336 | |
|
|
337 | a reference to a hash (as returned by &WeBWorK::Form::Vars) containing form |
|
|
338 | fields submitted by a problem processor. The translator will look for fields |
|
|
339 | like "AnSwEr[0-9]" containing submitted student answers. |
|
|
340 | |
|
|
341 | =item OPTIONS |
|
|
342 | |
|
|
343 | a reference to a hash containing the following data: |
|
|
344 | |
|
|
345 | =over |
|
|
346 | |
|
|
347 | =item displayMode |
|
|
348 | |
|
|
349 | one of "plainText", "formattedText", or "images" |
|
|
350 | |
|
|
351 | =item showHints |
|
|
352 | |
|
|
353 | boolean, render hints |
|
|
354 | |
|
|
355 | =item showSolutions |
|
|
356 | |
|
|
357 | boolean, render solutions |
|
|
358 | |
|
|
359 | =item refreshMath2img |
|
|
360 | |
|
|
361 | boolean, force images created by math2img (in "images" mode) to be recreated, |
|
|
362 | even if the PG source has not been updated. FIXME: remove this option. |
|
|
363 | |
|
|
364 | =item processAnswers |
|
|
365 | |
|
|
366 | boolean, call answer evaluators and graders |
|
|
367 | |
|
|
368 | =back |
|
|
369 | |
|
|
370 | =back |
|
|
371 | |
|
|
372 | =head2 RETURN VALUE |
|
|
373 | |
|
|
374 | The C<new> method returns a blessed hash reference containing the following |
|
|
375 | fields. More information can be found in the documentation for |
|
|
376 | WeBWorK::PG::Translator. |
|
|
377 | |
|
|
378 | =over |
|
|
379 | |
|
|
380 | =item translator |
|
|
381 | |
|
|
382 | The WeBWorK::PG::Translator object used to render the problem. |
|
|
383 | |
|
|
384 | =item head_text |
|
|
385 | |
|
|
386 | HTML code for the E<lt>headE<gt> block of an resulting web page. Used for |
|
|
387 | JavaScript features. |
|
|
388 | |
|
|
389 | =item body_text |
|
|
390 | |
|
|
391 | HTML code for the E<lt>bodyE<gt> block of an resulting web page. |
|
|
392 | |
|
|
393 | =item answers |
|
|
394 | |
|
|
395 | An C<AnswerHash> object containing submitted answers, and results of answer |
|
|
396 | evaluation. |
|
|
397 | |
|
|
398 | =item result |
|
|
399 | |
|
|
400 | A hash containing the results of grading the problem. |
|
|
401 | |
|
|
402 | =item state |
|
|
403 | |
|
|
404 | A hash containing the new problem state. |
|
|
405 | |
|
|
406 | =item errors |
|
|
407 | |
|
|
408 | A string containing any errors encountered while rendering the problem. |
|
|
409 | |
|
|
410 | =item warnings |
|
|
411 | |
|
|
412 | A string containing any warnings encountered while rendering the problem. |
|
|
413 | |
|
|
414 | =item flags |
|
|
415 | |
|
|
416 | A hash containing PG_flags (see the Translator docs). |
|
|
417 | |
|
|
418 | =back |
|
|
419 | |
|
|
420 | =head1 METHODS PROVIDED BY THE BASE CLASS |
|
|
421 | |
|
|
422 | The following methods are provided for use by subclasses of WeBWorK::PG. |
|
|
423 | |
|
|
424 | =over |
|
|
425 | |
|
|
426 | =item defineProblemEnvir ENVIRONMENT, USER, KEY, SET, PROBLEM, PSVN, FIELDS, OPTIONS |
|
|
427 | |
|
|
428 | Generate a problem environment hash to pass to the renderer. |
|
|
429 | |
|
|
430 | =item translateDisplayModeNames NAME |
|
|
431 | |
|
|
432 | NAME contains |
|
|
433 | |
|
|
434 | =back |
|
|
435 | |
|
|
436 | =head1 AUTHOR |
|
|
437 | |
|
|
438 | Written by Sam Hathaway, sh002i (at) math.rochester.edu. |
|
|
439 | |
|
|
440 | =cut |