AppletObjects.pl

Jump to: Site Navigation

NAME

AppletObjects.pl - Macro-based front end for the Applet.pm module.

DESCRIPTION

The subroutines in this file provide mechanisms to insert Flash applets, Java applets, HTML 5 Canvas applets, and Geogebra web applets into a WeBWorK problem.

See also Applet.pm.

FlashApplet

    Useage:    $applet = FlashApplet(...);

JavaApplet

    Useage:    $applet = JavaApplet(...);

CanvasApplet

    Useage:    $applet = CanvasApplet(...);

GeogebraWebApplet

    Useage:    $applet = GeogebraWebApplet(...);

Methods

insertAll

    Useage:  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 and provides mechanisms for revealing the state while debugging the applet.

Example problem

    DOCUMENT();

    # Load whatever macros you need for the problem
    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.
    ###################################

    $appletName = "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() },
        selfLoading => 1,
        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) {
            applet_loaded(param,1);  // report that applet is ready. 
            ww_applet_list[param].safe_applet_initialize(2);
        }
        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(debug => 0, reinitialize_button => 0, includeAnswerBox => 1) \}
    $PAR

    More problem text.

    END_TEXT

    LABELED_ANS($answerBox, $ans->cmp);

    ENDDOCUMENT();

Site Navigation