[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 4677 - (download) (as text) (annotate)
Mon Nov 27 18:37:23 2006 UTC (6 years, 5 months ago) by sh002i
File size: 13545 byte(s)
Add wrapped RestrictedMailer instance to PG envir.

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9