GeoGebraWeb1
GeoGebra Applets (JavaScript)
This PG code shows how to write WebWork questions with interactive GeoGebra applets that run using JavaScript (not Java).
- File location in OPL: FortLewis/Authoring/Templates/IntegralCalc/GeoGebraWeb1
PG problem file | Explanation |
---|---|
DOCUMENT(); loadMacros( "PGstandard.pl", "MathObjects.pl", "AppletObjects.pl", "unionTables.pl", "parserNumberWithUnits.pl", "PGcourse.pl", ); TEXT(beginproblem()); $showPartialCorrectAnswers = 1; |
Initialization:
We need to include the macro file |
Context("Numeric")->variables->are(t=>"Real"); $a = random(2,4,1); $b = random(5,8,1); $c = random(1,3,1); $d = random(1,3,1); while ($c == $d) { $d = random(1,3,1); } # $f = Formula("$c / (1+(t-2)^2) + $d / (1+(t-4)^2)"); $F = Formula("$c * arctan(t-2) + $d * arctan(t-4) - $c * arctan(-2) - $d * arctan(-4)"); $Fa = $F->eval(t=>$a); $Fb = $F->eval(t=>$b); $disp = $Fb - $Fa; |
Setup:
The GeoGebra applet displays a graph of
We define the accumulation function (antiderivative) |
###################################### # Create link to applet: ################################### # You can name your applet (anything reasonable :-) ) # Adjust the height and width as desired # Paste the geogebra parameters in at the bottom of the page just above the # command end command # so that they don't get in the way ################################### $appletName = "ggbfoo"; $applet = GeogebraWebApplet( code => "geogebra.GeoGebraApplet", archive => "geogebra.jar", codebase => findAppletCodebase("geogebra.jar"), appletName => $appletName, appletId => $appletName, submitActionAlias => 'getAppletValues', # default actionAlias initializeActionAlias => 'configure', # default actionAlias setStateAlias => 'setXML', getStateAlias => 'getXML', setConfigAlias => '', getConfigAlias => '', returnFieldName => '', width => 497, # may want to modify width height => 361, # may want to modify height # mayscript => "true", debugMode => 0, # set debugMode to 0 for no debug # to 1 to make xml representation visible # to 2 to add alerts detailing progression # through the applet onInit => 'ggbOnInit', type => 'geogebraweb', #submitActionScript => qq{ getQE('answerBox').value = getAppletValues() }, submitActionScript => '', selfLoading => 1, params => GEOGEBRA_PARAMS(),# paste parameters in ); ############################################### # # PNG picture of GeoGebra applet for hardcopy $im = image( "AF7-c".$c."-d".$d.".png", width=>486, height=>335, tex_size=>900 ); ################################## # Setup GeogebraWebApplet -- ################################### ############################################### # # For persistence of GeoGebra applet state # when answers are submitted or page is refreshed. # Uses JavaScript interface to GeoGebra. HEADER_TEXT( qq! <script language="javascript"> function ggbOnInit(param) { if (param == "$appletName") { applet_loaded(param,1); // report that applet is ready. ww_applet_list[param].safe_applet_initialize(2); } } </script> <noscript>For this question to work properly, you must enable JavaScript, or use another browser (or computer) on which JavaScript works.</noscript> !); ####################################################### # # For setting initial values in GeoGebra from WeBWorK # Uses JavaScript interface to GeoGebra. TEXT( MODES(TeX=>'', HTML=><<END_SCRIPT ) ); <script language="javascript"> function setAppletCoefficients() { var applet=getApplet("$appletName"); // alert("Updating coefficients"); if (typeof(applet)!="undefined") { if (typeof(applet.setValue)!="undefined") { //////////////////////////////////////////////////////////////// // List the values of the parameters to be set in the applet here // Set variables "c" and "d" in GeoGebra to values $c and $d from WebWork applet.setValue("c", $c); applet.setValue("d", $d); } else { setTimeout("setAppletCoefficients()", 1000); } } else { setTimeout("setAppletCoefficients()",1000); } } ww_applet_list["$appletName"].setConfig = function() {setAppletCoefficients()}; </script> END_SCRIPT ################################### #insert applet into body -- this does not need to be changed # except to adjust the insertion of the reinitialize_button or # a hidden AnswerBox ################################### # TEXT($PAR, MODES(TeX=>'geogebraweb code', # HTML=>$applet->insertAll( # debug =>0, # reinitialize_button => 1, # #includeAnswerBox=>1, # ))); |
GeoGebra Applet Setup:
We use
Since the GeoGebra parameters can be very lengthy, at the end of the PG file we put a new
method called |
Context()->texStrings; BEGIN_TEXT \{ ColumnTable( "The figure shows an interactive graph of velocity \( v(t) \), in meters per second, where time \( t \) is measured in seconds. You can change the value of \( t \) in the graph by clicking and dragging the red dot along the horizontal \( t \)-axis. $BR $BR (a) What is the displacement between \( t = 0 \) seconds and \( t = $a \) seconds? $BR". ans_rule(20). " (include ".helpLink('units').") $BR $BR (b) What is the displacement between \( t = 0 \) seconds and \( t = $b \) seconds? $BR". ans_rule(20). " (include ".helpLink('units').") $BR $BR (c) What is the displacement between \( t = $a \) seconds and \( t = $b \) seconds? $BR". ans_rule(20). " (include ".helpLink('units').")" , $BCENTER. $BBOLD."Graph of velocity \( y = v(t) \)".$EBOLD.$PAR. MODES(HTML=>$applet->insertAll(debug=>0, reinitialize_button=>1, includeAnswerBox=>0,), TeX=>$im). $ECENTER , indent => 0, separation => 30, valign => "TOP" ); \} END_TEXT Context()->normalStrings; |
Main Text:
The problem text section of the file is mostly as we'd expect.
We make use of a |
ANS( NumberWithUnits("$Fa","m") ->cmp(tolerance=>0.11, tolType=>'absolute') ); ANS( NumberWithUnits("$Fb","m") ->cmp(tolerance=>0.11, tolType=>'absolute') ); ANS( NumberWithUnits("$disp","m")->cmp(tolerance=>0.21, tolType=>'absolute') ); |
Answer Evaluation: Standard. |
sub GEOGEBRA_PARAMS { my $result = { showResetIcon=>"false" , enableRightClick=>"false" , enableLabelDrags=>"false", showMenuBar=>"false" , showToolBar=>"false", showAlgebraInput=>"false", useBrowserForJS=>"true", ggbbase64=>"UEsDBBQACAAIAHKAtUYAAAAAAAAAAAAAAAAWAAAAZ2VvZ2VicmFfamF2YXNjcmlwdC5qc0srzUsuyczPU0hPT/LP88zLLNHQVKiu5QIAUEsHCEXM3l0aAAAAGAAAAFBLAwQUAAgACABygLVGAAAAAAAAAAAAAAAADAAAAGdlb2dlYnJhLnhtbN1Y727jNhL/vH2KgQoUWTS2RVL/vLW3SAoUt8BuL2h6h0M/FKAl2mYjS6pEOc5iX+Deo0/WJ7khKcmSneTsTT8Uza5NURpyZn4z/M3Is293mxS2oqxkns0dMnYdEFmcJzJbzZ1aLUeR8+3bL2Yrka/EouSwzMsNV3PHG1Nnvw5nYy/Ui2Uyd5ZeRDkVfBQs6XLkLTw+mgrmj5ZTxr2IM06i0AHYVfJNlv/AN6IqeCxu47XY8Pd5zJXZc61U8WYyub+/H7fax3m5mqxWi/GuShxAy7Nq7jQXb3C7waJ7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jfgWaCwesuDX1L9lhpo6e4ZdJ/jTAv9M3v/W//B1BLBwjBLpipVgcAAJ8YAABQSwECFAAUAAgACABygLVGRczeXRoAAAAYAAAAFgAAAAAAAAAAAAAAAAAAAAAAZ2VvZ2VicmFfamF2YXNjcmlwdC5qc1BLAQIUABQACAAIAHKAtUbBLpipVgcAAJ8YAAAMAAAAAAAAAAAAAAAAAF4AAABnZW9nZWJyYS54bWxQSwUGAAAAAAIAAgB+AAAA7gcAAAAA" }; $result; } COMMENT('Uses GeoGebraWeb (JavaScript), so it should work on computers and tablets.'); ENDDOCUMENT(); |
GeoGebra Applet Parameters:
When you export your GeoGebra drawing pad via File -> Export -> Dynamic worksheet as Webpage (html), you get an html file with source code you can paste directly in here below the commented lines |
The really long parameter ggbBase64<param name="ggbBase64" value="UEsDBBQACAAIAGKtZj0AAAAAAAAAAAAAAAAMAAAAZ2VvZ2VicmEueG1s3Vdtb9s2EP68/gqCAwobQ2xKll1ntVJk2QYE6NYA2YpgHwbQ0tnmIokeSTlyfv2OpCTLypIm64C9+INlHk/Hu+c5PqQX76o8IztQWsgipsGIUQJFIlNRrGNamtXJnL47e7VYg1zDUnGykirnJqaTUUitvRRnr75Y6I28IzxzLh8F3MV0xTMNlOitAp7qDYA5svOyEpngav9h+RskRh8mfJDLYlviKkaVaEvy9L3QzXDsFtxmwnwrdiIFRTKZxHQ2xdTx10dQRiQ8i2nEvCWMadibRNPEzm6kEveyMNb9EDzjS8gQgGuzz4CQnZ0N/dQKnQnR4h4QLGdbjB0GCyiTTKSCF7ZOlyI6EXInUrPBXOYzXA3EemOxY5GPlkip0uu9NpCT6hdQEoPOLAV7PwhDR4jGjHG9KXNT3ZGLArtrMAbz1YRXoBvU1kqk3d+X+huZpS3QWykKc8G3plSOa1abXNFuqGy258U6g9oWIBUbSG6Xsrr2CEx86J/2W/eKS2e5vpCZVERZ2KfoUD+X/ul8bJ6tF3M+zHnUMWzQdj44DZ2Hey790/MkCp9aXXfQFB2wZhmhiTVYEL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:
|
- POD documentation: AppletObjects.pl
- PG macro: AppletObjects.pl