[system] / branches / rel-2-4-patches / webwork2 / lib / WeBWorK / PG.pm Repository:
ViewVC logotype

Annotation of /branches/rel-2-4-patches/webwork2/lib/WeBWorK/PG.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2983 - (view) (download) (as text)
Original Path: trunk/webwork2/lib/WeBWorK/PG.pm

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9