[system] / trunk / webwork-modperl / lib / WeBWorK / PG.pm Repository:
ViewVC logotype

Annotation of /trunk/webwork-modperl/lib/WeBWorK/PG.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2217 - (view) (download) (as text)

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9