[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 2166 - (download) (as text) (annotate)
Sat May 22 22:16:20 2004 UTC (9 years ago) by dpvc
File size: 12413 byte(s)
Files needed for jsMath display mode.

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9