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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5642 - (view) (download) (as text)

1 : gage 5582 ################################################################################
2 :     # WeBWorK Online Homework Delivery System
3 :     # Copyright 2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
4 : gage 5642 # $CVSHeader: pg/macros/AppletObjects.pl,v 1.6 2008/03/26 02:43:07 gage Exp $
5 : gage 5582 #
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 : gage 5618 AppletObjects.pl - Macro-based front end for the Applet.pm module.
20 : gage 5582
21 :    
22 :     =head1 DESCRIPTION
23 :    
24 :     This subroutines in this
25 :     file provide mechanisms to insert Flash applets (and later Java applets)
26 :     into a WeBWorK problem.
27 :    
28 :    
29 :     =head1 SEE ALSO
30 :    
31 :     L<Applets.pm>.
32 :    
33 :     =cut
34 :    
35 :    
36 : gage 5618 sub _AppletObjects_init{}; # don't reload this file
37 :    
38 :    
39 :     main::HEADER_TEXT(<<'END_HEADER_TEXT');
40 :     <script language="javascript">AC_FL_RunContent = 0;</script>
41 :     <script src="/webwork2_files/applets/AC_RunActiveContent.js" language="javascript">
42 :     </script>
43 :     <script src="/webwork2_files/js/Base64.js" language="javascript">
44 :     </script>
45 :    
46 :     <script language="JavaScript">
47 :     var applet_initializeAction_list = new Object;
48 :     var applet_submitAction_list = new Object;
49 :     var applet_setState_list = new Object;
50 :     var applet_getState_list = new Object;
51 :     var applet_config_list = new Object;
52 :    
53 :    
54 :     function base64Q(str) {
55 : gage 5626 return ( !str.match(/<XML/i) && !str.match(/<?xml/i));
56 : gage 5618 }
57 :    
58 :     function submitAction() {
59 :     //alert("submit Action" );
60 :     for (var applet in applet_submitAction_list) {
61 :     //alert(applet);
62 :     applet_submitAction_list[applet]();
63 :     }
64 :    
65 :     }
66 :     // Give some time delay before initializing
67 :     function initializeAction() {
68 :     //alert("ready to initialize");
69 :     // give some delay to allow flash applet to load. FIXME
70 :     window.setTimeout("initializeAction1()",200);
71 :     //initializeAction1();
72 :     }
73 :     function initializeAction1() {
74 :    
75 :     for (var appletName in applet_initializeAction_list) {
76 :     //alert("initialize: " + appletName);
77 :     try{
78 :     applet_config_list[appletName]();
79 :     } catch(e) {
80 :     alert("unable to configure " + appletName + " It may have been slow to load. " +e );
81 :     }
82 :     try{
83 :     applet_initializeAction_list[appletName]();
84 :     } catch(e) {
85 :     alert("unable to initialize " + appletName + " It may have been slow to load. " +e );
86 :     }
87 :     }
88 :     }
89 :    
90 :    
91 :     var flash;
92 :     function getApplet(appletName) {
93 :     var isIE = navigator.appName.indexOf("Microsoft") != -1;
94 :     var obj = (isIE) ? window[appletName] : window.document[appletName];
95 :     //return window.document[appletName];
96 :     if (obj && (obj.name = appletName)) {
97 :     return( obj );
98 :     } else {
99 :     alert ("can't find applet " + appletName);
100 :     }
101 :     }
102 :    
103 :     function listQuestionElements() { // list all HTML input and textarea elements in main problem form
104 :     var isIE = navigator.appName.indexOf("Microsoft") != -1;
105 :     var elementList = (isIE) ? document.getElementsByTagName("input") : document.problemMainForm.getElementsByTagName("input");
106 :     var str=elementList.length +" Question Elements\n type | name = value < id > \n";
107 :     for( var i=0; i< elementList.length; i++) {
108 :     str = str + " "+i+" " + elementList[i].type
109 :     + " | " + elementList[i].name
110 :     + "= " + elementList[i].value +
111 :     " <" + elementList[i].id + ">\n";
112 :     }
113 :     elementList = (isIE) ? document.getElementsByTagName("textarea") : document.problemMainForm.getElementsByTagName("textarea");
114 :     for( var i=0; i< elementList.length; i++) {
115 :     str = str + " "+i+" " + elementList[i].type
116 :     + " | " + elementList[i].name
117 :     + "= " + elementList[i].value +
118 :     " <" + elementList[i].id + ">\n";
119 :     }
120 :     alert(str +"\n Place listQuestionElements() at end of document in order to get all form elements!");
121 :     }
122 :    
123 :     function getQE(name1) { // get Question Element in problemMainForm by name
124 :     var isIE = navigator.appName.indexOf("Microsoft") != -1;
125 :     var obj = (isIE) ? document.getElementById(name1)
126 :     :document.problemMainForm[name1];
127 :     // needed for IE -- searches id and name space so it can be unreliable if names are not unique
128 :     if (!obj || obj.name != name1) {
129 :     alert("Can't find element " + name1);
130 :     listQuestionElements();
131 :     } else {
132 :     return( obj );
133 :     }
134 :    
135 :     }
136 :     function getQuestionElement(name1) {
137 :     return getQE(name1);
138 :     }
139 :    
140 :     </script>
141 :    
142 :     END_HEADER_TEXT
143 :    
144 :    
145 :    
146 : gage 5582 =head3
147 :     FlashApplet
148 :    
149 :     Useage: $applet = FlashApplet();
150 :    
151 :     =cut
152 :    
153 :     sub FlashApplet {
154 : gage 5597 return new FlashApplet(@_);
155 : gage 5582
156 :     }
157 :    
158 : gage 5618 sub JavaApplet {
159 :     return new JavaApplet(@_);
160 : gage 5582
161 : gage 5618 }
162 :    
163 :     package Applet;
164 :    
165 :    
166 :    
167 : gage 5582 =head2 Methods
168 :    
169 :     =cut
170 :    
171 :     ## this method is defined in this file
172 :     ## because the main subroutines HEADER_TEXT and MODES are
173 :     ## not available to the module FlashApplet when that file
174 :     ## is compiled (at the time the apache child process is first initialized)
175 :    
176 : gage 5618 =head3 insertAll
177 : gage 5582
178 : gage 5618 Useage: TEXT( $applet->insertAll() );
179 :     \{ $applet->insertAll() \} (used within BEGIN_TEXT/END_TEXT blocks)
180 : gage 5582
181 :     =cut
182 :    
183 :     =pod
184 :    
185 :     Inserts applet at this point in the HTML code. (In TeX mode a message "Applet" is written.) This method
186 :     also adds the applets header material into the header portion of the HTML page. It effectively inserts
187 : gage 5618 the outputs of both C<$applet-E<gt>insertHeader> and C<$applet-E<gt>insertObject> (defined in L<Applet.pm> )
188 :     in the appropriate places.
189 : gage 5582
190 : gage 5618 Note: This method is defined here rather than in Applet.pl because it
191 :     requires access to the RECORD_FORM_LABEL subroutine
192 :     and to the routine accessing the stored values of the answers. These are defined in main::.
193 :    
194 : gage 5582 =cut
195 :    
196 : gage 5618 sub insertAll { ## inserts both header text and object text
197 : gage 5582 my $self = shift;
198 : gage 5618 my %options = @_;
199 :     $self->debug( (defined($options{debug}) and $options{debug}==1) ? 1 : 0 );
200 :     my $reset_button = $options{reset_button} || 0;
201 :     # prepare html code for storing state
202 :     my $appletName = $self->appletName;
203 :     my $appletStateName = "${appletName}_state";
204 :     my $getState = $self->getStateAlias;
205 :     my $setState = $self->setStateAlias;
206 :     my $base64_initialState = $self->base64_state;
207 :     main::RECORD_FORM_LABEL($appletStateName); #this insures that they'll be saved from one invocation to the next
208 :     #main::RECORD_FORM_LABEL("previous_$appletStateName");
209 :     my $answer_value = '';
210 :     $answer_value = ${$main::inputs_ref}{$appletStateName} if defined(${$main::inputs_ref}{$appletStateName});
211 :    
212 :     if ( defined( $main::rh_sticky_answers->{$appletStateName} ) ) {
213 :     $answer_value = shift( @{ $main::rh_sticky_answers->{$appletStateName} });
214 :     $answer_value = '' unless defined($answer_value);
215 :     }
216 :     $answer_value =~ tr/\\$@`//d; #`## make sure student answers can not be interpolated by e.g. EV3
217 :     $answer_value =~ s/\s+/ /g; ## remove excessive whitespace from student answer
218 :    
219 :     #######
220 :     # insert a hidden variable to hold the applet's state (debug =>1 makes it visible for debugging and provides debugging buttons)
221 :     #######
222 :     my $base_64_encoded_answer_value = ($answer_value =~/<XML>/i)? encode_base64($answer_value) : $answer_value;
223 :     my $decoded_answer_value = ($answer_value =~/<XML>/i) ? $answer_value : decode_base64($answer_value);
224 :     my $debug_input_element = qq!\n<textarea rows="4" cols="80"
225 :     name = "$appletStateName">$decoded_answer_value</textarea><br/>
226 : gage 5623 <input type="button" value="$getState"
227 :     onClick="applet_getState_list['$appletName']()"
228 :     >
229 :     <input type="button" value="$setState"
230 :     onClick="var tmp = getQE('$appletStateName').value;
231 :     applet_setState_list['$appletName'](tmp);"
232 :     >
233 : gage 5618 !;
234 :     my $state_input_element = ($self->debug == 1) ? $debug_input_element :
235 :     qq!\n<input type="hidden" name = "$appletStateName" value ="$base_64_encoded_answer_value">!;
236 :     my $reset_button_str = ($reset_button) ?
237 : gage 5642 qq!<br/><input type='button' value='set applet state empty' onClick="applet_setState_list['$appletName']('<xml></xml>')">
238 :     <input type="button" value="reinitialize applet" onClick="initializeAction()"/>!
239 : gage 5618 : ''
240 :     ;
241 :     # always base64 encode the hidden answer value to prevent problems with quotes.
242 :     #
243 :     $state_storage_html_code =
244 :     $reset_button_str.
245 :     $state_input_element.
246 :     qq!<input type="hidden" name="previous_$appletStateName" value = "$base_64_encoded_answer_value">!;
247 :     #######
248 :     # insert header material
249 :     #######
250 : gage 5582 main::HEADER_TEXT($self->insertHeader());
251 : gage 5618 return main::MODES(TeX=>' {\bf applet } ', HTML=>$self->insertObject.$main::BR.$state_storage_html_code);
252 : gage 5582 }
253 :    
254 :     =head3 Example problem
255 :    
256 :    
257 :     =cut
258 :    
259 :    
260 :    
261 :     =pod
262 :    
263 :    
264 :     DOCUMENT();
265 :    
266 :     # Load whatever macros you need for the problem
267 :     loadMacros("PG.pl",
268 :     "PGbasicmacros.pl",
269 :     "PGchoicemacros.pl",
270 :     "PGanswermacros.pl",
271 :     "AppletObjects.pl",
272 :     "MathObjects.pl",
273 :     "source.pl"
274 :     );
275 :    
276 :     ## Do NOT show partial correct answers
277 :     $showPartialCorrectAnswers = 0;
278 :    
279 :    
280 :    
281 :     ###################################
282 :     # Create link to applet
283 :     ###################################
284 :    
285 :     $applet = FlashApplet();
286 :     my $appletName = "ExternalInterface";
287 :     $applet->codebase(findAppletCodebase("$appletName.swf"));
288 :     $applet->appletName($appletName);
289 :     $applet->appletId($appletName);
290 :    
291 :     # findAppletCodebase looks for the applet in a list
292 :     # of locations specified in global.conf
293 :    
294 :     ###################################
295 : gage 5618 # Add additional javaScript functions to header section of HTML to
296 : gage 5593 # communicate with the "ExternalInterface" applet.
297 : gage 5582 ###################################
298 :    
299 :     $applet->header(<<'END_HEADER');
300 :     <script type="text/javascript" src="https://devel.webwork.rochester.edu:8002/webwork2_files/js/BrowserSniffer.js">
301 :     </script>
302 :    
303 :    
304 :     <script language="JavaScript">
305 :     function getBrowser() {
306 :     //alert("look for sniffer");
307 :     var sniffer = new BrowserSniffer();
308 :     //alert("found sniffer" +sniffer);
309 :     return sniffer;
310 :     }
311 :    
312 :     function updateStatus(sMessage) {
313 : gage 5618 getQE("playbackStatus").value = sMessage;
314 : gage 5582 }
315 :    
316 :     function newColor() {
317 : gage 5618
318 :     getApplet("ExternalInterface").updateColor(Math.round(Math.random() * 0xFFFFFF));
319 : gage 5582 }
320 :    
321 :     </script>
322 :     END_HEADER
323 :    
324 :     ###################################
325 :     # Configure applet
326 :     ###################################
327 :    
328 :     # not used here. Allows for uploading an xml string for the applet
329 :    
330 :    
331 :    
332 :    
333 :     ###################################
334 :     # write the text for the problem
335 :     ###################################
336 :    
337 :     TEXT(beginproblem());
338 :    
339 :    
340 :    
341 :     BEGIN_TEXT
342 : gage 5618 \{ $applet->insertAll() \}
343 : gage 5582 $PAR
344 :    
345 :     The Flash object operates above this line. The box and button below this line are part of
346 :     the WeBWorK problem. They communicate with the Flash object.
347 :     $HR
348 :     Status <input type="text" id="playbackStatus" value="started" /><br />
349 :     Color <input type="button" value="new color" name="newColorButton" onClick="newColor()" />
350 :     $PAR $HR
351 :     This flash applet was created by Barbara Kaskosz.
352 :    
353 :     END_TEXT
354 :    
355 :     ENDDOCUMENT();
356 :    
357 :    
358 :    
359 :    
360 :     =cut

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9