Parent Directory
|
Revision Log
Changing cosmetics in code (PG.pm) and the listing of links (ContentGenerator)
1 ################################################################################ 2 # WeBWorK Online Homework Delivery System 3 # Copyright © 2000-2003 The WeBWorK Project, http://openwebwork.sf.net/ 4 # $CVSHeader: webwork-modperl/lib/WeBWorK/PG.pm,v 1.52 2004/05/27 01:34:22 jj 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 17 package WeBWorK::PG; 18 19 =head1 NAME 20 21 WeBWorK::PG - Invoke one of several PG rendering methods using an easy-to-use 22 API. 23 24 =cut 25 26 use strict; 27 use warnings; 28 use WeBWorK::PG::ImageGenerator; 29 use WeBWorK::Utils qw(runtime_use formatDateTime makeTempDirectory); 30 31 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 asciimath => "HTML_asciimath", 39 }; 40 41 use constant DISPLAY_MODE_FAILOVER => { 42 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 # 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 sub new { 56 shift; # throw away invocant -- we don't need it 57 my ($ce, $user, $key, $set, $problem, $psvn, $formFields, 58 $translationOptions) = @_; 59 60 my $renderer = $ce->{pg}->{renderer}; 61 62 runtime_use $renderer; 63 64 return $renderer->new(@_); 65 } 66 67 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 # ADDED: displayModeFailover, displayHintsQ, displaySolutionsQ, 90 # refreshMath2img, texDisposition 91 92 $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 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 # ADDED: jsMathURL 154 # ADDED: asciimathURL 155 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 $envir{localHelpURL} = $ce->{webworkURLs}->{local_help}."/"; 169 $envir{jsMathURL} = $ce->{webworkURLs}->{jsMath}; 170 $envir{asciimathURL} = $ce->{webworkURLs}->{asciimath}; 171 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 # 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 211 return \%envir; 212 } 213 214 sub translateDisplayModeNames($) { 215 my $name = shift; 216 return DISPLAY_MODES()->{$name}; 217 } 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 1; 248 249 __END__ 250 251 =head1 SYNOPSIS 252 253 $pg = WeBWorK::PG->new( 254 $ce, # a WeBWorK::CourseEnvironment object 255 $user, # a WeBWorK::DB::Record::User object 256 $sessionKey, 257 $set, # a WeBWorK::DB::Record::UserSet object 258 $problem, # a WeBWorK::DB::Record::UserProblem object 259 $psvn, 260 $formFields # in &WeBWorK::Form::Vars format 261 { # 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 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 287 =head1 THE WEBWORK::PG API 288 289 Modules which support this API must implement the following method: 290 291 =over 292 293 =item new ENVIRONMENT, USER, KEY, SET, PROBLEM, PSVN, FIELDS, OPTIONS 294 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 a WeBWorK::User object 312 313 =item KEY 314 315 the session key of the current session 316 317 =item SET 318 319 a WeBWorK::Set object 320 321 =item PROBLEM 322 323 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 328 =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 =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 even if the PG source has not been updated. FIXME: remove this option. 360 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 =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 =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 |