[system] / trunk / webwork2 / lib / WeBWorK / PG.pm Repository:
ViewVC logotype

View of /trunk/webwork2/lib/WeBWorK/PG.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2440 - (download) (as text) (annotate)
Sat Jul 3 17:29:32 2004 UTC (8 years, 10 months ago) by sh002i
File size: 12936 byte(s)
get dvipng settings from new place in CE

    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.55 2004/06/26 21:07:20 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 sub new {
   42   shift; # throw away invocant -- we don't need it
   43   my ($ce, $user, $key, $set, $problem, $psvn, $formFields,
   44     $translationOptions) = @_;
   45 
   46   my $renderer = $ce->{pg}->{renderer};
   47 
   48   runtime_use $renderer;
   49 
   50   return $renderer->new(@_);
   51 }
   52 
   53 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   # ADDED: displayModeFailover, displayHintsQ, displaySolutionsQ,
   76   #        refreshMath2img, texDisposition
   77 
   78   $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 
   92   # Problem Information
   93   # ADDED: courseName, formatedDueDate
   94 
   95   $envir{openDate}            = $set->open_date;
   96   $envir{formattedOpenDate}   = formatDateTime($envir{openDate});
   97   $envir{dueDate}             = $set->due_date;
   98   $envir{formattedDueDate}    = formatDateTime($envir{dueDate});
   99   $envir{formatedDueDate}     = $envir{formattedDueDate}; # typo in many header files
  100   $envir{answerDate}          = $set->answer_date;
  101   $envir{formattedAnswerDate} = formatDateTime($envir{answerDate});
  102   $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   # ADDED: jsMathURL
  139   # ADDED: asciimathURL
  140 
  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   $envir{localHelpURL}           = $ce->{webworkURLs}->{local_help}."/";
  154   $envir{jsMathURL}            = $ce->{webworkURLs}->{jsMath};
  155   $envir{asciimathURL}         = $ce->{webworkURLs}->{asciimath};
  156 
  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 
  173   # to make grabbing these options easier, we'll pull them out now...
  174   my %imagesModeOptions = %{$ce->{pg}->{displayModeOptions}->{images}};
  175 
  176   # 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     useMarkers      => ($imagesModeOptions{dvipng_align} && $imagesModeOptions{dvipng_align} eq 'mysql'),
  186     dvipng_align    => $imagesModeOptions{dvipng_align},
  187     dvipng_depth_db => $imagesModeOptions{dvipng_depth_db},
  188   );
  189 
  190   # Other things...
  191   $envir{QUIZ_PREFIX}              = $options->{QUIZ_PREFIX}; # used by quizzes
  192   $envir{PROBLEM_GRADER_TO_USE}    = $ce->{pg}->{options}->{grader};
  193   $envir{PRINT_FILE_NAMES_FOR}     = $ce->{pg}->{specialPGEnvironmentVars}->{PRINT_FILE_NAMES_FOR};
  194 
  195   # variables for interpreting capa problems and other things to be
  196         # seen in a pg file
  197   my $specialPGEnvironmentVarHash = $ce->{pg}->{specialPGEnvironmentVars};
  198   for my $SPGEV (keys %{$specialPGEnvironmentVarHash}) {
  199     $envir{$SPGEV} = $specialPGEnvironmentVarHash->{$SPGEV};
  200   }
  201 
  202   return \%envir;
  203 }
  204 
  205 sub translateDisplayModeNames($) {
  206   my $name = shift;
  207   return DISPLAY_MODES()->{$name};
  208 }
  209 
  210 sub oldSafetyFilter {
  211   my $answer = shift; # accepts one answer and checks it
  212   my $submittedAnswer = $answer;
  213   $answer = '' unless defined $answer;
  214   my ($errorno);
  215   $answer =~ tr/\000-\037/ /;
  216   # Return if answer field is empty
  217   unless ($answer =~ /\S/) {
  218     #$errorno = "<BR>No answer was submitted.";
  219     $errorno = 0;  ## don't report blank answer as error
  220     return ($answer,$errorno);
  221   }
  222   # replace ^ with **    (for exponentiation)
  223   # $answer =~ s/\^/**/g;
  224   # Return if forbidden characters are found
  225   unless ($answer =~ /^[a-zA-Z0-9_\-\+ \t\/@%\*\.\n^\[\]\(\)\,\|]+$/ )  {
  226     $answer =~ tr/a-zA-Z0-9_\-\+ \t\/@%\*\.\n^\(\)/#/c;
  227     $errorno = "<BR>There are forbidden characters in your answer: $submittedAnswer<BR>";
  228     return ($answer,$errorno);
  229   }
  230   $errorno = 0;
  231   return($answer, $errorno);
  232 }
  233 
  234 sub nullSafetyFilter {
  235   return shift, 0; # no errors
  236 }
  237 
  238 1;
  239 
  240 __END__
  241 
  242 =head1 SYNOPSIS
  243 
  244  $pg = WeBWorK::PG->new(
  245    $ce,         # a WeBWorK::CourseEnvironment object
  246    $user,       # a WeBWorK::DB::Record::User object
  247    $sessionKey,
  248    $set,        # a WeBWorK::DB::Record::UserSet object
  249    $problem,    # a WeBWorK::DB::Record::UserProblem object
  250    $psvn,
  251    $formFields  # in &WeBWorK::Form::Vars format
  252    { # translation options
  253      displayMode     => "images", # (plainText|formattedText|images)
  254      showHints       => 1,        # (0|1)
  255      showSolutions   => 0,        # (0|1)
  256      refreshMath2img => 0,        # (0|1)
  257      processAnswers  => 1,        # (0|1)
  258    },
  259  );
  260 
  261  $translator = $pg->{translator}; # WeBWorK::PG::Translator
  262  $body       = $pg->{body_text};  # text string
  263  $header     = $pg->{head_text};  # text string
  264  $answerHash = $pg->{answers};    # WeBWorK::PG::AnswerHash
  265  $result     = $pg->{result};     # hash reference
  266  $state      = $pg->{state};      # hash reference
  267  $errors     = $pg->{errors};     # text string
  268  $warnings   = $pg->{warnings};   # text string
  269  $flags      = $pg->{flags};      # hash reference
  270 
  271 =head1 DESCRIPTION
  272 
  273 WeBWorK::PG is a factory for modules which use the WeBWorK::PG API. Notable
  274 modules which use this API (and exist) are WeBWorK::PG::Local and
  275 WeBWorK::PG::Remote. The course environment key $pg{renderer} is consulted to
  276 determine which render to use.
  277 
  278 =head1 THE WEBWORK::PG API
  279 
  280 Modules which support this API must implement the following method:
  281 
  282 =over
  283 
  284 =item new ENVIRONMENT, USER, KEY, SET, PROBLEM, PSVN, FIELDS, OPTIONS
  285 
  286 The C<new> method creates a translator, initializes it using the parameters
  287 specified, translates a PG file, and processes answers. It returns a reference
  288 to a blessed hash containing the results of the translation process.
  289 
  290 =back
  291 
  292 =head2 Parameters
  293 
  294 =over
  295 
  296 =item ENVIRONMENT
  297 
  298 a WeBWorK::CourseEnvironment object
  299 
  300 =item USER
  301 
  302 a WeBWorK::User object
  303 
  304 =item KEY
  305 
  306 the session key of the current session
  307 
  308 =item SET
  309 
  310 a WeBWorK::Set object
  311 
  312 =item PROBLEM
  313 
  314 a WeBWorK::DB::Record::UserProblem object. The contents of the source_file
  315 field can specify a PG file either by absolute path or path relative to the
  316 "templates" directory. I<The caller should remove taint from this value before
  317 passing!>
  318 
  319 =item PSVN
  320 
  321 the problem set version number
  322 
  323 =item FIELDS
  324 
  325 a reference to a hash (as returned by &WeBWorK::Form::Vars) containing form
  326 fields submitted by a problem processor. The translator will look for fields
  327 like "AnSwEr[0-9]" containing submitted student answers.
  328 
  329 =item OPTIONS
  330 
  331 a reference to a hash containing the following data:
  332 
  333 =over
  334 
  335 =item displayMode
  336 
  337 one of "plainText", "formattedText", or "images"
  338 
  339 =item showHints
  340 
  341 boolean, render hints
  342 
  343 =item showSolutions
  344 
  345 boolean, render solutions
  346 
  347 =item refreshMath2img
  348 
  349 boolean, force images created by math2img (in "images" mode) to be recreated,
  350 even if the PG source has not been updated. FIXME: remove this option.
  351 
  352 =item processAnswers
  353 
  354 boolean, call answer evaluators and graders
  355 
  356 =back
  357 
  358 =back
  359 
  360 =head2 RETURN VALUE
  361 
  362 The C<new> method returns a blessed hash reference containing the following
  363 fields. More information can be found in the documentation for
  364 WeBWorK::PG::Translator.
  365 
  366 =over
  367 
  368 =item translator
  369 
  370 The WeBWorK::PG::Translator object used to render the problem.
  371 
  372 =item head_text
  373 
  374 HTML code for the E<lt>headE<gt> block of an resulting web page. Used for
  375 JavaScript features.
  376 
  377 =item body_text
  378 
  379 HTML code for the E<lt>bodyE<gt> block of an resulting web page.
  380 
  381 =item answers
  382 
  383 An C<AnswerHash> object containing submitted answers, and results of answer
  384 evaluation.
  385 
  386 =item result
  387 
  388 A hash containing the results of grading the problem.
  389 
  390 =item state
  391 
  392 A hash containing the new problem state.
  393 
  394 =item errors
  395 
  396 A string containing any errors encountered while rendering the problem.
  397 
  398 =item warnings
  399 
  400 A string containing any warnings encountered while rendering the problem.
  401 
  402 =item flags
  403 
  404 A hash containing PG_flags (see the Translator docs).
  405 
  406 =back
  407 
  408 =head1 METHODS PROVIDED BY THE BASE CLASS
  409 
  410 The following methods are provided for use by subclasses of WeBWorK::PG.
  411 
  412 =over
  413 
  414 =item defineProblemEnvir ENVIRONMENT, USER, KEY, SET, PROBLEM, PSVN, FIELDS, OPTIONS
  415 
  416 Generate a problem environment hash to pass to the renderer.
  417 
  418 =item translateDisplayModeNames NAME
  419 
  420 NAME contains
  421 
  422 =back
  423 
  424 =head1 AUTHOR
  425 
  426 Written by Sam Hathaway, sh002i (at) math.rochester.edu.
  427 
  428 =cut

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9