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

From WeBWorK

Jump to: navigation


NAME

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

DESCRIPTION

This subroutines in this file provide mechanisms to insert Flash applets (and Java applets) into a WeBWorK problem.

See also http://webwork.maa.org/pod/pg_TRUNK/lib/Applet.html.

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.

Note: This method is defined here rather than in Applet.pl because it requires access to the RECORD_FORM_LABEL subroutine and to the routine accessing the stored values of the answers. These are defined in main::. FIXME -- with the creation of the PGcore object this can now be rewritten

Example problem

        DOCUMENT();
        
        # Load whatever macros you need for the problem
        loadMacros("PG.pl",
                           "PGbasicmacros.pl",
                           "PGchoicemacros.pl",
                           "PGanswermacros.pl",
                           "AppletObjects.pl",
                           "MathObjects.pl",
                           "source.pl"
                          );
         
        ## Do NOT show partial correct answers
        $showPartialCorrectAnswers = 0;
        
        
        
        ###################################
        # Create  link to applet 
        ###################################
        
        $applet = FlashApplet();
        my $appletName = "ExternalInterface";
        $applet->codebase(findAppletCodebase("$appletName.swf"));
        $applet->appletName($appletName);
        $applet->appletId($appletName);
        
        # findAppletCodebase looks for the applet in a list
        # of locations specified in global.conf
        
        ###################################
        # Add additional javaScript functions to header section of HTML to 
        # communicate with the "ExternalInterface" applet.
        ###################################
        
        $applet->header(<<'END_HEADER');
        <script language="javascript" src="https://devel.webwork.rochester.edu:8002/webwork2_files/js/BrowserSniffer.js">
        </script>
        
        
        <script language="JavaScript">
                function getBrowser() {
                          //alert("look for sniffer");
                  var sniffer = new BrowserSniffer();
                  //alert("found sniffer" +sniffer);
                  return sniffer;
                }
        
                function updateStatus(sMessage) {
                          getQE("playbackStatus").value = sMessage;
                }
                
                function newColor() {
                  getApplet("ExternalInterface").updateColor(Math.round(Math.random() * 0xFFFFFF));
                }
                
        </script>
        END_HEADER
        
        ###################################
        # Configure applet
        ###################################
        
        # not used here.  Allows for uploading an xml string for the applet
        
        
        
        
        ###################################
        # write the text for the problem
        ###################################
        
        TEXT(beginproblem());
        
        
         
        BEGIN_TEXT
        \{ $applet->insertAll() \}
          $PAR
        
          The Flash object operates above this line.  The box and button below this line are part of 
          the WeBWorK problem.  They communicate with the Flash object.
          $HR
          Status <input type="text" id="playbackStatus" value="started" /><br />
          Color <input type="button" value="new color" name="newColorButton" onClick="newColor()" />
           $PAR $HR
           This flash applet was created by Barbara Kaskosz. 
        
        END_TEXT
        
        ENDDOCUMENT();