[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 1703 - (download) (as text) (annotate)
Mon Jan 5 01:02:41 2004 UTC (9 years, 4 months ago) by sh002i
File size: 12302 byte(s)
removed lingering references to "webwork-modperl", since we're calling
this product "webwork2" now. soon we should change the name of the CVS
module to "webwork2" as well.

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9