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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 6013 Revision 6023
1################################################################################ 1################################################################################
2# WeBWorK Online Homework Delivery System 2# WeBWorK Online Homework Delivery System
3# Copyright 2000-2007 The WeBWorK Project, http://openwebwork.sf.net/ 3# Copyright 2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
4# $CVSHeader: pg/lib/Applet.pm,v 1.16 2009/02/19 03:04:22 gage Exp $ 4# $CVSHeader: pg/lib/Applet.pm,v 1.18 2009/03/10 12:10:36 gage Exp $
5# 5#
6# This program is free software; you can redistribute it and/or modify it under 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 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 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. 9# version, or (b) the "Artistic License" which comes with this package.
108 108
109 getApplet(appletName) -- finds the applet path in the DOM 109 getApplet(appletName) -- finds the applet path in the DOM
110 110
111 submitAction() -- calls the submit action of the applets 111 submitAction() -- calls the submit action of the applets
112 112
113 initializeWWquestion() -- calls the initialize action of the applets 113 initializeWWquestion() -- calls the initialize action of the applets
114 114
115 getQE(name) -- gets an HTML element of the question by name 115 getQE(name) -- gets an HTML element of the question by name
116 or by id. Be sure to keep all names and ids 116 or by id. Be sure to keep all names and ids
117 unique within a given PG question. 117 unique within a given PG question.
118 118
124 Usage: Place this at the END of the question, just before END_DOCUMENT(): 124 Usage: Place this at the END of the question, just before END_DOCUMENT():
125 125
126 TEXT(qq!<script> listQuestionElements() </script>!); 126 TEXT(qq!<script> listQuestionElements() </script>!);
127 ENDDOCUMENT(); 127 ENDDOCUMENT();
128 to obtain a list of all of the HTML elements in the question 128 to obtain a list of all of the HTML elements in the question
129 129
130 ----------------------------------------------------------------------------
131
132
130 List of accessor methods made available by the FlashApplet class: 133 List of accessor methods made available by the FlashApplet class:
131 Usage: $current_value = $applet->method(new_value or empty) 134 Usage: $current_value = $applet->method(new_value or empty)
132 These can also be set when creating the class -- for exampe: 135 These can also be set when creating the class -- for exampe:
133 $applet = new FlashApplet( 136 $applet = new FlashApplet(
134 # can be replaced by $applet =FlashApplet() when using AppletObjects.pl 137 # can be replaced by $applet =FlashApplet() when using AppletObjects.pl
158 config configuration are those customizable attributes of the applet which don't 161 config configuration are those customizable attributes of the applet which don't
159 change as it is used. When stored in hidden answer fields 162 change as it is used. When stored in hidden answer fields
160 it is usually stored in base64 encoded format. 163 it is usually stored in base64 encoded format.
161 base64_config base64 encode version of the contents of config 164 base64_config base64 encode version of the contents of config
162 165
163 configAlias (default: config ) names the applet command called with the contents of $self->config 166 configAlias (default: setConfig ) names the applet command called with the contents of $self->config
164 to configure the applet. The parameters are passed to the applet in plain text using <xml> 167 to configure the applet. The parameters are passed to the applet in plain text using <xml>
165 The outer tags must be <xml> ..... </xml> 168 The outer tags must be <xml> ..... </xml>
169 setConfigAlias (default: setConfig) -- a synonym for configAlias
170 getConfigAlias (default: getConfig) -- retrieves the configuration from the applet. This is used
171 mainly for debugging. In principal the configuration remains the same for a given instance
172 of the applet -- i.e. for the homework question for a single student. The state however
173 will change depending on the interactions between the student and the applet.
166 state state consists of those customizable attributes of the applet which change 174 initialState the state consists of those customizable attributes of the applet which change
167 as the applet is used. It is stored by the calling .pg question so that 175 as the applet is used by the student. It is stored by the calling .pg question so that
168 when revisiting the question the applet
169 will be restored to the same state it was left in when the question was last 176 when revisiting the question the applet will be restored to the same state it was left in when the question was last
170 viewed. 177 viewed.
171 178
172 getStateAlias (default: getState) alias for command called to read the current state of the applet. 179 getStateAlias (default: getState) alias for command called to read the current state of the applet.
173 The state is passed in plain text xml format with outer tags: <xml>....</xml> 180 The state is passed in plain text xml format with outer tags: <xml>....</xml>
174 setStateAlias (default: setState) alias for the command called to reset the state of the applet. 181 setStateAlias (default: setState) alias for the command called to reset the state of the applet.
184 returnFieldName -- (deprecated) synonmym for answerBox 191 returnFieldName -- (deprecated) synonmym for answerBox
185 192
186 193
187=cut 194=cut
188 195
196=head4 More details
189 197
198There are three different "images" of the applet. The first is the java or flash applet itself. The object that actually does the work.
199The second is a perl image of the applet -- henceforth the perlApplet -- which is configured in the .pg file and allows a WeBWorK question
200to communicate with the applet. The third image is a javaScript image of the applet -- henceforth the jsApplet which is a mirror of the perlApplet
201but is available to the javaScript code setup and executed in the virtual HTML page defined by the .pg file of the WeBWorK question. One can think of
202the jsApplet as a runtime version of the perlApplet since it can be accessed and modified after the virtual HTML page has been created by
203the PG rendering process.
190 204
205The perlApplet is initialized by $newApplet = new flashApplet( appletName=>'myApplet',..... ); The jsApplet is automatically defined in
206ww_applet_list["myApplet"] by copying the instance variables of $newApplet to a corresponding javaScript object. So $newApplet->{appletName}
207corresponds to ww_applet_list["myApplet"].appletName. (This paragraph is not yet fully implemented :-().
208
209Currently all messages read by the applet are xml text. If some of the code needs to be printed in the HTML header than it is converted
210to a base64 constant and then converted back to text form when it is read by an javaScript subroutine.
211
212=cut
213
214=head4 Requirements for applets
215
216The following methods are desirable in an applet that preserves state in a WW question. None of them are required.
217
218 setState(str) (default: setXML)
219 -- set the current state of the applet from an xml string
220 -- should be able to accept an empty string or a string of
221 the form <XML>.....</XML> without creating errors
222 -- can be designed to receive other forms of input if it is
223 coordinated with the WW question.
224 getState() (default: getXML)
225 -- return the current state of the applet in an xml string.
226 -- an empty string or a string of the form <XML>.....</XML>
227 are the standard responses.
228 -- can be designed to return other strings if it is
229 coordinated with the WW question.
230 setConfig(str) (default: setConfig)
231 -- If the applet allows configuration this configures the applet
232 from an xml string
233 -- should be able to accept an empty string or a string of the
234 form <XML>.....</XML> without creating errors
235 -- can be designed to receive other forms of input if it is
236 coordinated with the WW question.
237 getConfig (default: getConfig)
238 -- This returns a string defining the configuration of the
239 applet in an xml string
240 -- an empty string or a string of the form <XML>.....</XML>
241 are the standard responses.
242 -- can be designed to return other strings if it is
243 coordinated with the WW question.
244 -- this method is used for debugging to ensure that
245 the configuration was set as expected.
246 getAnswer (default: getAnswer)
247 -- Returns a string (usually NOT xml) which is the
248 response that the student is submitting to answer
249 the WW question.
250
251
252=cut
191 253
192sub new { 254sub new {
193 my $class = shift; 255 my $class = shift;
194 my $self = { 256 my $self = {
195 appletName =>'', 257 appletName =>'',
198# appletId =>'', #always use identical applet Id's and applet Names 260# appletId =>'', #always use identical applet Id's and applet Names
199 params =>undef, 261 params =>undef,
200 width => 550, 262 width => 550,
201 height => 400, 263 height => 400,
202 bgcolor => "#869ca7", 264 bgcolor => "#869ca7",
203 base64_state => undef, # this is an state to use for initializing the first occurence of the question. 265 base64_state => undef, # this is a state to use for initializing the first occurence of the question.
204 base64_config => undef, # this is the initial (and final?) configuration 266 base64_config => undef, # this is the initial (and final?) configuration
267# configuration => '', # configuration defining the applet
268 initialState => '', # initial state. (I'm considering storing everything as ascii and converting on the fly to base64 when needed.)
205 getStateAlias => 'getXML', 269 getStateAlias => 'getXML',
206 setStateAlias => 'setXML', 270 setStateAlias => 'setXML',
271 configAlias => '', # deprecated
207 configAlias => 'config', 272 getConfigAlias => 'getConfig',
273 setConfigAlias => 'setConfig',
208 initializeActionAlias => 'setXML', 274 initializeActionAlias => 'setXML',
209 submitActionAlias => 'getXML', 275 submitActionAlias => 'getXML',
210 submitActionScript => '', # script executed on submitting the WW question 276 submitActionScript => '', # script executed on submitting the WW question
211 answerBox => 'answerBox', 277 answerBoxAlias => 'answerBox',
278 answerBox => '', # deprecated
279 returnFieldName => '', # deprecated
212 headerText => DEFAULT_HEADER_TEXT(), 280 headerText => DEFAULT_HEADER_TEXT(),
213 objectText => '', 281 objectText => '',
214 debug => 0, 282 debug => 0,
215 @_, 283 @_,
216 }; 284 };
217 bless $self, $class; 285 bless $self, $class;
218 $self->state('<xml></xml>'); 286 $self->initialState('<xml></xml>');
287 if ($self->{returnFieldName}) or $self->{answerBox} ) { # backward compatibility
288 warn "use answerBoxAlias instead of returnFieldName or answerBox";
289 $self->{answerBox}='';
290 $self->{returnFieldName}='';
291 }
292 if ($self->{configAlias}) { # backward compatibility
293 warn "use setConfigAlias instead of configAlias";
294 $self->{configAlias}='';
295 }
219 $self->config('<xml></xml>'); 296 $self->config('<xml></xml>');
220 return $self; 297 return $self;
221} 298}
222 299
223sub header { 300sub header {
277 $self->{setStateAlias} = shift ||$self->{setStateAlias}; # replace the current contents if non-empty 354 $self->{setStateAlias} = shift ||$self->{setStateAlias}; # replace the current contents if non-empty
278 $self->{setStateAlias}; 355 $self->{setStateAlias};
279} 356}
280sub configAlias { 357sub configAlias {
281 my $self = shift; 358 my $self = shift;
282 $self->{configAlias} = shift ||$self->{configAlias}; # replace the current contents if non-empty 359 $self->{setConfigAlias} = shift ||$self->{setConfigAlias}; # replace the current contents if non-empty
283 $self->{configAlias}; 360 $self->{setConfigAlias};
284} 361}
285sub returnFieldName { 362sub setConfigAlias {
286 my $self = shift; 363 my $self = shift;
287 $self->{answerBox} = shift ||$self->{answerBox}; # replace the current contents if non-empty 364 $self->{setConfigAlias} = shift ||$self->{setConfigAlias}; # replace the current contents if non-empty
288 $self->{answerBox}; 365 $self->{setConfigAlias};
289} 366}
367sub getConfigAlias {
368 my $self = shift;
369 $self->{getConfigAlias} = shift ||$self->{getConfigAlias}; # replace the current contents if non-empty
370 $self->{getConfigAlias};
371}
372
290sub answerBox { 373sub answerBoxName {
291 my $self = shift; 374 my $self = shift;
292 $self->{answerBox} = shift ||$self->{answerBox}; # replace the current contents if non-empty 375 $self->{answerBox} = shift ||$self->{answerBox}; # replace the current contents if non-empty
293 $self->{answerBox}; 376 $self->{answerBox};
294} 377}
295sub codebase { 378sub codebase {
334 $self->{debug}; 417 $self->{debug};
335} 418}
336sub appletId { 419sub appletId {
337 appletName(@_); 420 appletName(@_);
338} 421}
422
423sub initialState {
424 my $self = shift;
425 my $str = shift;
426 $self->{initialState} = $str ||$self->{initialState}; # replace the current string if non-empty
427 $self->{initialState};
428}
429
430sub config {
431 my $self = shift;
432 my $str = shift;
433 $self->{base64_config} = encode_base64($str) || $self->{base64_config}; # replace the current string if non-empty
434 $self->{base64_config} =~ s/\n//g;
435 decode_base64($self->{base64_config});
436}
437#######################
438# soon to be deprecated?
439#######################
339sub state { 440sub state {
340 my $self = shift; 441 my $self = shift;
341 my $str = shift; 442 my $str = shift;
342 $self->{base64_state} = encode_base64($str) ||$self->{base64_state}; # replace the current string if non-empty 443 $self->{base64_state} = encode_base64($str) ||$self->{base64_state}; # replace the current string if non-empty
343 $self->{base64_state} =~ s/\n//g; 444 $self->{base64_state} =~ s/\n//g;
344 decode_base64($self->{base64_state}); 445 decode_base64($self->{base64_state});
345} 446}
346
347sub base64_state{ 447sub base64_state{
348 my $self = shift; 448 my $self = shift;
349 $self->{base64_state} = shift ||$self->{base64_state}; # replace the current string if non-empty 449 $self->{base64_state} = shift ||$self->{base64_state}; # replace the current string if non-empty
350 $self->{base64_state}; 450 $self->{base64_state};
351} 451}
352sub config { 452
353 my $self = shift;
354 my $str = shift;
355 $self->{base64_config} = encode_base64($str) || $self->{base64_config}; # replace the current string if non-empty
356 $self->{base64_config} =~ s/\n//g;
357 decode_base64($self->{base64_config});
358}
359sub base64_config { 453sub base64_config {
360 my $self = shift; 454 my $self = shift;
361 $self->{base64_config} = shift ||$self->{base64_config}; # replace the current string if non-empty 455 $self->{base64_config} = shift ||$self->{base64_config}; # replace the current string if non-empty
362 $self->{base64_config} =$self->{base64_config}; 456 $self->{base64_config} =$self->{base64_config};
363 $self->{base64_config}; 457 $self->{base64_config};
364} 458}
459
460sub returnFieldName {
461 my $self = shift;
462 warn "use answerBoxName instead of returnFieldName";
463}
464sub answerBox {
465 my $self = shift;
466 warn "use answerBoxName instead of AnswerBox";
467}
468#########################
365#FIXME 469#FIXME
366# need to be able to adjust header material 470# need to be able to adjust header material
367 471
368sub insertHeader { 472sub insertHeader {
369 my $self = shift; 473 my $self = shift;
375 my $initializeAction = $self->initializeActionAlias; 479 my $initializeAction = $self->initializeActionAlias;
376 my $submitActionAlias = $self->submitActionAlias; 480 my $submitActionAlias = $self->submitActionAlias;
377 my $submitActionScript = $self->submitActionScript; 481 my $submitActionScript = $self->submitActionScript;
378 my $setStateAlias = $self->setStateAlias; 482 my $setStateAlias = $self->setStateAlias;
379 my $getStateAlias = $self->getStateAlias; 483 my $getStateAlias = $self->getStateAlias;
484
380 my $configAlias = $self->configAlias; 485 my $setConfigAlias = $self->setConfigAlias;
486 my $getConfigAlias = $self->getConfigAlias;
381 my $base64_config = $self->base64_config; 487 my $base64_config = $self->base64_config;
382 my $debugMode = ($self->debug) ? "1": "0"; 488 my $debugMode = ($self->debug) ? "1": "0";
383 my $returnFieldName = $self->{returnFieldName}; 489 my $returnFieldName = $self->{returnFieldName};
384 my $answerBox = $self->{answerBox}; 490 my $answerBox = $self->{answerBox};
385 my $headerText = $self->header(); 491 my $headerText = $self->header();
439use constant DEFAULT_HEADER_TEXT =><<'END_HEADER_SCRIPT'; 545use constant DEFAULT_HEADER_TEXT =><<'END_HEADER_SCRIPT';
440 <script src="/webwork2_files/js/Base64.js" language="javascript"> 546 <script src="/webwork2_files/js/Base64.js" language="javascript">
441 </script> 547 </script>
442 <script src="/webwork2_files/js/ww_applet_support.js" language="javascript"> 548 <script src="/webwork2_files/js/ww_applet_support.js" language="javascript">
443 //upload functions stored in /opt/webwork/webwork2/htdocs/js ... 549 //upload functions stored in /opt/webwork/webwork2/htdocs/js ...
550
444 </script> 551 </script>
445 <script language="JavaScript"> 552 <script language="JavaScript">
446 553
447 // set debug mode for this applet 554 // set debug mode for this applet
448 set_debug($debugMode); 555 set_debug($debugMode);
461 ww_applet_list["$appletName"].appletID = "$appletID"; 568 ww_applet_list["$appletName"].appletID = "$appletID";
462 ww_applet_list["$appletName"].base64_state = "$base64_initializationState"; 569 ww_applet_list["$appletName"].base64_state = "$base64_initializationState";
463 ww_applet_list["$appletName"].base64_config = "$base64_config"; 570 ww_applet_list["$appletName"].base64_config = "$base64_config";
464 ww_applet_list["$appletName"].getStateAlias = "$getStateAlias"; 571 ww_applet_list["$appletName"].getStateAlias = "$getStateAlias";
465 ww_applet_list["$appletName"].setStateAlias = "$setStateAlias"; 572 ww_applet_list["$appletName"].setStateAlias = "$setStateAlias";
466 ww_applet_list["$appletName"].configAlias = "$configAlias"; 573 ww_applet_list["$appletName"].setConfigAlias = "$setConfigAlias";
574 ww_applet_list["$appletName"].getConfigAlias = "$getConfigAlias";
467 ww_applet_list["$appletName"].initializeActionAlias = "$initializeAction"; 575 ww_applet_list["$appletName"].initializeActionAlias = "$initializeAction";
468 ww_applet_list["$appletName"].submitActionAlias = "$submitActionAlias"; 576 ww_applet_list["$appletName"].submitActionAlias = "$submitActionAlias";
469 ww_applet_list["$appletName"].submitActionScript = "$submitActionScript"; 577 ww_applet_list["$appletName"].submitActionScript = "$submitActionScript";
470 ww_applet_list["$appletName"].answerBox = "$answerBox"; 578 ww_applet_list["$appletName"].answerBox = "$answerBox";
471 ww_applet_list["$appletName"].debug = "$debugMode"; 579 ww_applet_list["$appletName"].debug = "$debugMode";

Legend:
Removed from v.6013  
changed lines
  Added in v.6023

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9