[system] / trunk / webwork-modperl / clients / renderProblem.pl Repository:
ViewVC logotype

View of /trunk/webwork-modperl/clients/renderProblem.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6231 - (download) (as text) (annotate)
Tue May 11 15:44:05 2010 UTC (3 years ago) by gage
File size: 11485 byte(s)
updated to work with apache2  and the new html2xml
facility

    1 #!/usr/bin/perl -w
    2 
    3 ################################################################################
    4 # WeBWorK Online Homework Delivery System
    5 # Copyright © 2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
    6 # $CVSHeader: webwork2/lib/WeBWorK/ContentGenerator/ProblemRenderer.pm,v 1.1 2008/04/29 19:27:34 sh002i Exp $
    7 #
    8 # This program is free software; you can redistribute it and/or modify it under
    9 # the terms of either: (a) the GNU General Public License as published by the
   10 # Free Software Foundation; either version 2, or (at your option) any later
   11 # version, or (b) the "Artistic License" which comes with this package.
   12 #
   13 # This program is distributed in the hope that it will be useful, but WITHOUT
   14 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   15 # FOR A PARTICULAR PURPOSE.  See either the GNU General Public License or the
   16 # Artistic License for more details.
   17 ################################################################################
   18 
   19 =head1 NAME
   20 
   21 webwork2/clients/renderProblem.pl
   22 
   23 This script will take a file and send it to a WeBWorK daemon webservice
   24 to have it rendered.  The result is split into the basic HTML rendering
   25 and evaluation of answers and then passed to a browser for printing.
   26 
   27 The formatting allows the browser presentation to be interactive with the
   28 daemon running the script webwork2/lib/renderViaXMLRPC.pm
   29 
   30 
   31 
   32 =cut
   33 
   34 use strict;
   35 use warnings;
   36 
   37 package WeBWorK::ContentGenerator::renderViaXMLRPC_client;
   38 use Crypt::SSLeay;  # needed for https
   39 use XMLRPC::Lite;
   40 use MIME::Base64 qw( encode_base64 decode_base64);
   41 
   42 ##################################################
   43 #  configuration section for client
   44 ##################################################
   45 # use constant  PROTOCOL         =>  'http';
   46 # use constant  HOSTNAME          =>  'localhost';
   47 # use constant  HOSTPORT         =>  80;
   48 # our $FORM_ACTION_URL           ='http://localhost/webwork2/html2xml';
   49 
   50 
   51 use constant  PROTOCOL         =>  'https';                         # or 'http';
   52 use constant  HOSTNAME          =>  'hosted2.webwork.rochester.edu'; # 'localhost';
   53 use constant  HOSTPORT         =>  443;  #( for secure https)       # 80;
   54 our $FULL_URL                  =   PROTOCOL."://".HOSTNAME;   # .":".HOSTPORT;
   55 our $FORM_ACTION_URL           =   "$FULL_URL/webwork2/html2xml";  # server parameter
   56 
   57 use constant  TRANSPORT_METHOD => 'XMLRPC::Lite';
   58 use constant  REQUEST_CLASS    => 'WebworkXMLRPC';  # WebworkXMLRPC is used for soap also!!
   59 use constant  REQUEST_URI      => 'mod_xmlrpc';
   60 use constant  TEMPOUTPUTFILE   => '/Users/gage/Desktop/renderProblemOutput.html'; # client only
   61 use constant  DISPLAY_COMMAND  => 'open -a firefox '; # client only
   62 
   63 use constant  XML_PASSWORD      => 'xmlwebwork';
   64 use constant  XML_COURSE        => 'daemon_course';
   65 
   66 
   67 
   68 
   69 use constant DISPLAYMODE   => 'images'; # tex and jsMath  are other possibilities.
   70 
   71 
   72 our @COMMANDS = qw( listLibraries    renderProblem  ); #listLib  readFile tex2pdf
   73 
   74 
   75 ##################################################
   76 # end configuration section
   77 ##################################################
   78 
   79 sub new {
   80   my $self = {
   81     output      => '',
   82     encodedSource   => '',
   83     self      => '',
   84     inputs_ref      => {     AnSwEr0001 => '',
   85                    AnSwEr0002 => '',
   86                    AnSwEr0003 => '',
   87     },,
   88   };
   89 
   90   bless $self;
   91 }
   92 our $xmlrpc_client = new WeBWorK::ContentGenerator::renderViaXMLRPC_client;
   93 
   94 ##################################################
   95 # input/output section
   96 ##################################################
   97 
   98 
   99 our $source;
  100 our $rh_result;
  101 # filter mode  main code
  102 
  103 undef $/;
  104 $source   = <>; #slurp input
  105 $xmlrpc_client->{encodedSource} = encodeSource($source);
  106 $/ =1;
  107 #xmlrpcCall('renderProblem');
  108 $xmlrpc_client->xmlrpcCall('renderProblem');
  109 
  110 
  111 local(*FH);
  112 open(FH, '>'.TEMPOUTPUTFILE) or die "Can't open file ".TEMPOUTPUTFILE()." for writing";
  113 print FH $xmlrpc_client->{output} ;
  114 close(FH);
  115 
  116 system(DISPLAY_COMMAND().TEMPOUTPUTFILE());
  117 
  118 ##################################################
  119 # end input/output section
  120 ##################################################
  121 
  122 
  123 our $result;
  124 
  125 ##################################################
  126 # Utilities --
  127 #    this code is identical between renderProblem.pl and renderViaXMLRPC.pm
  128 ##################################################
  129 
  130 sub xmlrpcCall {
  131   my $self        = shift;
  132   my $command     = shift;
  133   $command        = 'listLibraries' unless $command;
  134 
  135     my $requestResult = TRANSPORT_METHOD
  136           #->uri('http://'.HOSTNAME.':'.HOSTPORT.'/'.REQUEST_CLASS)
  137       -> proxy($FULL_URL.'/'.REQUEST_URI);
  138 
  139     my $input = $self->setInputTable();
  140     local( $result);
  141     # use eval to catch errors
  142     eval { $result = $requestResult->call(REQUEST_CLASS.'.'.$command,$input) };
  143     print STDERR "There were a lot of errors\n" if $@;
  144     print STDERR "Errors: \n $@\n End Errors\n" if $@;
  145 
  146 
  147 
  148 
  149     unless (ref($result) and $result->fault) {
  150       my $rh_result = $result->result();
  151       #print STDERR pretty_print_rh($rh_result);
  152     $self->{output} = $self->formatRenderedProblem($rh_result);
  153 
  154     } else {
  155     $self->{output} = 'Error from server: ', join( ",\n ",
  156       $result->faultcode,
  157       $result->faultstring);
  158     }
  159 }
  160 
  161 sub encodeSource {
  162   my $source = shift;
  163   encode_base64($source);
  164 }
  165 
  166 
  167 sub pretty_print_rh {
  168     shift if UNIVERSAL::isa($_[0] => __PACKAGE__);
  169   my $rh = shift;
  170   my $indent = shift || 0;
  171   my $out = "";
  172   my $type = ref($rh);
  173 
  174   if (defined($type) and $type) {
  175     $out .= " type = $type; ";
  176   } elsif (! defined($rh )) {
  177     $out .= " type = UNDEFINED; ";
  178   }
  179   return $out." " unless defined($rh);
  180 
  181   if ( ref($rh) =~/HASH/ or "$rh" =~/HASH/ ) {
  182       $out .= "{\n";
  183       $indent++;
  184     foreach my $key (sort keys %{$rh})  {
  185       $out .= "  "x$indent."$key => " . pretty_print_rh( $rh->{$key}, $indent ) . "\n";
  186     }
  187     $indent--;
  188     $out .= "\n"."  "x$indent."}\n";
  189 
  190   } elsif (ref($rh)  =~  /ARRAY/ or "$rh" =~/ARRAY/) {
  191       $out .= " ( ";
  192     foreach my $elem ( @{$rh} )  {
  193       $out .= pretty_print_rh($elem, $indent);
  194 
  195     }
  196     $out .=  " ) \n";
  197   } elsif ( ref($rh) =~ /SCALAR/ ) {
  198     $out .= "scalar reference ". ${$rh};
  199   } elsif ( ref($rh) =~/Base64/ ) {
  200     $out .= "base64 reference " .$$rh;
  201   } else {
  202     $out .=  $rh;
  203   }
  204 
  205   return $out." ";
  206 }
  207 
  208 sub setInputTable_for_listLib {
  209   my $self = shift;
  210   my $out = {
  211     pw          =>   XML_PASSWORD(),
  212     set         =>   'set0',
  213     library_name =>  'Library',
  214     command      =>  'all',
  215   };
  216 
  217   $out;
  218 }
  219 sub setInputTable {
  220   my $self = shift;
  221   my $out = {
  222     pw          =>   XML_PASSWORD(),
  223     library_name =>  'Library',
  224     command      =>  'renderProblem',
  225     answer_form_submitted   => 1,
  226     course                  => XML_COURSE(),
  227     extra_packages_to_load  => [qw( AlgParserWithImplicitExpand Expr
  228                                     ExprWithImplicitExpand AnswerEvaluator
  229                                     AnswerEvaluatorMaker
  230     )],
  231     mode                    => DISPLAYMODE(),
  232     modules_to_evaluate     => [ qw(
  233 Exporter
  234 DynaLoader
  235 GD
  236 WWPlot
  237 Fun
  238 Circle
  239 Label
  240 PGrandom
  241 Units
  242 Hermite
  243 List
  244 Match
  245 Multiple
  246 Select
  247 AlgParser
  248 AnswerHash
  249 Fraction
  250 VectorField
  251 Complex1
  252 Complex
  253 MatrixReal1 Matrix
  254 Distributions
  255 Regression
  256 
  257     )],
  258     envir                   => $self->environment(),
  259     problem_state           => {
  260 
  261       num_of_correct_ans  => 2,
  262       num_of_incorrect_ans => 4,
  263       recorded_score       => 1.0,
  264     },
  265     source                   => $self->{encodedSource},  #base64 encoded
  266 
  267 
  268 
  269   };
  270 
  271   $out;
  272 }
  273 
  274 sub environment {
  275   my $self = shift;
  276   my $envir = {
  277     answerDate  => '4014438528',
  278     CAPA_Graphics_URL=>"not defined",
  279     CAPA_GraphicsDirectory =>"not defined",
  280     CAPA_MCTools=>"not defined",
  281     CAPA_Tools=>'not defined',
  282     cgiDirectory=>'Not defined',
  283     cgiURL => 'Not defined',
  284     classDirectory=> 'Not defined',
  285     courseName=>'Not defined',
  286     courseScriptsDirectory=>'not defined',
  287     displayMode=>DISPLAYMODE,
  288     dueDate=> '4014438528',
  289     externalGif2EpsPath=>'not defined',
  290     externalPng2EpsPath=>'not defined',
  291     externalTTHPath=>'/usr/local/bin/tth',
  292     fileName=>'set0/prob1a.pg',
  293     formattedAnswerDate=>'6/19/00',
  294     formattedDueDate=>'6/19/00',
  295     formattedOpenDate=>'6/19/00',
  296     functAbsTolDefault=> 0.0000001,
  297     functLLimitDefault=>0,
  298     functMaxConstantOfIntegration=> 1000000000000.0,
  299     functNumOfPoints=> 5,
  300     functRelPercentTolDefault=> 0.000001,
  301     functULimitDefault=>1,
  302     functVarDefault=> 'x',
  303     functZeroLevelDefault=> 0.000001,
  304     functZeroLevelTolDefault=>0.000001,
  305     htmlDirectory =>'not defined',
  306     htmlURL =>'not defined',
  307     inputs_ref => $self->{inputs_ref},
  308     macroDirectory=>'not defined',
  309     numAbsTolDefault=>0.0000001,
  310     numFormatDefault=>'%0.13g',
  311     numOfAttempts=> 0,
  312     numRelPercentTolDefault => 0.0001,
  313     numZeroLevelDefault =>0.000001,
  314     numZeroLevelTolDefault =>0.000001,
  315     openDate=> '3014438528',
  316     PRINT_FILE_NAMES_FOR => [ 'gage'],
  317     probFileName => 'set0/prob1a.pg',
  318     problemSeed  => 1234,
  319     problemValue =>1,
  320     probNum => 13,
  321     psvn => 54321,
  322     psvnNumber=> 54321,
  323     questionNumber => 1,
  324     scriptDirectory => 'Not defined',
  325     sectionName => 'Gage',
  326     sectionNumber => 1,
  327     sessionKey=> 'Not defined',
  328     setNumber =>'not defined',
  329     studentLogin =>'gage',
  330     studentName => 'Mike Gage',
  331     tempDirectory => 'not defined',
  332     templateDirectory=>'not defined',
  333     tempURL=>'not defined',
  334     webworkDocsURL => 'not defined',
  335   };
  336   $envir;
  337 };
  338 
  339 sub formatAnswerRow {
  340   my $self = shift;
  341   my $rh_answer = shift;
  342   my $problemNumber = shift;
  343   my $answerString  = $rh_answer->{original_student_ans}||'&nbsp;';
  344   my $correctAnswer = $rh_answer->{correct_ans}||'';
  345   my $ans_message   = $rh_answer->{ans_message};
  346   my $score         = ($rh_answer->{score}) ? 'Correct' : 'Incorrect';
  347   my $row = qq{
  348     <tr>
  349         <td>
  350         Prob: $problemNumber
  351       </td>
  352       <td>
  353         $answerString
  354       </td>
  355       <td>
  356           $score
  357       </td>
  358       <td>
  359         Correct answer is $correctAnswer
  360       </td>
  361       <td>
  362         <i>$ans_message</i>
  363       </td>
  364     </tr>\n
  365   };
  366   $row;
  367 }
  368 
  369 sub formatRenderedProblem {
  370   my $self        = shift;
  371   my $rh_result         = shift;  # wrap problem in formats
  372   my $problemText       = decode_base64($rh_result->{text});
  373   my $rh_answers        = $rh_result->{answers};
  374   my $encodedSource     = $self->{encodedSource}||'foobar';
  375   my $warnings          = '';
  376   if ( defined ($rh_result->{WARNINGS}) and $rh_result->{WARNINGS} ){
  377     $warnings = "<div style=\"background-color:pink\">
  378                  <p >WARNINGS</p><p>".decode_base64($rh_result->{WARNINGS})."</p></div>";
  379   }
  380 
  381            ;
  382   # collect answers
  383   my $answerTemplate    = q{<hr>ANSWERS <table border="3" align="center">};
  384   my $problemNumber     = 1;
  385     foreach my $key (sort  keys %{$rh_answers}) {
  386       $answerTemplate  .= $self->formatAnswerRow($rh_answers->{$key}, $problemNumber++);
  387     }
  388   $answerTemplate      .= q{</table> <hr>};
  389 
  390 
  391 
  392   my $problemTemplate = <<ENDPROBLEMTEMPLATE;
  393 <html>
  394 <head>
  395 <title>WeBWorK Editor</title>
  396 </head>
  397 <body>
  398         $answerTemplate
  399         $warnings
  400         <form action="$FORM_ACTION_URL" method="post">
  401       $problemText
  402          <input type="hidden" name="answersSubmitted" value="1">
  403          <input type="hidden" name="problemAddress" value="probSource">
  404          <input type="hidden" name="problemSource" value="$encodedSource">
  405          <input type="hidden" name="problemSeed" value="1234">
  406          <input type="hidden" name="pathToProblemFile" value="foobar">
  407          <p><input type="submit" name="submit" value="submit answers"></p>
  408        </form>
  409 </body>
  410 </html>
  411 
  412 ENDPROBLEMTEMPLATE
  413 
  414 
  415 
  416   $problemTemplate;
  417 }
  418 
  419 
  420 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9