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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9