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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4733 - (view) (download) (as text)

1 : gage 4287 #!/usr/bin/perl -w
2 :    
3 :     =pod
4 :    
5 :     This script will take a file and send it to a WeBWorK daemon webservice
6 :     to have it rendered. The result is split into the basic HTML rendering
7 :     and evaluation of answers and then passed to Safari for printing.
8 :    
9 :     The formatting allows the Safari presentation to be interactive with the
10 :     daemon running on webhost.math.rochester.edu
11 :    
12 :    
13 :    
14 :     =cut
15 :    
16 :     use XMLRPC::Lite;
17 :     use MIME::Base64 qw( encode_base64 decode_base64);
18 : gage 4733 #require "webwork_xmlrpc_inc.pl";
19 : gage 4287
20 :     # configuration section
21 :     use constant PROTOCOL => 'https'; # or 'http';
22 : gage 4732 use constant HOSTURL => 'webwork.rochester.edu';
23 :     use constant HOSTPORT => 443;
24 : gage 4287 use constant TRANSPORT_METHOD => 'XMLRPC::Lite';
25 : gage 4732 use constant REQUEST_CLASS => 'WebworkXMLRPC'; # WebworkXMLRPC is used for soap also!!
26 :     use constant REQUEST_URI => 'mod_xmlrpc';
27 : gage 4287 use constant TEMPOUTPUTFILE => '/Users/gage/Desktop/renderProblemOutput.html';
28 :     use constant DISPLAY_COMMAND => 'open -a safari ';
29 :     use constant COURSE => 'daemon2_course';
30 :    
31 :    
32 :    
33 :     # $pg{displayModes} = [
34 :     # "plainText", # display raw TeX for math expressions
35 :     # "formattedText", # format math expressions using TtH
36 :     # "images", # display math expressions as images generated by dvipng
37 :     # "jsMath", # render TeX math expressions on the client side using jsMath
38 :     # "asciimath", # render TeX math expressions on the client side using ASCIIMathML
39 :     # ];
40 : gage 4733 use constant DISPLAYMODE => 'images'; # tex and jsMath are other possibilities.
41 : gage 4287
42 :    
43 :     my @COMMANDS = qw( listLibraries renderProblem ); #listLib readFile tex2pdf
44 :    
45 :     # end configuration section
46 :    
47 :    
48 :    
49 :     #print STDERR "inputs are ", join (" | ", @ARGV), "\n";
50 :     our $source;
51 :     our $encodedSource;
52 :     our $rh_result;
53 :     # filter mode main code
54 :    
55 :     undef $/;
56 :     $source = <>; #slurp input
57 :     $encodedSource = encodeSource($source);
58 :     $/ =1;
59 :     xmlrpcCall('renderProblem');
60 :    
61 :     # end filter main code
62 :    
63 :     sub xmlrpcCall {
64 :     my $command = shift;
65 :     $command = 'listLibraries' unless $command;
66 :    
67 :     my $requestResult = TRANSPORT_METHOD
68 :     #->uri('http://'.HOSTURL.':'.HOSTPORT.'/'.REQUEST_CLASS)
69 :     -> proxy(PROTOCOL.'://'.HOSTURL.':'.HOSTPORT.'/'.REQUEST_URI);
70 :    
71 :     my $test = [3,4,5,6];
72 :     my $input = setInputTable();
73 :     #print "displayMode=",$input->{envir}->{displayMode},"\n";
74 :     local( $result);
75 :     # use eval to catch errors
76 :     eval { $result = $requestResult->call(REQUEST_CLASS.'.'.$command,$input) };
77 :     print STDERR "There were a lot of errors\n" if $@;
78 :     print "Errors: \n $@\n End Errors\n" if $@;
79 :    
80 :     #print "result is|", ref($result),"|";
81 :    
82 :     unless (ref($result) and $result->fault) {
83 :    
84 :    
85 :     $rh_result = $result->result();
86 :    
87 :     # look at output
88 :    
89 :     local(*FH);
90 :     open(FH, '>'.TEMPOUTPUTFILE) or die "Can't open file ".TEMPOUTPUTFILE()." for writing";
91 :     print FH formatRenderedProblem($rh_result);
92 :     close(FH);
93 :    
94 :     system(DISPLAY_COMMAND().TEMPOUTPUTFILE());
95 :     #print pretty_print_rh($result->result()),"\n"; #$result->result()
96 :     } else {
97 :     print 'oops ', join ', ',
98 :     $result->faultcode,
99 :     $result->faultstring;
100 :     }
101 :     }
102 :    
103 :     sub encodeSource {
104 :     encode_base64($source);
105 :     }
106 :     sub pretty_print_rh {
107 :     shift if UNIVERSAL::isa($_[0] => __PACKAGE__);
108 :     my $rh = shift;
109 :     my $indent = shift || 0;
110 :     my $out = "";
111 :     my $type = ref($rh);
112 :    
113 :     if (defined($type) and $type) {
114 :     $out .= " type = $type; ";
115 :     } elsif (! defined($rh )) {
116 :     $out .= " type = UNDEFINED; ";
117 :     }
118 :     return $out." " unless defined($rh);
119 :    
120 :     if ( ref($rh) =~/HASH/ or "$rh" =~/HASH/ ) {
121 :     $out .= "{\n";
122 :     $indent++;
123 :     foreach my $key (sort keys %{$rh}) {
124 :     $out .= " "x$indent."$key => " . pretty_print_rh( $rh->{$key}, $indent ) . "\n";
125 :     }
126 :     $indent--;
127 :     $out .= "\n"." "x$indent."}\n";
128 :    
129 :     } elsif (ref($rh) =~ /ARRAY/ or "$rh" =~/ARRAY/) {
130 :     $out .= " ( ";
131 :     foreach my $elem ( @{$rh} ) {
132 :     $out .= pretty_print_rh($elem, $indent);
133 :    
134 :     }
135 :     $out .= " ) \n";
136 :     } elsif ( ref($rh) =~ /SCALAR/ ) {
137 :     $out .= "scalar reference ". ${$rh};
138 :     } elsif ( ref($rh) =~/Base64/ ) {
139 :     $out .= "base64 reference " .$$rh;
140 :     } else {
141 :     $out .= $rh;
142 :     }
143 :    
144 :     return $out." ";
145 :     }
146 :    
147 :     sub setInputTable_for_listLib {
148 :     $out = {
149 :     #password => 'geometry',
150 :     pw => 'geometry',
151 :     set => 'set0',
152 :     library_name => 'rochesterLibrary',
153 :     command => 'all',
154 :     };
155 :    
156 :     $out;
157 :     }
158 :     sub setInputTable {
159 :     $out = {
160 :     #password => 'geometry',
161 :     pw => 'geometry',
162 :     set => 'set0',
163 :     library_name => 'rochesterLibrary',
164 :     command => 'all',
165 :     answer_form_submitted => 1,
166 :     course => COURSE(),
167 :     extra_packages_to_load => [qw( AlgParserWithImplicitExpand Expr
168 :     ExprWithImplicitExpand AnswerEvaluator
169 :     AnswerEvaluatorMaker
170 :     )],
171 : gage 4733 mode => DISPLAMODE(),
172 : gage 4287 modules_to_evaluate => [ qw(
173 :     Exporter
174 :     DynaLoader
175 :     GD
176 :     WWPlot
177 :     Fun
178 :     Circle
179 :     Label
180 :     PGrandom
181 :     Units
182 :     Hermite
183 :     List
184 :     Match
185 :     Multiple
186 :     Select
187 :     AlgParser
188 :     AnswerHash
189 :     Fraction
190 :     VectorField
191 :     Complex1
192 :     Complex
193 :     MatrixReal1 Matrix
194 :     Distributions
195 :     Regression
196 :    
197 :     )],
198 :     envir => environment(),
199 :     problem_state => {
200 :    
201 :     num_of_correct_ans => 2,
202 :     num_of_incorrect_ans => 4,
203 :     recorded_score => 1.0,
204 :     },
205 :     source => $encodedSource, #base64 encoded
206 :    
207 :    
208 :    
209 :     };
210 :    
211 :     $out;
212 :     }
213 :    
214 :     sub environment {
215 :     my $envir = {
216 :     answerDate => '4014438528',
217 :     CAPA_Graphics_URL=>'http://webwork-db.math.rochester.edu/capa_graphics/',
218 :     CAPA_GraphicsDirectory =>'/ww/webwork/CAPA/CAPA_Graphics/',
219 :     CAPA_MCTools=>'/ww/webwork/CAPA/CAPA_MCTools/',
220 :     CAPA_Tools=>'/ww/webwork/CAPA/CAPA_Tools/',
221 :     cgiDirectory=>'Not defined',
222 :     cgiURL => 'Not defined',
223 :     classDirectory=> 'Not defined',
224 :     courseName=>'Not defined',
225 :     courseScriptsDirectory=>'/ww/webwork/system/courseScripts/',
226 :     displayMode=>DISPLAYMODE,
227 :     dueDate=> '4014438528',
228 :     externalGif2EpsPath=>'not defined',
229 :     externalPng2EpsPath=>'not defined',
230 :     externalTTHPath=>'/usr/local/bin/tth',
231 :     fileName=>'set0/prob1a.pg',
232 :     formattedAnswerDate=>'6/19/00',
233 :     formattedDueDate=>'6/19/00',
234 :     formattedOpenDate=>'6/19/00',
235 :     functAbsTolDefault=> 0.0000001,
236 :     functLLimitDefault=>0,
237 :     functMaxConstantOfIntegration=> 1000000000000.0,
238 :     functNumOfPoints=> 5,
239 :     functRelPercentTolDefault=> 0.000001,
240 :     functULimitDefault=>1,
241 :     functVarDefault=> 'x',
242 :     functZeroLevelDefault=> 0.000001,
243 :     functZeroLevelTolDefault=>0.000001,
244 :     htmlDirectory =>'/ww/webwork/courses/gage_course/html/',
245 :     htmlURL =>'http://webwork-db.math.rochester.edu/gage_course/',
246 :     inputs_ref => {
247 :     AnSwEr1 => '',
248 :     AnSwEr2 => '',
249 :     AnSwEr3 => '',
250 :     },
251 :     macroDirectory=>'/ww/webwork/courses/gage_course/templates/macros/',
252 :     numAbsTolDefault=>0.0000001,
253 :     numFormatDefault=>'%0.13g',
254 :     numOfAttempts=> 0,
255 :     numRelPercentTolDefault => 0.0001,
256 :     numZeroLevelDefault =>0.000001,
257 :     numZeroLevelTolDefault =>0.000001,
258 :     openDate=> '3014438528',
259 :     PRINT_FILE_NAMES_FOR => [ 'gage'],
260 :     probFileName => 'set0/prob1a.pg',
261 :     problemSeed => 1234,
262 :     problemValue =>1,
263 :     probNum => 13,
264 :     psvn => 54321,
265 :     psvnNumber=> 54321,
266 :     questionNumber => 1,
267 :     scriptDirectory => 'Not defined',
268 :     sectionName => 'Gage',
269 :     sectionNumber => 1,
270 :     sessionKey=> 'Not defined',
271 :     setNumber =>'MAAtutorial',
272 :     studentLogin =>'gage',
273 :     studentName => 'Mike Gage',
274 :     tempDirectory => '/ww/htdocs/tmp/gage_course/',
275 :     templateDirectory=>'/ww/webwork/courses/gage_course/templates/',
276 :     tempURL=>'http://webwork-db.math.rochester.edu/tmp/gage_course/',
277 :     webworkDocsURL => 'http://webwork.math.rochester.edu/webwork_gage_system_html',
278 :     };
279 :     $envir;
280 :     };
281 :    
282 :     sub formatAnswerRow {
283 :     my $rh_answer = shift;
284 :     my $problemNumber = shift;
285 :     my $answerString = $rh_answer->{original_student_ans}||'&nbsp;';
286 :     my $correctAnswer = $rh_answer->{correct_ans};
287 :     my $score = ($rh_answer->{score}) ? 'Correct' : 'Incorrect';
288 :     my $row = qq{
289 :     <tr>
290 :     <td>
291 :     $problemNumber
292 :     </td>
293 :     <td>
294 :     $answerString
295 :     </td>
296 :     <td>
297 :     $score
298 :     </td>
299 :     <td>
300 :     Correct answer is $correctAnswer
301 :     </td>
302 :     <td>
303 :     <i></i>
304 :     </td>
305 :     </tr>\n
306 :     };
307 :     $row;
308 :     }
309 :    
310 :     sub formatRenderedProblem {
311 :     my $rh_result = shift; # wrap problem in formats
312 :     my $problemText = decode_base64($rh_result->{text});
313 :     my $rh_answers = $rh_result->{answers};
314 :    
315 :     my $warnings = '';
316 :     if ( defined ($rh_result->{WARNINGS}) and $rh_result->{WARNINGS} ){
317 :     $warnings = "<div style=\"background-color:pink\">
318 :     <p >WARNINGS</p><p>".decode_base64($rh_result->{WARNINGS})."</p></div>";
319 :     }
320 :    
321 :     ;
322 :     # collect answers
323 :     my $answerTemplate = q{<hr>ANSWERS <table border="3" align="center">};
324 :     my $problemNumber = 1;
325 :     foreach my $key (sort keys %{$rh_answers}) {
326 :     $answerTemplate .= formatAnswerRow($rh_answers->{$key}, $problemNumber++);
327 :     }
328 :     $answerTemplate .= q{</table> <hr>};
329 :    
330 :    
331 :    
332 :     my $problemTemplate = <<ENDPROBLEMTEMPLATE;
333 :    
334 :     $answerTemplate
335 :     $warnings
336 :     <form action="http://webhost.math.rochester.edu/webworkdocs/ww/render" method="post">
337 :     $problemText
338 :     <input type="hidden" name="answersSubmitted" value="1">
339 :     <input type="hidden" name="problemAddress" value="probSource">
340 :     <input type="hidden" name="problemSource" value="$encodedSource">
341 :     <input type="hidden" name="problemSeed" value="1234">
342 :     <input type="hidden" name="pathToProblemFile" value="foobar">
343 :     <p><input type="submit" name="submit" value="submit answers"></p>
344 :     </form>
345 :    
346 :    
347 :     ENDPROBLEMTEMPLATE
348 :    
349 :    
350 :    
351 :     $problemTemplate;
352 :     }

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9