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