| … | |
… | |
| 20 | |
20 | |
| 21 | sub new { |
21 | sub new { |
| 22 | my $invocant = shift; |
22 | my $invocant = shift; |
| 23 | my $class = ref($invocant) || $invocant; |
23 | my $class = ref($invocant) || $invocant; |
| 24 | my ( |
24 | my ( |
| 25 | $courseEnv, |
25 | $ce, |
| 26 | $user, |
26 | $user, |
| 27 | $key, |
27 | $key, |
| 28 | $set, |
28 | $set, |
| 29 | $problem, |
29 | $problem, |
| 30 | $psvn, |
30 | $psvn, |
| … | |
… | |
| 33 | # translator, such as whether to show |
33 | # translator, such as whether to show |
| 34 | # hints and the display mode to use |
34 | # hints and the display mode to use |
| 35 | ) = @_; |
35 | ) = @_; |
| 36 | |
36 | |
| 37 | # write timing log entry |
37 | # write timing log entry |
| 38 | writeTimingLogEntry($courseEnv, "WeBWorK::PG::new", |
38 | writeTimingLogEntry($ce, "WeBWorK::PG::new", |
| 39 | "user=".$user->user_id.",problem=".$courseEnv->{courseName}."/".$set->set_id."/".$problem->problem_id.",mode=".$translationOptions->{displayMode}, |
39 | "user=".$user->user_id.",problem=".$ce->{courseName}."/".$set->set_id."/".$problem->problem_id.",mode=".$translationOptions->{displayMode}, |
| 40 | "begin"); |
40 | "begin"); |
| 41 | |
41 | |
| 42 | # install a local warn handler to collect warnings |
42 | # install a local warn handler to collect warnings |
| 43 | my $warnings = ""; |
43 | my $warnings = ""; |
| 44 | local $SIG{__WARN__} = sub { $warnings .= shift } |
44 | local $SIG{__WARN__} = sub { $warnings .= shift } |
| 45 | if $courseEnv->{pg}->{options}->{catchWarnings}; |
45 | if $ce->{pg}->{options}->{catchWarnings}; |
| 46 | |
46 | |
| 47 | # create a Translator |
47 | # create a Translator |
| 48 | #warn "PG: creating a Translator\n"; |
48 | #warn "PG: creating a Translator\n"; |
| 49 | my $translator = WeBWorK::PG::Translator->new; |
49 | my $translator = WeBWorK::PG::Translator->new; |
| 50 | |
50 | |
| 51 | # set the directory hash |
51 | # set the directory hash |
| 52 | #warn "PG: setting the directory hash\n"; |
52 | #warn "PG: setting the directory hash\n"; |
| 53 | $translator->rh_directories({ |
53 | $translator->rh_directories({ |
| 54 | courseScriptsDirectory => $courseEnv->{webworkDirs}->{macros}, |
54 | courseScriptsDirectory => $ce->{pg}->{directories}->{macros}, |
| 55 | macroDirectory => $courseEnv->{courseDirs}->{macros}, |
55 | macroDirectory => $ce->{courseDirs}->{macros}, |
| 56 | templateDirectory => $courseEnv->{courseDirs}->{templates}, |
56 | templateDirectory => $ce->{courseDirs}->{templates}, |
| 57 | tempDirectory => $courseEnv->{courseDirs}->{html_temp}, |
57 | tempDirectory => $ce->{courseDirs}->{html_temp}, |
| 58 | }); |
58 | }); |
| 59 | |
59 | |
| 60 | # evaluate modules and "extra packages" |
60 | # evaluate modules and "extra packages" |
| 61 | #warn "PG: evaluating modules and \"extra packages\"\n"; |
61 | #warn "PG: evaluating modules and \"extra packages\"\n"; |
| 62 | my @modules = @{ $courseEnv->{pg}->{modules} }; |
62 | my @modules = @{ $ce->{pg}->{modules} }; |
| 63 | foreach my $module_packages_ref (@modules) { |
63 | foreach my $module_packages_ref (@modules) { |
| 64 | my ($module, @extra_packages) = @$module_packages_ref; |
64 | my ($module, @extra_packages) = @$module_packages_ref; |
| 65 | # the first item is the main package |
65 | # the first item is the main package |
| 66 | $translator->evaluate_modules($module); |
66 | $translator->evaluate_modules($module); |
| 67 | # the remaining items are "extra" packages |
67 | # the remaining items are "extra" packages |
| … | |
… | |
| 69 | } |
69 | } |
| 70 | |
70 | |
| 71 | # set the environment (from defineProblemEnvir) |
71 | # set the environment (from defineProblemEnvir) |
| 72 | #warn "PG: setting the environment (from defineProblemEnvir)\n"; |
72 | #warn "PG: setting the environment (from defineProblemEnvir)\n"; |
| 73 | my $envir = defineProblemEnvir( |
73 | my $envir = defineProblemEnvir( |
| 74 | $courseEnv, |
74 | $ce, |
| 75 | $user, |
75 | $user, |
| 76 | $key, |
76 | $key, |
| 77 | $set, |
77 | $set, |
| 78 | $problem, |
78 | $problem, |
| 79 | $psvn, |
79 | $psvn, |
| … | |
… | |
| 89 | # load IO.pl, PG.pl, and dangerousMacros.pl using unrestricted_load |
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 |
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. |
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"; |
92 | #warn "PG: loading IO.pl, PG.pl, and dangerousMacros.pl using unrestricted_load\n"; |
| 93 | foreach (qw(IO.pl PG.pl dangerousMacros.pl)) { |
93 | foreach (qw(IO.pl PG.pl dangerousMacros.pl)) { |
| 94 | my $macroPath = $courseEnv->{webworkDirs}->{macros} . "/$_"; |
94 | my $macroPath = $ce->{pg}->{directories}->{macros} . "/$_"; |
| 95 | my $err = $translator->unrestricted_load($macroPath); |
95 | my $err = $translator->unrestricted_load($macroPath); |
| 96 | warn "Error while loading $macroPath: $err" if $err; |
96 | warn "Error while loading $macroPath: $err" if $err; |
| 97 | } |
97 | } |
| 98 | |
98 | |
| 99 | # set the opcode mask (using default values) |
99 | # set the opcode mask (using default values) |
| … | |
… | |
| 101 | $translator->set_mask(); |
101 | $translator->set_mask(); |
| 102 | |
102 | |
| 103 | # store the problem source |
103 | # store the problem source |
| 104 | #warn "PG: storing the problem source\n"; |
104 | #warn "PG: storing the problem source\n"; |
| 105 | my $sourceFile = $problem->source_file; |
105 | my $sourceFile = $problem->source_file; |
| 106 | $sourceFile = $courseEnv->{courseDirs}->{templates}."/".$sourceFile |
106 | $sourceFile = $ce->{courseDirs}->{templates}."/".$sourceFile |
| 107 | unless ($sourceFile =~ /^\//); |
107 | unless ($sourceFile =~ /^\//); |
| 108 | eval { $translator->source_string(readFile($sourceFile)) }; |
108 | eval { $translator->source_string(readFile($sourceFile)) }; |
| 109 | if ($@) { |
109 | if ($@) { |
| 110 | # well, we couldn't get the problem source, for some reason. |
110 | # well, we couldn't get the problem source, for some reason. |
| 111 | return bless { |
111 | return bless { |
| … | |
… | |
| 127 | # install a safety filter (&safetyFilter) |
127 | # install a safety filter (&safetyFilter) |
| 128 | #warn "PG: installing a safety filter\n"; |
128 | #warn "PG: installing a safety filter\n"; |
| 129 | $translator->rf_safety_filter(\&safetyFilter); |
129 | $translator->rf_safety_filter(\&safetyFilter); |
| 130 | |
130 | |
| 131 | # write timing log entry -- the translator is now all set up |
131 | # write timing log entry -- the translator is now all set up |
| 132 | writeTimingLogEntry($courseEnv, "WeBWorK::PG::new", |
132 | writeTimingLogEntry($ce, "WeBWorK::PG::new", |
| 133 | "initialized", |
133 | "initialized", |
| 134 | "intermediate"); |
134 | "intermediate"); |
| 135 | |
135 | |
| 136 | # translate the PG source into text |
136 | # translate the PG source into text |
| 137 | #warn "PG: translating the PG source into text\n"; |
137 | #warn "PG: translating the PG source into text\n"; |
| … | |
… | |
| 147 | } |
147 | } |
| 148 | |
148 | |
| 149 | # HTML_dpng, on the other hand, uses an ImageGenerator. We have to |
149 | # HTML_dpng, on the other hand, uses an ImageGenerator. We have to |
| 150 | # render the queued equations. |
150 | # render the queued equations. |
| 151 | if ($envir->{imagegen}) { |
151 | if ($envir->{imagegen}) { |
| 152 | my $sourceFile = $courseEnv->{courseDirs}->{templates} . "/" . $problem->source_file; |
152 | my $sourceFile = $ce->{courseDirs}->{templates} . "/" . $problem->source_file; |
| 153 | my %mtimeOption = -e $sourceFile |
153 | my %mtimeOption = -e $sourceFile |
| 154 | ? (mtime => (stat $sourceFile)[9]) |
154 | ? (mtime => (stat $sourceFile)[9]) |
| 155 | : (); |
155 | : (); |
| 156 | |
156 | |
| 157 | $envir->{imagegen}->render( |
157 | $envir->{imagegen}->render( |
| … | |
… | |
| 187 | # or fall back on the default from the course environment. |
187 | # or fall back on the default from the course environment. |
| 188 | # (two magic strings are accepted, to avoid having to |
188 | # (two magic strings are accepted, to avoid having to |
| 189 | # reference code when it would be difficult.) |
189 | # reference code when it would be difficult.) |
| 190 | #warn "PG: installing a grader\n"; |
190 | #warn "PG: installing a grader\n"; |
| 191 | my $grader = $translator->rh_flags->{PROBLEM_GRADER_TO_USE} |
191 | my $grader = $translator->rh_flags->{PROBLEM_GRADER_TO_USE} |
| 192 | || $courseEnv->{pg}->{options}->{grader}; |
192 | || $ce->{pg}->{options}->{grader}; |
| 193 | $grader = $translator->rf_std_problem_grader |
193 | $grader = $translator->rf_std_problem_grader |
| 194 | if $grader eq "std_problem_grader"; |
194 | if $grader eq "std_problem_grader"; |
| 195 | $grader = $translator->rf_avg_problem_grader |
195 | $grader = $translator->rf_avg_problem_grader |
| 196 | if $grader eq "avg_problem_grader"; |
196 | if $grader eq "avg_problem_grader"; |
| 197 | die "Problem grader $grader is not a CODE reference." |
197 | die "Problem grader $grader is not a CODE reference." |
| … | |
… | |
| 206 | ); |
206 | ); |
| 207 | |
207 | |
| 208 | } |
208 | } |
| 209 | |
209 | |
| 210 | # write timing log entry |
210 | # write timing log entry |
| 211 | writeTimingLogEntry($courseEnv, "WeBWorK::PG::new", "", "end"); |
211 | writeTimingLogEntry($ce, "WeBWorK::PG::new", "", "end"); |
| 212 | |
212 | |
| 213 | # return an object which contains the translator and the results of |
213 | # return an object which contains the translator and the results of |
| 214 | # the translation process. this is DIFFERENT from the "format expected |
214 | # the translation process. this is DIFFERENT from the "format expected |
| 215 | # by Webwork.pm (and I believe processProblem8, but check.)" |
215 | # by Webwork.pm (and I believe processProblem8, but check.)" |
| 216 | return bless { |
216 | return bless { |
| … | |
… | |
| 228 | |
228 | |
| 229 | # ----- |
229 | # ----- |
| 230 | |
230 | |
| 231 | sub defineProblemEnvir { |
231 | sub defineProblemEnvir { |
| 232 | my ( |
232 | my ( |
| 233 | $courseEnv, |
233 | $ce, |
| 234 | $user, |
234 | $user, |
| 235 | $key, |
235 | $key, |
| 236 | $set, |
236 | $set, |
| 237 | $problem, |
237 | $problem, |
| 238 | $psvn, |
238 | $psvn, |
| … | |
… | |
| 279 | $envir{answerDate} = $set->answer_date; |
279 | $envir{answerDate} = $set->answer_date; |
| 280 | $envir{formattedAnswerDate} = formatDateTime($envir{answerDate}); |
280 | $envir{formattedAnswerDate} = formatDateTime($envir{answerDate}); |
| 281 | $envir{numOfAttempts} = ($problem->num_correct || 0) + ($problem->num_incorrect || 0); |
281 | $envir{numOfAttempts} = ($problem->num_correct || 0) + ($problem->num_incorrect || 0); |
| 282 | $envir{problemValue} = $problem->value; |
282 | $envir{problemValue} = $problem->value; |
| 283 | $envir{sessionKey} = $key; |
283 | $envir{sessionKey} = $key; |
| 284 | $envir{courseName} = $courseEnv->{courseName}; |
284 | $envir{courseName} = $ce->{courseName}; |
| 285 | |
285 | |
| 286 | # Student Information |
286 | # Student Information |
| 287 | # ADDED: studentID |
287 | # ADDED: studentID |
| 288 | |
288 | |
| 289 | $envir{sectionName} = $user->section; |
289 | $envir{sectionName} = $user->section; |
| … | |
… | |
| 302 | |
302 | |
| 303 | # External Programs |
303 | # External Programs |
| 304 | # ADDED: externalLaTeXPath, externalDvipngPath, |
304 | # ADDED: externalLaTeXPath, externalDvipngPath, |
| 305 | # externalGif2EpsPath, externalPng2EpsPath |
305 | # externalGif2EpsPath, externalPng2EpsPath |
| 306 | |
306 | |
| 307 | $envir{externalTTHPath} = $courseEnv->{externalPrograms}->{tth}; |
307 | $envir{externalTTHPath} = $ce->{externalPrograms}->{tth}; |
| 308 | $envir{externalLaTeXPath} = $courseEnv->{externalPrograms}->{latex}; |
308 | $envir{externalLaTeXPath} = $ce->{externalPrograms}->{latex}; |
| 309 | $envir{externalDvipngPath} = $courseEnv->{externalPrograms}->{dvipng}; |
309 | $envir{externalDvipngPath} = $ce->{externalPrograms}->{dvipng}; |
| 310 | $envir{externalGif2EpsPath} = $courseEnv->{externalPrograms}->{gif2eps}; |
310 | $envir{externalGif2EpsPath} = $ce->{externalPrograms}->{gif2eps}; |
| 311 | $envir{externalPng2EpsPath} = $courseEnv->{externalPrograms}->{png2eps}; |
311 | $envir{externalPng2EpsPath} = $ce->{externalPrograms}->{png2eps}; |
| 312 | $envir{externalGif2PngPath} = $courseEnv->{externalPrograms}->{gif2png}; |
312 | $envir{externalGif2PngPath} = $ce->{externalPrograms}->{gif2png}; |
| 313 | |
313 | |
| 314 | # Directories and URLs |
314 | # Directories and URLs |
| 315 | # REMOVED: courseName |
315 | # REMOVED: courseName |
| 316 | # ADDED: dvipngTempDir |
316 | # ADDED: dvipngTempDir |
| 317 | |
317 | |
| 318 | $envir{cgiDirectory} = undef; |
318 | $envir{cgiDirectory} = undef; |
| 319 | $envir{cgiURL} = undef; |
319 | $envir{cgiURL} = undef; |
| 320 | $envir{classDirectory} = undef; |
320 | $envir{classDirectory} = undef; |
| 321 | $envir{courseScriptsDirectory} = $courseEnv->{webworkDirs}->{macros}."/"; |
321 | $envir{courseScriptsDirectory} = $ce->{pg}->{directories}->{macros}."/"; |
| 322 | $envir{htmlDirectory} = $courseEnv->{courseDirs}->{html}."/"; |
322 | $envir{htmlDirectory} = $ce->{courseDirs}->{html}."/"; |
| 323 | $envir{htmlURL} = $courseEnv->{courseURLs}->{html}."/"; |
323 | $envir{htmlURL} = $ce->{courseURLs}->{html}."/"; |
| 324 | $envir{macroDirectory} = $courseEnv->{courseDirs}->{macros}."/"; |
324 | $envir{macroDirectory} = $ce->{courseDirs}->{macros}."/"; |
| 325 | $envir{templateDirectory} = $courseEnv->{courseDirs}->{templates}."/"; |
325 | $envir{templateDirectory} = $ce->{courseDirs}->{templates}."/"; |
| 326 | $envir{tempDirectory} = $courseEnv->{courseDirs}->{html_temp}."/"; |
326 | $envir{tempDirectory} = $ce->{courseDirs}->{html_temp}."/"; |
| 327 | $envir{tempURL} = $courseEnv->{courseURLs}->{html_temp}."/"; |
327 | $envir{tempURL} = $ce->{courseURLs}->{html_temp}."/"; |
| 328 | $envir{scriptDirectory} = undef; |
328 | $envir{scriptDirectory} = undef; |
| 329 | $envir{webworkDocsURL} = $courseEnv->{webworkURLs}->{docs}."/"; |
329 | $envir{webworkDocsURL} = $ce->{webworkURLs}->{docs}."/"; |
| 330 | # FIXME: this is HTML_img mode-specific |
330 | # FIXME: this is HTML_img mode-specific |
| 331 | #$envir{dvipngTempDir} = $options->{displayMode} eq 'images' |
331 | #$envir{dvipngTempDir} = $options->{displayMode} eq 'images' |
| 332 | # ? makeTempDirectory($envir{tempDirectory}, "webwork-dvipng") |
332 | # ? makeTempDirectory($envir{tempDirectory}, "webwork-dvipng") |
| 333 | # : undef; |
333 | # : undef; |
| 334 | |
334 | |
| 335 | # Information for sending mail |
335 | # Information for sending mail |
| 336 | |
336 | |
| 337 | $envir{mailSmtpServer} = $courseEnv->{mail}->{smtpServer}; |
337 | $envir{mailSmtpServer} = $ce->{mail}->{smtpServer}; |
| 338 | $envir{mailSmtpSender} = $courseEnv->{mail}->{smtpSender}; |
338 | $envir{mailSmtpSender} = $ce->{mail}->{smtpSender}; |
| 339 | $envir{ALLOW_MAIL_TO} = $courseEnv->{mail}->{allowedRecipients}; |
339 | $envir{ALLOW_MAIL_TO} = $ce->{mail}->{allowedRecipients}; |
| 340 | |
340 | |
| 341 | # Default values for evaluating answers |
341 | # Default values for evaluating answers |
| 342 | |
342 | |
| 343 | my $ansEvalDefaults = $courseEnv->{pg}->{ansEvalDefaults}; |
343 | my $ansEvalDefaults = $ce->{pg}->{ansEvalDefaults}; |
| 344 | $envir{$_} = $ansEvalDefaults->{$_} foreach (keys %$ansEvalDefaults); |
344 | $envir{$_} = $ansEvalDefaults->{$_} foreach (keys %$ansEvalDefaults); |
| 345 | |
345 | |
| 346 | # ---------------------------------------------------------------------- |
346 | # ---------------------------------------------------------------------- |
| 347 | |
347 | |
| 348 | my $basename = "equation-$envir{psvn}.$envir{probNum}"; |
348 | my $basename = "equation-$envir{psvn}.$envir{probNum}"; |
| 349 | $basename .= ".$envir{problemSeed}" if $envir{problemSeed}; |
349 | $basename .= ".$envir{problemSeed}" if $envir{problemSeed}; |
| 350 | |
350 | |
| 351 | # Object for generating equation images |
351 | # Object for generating equation images |
| 352 | $envir{imagegen} = WeBWorK::PG::ImageGenerator->new( |
352 | $envir{imagegen} = WeBWorK::PG::ImageGenerator->new( |
| 353 | tempDir => $courseEnv->{webworkDirs}->{tmp}, # global temp dir |
353 | tempDir => $ce->{webworkDirs}->{tmp}, # global temp dir |
| 354 | dir => $envir{tempDirectory}, |
354 | dir => $envir{tempDirectory}, |
| 355 | url => $envir{tempURL}, |
355 | url => $envir{tempURL}, |
| 356 | basename => $basename, |
356 | basename => $basename, |
| 357 | latex => $envir{externalLaTeXPath}, |
357 | latex => $envir{externalLaTeXPath}, |
| 358 | dvipng => $envir{externalDvipngPath}, |
358 | dvipng => $envir{externalDvipngPath}, |
| 359 | ); |
359 | ); |
| 360 | |
360 | |
| 361 | # Other things... |
361 | # Other things... |
| 362 | $envir{QUIZ_PREFIX} = $options->{QUIZ_PREFIX}; # used by quizzes |
362 | $envir{QUIZ_PREFIX} = $options->{QUIZ_PREFIX}; # used by quizzes |
| 363 | $envir{PROBLEM_GRADER_TO_USE} = $courseEnv->{pg}->{options}->{grader}; |
363 | $envir{PROBLEM_GRADER_TO_USE} = $ce->{pg}->{options}->{grader}; |
| 364 | $envir{PRINT_FILE_NAMES_FOR} = $courseEnv->{pg}->{specialPGEnvironmentVars}->{PRINT_FILE_NAMES_FOR}; |
364 | $envir{PRINT_FILE_NAMES_FOR} = $ce->{pg}->{specialPGEnvironmentVars}->{PRINT_FILE_NAMES_FOR}; |
| 365 | |
365 | |
| 366 | # variables for interpreting capa problems. |
366 | # variables for interpreting capa problems. |
| 367 | $envir{CAPA_Tools} = $courseEnv->{pg}->{specialPGEnvironmentVars}->{CAPA_Tools}; |
367 | $envir{CAPA_Tools} = $ce->{pg}->{specialPGEnvironmentVars}->{CAPA_Tools}; |
| 368 | $envir{CAPA_MCTools} = $courseEnv->{pg}->{specialPGEnvironmentVars}->{CAPA_MCTools}; |
368 | $envir{CAPA_MCTools} = $ce->{pg}->{specialPGEnvironmentVars}->{CAPA_MCTools}; |
| 369 | $envir{CAPA_Graphics_URL} = $courseEnv->{pg}->{specialPGEnvironmentVars}->{CAPA_Graphics_URL}; |
369 | $envir{CAPA_Graphics_URL} = $ce->{pg}->{specialPGEnvironmentVars}->{CAPA_Graphics_URL}; |
| 370 | $envir{CAPA_GraphicsDirectory} = $courseEnv->{pg}->{specialPGEnvironmentVars}->{CAPA_GraphicsDirectory}; |
370 | $envir{CAPA_GraphicsDirectory} = $ce->{pg}->{specialPGEnvironmentVars}->{CAPA_GraphicsDirectory}; |
| 371 | |
371 | |
| 372 | return \%envir; |
372 | return \%envir; |
| 373 | } |
373 | } |
| 374 | |
374 | |
| 375 | sub translateDisplayModeNames($) { |
375 | sub translateDisplayModeNames($) { |
| … | |
… | |
| 411 | __END__ |
411 | __END__ |
| 412 | |
412 | |
| 413 | =head1 SYNOPSIS |
413 | =head1 SYNOPSIS |
| 414 | |
414 | |
| 415 | $pg = WeBWorK::PG->new( |
415 | $pg = WeBWorK::PG->new( |
| 416 | $courseEnv, # a WeBWorK::CourseEnvironment object |
416 | $ce, # a WeBWorK::CourseEnvironment object |
| 417 | $user, # a WeBWorK::DB::Record::User object |
417 | $user, # a WeBWorK::DB::Record::User object |
| 418 | $sessionKey, |
418 | $sessionKey, |
| 419 | $set, # a WeBWorK::DB::Record::UserSet object |
419 | $set, # a WeBWorK::DB::Record::UserSet object |
| 420 | $problem, # a WeBWorK::DB::Record::UserProblem object |
420 | $problem, # a WeBWorK::DB::Record::UserProblem object |
| 421 | $psvn, |
421 | $psvn, |