[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 5996 - (download) (as text) (annotate)
Sat Feb 7 22:55:29 2009 UTC (10 years, 7 months ago) by gage
File size: 9725 byte(s)
 Fixed typo

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9