Annotation of loncom/homework/functionplotresponse.pm, revision 1.70
1.1 www 1: # LearningOnline Network with CAPA
1.70 ! www 2: # Functionplot responses
1.1 www 3: #
1.70 ! www 4: # $Id: functionplotresponse.pm,v 1.69 2011/10/27 00:36:37 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;
1.65 www 34: use Apache::run;
1.36 www 35:
1.1 www 36: BEGIN {
1.37 www 37: &Apache::lonxml::register('Apache::functionplotresponse',('functionplotresponse','backgroundplot','spline',
1.70 ! www 38: 'plotobject','plotvector',
1.37 www 39: 'functionplotrule','functionplotruleset',
40: 'functionplotelements'));
1.1 www 41: }
42:
1.5 www 43: #
44: # There can be a number of applets on a page, each called ggbApplet_$id,
45: # where $id is the "_"-concatenated part and responseid
46: #
47:
1.1 www 48: sub geogebra_startcode {
1.3 www 49: my ($id)=@_;
1.1 www 50: return (<<ENDSTARTCODE);
1.3 www 51: <applet name="ggbApplet_$id" code="geogebra.GeoGebraApplet" archive="geogebra.jar"
1.40 www 52: codebase="/adm/geogebra/" width="722" height="447" MAYSCRIPT>
1.1 www 53: <param name="java_arguments" value="-Xmx512m -Djnlp.packEnabled=true"/>
54: ENDSTARTCODE
55: }
56:
57: sub geogebra_endcode {
1.2 www 58: return &Apache::lonhtmlcommon::java_not_enabled()."</applet>\n";
59: }
60:
1.5 www 61: #
62: # This is the internal GeoGebra bytecode which defines the spline functions
63: #
1.2 www 64: sub geogebra_spline_program {
65: return (<<ENDSPLINEPROGRAM);
1.57 www 66: <param name="ggbBase64" value="UEsDBBQACAAIAKNNfz4AAAAAAAAAAAAAAAASAAAAZ2VvZ2VicmFfbWFjcm8ueG1s7Vxtb+pGGv3c/grLH6pk21wSIITeDbcqfq3U217pVquVVrsrBxzCLtjIOAnTX78zYxtCxsDYi/EA50MyjjOM7XPs55iZ8zz3Py2mE+3Fj+bjMOjpNx+udc0PBuFwHIx6+nP8eNXVf/r07f3ID0f+Q+Rpj2E09eKe3mY9F/PxxyD8zZv685k38L8Onvyp92s48GI+2lMczz42Gq+vrx+yz38Io1FjNIo/LOZD+vnpJJj39HTjIx1u7UOvLd69eX190/j751+T4a/GwTz2goGva/S8pt4gCrXBdMhOoqd/nU3Ggd/UtTgMJzm7XH8yW+76x5ebH7Sv9OdLk7bNf+raeBAG9njis1OaP4WvvwR/0A/1vainx9Gzr2cH/CWYPcead93Tf9Y174Y2Hm2bPb1PmxZtPL2R9f39OV52fkl799PeL7wbPeicDj9goGnxOE6O7z3HT2HEtoZezPbQnv7En/pBrMVkRvfMwnEQ69rEe/An7Ew+ffvNPTtrLXz4jz+I03PO/v/oTeY+O9439/T/RjgJI40OT1kc8d8P/Lc3mT15dIuyy7tOPOJH2os3Yf9N99DhPodDf22vF4ynnHVtHvszNsANhXDm+0N6U+npCdPxZ3RAfmu9OZ1BGEbDubZIDquR9Ob6M7kdeRd+qV/Hf6YHbb3dG5PJ23O5b6Qo7cCrfwJ4NQ+IF73F/2/Amre3qkDWOcQtdhqQtfcN2WIW+XMmOBkO3sO/F1R3FrNk82JxqfW0i6b2F23xr4vWpXaltZLt5qX2vXZzyf64+Jlts/0XWafmm06LtJN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iXSLqX6eHJCpESFTQ0KmxoRMDQqZGhUyNSxkalzI1MCQqZGhUyNDJ5+VqZGhUyNDp0aGfiEysrR3O10BbNdZo8vSVYf9aJw5AW7YuPdNX6zTo8Zrpatv349uSxZ/49lf2bfAefbDF2dfodBWNj/jm7Q8P7pwwRv+BvchLxjeoXw3Cad6Wpb6sMZLqkrL73Jl9PkJ4pbe40vivapPtRXk7x0O3iFQjcwvSv3Ic71lALm8T7vS9OpcZX5SiZcPssEd+MZfYb/udNf62/FIeo427hD1C33bS21L/gU7iJ/NZdHIofGU7guFb4pu9eJy8dn0cj4YsWyzRtX21oA9qiq6tMBmVHVliQ0XX2UPF5u2Bre4NZ8HRhnbuda17cbwFp9//ZcYdGZntta3P9qPK3CX4oI5BpCnBhkCq38syb0I+k8st38AUEsHCA04kj02BQAA1BEAAFBLAQIUABQACAAIAKNNfz4bb+CNfwoAAFdEAQASAAAAAAAAAAAAAAAAAAAAAABnZW9nZWJyYV9tYWNyby54bWxQSwECFAAUAAgACACkTX8+RczeXRoAAAAYAAAAFgAAAAAAAAAAAAAAAAC/CgAAZ2VvZ2VicmFfamF2YXNjcmlwdC5qc1BLAQIUABQACAAIAKRNfz4NOJI9NgUAANQRAAAMAAAAAAAAAAAAAAAAAB0LAABnZW9nZWJyYS54bWxQSwUGAAAAAAMAAwC+AAAAjRAAAAAA"/>
1.2 www 67: ENDSPLINEPROGRAM
68: }
69:
1.5 www 70: #
71: # The standard set of parameters inside <applet>
72: #
1.2 www 73: sub geogebra_default_parameters {
1.3 www 74: my ($id)=@_;
1.2 www 75: return(<<ENDDEFAULTPARAMETERS);
76: <param name="image" value="/adm/lonIcons/lonanim.gif" />
77: <param name="boxborder" value="false" />
78: <param name="centerimage" value="true" />
1.62 www 79: <param name="cache_archive" value="geogebra.jar, geogebra_main.jar, geogebra_gui.jar, geogebra_cas.jar, geogebra_export.jar, geogebra_algos.jar, geogebra_javascript.jar, geogebra_properties.jar, jlatexmath.jar, jlm_cyrillic.jar, jlm_greek.jar" />
1.69 www 80: <param name="cache_version" value="4.0.1.0,4.0.1.0,4.0.1.0,4.0.1.0,4.0.1.0,4.0.1.0,4.0.1.0,4.0.1.0,4.0.1.0,4.0.1.0,4.0.1.0" />
1.2 www 81: <param name="framePossible" value="false" />
82:
83: <param name="showResetIcon" value="false" />
84: <param name="showAnimationButton" value="false" />
85: <param name="enableRightClick" value="false" />
86: <param name="errorDialogsActive" value="true" />
87: <param name="enableLabelDrags" value="false" />
88: <param name="showMenuBar" value="false" />
89: <param name="showToolBar" value="false" />
90: <param name="showToolBarHelp" value="false" />
91: <param name="showAlgebraInput" value="false" />
92: <param name="enableShiftDragZoom" value="false" />
93: <param name="allowRescaling" value="false" />
94: <param name="enableLabelDrags" value="false" />
1.3 www 95: <param name="ggbOnInitParam" value="applet_$id" />
1.2 www 96: ENDDEFAULTPARAMETERS
1.1 www 97: }
98:
1.5 www 99: #
100: # This subroutine is called by LON-CAPA at </problem>
101: # Each applet on the page will call function ggbOnInit when it is done loading
102: # This function in turn will call the respective function registered by start_init_script
103: # Which one of the registered functions is called is determined by ggbOnInitParam, which GeoGebra passes to ggbOnInit
104: #
105:
1.3 www 106: sub init_script {
1.16 www 107: if ($#Apache::functionplotresponse::callscripts>=0) {
108: my $script='';
109: foreach my $id (@Apache::functionplotresponse::callscripts) {
110: $script.="if (param=='applet_$id') { loaded_$id=true; }\n";
111: }
112: $script.="if (".join(' && ',map { "loaded_$_" } (@Apache::functionplotresponse::callscripts)).
1.52 www 113: ") { setTimeout('ggbInitAll()',200) }";
1.16 www 114: my $calls=join("\n",map { "ggbInit_$_();" } (@Apache::functionplotresponse::callscripts));
1.3 www 115: return (<<ENDGGBINIT);
116: <script type="text/javascript">
117: // <![CDATA[
1.16 www 118: // Function that each applet will call when loaded
119: // It will pass "its" parameter
120: // Set flags for when an applet is loaded, wait till all are loaded, and then some
1.3 www 121: function ggbOnInit(param) {
1.16 www 122: $script
123: }
124: function ggbInitAll() {
1.17 www 125: $calls
1.3 www 126: }
127: // ]]>
128: </script>
129: ENDGGBINIT
130: }
131: }
132:
1.5 www 133: #
134: # Each Geogebra applet is supposed to call this when parameters change
1.10 www 135: # Changes the hidden fields on the web page
1.5 www 136: #
137: sub update_script {
138: my ($id)=@_;
139: return (<<ENDUPDATESCRIPT);
140: <script type="text/javascript">
141: // <![CDATA[
142: function updatePointCoordinates_$id(coordinateName) {
143: var x = document.ggbApplet_$id.getXcoord(coordinateName);
144: var y = document.ggbApplet_$id.getYcoord(coordinateName);
1.9 www 145: document.lonhomework.elements["HWVAL_$id\_" + coordinateName + "_x"].value = x;
146: document.lonhomework.elements["HWVAL_$id\_" + coordinateName + "_y"].value = y;
1.5 www 147: }
148: // ]]>
149: </script>
150: ENDUPDATESCRIPT
151: }
152:
153: #
154: # Register the above update-handler for a variable
155: #
156:
157: sub update_register {
158: my ($id,$variable)=@_;
1.6 www 159: return "document.ggbApplet_$id.registerObjectUpdateListener('$variable','updatePointCoordinates_$id');\n";
160: }
161:
162: #
1.40 www 163: # Set a point coordinate variable
1.6 www 164: #
1.40 www 165: sub set_point_coordinate {
1.48 www 166: my ($id,$variable,$x,$y,$fixed)=@_;
167: my $mult=($fixed?'a*':'');
1.68 www 168: # Get rid of wild exponents, make sure it's a number
169: $x=1.*$x;
170: $y=1.*$y;
171: # GeoGebra does not understand "E"
172: $x=~s/[e|E]/\*10\^/;
173: $x=~s/\+//;
174: $y=~s/[e|E]/\*10\^/;
175: $y=~s/\+//;
1.6 www 176: return (<<ENDSETVARIABLE);
1.48 www 177: document.ggbApplet_$id.evalCommand("a=1");
178: document.ggbApplet_$id.evalCommand("$variable=$mult($x,$y)");
1.40 www 179: document.ggbApplet_$id.setLabelVisible("$variable",false);
1.6 www 180: ENDSETVARIABLE
181: }
182:
183: #
1.40 www 184: # Set a slope coordinate variable
185: #
186: sub set_slope_coordinate {
1.48 www 187: my ($id,$variable,$xrel,$yrel,$xmin,$xmax,$ymin,$ymax,$pointname,$fixed)=@_;
1.40 www 188: my $xvariable=$variable.'x';
189: my $yvariable=$variable.'y';
190: my $domain=$xmax-$xmin;
191: my $range=$ymax-$ymin;
192: my $xinterval=$domain/100.;
193: my $yinterval=$range/200.;
1.48 www 194: my $mult=($fixed?'a*':'');
1.40 www 195: return (<<ENDSETSVARIABLE);
1.48 www 196: document.ggbApplet_$id.evalCommand("a=1");
1.51 www 197: document.ggbApplet_$id.evalCommand("$xvariable=Slider[$xinterval,$domain,$xinterval]");
1.40 www 198: document.ggbApplet_$id.setVisible("$xvariable", false);
199: document.ggbApplet_$id.evalCommand("$xvariable=$xrel");
200: document.ggbApplet_$id.evalCommand("$yvariable=Slider[-$range,$range,$yinterval]");
201: document.ggbApplet_$id.setVisible("$yvariable", false);
202: document.ggbApplet_$id.evalCommand("$yvariable=$yrel");
1.48 www 203: document.ggbApplet_$id.evalCommand("$variable=$mult($xvariable+x($pointname),$yvariable+y($pointname))");
1.40 www 204: document.ggbApplet_$id.setLabelVisible("$variable", false);
205: ENDSETSVARIABLE
206: }
207:
208: #
1.9 www 209: # Input field name for a coordinate variable
210: #
211:
212: sub field_name {
213: my ($id,$variable,$name)=@_;
214: return "HWVAL_$id\_$variable\_$name";
215: }
216:
217: #
218: # Generate an input field for a coordinate variable
219: #
220:
221: sub generate_input_field {
222: my ($id,$variable,$x,$y)=@_;
223: $Apache::functionplotresponse::inputfields.=
224: "<input type='hidden' name='".&field_name($id,$variable,'x')."' value='$x' />\n".
225: "<input type='hidden' name='".&field_name($id,$variable,'y')."' value='$y' />\n";
226: }
227:
228: #
1.40 www 229: # Initialize a new point coordinate variable at set a listener on it
1.6 www 230: #
1.40 www 231: sub new_point_coordinate {
1.48 www 232: my ($id,$variable,$x,$y,$fixed)=@_;
1.11 www 233: if (defined($Apache::functionplotresponse::previous{&field_name($id,$variable,'x')})) {
1.10 www 234: $x=$Apache::functionplotresponse::previous{&field_name($id,$variable,'x')};
235: }
1.11 www 236: if (defined($Apache::functionplotresponse::previous{&field_name($id,$variable,'y')})) {
1.10 www 237: $y=$Apache::functionplotresponse::previous{&field_name($id,$variable,'y')};
238: }
1.9 www 239: &generate_input_field($id,$variable,$x,$y);
1.48 www 240: return &set_point_coordinate($id,$variable,$x,$y,$fixed).&update_register($id,$variable);
1.40 www 241: }
242:
243: #
244: # Initialize a new slope coordinate variable at set a listener on it
245: #
246: sub new_slope_coordinate {
1.48 www 247: my ($id,$variable,$x,$y,$pointname,$xp,$yp,$xmin,$xmax,$ymin,$ymax,$fixed)=@_;
1.40 www 248: #
249: # $variable: name of the slope point
250: # $x, $y: coordinates of the slope point
251: # $pointname: name of the associated point point
252: # $xp $yp: coordinates of the point point
253: #
254: if (defined($Apache::functionplotresponse::previous{&field_name($id,$variable,'x')})) {
255: $x=$Apache::functionplotresponse::previous{&field_name($id,$variable,'x')};
256: }
257: if (defined($Apache::functionplotresponse::previous{&field_name($id,$variable,'y')})) {
258: $y=$Apache::functionplotresponse::previous{&field_name($id,$variable,'y')};
259: }
260: if (defined($Apache::functionplotresponse::previous{&field_name($id,$pointname,'x')})) {
261: $xp=$Apache::functionplotresponse::previous{&field_name($id,$pointname,'x')};
262: }
263: if (defined($Apache::functionplotresponse::previous{&field_name($id,$pointname,'y')})) {
264: $yp=$Apache::functionplotresponse::previous{&field_name($id,$pointname,'y')};
265: }
266:
267: &generate_input_field($id,$variable,$x,$y);
268: my $xrel=$x-$xp;
269: my $yrel=$y-$yp;
1.48 www 270: return &set_slope_coordinate($id,$variable,$xrel,$yrel,$xmin,$xmax,$ymin,$ymax,$pointname,$fixed).&update_register($id,$variable);
1.5 www 271: }
272:
273: #
274: # This registers the init-function call for ggbOnInit, which LON-CAPA will place at </problem>
275: # It then starts the right headers
276: #
1.3 www 277: sub start_init_script {
278: my ($id)=@_;
1.16 www 279: # Add id to the list of ggbInit_$id functions that need to be called
280: push(@Apache::functionplotresponse::callscripts,$id);
1.5 www 281: # ... and open this function
1.3 www 282: return (<<ENDSTARTINIT);
283: <script type="text/javascript">
284: // <![CDATA[
1.5 www 285: // variable that will eventually be passed back to the server
286: var coordinateMap_$id = [];
1.16 www 287: // flag for not loaded yet
288: var loaded_$id=false;
1.5 www 289: // Init-function for applet
1.3 www 290: function ggbInit_$id() {
291: ENDSTARTINIT
292: }
293:
1.5 www 294: #
295: # This sets the axes inside ggbInit_$id
296: #
297:
1.4 www 298: sub axes_script {
299: my ($id,$xmin,$xmax,$ymin,$ymax,$xvisible,$yvisible,$gvisible)=@_;
300: return (<<ENDAXESSCRIPT);
301: // changes (xmin, xmax, ymin, ymax)
302: document.ggbApplet_$id.setCoordSystem($xmin,$xmax,$ymin,$ymax);
303:
304: // makes the (x,y) axis (in)visible
305: document.ggbApplet_$id.setAxesVisible($xvisible,$yvisible);
306: // makes the grid (in)visible
307: document.ggbApplet_$id.setGridVisible($gvisible);
308: ENDAXESSCRIPT
309: }
310:
1.14 www 311: sub axes_label {
1.45 www 312: my ($id,$xmin,$xmax,$ymin,$ymax,$xlabel,$ylabel)=@_;
1.14 www 313: unless ($xlabel || $ylabel) { return ''; }
314: my $return='document.ggbApplet_'.$id.'.evalCommand("topRight=Corner[3]");';
315: if ($xlabel) {
1.45 www 316: if (($ymin<0) && ($ymax>0)) {
1.14 www 317: $return.=(<<ENDXAXISLABELSCRIPT);
318: document.ggbApplet_$id.evalCommand("Xlabel=(x(topRight)-AxisStepX[],AxisStepY[]/6)");
319: document.ggbApplet_$id.setVisible("Xlabel",false);
320: document.ggbApplet_$id.evalCommand("Text[\\"$xlabel\\", Xlabel]");
321: ENDXAXISLABELSCRIPT
1.45 www 322: } else {
323: $return.=(<<ENDXOFFAXISLABEL);
324: document.ggbApplet_$id.evalCommand("LowerRight=Corner[2]");
325: document.ggbApplet_$id.evalCommand("Text[\\"$xlabel\\", (x(LowerRight) - AxisStepX[], y(LowerRight) + AxisStepY[] / 2)]");
326: ENDXOFFAXISLABEL
327: }
1.14 www 328: }
329: if ($ylabel) {
1.45 www 330: if (($xmin<0) && ($xmax>0)) {
1.14 www 331: $return.=(<<ENDYAXISLABELSCRIPT);
332: document.ggbApplet_$id.evalCommand("Ylabel=(AxisStepX[]/6,y(topRight)-AxisStepY[]/3)");
333: document.ggbApplet_$id.setVisible("Ylabel",false);
334: document.ggbApplet_$id.evalCommand("Text[\\"$ylabel\\", Ylabel]");
335: ENDYAXISLABELSCRIPT
1.45 www 336: } else {
337: $return.=(<<ENDYOFFAXISLABEL);
338: document.ggbApplet_$id.evalCommand("UpperLeft=Corner[4]");
339: document.ggbApplet_$id.evalCommand("Text[\\"$ylabel\\", (x(UpperLeft) + AxisStepX[] / 5, y(UpperLeft) - AxisStepY[] / 1.8)]");
340: ENDYOFFAXISLABEL
341: }
1.14 www 342: }
343: return $return;
344: }
345:
1.58 www 346: #
347: # Subroutine to produce background and answer plots
348: #
349:
1.4 www 350: sub plot_script {
1.47 www 351: my ($id,$function,$fixed,$label,$color,$xmin,$xmax,$thickness)=@_;
1.41 www 352: $label=~s/\W//g;
353: if (($label) && ($label!~/^[A-Za-z]/)) {
354: $label='C'.$label;
355: }
356: my $visible=0;
357: if ($label) {
358: $visible="1";
359: } else {
360: $Apache::functionplotresponse::counter++;
361: $label='C'.$Apache::functionplotresponse::counter;
362: }
1.46 www 363: my $rc=0;
364: my $gc=0;
365: my $bc=0;
366: if ($color) {
367: my ($rh,$gh,$bh)=($color=~/(..)(..)(..)/);
368: $rc=hex($rh);
369: $gc=hex($gh);
370: $bc=hex($bh);
371: }
1.6 www 372: if ($fixed) {
1.43 www 373: return "document.ggbApplet_$id.evalCommand('$label=Function[$function,$xmin,$xmax]');\n".
1.46 www 374: ($visible?'':"document.ggbApplet_$id.setLabelVisible('$label', false);\n").
1.47 www 375: ($color?"document.ggbApplet_$id.setColor('$label',$rc,$gc,$bc);\n":'').
376: ($thickness?"document.ggbApplet_$id.setLineThickness('$label',$thickness);\n":'');
1.6 www 377: } else {
1.46 www 378: return "document.ggbApplet_$id.evalCommand('y=$function');\n";
1.6 www 379: }
1.4 www 380: }
381:
1.5 www 382: #
1.70 ! www 383: # Subroutine to produce objects
! 384: #
! 385:
! 386: sub plotobject_script {
! 387: my ($id,$label,$x,$y)=@_;
! 388: unless ($label) {
! 389: $Apache::functionplotresponse::counter++;
! 390: $label='O'.$Apache::functionplotresponse::counter;
! 391: }
! 392: return "document.ggbApplet_$id.evalCommand('a=1');\n".
! 393: "document.ggbApplet_$id.setVisible('a', false);\n".
! 394: "document.ggbApplet_$id.setLabelVisible('a', false);\n".
! 395: "document.ggbApplet_$id.evalCommand('$label=a*($x,$y)');\n".
! 396: "document.ggbApplet_$id.setVisible('$label', true);\n".
! 397: "document.ggbApplet_$id.setLabelVisible('$label', true);\n";
! 398: }
! 399:
! 400: #
! 401: # Subroutine to produce vectors
! 402: #
! 403:
! 404: sub plotvector_script {
! 405: my ($id,$label,$xs,$ys,$xe,$ye)=@_;
! 406: unless ($label) {
! 407: $Apache::functionplotresponse::counter++;
! 408: $label='V'.$Apache::functionplotresponse::counter;
! 409: }
! 410: return(<<ENDVECTOR);
! 411: document.ggbApplet1.evalCommand("Gravitystart=(20,0)");
! 412: document.ggbApplet1.setVisible("Gravitystart",false);
! 413: document.ggbApplet1.setLabelVisible("Gravitystart",false);
! 414: document.ggbApplet1.evalCommand("Gravityend=(20,-5)");
! 415: document.ggbApplet1.setLabelVisible("Gravityend",false);
! 416: document.ggbApplet1.evalCommand("Gravity=Vector[Gravitystart, Gravityend]");
! 417: document.ggbApplet1.setLabelVisible("Gravity",true);
! 418: document.ggbApplet1.setLineThickness("Gravity",8);
! 419: // Displays the Angle
! 420: document.ggbApplet1.evalCommand("Gravitypoint=(110,y(Gravitystart))"); //The x-value for this should be 2*(xmax-xmin)+xmax;
! 421: document.ggbApplet1.evalCommand("GravityAngle=Angle[Gravitypoint,Gravitystart,Gravityend]");
! 422: document.ggbApplet1.setLabelVisible("GravityAngle",true);
! 423: document.ggbApplet1.setLabelStyle("GravityAngle",VALUE=2);
! 424: // Keeps track of points we care about (This should use the same listener function we use in graph problems)
! 425: //document.ggbApplet1.registerObjectUpdateListener('Gravitystart','updatePointCoordinates');
! 426: //document.ggbApplet1.registerObjectUpdateListener('Gravityend','updatePointCoordinates');
! 427: //document.ggbApplet1.registerObjectUpdateListener('GravityAngle','updatePointCoordinates');
! 428: ENDVECTOR
! 429: }
! 430:
! 431: #
1.58 www 432: # Answer spline display
433: #
434: # points: x,y,slope_x,slope_y
435:
436: sub answer_spline_script {
437: my ($id,@points)=@_;
438: my $order=int(($#points+1)/4);
439: if ($order<2) { $order=2; }
440: if ($order>8) { $order=8; }
441: $Apache::functionplotresponse::counter++;
1.60 www 442: my $label='CSpline'.$Apache::functionplotresponse::counter;
1.59 www 443: my $output='document.ggbApplet_'.$id.'.evalCommand("'.$label.'=Spline'.$order.'[';
444: for (my $i=0;$i<=$#points;$i+=4) {
445: $output.="($points[$i],$points[$i+1]),($points[$i+2],$points[$i+3]),";
446: }
447: $output=~s/\,$//;
448: $output.=']");'."\n";
449: for (my $i=2; $i<2*$order; $i+=2) {
1.61 www 450: $output.='document.ggbApplet_'.$id.'.setColor("'.$label.'_'.($i>=10?'{':'').$i.($i>=10?'}':'').'",0,170,0);'."\n";
1.59 www 451: }
1.60 www 452: for (my $i=1; $i<2*$order; $i+=2) {
1.61 www 453: $output.='document.ggbApplet_'.$id.'.setVisible("'.$label.'_'.($i>=10?'{':'').$i.($i>=10?'}':'').'",false);'."\n";
1.60 www 454: }
455:
1.59 www 456: return $output;
1.58 www 457: }
458:
459: #
1.7 www 460: # Subroutine that generates code for spline $label based on stored information
1.6 www 461: #
462:
463: sub generate_spline {
1.48 www 464: my ($id,$label,$xmin,$xmax,$ymin,$ymax,$fixed)=@_;
1.6 www 465: my $result='';
1.7 www 466: my $order=$Apache::functionplotresponse::splineorder{$label};
467: my $x=$Apache::functionplotresponse::splineinitx{$label};
468: my $y=$Apache::functionplotresponse::splineinity{$label};
469: my $sx=$Apache::functionplotresponse::splinescalex{$label};
470: my $sy=$Apache::functionplotresponse::splinescaley{$label};
471: my @coords=();
472: foreach my $i (1..$order) {
1.48 www 473: $result.=&new_point_coordinate($id,$label.'P'.$i,$x,$y,$fixed);
1.40 www 474: my $xp=$x;
1.8 www 475: $x+=$sx/(2.*($order-1));
1.7 www 476: push(@coords,$label.'P'.$i);
1.48 www 477: $result.=&new_slope_coordinate($id,$label.'S'.$i,$x,$y+$sy,$label.'P'.$i,$xp,$y,$xmin,$xmax,$ymin,$ymax,$fixed);
1.8 www 478: $x+=$sx/(2.*($order-1));
1.7 www 479: push(@coords,$label.'S'.$i);
480: }
481: $result.='document.ggbApplet_'.$id.'.evalCommand("Spline'.$order.'['.join(',',@coords).']");'."\n";
1.6 www 482: return $result;
483: }
1.70 ! www 484:
! 485: #
! 486: # Object
! 487: #
! 488:
! 489: sub start_plotobject {
! 490: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
! 491: my $result='';
! 492: my $internalid = $Apache::inputtags::part.'_'.$Apache::inputtags::response[-1];
! 493: my $x=&Apache::lonxml::get_param('x',$parstack,$safeeval);
! 494: my $y=&Apache::lonxml::get_param('y',$parstack,$safeeval);
! 495: my $label=&Apache::lonxml::get_param('label',$parstack,$safeeval);
! 496: $label=~s/\W//gs;
! 497: $label=ucfirst($label);
! 498: unless ($label) { $label="NewObject"; }
! 499: if ($target eq 'web') {
! 500: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-3);
! 501: unless (defined($x)) { $x=$xmin; }
! 502: unless (defined($y)) { $y=$ymin; }
! 503: $result.=&plotobject_script($internalid,$label,$x,$y);
! 504: } elsif ($target eq 'edit') {
! 505: $result=&Apache::edit::tag_start($target,$token,'Plot Object').
! 506: &Apache::edit::text_arg('Label on Plot:','label',
! 507: $token,'16').
! 508: &Apache::edit::text_arg('x:','x',
! 509: $token,'8').
! 510: &Apache::edit::text_arg('y:','y',
! 511: $token,'8').
! 512: &Apache::edit::end_row();
! 513: } elsif ($target eq 'modified') {
! 514: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'label','x','y');
! 515: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
! 516: }
! 517: return $result;
! 518: }
! 519:
! 520: sub end_plotobject {
! 521: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
! 522: my $result='';
! 523: if ($target eq 'edit') {
! 524: $result=&Apache::edit::end_table();
! 525: }
! 526: return $result;
! 527: }
! 528:
! 529:
1.6 www 530: #
531: # <backgroundplot function="..." fixed="yes/no" />
1.5 www 532: #
1.4 www 533: sub start_backgroundplot {
534: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
535: my $result='';
536: my $internalid = $Apache::inputtags::part.'_'.$Apache::inputtags::response[-1];
537: my $function=&Apache::lonxml::get_param('function',$parstack,$safeeval);
1.47 www 538: my $xinitial=&Apache::lonxml::get_param('xinitial',$parstack,$safeeval);
539: my $xfinal=&Apache::lonxml::get_param('xfinal',$parstack,$safeeval);
1.41 www 540: my $label=&Apache::lonxml::get_param('label',$parstack,$safeeval);
1.46 www 541: my $color=&Apache::lonxml::get_param('color',$parstack,$safeeval);
542: $color=~s/[^a-fA-F0-9]//gs;
543: unless (length($color)==6) { $color=''; }
1.6 www 544: my $fixed=(&Apache::lonxml::get_param('fixed',$parstack,$safeeval)=~/on|true|yes|1/i?1:0);
545:
1.4 www 546: unless ($function) { $function="0"; }
547: if ($target eq 'web') {
1.41 www 548: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-3);
1.47 www 549: unless (defined($xinitial)) { $xinitial=$xmin; }
550: unless (defined($xfinal)) { $xfinal=$xmax; }
551: $result.=&plot_script($internalid,$function,$fixed,$label,$color,$xinitial,$xfinal);
1.13 www 552: } elsif ($target eq 'edit') {
553: $result=&Apache::edit::tag_start($target,$token,'Background Function Plot').
554: &Apache::edit::text_arg('Function:','function',
555: $token,'16').
1.47 www 556: &Apache::edit::text_arg('Initial x-value (optional):','xinitial',
557: $token,'8').
558: &Apache::edit::text_arg('Final x-value (optional):','xfinal',
559: $token,'8').
1.41 www 560: &Apache::edit::text_arg('Label on Plot:','label',
561: $token,'8').
1.46 www 562: &Apache::edit::text_arg('Color (hex code):','color',
563: $token,'8').
1.13 www 564: &Apache::edit::select_arg('Fixed location:','fixed',
565: ['yes','no'],$token).
566: &Apache::edit::end_row();
567: } elsif ($target eq 'modified') {
568: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.47 www 569: $safeeval,'function','label','xinitial','xfinal','color','fixed');
1.13 www 570: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
571: }
572: return $result;
1.4 www 573: }
574:
575: sub end_backgroundplot {
1.13 www 576: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
577: my $result='';
578: if ($target eq 'edit') {
579: $result=&Apache::edit::end_table();
580: }
581: return $result;
1.6 www 582: }
583:
584: #
1.25 www 585: # <functionplotrule ... />
1.12 www 586: #
1.25 www 587: sub start_functionplotrule {
1.12 www 588: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
589: my $result='';
1.24 www 590: my $label=&Apache::lonxml::get_param('index',$parstack,$safeeval);
591: $Apache::functionplotresponse::counter++;
592: if ($label=~/\W/) {
1.25 www 593: &Apache::lonxml::warning(&mt('Rule indices should only contain alphanumeric characters.'));
1.24 www 594: }
595: $label=~s/\W//gs;
596: unless ($label) {
597: $label='R'.$Apache::functionplotresponse::counter;
598: } else {
599: $label='R'.$label;
600: }
601: if ($Apache::functionplotresponse::splineorder{$label}) {
1.25 www 602: &Apache::lonxml::error(&mt('Rule indices must be unique.'));
1.24 www 603: }
604:
605:
606: if ($target eq 'grade') {
607: # Simply remember - in order - for later
1.25 www 608: my $beginninglabel=&Apache::lonxml::get_param('xinitiallabel',$parstack,$safeeval);
609: my $endinglabel=&Apache::lonxml::get_param('xfinallabel',$parstack,$safeeval);
1.24 www 610: if (($beginninglabel=~/\W/) || ($endinglabel=~/W/)) {
1.25 www 611: &Apache::lonxml::warning(&mt('Rule labels must be alphanumeric.'));
1.24 www 612: }
613: $beginninglabel=~s/\W//gs;
614: $endinglabel=~s/\W//gs;
615: my $relationship=&Apache::lonxml::get_param('relationship',$parstack,$safeeval);
616: $relationship=~s/\W//gs;
617: $relationship=lc($relationship);
618: unless ($relationship=~/^(eq|ge|gt|le|lt|ne)$/) {
1.25 www 619: &Apache::lonxml::warning(&mt('Rule relationship not defined.'));
1.24 www 620: $relationship='eq';
621: }
1.35 www 622: my $derivative=&Apache::lonxml::get_param('derivativeorder',$parstack,$safeeval);
1.42 www 623: unless (($derivative==-1) || ($derivative==0) || ($derivative==1) || ($derivative==2)) {
1.25 www 624: &Apache::lonxml::warning(&mt('Rule derivative not defined.'));
1.24 www 625: $derivative=0;
626: }
1.25 www 627: push(@Apache::functionplotresponse::functionplotrules,join(':',(
1.24 www 628: $label,
629: $derivative,
1.25 www 630: &Apache::lonxml::get_param('xinitial',$parstack,$safeeval),
1.24 www 631: $beginninglabel,
1.25 www 632: &Apache::lonxml::get_param('xfinal',$parstack,$safeeval),
1.24 www 633: $endinglabel,
1.35 www 634: &Apache::lonxml::get_param('minimumlength',$parstack,$safeeval),
635: &Apache::lonxml::get_param('maximumlength',$parstack,$safeeval),
1.24 www 636: $relationship,
1.33 www 637: &Apache::lonxml::get_param('value',$parstack,$safeeval),
638: &Apache::lonxml::get_param('percenterror',$parstack,$safeeval)
1.24 www 639: )));
640: } elsif ($target eq 'edit') {
1.29 www 641: $result=&Apache::edit::tag_start($target,$token,'Function Plot Evaluation Rule').
1.36 www 642: &Apache::edit::text_arg('Index/Name:','index',
643: $token,'10').' '.
1.35 www 644: &Apache::edit::select_arg(&mt('Function:'),'derivativeorder',
1.29 www 645: [['0','Function itself'],
646: ['1','First derivative'],
1.42 www 647: ['2','Second derivative'],
648: ['-1','Integral']],$token).'<br />'.
1.44 www 649: &Apache::edit::text_arg('Initial x-value:','xinitial',
1.35 www 650: $token,'8').
1.44 www 651: &Apache::edit::select_or_text_arg('Initial x-value label:','xinitiallabel',
652: [['start','Start of Plot'],
653: ['end','End of Plot']],$token,'8').'<br />'.
1.30 www 654:
1.44 www 655: &Apache::edit::text_arg('Final x-value (optional):','xfinal',
1.35 www 656: $token,'8').
1.44 www 657: &Apache::edit::select_or_text_arg('Final x-value label (optional):','xfinallabel',
1.32 www 658: [['end','End of Plot']],$token,'8').'<br />'.
1.44 www 659: &Apache::edit::text_arg('Minimum length for range (optional):','minimumlength',
1.35 www 660: $token,'8').
1.44 www 661: &Apache::edit::text_arg('Maximum length for range (optional):','maximumlength',
1.35 www 662: $token,'8').'<br />'.
1.38 www 663: &Apache::edit::select_or_text_arg(&mt('Relationship:'),'relationship',
1.29 www 664: [['eq','equal'],
665: ['ne','not equal'],
666: ['ge','greater than or equal'],
667: ['gt','greater than'],
668: ['lt','less than'],
669: ['le','less than or equal']],$token).
670: $result.= &Apache::edit::select_or_text_arg('Value:','value',
1.65 www 671: [['undef','not defined']],$token,'30').
1.29 www 672: &Apache::edit::text_arg('Percent error:','percenterror',
1.35 www 673: $token,'8').
1.29 www 674: &Apache::edit::end_row();
675: } elsif ($target eq 'modified') {
1.54 www 676: if (($env{'form.'.&Apache::edit::html_element_name('xinitial')} ne '') && ($env{'form.'.&Apache::edit::html_element_name('xinitiallabel')} eq 'start')) {
1.53 www 677: $env{'form.'.&Apache::edit::html_element_name('xinitiallabel')}='';
678: }
1.54 www 679: if (($env{'form.'.&Apache::edit::html_element_name('xfinal')} ne '') && ($env{'form.'.&Apache::edit::html_element_name('xfinallabel')} eq 'end')) {
1.53 www 680: $env{'form.'.&Apache::edit::html_element_name('xfinallabel')}='';
681: }
1.29 www 682: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.35 www 683: $safeeval,'index','derivativeorder',
684: 'xinitial','xinitiallabel','xfinal','xfinallabel',
685: 'minimumlength','maximumlength',
686: 'relationship','value','percenterror');
1.29 www 687: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
1.13 www 688: }
1.12 www 689: return $result;
690: }
691:
1.25 www 692: sub end_functionplotrule {
1.13 www 693: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
694: my $result='';
695: if ($target eq 'edit') {
696: $result=&Apache::edit::end_table();
697: }
698: return $result;
1.12 www 699: }
700:
701:
702: #
1.23 www 703: # <spline index="..." order="1,2,3,4" initx="..." inity="..." scalex="..." scaley="..." />
1.6 www 704: #
705: # Unfortunately, GeoGebra seems to want all splines after everything else, so we need to store them
706: #
707: sub start_spline {
708: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.13 www 709: my $result='';
710: if ($target eq 'web') {
1.23 www 711: my $label=&Apache::lonxml::get_param('index',$parstack,$safeeval);
1.13 www 712: $Apache::functionplotresponse::counter++;
713: if ($label=~/\W/) {
1.23 www 714: &Apache::lonxml::warning(&mt('Spline indices should only contain alphanumeric characters.'));
1.13 www 715: }
716: $label=~s/\W//gs;
1.23 www 717: unless ($label) {
718: $label='S'.$Apache::functionplotresponse::counter;
719: } else {
720: $label='S'.$label;
721: }
1.13 www 722: if ($Apache::functionplotresponse::splineorder{$label}) {
1.23 www 723: &Apache::lonxml::error(&mt('Spline indices must be unique.'));
1.13 www 724: }
725:
726: my $order=&Apache::lonxml::get_param('order',$parstack,$safeeval);
727: if ($order<2) { $order=2; }
728: if ($order>8) { $order=8; }
729: $Apache::functionplotresponse::splineorder{$label}=$order;
730:
731: my $x=&Apache::lonxml::get_param('initx',$parstack,$safeeval);
732: unless ($x) { $x=0; }
733: $Apache::functionplotresponse::splineinitx{$label}=$x;
734:
735: my $y=&Apache::lonxml::get_param('inity',$parstack,$safeeval);
736: unless ($y) { $y=0; }
737: $Apache::functionplotresponse::splineinity{$label}=$y;
738:
739: my $sx=&Apache::lonxml::get_param('scalex',$parstack,$safeeval);
740: unless ($sx) { $sx=$order; }
741: $Apache::functionplotresponse::splinescalex{$label}=$sx;
742:
743: my $sy=&Apache::lonxml::get_param('scaley',$parstack,$safeeval);
744: unless ($sy) { $sy=2; }
745: $Apache::functionplotresponse::splinescaley{$label}=$sy;
746: } elsif ($target eq 'edit') {
747: $result=&Apache::edit::tag_start($target,$token,'Spline').
1.23 www 748: &Apache::edit::text_arg('Index:','index',
1.14 www 749: $token,'4').' '.
1.13 www 750: &Apache::edit::select_arg('Order:','order',
1.44 www 751: ['2','3','4','5','6','7','8'],$token).' '.
1.13 www 752: &Apache::edit::text_arg('Initial x-value:','initx',
1.14 www 753: $token,'4').' '.
1.13 www 754: &Apache::edit::text_arg('Initial y-value:','inity',
1.14 www 755: $token,'4').' '.
1.13 www 756: &Apache::edit::text_arg('Scale x:','scalex',
1.14 www 757: $token,'4').' '.
1.13 www 758: &Apache::edit::text_arg('Scale y:','scaley',
759: $token,'4').
760: &Apache::edit::end_row();
761: } elsif ($target eq 'modified') {
762: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.23 www 763: $safeeval,'index','order','initx','inity',
1.13 www 764: 'scalex','scaley');
765: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
766: }
767: return $result;
1.6 www 768: }
769:
770: sub end_spline {
1.13 www 771: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
772: my $result='';
773: if ($target eq 'edit') {
774: $result=&Apache::edit::end_table();
775: }
776: return $result;
1.4 www 777: }
778:
1.3 www 779: sub end_init_script {
780: return (<<ENDENDINIT);
781: }
782: // ]]>
783: </script>
784: ENDENDINIT
785: }
786:
1.10 www 787: #
788: # Storing and restoring spline coordinates from part answers
789: #
790: sub decode_previous_answer {
791: my ($answer)=@_;
792: foreach my $coordinate (split(/\,/,$answer)) {
793: my ($key,$value)=split(/\=/,$coordinate);
794: $Apache::functionplotresponse::previous{$key}=$value;
795: }
796: }
797:
798: sub get_answer_from_form_fields {
799: my ($id)=@_;
800: my $answer='';
801: my %coords=();
802: foreach my $field (keys(%env)) {
803: if ($field=~/^form\.HWVAL\_$id/) {
804: $field=~/^form\.(.*)$/;
805: $coords{$1}=$env{$field};
806: }
807: }
808: $answer=join(',',map { $_.'='.$coords{$_} } (sort(keys(%coords))));
809: return ($answer,%coords);
810: }
811:
1.15 www 812: #
813: # The following functions calculate the cubic-hermite splines server-side
814: #
815:
816: sub cubic_hermite {
817: my ($t,$p1,$s1,$p2,$s2)=@_;
1.42 www 818: return (2.*$t*$t*$t-3.*$t*$t+1.)*$p1 + 3.*($t*$t*$t-2.*$t*$t+$t)*($s1-$p1)+
819: (-2.*$t*$t*$t+3.*$t*$t) *$p2 + 3.*($t*$t*$t-$t*$t) *($s2-$p2);
1.15 www 820: }
821:
822: #
823: # d/dt(...)
824: #
825:
826: sub ddt_cubic_hermite {
827: my ($t,$p1,$s1,$p2,$s2)=@_;
1.42 www 828: return (6.*$t*$t-6.*$t) *$p1 + 3.*(3.*$t*$t-4.*$t+1.)*($s1-$p1)+
829: (-6.*$t*$t+6.*$t)*$p2 + 3.*(3.*$t*$t-2.*$t) *($s2-$p2);
1.15 www 830: }
831:
832: #
833: # d^2/dt^2(...)
834: #
835:
836: sub d2dt2_cubic_hermite {
837: my ($t,$p1,$s1,$p2,$s2)=@_;
1.42 www 838: return (12.*$t-6.) *$p1 + 3.*(6.*$t-4.)*($s1-$p1)+
839: (-12.*$t+6.)*$p2 + 3.*(6.*$t-2.)*($s2-$p2);
1.15 www 840: }
841:
842: #
1.18 www 843: # Array index calculation
844: #
845: sub array_index {
846: my ($xmin,$xmax,$x)=@_;
1.38 www 847: if ($x ne '') {
848: return int(($x-$xmin)/($xmax-$xmin)*400.+0.5);
849: } else {
850: return undef;
851: }
1.20 www 852: }
853:
854: #
1.18 www 855: # Populate the arrays
856: #
857:
858: sub populate_arrays {
1.64 www 859: my ($id,$xmin,$xmax,$ymin,$ymax)=@_;
1.26 www 860: for (my $i=0; $i<=400; $i++) {
1.35 www 861: $Apache::functionplotresponse::actualxval[$i]=undef;
1.19 www 862: $Apache::functionplotresponse::func[$i]=undef;
1.22 www 863: $Apache::functionplotresponse::dfuncdx[$i]=undef;
1.19 www 864: $Apache::functionplotresponse::d2funcd2x[$i]=undef;
1.18 www 865: }
1.19 www 866: unless ($xmax>$xmin) { return 'no_func'; }
1.18 www 867: # Run over all splines in response
868: foreach my $label (split(/\,/,$env{"form.HWVAL_AllSplines_$id"})) {
1.19 www 869: my $xiold=-1;
1.18 www 870: # Run over all points in spline
1.19 www 871: for (my $i=1; $i<$env{"form.HWVAL_SplineOrder_".$id."_".$label}; $i++) {
1.18 www 872: my $ni=$i+1;
873: my @xparms=($env{'form.HWVAL_'.$id.'_'.$label.'P'.$i.'_x'},
874: $env{'form.HWVAL_'.$id.'_'.$label.'S'.$i.'_x'},
875: $env{'form.HWVAL_'.$id.'_'.$label.'P'.$ni.'_x'},
876: $env{'form.HWVAL_'.$id.'_'.$label.'S'.$ni.'_x'});
877: my @yparms=($env{'form.HWVAL_'.$id.'_'.$label.'P'.$i.'_y'},
878: $env{'form.HWVAL_'.$id.'_'.$label.'S'.$i.'_y'},
879: $env{'form.HWVAL_'.$id.'_'.$label.'P'.$ni.'_y'},
880: $env{'form.HWVAL_'.$id.'_'.$label.'S'.$ni.'_y'});
881: # Run in small steps over spline parameter
1.26 www 882: for (my $t=0; $t<=1; $t+=0.0001) {
1.35 www 883: my $xreal=&cubic_hermite($t,@xparms);
884: my $xi=&array_index($xmin,$xmax,$xreal);
1.19 www 885: if ($xi<$xiold) { return 'no_func'; }
1.26 www 886: if (($xi>$xiold) && ($xi>=0) && ($xi<=400)) {
1.18 www 887: $xiold=$xi;
1.35 www 888: $Apache::functionplotresponse::actualxval[$xi]=$xreal;
1.22 www 889: # Function value
1.30 www 890: my $funcval=&cubic_hermite($t,@yparms);
1.64 www 891:
892: # Do we already have a value for this point, and is it different from the new one?
893: if ((defined($Apache::functionplotresponse::func[$xi])) &&
894: (abs($Apache::functionplotresponse::func[$xi]-$funcval)>($ymax-$ymin)/100.)) {
895: return 'no_func';
896: }
897: # Okay, remember the new point
1.30 www 898: $Apache::functionplotresponse::func[$xi]=$funcval;
1.64 www 899:
1.30 www 900: if (defined($funcval)) {
901: if ($xi<$Apache::functionplotresponse::functionplotrulelabels{'start'}) {
902: $Apache::functionplotresponse::functionplotrulelabels{'start'}=$xi;
903: }
904: if ($xi>$Apache::functionplotresponse::functionplotrulelabels{'end'}) {
905: $Apache::functionplotresponse::functionplotrulelabels{'end'}=$xi;
906: }
907: }
1.28 www 908: # Chain rule
1.22 www 909: # dy/dx=dy/dt/(dx/dt)
910: my $dxdt=&ddt_cubic_hermite($t,@xparms);
911: if ($dxdt) {
912: $Apache::functionplotresponse::dfuncdx[$xi]=&ddt_cubic_hermite($t,@yparms)/$dxdt;
1.34 www 913: # Second derivative
1.28 www 914: $Apache::functionplotresponse::d2funcdx2[$xi]=
1.34 www 915: ($dxdt*&d2dt2_cubic_hermite($t,@yparms)-&ddt_cubic_hermite($t,@yparms)*&d2dt2_cubic_hermite($t,@xparms))/
916: ($dxdt*$dxdt*$dxdt);
1.22 www 917: }
1.18 www 918: }
919: }
920: }
921: }
922: }
923:
924: #
1.37 www 925: # Implementation of <functionplotresponse>
1.15 www 926: #
927:
1.1 www 928: sub start_functionplotresponse {
929: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
930: my $result='';
1.6 www 931: # To remember the splines - somehow, they need to come last
932: undef %Apache::functionplotresponse::splineorder;
933: undef %Apache::functionplotresponse::splineinitx;
934: undef %Apache::functionplotresponse::splineinity;
935: undef %Apache::functionplotresponse::splinescalex;
936: undef %Apache::functionplotresponse::splinescaley;
1.10 www 937: # Remember input fields, etc
938: undef %Apache::functionplotresponse::previous;
1.9 www 939: $Apache::functionplotresponse::inputfields='';
1.6 www 940: $Apache::functionplotresponse::counter=0;
1.24 www 941: # Remember rules
1.25 www 942: undef @Apache::functionplotresponse::functionplotrules;
1.37 www 943: # Remember failed rules
944: if ($target eq 'grade') {
945: undef @Apache::functionplotresponse::failedrules;
946: }
947: # Delete previous awards
948: undef $Apache::functionplotresponse::awarddetail;
1.10 www 949: # Part and ID
950: my $partid=$Apache::inputtags::part;
951: my $id=&Apache::response::start_response($parstack,$safeeval);
1.6 www 952: # Internal ID to mark the applet and its coordinates
1.10 www 953: my $internalid = $partid.'_'.$id;
954: # Previous answer
955: &decode_previous_answer($Apache::lonhomework::history{"resource.$partid.$id.submission"});
956:
1.6 www 957: # Parameters of <functionplotresponse>
1.41 www 958: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval);
1.6 www 959: my $xaxisvisible=(&Apache::lonxml::get_param('xaxisvisible',$parstack,$safeeval)=~/on|true|yes|1/i?'true':'false');
960: my $yaxisvisible=(&Apache::lonxml::get_param('yaxisvisible',$parstack,$safeeval)=~/on|true|yes|1/i?'true':'false');
961: my $gridvisible=(&Apache::lonxml::get_param('gridvisible',$parstack,$safeeval)=~/on|true|yes|1/i?'true':'false');
1.14 www 962: my $xlabel=&Apache::lonxml::get_param('xlabel',$parstack,$safeeval);
963: my $ylabel=&Apache::lonxml::get_param('ylabel',$parstack,$safeeval);
1.12 www 964: if ($target eq 'edit') {
965: $result.=&Apache::edit::start_table($token)
966: .'<tr><td><span class="LC_nobreak">'.&mt('Function Plot Question').'</span></td>'
967: .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
1.37 www 968: .&Apache::edit::deletelist($target,$token).' '
1.50 www 969: .&Apache::edit::insertlist($target,$token).' '
970: .&Apache::loncommon::help_open_topic('Function_Plot_Response_Question','Function Plot Responses')
1.12 www 971: .'</span></td>'
972: ."<td> "
973: .&Apache::edit::end_row()
974: .&Apache::edit::start_spanning_row()
975: ."\n";
1.14 www 976: $result.=&Apache::edit::text_arg('Label x-axis:','xlabel',
977: $token,'6').' '.
978: &Apache::edit::text_arg('Minimum x-value:','xmin',
979: $token,'4').' '.
1.12 www 980: &Apache::edit::text_arg('Maximum x-value:','xmax',
1.14 www 981: $token,'4').' '.
1.12 www 982: &Apache::edit::select_arg('x-axis visible:','xaxisvisible',
1.32 www 983: ['yes','no'],$token).'<br />'.
1.14 www 984: &Apache::edit::text_arg('Label y-axis:','ylabel',
985: $token,'6').' '.
1.12 www 986: &Apache::edit::text_arg('Minimum y-value:','ymin',
1.14 www 987: $token,'4').' '.
1.12 www 988: &Apache::edit::text_arg('Maximum y-value:','ymax',
1.14 www 989: $token,'4').' '.
1.12 www 990: &Apache::edit::select_arg('y-axis visible:','yaxisvisible',
1.32 www 991: ['yes','no'],$token).'<br />'.
1.12 www 992: &Apache::edit::select_arg('Grid visible:','gridvisible',
1.47 www 993: ['yes','no'],$token).'<br />'.
1.58 www 994: &Apache::edit::text_arg('Background plot(s) for answer (function(x):xmin:xmax,function(x):xmin:xmax,x1:y1:sx1:sy1:x2:y2:sx2:sy2,...):',
1.63 raeburn 995: 'answerdisplay',$token,'50').
1.12 www 996: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
997: } elsif ($target eq 'modified') {
998: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.14 www 999: $safeeval,'xlabel','xmin','xmax','ylabel','ymin','ymax',
1.47 www 1000: 'xaxisvisible','yaxisvisible','gridvisible','answerdisplay');
1.12 www 1001: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.3 www 1002:
1.12 www 1003: } elsif ($target eq 'meta') {
1.10 www 1004: $result=&Apache::response::meta_package_write('functionplotresponse');
1.35 www 1005: } elsif (($target eq 'answer') &&
1006: ($env{'form.answer_output_mode'} ne 'tex') &&
1007: ($Apache::lonhomework::viewgrades == 'F')) {
1008: my (undef,undef,$udom,$uname)=&Apache::lonnet::whichuser();
1009: my $windowopen=&Apache::lonhtmlcommon::javascript_docopen();
1010: my $start_page = &Apache::loncommon::start_page('Rules Log', undef,
1011: {'only_body' => 1,
1012: 'bgcolor' => '#FFFFFF',
1013: 'js_ready' => 1,});
1014: my $end_page = &Apache::loncommon::end_page({'js_ready' => 1,});
1015: $uname =~s/\W//g;
1016: $udom =~s/\W//g;
1017: my $function_name =
1018: join('_','LONCAPA_scriptvars',$uname,$udom,
1019: $env{'form.counter'},$Apache::lonxml::curdepth);
1020: my $rules_var ="<script type=\"text/javascript\">
1021: // <![CDATA[
1022: function $function_name() {newWindow=open('','new_W','width=500,height=500,scrollbars=1,resizable=yes');newWindow.$windowopen;newWindow.document.writeln('$start_page<pre>".
1023: $Apache::functionplotresponse::ruleslog.
1024: "<\\/pre>$end_page');newWindow.document.close();newWindow.focus()}
1025: // ]]>
1026: </script><a href=\"javascript:$function_name();void(0);\">".&mt('Rules Log')."</a><br />";
1027: &Apache::lonxml::add_script_result($rules_var);
1028: }
1029:
1.1 www 1030: return $result;
1031: }
1032:
1.25 www 1033: sub compare_rel {
1.26 www 1034: my ($relationship,$value,$realval,$tol)=@_;
1.36 www 1035: # is the real value undefined?
1.26 www 1036: unless (defined($realval)) {
1.36 www 1037: # the real value is not defined
1.26 www 1038: if ($relationship eq 'eq') {
1039: if ($value eq 'undef') {
1040: return 1;
1041: } else {
1042: return 0;
1043: }
1044: } elsif ($relationship eq 'ne') {
1045: if ($value eq 'undef') {
1046: return 0;
1047: } else {
1048: return 1;
1049: }
1050: } else {
1051: return 0;
1052: }
1053: }
1054:
1.36 www 1055: # is the expected value undefined?
1056: if ($value eq 'undef') {
1057: # but by now we know that the real value is defined
1058: return 0;
1059: }
1060:
1061: # both are defined.
1.26 www 1062: if ($relationship eq 'gt') {
1063: return ($realval>$value);
1064: } elsif ($relationship eq 'ge') {
1065: return ($realval>$value-$tol);
1066: } elsif ($relationship eq 'lt') {
1067: return ($realval<$value);
1068: } elsif ($relationship eq 'le') {
1069: return ($realval<$value+$tol);
1070: } elsif ($relationship eq 'ne') {
1071: return (abs($value-$realval)>$tol);
1072: } else {
1073: return (abs($value-$realval)<$tol);
1074: }
1.25 www 1075: return 0;
1076: }
1077:
1.35 www 1078: sub addlog {
1079: my ($text)=@_;
1.67 www 1080: $text=~s/\'/\\\'/g;
1.35 www 1081: $Apache::functionplotresponse::ruleslog.=$text.'<br />';
1082: }
1083:
1084: sub actualval {
1085: my ($i,$xmin,$xmax)=@_;
1086: return $xmin+$i/400.*($xmax-$xmin);
1087: }
1.65 www 1088:
1.66 www 1089: sub fpr_val {
1090: my ($arg)=@_;
1091: return &actualval($Apache::functionplotresponse::functionplotrulelabels{$arg},
1092: $Apache::functionplotresponse::fpr_xmin,
1093: $Apache::functionplotresponse::fpr_xmax);
1094: }
1095:
1.65 www 1096: sub fpr_f {
1097: my ($arg)=@_;
1098: return $Apache::functionplotresponse::func[&array_index($Apache::functionplotresponse::fpr_xmin,
1099: $Apache::functionplotresponse::fpr_xmax,
1100: $arg)];
1101: }
1102:
1103: sub fpr_dfdx {
1104: my ($arg)=@_;
1105: return $Apache::functionplotresponse::dfuncdx[&array_index($Apache::functionplotresponse::fpr_xmin,
1106: $Apache::functionplotresponse::fpr_xmax,
1107: $arg)];
1108: }
1109:
1110: sub fpr_d2fdx2 {
1111: my ($arg)=@_;
1112: return $Apache::functionplotresponse::d2funcdx2[&array_index($Apache::functionplotresponse::fpr_xmin,
1113: $Apache::functionplotresponse::fpr_xmax,
1114: $arg)];
1115: }
1.35 www 1116:
1.25 www 1117: sub functionplotrulecheck {
1.65 www 1118: my ($rule,$xmin,$xmax,$ymin,$ymax,$safeeval)=@_;
1.33 www 1119:
1.35 www 1120: my ($label,$derivative,$xinitial,$xinitiallabel,$xfinal,$xfinallabel,$minimumlength,$maximumlength,$relationship,$value,$percent)
1.25 www 1121: =split(/\:/,$rule);
1.33 www 1122: $percent=($percent>0?$percent:5);
1.35 www 1123: &addlog("=================");
1.42 www 1124: &addlog("Rule $label for ".($derivative<0?'integral':('function itself','first derivative','second derivative')[$derivative])." $relationship $value");
1.65 www 1125: #
1126: # Evaluate the value
1127: #
1128: if ($value=~/\D/) {
1129: $Apache::functionplotresponse::fpr_xmin=$xmin;
1130: $Apache::functionplotresponse::fpr_xmax=$xmax;
1131: $value=&Apache::run::run($value,$safeeval);
1132: &addlog("Value evaluated to $value");
1133: }
1134:
1135: #
1136: # Minimum and maximum lengths of the interval
1137: #
1.49 www 1138: if ((defined($minimumlength)) || (defined($maximumlength))) {
1139: &addlog("Minimumlength $minimumlength Maximumlength $maximumlength");
1140: }
1.30 www 1141: my $li=0;
1142: my $lh=400;
1143:
1.31 www 1144: # Special case: the upper boundary was not defined
1145: # and needs to be set to the value where
1146: # the condition is not true anymore => set flag
1147:
1148: my $findupper=0;
1149: if (($xfinal eq '')
1150: && (!defined($Apache::functionplotresponse::functionplotrulelabels{$xfinallabel}))
1151: && ($xfinallabel)) {
1152: $findupper=1;
1153: }
1154:
1.25 www 1155: # if a hard value is set for the boundaries, it overrides the label
1.30 www 1156: if (($xinitial ne '') && ($xinitiallabel ne '') && ($xinitiallabel ne 'start')) {
1157: $li=&array_index($xmin,$xmax,$xinitial);
1158: $Apache::functionplotresponse::functionplotrulelabels{$xinitiallabel}=$li;
1159: }
1160: if (($xfinal ne '') && ($xfinallabel ne '') && ($xfinallabel ne 'end')) {
1161: $lh=&array_index($xmin,$xmax,$xfinal);
1162: $Apache::functionplotresponse::functionplotrulelabels{$xfinallabel}=$lh;
1.25 www 1163: }
1.31 www 1164: # if the label is defined, use it
1.25 www 1165: if (defined($Apache::functionplotresponse::functionplotrulelabels{$xinitiallabel})) {
1.35 www 1166: &addlog("Using lower label $xinitiallabel");
1.30 www 1167: $li=$Apache::functionplotresponse::functionplotrulelabels{$xinitiallabel};
1168: } else {
1169: $li=&array_index($xmin,$xmax,$xinitial);
1.25 www 1170: }
1.31 www 1171: unless ($findupper) {
1172: if (defined($Apache::functionplotresponse::functionplotrulelabels{$xfinallabel})) {
1.35 www 1173: &addlog("Using upper label $xfinallabel");
1.33 www 1174: $lh=$Apache::functionplotresponse::functionplotrulelabels{$xfinallabel}-1;
1.31 www 1175: } else {
1176: $lh=&array_index($xmin,$xmax,$xfinal);
1177: }
1.25 www 1178: }
1179: # Basic sanity checks
1.30 www 1180: if ($li<0) { $li=0; }
1181: if ($lh>400) { $lh=400; }
1.38 www 1182: if (($li>$lh) || (!defined($lh))) {
1.30 www 1183: $lh=$li;
1.25 www 1184: }
1.31 www 1185:
1.35 www 1186: &addlog("Boundaries: x=".&actualval($li,$xmin,$xmax)." (".$Apache::functionplotresponse::actualxval[$li]."; index $li)) to x=".
1187: &actualval($lh,$xmin,$xmax)." (".$Apache::functionplotresponse::actualxval[$lh]."; index $lh))");
1188: if ($findupper) {
1189: &addlog("Looking for label $xfinallabel");
1190: }
1.33 www 1191: my $tol=$percent*($ymax-$ymin)/100;
1192: if ($xmax>$xmin) {
1193: if ($derivative==2) {
1194: $tol=4.*$tol/($xmax-$xmin);
1195: } elsif ($derivative==1) {
1196: $tol=2.*$tol/($xmax-$xmin);
1.42 www 1197: } elsif ($derivative==-1) {
1198: $tol=$tol*($xmax-$xmin)/2.;
1.33 www 1199: }
1.27 www 1200: }
1.42 www 1201: my $integral=0;
1202: my $binwidth=($xmax-$xmin)/400.;
1203: if (($derivative<0) && (!$findupper)) {
1204: # definite integral, calculate over whole length
1205: &addlog("Calculating definite integral");
1206: for (my $i=$li; $i<=$lh; $i++) {
1207: $integral+=$Apache::functionplotresponse::func[$i]*$binwidth;
1208: }
1209: unless (&compare_rel($relationship,$value,$integral,$tol)) {
1210: &addlog("Actual integral ".(defined($integral)?$integral:'undef').", expected $value, tolerance $tol");
1211: &addlog("Rule $label failed.");
1212: my $hintlabel=$label;
1213: $hintlabel=~s/^R//;
1214: push(@Apache::functionplotresponse::failedrules,$hintlabel);
1215: &addlog("Set hint condition $hintlabel");
1216: return 0;
1217: }
1218: } else {
1219: for (my $i=$li; $i<=$lh; $i++) {
1.25 www 1220: my $val;
1221: if ($derivative==2) {
1.27 www 1222: $val=$Apache::functionplotresponse::d2funcdx2[$i];
1.25 www 1223: } elsif ($derivative==1) {
1.27 www 1224: $val=$Apache::functionplotresponse::dfuncdx[$i];
1.42 www 1225: } elsif ($derivative==-1) {
1226: $integral+=$Apache::functionplotresponse::func[$i]*$binwidth;
1227: $val=$integral;
1.25 www 1228: } else {
1.27 www 1229: $val=$Apache::functionplotresponse::func[$i];
1.25 www 1230: }
1.31 www 1231: unless (&compare_rel($relationship,$value,$val,$tol)) {
1.37 www 1232: &addlog("Actual value ".(defined($val)?$val:'undef').", expected $value, tolerance $tol");
1.35 www 1233: &addlog("Condition not fulfilled at x=".&actualval($i,$xmin,$xmax)." (".$Apache::functionplotresponse::actualxval[$i]."; index $i)");
1.31 www 1234: if (($findupper) && ($i>$li)) {
1.49 www 1235: # Check lengths
1236: unless (&checklength($i,$li,$minimumlength,$maximumlength,$xmin,$xmax,$label)) { return 0; }
1237: # Successfully found a new label, set it
1.31 www 1238: $Apache::functionplotresponse::functionplotrulelabels{$xfinallabel}=$i;
1.35 www 1239: &addlog("Rule $label passed, setting label $xfinallabel");
1.31 www 1240: return 1;
1241: } else {
1.35 www 1242: &addlog("Rule $label failed.");
1.49 www 1243: &setfailed($label);
1.31 www 1244: return 0;
1245: }
1246: }
1.42 www 1247: }
1.25 www 1248: }
1.49 www 1249: # Corner case where this makes sense: using start or stop as defined labels
1250: unless (&checklength($lh,$li,$minimumlength,$maximumlength,$xmin,$xmax,$label)) { return 0; }
1.35 www 1251: &addlog("Rule $label passed.");
1.27 www 1252: return 1;
1.24 www 1253: }
1254:
1.49 www 1255: #
1256: # check for minimum and maximum lengths
1257: #
1258:
1259: sub checklength {
1260: my ($i,$li,$minimumlength,$maximumlength,$xmin,$xmax,$label)=@_;
1261: unless (($minimumlength) || ($maximumlength)) { return 1; }
1262: my $length=&actualval($i,$xmin,$xmax)-&actualval($li,$xmin,$xmax);
1263: if ($minimumlength) {
1264: if ($length<$minimumlength) {
1265: &addlog("Rule $label failed, actual length $length, minimum length $minimumlength");
1266: &setfailed($label);
1267: return 0;
1268: }
1269: }
1270: if ($maximumlength) {
1271: if ($length>$maximumlength) {
1272: &addlog("Rule $label failed, actual length $length, maximum length $maximumlength");
1273: &setfailed($label);
1274: return 0;
1275: }
1276: }
1277: return 1;
1278: }
1279:
1280: sub setfailed {
1281: my ($label)=@_;
1282: my $hintlabel=$label;
1283: $hintlabel=~s/^R//;
1284: push(@Apache::functionplotresponse::failedrules,$hintlabel);
1285: &addlog("Set hint condition $hintlabel");
1286: }
1287:
1.37 www 1288: sub start_functionplotruleset {
1289: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1290: if ($target eq 'edit') {
1291: return &Apache::edit::start_table($token).
1292: '<tr><td><span class="LC_nobreak">'.&mt('Function Plot Rule Set').'</span></td>'
1293: .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
1294: .&Apache::edit::deletelist($target,$token).' '.
1.50 www 1295: &Apache::edit::insertlist($target,$token).' '
1296: .&Apache::loncommon::help_open_topic('Function_Plot_Response_Rule_Set','Function Plot Rules')
1.37 www 1297: .'</span></td>'
1298: ."<td> "
1299: .&Apache::edit::end_row()
1300: .&Apache::edit::start_spanning_row()
1301: ."\n";
1302: }
1303: }
1.3 www 1304:
1.37 www 1305: sub end_functionplotruleset {
1306: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1307: my $id=$Apache::inputtags::response[-1];
1308: my $partid=$Apache::inputtags::part;
1309: my $internalid = $partid.'_'.$id;
1310:
1311: if ($target eq 'edit' ) {
1312: return &Apache::edit::end_table();
1313: } elsif ($target eq 'grade'
1.10 www 1314: && &Apache::response::submitted()
1315: && $Apache::lonhomework::type ne 'exam') {
1.12 www 1316: #
1317: # Actually grade
1318: #
1.41 www 1319: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-2);
1.27 www 1320:
1.24 www 1321: my $ad='';
1.30 www 1322: undef %Apache::functionplotresponse::functionplotrulelabels;
1.35 www 1323: $Apache::functionplotresponse::ruleslog='';
1.30 www 1324: $Apache::functionplotresponse::functionplotrulelabels{'start'}=400;
1325: $Apache::functionplotresponse::functionplotrulelabels{'end'}=0;
1.64 www 1326: if (&populate_arrays($internalid,$xmin,$xmax,$ymin,$ymax) eq 'no_func') {
1.21 www 1327: $ad='NOT_FUNCTION';
1.18 www 1328: } else {
1.35 www 1329: &addlog("Start of function ".&actualval($Apache::functionplotresponse::functionplotrulelabels{'start'},$xmin,$xmax)." (index ".
1330: $Apache::functionplotresponse::functionplotrulelabels{'start'}.")");
1331: &addlog("End of function ".&actualval($Apache::functionplotresponse::functionplotrulelabels{'end'},$xmin,$xmax)." (index ".
1332: $Apache::functionplotresponse::functionplotrulelabels{'end'}.")");
1333:
1.24 www 1334: # We have a function that we can actually grade, go through the spline rules.
1.25 www 1335: foreach my $rule (@Apache::functionplotresponse::functionplotrules) {
1.65 www 1336: unless (&functionplotrulecheck($rule,$xmin,$xmax,$ymin,$ymax,$safeeval)) {
1.24 www 1337: $ad='INCORRECT';
1338: last;
1339: }
1340: }
1341: # If it's not wrong, it's correct
1342: unless ($ad) { $ad='EXACT_ANS' };
1.18 www 1343: }
1.37 www 1344: &addlog("Set hint conditions: ".join(",",@Apache::functionplotresponse::failedrules));
1345: &addlog("Assigned award detail: $ad");
1346: # Store for later to be assigned at end_functionplotresponse
1347: $Apache::functionplotresponse::awarddetail=$ad;
1348: }
1349: }
1350:
1351:
1352: sub end_functionplotresponse {
1353: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1354: &Apache::response::end_response;
1355:
1356: my $result;
1357: my $id=$Apache::inputtags::response[-1];
1358: my $partid=$Apache::inputtags::part;
1359: my $internalid = $partid.'_'.$id;
1.20 www 1360:
1.37 www 1361: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
1362: if ($target eq 'grade'
1363: && &Apache::response::submitted()
1364: && $Apache::lonhomework::type eq 'exam') {
1365:
1366: &Apache::response::scored_response($partid,$id);
1367:
1368: } elsif ($target eq 'grade'
1369: && &Apache::response::submitted()
1370: && $Apache::lonhomework::type ne 'exam') {
1371: my ($response,%coords)=&get_answer_from_form_fields($internalid);
1372: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
1373: my %previous=&Apache::response::check_for_previous($response,$partid,$id);
1374: #
1375: # Assign grade
1376: #
1377: my $ad=$Apache::functionplotresponse::awarddetail;
1.12 www 1378: #
1379: # Store grading info
1380: #
1381: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
1382: &Apache::response::handle_previous(\%previous,$ad);
1.10 www 1383: } elsif ($target eq 'web') {
1.37 www 1384: undef @Apache::functionplotresponse::failedrules;
1385: }
1386: return $result;
1387: }
1388:
1389: sub end_functionplotelements {
1390: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1391: my $result='';
1392: my $id=$Apache::inputtags::response[-1];
1393: my $partid=$Apache::inputtags::part;
1394: my $internalid = $partid.'_'.$id;
1395: if ($target eq 'edit' ) {
1396: $result=&Apache::edit::end_table();
1397: } elsif ($target eq 'web') {
1.47 www 1398: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-2);
1399:
1400: # Are we in show answer mode?
1401: my $showanswer=&Apache::response::show_answer();
1402: if ($showanswer) {
1403: # Render answerdisplay
1404: my $answerdisplay=&Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval,-2);
1405: if ($answerdisplay=~/\S/s) {
1406: foreach my $plot (split(/\s*\,\s*/,$answerdisplay)) {
1.58 www 1407: my @components=split(/\s*\:\s*/,$plot);
1408: if ($#components<3) {
1409: # Just a simple plot
1410: my ($func,$xl,$xh)=@components;
1411: if ((!defined($xl)) || ($xl eq '')) { $xl=$xmin; }
1412: if ((!defined($xh)) || ($xh eq '')) { $xh=$xmax; }
1413: $result.=&plot_script($internalid,$func,1,'','00aa00',$xl,$xh,6);
1414: } else {
1415: # This is a spline
1.59 www 1416: $result.=&answer_spline_script($internalid,@components);
1.58 www 1417: }
1.47 www 1418: }
1419: }
1420: }
1.48 www 1421: my $fixed=0;
1422: if (($showanswer) || (&Apache::response::check_status()>=2)) { $fixed=1; }
1.6 www 1423: # Now is the time to render all of the stored splines
1424: foreach my $label (keys(%Apache::functionplotresponse::splineorder)) {
1.48 www 1425: $result.=&generate_spline($internalid,$label,$xmin,$xmax,$ymin,$ymax,$fixed);
1.6 www 1426: }
1.5 www 1427: # close the init script
1.3 www 1428: $result.=&end_init_script();
1.17 www 1429: # register all splines in this response
1430: $result.='<input type="hidden" name="HWVAL_AllSplines_'.$internalid.'" value="'.
1431: join(',',keys(%Apache::functionplotresponse::splineorder)).'" />'."\n";
1432: foreach my $label (keys(%Apache::functionplotresponse::splineorder)) {
1433: $result.='<input type="hidden" name="HWVAL_SplineOrder_'.$internalid.'_'.$label.'" value="'.
1434: $Apache::functionplotresponse::splineorder{$label}.'" />'."\n";
1435: }
1.9 www 1436: # generate the input fields
1437: $result.=$Apache::functionplotresponse::inputfields;
1.5 www 1438: # actually start the <applet>-tag
1.3 www 1439: $result.=&geogebra_startcode($internalid);
1.5 www 1440: # load the spline bytecode
1.3 www 1441: $result.=&geogebra_spline_program();
1.5 www 1442: # set default parameters
1.3 www 1443: $result.=&geogebra_default_parameters($internalid);
1.5 www 1444: # close the <applet>-tag
1.1 www 1445: $result.=&geogebra_endcode();
1446: }
1447: return $result;
1448: }
1449:
1.41 www 1450: sub boundaries {
1451: my ($parstack,$safeeval,$level)=@_;
1452: my $xmin=&Apache::lonxml::get_param('xmin',$parstack,$safeeval,$level);
1453: $xmin=(defined($xmin)?$xmin:-10);
1454: my $xmax=&Apache::lonxml::get_param('xmax',$parstack,$safeeval,$level);
1455: $xmax=(defined($xmax)?$xmax:10);
1456: my $ymin=&Apache::lonxml::get_param('ymin',$parstack,$safeeval,$level);
1457: $ymin=(defined($ymin)?$ymin:-10);
1458: my $ymax=&Apache::lonxml::get_param('ymax',$parstack,$safeeval,$level);
1459: $ymax=(defined($ymax)?$ymax:10);
1460: if ($xmax<=$xmin) {
1461: $xmax=$xmin+20;
1462: }
1463: if ($ymax<=$ymin) {
1464: $ymax=$ymin+20;
1465: }
1466: return ($xmin,$xmax,$ymin,$ymax);
1467: }
1468:
1.37 www 1469: sub start_functionplotelements {
1470: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1471: my $result='';
1472: my $id=$Apache::inputtags::response[-1];
1473: my $partid=$Apache::inputtags::part;
1474: my $internalid = $partid.'_'.$id;
1475:
1476: if ($target eq 'edit') {
1477: return &Apache::edit::start_table($token).
1478: '<tr><td><span class="LC_nobreak">'.&mt('Function Plot Elements').'</span></td>'
1479: .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
1480: .&Apache::edit::deletelist($target,$token).' '.
1.50 www 1481: &Apache::edit::insertlist($target,$token).' '
1482: .&Apache::loncommon::help_open_topic('Function_Plot_Response_Elements','Function Plot Elements')
1.37 www 1483: .'</span></td>'
1484: ."<td> "
1485: .&Apache::edit::end_row()
1486: .&Apache::edit::start_spanning_row()
1487: ."\n";
1488: } elsif ($target eq 'web') {
1.41 www 1489: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-2);
1.37 www 1490: my $xaxisvisible=(&Apache::lonxml::get_param('xaxisvisible',$parstack,$safeeval,-2)=~/on|true|yes|1/i?'true':'false');
1491: my $yaxisvisible=(&Apache::lonxml::get_param('yaxisvisible',$parstack,$safeeval,-2)=~/on|true|yes|1/i?'true':'false');
1492: my $gridvisible=(&Apache::lonxml::get_param('gridvisible',$parstack,$safeeval,-2)=~/on|true|yes|1/i?'true':'false');
1493: my $xlabel=&Apache::lonxml::get_param('xlabel',$parstack,$safeeval,-2);
1494: my $ylabel=&Apache::lonxml::get_param('ylabel',$parstack,$safeeval,-2);
1495:
1496:
1497: # paste in the update routine to receive stuff back from the applet
1498: $result.=&update_script($internalid);
1499: # start the initscript for this applet
1500: $result.=&start_init_script($internalid);
1501: # put the axis commands inside
1502: $result.=&axes_script($internalid,$xmin,$xmax,$ymin,$ymax,$xaxisvisible,$yaxisvisible,$gridvisible);
1.45 www 1503: $result.=&axes_label($internalid,$xmin,$xmax,$ymin,$ymax,$xlabel,$ylabel);
1.37 www 1504: # init script is left open
1505: }
1506: return $result;
1507: }
1508:
1.1 www 1509: 1;
1510:
1511: __END__
1512:
1513: =head1 NAME
1514:
1515: Apache::functionplotresponse.pm;
1516:
1517: =head1 SYNOPSIS
1518:
1519: Handles tags associated with accepting function plots.
1520:
1521: This is part of the LearningOnline Network with CAPA project
1522: described at http://www.lon-capa.org.
1523:
1524: =head1 HANDLER SUBROUTINE
1525:
1526: start_functionplotresponse()
1527:
1528: =head1 OTHER SUBROUTINES
1529:
1530: =over
1531:
1532: =item end_functionplotresponse()
1533:
1534: =back
1535:
1536: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>