[system] / trunk / pg / lib / Applet.pm Repository:
ViewVC logotype

View of /trunk/pg/lib/Applet.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5574 - (download) (as text) (annotate)
Tue Oct 30 15:57:04 2007 UTC (12 years ago) by gage
File size: 10005 byte(s)
Preliminary version of Applet.pm which will provide a uniform interface
for including FlashApplets (eventually other applets) in a webwork
problem.   This remains experimental so the interface will probably
change.

    1 ################################################################################
    2 # WeBWorK Online Homework Delivery System
    3 # Copyright  2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
    4 # $CVSHeader: pg/macros/PG.pl,v 1.34 2007/10/25 17:11:59 sh002i 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 Applet.pl - Provides code for inserting FlashApplets and JavaApplets into webwork problems
   20 
   21 =head1 SYNPOSIS
   22 
   23   ###################################
   24   # Create  link to applet
   25   ###################################
   26   my $appletName = "LineThruPointsWW";
   27   $applet = new FlashApplet(
   28      codebase   => findAppletCodebase("$appletName.swf"),
   29      appletName => $appletName,
   30      appletId   => $appletName,
   31      submitActionAlias => 'checkAnswer',
   32   );
   33 
   34   ###################################
   35   # Configure applet
   36   ###################################
   37 
   38   #xml data to set up the problem-rac
   39   $applet->xmlString(qq{<XML>
   40   <point xval='$xval_1' yval='$yval_1' />
   41   <point xval='$xval_2' yval='$yval_2' />
   42   </XML>});
   43 
   44 
   45   ###################################
   46   # insert applet header material
   47   ###################################
   48   HEADER_TEXT($applet->insertHeader );
   49 
   50   ###################################
   51   # Text section
   52   #
   53 
   54   ###################################
   55   #insert applet into body
   56   ###################################
   57   TEXT( MODES(TeX=>'object code', HTML=>$applet->insertObject));
   58 
   59 
   60 =head1 DESCRIPTION
   61 
   62 This file provides an object to store in one place
   63 all of the information needed to call an applet.
   64 
   65 The object FlashApplet has defaults for inserting flash applets.
   66 
   67 =over
   68 
   69 =item *
   70 
   71 =item *
   72 
   73 =back
   74 
   75 (not yet completed)
   76 
   77 The module JavaApplet has defaults for inserting java applets.
   78 
   79 The module Applet will store common code for the two types of applet.
   80 
   81 =head1 USAGE
   82 
   83 This file is included by listing it in the modules section of global.conf.
   84 
   85 =cut
   86 
   87 
   88 
   89 package Applet;
   90 
   91 
   92 package FlashApplet;
   93 
   94 
   95 use MIME::Base64 qw( encode_base64 decode_base64);
   96 
   97 use constant DEFAULT_HEADER_TEXT =><<'END_HEADER_SCRIPT';
   98     <script language="javascript">AC_FL_RunContent = 0;</script>
   99     <script src="http://hosted2.webwork.rochester.edu/webwork2_files/applets/AC_RunActiveContent.js" language="javascript"></script>
  100 
  101     <script type="text/javascript" src="http://hosted2.webwork.rochester.edu/webwork2_files/js/BrowserSniffer.js">
  102     </script>
  103   <script language="JavaScript">
  104 
  105   var flash;
  106   function getFlashMovie(movieName) {
  107       var isIE = navigator.appName.indexOf("Microsoft") != -1;
  108       return (isIE) ? window[movieName] : window.document[movieName];
  109       //return window.document[movieName];
  110    }
  111   function getBrowser() {
  112     return new BrowserSniffer();
  113   }
  114 
  115   function initialize() {
  116       getFlashMovie("$appletId").$initializeAction("$base64_xmlString");
  117   }
  118   function submitAction() {
  119     document.problemMainForm.$returnFieldName.value = getFlashMovie("$appletId").$submitAction();
  120    }
  121 
  122     </script>,
  123 
  124 END_HEADER_SCRIPT
  125 
  126 
  127 #   <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0"
  128 #            width="550" height="400" id="$appletId" align="middle">
  129 #       <param name="allowScriptAccess" value="sameDomain" />
  130 #       <param name="allowFullScreen" value="false" />
  131 #       <param name="movie" value="$appletName.swf" />
  132 #       <param name="quality" value="high" />
  133 #       <param name="bgcolor" value="#ffffcc" />
  134 #       <embed src="$codebase/$appletName.swf" quality="high" bgcolor="#ffffcc" width="550" height="400" name="$appletName"
  135 #          align="middle"  id="$appletId",
  136 #          align="middle" allowScriptAccess="sameDomain"
  137 #          allowFullScreen="false"
  138 #         type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
  139 #         <param name="quality" value="high" /><param name="bgcolor" value="#ffffcc" />
  140 #     </object>
  141 
  142 use constant DEFAULT_OBJECT_TEXT =><<'END_OBJECT_TEXT';
  143   <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
  144              id="ExternalInterface" width="500" height="375"
  145              codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">
  146          <param name="movie" value="$codebase/$appletName.swf" />
  147          <param name="quality" value="high" />
  148          <param name="bgcolor" value="#869ca7" />
  149          <param name="allowScriptAccess" value="sameDomain" />
  150          <embed src="$codebase/$appletName.swf" quality="high" bgcolor="#869ca7"
  151              width="550" height="400" name="$appletName" align="middle" id="$appletID"
  152              play="true" loop="false" quality="high" allowScriptAccess="sameDomain"
  153              type="application/x-shockwave-flash"
  154              pluginspage="http://www.macromedia.com/go/getflashplayer">
  155          </embed>
  156 
  157      </object>
  158 END_OBJECT_TEXT
  159 
  160 
  161 sub new {
  162    my $class = shift;
  163    my $self = {
  164     host =>'',
  165     port => '',
  166     path => '',
  167     appletName =>'',
  168     codebase=>'',
  169     appletId  =>'',
  170     params    =>undef,
  171     base64_xmlString => 'foobar',
  172     initializeActionAlias => 'setupProblem',
  173     submitActionAlias  => 'checkAnswer',
  174     returnFieldName    => 'receivedField',
  175     headerText   => DEFAULT_HEADER_TEXT(),
  176     objectText   => DEFAULT_OBJECT_TEXT(),
  177     @_,
  178   };
  179   bless $self, $class;
  180   #$self -> _initialize(@_);
  181   return $self;
  182 }
  183 
  184 sub  header {
  185   my $self = shift;
  186   if ($_[0] eq "reset") {  # $applet->header('reset');  erases default header text.
  187     $self->{headerText}='';
  188   } else {
  189     $self->{headerText} .= join("",@_);  # $applet->header(new_text); concatenates new_text to existing header.
  190   }
  191     $self->{headerText};
  192 }
  193 sub  object {
  194   my $self = shift;
  195   if ($_[0] eq "reset") {
  196     $self->{objectText}='';
  197   } else {
  198     $self->{objectText} .= join("",@_);
  199   }
  200     $self->{objectText};
  201 }
  202 sub params {
  203   my $self = shift;
  204   if (ref($_[0]) =~/HASH/) {
  205     $self->{params} = shift;
  206   } elsif ( $_[0] =~ '') {
  207     # do nothing (read)
  208   } else {
  209     warn "You must enter a reference to a hash for the parameter list";
  210   }
  211   $self->{params};
  212 }
  213 
  214 sub initializeActionAlias {
  215   my $self = shift;
  216   $self->{initializeActionAlias} = shift ||$self->{initializeActionAlias}; # replace the current contents if non-empty
  217     $self->{initializeActionAlias};
  218 }
  219 
  220 sub submitActionAlias {
  221   my $self = shift;
  222   $self->{submitActionAlias} = shift ||$self->{submitActionAlias}; # replace the current contents if non-empty
  223     $self->{submitActionAlias};
  224 }
  225 sub returnFieldName {
  226   my $self = shift;
  227   $self->{returnFieldName} = shift ||$self->{returnFieldName}; # replace the current contents if non-empty
  228     $self->{returnFieldName};
  229 }
  230 sub codebase {
  231   my $self = shift;
  232   $self->{codebase} = shift ||$self->{codebase}; # replace the current codebase if non-empty
  233     $self->{codebase};
  234 }
  235 sub appletName {
  236   my $self = shift;
  237   $self->{appletName} = shift ||$self->{appletName}; # replace the current appletName if non-empty
  238     $self->{appletName};
  239 }
  240 sub appletId {
  241   my $self = shift;
  242   $self->{appletId} = shift ||$self->{appletId}; # replace the current appletName if non-empty
  243     $self->{appletId};
  244 }
  245 sub xmlString {
  246   my $self = shift;
  247   my $str = shift;
  248   $self->{base64_xmlString} =  encode_base64($str)   ||$self->{base64_xmlString}; # replace the current string if non-empty
  249   $self->{base64_xmlString} =~ s/\n//g;
  250     decode_base64($self->{base64_xmlString});
  251 }
  252 
  253 sub base64_xmlString{
  254   my $self = shift;
  255   $self->{base64_xmlString} = shift ||$self->{base64_xmlString}; # replace the current string if non-empty
  256     $self->{base64_xmlString};
  257 }
  258 
  259 #FIXME
  260 # need to be able to adjust header material
  261 
  262 sub insertHeader {
  263     my $self = shift;
  264     my $codebase = $self->{codebase};
  265     my $appletId = $self->{appletId};
  266     my $appletName = $self->{appletName};
  267     my $base64_xmlString = $self->{base64_xmlString};
  268     my $initializeAction = $self->{initializeActionAlias};
  269     my $submitAction = $self->{submitActionAlias};
  270     my $returnFieldName= $self->{returnFieldName};
  271     my $headerText = $self->header();
  272     $headerText =~ s/(\$\w+)/$1/gee;   # interpolate variables p17 of Cookbook
  273 
  274     return $headerText;
  275 
  276 
  277 }
  278 
  279 
  280 # <script language="javascript">
  281 #   if (AC_FL_RunContent == 0) {
  282 #     alert("This page requires AC_RunActiveContent.js.");
  283 #   } else {
  284 #     AC_FL_RunContent(
  285 #       'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
  286 #       'width', '100%',
  287 #       'height', '100%',
  288 #       'src', 'http://$codebase/$appletName',
  289 #       'quality', 'high',
  290 #       'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
  291 #       'align', 'middle',
  292 #       'play', 'true',
  293 #       'loop', 'true',
  294 #       'scale', 'showall',
  295 #       'wmode', 'window',
  296 #       'devicefont', 'false',
  297 #       'id', '$appletId',
  298 #       'bgcolor', '#ffffcc',
  299 #       'name', '$appletName',
  300 #       'menu', 'true',
  301 #       'allowFullScreen', 'false',
  302 #       'allowScriptAccess','sameDomain',
  303 #       'movie', '$appletName',
  304 #       'salign', ''
  305 #       ); //end AC code
  306 #   }
  307 # </script>
  308 sub insertObject {
  309     my $self = shift;
  310     my $codebase = $self->{codebase};
  311     my $appletId = $self->{appletId};
  312     my $appletName = $self->{appletName};
  313     $codebase = findAppletCodebase("$appletName.swf") unless $codebase;
  314     $objectText = $self->{objectText};
  315     $objectText =~ s/(\$\w+)/$1/gee;
  316     return $objectText;
  317 }
  318 
  319 sub initialize  {
  320     my $self = shift;
  321   return q{
  322     <script>
  323       initialize();
  324       // this should really be done in the <body> tag
  325     </script>
  326   };
  327 
  328 }
  329 
  330 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9