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