Annotation of loncom/homework/functionplotresponse.pm, revision 1.73
1.1 www 1: # LearningOnline Network with CAPA
1.70 www 2: # Functionplot responses
1.1 www 3: #
1.73 ! www 4: # $Id: functionplotresponse.pm,v 1.72 2011/11/18 18:02:04 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/0XWafmm06LtJNH9/O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qX6eHJCpESFTQ0KmxoRMDQqZGhUyNSxkalzI1MCQqZGhUyNDJ5+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 {
1.72 www 405: my ($id,$label,$xs,$ys,$xe,$ye,$xmin,$xmax)=@_;
1.70 www 406: unless ($label) {
407: $Apache::functionplotresponse::counter++;
408: $label='V'.$Apache::functionplotresponse::counter;
409: }
1.71 www 410: my $startlabel=$label.'Start';
411: my $endlabel=$label.'End';
412: my $pointlabel=$label.'Point';
1.72 www 413: my $pointx=2.*($xmax-$xmin)+$xmax;
1.71 www 414: my $anglelabel=$label.'Angle';
1.70 www 415: return(<<ENDVECTOR);
1.71 www 416: document.ggbApplet_$id.evalCommand("$startlabel=($xs,$ys)");
417: document.ggbApplet_$id.setVisible("$startlabel",false);
418: document.ggbApplet_$id.setLabelVisible("$startlabel",false);
419: document.ggbApplet_$id.evalCommand("$endlabel=($xe,$ye)");
420: document.ggbApplet_$id.setLabelVisible("$endlabel",false);
421: document.ggbApplet_$id.evalCommand("$label=Vector[$startlabel,$endlabel]");
422: document.ggbApplet_$id.setLabelVisible("$label",true);
423: document.ggbApplet_$id.setLineThickness("$label",8);
1.72 www 424: document.ggbApplet_$id.evalCommand("$pointlabel=($pointx,y($startlabel))");
425: document.ggbApplet_$id.evalCommand("$anglelabel=Angle[$pointlabel,$startlabel,$endlabel]");
426: document.ggbApplet_$id.setLabelVisible("$anglelabel",true);
427: document.ggbApplet_$id.setLabelStyle("$anglelabel",VALUE=2);
1.73 ! www 428: document.ggbApplet_$id.registerObjectUpdateListener('$startlabel','updatePointCoordinates_$id');
! 429: document.ggbApplet_$id.registerObjectUpdateListener('$endlabel','updatePointCoordinates_$id');
! 430: document.ggbApplet_$id.registerObjectUpdateListener('$anglelabel','updatePointCoordinates_$id');
1.70 www 431: ENDVECTOR
432: }
433:
434: #
1.58 www 435: # Answer spline display
436: #
437: # points: x,y,slope_x,slope_y
438:
439: sub answer_spline_script {
440: my ($id,@points)=@_;
441: my $order=int(($#points+1)/4);
442: if ($order<2) { $order=2; }
443: if ($order>8) { $order=8; }
444: $Apache::functionplotresponse::counter++;
1.60 www 445: my $label='CSpline'.$Apache::functionplotresponse::counter;
1.59 www 446: my $output='document.ggbApplet_'.$id.'.evalCommand("'.$label.'=Spline'.$order.'[';
447: for (my $i=0;$i<=$#points;$i+=4) {
448: $output.="($points[$i],$points[$i+1]),($points[$i+2],$points[$i+3]),";
449: }
450: $output=~s/\,$//;
451: $output.=']");'."\n";
452: for (my $i=2; $i<2*$order; $i+=2) {
1.61 www 453: $output.='document.ggbApplet_'.$id.'.setColor("'.$label.'_'.($i>=10?'{':'').$i.($i>=10?'}':'').'",0,170,0);'."\n";
1.59 www 454: }
1.60 www 455: for (my $i=1; $i<2*$order; $i+=2) {
1.61 www 456: $output.='document.ggbApplet_'.$id.'.setVisible("'.$label.'_'.($i>=10?'{':'').$i.($i>=10?'}':'').'",false);'."\n";
1.60 www 457: }
458:
1.59 www 459: return $output;
1.58 www 460: }
461:
462: #
1.7 www 463: # Subroutine that generates code for spline $label based on stored information
1.6 www 464: #
465:
466: sub generate_spline {
1.48 www 467: my ($id,$label,$xmin,$xmax,$ymin,$ymax,$fixed)=@_;
1.6 www 468: my $result='';
1.7 www 469: my $order=$Apache::functionplotresponse::splineorder{$label};
470: my $x=$Apache::functionplotresponse::splineinitx{$label};
471: my $y=$Apache::functionplotresponse::splineinity{$label};
472: my $sx=$Apache::functionplotresponse::splinescalex{$label};
473: my $sy=$Apache::functionplotresponse::splinescaley{$label};
474: my @coords=();
475: foreach my $i (1..$order) {
1.48 www 476: $result.=&new_point_coordinate($id,$label.'P'.$i,$x,$y,$fixed);
1.40 www 477: my $xp=$x;
1.8 www 478: $x+=$sx/(2.*($order-1));
1.7 www 479: push(@coords,$label.'P'.$i);
1.48 www 480: $result.=&new_slope_coordinate($id,$label.'S'.$i,$x,$y+$sy,$label.'P'.$i,$xp,$y,$xmin,$xmax,$ymin,$ymax,$fixed);
1.8 www 481: $x+=$sx/(2.*($order-1));
1.7 www 482: push(@coords,$label.'S'.$i);
483: }
484: $result.='document.ggbApplet_'.$id.'.evalCommand("Spline'.$order.'['.join(',',@coords).']");'."\n";
1.6 www 485: return $result;
486: }
1.70 www 487:
488: #
489: # Object
490: #
491:
492: sub start_plotobject {
493: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
494: my $result='';
495: my $internalid = $Apache::inputtags::part.'_'.$Apache::inputtags::response[-1];
496: my $x=&Apache::lonxml::get_param('x',$parstack,$safeeval);
497: my $y=&Apache::lonxml::get_param('y',$parstack,$safeeval);
498: my $label=&Apache::lonxml::get_param('label',$parstack,$safeeval);
499: $label=~s/\W//gs;
500: $label=ucfirst($label);
501: unless ($label) { $label="NewObject"; }
502: if ($target eq 'web') {
503: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-3);
504: unless (defined($x)) { $x=$xmin; }
505: unless (defined($y)) { $y=$ymin; }
506: $result.=&plotobject_script($internalid,$label,$x,$y);
507: } elsif ($target eq 'edit') {
508: $result=&Apache::edit::tag_start($target,$token,'Plot Object').
509: &Apache::edit::text_arg('Label on Plot:','label',
510: $token,'16').
511: &Apache::edit::text_arg('x:','x',
512: $token,'8').
513: &Apache::edit::text_arg('y:','y',
514: $token,'8').
515: &Apache::edit::end_row();
516: } elsif ($target eq 'modified') {
517: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'label','x','y');
518: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
519: }
520: return $result;
521: }
522:
523: sub end_plotobject {
524: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
525: my $result='';
526: if ($target eq 'edit') {
527: $result=&Apache::edit::end_table();
528: }
529: return $result;
530: }
531:
1.71 www 532: #
533: # Vector
534: #
535:
536: sub start_plotvector {
537: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
538: my $result='';
539: my $internalid = $Apache::inputtags::part.'_'.$Apache::inputtags::response[-1];
540: my $tailx=&Apache::lonxml::get_param('tailx',$parstack,$safeeval);
541: my $taily=&Apache::lonxml::get_param('taily',$parstack,$safeeval);
542: my $tipx=&Apache::lonxml::get_param('tipx',$parstack,$safeeval);
543: my $tipy=&Apache::lonxml::get_param('tipy',$parstack,$safeeval);
544:
545: my $label=&Apache::lonxml::get_param('label',$parstack,$safeeval);
546: $label=~s/\W//gs;
547: $label=ucfirst($label);
548: unless ($label) { $label="NewVector"; }
549: if ($target eq 'web') {
550: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-3);
551: unless (defined($tailx)) { $tailx=$xmin; }
552: unless (defined($taily)) { $taily=$ymin; }
553: unless (defined($tipx)) { $tipx=$xmin; }
554: unless (defined($tipy)) { $tipy=$ymin; }
1.72 www 555: $result.=&plotvector_script($internalid,$label,$tailx,$taily,$tipx,$tipy,$xmin,$xmax);
1.71 www 556: } elsif ($target eq 'edit') {
557: $result=&Apache::edit::tag_start($target,$token,'Plot Vector').
558: &Apache::edit::text_arg('Label on Plot:','label',
559: $token,'16').
560: &Apache::edit::text_arg('Tail x:','tailx',
561: $token,'8').
562: &Apache::edit::text_arg('Tail y:','taily',
563: $token,'8').
564: &Apache::edit::text_arg('Tip x:','tipx',
565: $token,'8').
566: &Apache::edit::text_arg('Tip y:','tipy',
567: $token,'8').
568:
569: &Apache::edit::end_row();
570: } elsif ($target eq 'modified') {
571: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'label','tailx','taily','tipx','tipy');
572: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
573: }
574: return $result;
575: }
576:
577: sub end_plotvector {
578: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
579: my $result='';
580: if ($target eq 'edit') {
581: $result=&Apache::edit::end_table();
582: }
583: return $result;
584: }
585:
586:
1.70 www 587:
1.6 www 588: #
589: # <backgroundplot function="..." fixed="yes/no" />
1.5 www 590: #
1.4 www 591: sub start_backgroundplot {
592: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
593: my $result='';
594: my $internalid = $Apache::inputtags::part.'_'.$Apache::inputtags::response[-1];
595: my $function=&Apache::lonxml::get_param('function',$parstack,$safeeval);
1.47 www 596: my $xinitial=&Apache::lonxml::get_param('xinitial',$parstack,$safeeval);
597: my $xfinal=&Apache::lonxml::get_param('xfinal',$parstack,$safeeval);
1.41 www 598: my $label=&Apache::lonxml::get_param('label',$parstack,$safeeval);
1.46 www 599: my $color=&Apache::lonxml::get_param('color',$parstack,$safeeval);
600: $color=~s/[^a-fA-F0-9]//gs;
601: unless (length($color)==6) { $color=''; }
1.6 www 602: my $fixed=(&Apache::lonxml::get_param('fixed',$parstack,$safeeval)=~/on|true|yes|1/i?1:0);
603:
1.4 www 604: unless ($function) { $function="0"; }
605: if ($target eq 'web') {
1.41 www 606: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-3);
1.47 www 607: unless (defined($xinitial)) { $xinitial=$xmin; }
608: unless (defined($xfinal)) { $xfinal=$xmax; }
609: $result.=&plot_script($internalid,$function,$fixed,$label,$color,$xinitial,$xfinal);
1.13 www 610: } elsif ($target eq 'edit') {
611: $result=&Apache::edit::tag_start($target,$token,'Background Function Plot').
612: &Apache::edit::text_arg('Function:','function',
613: $token,'16').
1.47 www 614: &Apache::edit::text_arg('Initial x-value (optional):','xinitial',
615: $token,'8').
616: &Apache::edit::text_arg('Final x-value (optional):','xfinal',
617: $token,'8').
1.41 www 618: &Apache::edit::text_arg('Label on Plot:','label',
619: $token,'8').
1.46 www 620: &Apache::edit::text_arg('Color (hex code):','color',
621: $token,'8').
1.13 www 622: &Apache::edit::select_arg('Fixed location:','fixed',
623: ['yes','no'],$token).
624: &Apache::edit::end_row();
625: } elsif ($target eq 'modified') {
626: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.47 www 627: $safeeval,'function','label','xinitial','xfinal','color','fixed');
1.13 www 628: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
629: }
630: return $result;
1.4 www 631: }
632:
633: sub end_backgroundplot {
1.13 www 634: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
635: my $result='';
636: if ($target eq 'edit') {
637: $result=&Apache::edit::end_table();
638: }
639: return $result;
1.6 www 640: }
641:
642: #
1.25 www 643: # <functionplotrule ... />
1.12 www 644: #
1.25 www 645: sub start_functionplotrule {
1.12 www 646: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
647: my $result='';
1.24 www 648: my $label=&Apache::lonxml::get_param('index',$parstack,$safeeval);
649: $Apache::functionplotresponse::counter++;
650: if ($label=~/\W/) {
1.25 www 651: &Apache::lonxml::warning(&mt('Rule indices should only contain alphanumeric characters.'));
1.24 www 652: }
653: $label=~s/\W//gs;
654: unless ($label) {
655: $label='R'.$Apache::functionplotresponse::counter;
656: } else {
657: $label='R'.$label;
658: }
659: if ($Apache::functionplotresponse::splineorder{$label}) {
1.25 www 660: &Apache::lonxml::error(&mt('Rule indices must be unique.'));
1.24 www 661: }
662:
663:
664: if ($target eq 'grade') {
665: # Simply remember - in order - for later
1.25 www 666: my $beginninglabel=&Apache::lonxml::get_param('xinitiallabel',$parstack,$safeeval);
667: my $endinglabel=&Apache::lonxml::get_param('xfinallabel',$parstack,$safeeval);
1.24 www 668: if (($beginninglabel=~/\W/) || ($endinglabel=~/W/)) {
1.25 www 669: &Apache::lonxml::warning(&mt('Rule labels must be alphanumeric.'));
1.24 www 670: }
671: $beginninglabel=~s/\W//gs;
672: $endinglabel=~s/\W//gs;
673: my $relationship=&Apache::lonxml::get_param('relationship',$parstack,$safeeval);
674: $relationship=~s/\W//gs;
675: $relationship=lc($relationship);
676: unless ($relationship=~/^(eq|ge|gt|le|lt|ne)$/) {
1.25 www 677: &Apache::lonxml::warning(&mt('Rule relationship not defined.'));
1.24 www 678: $relationship='eq';
679: }
1.35 www 680: my $derivative=&Apache::lonxml::get_param('derivativeorder',$parstack,$safeeval);
1.42 www 681: unless (($derivative==-1) || ($derivative==0) || ($derivative==1) || ($derivative==2)) {
1.25 www 682: &Apache::lonxml::warning(&mt('Rule derivative not defined.'));
1.24 www 683: $derivative=0;
684: }
1.25 www 685: push(@Apache::functionplotresponse::functionplotrules,join(':',(
1.24 www 686: $label,
687: $derivative,
1.25 www 688: &Apache::lonxml::get_param('xinitial',$parstack,$safeeval),
1.24 www 689: $beginninglabel,
1.25 www 690: &Apache::lonxml::get_param('xfinal',$parstack,$safeeval),
1.24 www 691: $endinglabel,
1.35 www 692: &Apache::lonxml::get_param('minimumlength',$parstack,$safeeval),
693: &Apache::lonxml::get_param('maximumlength',$parstack,$safeeval),
1.24 www 694: $relationship,
1.33 www 695: &Apache::lonxml::get_param('value',$parstack,$safeeval),
696: &Apache::lonxml::get_param('percenterror',$parstack,$safeeval)
1.24 www 697: )));
698: } elsif ($target eq 'edit') {
1.29 www 699: $result=&Apache::edit::tag_start($target,$token,'Function Plot Evaluation Rule').
1.36 www 700: &Apache::edit::text_arg('Index/Name:','index',
701: $token,'10').' '.
1.35 www 702: &Apache::edit::select_arg(&mt('Function:'),'derivativeorder',
1.29 www 703: [['0','Function itself'],
704: ['1','First derivative'],
1.42 www 705: ['2','Second derivative'],
706: ['-1','Integral']],$token).'<br />'.
1.44 www 707: &Apache::edit::text_arg('Initial x-value:','xinitial',
1.35 www 708: $token,'8').
1.44 www 709: &Apache::edit::select_or_text_arg('Initial x-value label:','xinitiallabel',
710: [['start','Start of Plot'],
711: ['end','End of Plot']],$token,'8').'<br />'.
1.30 www 712:
1.44 www 713: &Apache::edit::text_arg('Final x-value (optional):','xfinal',
1.35 www 714: $token,'8').
1.44 www 715: &Apache::edit::select_or_text_arg('Final x-value label (optional):','xfinallabel',
1.32 www 716: [['end','End of Plot']],$token,'8').'<br />'.
1.44 www 717: &Apache::edit::text_arg('Minimum length for range (optional):','minimumlength',
1.35 www 718: $token,'8').
1.44 www 719: &Apache::edit::text_arg('Maximum length for range (optional):','maximumlength',
1.35 www 720: $token,'8').'<br />'.
1.38 www 721: &Apache::edit::select_or_text_arg(&mt('Relationship:'),'relationship',
1.29 www 722: [['eq','equal'],
723: ['ne','not equal'],
724: ['ge','greater than or equal'],
725: ['gt','greater than'],
726: ['lt','less than'],
727: ['le','less than or equal']],$token).
728: $result.= &Apache::edit::select_or_text_arg('Value:','value',
1.65 www 729: [['undef','not defined']],$token,'30').
1.29 www 730: &Apache::edit::text_arg('Percent error:','percenterror',
1.35 www 731: $token,'8').
1.29 www 732: &Apache::edit::end_row();
733: } elsif ($target eq 'modified') {
1.54 www 734: if (($env{'form.'.&Apache::edit::html_element_name('xinitial')} ne '') && ($env{'form.'.&Apache::edit::html_element_name('xinitiallabel')} eq 'start')) {
1.53 www 735: $env{'form.'.&Apache::edit::html_element_name('xinitiallabel')}='';
736: }
1.54 www 737: if (($env{'form.'.&Apache::edit::html_element_name('xfinal')} ne '') && ($env{'form.'.&Apache::edit::html_element_name('xfinallabel')} eq 'end')) {
1.53 www 738: $env{'form.'.&Apache::edit::html_element_name('xfinallabel')}='';
739: }
1.29 www 740: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.35 www 741: $safeeval,'index','derivativeorder',
742: 'xinitial','xinitiallabel','xfinal','xfinallabel',
743: 'minimumlength','maximumlength',
744: 'relationship','value','percenterror');
1.29 www 745: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
1.13 www 746: }
1.12 www 747: return $result;
748: }
749:
1.25 www 750: sub end_functionplotrule {
1.13 www 751: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
752: my $result='';
753: if ($target eq 'edit') {
754: $result=&Apache::edit::end_table();
755: }
756: return $result;
1.12 www 757: }
758:
759:
760: #
1.23 www 761: # <spline index="..." order="1,2,3,4" initx="..." inity="..." scalex="..." scaley="..." />
1.6 www 762: #
763: # Unfortunately, GeoGebra seems to want all splines after everything else, so we need to store them
764: #
765: sub start_spline {
766: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.13 www 767: my $result='';
768: if ($target eq 'web') {
1.23 www 769: my $label=&Apache::lonxml::get_param('index',$parstack,$safeeval);
1.13 www 770: $Apache::functionplotresponse::counter++;
771: if ($label=~/\W/) {
1.23 www 772: &Apache::lonxml::warning(&mt('Spline indices should only contain alphanumeric characters.'));
1.13 www 773: }
774: $label=~s/\W//gs;
1.23 www 775: unless ($label) {
776: $label='S'.$Apache::functionplotresponse::counter;
777: } else {
778: $label='S'.$label;
779: }
1.13 www 780: if ($Apache::functionplotresponse::splineorder{$label}) {
1.23 www 781: &Apache::lonxml::error(&mt('Spline indices must be unique.'));
1.13 www 782: }
783:
784: my $order=&Apache::lonxml::get_param('order',$parstack,$safeeval);
785: if ($order<2) { $order=2; }
786: if ($order>8) { $order=8; }
787: $Apache::functionplotresponse::splineorder{$label}=$order;
788:
789: my $x=&Apache::lonxml::get_param('initx',$parstack,$safeeval);
790: unless ($x) { $x=0; }
791: $Apache::functionplotresponse::splineinitx{$label}=$x;
792:
793: my $y=&Apache::lonxml::get_param('inity',$parstack,$safeeval);
794: unless ($y) { $y=0; }
795: $Apache::functionplotresponse::splineinity{$label}=$y;
796:
797: my $sx=&Apache::lonxml::get_param('scalex',$parstack,$safeeval);
798: unless ($sx) { $sx=$order; }
799: $Apache::functionplotresponse::splinescalex{$label}=$sx;
800:
801: my $sy=&Apache::lonxml::get_param('scaley',$parstack,$safeeval);
802: unless ($sy) { $sy=2; }
803: $Apache::functionplotresponse::splinescaley{$label}=$sy;
804: } elsif ($target eq 'edit') {
805: $result=&Apache::edit::tag_start($target,$token,'Spline').
1.23 www 806: &Apache::edit::text_arg('Index:','index',
1.14 www 807: $token,'4').' '.
1.13 www 808: &Apache::edit::select_arg('Order:','order',
1.44 www 809: ['2','3','4','5','6','7','8'],$token).' '.
1.13 www 810: &Apache::edit::text_arg('Initial x-value:','initx',
1.14 www 811: $token,'4').' '.
1.13 www 812: &Apache::edit::text_arg('Initial y-value:','inity',
1.14 www 813: $token,'4').' '.
1.13 www 814: &Apache::edit::text_arg('Scale x:','scalex',
1.14 www 815: $token,'4').' '.
1.13 www 816: &Apache::edit::text_arg('Scale y:','scaley',
817: $token,'4').
818: &Apache::edit::end_row();
819: } elsif ($target eq 'modified') {
820: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.23 www 821: $safeeval,'index','order','initx','inity',
1.13 www 822: 'scalex','scaley');
823: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
824: }
825: return $result;
1.6 www 826: }
827:
828: sub end_spline {
1.13 www 829: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
830: my $result='';
831: if ($target eq 'edit') {
832: $result=&Apache::edit::end_table();
833: }
834: return $result;
1.4 www 835: }
836:
1.3 www 837: sub end_init_script {
838: return (<<ENDENDINIT);
839: }
840: // ]]>
841: </script>
842: ENDENDINIT
843: }
844:
1.10 www 845: #
846: # Storing and restoring spline coordinates from part answers
847: #
848: sub decode_previous_answer {
849: my ($answer)=@_;
850: foreach my $coordinate (split(/\,/,$answer)) {
851: my ($key,$value)=split(/\=/,$coordinate);
852: $Apache::functionplotresponse::previous{$key}=$value;
853: }
854: }
855:
856: sub get_answer_from_form_fields {
857: my ($id)=@_;
858: my $answer='';
859: my %coords=();
860: foreach my $field (keys(%env)) {
861: if ($field=~/^form\.HWVAL\_$id/) {
862: $field=~/^form\.(.*)$/;
863: $coords{$1}=$env{$field};
864: }
865: }
866: $answer=join(',',map { $_.'='.$coords{$_} } (sort(keys(%coords))));
867: return ($answer,%coords);
868: }
869:
1.15 www 870: #
871: # The following functions calculate the cubic-hermite splines server-side
872: #
873:
874: sub cubic_hermite {
875: my ($t,$p1,$s1,$p2,$s2)=@_;
1.42 www 876: return (2.*$t*$t*$t-3.*$t*$t+1.)*$p1 + 3.*($t*$t*$t-2.*$t*$t+$t)*($s1-$p1)+
877: (-2.*$t*$t*$t+3.*$t*$t) *$p2 + 3.*($t*$t*$t-$t*$t) *($s2-$p2);
1.15 www 878: }
879:
880: #
881: # d/dt(...)
882: #
883:
884: sub ddt_cubic_hermite {
885: my ($t,$p1,$s1,$p2,$s2)=@_;
1.42 www 886: return (6.*$t*$t-6.*$t) *$p1 + 3.*(3.*$t*$t-4.*$t+1.)*($s1-$p1)+
887: (-6.*$t*$t+6.*$t)*$p2 + 3.*(3.*$t*$t-2.*$t) *($s2-$p2);
1.15 www 888: }
889:
890: #
891: # d^2/dt^2(...)
892: #
893:
894: sub d2dt2_cubic_hermite {
895: my ($t,$p1,$s1,$p2,$s2)=@_;
1.42 www 896: return (12.*$t-6.) *$p1 + 3.*(6.*$t-4.)*($s1-$p1)+
897: (-12.*$t+6.)*$p2 + 3.*(6.*$t-2.)*($s2-$p2);
1.15 www 898: }
899:
900: #
1.18 www 901: # Array index calculation
902: #
903: sub array_index {
904: my ($xmin,$xmax,$x)=@_;
1.38 www 905: if ($x ne '') {
906: return int(($x-$xmin)/($xmax-$xmin)*400.+0.5);
907: } else {
908: return undef;
909: }
1.20 www 910: }
911:
912: #
1.18 www 913: # Populate the arrays
914: #
915:
916: sub populate_arrays {
1.64 www 917: my ($id,$xmin,$xmax,$ymin,$ymax)=@_;
1.26 www 918: for (my $i=0; $i<=400; $i++) {
1.35 www 919: $Apache::functionplotresponse::actualxval[$i]=undef;
1.19 www 920: $Apache::functionplotresponse::func[$i]=undef;
1.22 www 921: $Apache::functionplotresponse::dfuncdx[$i]=undef;
1.19 www 922: $Apache::functionplotresponse::d2funcd2x[$i]=undef;
1.18 www 923: }
1.19 www 924: unless ($xmax>$xmin) { return 'no_func'; }
1.18 www 925: # Run over all splines in response
926: foreach my $label (split(/\,/,$env{"form.HWVAL_AllSplines_$id"})) {
1.19 www 927: my $xiold=-1;
1.18 www 928: # Run over all points in spline
1.19 www 929: for (my $i=1; $i<$env{"form.HWVAL_SplineOrder_".$id."_".$label}; $i++) {
1.18 www 930: my $ni=$i+1;
931: my @xparms=($env{'form.HWVAL_'.$id.'_'.$label.'P'.$i.'_x'},
932: $env{'form.HWVAL_'.$id.'_'.$label.'S'.$i.'_x'},
933: $env{'form.HWVAL_'.$id.'_'.$label.'P'.$ni.'_x'},
934: $env{'form.HWVAL_'.$id.'_'.$label.'S'.$ni.'_x'});
935: my @yparms=($env{'form.HWVAL_'.$id.'_'.$label.'P'.$i.'_y'},
936: $env{'form.HWVAL_'.$id.'_'.$label.'S'.$i.'_y'},
937: $env{'form.HWVAL_'.$id.'_'.$label.'P'.$ni.'_y'},
938: $env{'form.HWVAL_'.$id.'_'.$label.'S'.$ni.'_y'});
939: # Run in small steps over spline parameter
1.26 www 940: for (my $t=0; $t<=1; $t+=0.0001) {
1.35 www 941: my $xreal=&cubic_hermite($t,@xparms);
942: my $xi=&array_index($xmin,$xmax,$xreal);
1.19 www 943: if ($xi<$xiold) { return 'no_func'; }
1.26 www 944: if (($xi>$xiold) && ($xi>=0) && ($xi<=400)) {
1.18 www 945: $xiold=$xi;
1.35 www 946: $Apache::functionplotresponse::actualxval[$xi]=$xreal;
1.22 www 947: # Function value
1.30 www 948: my $funcval=&cubic_hermite($t,@yparms);
1.64 www 949:
950: # Do we already have a value for this point, and is it different from the new one?
951: if ((defined($Apache::functionplotresponse::func[$xi])) &&
952: (abs($Apache::functionplotresponse::func[$xi]-$funcval)>($ymax-$ymin)/100.)) {
953: return 'no_func';
954: }
955: # Okay, remember the new point
1.30 www 956: $Apache::functionplotresponse::func[$xi]=$funcval;
1.64 www 957:
1.30 www 958: if (defined($funcval)) {
959: if ($xi<$Apache::functionplotresponse::functionplotrulelabels{'start'}) {
960: $Apache::functionplotresponse::functionplotrulelabels{'start'}=$xi;
961: }
962: if ($xi>$Apache::functionplotresponse::functionplotrulelabels{'end'}) {
963: $Apache::functionplotresponse::functionplotrulelabels{'end'}=$xi;
964: }
965: }
1.28 www 966: # Chain rule
1.22 www 967: # dy/dx=dy/dt/(dx/dt)
968: my $dxdt=&ddt_cubic_hermite($t,@xparms);
969: if ($dxdt) {
970: $Apache::functionplotresponse::dfuncdx[$xi]=&ddt_cubic_hermite($t,@yparms)/$dxdt;
1.34 www 971: # Second derivative
1.28 www 972: $Apache::functionplotresponse::d2funcdx2[$xi]=
1.34 www 973: ($dxdt*&d2dt2_cubic_hermite($t,@yparms)-&ddt_cubic_hermite($t,@yparms)*&d2dt2_cubic_hermite($t,@xparms))/
974: ($dxdt*$dxdt*$dxdt);
1.22 www 975: }
1.18 www 976: }
977: }
978: }
979: }
980: }
981:
982: #
1.37 www 983: # Implementation of <functionplotresponse>
1.15 www 984: #
985:
1.1 www 986: sub start_functionplotresponse {
987: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
988: my $result='';
1.6 www 989: # To remember the splines - somehow, they need to come last
990: undef %Apache::functionplotresponse::splineorder;
991: undef %Apache::functionplotresponse::splineinitx;
992: undef %Apache::functionplotresponse::splineinity;
993: undef %Apache::functionplotresponse::splinescalex;
994: undef %Apache::functionplotresponse::splinescaley;
1.10 www 995: # Remember input fields, etc
996: undef %Apache::functionplotresponse::previous;
1.9 www 997: $Apache::functionplotresponse::inputfields='';
1.6 www 998: $Apache::functionplotresponse::counter=0;
1.24 www 999: # Remember rules
1.25 www 1000: undef @Apache::functionplotresponse::functionplotrules;
1.37 www 1001: # Remember failed rules
1002: if ($target eq 'grade') {
1003: undef @Apache::functionplotresponse::failedrules;
1004: }
1005: # Delete previous awards
1006: undef $Apache::functionplotresponse::awarddetail;
1.10 www 1007: # Part and ID
1008: my $partid=$Apache::inputtags::part;
1009: my $id=&Apache::response::start_response($parstack,$safeeval);
1.6 www 1010: # Internal ID to mark the applet and its coordinates
1.10 www 1011: my $internalid = $partid.'_'.$id;
1012: # Previous answer
1013: &decode_previous_answer($Apache::lonhomework::history{"resource.$partid.$id.submission"});
1014:
1.6 www 1015: # Parameters of <functionplotresponse>
1.41 www 1016: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval);
1.6 www 1017: my $xaxisvisible=(&Apache::lonxml::get_param('xaxisvisible',$parstack,$safeeval)=~/on|true|yes|1/i?'true':'false');
1018: my $yaxisvisible=(&Apache::lonxml::get_param('yaxisvisible',$parstack,$safeeval)=~/on|true|yes|1/i?'true':'false');
1019: my $gridvisible=(&Apache::lonxml::get_param('gridvisible',$parstack,$safeeval)=~/on|true|yes|1/i?'true':'false');
1.14 www 1020: my $xlabel=&Apache::lonxml::get_param('xlabel',$parstack,$safeeval);
1021: my $ylabel=&Apache::lonxml::get_param('ylabel',$parstack,$safeeval);
1.12 www 1022: if ($target eq 'edit') {
1023: $result.=&Apache::edit::start_table($token)
1024: .'<tr><td><span class="LC_nobreak">'.&mt('Function Plot Question').'</span></td>'
1025: .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
1.37 www 1026: .&Apache::edit::deletelist($target,$token).' '
1.50 www 1027: .&Apache::edit::insertlist($target,$token).' '
1028: .&Apache::loncommon::help_open_topic('Function_Plot_Response_Question','Function Plot Responses')
1.12 www 1029: .'</span></td>'
1030: ."<td> "
1031: .&Apache::edit::end_row()
1032: .&Apache::edit::start_spanning_row()
1033: ."\n";
1.14 www 1034: $result.=&Apache::edit::text_arg('Label x-axis:','xlabel',
1035: $token,'6').' '.
1036: &Apache::edit::text_arg('Minimum x-value:','xmin',
1037: $token,'4').' '.
1.12 www 1038: &Apache::edit::text_arg('Maximum x-value:','xmax',
1.14 www 1039: $token,'4').' '.
1.12 www 1040: &Apache::edit::select_arg('x-axis visible:','xaxisvisible',
1.32 www 1041: ['yes','no'],$token).'<br />'.
1.14 www 1042: &Apache::edit::text_arg('Label y-axis:','ylabel',
1043: $token,'6').' '.
1.12 www 1044: &Apache::edit::text_arg('Minimum y-value:','ymin',
1.14 www 1045: $token,'4').' '.
1.12 www 1046: &Apache::edit::text_arg('Maximum y-value:','ymax',
1.14 www 1047: $token,'4').' '.
1.12 www 1048: &Apache::edit::select_arg('y-axis visible:','yaxisvisible',
1.32 www 1049: ['yes','no'],$token).'<br />'.
1.12 www 1050: &Apache::edit::select_arg('Grid visible:','gridvisible',
1.47 www 1051: ['yes','no'],$token).'<br />'.
1.58 www 1052: &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 1053: 'answerdisplay',$token,'50').
1.12 www 1054: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
1055: } elsif ($target eq 'modified') {
1056: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.14 www 1057: $safeeval,'xlabel','xmin','xmax','ylabel','ymin','ymax',
1.47 www 1058: 'xaxisvisible','yaxisvisible','gridvisible','answerdisplay');
1.12 www 1059: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.3 www 1060:
1.12 www 1061: } elsif ($target eq 'meta') {
1.10 www 1062: $result=&Apache::response::meta_package_write('functionplotresponse');
1.35 www 1063: } elsif (($target eq 'answer') &&
1064: ($env{'form.answer_output_mode'} ne 'tex') &&
1065: ($Apache::lonhomework::viewgrades == 'F')) {
1066: my (undef,undef,$udom,$uname)=&Apache::lonnet::whichuser();
1067: my $windowopen=&Apache::lonhtmlcommon::javascript_docopen();
1068: my $start_page = &Apache::loncommon::start_page('Rules Log', undef,
1069: {'only_body' => 1,
1070: 'bgcolor' => '#FFFFFF',
1071: 'js_ready' => 1,});
1072: my $end_page = &Apache::loncommon::end_page({'js_ready' => 1,});
1073: $uname =~s/\W//g;
1074: $udom =~s/\W//g;
1075: my $function_name =
1076: join('_','LONCAPA_scriptvars',$uname,$udom,
1077: $env{'form.counter'},$Apache::lonxml::curdepth);
1078: my $rules_var ="<script type=\"text/javascript\">
1079: // <![CDATA[
1080: function $function_name() {newWindow=open('','new_W','width=500,height=500,scrollbars=1,resizable=yes');newWindow.$windowopen;newWindow.document.writeln('$start_page<pre>".
1081: $Apache::functionplotresponse::ruleslog.
1082: "<\\/pre>$end_page');newWindow.document.close();newWindow.focus()}
1083: // ]]>
1084: </script><a href=\"javascript:$function_name();void(0);\">".&mt('Rules Log')."</a><br />";
1085: &Apache::lonxml::add_script_result($rules_var);
1086: }
1087:
1.1 www 1088: return $result;
1089: }
1090:
1.25 www 1091: sub compare_rel {
1.26 www 1092: my ($relationship,$value,$realval,$tol)=@_;
1.36 www 1093: # is the real value undefined?
1.26 www 1094: unless (defined($realval)) {
1.36 www 1095: # the real value is not defined
1.26 www 1096: if ($relationship eq 'eq') {
1097: if ($value eq 'undef') {
1098: return 1;
1099: } else {
1100: return 0;
1101: }
1102: } elsif ($relationship eq 'ne') {
1103: if ($value eq 'undef') {
1104: return 0;
1105: } else {
1106: return 1;
1107: }
1108: } else {
1109: return 0;
1110: }
1111: }
1112:
1.36 www 1113: # is the expected value undefined?
1114: if ($value eq 'undef') {
1115: # but by now we know that the real value is defined
1116: return 0;
1117: }
1118:
1119: # both are defined.
1.26 www 1120: if ($relationship eq 'gt') {
1121: return ($realval>$value);
1122: } elsif ($relationship eq 'ge') {
1123: return ($realval>$value-$tol);
1124: } elsif ($relationship eq 'lt') {
1125: return ($realval<$value);
1126: } elsif ($relationship eq 'le') {
1127: return ($realval<$value+$tol);
1128: } elsif ($relationship eq 'ne') {
1129: return (abs($value-$realval)>$tol);
1130: } else {
1131: return (abs($value-$realval)<$tol);
1132: }
1.25 www 1133: return 0;
1134: }
1135:
1.35 www 1136: sub addlog {
1137: my ($text)=@_;
1.67 www 1138: $text=~s/\'/\\\'/g;
1.35 www 1139: $Apache::functionplotresponse::ruleslog.=$text.'<br />';
1140: }
1141:
1142: sub actualval {
1143: my ($i,$xmin,$xmax)=@_;
1144: return $xmin+$i/400.*($xmax-$xmin);
1145: }
1.65 www 1146:
1.66 www 1147: sub fpr_val {
1148: my ($arg)=@_;
1149: return &actualval($Apache::functionplotresponse::functionplotrulelabels{$arg},
1150: $Apache::functionplotresponse::fpr_xmin,
1151: $Apache::functionplotresponse::fpr_xmax);
1152: }
1153:
1.65 www 1154: sub fpr_f {
1155: my ($arg)=@_;
1156: return $Apache::functionplotresponse::func[&array_index($Apache::functionplotresponse::fpr_xmin,
1157: $Apache::functionplotresponse::fpr_xmax,
1158: $arg)];
1159: }
1160:
1161: sub fpr_dfdx {
1162: my ($arg)=@_;
1163: return $Apache::functionplotresponse::dfuncdx[&array_index($Apache::functionplotresponse::fpr_xmin,
1164: $Apache::functionplotresponse::fpr_xmax,
1165: $arg)];
1166: }
1167:
1168: sub fpr_d2fdx2 {
1169: my ($arg)=@_;
1170: return $Apache::functionplotresponse::d2funcdx2[&array_index($Apache::functionplotresponse::fpr_xmin,
1171: $Apache::functionplotresponse::fpr_xmax,
1172: $arg)];
1173: }
1.35 www 1174:
1.25 www 1175: sub functionplotrulecheck {
1.65 www 1176: my ($rule,$xmin,$xmax,$ymin,$ymax,$safeeval)=@_;
1.33 www 1177:
1.35 www 1178: my ($label,$derivative,$xinitial,$xinitiallabel,$xfinal,$xfinallabel,$minimumlength,$maximumlength,$relationship,$value,$percent)
1.25 www 1179: =split(/\:/,$rule);
1.33 www 1180: $percent=($percent>0?$percent:5);
1.35 www 1181: &addlog("=================");
1.42 www 1182: &addlog("Rule $label for ".($derivative<0?'integral':('function itself','first derivative','second derivative')[$derivative])." $relationship $value");
1.65 www 1183: #
1184: # Evaluate the value
1185: #
1186: if ($value=~/\D/) {
1187: $Apache::functionplotresponse::fpr_xmin=$xmin;
1188: $Apache::functionplotresponse::fpr_xmax=$xmax;
1189: $value=&Apache::run::run($value,$safeeval);
1190: &addlog("Value evaluated to $value");
1191: }
1192:
1193: #
1194: # Minimum and maximum lengths of the interval
1195: #
1.49 www 1196: if ((defined($minimumlength)) || (defined($maximumlength))) {
1197: &addlog("Minimumlength $minimumlength Maximumlength $maximumlength");
1198: }
1.30 www 1199: my $li=0;
1200: my $lh=400;
1201:
1.31 www 1202: # Special case: the upper boundary was not defined
1203: # and needs to be set to the value where
1204: # the condition is not true anymore => set flag
1205:
1206: my $findupper=0;
1207: if (($xfinal eq '')
1208: && (!defined($Apache::functionplotresponse::functionplotrulelabels{$xfinallabel}))
1209: && ($xfinallabel)) {
1210: $findupper=1;
1211: }
1212:
1.25 www 1213: # if a hard value is set for the boundaries, it overrides the label
1.30 www 1214: if (($xinitial ne '') && ($xinitiallabel ne '') && ($xinitiallabel ne 'start')) {
1215: $li=&array_index($xmin,$xmax,$xinitial);
1216: $Apache::functionplotresponse::functionplotrulelabels{$xinitiallabel}=$li;
1217: }
1218: if (($xfinal ne '') && ($xfinallabel ne '') && ($xfinallabel ne 'end')) {
1219: $lh=&array_index($xmin,$xmax,$xfinal);
1220: $Apache::functionplotresponse::functionplotrulelabels{$xfinallabel}=$lh;
1.25 www 1221: }
1.31 www 1222: # if the label is defined, use it
1.25 www 1223: if (defined($Apache::functionplotresponse::functionplotrulelabels{$xinitiallabel})) {
1.35 www 1224: &addlog("Using lower label $xinitiallabel");
1.30 www 1225: $li=$Apache::functionplotresponse::functionplotrulelabels{$xinitiallabel};
1226: } else {
1227: $li=&array_index($xmin,$xmax,$xinitial);
1.25 www 1228: }
1.31 www 1229: unless ($findupper) {
1230: if (defined($Apache::functionplotresponse::functionplotrulelabels{$xfinallabel})) {
1.35 www 1231: &addlog("Using upper label $xfinallabel");
1.33 www 1232: $lh=$Apache::functionplotresponse::functionplotrulelabels{$xfinallabel}-1;
1.31 www 1233: } else {
1234: $lh=&array_index($xmin,$xmax,$xfinal);
1235: }
1.25 www 1236: }
1237: # Basic sanity checks
1.30 www 1238: if ($li<0) { $li=0; }
1239: if ($lh>400) { $lh=400; }
1.38 www 1240: if (($li>$lh) || (!defined($lh))) {
1.30 www 1241: $lh=$li;
1.25 www 1242: }
1.31 www 1243:
1.35 www 1244: &addlog("Boundaries: x=".&actualval($li,$xmin,$xmax)." (".$Apache::functionplotresponse::actualxval[$li]."; index $li)) to x=".
1245: &actualval($lh,$xmin,$xmax)." (".$Apache::functionplotresponse::actualxval[$lh]."; index $lh))");
1246: if ($findupper) {
1247: &addlog("Looking for label $xfinallabel");
1248: }
1.33 www 1249: my $tol=$percent*($ymax-$ymin)/100;
1250: if ($xmax>$xmin) {
1251: if ($derivative==2) {
1252: $tol=4.*$tol/($xmax-$xmin);
1253: } elsif ($derivative==1) {
1254: $tol=2.*$tol/($xmax-$xmin);
1.42 www 1255: } elsif ($derivative==-1) {
1256: $tol=$tol*($xmax-$xmin)/2.;
1.33 www 1257: }
1.27 www 1258: }
1.42 www 1259: my $integral=0;
1260: my $binwidth=($xmax-$xmin)/400.;
1261: if (($derivative<0) && (!$findupper)) {
1262: # definite integral, calculate over whole length
1263: &addlog("Calculating definite integral");
1264: for (my $i=$li; $i<=$lh; $i++) {
1265: $integral+=$Apache::functionplotresponse::func[$i]*$binwidth;
1266: }
1267: unless (&compare_rel($relationship,$value,$integral,$tol)) {
1268: &addlog("Actual integral ".(defined($integral)?$integral:'undef').", expected $value, tolerance $tol");
1269: &addlog("Rule $label failed.");
1270: my $hintlabel=$label;
1271: $hintlabel=~s/^R//;
1272: push(@Apache::functionplotresponse::failedrules,$hintlabel);
1273: &addlog("Set hint condition $hintlabel");
1274: return 0;
1275: }
1276: } else {
1277: for (my $i=$li; $i<=$lh; $i++) {
1.25 www 1278: my $val;
1279: if ($derivative==2) {
1.27 www 1280: $val=$Apache::functionplotresponse::d2funcdx2[$i];
1.25 www 1281: } elsif ($derivative==1) {
1.27 www 1282: $val=$Apache::functionplotresponse::dfuncdx[$i];
1.42 www 1283: } elsif ($derivative==-1) {
1284: $integral+=$Apache::functionplotresponse::func[$i]*$binwidth;
1285: $val=$integral;
1.25 www 1286: } else {
1.27 www 1287: $val=$Apache::functionplotresponse::func[$i];
1.25 www 1288: }
1.31 www 1289: unless (&compare_rel($relationship,$value,$val,$tol)) {
1.37 www 1290: &addlog("Actual value ".(defined($val)?$val:'undef').", expected $value, tolerance $tol");
1.35 www 1291: &addlog("Condition not fulfilled at x=".&actualval($i,$xmin,$xmax)." (".$Apache::functionplotresponse::actualxval[$i]."; index $i)");
1.31 www 1292: if (($findupper) && ($i>$li)) {
1.49 www 1293: # Check lengths
1294: unless (&checklength($i,$li,$minimumlength,$maximumlength,$xmin,$xmax,$label)) { return 0; }
1295: # Successfully found a new label, set it
1.31 www 1296: $Apache::functionplotresponse::functionplotrulelabels{$xfinallabel}=$i;
1.35 www 1297: &addlog("Rule $label passed, setting label $xfinallabel");
1.31 www 1298: return 1;
1299: } else {
1.35 www 1300: &addlog("Rule $label failed.");
1.49 www 1301: &setfailed($label);
1.31 www 1302: return 0;
1303: }
1304: }
1.42 www 1305: }
1.25 www 1306: }
1.49 www 1307: # Corner case where this makes sense: using start or stop as defined labels
1308: unless (&checklength($lh,$li,$minimumlength,$maximumlength,$xmin,$xmax,$label)) { return 0; }
1.35 www 1309: &addlog("Rule $label passed.");
1.27 www 1310: return 1;
1.24 www 1311: }
1312:
1.49 www 1313: #
1314: # check for minimum and maximum lengths
1315: #
1316:
1317: sub checklength {
1318: my ($i,$li,$minimumlength,$maximumlength,$xmin,$xmax,$label)=@_;
1319: unless (($minimumlength) || ($maximumlength)) { return 1; }
1320: my $length=&actualval($i,$xmin,$xmax)-&actualval($li,$xmin,$xmax);
1321: if ($minimumlength) {
1322: if ($length<$minimumlength) {
1323: &addlog("Rule $label failed, actual length $length, minimum length $minimumlength");
1324: &setfailed($label);
1325: return 0;
1326: }
1327: }
1328: if ($maximumlength) {
1329: if ($length>$maximumlength) {
1330: &addlog("Rule $label failed, actual length $length, maximum length $maximumlength");
1331: &setfailed($label);
1332: return 0;
1333: }
1334: }
1335: return 1;
1336: }
1337:
1338: sub setfailed {
1339: my ($label)=@_;
1340: my $hintlabel=$label;
1341: $hintlabel=~s/^R//;
1342: push(@Apache::functionplotresponse::failedrules,$hintlabel);
1343: &addlog("Set hint condition $hintlabel");
1344: }
1345:
1.37 www 1346: sub start_functionplotruleset {
1347: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1348: if ($target eq 'edit') {
1349: return &Apache::edit::start_table($token).
1350: '<tr><td><span class="LC_nobreak">'.&mt('Function Plot Rule Set').'</span></td>'
1351: .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
1352: .&Apache::edit::deletelist($target,$token).' '.
1.50 www 1353: &Apache::edit::insertlist($target,$token).' '
1354: .&Apache::loncommon::help_open_topic('Function_Plot_Response_Rule_Set','Function Plot Rules')
1.37 www 1355: .'</span></td>'
1356: ."<td> "
1357: .&Apache::edit::end_row()
1358: .&Apache::edit::start_spanning_row()
1359: ."\n";
1360: }
1361: }
1.3 www 1362:
1.37 www 1363: sub end_functionplotruleset {
1364: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1365: my $id=$Apache::inputtags::response[-1];
1366: my $partid=$Apache::inputtags::part;
1367: my $internalid = $partid.'_'.$id;
1368:
1369: if ($target eq 'edit' ) {
1370: return &Apache::edit::end_table();
1371: } elsif ($target eq 'grade'
1.10 www 1372: && &Apache::response::submitted()
1373: && $Apache::lonhomework::type ne 'exam') {
1.12 www 1374: #
1375: # Actually grade
1376: #
1.41 www 1377: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-2);
1.27 www 1378:
1.24 www 1379: my $ad='';
1.30 www 1380: undef %Apache::functionplotresponse::functionplotrulelabels;
1.35 www 1381: $Apache::functionplotresponse::ruleslog='';
1.30 www 1382: $Apache::functionplotresponse::functionplotrulelabels{'start'}=400;
1383: $Apache::functionplotresponse::functionplotrulelabels{'end'}=0;
1.64 www 1384: if (&populate_arrays($internalid,$xmin,$xmax,$ymin,$ymax) eq 'no_func') {
1.21 www 1385: $ad='NOT_FUNCTION';
1.18 www 1386: } else {
1.35 www 1387: &addlog("Start of function ".&actualval($Apache::functionplotresponse::functionplotrulelabels{'start'},$xmin,$xmax)." (index ".
1388: $Apache::functionplotresponse::functionplotrulelabels{'start'}.")");
1389: &addlog("End of function ".&actualval($Apache::functionplotresponse::functionplotrulelabels{'end'},$xmin,$xmax)." (index ".
1390: $Apache::functionplotresponse::functionplotrulelabels{'end'}.")");
1391:
1.24 www 1392: # We have a function that we can actually grade, go through the spline rules.
1.25 www 1393: foreach my $rule (@Apache::functionplotresponse::functionplotrules) {
1.65 www 1394: unless (&functionplotrulecheck($rule,$xmin,$xmax,$ymin,$ymax,$safeeval)) {
1.24 www 1395: $ad='INCORRECT';
1396: last;
1397: }
1398: }
1399: # If it's not wrong, it's correct
1400: unless ($ad) { $ad='EXACT_ANS' };
1.18 www 1401: }
1.37 www 1402: &addlog("Set hint conditions: ".join(",",@Apache::functionplotresponse::failedrules));
1403: &addlog("Assigned award detail: $ad");
1404: # Store for later to be assigned at end_functionplotresponse
1405: $Apache::functionplotresponse::awarddetail=$ad;
1406: }
1407: }
1408:
1409:
1410: sub end_functionplotresponse {
1411: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1412: &Apache::response::end_response;
1413:
1414: my $result;
1415: my $id=$Apache::inputtags::response[-1];
1416: my $partid=$Apache::inputtags::part;
1417: my $internalid = $partid.'_'.$id;
1.20 www 1418:
1.37 www 1419: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
1420: if ($target eq 'grade'
1421: && &Apache::response::submitted()
1422: && $Apache::lonhomework::type eq 'exam') {
1423:
1424: &Apache::response::scored_response($partid,$id);
1425:
1426: } elsif ($target eq 'grade'
1427: && &Apache::response::submitted()
1428: && $Apache::lonhomework::type ne 'exam') {
1429: my ($response,%coords)=&get_answer_from_form_fields($internalid);
1430: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
1431: my %previous=&Apache::response::check_for_previous($response,$partid,$id);
1432: #
1433: # Assign grade
1434: #
1435: my $ad=$Apache::functionplotresponse::awarddetail;
1.12 www 1436: #
1437: # Store grading info
1438: #
1439: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
1440: &Apache::response::handle_previous(\%previous,$ad);
1.10 www 1441: } elsif ($target eq 'web') {
1.37 www 1442: undef @Apache::functionplotresponse::failedrules;
1443: }
1444: return $result;
1445: }
1446:
1447: sub end_functionplotelements {
1448: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1449: my $result='';
1450: my $id=$Apache::inputtags::response[-1];
1451: my $partid=$Apache::inputtags::part;
1452: my $internalid = $partid.'_'.$id;
1453: if ($target eq 'edit' ) {
1454: $result=&Apache::edit::end_table();
1455: } elsif ($target eq 'web') {
1.47 www 1456: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-2);
1457:
1458: # Are we in show answer mode?
1459: my $showanswer=&Apache::response::show_answer();
1460: if ($showanswer) {
1461: # Render answerdisplay
1462: my $answerdisplay=&Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval,-2);
1463: if ($answerdisplay=~/\S/s) {
1464: foreach my $plot (split(/\s*\,\s*/,$answerdisplay)) {
1.58 www 1465: my @components=split(/\s*\:\s*/,$plot);
1466: if ($#components<3) {
1467: # Just a simple plot
1468: my ($func,$xl,$xh)=@components;
1469: if ((!defined($xl)) || ($xl eq '')) { $xl=$xmin; }
1470: if ((!defined($xh)) || ($xh eq '')) { $xh=$xmax; }
1471: $result.=&plot_script($internalid,$func,1,'','00aa00',$xl,$xh,6);
1472: } else {
1473: # This is a spline
1.59 www 1474: $result.=&answer_spline_script($internalid,@components);
1.58 www 1475: }
1.47 www 1476: }
1477: }
1478: }
1.48 www 1479: my $fixed=0;
1480: if (($showanswer) || (&Apache::response::check_status()>=2)) { $fixed=1; }
1.6 www 1481: # Now is the time to render all of the stored splines
1482: foreach my $label (keys(%Apache::functionplotresponse::splineorder)) {
1.48 www 1483: $result.=&generate_spline($internalid,$label,$xmin,$xmax,$ymin,$ymax,$fixed);
1.6 www 1484: }
1.5 www 1485: # close the init script
1.3 www 1486: $result.=&end_init_script();
1.17 www 1487: # register all splines in this response
1488: $result.='<input type="hidden" name="HWVAL_AllSplines_'.$internalid.'" value="'.
1489: join(',',keys(%Apache::functionplotresponse::splineorder)).'" />'."\n";
1490: foreach my $label (keys(%Apache::functionplotresponse::splineorder)) {
1491: $result.='<input type="hidden" name="HWVAL_SplineOrder_'.$internalid.'_'.$label.'" value="'.
1492: $Apache::functionplotresponse::splineorder{$label}.'" />'."\n";
1493: }
1.9 www 1494: # generate the input fields
1495: $result.=$Apache::functionplotresponse::inputfields;
1.5 www 1496: # actually start the <applet>-tag
1.3 www 1497: $result.=&geogebra_startcode($internalid);
1.5 www 1498: # load the spline bytecode
1.3 www 1499: $result.=&geogebra_spline_program();
1.5 www 1500: # set default parameters
1.3 www 1501: $result.=&geogebra_default_parameters($internalid);
1.5 www 1502: # close the <applet>-tag
1.1 www 1503: $result.=&geogebra_endcode();
1504: }
1505: return $result;
1506: }
1507:
1.41 www 1508: sub boundaries {
1509: my ($parstack,$safeeval,$level)=@_;
1510: my $xmin=&Apache::lonxml::get_param('xmin',$parstack,$safeeval,$level);
1511: $xmin=(defined($xmin)?$xmin:-10);
1512: my $xmax=&Apache::lonxml::get_param('xmax',$parstack,$safeeval,$level);
1513: $xmax=(defined($xmax)?$xmax:10);
1514: my $ymin=&Apache::lonxml::get_param('ymin',$parstack,$safeeval,$level);
1515: $ymin=(defined($ymin)?$ymin:-10);
1516: my $ymax=&Apache::lonxml::get_param('ymax',$parstack,$safeeval,$level);
1517: $ymax=(defined($ymax)?$ymax:10);
1518: if ($xmax<=$xmin) {
1519: $xmax=$xmin+20;
1520: }
1521: if ($ymax<=$ymin) {
1522: $ymax=$ymin+20;
1523: }
1524: return ($xmin,$xmax,$ymin,$ymax);
1525: }
1526:
1.37 www 1527: sub start_functionplotelements {
1528: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1529: my $result='';
1530: my $id=$Apache::inputtags::response[-1];
1531: my $partid=$Apache::inputtags::part;
1532: my $internalid = $partid.'_'.$id;
1533:
1534: if ($target eq 'edit') {
1535: return &Apache::edit::start_table($token).
1536: '<tr><td><span class="LC_nobreak">'.&mt('Function Plot Elements').'</span></td>'
1537: .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
1538: .&Apache::edit::deletelist($target,$token).' '.
1.50 www 1539: &Apache::edit::insertlist($target,$token).' '
1540: .&Apache::loncommon::help_open_topic('Function_Plot_Response_Elements','Function Plot Elements')
1.37 www 1541: .'</span></td>'
1542: ."<td> "
1543: .&Apache::edit::end_row()
1544: .&Apache::edit::start_spanning_row()
1545: ."\n";
1546: } elsif ($target eq 'web') {
1.41 www 1547: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-2);
1.37 www 1548: my $xaxisvisible=(&Apache::lonxml::get_param('xaxisvisible',$parstack,$safeeval,-2)=~/on|true|yes|1/i?'true':'false');
1549: my $yaxisvisible=(&Apache::lonxml::get_param('yaxisvisible',$parstack,$safeeval,-2)=~/on|true|yes|1/i?'true':'false');
1550: my $gridvisible=(&Apache::lonxml::get_param('gridvisible',$parstack,$safeeval,-2)=~/on|true|yes|1/i?'true':'false');
1551: my $xlabel=&Apache::lonxml::get_param('xlabel',$parstack,$safeeval,-2);
1552: my $ylabel=&Apache::lonxml::get_param('ylabel',$parstack,$safeeval,-2);
1553:
1554:
1555: # paste in the update routine to receive stuff back from the applet
1556: $result.=&update_script($internalid);
1557: # start the initscript for this applet
1558: $result.=&start_init_script($internalid);
1559: # put the axis commands inside
1560: $result.=&axes_script($internalid,$xmin,$xmax,$ymin,$ymax,$xaxisvisible,$yaxisvisible,$gridvisible);
1.45 www 1561: $result.=&axes_label($internalid,$xmin,$xmax,$ymin,$ymax,$xlabel,$ylabel);
1.37 www 1562: # init script is left open
1563: }
1564: return $result;
1565: }
1566:
1.1 www 1567: 1;
1568:
1569: __END__
1570:
1571: =head1 NAME
1572:
1573: Apache::functionplotresponse.pm;
1574:
1575: =head1 SYNOPSIS
1576:
1577: Handles tags associated with accepting function plots.
1578:
1579: This is part of the LearningOnline Network with CAPA project
1580: described at http://www.lon-capa.org.
1581:
1582: =head1 HANDLER SUBROUTINE
1583:
1584: start_functionplotresponse()
1585:
1586: =head1 OTHER SUBROUTINES
1587:
1588: =over
1589:
1590: =item end_functionplotresponse()
1591:
1592: =back
1593:
1594: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>