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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9