[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 5865 - (download) (as text) (annotate)
Thu Aug 7 18:51:52 2008 UTC (4 years, 10 months ago) by gage
File size: 13829 byte(s)
Make htmlURL available within set

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9