################################################################################ # WeBWorK Online Homework Delivery System # Copyright © 2000-2007 The WeBWorK Project, http://openwebwork.sf.net/ # $CVSHeader: pg/macros/AppletObjects.pl,v 1.13 2009/02/07 22:27:29 gage Exp $ # # This program is free software; you can redistribute it and/or modify it under # the terms of either: (a) the GNU General Public License as published by the # Free Software Foundation; either version 2, or (at your option) any later # version, or (b) the "Artistic License" which comes with this package. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the # Artistic License for more details. ################################################################################ =head1 NAME AppletObjects.pl - Macro-based front end for the Applet.pm module. =head1 DESCRIPTION This subroutines in this file provide mechanisms to insert Flash applets (and Java applets) into a WeBWorK problem. =head1 SEE ALSO L. =cut ######################################################################### # # Add basic functionality to the header of the question # # don't reload this file ######################################################################### sub _AppletObjects_init { main::HEADER_TEXT(<<'END_HEADER_TEXT'); if (!( typeof(set_debug) == "function") ) { alert("Can't find the function set_debug. Is the file ww_applet_support.js in /webwork2/htdocs/js"); } END_HEADER_TEXT }; =head3 FlashApplet Useage: $applet = FlashApplet(); =cut sub FlashApplet { return new FlashApplet(@_); } sub JavaApplet { return new JavaApplet(@_); } package Applet; =head2 Methods =cut ## this method is defined in this file ## because the main subroutines HEADER_TEXT and MODES are ## not available to the module FlashApplet when that file ## is compiled (at the time the apache child process is first initialized) =head3 insertAll Useage: TEXT( $applet->insertAll() ); \{ $applet->insertAll() \} (used within BEGIN_TEXT/END_TEXT blocks) =cut =pod 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 C<$applet-EinsertHeader> and C<$applet-EinsertObject> (defined in L ) in the appropriate places. 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::. =cut sub insertAll { ## inserts both header text and object text my $self = shift; my %options = @_; $self->debug( (defined($options{debug}) and $options{debug}==1) ? 1 : 0 ); my $reset_button = $options{reset_button} || 0; # prepare html code for storing state my $appletName = $self->appletName; my $appletStateName = "${appletName}_state"; my $getState = $self->getStateAlias; my $setState = $self->setStateAlias; my $base64_initialState = $self->base64_state; main::RECORD_FORM_LABEL($appletStateName); #this insures that they'll be saved from one invocation to the next #main::RECORD_FORM_LABEL("previous_$appletStateName"); my $answer_value = ''; if ( defined( ${$main::inputs_ref}{$appletStateName} ) and ${$main::inputs_ref}{$appletStateName} =~ /\S/ ) { $answer_value = ${$main::inputs_ref}{$appletStateName}; } elsif ( defined( $main::rh_sticky_answers->{$appletStateName} ) ) { warn "type of sticky answers is ", ref( $main::rh_sticky_answers->{$appletStateName} ); $answer_value = shift( @{ $main::rh_sticky_answers->{$appletStateName} }); } $answer_value =~ tr/\\$@`//d; #`## make sure student answers can not be interpolated by e.g. EV3 $answer_value =~ s/\s+/ /g; ## remove excessive whitespace from student answer ####### # insert a hidden variable to hold the applet's state (debug =>1 makes it visible for debugging and provides debugging buttons) ####### my $base_64_encoded_answer_value; my $decoded_answer_value; if ( $answer_value =~/$decoded_answer_value
!; my $state_input_element = ($self->debug == 1) ? $debug_input_element : qq!\n!; my $reset_button_str = ($reset_button) ? qq!
! : '' ; # always base64 encode the hidden answer value to prevent problems with quotes. # $state_storage_html_code = $reset_button_str. $state_input_element. qq!!; $state_storage_html_code = qq!! . $reset_button_str . $state_input_element ; ####### # insert header material ####### main::HEADER_TEXT($self->insertHeader()); return main::MODES(TeX=>' {\bf applet } ', HTML=>$self->insertObject.$main::BR.$state_storage_html_code); } =head3 Example problem =cut =pod 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'); 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
Color $PAR $HR This flash applet was created by Barbara Kaskosz. END_TEXT ENDDOCUMENT(); =cut