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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4091 - (download) (as text) (annotate)
Sun May 21 00:52:20 2006 UTC (7 years ago) by gage
Original Path: trunk/webwork2/lib/WeBWorK/PG.pm
File size: 13505 byte(s)
Added support for directories for housing applet files

    1 ################################################################################
    2 # WeBWorK Online Homework Delivery System
    3 # Copyright © 2000-2006 The WeBWorK Project, http://openwebwork.sf.net/
    4 # $CVSHeader: webwork-modperl/lib/WeBWorK/PG.pm,v 1.63 2006/01/25 23:13:51 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 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}, $ce->{siteDefaults}{timezone});
   97   $envir{dueDate}             = $set->due_date;
   98   $envir{formattedDueDate}    = formatDateTime($envir{dueDate}, $ce->{siteDefaults}{timezone});
   99   $envir{formatedDueDate}     = $envir{formattedDueDate}; # typo in many header files
  100   $envir{answerDate}          = $set->answer_date;
  101   $envir{formattedAnswerDate} = formatDateTime($envir{answerDate}, $ce->{siteDefaults}{timezone});
  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   # ADDED: macrosPath
  141   # REMOVED: macrosDirectory, courseScriptsDirectory
  142 
  143   $envir{cgiDirectory}           = undef;
  144   $envir{cgiURL}                 = undef;
  145   $envir{classDirectory}         = undef;
  146     $envir{macrosPath}             = $ce->{pg}->{directories}{macrosPath};
  147     $envir{appletDirs}             = $ce->{pg}->{directories}{appletDirs};
  148   $envir{htmlDirectory}          = $ce->{courseDirs}->{html}."/";
  149   $envir{htmlURL}                = $ce->{courseURLs}->{html}."/";
  150   $envir{templateDirectory}      = $ce->{courseDirs}->{templates}."/";
  151   $envir{tempDirectory}          = $ce->{courseDirs}->{html_temp}."/";
  152   $envir{tempURL}                = $ce->{courseURLs}->{html_temp}."/";
  153   $envir{scriptDirectory}        = undef;
  154   $envir{webworkDocsURL}         = $ce->{webworkURLs}->{docs}."/";
  155   $envir{localHelpURL}           = $ce->{webworkURLs}->{local_help}."/";
  156   $envir{jsMathURL}              = $ce->{webworkURLs}->{jsMath};
  157   $envir{asciimathURL}         = $ce->{webworkURLs}->{asciimath};
  158 
  159   # Information for sending mail
  160 
  161   $envir{mailSmtpServer} = $ce->{mail}->{smtpServer};
  162   $envir{mailSmtpSender} = $ce->{mail}->{smtpSender};
  163   $envir{ALLOW_MAIL_TO}  = $ce->{mail}->{allowedRecipients};
  164 
  165   # Default values for evaluating answers
  166 
  167   my $ansEvalDefaults = $ce->{pg}->{ansEvalDefaults};
  168   $envir{$_} = $ansEvalDefaults->{$_} foreach (keys %$ansEvalDefaults);
  169 
  170   # ----------------------------------------------------------------------
  171 
  172   my $basename = "equation-$envir{psvn}.$envir{probNum}";
  173   $basename .= ".$envir{problemSeed}" if $envir{problemSeed};
  174 
  175   # to make grabbing these options easier, we'll pull them out now...
  176   my %imagesModeOptions = %{$ce->{pg}->{displayModeOptions}->{images}};
  177 
  178   # Object for generating equation images
  179   $envir{imagegen} = WeBWorK::PG::ImageGenerator->new(
  180     tempDir  => $ce->{webworkDirs}->{tmp}, # global temp dir
  181     latex  => $envir{externalLaTeXPath},
  182     dvipng   => $envir{externalDvipngPath},
  183     useCache => 1,
  184     cacheDir => $ce->{webworkDirs}->{equationCache},
  185     cacheURL => $ce->{webworkURLs}->{equationCache},
  186     cacheDB  => $ce->{webworkFiles}->{equationCacheDB},
  187     useMarkers      => ($imagesModeOptions{dvipng_align} && $imagesModeOptions{dvipng_align} eq 'mysql'),
  188     dvipng_align    => $imagesModeOptions{dvipng_align},
  189     dvipng_depth_db => $imagesModeOptions{dvipng_depth_db},
  190   );
  191 
  192   #  ADDED: jsMath options
  193   $envir{jsMath} = {%{$ce->{pg}{displayModeOptions}{jsMath}}};
  194 
  195   # Other things...
  196   $envir{QUIZ_PREFIX}              = $options->{QUIZ_PREFIX}; # used by quizzes
  197   $envir{PROBLEM_GRADER_TO_USE}    = $ce->{pg}->{options}->{grader};
  198   $envir{PRINT_FILE_NAMES_FOR}     = $ce->{pg}->{specialPGEnvironmentVars}->{PRINT_FILE_NAMES_FOR};
  199 
  200         #  ADDED: __files__
  201         #    an array for mapping (eval nnn) to filenames in error messages
  202   $envir{__files__} = {
  203     root => $ce->{webworkDirs}{root},     # used to shorten filenames
  204     pg   => $ce->{pg}{directories}{root}, # ditto
  205     tmpl => $ce->{courseDirs}{templates}, # ditto
  206   };
  207 
  208   # variables for interpreting capa problems and other things to be
  209         # seen in a pg file
  210   my $specialPGEnvironmentVarHash = $ce->{pg}->{specialPGEnvironmentVars};
  211   for my $SPGEV (keys %{$specialPGEnvironmentVarHash}) {
  212     $envir{$SPGEV} = $specialPGEnvironmentVarHash->{$SPGEV};
  213   }
  214 
  215   return \%envir;
  216 }
  217 
  218 sub translateDisplayModeNames($) {
  219   my $name = shift;
  220   return DISPLAY_MODES()->{$name};
  221 }
  222 
  223 sub oldSafetyFilter {
  224   my $answer = shift; # accepts one answer and checks it
  225   my $submittedAnswer = $answer;
  226   $answer = '' unless defined $answer;
  227   my ($errorno);
  228   $answer =~ tr/\000-\037/ /;
  229   # Return if answer field is empty
  230   unless ($answer =~ /\S/) {
  231     #$errorno = "<BR>No answer was submitted.";
  232     $errorno = 0;  ## don't report blank answer as error
  233     return ($answer,$errorno);
  234   }
  235   # replace ^ with **    (for exponentiation)
  236   # $answer =~ s/\^/**/g;
  237   # Return if forbidden characters are found
  238   unless ($answer =~ /^[a-zA-Z0-9_\-\+ \t\/@%\*\.\n^\[\]\(\)\,\|]+$/ )  {
  239     $answer =~ tr/a-zA-Z0-9_\-\+ \t\/@%\*\.\n^\(\)/#/c;
  240     $errorno = "<BR>There are forbidden characters in your answer: $submittedAnswer<BR>";
  241     return ($answer,$errorno);
  242   }
  243   $errorno = 0;
  244   return($answer, $errorno);
  245 }
  246 
  247 sub nullSafetyFilter {
  248   return shift, 0; # no errors
  249 }
  250 
  251 1;
  252 
  253 __END__
  254 
  255 =head1 SYNOPSIS
  256 
  257  $pg = WeBWorK::PG->new(
  258    $ce,         # a WeBWorK::CourseEnvironment object
  259    $user,       # a WeBWorK::DB::Record::User object
  260    $sessionKey,
  261    $set,        # a WeBWorK::DB::Record::UserSet object
  262    $problem,    # a WeBWorK::DB::Record::UserProblem object
  263    $psvn,
  264    $formFields  # in &WeBWorK::Form::Vars format
  265    { # translation options
  266      displayMode     => "images", # (plainText|formattedText|images)
  267      showHints       => 1,        # (0|1)
  268      showSolutions   => 0,        # (0|1)
  269      refreshMath2img => 0,        # (0|1)
  270      processAnswers  => 1,        # (0|1)
  271    },
  272  );
  273 
  274  $translator = $pg->{translator}; # WeBWorK::PG::Translator
  275  $body       = $pg->{body_text};  # text string
  276  $header     = $pg->{head_text};  # text string
  277  $answerHash = $pg->{answers};    # WeBWorK::PG::AnswerHash
  278  $result     = $pg->{result};     # hash reference
  279  $state      = $pg->{state};      # hash reference
  280  $errors     = $pg->{errors};     # text string
  281  $warnings   = $pg->{warnings};   # text string
  282  $flags      = $pg->{flags};      # hash reference
  283 
  284 =head1 DESCRIPTION
  285 
  286 WeBWorK::PG is a factory for modules which use the WeBWorK::PG API. Notable
  287 modules which use this API (and exist) are WeBWorK::PG::Local and
  288 WeBWorK::PG::Remote. The course environment key $pg{renderer} is consulted to
  289 determine which render to use.
  290 
  291 =head1 THE WEBWORK::PG API
  292 
  293 Modules which support this API must implement the following method:
  294 
  295 =over
  296 
  297 =item new ENVIRONMENT, USER, KEY, SET, PROBLEM, PSVN, FIELDS, OPTIONS
  298 
  299 The C<new> method creates a translator, initializes it using the parameters
  300 specified, translates a PG file, and processes answers. It returns a reference
  301 to a blessed hash containing the results of the translation process.
  302 
  303 =back
  304 
  305 =head2 Parameters
  306 
  307 =over
  308 
  309 =item ENVIRONMENT
  310 
  311 a WeBWorK::CourseEnvironment object
  312 
  313 =item USER
  314 
  315 a WeBWorK::User object
  316 
  317 =item KEY
  318 
  319 the session key of the current session
  320 
  321 =item SET
  322 
  323 a WeBWorK::Set object
  324 
  325 =item PROBLEM
  326 
  327 a WeBWorK::DB::Record::UserProblem object. The contents of the source_file
  328 field can specify a PG file either by absolute path or path relative to the
  329 "templates" directory. I<The caller should remove taint from this value before
  330 passing!>
  331 
  332 =item PSVN
  333 
  334 the problem set version number
  335 
  336 =item FIELDS
  337 
  338 a reference to a hash (as returned by &WeBWorK::Form::Vars) containing form
  339 fields submitted by a problem processor. The translator will look for fields
  340 like "AnSwEr[0-9]" containing submitted student answers.
  341 
  342 =item OPTIONS
  343 
  344 a reference to a hash containing the following data:
  345 
  346 =over
  347 
  348 =item displayMode
  349 
  350 one of "plainText", "formattedText", or "images"
  351 
  352 =item showHints
  353 
  354 boolean, render hints
  355 
  356 =item showSolutions
  357 
  358 boolean, render solutions
  359 
  360 =item refreshMath2img
  361 
  362 boolean, force images created by math2img (in "images" mode) to be recreated,
  363 even if the PG source has not been updated. FIXME: remove this option.
  364 
  365 =item processAnswers
  366 
  367 boolean, call answer evaluators and graders
  368 
  369 =back
  370 
  371 =back
  372 
  373 =head2 RETURN VALUE
  374 
  375 The C<new> method returns a blessed hash reference containing the following
  376 fields. More information can be found in the documentation for
  377 WeBWorK::PG::Translator.
  378 
  379 =over
  380 
  381 =item translator
  382 
  383 The WeBWorK::PG::Translator object used to render the problem.
  384 
  385 =item head_text
  386 
  387 HTML code for the E<lt>headE<gt> block of an resulting web page. Used for
  388 JavaScript features.
  389 
  390 =item body_text
  391 
  392 HTML code for the E<lt>bodyE<gt> block of an resulting web page.
  393 
  394 =item answers
  395 
  396 An C<AnswerHash> object containing submitted answers, and results of answer
  397 evaluation.
  398 
  399 =item result
  400 
  401 A hash containing the results of grading the problem.
  402 
  403 =item state
  404 
  405 A hash containing the new problem state.
  406 
  407 =item errors
  408 
  409 A string containing any errors encountered while rendering the problem.
  410 
  411 =item warnings
  412 
  413 A string containing any warnings encountered while rendering the problem.
  414 
  415 =item flags
  416 
  417 A hash containing PG_flags (see the Translator docs).
  418 
  419 =back
  420 
  421 =head1 METHODS PROVIDED BY THE BASE CLASS
  422 
  423 The following methods are provided for use by subclasses of WeBWorK::PG.
  424 
  425 =over
  426 
  427 =item defineProblemEnvir ENVIRONMENT, USER, KEY, SET, PROBLEM, PSVN, FIELDS, OPTIONS
  428 
  429 Generate a problem environment hash to pass to the renderer.
  430 
  431 =item translateDisplayModeNames NAME
  432 
  433 NAME contains
  434 
  435 =back
  436 
  437 =head1 AUTHOR
  438 
  439 Written by Sam Hathaway, sh002i (at) math.rochester.edu.
  440 
  441 =cut

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9