[system] / trunk / webwork-modperl / lib / WeBWorK / PG.pm Repository:
ViewVC logotype

View of /trunk/webwork-modperl/lib/WeBWorK/PG.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3224 - (download) (as text) (annotate)
Sat May 14 01:32:19 2005 UTC (8 years ago) by dpvc
File size: 13446 byte(s)
Changes needed to make loadMacros() look through a path of directories
rather than just course/templates/macros and pg/macros.  You can
specify the path in the global.conf file.

You also need to make the update to pg/macros/dangerousMacros.pl

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9