Annotation of loncom/homework/functionplotresponse.pm, revision 1.15
1.1 www 1: # LearningOnline Network with CAPA
2: # option list style responses
3: #
1.15 ! www 4: # $Id: functionplotresponse.pm,v 1.14 2010/10/17 22:32:19 www Exp $
1.1 www 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28:
29: package Apache::functionplotresponse;
30: use strict;
31: use Apache::response();
32: use Apache::lonlocal;
33: use Apache::lonnet;
34:
35: BEGIN {
1.12 www 36: &Apache::lonxml::register('Apache::functionplotresponse',('functionplotresponse','backgroundplot','spline','splinerule'));
1.1 www 37: }
38:
1.5 www 39: #
40: # There can be a number of applets on a page, each called ggbApplet_$id,
41: # where $id is the "_"-concatenated part and responseid
42: #
43:
1.1 www 44: sub geogebra_startcode {
1.3 www 45: my ($id)=@_;
1.1 www 46: return (<<ENDSTARTCODE);
1.3 www 47: <applet name="ggbApplet_$id" code="geogebra.GeoGebraApplet" archive="geogebra.jar"
1.1 www 48: codebase="/adm/geogebra/" width="714" height="447" MAYSCRIPT>
49: <param name="java_arguments" value="-Xmx512m -Djnlp.packEnabled=true"/>
50: ENDSTARTCODE
51: }
52:
53: sub geogebra_endcode {
1.2 www 54: return &Apache::lonhtmlcommon::java_not_enabled()."</applet>\n";
55: }
56:
1.5 www 57: #
58: # This is the internal GeoGebra bytecode which defines the spline functions
59: #
1.2 www 60: sub geogebra_spline_program {
61: return (<<ENDSPLINEPROGRAM);
62: <param name="ggbBase64" value="UEsDBBQACAAIADhQET0AAAAAAAAAAAAAAAASAAAAZ2VvZ2VicmFfbWFjcm8ueG1s7Z1bb+PGGYavk19BMDfexkvP+QCsEqzOuWi7gIveBG1By7RXrSwJsqyY+fWdGZLS2pQtzdqWSPoDApOSqCH5vSTnmdH7bj79en8zCVbJ4nY8m7ZCHKEwSKaj2eV4et0K75ZXH1X46y8/frpOZtfJxSIOrmaLm3jZCmlEQvP+TTxazILRzeXf4pukFZ7PJ+NpQsJgOZtNtrw1TCbz9Vu/f8Gn5/j0Czk9J/8Kg/FoNu2PJ+YrYXD7dfbHb9N/mG+040UrXC7ukvXefpvO75ZBjFrh5zCIsVnEZklaYdssqFnE4Vmx7d/vluuNV9nW7VW29ee228zs9NY0P1qa8w+W42W2//hu+XW2sGuX8dK+Y7ZMJslNMl0Gy3Ru3pnPxtNlGEzii2Rij+SXH3/4ZI86mF38Nxkt82MuPr+KJ7eJ3d8Pn8znndlktghM86bY1+7vhfsbT+ZfY7NmRHCbTuI0WQSreGI/zd8xzf11dpk8eDeejo0m9gRul8ncNoBNCedJcpkrmh2yWZmbJp2y3xzQaDZbXN4G962QRBwhKpEginGFJA2DtBWyiDPNJOVYEiwFEmHwZ9au+7orxPn4z/yQqKvqWV6sHWVrN6JsLMIIESqJ4EIiJiXP6yYoUoQIoYggFPHXq5u54l9cOML58UtHIkW4FJJQIqhQQrrK8UhizqRCmHPCNCeveMU1pXIsYlIrIYS57jBhiOWV4+ZdLZCWGEtMsGfl7ueL5NZ2BUU54ov/3Jse4X6erZ7cfwhawQmJUPCX4P7fJzRCH4KPAS1eE/v65wDbhXnj5LN9xd2nJ5vNyaPN7/ONY/OZ+8bJx4d7+PnhHrLN21vbfriJbbH9IQzOSpfF1d3UPfPDB2fqdDN90/kynSTB8ut49L+pKYjRfCPMWekCygQq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EdBp4ZfYA9gPO+7OQguvLz+IPlNbcDIHkue16vMXqHrGyqTbCjNupv7CTwn4TR1GBJlKVOV0kKB2/XkFbUXKZ0/ksICyn7M6IUOyse6XFm2qtc7+R7xAhxIg3wIwwh2VTH2gGfSlFJXjrgvE60ImQk6jsPqfLhCEliHqPWpNoFthKpXS1vRgLDTn/Bq3w7iWbLJaLmMX4wz88dgkWlqSTZJGweTRny9UqWWIKUS8KYYtJzFJkZctZki5SluChf9v81XDcgrVYSkP4GcyYz1qL6ppwt3k2X5W8QZ0SrX3ine21b4N4gLb24m7DvGkX45e2ljBgEVZpD+WhUOdtyFscqF8unT/i9RT1k5JKE+0CRf31sBZh9T5O6NVr5n1m3mPgcKRXe7Ri3sOvRVi9lxRtkDYEHo1Rj1XhZ2GIA5Dcde+YG8kLwGagpG+F3YwbbJrDLVLn/6NvCpya921zpYz+E2U2veu37AC6BRm6qsXC9qo35Oi6N5TO66igFA1ug2FICHfF+oUCAlpBrWHUHUYupMtbP3TuHZxNRxFOg0GtpcW3A+OxLpZt51Kfugm3OF05/c4bMGTD8Y2xF0oqbtHLPR3T98f9iA1vzPovUEsHCEyILqxEAgAAlQQAAFBLAQIUABQACAAIADhQET1gj4YNdwsAAFgqAQASAAAAAAAAAAAAAAAAAAAAAABnZW9nZWJyYV9tYWNyby54bWxQSwECFAAUAAgACAA4UBE9TIgurEQCAACVBAAADAAAAAAAAAAAAAAAAAC3CwAAZ2VvZ2VicmEueG1sUEsFBgAAAAACAAIAegAAADUOAAAAAA=="/>
63: ENDSPLINEPROGRAM
64: }
65:
1.5 www 66: #
67: # The standard set of parameters inside <applet>
68: #
1.2 www 69: sub geogebra_default_parameters {
1.3 www 70: my ($id)=@_;
1.2 www 71: return(<<ENDDEFAULTPARAMETERS);
72: <param name="image" value="/adm/lonIcons/lonanim.gif" />
73: <param name="boxborder" value="false" />
74: <param name="centerimage" value="true" />
75: <param name="java_arguments" value="-Xmx512m" />
76: <param name="cache_archive" value="geogebra.jar, geogebra_main.jar, geogebra_gui.jar, geogebra_cas.jar, geogebra_export.jar, geogebra_properties.jar" />
77: <param name="cache_version" value="3.2.44.0, 3.2.44.0, 3.2.44.0, 3.2.44.0, 3.2.44.0, 3.2.44.0" />
78: <param name="framePossible" value="false" />
79:
80: <param name="showResetIcon" value="false" />
81: <param name="showAnimationButton" value="false" />
82: <param name="enableRightClick" value="false" />
83: <param name="errorDialogsActive" value="true" />
84: <param name="enableLabelDrags" value="false" />
85: <param name="showMenuBar" value="false" />
86: <param name="showToolBar" value="false" />
87: <param name="showToolBarHelp" value="false" />
88: <param name="showAlgebraInput" value="false" />
89: <param name="enableShiftDragZoom" value="false" />
90: <param name="allowRescaling" value="false" />
91: <param name="enableLabelDrags" value="false" />
1.3 www 92: <param name="ggbOnInitParam" value="applet_$id" />
1.2 www 93: ENDDEFAULTPARAMETERS
1.1 www 94: }
95:
1.5 www 96: #
97: # This subroutine is called by LON-CAPA at </problem>
98: # Each applet on the page will call function ggbOnInit when it is done loading
99: # This function in turn will call the respective function registered by start_init_script
100: # Which one of the registered functions is called is determined by ggbOnInitParam, which GeoGebra passes to ggbOnInit
101: #
102:
1.3 www 103: sub init_script {
104: if ($Apache::functionplotresponse::callscripts) {
105: return (<<ENDGGBINIT);
106: <script type="text/javascript">
107: // <![CDATA[
108: function ggbOnInit(param) {
109: $Apache::functionplotresponse::callscripts
110: }
111: // ]]>
112: </script>
113: ENDGGBINIT
114: }
115: }
116:
1.5 www 117: #
118: # Each Geogebra applet is supposed to call this when parameters change
1.10 www 119: # Changes the hidden fields on the web page
1.5 www 120: #
121: sub update_script {
122: my ($id)=@_;
123: return (<<ENDUPDATESCRIPT);
124: <script type="text/javascript">
125: // <![CDATA[
126: function updatePointCoordinates_$id(coordinateName) {
127: var x = document.ggbApplet_$id.getXcoord(coordinateName);
128: var y = document.ggbApplet_$id.getYcoord(coordinateName);
1.9 www 129: document.lonhomework.elements["HWVAL_$id\_" + coordinateName + "_x"].value = x;
130: document.lonhomework.elements["HWVAL_$id\_" + coordinateName + "_y"].value = y;
1.5 www 131: }
132: // ]]>
133: </script>
134: ENDUPDATESCRIPT
135: }
136:
137: #
138: # Register the above update-handler for a variable
139: #
140:
141: sub update_register {
142: my ($id,$variable)=@_;
1.6 www 143: return "document.ggbApplet_$id.registerObjectUpdateListener('$variable','updatePointCoordinates_$id');\n";
144: }
145:
146: #
147: # Set a coordinate variable
148: #
149: sub set_coordinate {
150: my ($id,$variable,$x,$y)=@_;
151: return (<<ENDSETVARIABLE);
1.9 www 152: document.ggbApplet_$id.evalCommand("$variable=($x,$y)");
1.6 www 153: ENDSETVARIABLE
154: }
155:
156: #
1.9 www 157: # Input field name for a coordinate variable
158: #
159:
160: sub field_name {
161: my ($id,$variable,$name)=@_;
162: return "HWVAL_$id\_$variable\_$name";
163: }
164:
165: #
166: # Generate an input field for a coordinate variable
167: #
168:
169: sub generate_input_field {
170: my ($id,$variable,$x,$y)=@_;
171: $Apache::functionplotresponse::inputfields.=
172: "<input type='hidden' name='".&field_name($id,$variable,'x')."' value='$x' />\n".
173: "<input type='hidden' name='".&field_name($id,$variable,'y')."' value='$y' />\n";
174: }
175:
176: #
1.6 www 177: # Initialize a new coordinate variable at set a listener on it
178: #
179: sub new_coordinate {
180: my ($id,$variable,$x,$y)=@_;
1.11 www 181: if (defined($Apache::functionplotresponse::previous{&field_name($id,$variable,'x')})) {
1.10 www 182: $x=$Apache::functionplotresponse::previous{&field_name($id,$variable,'x')};
183: }
1.11 www 184: if (defined($Apache::functionplotresponse::previous{&field_name($id,$variable,'y')})) {
1.10 www 185: $y=$Apache::functionplotresponse::previous{&field_name($id,$variable,'y')};
186: }
1.9 www 187: &generate_input_field($id,$variable,$x,$y);
1.6 www 188: return &set_coordinate($id,$variable,$x,$y).&update_register($id,$variable);
1.5 www 189: }
190:
191: #
192: # This registers the init-function call for ggbOnInit, which LON-CAPA will place at </problem>
193: # It then starts the right headers
194: #
1.3 www 195: sub start_init_script {
196: my ($id)=@_;
1.5 www 197: # Add a line to ggbOnInit-function which calls the right function based on parameter passed from GeoGebra
1.3 www 198: $Apache::functionplotresponse::callscripts.="if (param=='applet_$id') { ggbInit_$id(); }\n";
1.5 www 199: # ... and open this function
1.3 www 200: return (<<ENDSTARTINIT);
201: <script type="text/javascript">
202: // <![CDATA[
1.5 www 203: // variable that will eventually be passed back to the server
204: var coordinateMap_$id = [];
205: // Init-function for applet
1.3 www 206: function ggbInit_$id() {
207: ENDSTARTINIT
208: }
209:
1.5 www 210: #
211: # This sets the axes inside ggbInit_$id
212: #
213:
1.4 www 214: sub axes_script {
215: my ($id,$xmin,$xmax,$ymin,$ymax,$xvisible,$yvisible,$gvisible)=@_;
216: return (<<ENDAXESSCRIPT);
217: // changes (xmin, xmax, ymin, ymax)
218: document.ggbApplet_$id.setCoordSystem($xmin,$xmax,$ymin,$ymax);
219:
220: // makes the (x,y) axis (in)visible
221: document.ggbApplet_$id.setAxesVisible($xvisible,$yvisible);
222: // makes the grid (in)visible
223: document.ggbApplet_$id.setGridVisible($gvisible);
224: ENDAXESSCRIPT
225: }
226:
1.14 www 227: sub axes_label {
228: my ($id,$xlabel,$ylabel)=@_;
229: unless ($xlabel || $ylabel) { return ''; }
230: my $return='document.ggbApplet_'.$id.'.evalCommand("topRight=Corner[3]");';
231: if ($xlabel) {
232: $return.=(<<ENDXAXISLABELSCRIPT);
233: document.ggbApplet_$id.evalCommand("Xlabel=(x(topRight)-AxisStepX[],AxisStepY[]/6)");
234: document.ggbApplet_$id.setVisible("Xlabel",false);
235: document.ggbApplet_$id.evalCommand("Text[\\"$xlabel\\", Xlabel]");
236: ENDXAXISLABELSCRIPT
237: }
238: if ($ylabel) {
239: $return.=(<<ENDYAXISLABELSCRIPT);
240: document.ggbApplet_$id.evalCommand("Ylabel=(AxisStepX[]/6,y(topRight)-AxisStepY[]/3)");
241: document.ggbApplet_$id.setVisible("Ylabel",false);
242: document.ggbApplet_$id.evalCommand("Text[\\"$ylabel\\", Ylabel]");
243: ENDYAXISLABELSCRIPT
244: }
245: return $return;
246: }
247:
1.4 www 248: sub plot_script {
1.6 www 249: my ($id,$function,$fixed)=@_;
250: if ($fixed) {
251: # Use stupid trick to nail this to a location
1.14 www 252: # Oddly, it seems like only one plot cannot be nailed to a location
253: # Non-nailed-down plots apparently get overwritten
1.6 www 254: return (<<ENDPLOTSCRIPT);
255: document.ggbApplet_$id.evalCommand("a=1");
256: document.ggbApplet_$id.setVisible("a",false);
257: // This MUST be a DEPENDENT OBJECT in order to be locked down:
258: document.ggbApplet_$id.evalCommand("y=a*($function)");
1.4 www 259: ENDPLOTSCRIPT
1.6 www 260: } else {
261: return "document.ggbApplet_$id.evalCommand('y=$function')";
262: }
1.4 www 263: }
264:
1.5 www 265: #
1.7 www 266: # Subroutine that generates code for spline $label based on stored information
1.6 www 267: #
268:
269: sub generate_spline {
270: my ($id,$label)=@_;
271: my $result='';
1.7 www 272: my $order=$Apache::functionplotresponse::splineorder{$label};
273: my $x=$Apache::functionplotresponse::splineinitx{$label};
274: my $y=$Apache::functionplotresponse::splineinity{$label};
275: my $sx=$Apache::functionplotresponse::splinescalex{$label};
276: my $sy=$Apache::functionplotresponse::splinescaley{$label};
277: my @coords=();
278: foreach my $i (1..$order) {
279: $result.=&new_coordinate($id,$label.'P'.$i,$x,$y);
1.8 www 280: $x+=$sx/(2.*($order-1));
1.7 www 281: push(@coords,$label.'P'.$i);
282: $result.=&new_coordinate($id,$label.'S'.$i,$x,$y+$sy);
1.8 www 283: $x+=$sx/(2.*($order-1));
1.7 www 284: push(@coords,$label.'S'.$i);
285: }
286: $result.='document.ggbApplet_'.$id.'.evalCommand("Spline'.$order.'['.join(',',@coords).']");'."\n";
1.6 www 287: return $result;
288: }
289: #
290: # <backgroundplot function="..." fixed="yes/no" />
1.5 www 291: #
1.4 www 292: sub start_backgroundplot {
293: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
294: my $result='';
295: my $internalid = $Apache::inputtags::part.'_'.$Apache::inputtags::response[-1];
296: my $function=&Apache::lonxml::get_param('function',$parstack,$safeeval);
1.6 www 297: my $fixed=(&Apache::lonxml::get_param('fixed',$parstack,$safeeval)=~/on|true|yes|1/i?1:0);
298:
1.4 www 299: unless ($function) { $function="0"; }
300: if ($target eq 'web') {
1.6 www 301: $result.=&plot_script($internalid,$function,$fixed);
1.13 www 302: } elsif ($target eq 'edit') {
303: $result=&Apache::edit::tag_start($target,$token,'Background Function Plot').
304: &Apache::edit::text_arg('Function:','function',
305: $token,'16').
306: &Apache::edit::select_arg('Fixed location:','fixed',
307: ['yes','no'],$token).
308: &Apache::edit::end_row();
309: } elsif ($target eq 'modified') {
310: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
311: $safeeval,'function','fixed');
312: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
313: }
314: return $result;
1.4 www 315: }
316:
317: sub end_backgroundplot {
1.13 www 318: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
319: my $result='';
320: if ($target eq 'edit') {
321: $result=&Apache::edit::end_table();
322: }
323: return $result;
1.6 www 324: }
325:
326: #
1.12 www 327: # <splinerule ... />
328: #
329: sub start_splinerule {
330: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
331: my $result='';
332: my $internalid = $Apache::inputtags::part.'_'.$Apache::inputtags::response[-1];
1.13 www 333: if ($target eq 'edit') {
334: $result=&Apache::edit::tag_start($target,$token,'Spline Evaluation Rule').
335: &Apache::edit::end_row();
336: }
1.12 www 337: return $result;
338: }
339:
340: sub end_splinerule {
1.13 www 341: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
342: my $result='';
343: if ($target eq 'edit') {
344: $result=&Apache::edit::end_table();
345: }
346: return $result;
1.12 www 347: }
348:
349:
350: #
1.6 www 351: # <spline label="..." order="1,2,3,4" initx="..." inity="..." scalex="..." scaley="..." />
352: #
353: # Unfortunately, GeoGebra seems to want all splines after everything else, so we need to store them
354: #
355: sub start_spline {
356: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.13 www 357: my $result='';
358: if ($target eq 'web') {
359: my $label=&Apache::lonxml::get_param('label',$parstack,$safeeval);
360: $Apache::functionplotresponse::counter++;
361: if ($label=~/\W/) {
362: &Apache::lonxml::warning(&mt('Spline labels should only contain alphanumeric characters.'));
363: }
364: $label=~s/\W//gs;
365: unless ($label) { $label='S'.$Apache::functionplotresponse::counter; }
366: if ($Apache::functionplotresponse::splineorder{$label}) {
367: &Apache::lonxml::error(&mt('Spline labels must be unique.'));
368: }
369:
370: my $order=&Apache::lonxml::get_param('order',$parstack,$safeeval);
371: if ($order<2) { $order=2; }
372: if ($order>8) { $order=8; }
373: $Apache::functionplotresponse::splineorder{$label}=$order;
374:
375: my $x=&Apache::lonxml::get_param('initx',$parstack,$safeeval);
376: unless ($x) { $x=0; }
377: $Apache::functionplotresponse::splineinitx{$label}=$x;
378:
379: my $y=&Apache::lonxml::get_param('inity',$parstack,$safeeval);
380: unless ($y) { $y=0; }
381: $Apache::functionplotresponse::splineinity{$label}=$y;
382:
383: my $sx=&Apache::lonxml::get_param('scalex',$parstack,$safeeval);
384: unless ($sx) { $sx=$order; }
385: $Apache::functionplotresponse::splinescalex{$label}=$sx;
386:
387: my $sy=&Apache::lonxml::get_param('scaley',$parstack,$safeeval);
388: unless ($sy) { $sy=2; }
389: $Apache::functionplotresponse::splinescaley{$label}=$sy;
390: } elsif ($target eq 'edit') {
391: $result=&Apache::edit::tag_start($target,$token,'Spline').
392: &Apache::edit::text_arg('Label:','label',
1.14 www 393: $token,'4').' '.
1.13 www 394: &Apache::edit::select_arg('Order:','order',
1.14 www 395: ['2','3','4','5','6','7','8','9'],$token).' '.
1.13 www 396: &Apache::edit::text_arg('Initial x-value:','initx',
1.14 www 397: $token,'4').' '.
1.13 www 398: &Apache::edit::text_arg('Initial y-value:','inity',
1.14 www 399: $token,'4').' '.
1.13 www 400: &Apache::edit::text_arg('Scale x:','scalex',
1.14 www 401: $token,'4').' '.
1.13 www 402: &Apache::edit::text_arg('Scale y:','scaley',
403: $token,'4').
404: &Apache::edit::end_row();
405: } elsif ($target eq 'modified') {
406: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
407: $safeeval,'label','order','initx','inity',
408: 'scalex','scaley');
409: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
410: }
411: return $result;
1.6 www 412: }
413:
414: sub end_spline {
1.13 www 415: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
416: my $result='';
417: if ($target eq 'edit') {
418: $result=&Apache::edit::end_table();
419: }
420: return $result;
1.4 www 421: }
422:
1.3 www 423: sub end_init_script {
424: return (<<ENDENDINIT);
425: }
426: // ]]>
427: </script>
428: ENDENDINIT
429: }
430:
1.10 www 431: #
432: # Storing and restoring spline coordinates from part answers
433: #
434: sub decode_previous_answer {
435: my ($answer)=@_;
436: foreach my $coordinate (split(/\,/,$answer)) {
437: my ($key,$value)=split(/\=/,$coordinate);
438: $Apache::functionplotresponse::previous{$key}=$value;
439: }
440: }
441:
442: sub get_answer_from_form_fields {
443: my ($id)=@_;
444: my $answer='';
445: my %coords=();
446: foreach my $field (keys(%env)) {
447: if ($field=~/^form\.HWVAL\_$id/) {
448: $field=~/^form\.(.*)$/;
449: $coords{$1}=$env{$field};
450: }
451: }
452: $answer=join(',',map { $_.'='.$coords{$_} } (sort(keys(%coords))));
453: return ($answer,%coords);
454: }
455:
1.15 ! www 456: #
! 457: # The following functions calculate the cubic-hermite splines server-side
! 458: #
! 459:
! 460: sub cubic_hermite {
! 461: my ($t,$p1,$s1,$p2,$s2)=@_;
! 462: return (2.*$t*$t*$t-3.*$t*$t+1.)*$p1 + 5.*($t*$t*$t-2.*$t*$t+$t)*($s1-$p1)+
! 463: (-2.*$t*$t*$t+3.*$t*$t) *$p2 + 5.*($t*$t*$t-$t*$t) *($s2-$p2);
! 464: }
! 465:
! 466: #
! 467: # d/dt(...)
! 468: #
! 469:
! 470: sub ddt_cubic_hermite {
! 471: my ($t,$p1,$s1,$p2,$s2)=@_;
! 472: return (6.*$t*$t-6.*$t) *$p1 + 5.*(3.*$t*$t-4.*$t+1.)*($s1-$p1)+
! 473: (-6.*$t*$t+6.*$t)*$p2 + 5.*(3.*$t*$t-2.*$t) *($s2-$p2);
! 474: }
! 475:
! 476: #
! 477: # d^2/dt^2(...)
! 478: #
! 479:
! 480: sub d2dt2_cubic_hermite {
! 481: my ($t,$p1,$s1,$p2,$s2)=@_;
! 482: return (12.*$t-6.) *$p1 + 5.*(6.*$t-4.)*($s1-$p1)+
! 483: (-12.*$t+6.)*$p2 + 5.*(6.*$t-2.)*($s2-$p2);
! 484: }
! 485:
! 486: #
! 487: # Implentation of <functionplotresponse>
! 488: #
! 489:
1.1 www 490: sub start_functionplotresponse {
491: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
492: my $result='';
1.6 www 493: # To remember the splines - somehow, they need to come last
494: undef %Apache::functionplotresponse::splineorder;
495: undef %Apache::functionplotresponse::splineinitx;
496: undef %Apache::functionplotresponse::splineinity;
497: undef %Apache::functionplotresponse::splinescalex;
498: undef %Apache::functionplotresponse::splinescaley;
1.10 www 499: # Remember input fields, etc
500: undef %Apache::functionplotresponse::previous;
1.9 www 501: $Apache::functionplotresponse::inputfields='';
1.6 www 502: $Apache::functionplotresponse::counter=0;
1.10 www 503: # Part and ID
504: my $partid=$Apache::inputtags::part;
505: my $id=&Apache::response::start_response($parstack,$safeeval);
1.6 www 506: # Internal ID to mark the applet and its coordinates
1.10 www 507: my $internalid = $partid.'_'.$id;
508: # Previous answer
509: &decode_previous_answer($Apache::lonhomework::history{"resource.$partid.$id.submission"});
510:
1.6 www 511: # Parameters of <functionplotresponse>
1.3 www 512: my $xmin=&Apache::lonxml::get_param('xmin',$parstack,$safeeval);
1.4 www 513: $xmin=($xmin?$xmin:-10);
1.3 www 514: my $xmax=&Apache::lonxml::get_param('xmax',$parstack,$safeeval);
1.4 www 515: $xmax=($xmax?$xmax:10);
1.3 www 516: my $ymin=&Apache::lonxml::get_param('ymin',$parstack,$safeeval);
1.4 www 517: $ymin=($ymin?$ymin:-10);
1.3 www 518: my $ymax=&Apache::lonxml::get_param('ymax',$parstack,$safeeval);
1.4 www 519: $ymax=($ymax?$ymax:10);
1.6 www 520: my $xaxisvisible=(&Apache::lonxml::get_param('xaxisvisible',$parstack,$safeeval)=~/on|true|yes|1/i?'true':'false');
521: my $yaxisvisible=(&Apache::lonxml::get_param('yaxisvisible',$parstack,$safeeval)=~/on|true|yes|1/i?'true':'false');
522: my $gridvisible=(&Apache::lonxml::get_param('gridvisible',$parstack,$safeeval)=~/on|true|yes|1/i?'true':'false');
1.14 www 523: my $xlabel=&Apache::lonxml::get_param('xlabel',$parstack,$safeeval);
524: my $ylabel=&Apache::lonxml::get_param('ylabel',$parstack,$safeeval);
1.12 www 525: if ($target eq 'edit') {
526: $result.=&Apache::edit::start_table($token)
527: .'<tr><td><span class="LC_nobreak">'.&mt('Function Plot Question').'</span></td>'
528: .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
529: .&Apache::edit::deletelist($target,$token)
530: .'</span></td>'
531: ."<td> "
532: .&Apache::edit::end_row()
533: .&Apache::edit::start_spanning_row()
534: ."\n";
1.14 www 535: $result.=&Apache::edit::text_arg('Label x-axis:','xlabel',
536: $token,'6').' '.
537: &Apache::edit::text_arg('Minimum x-value:','xmin',
538: $token,'4').' '.
1.12 www 539: &Apache::edit::text_arg('Maximum x-value:','xmax',
1.14 www 540: $token,'4').' '.
1.12 www 541: &Apache::edit::select_arg('x-axis visible:','xaxisvisible',
1.14 www 542: ['yes','no'],$token).' '.
543: &Apache::edit::text_arg('Label y-axis:','ylabel',
544: $token,'6').' '.
1.12 www 545: &Apache::edit::text_arg('Minimum y-value:','ymin',
1.14 www 546: $token,'4').' '.
1.12 www 547: &Apache::edit::text_arg('Maximum y-value:','ymax',
1.14 www 548: $token,'4').' '.
1.12 www 549: &Apache::edit::select_arg('y-axis visible:','yaxisvisible',
1.14 www 550: ['yes','no'],$token).' '.
1.12 www 551: &Apache::edit::select_arg('Grid visible:','gridvisible',
552: ['yes','no'],$token).
553: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
554: } elsif ($target eq 'modified') {
555: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.14 www 556: $safeeval,'xlabel','xmin','xmax','ylabel','ymin','ymax',
1.12 www 557: 'xaxisvisible','yaxisvisible','gridvisible');
558: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.3 www 559:
1.12 www 560: } elsif ($target eq 'meta') {
1.10 www 561: $result=&Apache::response::meta_package_write('functionplotresponse');
1.12 www 562: } elsif ($target eq 'web') {
1.5 www 563: # paste in the update routine to receive stuff back from the applet
564: $result.=&update_script($internalid);
565: # start the initscript for this applet
1.3 www 566: $result.=&start_init_script($internalid);
1.5 www 567: # put the axis commands inside
1.4 www 568: $result.=&axes_script($internalid,$xmin,$xmax,$ymin,$ymax,$xaxisvisible,$yaxisvisible,$gridvisible);
1.14 www 569: $result.=&axes_label($internalid,$xlabel,$ylabel);
1.5 www 570: # init script is left open
1.1 www 571: }
572: return $result;
573: }
574:
575: sub end_functionplotresponse {
576: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
577: &Apache::response::end_response;
1.3 www 578:
1.1 www 579: my $result;
1.10 www 580: my $id=$Apache::inputtags::response[-1];
581: my $partid=$Apache::inputtags::part;
582: my $internalid = $partid.'_'.$id;
1.3 www 583:
1.12 www 584: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
1.10 www 585: if ($target eq 'grade'
586: && &Apache::response::submitted()
587: && $Apache::lonhomework::type eq 'exam') {
588:
589: #&Apache::response::scored_response($partid,$id);
590:
591: } elsif ($target eq 'grade'
592: && &Apache::response::submitted()
593: && $Apache::lonhomework::type ne 'exam') {
594: my ($response,%coords)=&get_answer_from_form_fields($internalid);
595: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
1.12 www 596: my %previous=&Apache::response::check_for_previous($response,$partid,$id);
597: #
598: # Actually grade
599: #
600: my $ad='INCORRECT';
601: #
602: # Store grading info
603: #
604: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
605: &Apache::response::handle_previous(\%previous,$ad);
1.10 www 606: } elsif ($target eq 'web') {
1.6 www 607: # Now is the time to render all of the stored splines
608: foreach my $label (keys(%Apache::functionplotresponse::splineorder)) {
609: $result.=&generate_spline($internalid,$label);
610: }
1.5 www 611: # close the init script
1.3 www 612: $result.=&end_init_script();
1.9 www 613: # generate the input fields
614: $result.=$Apache::functionplotresponse::inputfields;
1.5 www 615: # actually start the <applet>-tag
1.3 www 616: $result.=&geogebra_startcode($internalid);
1.5 www 617: # load the spline bytecode
1.3 www 618: $result.=&geogebra_spline_program();
1.5 www 619: # set default parameters
1.3 www 620: $result.=&geogebra_default_parameters($internalid);
1.5 www 621: # close the <applet>-tag
1.1 www 622: $result.=&geogebra_endcode();
623: }
624: return $result;
625: }
626:
627: 1;
628:
629: __END__
630:
631: =head1 NAME
632:
633: Apache::functionplotresponse.pm;
634:
635: =head1 SYNOPSIS
636:
637: Handles tags associated with accepting function plots.
638:
639: This is part of the LearningOnline Network with CAPA project
640: described at http://www.lon-capa.org.
641:
642: =head1 HANDLER SUBROUTINE
643:
644: start_functionplotresponse()
645:
646: =head1 OTHER SUBROUTINES
647:
648: =over
649:
650: =item end_functionplotresponse()
651:
652: =back
653:
654: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>