[system] / trunk / pg / macros / AppletObjects.pl Repository:
ViewVC logotype

View of /trunk/pg/macros/AppletObjects.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6010 - (download) (as text) (annotate)
Thu Feb 19 03:05:14 2009 UTC (10 years, 7 months ago) by gage
File size: 9948 byte(s)
Minor cosmetic change

    1 ################################################################################
    2 # WeBWorK Online Homework Delivery System
    3 # Copyright  2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
    4 # $CVSHeader: pg/macros/AppletObjects.pl,v 1.16 2009/02/07 23:08:34 gage Exp $
    5 #
    6 # This program is free software; you can redistribute it and/or modify it under
    7 # the terms of either: (a) the GNU General Public License as published by the
    8 # Free Software Foundation; either version 2, or (at your option) any later
    9 # version, or (b) the "Artistic License" which comes with this package.
   10 #
   11 # This program is distributed in the hope that it will be useful, but WITHOUT
   12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   13 # FOR A PARTICULAR PURPOSE.  See either the GNU General Public License or the
   14 # Artistic License for more details.
   15 ################################################################################
   16 
   17 =head1 NAME
   18 
   19 AppletObjects.pl - Macro-based front end for the Applet.pm module.
   20 
   21 
   22 =head1 DESCRIPTION
   23 
   24 This subroutines in this
   25 file provide mechanisms to insert Flash applets (and  Java applets)
   26 into a WeBWorK problem.
   27 
   28 
   29 =head1 SEE ALSO
   30 
   31 L<Applets.pm>.
   32 
   33 =cut
   34 
   35 #########################################################################
   36 #
   37 # Add basic functionality to the header of the question
   38 #
   39 # don't reload this file
   40 #########################################################################
   41 
   42 sub _AppletObjects_init  {
   43 
   44 main::HEADER_TEXT(<<'END_HEADER_TEXT');
   45   <script language="javascript">AC_FL_RunContent = 0;</script>
   46     <script src="/webwork2_files/applets/AC_RunActiveContent.js" language="javascript">
   47     </script>
   48     <script src="/webwork2_files/js/Base64.js" language="javascript">
   49     </script>
   50     <script src="/webwork2_files/js/ww_applet_support.js">
   51         //upload functions stored in /opt/webwork/webwork2/htdocs/js ...
   52 
   53     </script>
   54 END_HEADER_TEXT
   55 
   56 };
   57 
   58 =head3
   59   FlashApplet
   60 
   61   Useage:    $applet = FlashApplet();
   62 
   63 =cut
   64 
   65 sub FlashApplet {
   66   return new FlashApplet(@_);
   67 
   68 }
   69 
   70 sub JavaApplet {
   71   return new JavaApplet(@_);
   72 
   73 }
   74 
   75 package Applet;
   76 
   77 
   78 
   79 =head2 Methods
   80 
   81 =cut
   82 
   83 ## this method is defined in this file
   84 ## because the main subroutines HEADER_TEXT and MODES are
   85 ## not available to the module FlashApplet when that file
   86 ## is compiled (at the time the apache child process is first initialized)
   87 
   88 =head3  insertAll
   89 
   90   Useage:   TEXT( $applet->insertAll() );
   91             \{ $applet->insertAll() \}     (used within BEGIN_TEXT/END_TEXT blocks)
   92 
   93 =cut
   94 
   95 =pod
   96 
   97 Inserts applet at this point in the HTML code.  (In TeX mode a message "Applet" is written.)  This method
   98 also adds the applets header material into the header portion of the HTML page. It effectively inserts
   99 the outputs of both C<$applet-E<gt>insertHeader> and C<$applet-E<gt>insertObject> (defined in L<Applet.pm> )
  100 in the appropriate places.
  101 
  102 Note: This method is defined here rather than in Applet.pl because it
  103       requires access to the RECORD_FORM_LABEL subroutine
  104       and to the routine accessing the stored values of the answers.  These are defined in main::.
  105 
  106 =cut
  107 
  108 sub insertAll {  ## inserts both header text and object text
  109   my $self = shift;
  110   my %options = @_;
  111   $self->debug( (defined($options{debug}) and $options{debug}==1) ? 1 : 0 );
  112   my $reset_button = $options{reset_button} || 0;
  113   # prepare html code for storing state
  114   my $appletName      = $self->appletName;
  115   my $appletStateName = "${appletName}_state";
  116   my $getState        = $self->getStateAlias;
  117   my $setState        = $self->setStateAlias;
  118   my $base64_initialState     = $self->base64_state;
  119   main::RECORD_FORM_LABEL($appletStateName);            #this insures that they'll be saved from one invocation to the next
  120   #main::RECORD_FORM_LABEL("previous_$appletStateName");
  121     my $answer_value = '';
  122 
  123     if ( defined( ${$main::inputs_ref}{$appletStateName} ) and ${$main::inputs_ref}{$appletStateName} =~ /\S/ ) {
  124     $answer_value = ${$main::inputs_ref}{$appletStateName};
  125   } elsif ( defined( $main::rh_sticky_answers->{$appletStateName} )  ) {
  126       warn "type of sticky answers is ", ref( $main::rh_sticky_answers->{$appletStateName} );
  127     $answer_value = shift( @{ $main::rh_sticky_answers->{$appletStateName} });
  128   }
  129   $answer_value =~ tr/\\$@`//d;   #`## make sure student answers can not be interpolated by e.g. EV3
  130   $answer_value =~ s/\s+/ /g;     ## remove excessive whitespace from student answer
  131   #######
  132   # insert a hidden variable to hold the applet's state (debug =>1 makes it visible for debugging and provides debugging buttons)
  133   #######
  134   my $base_64_encoded_answer_value;
  135   my $decoded_answer_value;
  136   if ( $answer_value =~/<XML|<?xml/i) {
  137     $base_64_encoded_answer_value = encode_base64($answer_value);
  138     $decoded_answer_value = $answer_value;
  139   } else {
  140     $decoded_answer_value = decode_base64($answer_value);
  141     if ( $decoded_answer_value =~/<XML|<?xml/i) {  # great, we've decoded the answer to obtain an xml string
  142       $base_64_encoded_answer_value = $answer_value;
  143     } else {    #WTF??  apparently we don't have XML tags
  144       $answer_value = "<xml>$answer_value</xml>";
  145       $base_64_encoded_answer_value = encode_base64($answer_value);
  146       $decoded_answer_value = $answer_value;
  147     }
  148   }
  149   $base_64_encoded_answer_value =~ s/\r|\n//g;    # get rid of line returns
  150     # debug version of the applet state answerBox and controls
  151     my $debug_input_element  = qq!\n<textarea  rows="4" cols="80"
  152      name = "$appletStateName">$decoded_answer_value</textarea><br/>
  153           <input type="button"  value="$getState"
  154                  onClick="debugText='';
  155                           ww_applet_list['$appletName'].getState();
  156                           alert(debugText);"
  157           >
  158           <input type="button"  value="$setState"
  159                  onClick="debugText='';
  160                           ww_applet_list['$appletName'].setState();
  161                           alert(debugText);"
  162           >
  163     !;
  164   my $state_input_element = ($self->debug == 1) ? $debug_input_element :
  165         qq!\n<input type="hidden" name = "$appletStateName" value ="$base_64_encoded_answer_value">!;
  166     my $reset_button_str = ($reset_button) ?
  167             qq!<br/><input type='button' value='set applet state to restart' onClick="setAppletStateToRestart('$appletName')">
  168                     <input type="button" value="reinitialize applet" onClick="getQE('$appletStateName').value='$base64_initialState'"/><br/>!
  169             : ''
  170     ;
  171   # always base64 encode the hidden answer value to prevent problems with quotes.
  172     #
  173   $state_storage_html_code =
  174                       $reset_button_str.
  175                       $state_input_element.
  176                         qq!<input type="hidden"  name="previous_$appletStateName" value = "$base_64_encoded_answer_value">!;
  177     $state_storage_html_code = qq!<input type="hidden"  name="previous_$appletStateName" value = "$base_64_encoded_answer_value">!
  178                               . $reset_button_str
  179                               . $state_input_element
  180                              ;
  181     $answerBox_code = qq!<br/><input type="input" name="answerBox" value="" size =50><br/>
  182                          <input type="button" value="get Answer from applet" onClick="eval(ww_applet_list['$appletName'].submitActionScript )"/>
  183                          <br/>
  184                         !;
  185     $answerBox_code = ($self->debug == 1) ? $answerBox_code : "";
  186     #######
  187     # insert header material
  188     #######
  189   main::HEADER_TEXT($self->insertHeader());
  190     return main::MODES(TeX=>' {\bf  applet } ', HTML=>$self->insertObject.$main::BR.$state_storage_html_code.$answerBox_code);
  191 }
  192 
  193 =head3 Example problem
  194 
  195 
  196 =cut
  197 
  198 
  199 
  200 =pod
  201 
  202 
  203   DOCUMENT();
  204 
  205   # Load whatever macros you need for the problem
  206   loadMacros("PG.pl",
  207          "PGbasicmacros.pl",
  208          "PGchoicemacros.pl",
  209          "PGanswermacros.pl",
  210          "AppletObjects.pl",
  211          "MathObjects.pl",
  212          "source.pl"
  213         );
  214 
  215   ## Do NOT show partial correct answers
  216   $showPartialCorrectAnswers = 0;
  217 
  218 
  219 
  220   ###################################
  221   # Create  link to applet
  222   ###################################
  223 
  224   $applet = FlashApplet();
  225   my $appletName = "ExternalInterface";
  226   $applet->codebase(findAppletCodebase("$appletName.swf"));
  227   $applet->appletName($appletName);
  228   $applet->appletId($appletName);
  229 
  230   # findAppletCodebase looks for the applet in a list
  231   # of locations specified in global.conf
  232 
  233   ###################################
  234   # Add additional javaScript functions to header section of HTML to
  235   # communicate with the "ExternalInterface" applet.
  236   ###################################
  237 
  238   $applet->header(<<'END_HEADER');
  239   <script type="text/javascript" src="https://devel.webwork.rochester.edu:8002/webwork2_files/js/BrowserSniffer.js">
  240   </script>
  241 
  242 
  243   <script language="JavaScript">
  244     function getBrowser() {
  245         //alert("look for sniffer");
  246       var sniffer = new BrowserSniffer();
  247       //alert("found sniffer" +sniffer);
  248       return sniffer;
  249     }
  250 
  251     function updateStatus(sMessage) {
  252         getQE("playbackStatus").value = sMessage;
  253     }
  254 
  255     function newColor() {
  256 
  257       getApplet("ExternalInterface").updateColor(Math.round(Math.random() * 0xFFFFFF));
  258     }
  259 
  260   </script>
  261   END_HEADER
  262 
  263   ###################################
  264   # Configure applet
  265   ###################################
  266 
  267   # not used here.  Allows for uploading an xml string for the applet
  268 
  269 
  270 
  271 
  272   ###################################
  273   # write the text for the problem
  274   ###################################
  275 
  276   TEXT(beginproblem());
  277 
  278 
  279 
  280   BEGIN_TEXT
  281   \{ $applet->insertAll() \}
  282     $PAR
  283 
  284     The Flash object operates above this line.  The box and button below this line are part of
  285     the WeBWorK problem.  They communicate with the Flash object.
  286     $HR
  287     Status <input type="text" id="playbackStatus" value="started" /><br />
  288     Color <input type="button" value="new color" name="newColorButton" onClick="newColor()" />
  289      $PAR $HR
  290      This flash applet was created by Barbara Kaskosz.
  291 
  292   END_TEXT
  293 
  294   ENDDOCUMENT();
  295 
  296 
  297 
  298 
  299 =cut

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9