AppletObjects.pl - Macro-based front end for the Applet.pm module.
The subroutines in this file provide mechanisms to insert Geogebra web applets into a WeBWorK problem.
See also Applet.pm.
Usage: $applet = GeogebraWebApplet(...);
This method is defined in this file because it uses methods in PG.pl and PGbasicmacros.pl that are not available to Applet.pm when it is compiled (at the time the apache child process is first initialized).
Usage: TEXT($applet->insertAll());
\{ $applet->insertAll() \} (used within BEGIN_TEXT/END_TEXT blocks)
Inserts applet at this point in the HTML code. (In TeX mode a message "Applet" is written.) This method also adds the applets header material into the header portion of the HTML page. It effectively inserts the outputs of both $applet->insertHeader
and $applet->insertObject
(defined in Applet.pm) in the appropriate places. In addition it creates a hidden answer blank for storing the state of the applet.
DOCUMENT();
# Load macros
loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"AppletObjects.pl",
"PGcourse.pl",
);
TEXT(beginproblem());
###################################
# Standard PG problem setup. Random parameters, answers, and such.
###################################
$ans = Compute("0");
###################################
# $appletName can be anything reasonable, but should try to choose a name that will not be
# used by other problems. If multiple problems appear on the same page in a gateway quiz
# that use the same name, one of the applets will not work. So to ensure uniqueness the
# applet name should be prefixed with the quiz prefix.
###################################
$appletName = $PG->{QUIZ_PREFIX} . "myUniqueAppletName";
###################################
# Generate the answer box name to use. This is only needed if the applet returns an answer
# that will be checked by WeBWorK. The approach of using NEW_ANS_NAME guarantees that you
# will get an answer name that will work in any problem, including a gateway quiz. If there
# are other answers in the problem, this may cause issues with the order of the answers in
# the results table as NEW_ANS_NAME records the answer now. If that is the case you may use
# any name you want, but make sure that you prefix it with $PG->{QUIZ_PREFIX}.
# (Eg: $answerBox = $PG->{QUIZ_PREFIX} . 'answerBox';)
###################################
$answerBox = NEW_ANS_NAME();
###################################
# Create the perlApplet object
###################################
$applet = GeogebraWebApplet(
appletName => $appletName,
onInit => 'myUniqueAppletOnInit',
answerBoxAlias => $answerBox,
submitActionScript => qq{ getQE('$answerBox').value = getAppletValues() },
params => {
ggbBase64 => "...", // The long base 64 encoded string for your applet.
enableShiftDragZoom => "false",
enableRightClick => "false" ,
enableLabelDrags => "false",
showMenuBar => "false" ,
showToolBar => "false",
showAlgebraInput => "false",
useBrowserForJS => "true", // Required or the onInit handler will not be called.
},
);
###################################
# Add additional JavaScript functions to header section of HTML.
###################################
$applet->header(<<END_HEADER);
<script>
// The applet name is passed to this function, although it is not really neccessary to
// check it, as the method will only be called for this applet. The applet name is only
// provided for backwards compatibility.
function myUniqueAppletOnInit(appletName) {
ww_applet_list[param].safe_applet_initialize();
}
function getAppletValues() {
var applet = getApplet("$appletName");
...
JavaScript code to extract answer from applet
...
return answer;
}
</script>
END_HEADER
###################################
# Write the text for the problem
###################################
BEGIN_TEXT
The applet will appear below. You can put other problem text here.
$PAR
\{ $applet->insertAll(reinitialize_button => 0, includeAnswerBox => 1) \}
$PAR
More problem text.
END_TEXT
LABELED_ANS($answerBox, $ans->cmp);
ENDDOCUMENT();