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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9