Annotation of loncom/homework/functionplotresponse.pm, revision 1.87
1.1 www 1: # LearningOnline Network with CAPA
1.70 www 2: # Functionplot responses
1.1 www 3: #
1.87 ! www 4: # $Id: functionplotresponse.pm,v 1.86 2011/11/24 13:43:21 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.85 www 35: use LONCAPA;
1.36 www 36:
1.1 www 37: BEGIN {
1.37 www 38: &Apache::lonxml::register('Apache::functionplotresponse',('functionplotresponse','backgroundplot','spline',
1.85 www 39: 'plotobject','plotvector',
40: 'functionplotvectorrule','functionplotvectorsumrule',
41: 'functionplotcustomrule',
1.37 www 42: 'functionplotrule','functionplotruleset',
43: 'functionplotelements'));
1.1 www 44: }
45:
1.5 www 46: #
47: # There can be a number of applets on a page, each called ggbApplet_$id,
48: # where $id is the "_"-concatenated part and responseid
49: #
50:
1.1 www 51: sub geogebra_startcode {
1.3 www 52: my ($id)=@_;
1.1 www 53: return (<<ENDSTARTCODE);
1.3 www 54: <applet name="ggbApplet_$id" code="geogebra.GeoGebraApplet" archive="geogebra.jar"
1.40 www 55: codebase="/adm/geogebra/" width="722" height="447" MAYSCRIPT>
1.1 www 56: <param name="java_arguments" value="-Xmx512m -Djnlp.packEnabled=true"/>
57: ENDSTARTCODE
58: }
59:
60: sub geogebra_endcode {
1.2 www 61: return &Apache::lonhtmlcommon::java_not_enabled()."</applet>\n";
62: }
63:
1.5 www 64: #
65: # This is the internal GeoGebra bytecode which defines the spline functions
66: #
1.2 www 67: sub geogebra_spline_program {
68: return (<<ENDSPLINEPROGRAM);
1.57 www 69: <param name="ggbBase64" value="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"/>
1.2 www 70: ENDSPLINEPROGRAM
71: }
72:
1.5 www 73: #
74: # The standard set of parameters inside <applet>
75: #
1.2 www 76: sub geogebra_default_parameters {
1.3 www 77: my ($id)=@_;
1.2 www 78: return(<<ENDDEFAULTPARAMETERS);
79: <param name="image" value="/adm/lonIcons/lonanim.gif" />
80: <param name="boxborder" value="false" />
81: <param name="centerimage" value="true" />
1.62 www 82: <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 83: <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 84: <param name="framePossible" value="false" />
85:
86: <param name="showResetIcon" value="false" />
87: <param name="showAnimationButton" value="false" />
88: <param name="enableRightClick" value="false" />
89: <param name="errorDialogsActive" value="true" />
90: <param name="enableLabelDrags" value="false" />
91: <param name="showMenuBar" value="false" />
92: <param name="showToolBar" value="false" />
93: <param name="showToolBarHelp" value="false" />
94: <param name="showAlgebraInput" value="false" />
95: <param name="enableShiftDragZoom" value="false" />
96: <param name="allowRescaling" value="false" />
97: <param name="enableLabelDrags" value="false" />
1.3 www 98: <param name="ggbOnInitParam" value="applet_$id" />
1.2 www 99: ENDDEFAULTPARAMETERS
1.1 www 100: }
101:
1.5 www 102: #
103: # This subroutine is called by LON-CAPA at </problem>
104: # Each applet on the page will call function ggbOnInit when it is done loading
105: # This function in turn will call the respective function registered by start_init_script
106: # Which one of the registered functions is called is determined by ggbOnInitParam, which GeoGebra passes to ggbOnInit
107: #
108:
1.3 www 109: sub init_script {
1.16 www 110: if ($#Apache::functionplotresponse::callscripts>=0) {
111: my $script='';
112: foreach my $id (@Apache::functionplotresponse::callscripts) {
113: $script.="if (param=='applet_$id') { loaded_$id=true; }\n";
114: }
115: $script.="if (".join(' && ',map { "loaded_$_" } (@Apache::functionplotresponse::callscripts)).
1.52 www 116: ") { setTimeout('ggbInitAll()',200) }";
1.16 www 117: my $calls=join("\n",map { "ggbInit_$_();" } (@Apache::functionplotresponse::callscripts));
1.3 www 118: return (<<ENDGGBINIT);
119: <script type="text/javascript">
120: // <![CDATA[
1.16 www 121: // Function that each applet will call when loaded
122: // It will pass "its" parameter
123: // Set flags for when an applet is loaded, wait till all are loaded, and then some
1.3 www 124: function ggbOnInit(param) {
1.16 www 125: $script
126: }
127: function ggbInitAll() {
1.17 www 128: $calls
1.3 www 129: }
130: // ]]>
131: </script>
132: ENDGGBINIT
133: }
134: }
135:
1.5 www 136: #
137: # Each Geogebra applet is supposed to call this when parameters change
1.10 www 138: # Changes the hidden fields on the web page
1.5 www 139: #
140: sub update_script {
141: my ($id)=@_;
142: return (<<ENDUPDATESCRIPT);
143: <script type="text/javascript">
144: // <![CDATA[
145: function updatePointCoordinates_$id(coordinateName) {
146: var x = document.ggbApplet_$id.getXcoord(coordinateName);
147: var y = document.ggbApplet_$id.getYcoord(coordinateName);
1.9 www 148: document.lonhomework.elements["HWVAL_$id\_" + coordinateName + "_x"].value = x;
149: document.lonhomework.elements["HWVAL_$id\_" + coordinateName + "_y"].value = y;
1.5 www 150: }
151: // ]]>
152: </script>
153: ENDUPDATESCRIPT
154: }
155:
156: #
157: # Register the above update-handler for a variable
158: #
159:
160: sub update_register {
161: my ($id,$variable)=@_;
1.6 www 162: return "document.ggbApplet_$id.registerObjectUpdateListener('$variable','updatePointCoordinates_$id');\n";
163: }
164:
165: #
1.40 www 166: # Set a point coordinate variable
1.6 www 167: #
1.40 www 168: sub set_point_coordinate {
1.48 www 169: my ($id,$variable,$x,$y,$fixed)=@_;
170: my $mult=($fixed?'a*':'');
1.68 www 171: # Get rid of wild exponents, make sure it's a number
172: $x=1.*$x;
173: $y=1.*$y;
174: # GeoGebra does not understand "E"
175: $x=~s/[e|E]/\*10\^/;
176: $x=~s/\+//;
177: $y=~s/[e|E]/\*10\^/;
178: $y=~s/\+//;
1.6 www 179: return (<<ENDSETVARIABLE);
1.48 www 180: document.ggbApplet_$id.evalCommand("a=1");
181: document.ggbApplet_$id.evalCommand("$variable=$mult($x,$y)");
1.40 www 182: document.ggbApplet_$id.setLabelVisible("$variable",false);
1.6 www 183: ENDSETVARIABLE
184: }
185:
186: #
1.40 www 187: # Set a slope coordinate variable
188: #
189: sub set_slope_coordinate {
1.48 www 190: my ($id,$variable,$xrel,$yrel,$xmin,$xmax,$ymin,$ymax,$pointname,$fixed)=@_;
1.40 www 191: my $xvariable=$variable.'x';
192: my $yvariable=$variable.'y';
193: my $domain=$xmax-$xmin;
194: my $range=$ymax-$ymin;
195: my $xinterval=$domain/100.;
196: my $yinterval=$range/200.;
1.48 www 197: my $mult=($fixed?'a*':'');
1.40 www 198: return (<<ENDSETSVARIABLE);
1.48 www 199: document.ggbApplet_$id.evalCommand("a=1");
1.51 www 200: document.ggbApplet_$id.evalCommand("$xvariable=Slider[$xinterval,$domain,$xinterval]");
1.40 www 201: document.ggbApplet_$id.setVisible("$xvariable", false);
202: document.ggbApplet_$id.evalCommand("$xvariable=$xrel");
203: document.ggbApplet_$id.evalCommand("$yvariable=Slider[-$range,$range,$yinterval]");
204: document.ggbApplet_$id.setVisible("$yvariable", false);
205: document.ggbApplet_$id.evalCommand("$yvariable=$yrel");
1.48 www 206: document.ggbApplet_$id.evalCommand("$variable=$mult($xvariable+x($pointname),$yvariable+y($pointname))");
1.40 www 207: document.ggbApplet_$id.setLabelVisible("$variable", false);
208: ENDSETSVARIABLE
209: }
210:
211: #
1.9 www 212: # Input field name for a coordinate variable
213: #
214:
215: sub field_name {
216: my ($id,$variable,$name)=@_;
217: return "HWVAL_$id\_$variable\_$name";
218: }
219:
220: #
221: # Generate an input field for a coordinate variable
222: #
223:
224: sub generate_input_field {
225: my ($id,$variable,$x,$y)=@_;
226: $Apache::functionplotresponse::inputfields.=
227: "<input type='hidden' name='".&field_name($id,$variable,'x')."' value='$x' />\n".
228: "<input type='hidden' name='".&field_name($id,$variable,'y')."' value='$y' />\n";
229: }
230:
231: #
1.40 www 232: # Initialize a new point coordinate variable at set a listener on it
1.6 www 233: #
1.40 www 234: sub new_point_coordinate {
1.48 www 235: my ($id,$variable,$x,$y,$fixed)=@_;
1.11 www 236: if (defined($Apache::functionplotresponse::previous{&field_name($id,$variable,'x')})) {
1.10 www 237: $x=$Apache::functionplotresponse::previous{&field_name($id,$variable,'x')};
238: }
1.11 www 239: if (defined($Apache::functionplotresponse::previous{&field_name($id,$variable,'y')})) {
1.10 www 240: $y=$Apache::functionplotresponse::previous{&field_name($id,$variable,'y')};
241: }
1.9 www 242: &generate_input_field($id,$variable,$x,$y);
1.48 www 243: return &set_point_coordinate($id,$variable,$x,$y,$fixed).&update_register($id,$variable);
1.40 www 244: }
245:
246: #
247: # Initialize a new slope coordinate variable at set a listener on it
248: #
249: sub new_slope_coordinate {
1.48 www 250: my ($id,$variable,$x,$y,$pointname,$xp,$yp,$xmin,$xmax,$ymin,$ymax,$fixed)=@_;
1.40 www 251: #
252: # $variable: name of the slope point
253: # $x, $y: coordinates of the slope point
254: # $pointname: name of the associated point point
255: # $xp $yp: coordinates of the point point
256: #
257: if (defined($Apache::functionplotresponse::previous{&field_name($id,$variable,'x')})) {
258: $x=$Apache::functionplotresponse::previous{&field_name($id,$variable,'x')};
259: }
260: if (defined($Apache::functionplotresponse::previous{&field_name($id,$variable,'y')})) {
261: $y=$Apache::functionplotresponse::previous{&field_name($id,$variable,'y')};
262: }
263: if (defined($Apache::functionplotresponse::previous{&field_name($id,$pointname,'x')})) {
264: $xp=$Apache::functionplotresponse::previous{&field_name($id,$pointname,'x')};
265: }
266: if (defined($Apache::functionplotresponse::previous{&field_name($id,$pointname,'y')})) {
267: $yp=$Apache::functionplotresponse::previous{&field_name($id,$pointname,'y')};
268: }
269:
270: &generate_input_field($id,$variable,$x,$y);
271: my $xrel=$x-$xp;
272: my $yrel=$y-$yp;
1.48 www 273: return &set_slope_coordinate($id,$variable,$xrel,$yrel,$xmin,$xmax,$ymin,$ymax,$pointname,$fixed).&update_register($id,$variable);
1.5 www 274: }
275:
276: #
277: # This registers the init-function call for ggbOnInit, which LON-CAPA will place at </problem>
278: # It then starts the right headers
279: #
1.3 www 280: sub start_init_script {
281: my ($id)=@_;
1.16 www 282: # Add id to the list of ggbInit_$id functions that need to be called
283: push(@Apache::functionplotresponse::callscripts,$id);
1.5 www 284: # ... and open this function
1.3 www 285: return (<<ENDSTARTINIT);
286: <script type="text/javascript">
287: // <![CDATA[
1.5 www 288: // variable that will eventually be passed back to the server
289: var coordinateMap_$id = [];
1.16 www 290: // flag for not loaded yet
291: var loaded_$id=false;
1.5 www 292: // Init-function for applet
1.3 www 293: function ggbInit_$id() {
294: ENDSTARTINIT
295: }
296:
1.5 www 297: #
298: # This sets the axes inside ggbInit_$id
299: #
300:
1.4 www 301: sub axes_script {
302: my ($id,$xmin,$xmax,$ymin,$ymax,$xvisible,$yvisible,$gvisible)=@_;
303: return (<<ENDAXESSCRIPT);
304: // changes (xmin, xmax, ymin, ymax)
305: document.ggbApplet_$id.setCoordSystem($xmin,$xmax,$ymin,$ymax);
306:
307: // makes the (x,y) axis (in)visible
308: document.ggbApplet_$id.setAxesVisible($xvisible,$yvisible);
309: // makes the grid (in)visible
310: document.ggbApplet_$id.setGridVisible($gvisible);
311: ENDAXESSCRIPT
312: }
313:
1.14 www 314: sub axes_label {
1.45 www 315: my ($id,$xmin,$xmax,$ymin,$ymax,$xlabel,$ylabel)=@_;
1.14 www 316: unless ($xlabel || $ylabel) { return ''; }
317: my $return='document.ggbApplet_'.$id.'.evalCommand("topRight=Corner[3]");';
318: if ($xlabel) {
1.45 www 319: if (($ymin<0) && ($ymax>0)) {
1.14 www 320: $return.=(<<ENDXAXISLABELSCRIPT);
321: document.ggbApplet_$id.evalCommand("Xlabel=(x(topRight)-AxisStepX[],AxisStepY[]/6)");
322: document.ggbApplet_$id.setVisible("Xlabel",false);
323: document.ggbApplet_$id.evalCommand("Text[\\"$xlabel\\", Xlabel]");
324: ENDXAXISLABELSCRIPT
1.45 www 325: } else {
326: $return.=(<<ENDXOFFAXISLABEL);
327: document.ggbApplet_$id.evalCommand("LowerRight=Corner[2]");
328: document.ggbApplet_$id.evalCommand("Text[\\"$xlabel\\", (x(LowerRight) - AxisStepX[], y(LowerRight) + AxisStepY[] / 2)]");
329: ENDXOFFAXISLABEL
330: }
1.14 www 331: }
332: if ($ylabel) {
1.45 www 333: if (($xmin<0) && ($xmax>0)) {
1.14 www 334: $return.=(<<ENDYAXISLABELSCRIPT);
335: document.ggbApplet_$id.evalCommand("Ylabel=(AxisStepX[]/6,y(topRight)-AxisStepY[]/3)");
336: document.ggbApplet_$id.setVisible("Ylabel",false);
337: document.ggbApplet_$id.evalCommand("Text[\\"$ylabel\\", Ylabel]");
338: ENDYAXISLABELSCRIPT
1.45 www 339: } else {
340: $return.=(<<ENDYOFFAXISLABEL);
341: document.ggbApplet_$id.evalCommand("UpperLeft=Corner[4]");
342: document.ggbApplet_$id.evalCommand("Text[\\"$ylabel\\", (x(UpperLeft) + AxisStepX[] / 5, y(UpperLeft) - AxisStepY[] / 1.8)]");
343: ENDYOFFAXISLABEL
344: }
1.14 www 345: }
346: return $return;
347: }
348:
1.58 www 349: #
350: # Subroutine to produce background and answer plots
351: #
352:
1.4 www 353: sub plot_script {
1.47 www 354: my ($id,$function,$fixed,$label,$color,$xmin,$xmax,$thickness)=@_;
1.41 www 355: $label=~s/\W//g;
356: if (($label) && ($label!~/^[A-Za-z]/)) {
357: $label='C'.$label;
358: }
359: my $visible=0;
360: if ($label) {
361: $visible="1";
362: } else {
363: $Apache::functionplotresponse::counter++;
364: $label='C'.$Apache::functionplotresponse::counter;
365: }
1.46 www 366: my $rc=0;
367: my $gc=0;
368: my $bc=0;
369: if ($color) {
370: my ($rh,$gh,$bh)=($color=~/(..)(..)(..)/);
371: $rc=hex($rh);
372: $gc=hex($gh);
373: $bc=hex($bh);
374: }
1.6 www 375: if ($fixed) {
1.43 www 376: return "document.ggbApplet_$id.evalCommand('$label=Function[$function,$xmin,$xmax]');\n".
1.46 www 377: ($visible?'':"document.ggbApplet_$id.setLabelVisible('$label', false);\n").
1.47 www 378: ($color?"document.ggbApplet_$id.setColor('$label',$rc,$gc,$bc);\n":'').
379: ($thickness?"document.ggbApplet_$id.setLineThickness('$label',$thickness);\n":'');
1.6 www 380: } else {
1.46 www 381: return "document.ggbApplet_$id.evalCommand('y=$function');\n";
1.6 www 382: }
1.4 www 383: }
384:
1.5 www 385: #
1.70 www 386: # Subroutine to produce objects
387: #
388:
389: sub plotobject_script {
390: my ($id,$label,$x,$y)=@_;
391: unless ($label) {
392: $Apache::functionplotresponse::counter++;
393: $label='O'.$Apache::functionplotresponse::counter;
394: }
1.77 www 395: &generate_input_field($id,$label,$x,$y);
1.70 www 396: return "document.ggbApplet_$id.evalCommand('a=1');\n".
397: "document.ggbApplet_$id.setVisible('a', false);\n".
398: "document.ggbApplet_$id.setLabelVisible('a', false);\n".
399: "document.ggbApplet_$id.evalCommand('$label=a*($x,$y)');\n".
400: "document.ggbApplet_$id.setVisible('$label', true);\n".
401: "document.ggbApplet_$id.setLabelVisible('$label', true);\n";
402: }
403:
404: #
405: # Subroutine to produce vectors
406: #
407:
408: sub plotvector_script {
1.72 www 409: my ($id,$label,$xs,$ys,$xe,$ye,$xmin,$xmax)=@_;
1.70 www 410: unless ($label) {
411: $Apache::functionplotresponse::counter++;
412: $label='V'.$Apache::functionplotresponse::counter;
413: }
1.71 www 414: my $startlabel=$label.'Start';
415: my $endlabel=$label.'End';
416: my $pointlabel=$label.'Point';
1.72 www 417: my $pointx=2.*($xmax-$xmin)+$xmax;
1.71 www 418: my $anglelabel=$label.'Angle';
1.74 www 419: return
420: &new_point_coordinate($id,$startlabel,$xs,$ys,0).
421: &new_point_coordinate($id,$endlabel,$xe,$ye,0).
422: (<<ENDVECTOR);
1.71 www 423: document.ggbApplet_$id.evalCommand("$label=Vector[$startlabel,$endlabel]");
424: document.ggbApplet_$id.setLabelVisible("$label",true);
425: document.ggbApplet_$id.setLineThickness("$label",8);
1.72 www 426: document.ggbApplet_$id.evalCommand("$pointlabel=($pointx,y($startlabel))");
427: document.ggbApplet_$id.evalCommand("$anglelabel=Angle[$pointlabel,$startlabel,$endlabel]");
428: document.ggbApplet_$id.setLabelVisible("$anglelabel",true);
429: document.ggbApplet_$id.setLabelStyle("$anglelabel",VALUE=2);
1.70 www 430: ENDVECTOR
431: }
432:
433: #
1.58 www 434: # Answer spline display
435: #
436: # points: x,y,slope_x,slope_y
437:
438: sub answer_spline_script {
439: my ($id,@points)=@_;
440: my $order=int(($#points+1)/4);
441: if ($order<2) { $order=2; }
442: if ($order>8) { $order=8; }
443: $Apache::functionplotresponse::counter++;
1.60 www 444: my $label='CSpline'.$Apache::functionplotresponse::counter;
1.59 www 445: my $output='document.ggbApplet_'.$id.'.evalCommand("'.$label.'=Spline'.$order.'[';
446: for (my $i=0;$i<=$#points;$i+=4) {
447: $output.="($points[$i],$points[$i+1]),($points[$i+2],$points[$i+3]),";
448: }
449: $output=~s/\,$//;
450: $output.=']");'."\n";
451: for (my $i=2; $i<2*$order; $i+=2) {
1.61 www 452: $output.='document.ggbApplet_'.$id.'.setColor("'.$label.'_'.($i>=10?'{':'').$i.($i>=10?'}':'').'",0,170,0);'."\n";
1.59 www 453: }
1.60 www 454: for (my $i=1; $i<2*$order; $i+=2) {
1.61 www 455: $output.='document.ggbApplet_'.$id.'.setVisible("'.$label.'_'.($i>=10?'{':'').$i.($i>=10?'}':'').'",false);'."\n";
1.60 www 456: }
457:
1.59 www 458: return $output;
1.58 www 459: }
460:
461: #
1.7 www 462: # Subroutine that generates code for spline $label based on stored information
1.6 www 463: #
464:
465: sub generate_spline {
1.48 www 466: my ($id,$label,$xmin,$xmax,$ymin,$ymax,$fixed)=@_;
1.6 www 467: my $result='';
1.7 www 468: my $order=$Apache::functionplotresponse::splineorder{$label};
469: my $x=$Apache::functionplotresponse::splineinitx{$label};
470: my $y=$Apache::functionplotresponse::splineinity{$label};
471: my $sx=$Apache::functionplotresponse::splinescalex{$label};
472: my $sy=$Apache::functionplotresponse::splinescaley{$label};
473: my @coords=();
474: foreach my $i (1..$order) {
1.48 www 475: $result.=&new_point_coordinate($id,$label.'P'.$i,$x,$y,$fixed);
1.40 www 476: my $xp=$x;
1.8 www 477: $x+=$sx/(2.*($order-1));
1.7 www 478: push(@coords,$label.'P'.$i);
1.48 www 479: $result.=&new_slope_coordinate($id,$label.'S'.$i,$x,$y+$sy,$label.'P'.$i,$xp,$y,$xmin,$xmax,$ymin,$ymax,$fixed);
1.8 www 480: $x+=$sx/(2.*($order-1));
1.7 www 481: push(@coords,$label.'S'.$i);
482: }
483: $result.='document.ggbApplet_'.$id.'.evalCommand("Spline'.$order.'['.join(',',@coords).']");'."\n";
1.6 www 484: return $result;
485: }
1.70 www 486:
487: #
488: # Object
489: #
490:
491: sub start_plotobject {
492: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
493: my $result='';
494: my $internalid = $Apache::inputtags::part.'_'.$Apache::inputtags::response[-1];
495: my $x=&Apache::lonxml::get_param('x',$parstack,$safeeval);
496: my $y=&Apache::lonxml::get_param('y',$parstack,$safeeval);
497: my $label=&Apache::lonxml::get_param('label',$parstack,$safeeval);
498: $label=~s/\W//gs;
499: $label=ucfirst($label);
500: unless ($label) { $label="NewObject"; }
501: if ($target eq 'web') {
502: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-3);
503: unless (defined($x)) { $x=$xmin; }
504: unless (defined($y)) { $y=$ymin; }
505: $result.=&plotobject_script($internalid,$label,$x,$y);
506: } elsif ($target eq 'edit') {
507: $result=&Apache::edit::tag_start($target,$token,'Plot Object').
508: &Apache::edit::text_arg('Label on Plot:','label',
509: $token,'16').
510: &Apache::edit::text_arg('x:','x',
511: $token,'8').
512: &Apache::edit::text_arg('y:','y',
513: $token,'8').
514: &Apache::edit::end_row();
515: } elsif ($target eq 'modified') {
1.76 www 516: $env{'form.'.&Apache::edit::html_element_name('label')}=ucfirst($env{'form.'.&Apache::edit::html_element_name('label')});
1.70 www 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') {
1.76 www 571: $env{'form.'.&Apache::edit::html_element_name('label')}=ucfirst($env{'form.'.&Apache::edit::html_element_name('label')});
1.71 www 572: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'label','tailx','taily','tipx','tipy');
573: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
574: }
575: return $result;
576: }
577:
578: sub end_plotvector {
579: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
580: my $result='';
581: if ($target eq 'edit') {
582: $result=&Apache::edit::end_table();
583: }
584: return $result;
585: }
586:
587:
1.70 www 588:
1.6 www 589: #
590: # <backgroundplot function="..." fixed="yes/no" />
1.5 www 591: #
1.4 www 592: sub start_backgroundplot {
593: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
594: my $result='';
595: my $internalid = $Apache::inputtags::part.'_'.$Apache::inputtags::response[-1];
596: my $function=&Apache::lonxml::get_param('function',$parstack,$safeeval);
1.47 www 597: my $xinitial=&Apache::lonxml::get_param('xinitial',$parstack,$safeeval);
598: my $xfinal=&Apache::lonxml::get_param('xfinal',$parstack,$safeeval);
1.41 www 599: my $label=&Apache::lonxml::get_param('label',$parstack,$safeeval);
1.46 www 600: my $color=&Apache::lonxml::get_param('color',$parstack,$safeeval);
601: $color=~s/[^a-fA-F0-9]//gs;
602: unless (length($color)==6) { $color=''; }
1.6 www 603: my $fixed=(&Apache::lonxml::get_param('fixed',$parstack,$safeeval)=~/on|true|yes|1/i?1:0);
604:
1.4 www 605: unless ($function) { $function="0"; }
606: if ($target eq 'web') {
1.41 www 607: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-3);
1.47 www 608: unless (defined($xinitial)) { $xinitial=$xmin; }
609: unless (defined($xfinal)) { $xfinal=$xmax; }
610: $result.=&plot_script($internalid,$function,$fixed,$label,$color,$xinitial,$xfinal);
1.13 www 611: } elsif ($target eq 'edit') {
612: $result=&Apache::edit::tag_start($target,$token,'Background Function Plot').
613: &Apache::edit::text_arg('Function:','function',
614: $token,'16').
1.47 www 615: &Apache::edit::text_arg('Initial x-value (optional):','xinitial',
616: $token,'8').
617: &Apache::edit::text_arg('Final x-value (optional):','xfinal',
618: $token,'8').
1.41 www 619: &Apache::edit::text_arg('Label on Plot:','label',
620: $token,'8').
1.46 www 621: &Apache::edit::text_arg('Color (hex code):','color',
622: $token,'8').
1.13 www 623: &Apache::edit::select_arg('Fixed location:','fixed',
624: ['yes','no'],$token).
625: &Apache::edit::end_row();
626: } elsif ($target eq 'modified') {
627: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.47 www 628: $safeeval,'function','label','xinitial','xfinal','color','fixed');
1.13 www 629: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
630: }
631: return $result;
1.4 www 632: }
633:
634: sub end_backgroundplot {
1.13 www 635: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
636: my $result='';
637: if ($target eq 'edit') {
638: $result=&Apache::edit::end_table();
639: }
640: return $result;
1.6 www 641: }
642:
643: #
1.25 www 644: # <functionplotrule ... />
1.12 www 645: #
1.25 www 646: sub start_functionplotrule {
1.12 www 647: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
648: my $result='';
1.24 www 649: my $label=&Apache::lonxml::get_param('index',$parstack,$safeeval);
650: $Apache::functionplotresponse::counter++;
651: if ($label=~/\W/) {
1.25 www 652: &Apache::lonxml::warning(&mt('Rule indices should only contain alphanumeric characters.'));
1.24 www 653: }
654: $label=~s/\W//gs;
655: unless ($label) {
656: $label='R'.$Apache::functionplotresponse::counter;
657: } else {
658: $label='R'.$label;
659: }
660:
661: if ($target eq 'grade') {
662: # Simply remember - in order - for later
1.25 www 663: my $beginninglabel=&Apache::lonxml::get_param('xinitiallabel',$parstack,$safeeval);
664: my $endinglabel=&Apache::lonxml::get_param('xfinallabel',$parstack,$safeeval);
1.24 www 665: if (($beginninglabel=~/\W/) || ($endinglabel=~/W/)) {
1.25 www 666: &Apache::lonxml::warning(&mt('Rule labels must be alphanumeric.'));
1.24 www 667: }
668: $beginninglabel=~s/\W//gs;
669: $endinglabel=~s/\W//gs;
670: my $relationship=&Apache::lonxml::get_param('relationship',$parstack,$safeeval);
671: $relationship=~s/\W//gs;
672: $relationship=lc($relationship);
673: unless ($relationship=~/^(eq|ge|gt|le|lt|ne)$/) {
1.25 www 674: &Apache::lonxml::warning(&mt('Rule relationship not defined.'));
1.24 www 675: $relationship='eq';
676: }
1.35 www 677: my $derivative=&Apache::lonxml::get_param('derivativeorder',$parstack,$safeeval);
1.42 www 678: unless (($derivative==-1) || ($derivative==0) || ($derivative==1) || ($derivative==2)) {
1.25 www 679: &Apache::lonxml::warning(&mt('Rule derivative not defined.'));
1.24 www 680: $derivative=0;
681: }
1.25 www 682: push(@Apache::functionplotresponse::functionplotrules,join(':',(
1.24 www 683: $label,
684: $derivative,
1.25 www 685: &Apache::lonxml::get_param('xinitial',$parstack,$safeeval),
1.24 www 686: $beginninglabel,
1.25 www 687: &Apache::lonxml::get_param('xfinal',$parstack,$safeeval),
1.24 www 688: $endinglabel,
1.35 www 689: &Apache::lonxml::get_param('minimumlength',$parstack,$safeeval),
690: &Apache::lonxml::get_param('maximumlength',$parstack,$safeeval),
1.24 www 691: $relationship,
1.33 www 692: &Apache::lonxml::get_param('value',$parstack,$safeeval),
693: &Apache::lonxml::get_param('percenterror',$parstack,$safeeval)
1.24 www 694: )));
695: } elsif ($target eq 'edit') {
1.75 www 696: $result=&Apache::edit::tag_start($target,$token,'Function Plot Graph Rule').
1.36 www 697: &Apache::edit::text_arg('Index/Name:','index',
698: $token,'10').' '.
1.35 www 699: &Apache::edit::select_arg(&mt('Function:'),'derivativeorder',
1.29 www 700: [['0','Function itself'],
701: ['1','First derivative'],
1.42 www 702: ['2','Second derivative'],
703: ['-1','Integral']],$token).'<br />'.
1.44 www 704: &Apache::edit::text_arg('Initial x-value:','xinitial',
1.35 www 705: $token,'8').
1.44 www 706: &Apache::edit::select_or_text_arg('Initial x-value label:','xinitiallabel',
707: [['start','Start of Plot'],
708: ['end','End of Plot']],$token,'8').'<br />'.
1.30 www 709:
1.44 www 710: &Apache::edit::text_arg('Final x-value (optional):','xfinal',
1.35 www 711: $token,'8').
1.44 www 712: &Apache::edit::select_or_text_arg('Final x-value label (optional):','xfinallabel',
1.32 www 713: [['end','End of Plot']],$token,'8').'<br />'.
1.44 www 714: &Apache::edit::text_arg('Minimum length for range (optional):','minimumlength',
1.35 www 715: $token,'8').
1.44 www 716: &Apache::edit::text_arg('Maximum length for range (optional):','maximumlength',
1.35 www 717: $token,'8').'<br />'.
1.38 www 718: &Apache::edit::select_or_text_arg(&mt('Relationship:'),'relationship',
1.29 www 719: [['eq','equal'],
720: ['ne','not equal'],
721: ['ge','greater than or equal'],
722: ['gt','greater than'],
723: ['lt','less than'],
724: ['le','less than or equal']],$token).
725: $result.= &Apache::edit::select_or_text_arg('Value:','value',
1.65 www 726: [['undef','not defined']],$token,'30').
1.29 www 727: &Apache::edit::text_arg('Percent error:','percenterror',
1.35 www 728: $token,'8').
1.29 www 729: &Apache::edit::end_row();
730: } elsif ($target eq 'modified') {
1.54 www 731: if (($env{'form.'.&Apache::edit::html_element_name('xinitial')} ne '') && ($env{'form.'.&Apache::edit::html_element_name('xinitiallabel')} eq 'start')) {
1.53 www 732: $env{'form.'.&Apache::edit::html_element_name('xinitiallabel')}='';
733: }
1.54 www 734: if (($env{'form.'.&Apache::edit::html_element_name('xfinal')} ne '') && ($env{'form.'.&Apache::edit::html_element_name('xfinallabel')} eq 'end')) {
1.53 www 735: $env{'form.'.&Apache::edit::html_element_name('xfinallabel')}='';
736: }
1.29 www 737: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.35 www 738: $safeeval,'index','derivativeorder',
739: 'xinitial','xinitiallabel','xfinal','xfinallabel',
740: 'minimumlength','maximumlength',
741: 'relationship','value','percenterror');
1.29 www 742: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
1.13 www 743: }
1.12 www 744: return $result;
745: }
746:
1.25 www 747: sub end_functionplotrule {
1.13 www 748: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
749: my $result='';
750: if ($target eq 'edit') {
751: $result=&Apache::edit::end_table();
752: }
753: return $result;
1.12 www 754: }
755:
756:
757: #
1.75 www 758: # <functionplotvectorrule ... />
759: #
760: sub start_functionplotvectorrule {
761: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
762: my $result='';
763: my $label=&Apache::lonxml::get_param('index',$parstack,$safeeval);
764: $Apache::functionplotresponse::counter++;
765: if ($label=~/\W/) {
766: &Apache::lonxml::warning(&mt('Rule indices should only contain alphanumeric characters.'));
767: }
768: $label=~s/\W//gs;
769: unless ($label) {
1.79 www 770: $label='R'.$Apache::functionplotresponse::counter;
1.75 www 771: } else {
1.79 www 772: $label='R'.$label;
1.75 www 773: }
774:
775: if ($target eq 'grade') {
776: # Simply remember - in order - for later
1.77 www 777:
778: my $id=$Apache::inputtags::response[-1];
779: my $partid=$Apache::inputtags::part;
780: my $internalid = $partid.'_'.$id;
781:
1.75 www 782: my $vector=&Apache::lonxml::get_param('vector',$parstack,$safeeval);
783: $vector=~s/\W//gs;
784: $vector=ucfirst($vector);
1.76 www 785:
1.75 www 786: push(@Apache::functionplotresponse::functionplotvectorrules,join(':',(
787: $label,
1.76 www 788: 'vector',
1.77 www 789: $internalid,
1.75 www 790: $vector,
1.87 ! www 791: &Apache::lonxml::get_param('attachpoint',$parstack,$safeeval),
! 792: &Apache::lonxml::get_param('notattachpoint',$parstack,$safeeval),
! 793: &Apache::lonxml::get_param('tailpoint',$parstack,$safeeval),
! 794: &Apache::lonxml::get_param('tippoint',$parstack,$safeeval),
! 795: &Apache::lonxml::get_param('nottailpoint',$parstack,$safeeval),
! 796: &Apache::lonxml::get_param('nottippoint',$parstack,$safeeval),
1.75 www 797: &Apache::lonxml::get_param('length',$parstack,$safeeval),
798: &Apache::lonxml::get_param('angle',$parstack,$safeeval),
1.79 www 799: &Apache::lonxml::get_param('lengtherror',$parstack,$safeeval),
800: &Apache::lonxml::get_param('angleerror',$parstack,$safeeval),
1.75 www 801: )));
802: } elsif ($target eq 'edit') {
803: $result=&Apache::edit::tag_start($target,$token,'Function Plot Vector Rule').
804: &Apache::edit::text_arg('Index/Name:','index',
805: $token,'10').' '.
806: &Apache::edit::text_arg('Vector:','vector',
807: $token,'16').'<br />'.
1.76 www 808: &Apache::edit::text_arg('Attached to object:','attachpoint',
809: $token,'16').
810: &Apache::edit::text_arg('Not attached to object:','notattachpoint',
811: $token,'16').'<br />'.
1.75 www 812: &Apache::edit::text_arg('Tail attached to object:','tailpoint',
813: $token,'16').
814: &Apache::edit::text_arg('Tip attached to object:','tippoint',
815: $token,'16').
816: &Apache::edit::text_arg('Tail not attached to object:','nottailpoint',
817: $token,'16').
818: &Apache::edit::text_arg('Tip not attached to object:','nottippoint',
819: $token,'16').'<br />'.
820: &Apache::edit::text_arg('Length:','length',
1.84 www 821: $token,'30').
822: &Apache::edit::text_arg('Absolute error length:','lengtherror',
823: $token,'8').'<br />'.
1.75 www 824: &Apache::edit::text_arg('Angle:','angle',
1.84 www 825: $token,'30').
1.79 www 826: &Apache::edit::text_arg('Absolute error angle:','angleerror',
1.75 www 827: $token,'8').
828: &Apache::edit::end_row();
829: } elsif ($target eq 'modified') {
1.76 www 830: $env{'form.'.&Apache::edit::html_element_name('vector')}=ucfirst($env{'form.'.&Apache::edit::html_element_name('vector')});
1.75 www 831: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.76 www 832: $safeeval,'index','vector','attachpoint','notattachpoint',
1.75 www 833: 'tailpoint','tippoint','nottailpoint','nottipoint',
834: 'length','angle',
1.79 www 835: 'lengtherror','angleerror');
1.75 www 836: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
837: }
838: return $result;
839: }
840:
841: sub end_functionplotvectorrule {
842: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
843: my $result='';
844: if ($target eq 'edit') {
845: $result=&Apache::edit::end_table();
846: }
847: return $result;
848: }
849:
1.76 www 850: #
851: # <functionplotvectorsumrule ... />
852: #
853: sub start_functionplotvectorsumrule {
854: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
855: my $result='';
856: my $label=&Apache::lonxml::get_param('index',$parstack,$safeeval);
857: $Apache::functionplotresponse::counter++;
858: if ($label=~/\W/) {
859: &Apache::lonxml::warning(&mt('Rule indices should only contain alphanumeric characters.'));
860: }
861: $label=~s/\W//gs;
862: unless ($label) {
1.79 www 863: $label='R'.$Apache::functionplotresponse::counter;
1.76 www 864: } else {
1.79 www 865: $label='R'.$label;
1.76 www 866: }
867: if ($target eq 'grade') {
868: # Simply remember - in order - for later
1.77 www 869: my $id=$Apache::inputtags::response[-1];
870: my $partid=$Apache::inputtags::part;
871: my $internalid = $partid.'_'.$id;
1.82 www 872: my $vectors=&Apache::lonxml::get_param('vectors',$parstack,$safeeval);
1.76 www 873: push(@Apache::functionplotresponse::functionplotvectorrules,join(':',(
874: $label,
875: 'sum',
1.77 www 876: $internalid,
1.82 www 877: $vectors,
1.76 www 878: &Apache::lonxml::get_param('length',$parstack,$safeeval),
879: &Apache::lonxml::get_param('angle',$parstack,$safeeval),
1.80 www 880: &Apache::lonxml::get_param('lengtherror',$parstack,$safeeval),
1.79 www 881: &Apache::lonxml::get_param('angleerror',$parstack,$safeeval),
1.76 www 882: )));
883: } elsif ($target eq 'edit') {
884: $result=&Apache::edit::tag_start($target,$token,'Function Plot Vector Sum Rule').
885: &Apache::edit::text_arg('Index/Name:','index',
886: $token,'10').' '.
1.82 www 887: &Apache::edit::text_arg('Comma-separated list of vectors:','vectors',
888: $token,'30').'<br />'.
1.76 www 889: &Apache::edit::text_arg('Sum vector length:','length',
1.84 www 890: $token,'30').
891: &Apache::edit::text_arg('Absolute error length:','lengtherror',
892: $token,'8').'<br />'.
1.76 www 893: &Apache::edit::text_arg('Sum vector angle:','angle',
1.84 www 894: $token,'30').
895: &Apache::edit::text_arg('Absolute error angle:','angleerror',
1.76 www 896: $token,'8').
897: &Apache::edit::end_row();
1.85 www 898: } elsif ($target eq 'modified') {
899: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
900: $safeeval,'index','vectors',
901: 'length','angle',
902: 'lengtherror','angleerror');
903: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
1.76 www 904: }
905: return $result;
906: }
1.75 www 907:
1.76 www 908: sub end_functionplotvectorsumrule {
909: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
910: my $result='';
911: if ($target eq 'edit') {
912: $result=&Apache::edit::end_table();
913: }
914: return $result;
915: }
1.75 www 916:
917: #
1.85 www 918: # <functionplotcustom ... />
919: #
920: sub start_functionplotcustomrule {
921: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
922: my $result='';
923: my $label=&Apache::lonxml::get_param('index',$parstack,$safeeval);
924: $Apache::functionplotresponse::counter++;
925: if ($label=~/\W/) {
926: &Apache::lonxml::warning(&mt('Rule indices should only contain alphanumeric characters.'));
927: }
928: $label=~s/\W//gs;
929: unless ($label) {
930: $label='R'.$Apache::functionplotresponse::counter;
931: } else {
932: $label='R'.$label;
933: }
934: &Apache::lonxml::register('Apache::response',('answer'));
935: if ($target eq 'edit') {
936: $result=&Apache::edit::tag_start($target,$token,'Function Plot Custom Rule').
937: &Apache::edit::text_arg('Index/Name:','index',$token,'10').
938: &Apache::edit::end_row();
939: } elsif ($target eq 'modified') {
940: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'index');
941: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
942: }
943: return $result;
944: }
945:
946: sub end_functionplotcustomrule {
947: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
948: my $result='';
949: if ($target eq 'edit') {
950: $result=&Apache::edit::end_table();
951: } elsif ($target eq 'grade') {
952: # Simply remember - in order - for later
953: my $label=&Apache::lonxml::get_param('index',$parstack,$safeeval);
954: $Apache::functionplotresponse::counter++;
955: if ($label=~/\W/) {
956: &Apache::lonxml::warning(&mt('Rule indices should only contain alphanumeric characters.'));
957: }
958: $label=~s/\W//gs;
959: unless ($label) {
960: $label='R'.$Apache::functionplotresponse::counter;
961: } else {
962: $label='R'.$label;
963: }
964: push(@Apache::functionplotresponse::functionplotvectorrules,join(':',(
965: $label,
966: 'custom',
967: &escape($Apache::response::custom_answer[-1])
968: )));
969: }
970: &Apache::lonxml::deregister('Apache::response',('answer'));
971: return $result;
972: }
973:
974:
975:
976: #
1.23 www 977: # <spline index="..." order="1,2,3,4" initx="..." inity="..." scalex="..." scaley="..." />
1.6 www 978: #
979: # Unfortunately, GeoGebra seems to want all splines after everything else, so we need to store them
980: #
981: sub start_spline {
982: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.13 www 983: my $result='';
984: if ($target eq 'web') {
1.23 www 985: my $label=&Apache::lonxml::get_param('index',$parstack,$safeeval);
1.13 www 986: $Apache::functionplotresponse::counter++;
987: if ($label=~/\W/) {
1.23 www 988: &Apache::lonxml::warning(&mt('Spline indices should only contain alphanumeric characters.'));
1.13 www 989: }
990: $label=~s/\W//gs;
1.23 www 991: unless ($label) {
992: $label='S'.$Apache::functionplotresponse::counter;
993: } else {
994: $label='S'.$label;
995: }
1.13 www 996: if ($Apache::functionplotresponse::splineorder{$label}) {
1.23 www 997: &Apache::lonxml::error(&mt('Spline indices must be unique.'));
1.13 www 998: }
999:
1000: my $order=&Apache::lonxml::get_param('order',$parstack,$safeeval);
1001: if ($order<2) { $order=2; }
1002: if ($order>8) { $order=8; }
1003: $Apache::functionplotresponse::splineorder{$label}=$order;
1004:
1005: my $x=&Apache::lonxml::get_param('initx',$parstack,$safeeval);
1006: unless ($x) { $x=0; }
1007: $Apache::functionplotresponse::splineinitx{$label}=$x;
1008:
1009: my $y=&Apache::lonxml::get_param('inity',$parstack,$safeeval);
1010: unless ($y) { $y=0; }
1011: $Apache::functionplotresponse::splineinity{$label}=$y;
1012:
1013: my $sx=&Apache::lonxml::get_param('scalex',$parstack,$safeeval);
1014: unless ($sx) { $sx=$order; }
1015: $Apache::functionplotresponse::splinescalex{$label}=$sx;
1016:
1017: my $sy=&Apache::lonxml::get_param('scaley',$parstack,$safeeval);
1018: unless ($sy) { $sy=2; }
1019: $Apache::functionplotresponse::splinescaley{$label}=$sy;
1020: } elsif ($target eq 'edit') {
1021: $result=&Apache::edit::tag_start($target,$token,'Spline').
1.23 www 1022: &Apache::edit::text_arg('Index:','index',
1.14 www 1023: $token,'4').' '.
1.13 www 1024: &Apache::edit::select_arg('Order:','order',
1.44 www 1025: ['2','3','4','5','6','7','8'],$token).' '.
1.13 www 1026: &Apache::edit::text_arg('Initial x-value:','initx',
1.14 www 1027: $token,'4').' '.
1.13 www 1028: &Apache::edit::text_arg('Initial y-value:','inity',
1.14 www 1029: $token,'4').' '.
1.13 www 1030: &Apache::edit::text_arg('Scale x:','scalex',
1.14 www 1031: $token,'4').' '.
1.13 www 1032: &Apache::edit::text_arg('Scale y:','scaley',
1033: $token,'4').
1034: &Apache::edit::end_row();
1035: } elsif ($target eq 'modified') {
1036: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.23 www 1037: $safeeval,'index','order','initx','inity',
1.13 www 1038: 'scalex','scaley');
1039: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
1040: }
1041: return $result;
1.6 www 1042: }
1043:
1044: sub end_spline {
1.13 www 1045: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1046: my $result='';
1047: if ($target eq 'edit') {
1048: $result=&Apache::edit::end_table();
1049: }
1050: return $result;
1.4 www 1051: }
1052:
1.3 www 1053: sub end_init_script {
1054: return (<<ENDENDINIT);
1055: }
1056: // ]]>
1057: </script>
1058: ENDENDINIT
1059: }
1060:
1.10 www 1061: #
1062: # Storing and restoring spline coordinates from part answers
1063: #
1064: sub decode_previous_answer {
1065: my ($answer)=@_;
1066: foreach my $coordinate (split(/\,/,$answer)) {
1067: my ($key,$value)=split(/\=/,$coordinate);
1068: $Apache::functionplotresponse::previous{$key}=$value;
1069: }
1070: }
1071:
1072: sub get_answer_from_form_fields {
1073: my ($id)=@_;
1074: my $answer='';
1075: my %coords=();
1076: foreach my $field (keys(%env)) {
1077: if ($field=~/^form\.HWVAL\_$id/) {
1078: $field=~/^form\.(.*)$/;
1079: $coords{$1}=$env{$field};
1080: }
1081: }
1082: $answer=join(',',map { $_.'='.$coords{$_} } (sort(keys(%coords))));
1083: return ($answer,%coords);
1084: }
1085:
1.15 www 1086: #
1087: # The following functions calculate the cubic-hermite splines server-side
1088: #
1089:
1090: sub cubic_hermite {
1091: my ($t,$p1,$s1,$p2,$s2)=@_;
1.42 www 1092: return (2.*$t*$t*$t-3.*$t*$t+1.)*$p1 + 3.*($t*$t*$t-2.*$t*$t+$t)*($s1-$p1)+
1093: (-2.*$t*$t*$t+3.*$t*$t) *$p2 + 3.*($t*$t*$t-$t*$t) *($s2-$p2);
1.15 www 1094: }
1095:
1096: #
1097: # d/dt(...)
1098: #
1099:
1100: sub ddt_cubic_hermite {
1101: my ($t,$p1,$s1,$p2,$s2)=@_;
1.42 www 1102: return (6.*$t*$t-6.*$t) *$p1 + 3.*(3.*$t*$t-4.*$t+1.)*($s1-$p1)+
1103: (-6.*$t*$t+6.*$t)*$p2 + 3.*(3.*$t*$t-2.*$t) *($s2-$p2);
1.15 www 1104: }
1105:
1106: #
1107: # d^2/dt^2(...)
1108: #
1109:
1110: sub d2dt2_cubic_hermite {
1111: my ($t,$p1,$s1,$p2,$s2)=@_;
1.42 www 1112: return (12.*$t-6.) *$p1 + 3.*(6.*$t-4.)*($s1-$p1)+
1113: (-12.*$t+6.)*$p2 + 3.*(6.*$t-2.)*($s2-$p2);
1.15 www 1114: }
1115:
1116: #
1.18 www 1117: # Array index calculation
1118: #
1119: sub array_index {
1120: my ($xmin,$xmax,$x)=@_;
1.38 www 1121: if ($x ne '') {
1122: return int(($x-$xmin)/($xmax-$xmin)*400.+0.5);
1123: } else {
1124: return undef;
1125: }
1.20 www 1126: }
1127:
1128: #
1.18 www 1129: # Populate the arrays
1130: #
1131:
1132: sub populate_arrays {
1.64 www 1133: my ($id,$xmin,$xmax,$ymin,$ymax)=@_;
1.26 www 1134: for (my $i=0; $i<=400; $i++) {
1.35 www 1135: $Apache::functionplotresponse::actualxval[$i]=undef;
1.19 www 1136: $Apache::functionplotresponse::func[$i]=undef;
1.22 www 1137: $Apache::functionplotresponse::dfuncdx[$i]=undef;
1.19 www 1138: $Apache::functionplotresponse::d2funcd2x[$i]=undef;
1.18 www 1139: }
1.19 www 1140: unless ($xmax>$xmin) { return 'no_func'; }
1.18 www 1141: # Run over all splines in response
1142: foreach my $label (split(/\,/,$env{"form.HWVAL_AllSplines_$id"})) {
1.19 www 1143: my $xiold=-1;
1.18 www 1144: # Run over all points in spline
1.19 www 1145: for (my $i=1; $i<$env{"form.HWVAL_SplineOrder_".$id."_".$label}; $i++) {
1.18 www 1146: my $ni=$i+1;
1147: my @xparms=($env{'form.HWVAL_'.$id.'_'.$label.'P'.$i.'_x'},
1148: $env{'form.HWVAL_'.$id.'_'.$label.'S'.$i.'_x'},
1149: $env{'form.HWVAL_'.$id.'_'.$label.'P'.$ni.'_x'},
1150: $env{'form.HWVAL_'.$id.'_'.$label.'S'.$ni.'_x'});
1151: my @yparms=($env{'form.HWVAL_'.$id.'_'.$label.'P'.$i.'_y'},
1152: $env{'form.HWVAL_'.$id.'_'.$label.'S'.$i.'_y'},
1153: $env{'form.HWVAL_'.$id.'_'.$label.'P'.$ni.'_y'},
1154: $env{'form.HWVAL_'.$id.'_'.$label.'S'.$ni.'_y'});
1155: # Run in small steps over spline parameter
1.26 www 1156: for (my $t=0; $t<=1; $t+=0.0001) {
1.35 www 1157: my $xreal=&cubic_hermite($t,@xparms);
1158: my $xi=&array_index($xmin,$xmax,$xreal);
1.19 www 1159: if ($xi<$xiold) { return 'no_func'; }
1.26 www 1160: if (($xi>$xiold) && ($xi>=0) && ($xi<=400)) {
1.18 www 1161: $xiold=$xi;
1.35 www 1162: $Apache::functionplotresponse::actualxval[$xi]=$xreal;
1.22 www 1163: # Function value
1.30 www 1164: my $funcval=&cubic_hermite($t,@yparms);
1.64 www 1165:
1166: # Do we already have a value for this point, and is it different from the new one?
1167: if ((defined($Apache::functionplotresponse::func[$xi])) &&
1168: (abs($Apache::functionplotresponse::func[$xi]-$funcval)>($ymax-$ymin)/100.)) {
1169: return 'no_func';
1170: }
1171: # Okay, remember the new point
1.30 www 1172: $Apache::functionplotresponse::func[$xi]=$funcval;
1.64 www 1173:
1.30 www 1174: if (defined($funcval)) {
1175: if ($xi<$Apache::functionplotresponse::functionplotrulelabels{'start'}) {
1176: $Apache::functionplotresponse::functionplotrulelabels{'start'}=$xi;
1177: }
1178: if ($xi>$Apache::functionplotresponse::functionplotrulelabels{'end'}) {
1179: $Apache::functionplotresponse::functionplotrulelabels{'end'}=$xi;
1180: }
1181: }
1.28 www 1182: # Chain rule
1.22 www 1183: # dy/dx=dy/dt/(dx/dt)
1184: my $dxdt=&ddt_cubic_hermite($t,@xparms);
1185: if ($dxdt) {
1186: $Apache::functionplotresponse::dfuncdx[$xi]=&ddt_cubic_hermite($t,@yparms)/$dxdt;
1.34 www 1187: # Second derivative
1.28 www 1188: $Apache::functionplotresponse::d2funcdx2[$xi]=
1.34 www 1189: ($dxdt*&d2dt2_cubic_hermite($t,@yparms)-&ddt_cubic_hermite($t,@yparms)*&d2dt2_cubic_hermite($t,@xparms))/
1190: ($dxdt*$dxdt*$dxdt);
1.22 www 1191: }
1.18 www 1192: }
1193: }
1194: }
1195: }
1196: }
1197:
1198: #
1.37 www 1199: # Implementation of <functionplotresponse>
1.15 www 1200: #
1201:
1.1 www 1202: sub start_functionplotresponse {
1203: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1204: my $result='';
1.6 www 1205: # To remember the splines - somehow, they need to come last
1206: undef %Apache::functionplotresponse::splineorder;
1207: undef %Apache::functionplotresponse::splineinitx;
1208: undef %Apache::functionplotresponse::splineinity;
1209: undef %Apache::functionplotresponse::splinescalex;
1210: undef %Apache::functionplotresponse::splinescaley;
1.10 www 1211: # Remember input fields, etc
1212: undef %Apache::functionplotresponse::previous;
1.9 www 1213: $Apache::functionplotresponse::inputfields='';
1.6 www 1214: $Apache::functionplotresponse::counter=0;
1.24 www 1215: # Remember rules
1.25 www 1216: undef @Apache::functionplotresponse::functionplotrules;
1.75 www 1217: undef @Apache::functionplotresponse::functionplotvectorrules;
1.37 www 1218: # Remember failed rules
1219: if ($target eq 'grade') {
1220: undef @Apache::functionplotresponse::failedrules;
1221: }
1222: # Delete previous awards
1223: undef $Apache::functionplotresponse::awarddetail;
1.10 www 1224: # Part and ID
1225: my $partid=$Apache::inputtags::part;
1226: my $id=&Apache::response::start_response($parstack,$safeeval);
1.6 www 1227: # Internal ID to mark the applet and its coordinates
1.10 www 1228: my $internalid = $partid.'_'.$id;
1229: # Previous answer
1230: &decode_previous_answer($Apache::lonhomework::history{"resource.$partid.$id.submission"});
1231:
1.6 www 1232: # Parameters of <functionplotresponse>
1.41 www 1233: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval);
1.6 www 1234: my $xaxisvisible=(&Apache::lonxml::get_param('xaxisvisible',$parstack,$safeeval)=~/on|true|yes|1/i?'true':'false');
1235: my $yaxisvisible=(&Apache::lonxml::get_param('yaxisvisible',$parstack,$safeeval)=~/on|true|yes|1/i?'true':'false');
1236: my $gridvisible=(&Apache::lonxml::get_param('gridvisible',$parstack,$safeeval)=~/on|true|yes|1/i?'true':'false');
1.14 www 1237: my $xlabel=&Apache::lonxml::get_param('xlabel',$parstack,$safeeval);
1238: my $ylabel=&Apache::lonxml::get_param('ylabel',$parstack,$safeeval);
1.12 www 1239: if ($target eq 'edit') {
1240: $result.=&Apache::edit::start_table($token)
1241: .'<tr><td><span class="LC_nobreak">'.&mt('Function Plot Question').'</span></td>'
1242: .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
1.37 www 1243: .&Apache::edit::deletelist($target,$token).' '
1.50 www 1244: .&Apache::edit::insertlist($target,$token).' '
1245: .&Apache::loncommon::help_open_topic('Function_Plot_Response_Question','Function Plot Responses')
1.12 www 1246: .'</span></td>'
1247: ."<td> "
1248: .&Apache::edit::end_row()
1249: .&Apache::edit::start_spanning_row()
1250: ."\n";
1.14 www 1251: $result.=&Apache::edit::text_arg('Label x-axis:','xlabel',
1252: $token,'6').' '.
1253: &Apache::edit::text_arg('Minimum x-value:','xmin',
1254: $token,'4').' '.
1.12 www 1255: &Apache::edit::text_arg('Maximum x-value:','xmax',
1.14 www 1256: $token,'4').' '.
1.12 www 1257: &Apache::edit::select_arg('x-axis visible:','xaxisvisible',
1.32 www 1258: ['yes','no'],$token).'<br />'.
1.14 www 1259: &Apache::edit::text_arg('Label y-axis:','ylabel',
1260: $token,'6').' '.
1.12 www 1261: &Apache::edit::text_arg('Minimum y-value:','ymin',
1.14 www 1262: $token,'4').' '.
1.12 www 1263: &Apache::edit::text_arg('Maximum y-value:','ymax',
1.14 www 1264: $token,'4').' '.
1.12 www 1265: &Apache::edit::select_arg('y-axis visible:','yaxisvisible',
1.32 www 1266: ['yes','no'],$token).'<br />'.
1.12 www 1267: &Apache::edit::select_arg('Grid visible:','gridvisible',
1.47 www 1268: ['yes','no'],$token).'<br />'.
1.58 www 1269: &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 1270: 'answerdisplay',$token,'50').
1.12 www 1271: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
1272: } elsif ($target eq 'modified') {
1273: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.14 www 1274: $safeeval,'xlabel','xmin','xmax','ylabel','ymin','ymax',
1.47 www 1275: 'xaxisvisible','yaxisvisible','gridvisible','answerdisplay');
1.12 www 1276: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.3 www 1277:
1.12 www 1278: } elsif ($target eq 'meta') {
1.10 www 1279: $result=&Apache::response::meta_package_write('functionplotresponse');
1.35 www 1280: } elsif (($target eq 'answer') &&
1281: ($env{'form.answer_output_mode'} ne 'tex') &&
1282: ($Apache::lonhomework::viewgrades == 'F')) {
1283: my (undef,undef,$udom,$uname)=&Apache::lonnet::whichuser();
1284: my $windowopen=&Apache::lonhtmlcommon::javascript_docopen();
1285: my $start_page = &Apache::loncommon::start_page('Rules Log', undef,
1286: {'only_body' => 1,
1287: 'bgcolor' => '#FFFFFF',
1288: 'js_ready' => 1,});
1289: my $end_page = &Apache::loncommon::end_page({'js_ready' => 1,});
1290: $uname =~s/\W//g;
1291: $udom =~s/\W//g;
1292: my $function_name =
1293: join('_','LONCAPA_scriptvars',$uname,$udom,
1294: $env{'form.counter'},$Apache::lonxml::curdepth);
1295: my $rules_var ="<script type=\"text/javascript\">
1296: // <![CDATA[
1297: function $function_name() {newWindow=open('','new_W','width=500,height=500,scrollbars=1,resizable=yes');newWindow.$windowopen;newWindow.document.writeln('$start_page<pre>".
1298: $Apache::functionplotresponse::ruleslog.
1299: "<\\/pre>$end_page');newWindow.document.close();newWindow.focus()}
1300: // ]]>
1301: </script><a href=\"javascript:$function_name();void(0);\">".&mt('Rules Log')."</a><br />";
1302: &Apache::lonxml::add_script_result($rules_var);
1303: }
1304:
1.1 www 1305: return $result;
1306: }
1307:
1.25 www 1308: sub compare_rel {
1.26 www 1309: my ($relationship,$value,$realval,$tol)=@_;
1.36 www 1310: # is the real value undefined?
1.26 www 1311: unless (defined($realval)) {
1.36 www 1312: # the real value is not defined
1.26 www 1313: if ($relationship eq 'eq') {
1314: if ($value eq 'undef') {
1315: return 1;
1316: } else {
1317: return 0;
1318: }
1319: } elsif ($relationship eq 'ne') {
1320: if ($value eq 'undef') {
1321: return 0;
1322: } else {
1323: return 1;
1324: }
1325: } else {
1326: return 0;
1327: }
1328: }
1329:
1.36 www 1330: # is the expected value undefined?
1331: if ($value eq 'undef') {
1332: # but by now we know that the real value is defined
1333: return 0;
1334: }
1335:
1336: # both are defined.
1.26 www 1337: if ($relationship eq 'gt') {
1338: return ($realval>$value);
1339: } elsif ($relationship eq 'ge') {
1340: return ($realval>$value-$tol);
1341: } elsif ($relationship eq 'lt') {
1342: return ($realval<$value);
1343: } elsif ($relationship eq 'le') {
1344: return ($realval<$value+$tol);
1345: } elsif ($relationship eq 'ne') {
1346: return (abs($value-$realval)>$tol);
1347: } else {
1348: return (abs($value-$realval)<$tol);
1349: }
1.25 www 1350: return 0;
1351: }
1352:
1.35 www 1353: sub addlog {
1354: my ($text)=@_;
1.67 www 1355: $text=~s/\'/\\\'/g;
1.35 www 1356: $Apache::functionplotresponse::ruleslog.=$text.'<br />';
1357: }
1358:
1359: sub actualval {
1360: my ($i,$xmin,$xmax)=@_;
1361: return $xmin+$i/400.*($xmax-$xmin);
1362: }
1.65 www 1363:
1.66 www 1364: sub fpr_val {
1365: my ($arg)=@_;
1366: return &actualval($Apache::functionplotresponse::functionplotrulelabels{$arg},
1367: $Apache::functionplotresponse::fpr_xmin,
1368: $Apache::functionplotresponse::fpr_xmax);
1369: }
1370:
1.65 www 1371: sub fpr_f {
1372: my ($arg)=@_;
1373: return $Apache::functionplotresponse::func[&array_index($Apache::functionplotresponse::fpr_xmin,
1374: $Apache::functionplotresponse::fpr_xmax,
1375: $arg)];
1376: }
1377:
1378: sub fpr_dfdx {
1379: my ($arg)=@_;
1380: return $Apache::functionplotresponse::dfuncdx[&array_index($Apache::functionplotresponse::fpr_xmin,
1381: $Apache::functionplotresponse::fpr_xmax,
1382: $arg)];
1383: }
1384:
1385: sub fpr_d2fdx2 {
1386: my ($arg)=@_;
1387: return $Apache::functionplotresponse::d2funcdx2[&array_index($Apache::functionplotresponse::fpr_xmin,
1388: $Apache::functionplotresponse::fpr_xmax,
1389: $arg)];
1390: }
1.77 www 1391:
1.84 www 1392: sub fpr_vectorcoords {
1393: my ($arg)=@_;
1394: $arg=~s/\W//gs;
1395: $arg=ucfirst($arg);
1396: my $id=$Apache::inputtags::response[-1];
1397: my $partid=$Apache::inputtags::part;
1398: my $internalid = $partid.'_'.$id;
1399: return ($env{'form.HWVAL_'.$internalid.'_'.$arg.'Start_x'},
1400: $env{'form.HWVAL_'.$internalid.'_'.$arg.'End_x'},
1401: $env{'form.HWVAL_'.$internalid.'_'.$arg.'Start_y'},
1402: $env{'form.HWVAL_'.$internalid.'_'.$arg.'End_y'});
1403: }
1404:
1405: sub fpr_objectcoords {
1406: my ($arg)=@_;
1407: $arg=~s/\W//gs;
1408: $arg=ucfirst($arg);
1409: my $id=$Apache::inputtags::response[-1];
1410: my $partid=$Apache::inputtags::part;
1411: my $internalid = $partid.'_'.$id;
1412: return ($env{'form.HWVAL_'.$internalid.'_'.$arg.'_x'},
1413: $env{'form.HWVAL_'.$internalid.'_'.$arg.'_y'});
1414: }
1415:
1416: sub fpr_vectorlength {
1417: my ($arg)=@_;
1418: my ($xs,$xe,$ys,$ye)=&fpr_vectorcoords($arg);
1419: return sqrt(($xe-$xs)*($xe-$xs)+($ye-$ys)*($ye-$ys));
1420: }
1421:
1422: sub fpr_vectorangle {
1423: my ($arg)=@_;
1424: my ($xs,$xe,$ys,$ye)=&fpr_vectorcoords($arg);
1425: my $angle=57.2957795*atan2(($ye-$ys),($xe-$xs));
1426: if ($angle<0) { $angle=360+$angle; }
1427: return $angle;
1428: }
1.77 www 1429:
1430: sub vectorcoords {
1431: my ($id,$label)=@_;
1432: return ($env{'form.HWVAL_'.$id.'_'.$label.'Start_x'},
1433: $env{'form.HWVAL_'.$id.'_'.$label.'End_x'},
1434: $env{'form.HWVAL_'.$id.'_'.$label.'Start_y'},
1435: $env{'form.HWVAL_'.$id.'_'.$label.'End_y'});
1436: }
1437:
1438: sub objectcoords {
1439: my ($id,$label)=@_;
1440: return ($env{'form.HWVAL_'.$id.'_'.$label.'_x'},
1441: $env{'form.HWVAL_'.$id.'_'.$label.'_y'});
1442: }
1.81 www 1443:
1444: sub attached {
1.87 ! www 1445: my ($id,$vector,$objects,$xmin,$xmax,$ymin,$ymax)=@_;
1.81 www 1446: my ($xs,$xe,$ys,$ye)=&vectorcoords($id,$vector);
1447: my $tolx=($xmax-$xmin)/100.;
1448: my $toly=($ymax-$ymin)/100.;
1449: my $tail=0;
1450: my $tip=0;
1.87 ! www 1451: foreach my $obj (split(/\s*\,\s*/,$objects)) {
! 1452: $obj=~s/\W//g;
! 1453: unless ($obj) { next; }
! 1454: $obj=ucfirst($obj);
! 1455: my ($xo,$yo)=&objectcoords($id,$obj);
! 1456: &addlog("Proximity $vector ($xs,$ys)-($xe,$ye) to $obj ($xo,$yo)");
! 1457: if ((abs($xs-$xo)<$tolx) && (abs($ys-$yo)<$toly)) {
! 1458: $tail=1;
! 1459: &addlog("Attached tail: $obj");
! 1460: }
! 1461: if ((abs($xe-$xo)<$tolx) && (abs($ye-$yo)<$toly)) {
! 1462: $tip=1;
! 1463: &addlog("Attached tip: $obj");
! 1464: }
! 1465: }
1.81 www 1466: &addlog("Result tail:$tail tip:$tip");
1467: return($tail,$tip);
1468: }
1469:
1.77 www 1470:
1471: sub vectorangle {
1472: my ($x,$y)=@_;
1.78 www 1473: my $angle=57.2957795*atan2($y,$x);
1474: if ($angle<0) { $angle=360+$angle; }
1475: return $angle;
1.77 www 1476: }
1477:
1478: sub vectorlength {
1479: my ($x,$y)=@_;
1480: return sqrt($x*$x+$y*$y);
1481: }
1482:
1.78 www 1483: sub relvector {
1484: my ($xs,$xe,$ys,$ye)=@_;
1485: return ($xe-$xs,$ye-$ys);
1486: }
1487:
1488: sub plotvectorlength {
1489: return &vectorlength(&relvector(&vectorcoords(@_)));
1490: }
1491:
1492: sub plotvectorangle {
1493: return &vectorangle(&relvector(&vectorcoords(@_)));
1494: }
1495:
1.77 www 1496:
1497: #
1498: # Evaluate a functionplotvectorrule
1499: #
1500:
1501: sub functionplotvectorrulecheck {
1502: my ($rule,$xmin,$xmax,$ymin,$ymax,$safeeval)=@_;
1.78 www 1503: &addlog("=================");
1.77 www 1504: my ($label,$type)=split(/\:/,$rule);
1505: if ($type eq 'vector') {
1506: return &vectorcheck($rule,$xmin,$xmax,$ymin,$ymax,$safeeval);
1507: } elsif ($type eq 'sum') {
1508: return &sumcheck($rule,$xmin,$xmax,$ymin,$ymax,$safeeval);
1.85 www 1509: } elsif ($type eq 'custom') {
1510: return &customcheck($rule,$safeeval);
1.77 www 1511: }
1512: }
1513:
1514: sub vectorcheck {
1515: my ($rule,$xmin,$xmax,$ymin,$ymax,$safeeval)=@_;
1516: my ($label,$type,$id,$vector,
1517: $attachpoint,$notattachpoint,
1518: $tailpoint,$tippoint,$nottailpoint,$nottippoint,
1.79 www 1519: $length,$angle,$lengtherror,$angleerror)=split(/\:/,$rule);
1.78 www 1520: &addlog("Vector Rule $label for vector ".$vector);
1.79 www 1521: if ($length ne '') {
1522: &addlog("Checking for length $length with error $lengtherror");
1523: $length=&Apache::run::run($length,$safeeval);
1524: &addlog("Length evaluated to $length");
1525: my $thislength=&plotvectorlength($id,$vector);
1526: &addlog("Found length $thislength");
1527: if (abs($thislength-$length)>$lengtherror) {
1528: &setfailed($label);
1529: return 0;
1530: }
1531: }
1.80 www 1532: if ($angle ne '') {
1533: &addlog("Checking for angle $angle with error $angleerror");
1534: $angle=&Apache::run::run($angle,$safeeval);
1535: &addlog("Angle evaluated to $angle");
1536: my $thisangle=&plotvectorangle($id,$vector);
1537: &addlog("Found angle $thisangle");
1538: my $anglediff=abs($thisangle-$angle);
1539: &addlog("Angle difference: $anglediff");
1540: if ($anglediff>360.-$anglediff) {
1541: $anglediff=360.-$anglediff;
1542: }
1543: &addlog("Smallest angle difference: $anglediff");
1544: if ($anglediff>$angleerror) {
1545: &setfailed($label);
1546: return 0;
1547: }
1548: }
1.81 www 1549: if ($attachpoint ne '') {
1550: &addlog("Checking attached: ".$attachpoint);
1551: my ($tail,$tip)=&attached($id,$vector,$attachpoint,$xmin,$xmax,$ymin,$ymax);
1552: unless ($tail || $tip) {
1553: &setfailed($label);
1554: return 0;
1555: }
1556: }
1557: if ($notattachpoint ne '') {
1558: &addlog("Checking not attached: ".$notattachpoint);
1559: my ($tail,$tip)=&attached($id,$vector,$notattachpoint,$xmin,$xmax,$ymin,$ymax);
1560: if ($tail || $tip) {
1561: &setfailed($label);
1562: return 0;
1563: }
1564: }
1565: if ($tailpoint ne '') {
1566: &addlog("Checking tail: ".$tailpoint);
1567: my ($tail,$tip)=&attached($id,$vector,$tailpoint,$xmin,$xmax,$ymin,$ymax);
1568: unless ($tail) {
1569: &setfailed($label);
1570: return 0;
1571: }
1572: }
1573: if ($nottailpoint ne '') {
1574: &addlog("Checking not tail: ".$nottailpoint);
1575: my ($tail,$tip)=&attached($id,$vector,$nottailpoint,$xmin,$xmax,$ymin,$ymax);
1576: if ($tail) {
1577: &setfailed($label);
1578: return 0;
1579: }
1580: }
1581: if ($tippoint ne '') {
1582: &addlog("Checking tip: ".$tippoint);
1583: my ($tail,$tip)=&attached($id,$vector,$tippoint,$xmin,$xmax,$ymin,$ymax);
1584: unless ($tip) {
1585: &setfailed($label);
1586: return 0;
1587: }
1588: }
1589: if ($nottippoint ne '') {
1590: &addlog("Checking not tip: ".$nottippoint);
1591: my ($tail,$tip)=&attached($id,$vector,$nottippoint,$xmin,$xmax,$ymin,$ymax);
1592: if ($tip) {
1593: &setfailed($label);
1594: return 0;
1595: }
1596: }
1597:
1.79 www 1598: &addlog("Rule $label passed.");
1599: return 1;
1.77 www 1600: }
1601:
1602: sub sumcheck {
1603: my ($rule,$xmin,$xmax,$ymin,$ymax,$safeeval)=@_;
1.82 www 1604: my ($label,$type,$id,$vectors,$length,$angle,$lengtherror,$angleerror)=split(/\:/,$rule);
1605: &addlog("Vector Sum Rule $label for vectors ".$vectors);
1606: my $sumx=0;
1607: my $sumy=0;
1608: foreach my $sv (split(/\s*\,\s*/,$vectors)) {
1609: my ($rx,$ry)=&relvector(&vectorcoords($id,$sv));
1610: $sumx+=$rx;
1611: $sumy+=$ry;
1612: }
1613: &addlog("Sum vector ($sumx,$sumy)");
1.83 www 1614: if ($length ne '') {
1615: &addlog("Checking length $length with error $lengtherror");
1616: $length=&Apache::run::run($length,$safeeval);
1617: &addlog("Evaluated to $length");
1618: my $thislength=&vectorlength($sumx,$sumy);
1619: &addlog("Actual length $thislength");
1620: if (abs($length-$thislength)>$lengtherror) {
1621: &setfailed($label);
1622: return 0;
1623: }
1624: }
1625: if ($angle ne '') {
1626: &addlog("Checking angle $angle with error $angleerror");
1627: $angle=&Apache::run::run($angle,$safeeval);
1628: &addlog("Evaluated to $angle");
1629: my $thisangle=&vectorangle($sumx,$sumy);
1630: &addlog("Actual angle $thisangle");
1631: my $anglediff=abs($thisangle-$angle);
1632: &addlog("Angle difference: $anglediff");
1633: if ($anglediff>360.-$anglediff) {
1634: $anglediff=360.-$anglediff;
1635: }
1636: &addlog("Smallest angle difference: $anglediff");
1637: if ($anglediff>$angleerror) {
1638: &setfailed($label);
1639: return 0;
1640: }
1641: }
1.79 www 1642: &addlog("Rule $label passed.");
1643: return 1;
1.77 www 1644: }
1645:
1.85 www 1646: sub customcheck {
1647: my ($rule,$safeeval)=@_;
1648: my ($label,$type,$prg)=split(/\:/,$rule);
1649: &addlog("Custom Rule ".$label);
1650: my $result=&Apache::run::run(&unescape($prg),$safeeval);
1651: &addlog("Algorithm returned $result");
1652: unless ($result) {
1653: &setfailed($label);
1654: return 0;
1655: }
1656: &addlog("Rule $label passed.");
1657: return 1;
1658: }
1659:
1.77 www 1660: #
1661: # Evaluate a functionplotrule
1662: #
1.35 www 1663:
1.25 www 1664: sub functionplotrulecheck {
1.65 www 1665: my ($rule,$xmin,$xmax,$ymin,$ymax,$safeeval)=@_;
1.33 www 1666:
1.35 www 1667: my ($label,$derivative,$xinitial,$xinitiallabel,$xfinal,$xfinallabel,$minimumlength,$maximumlength,$relationship,$value,$percent)
1.25 www 1668: =split(/\:/,$rule);
1.33 www 1669: $percent=($percent>0?$percent:5);
1.35 www 1670: &addlog("=================");
1.42 www 1671: &addlog("Rule $label for ".($derivative<0?'integral':('function itself','first derivative','second derivative')[$derivative])." $relationship $value");
1.65 www 1672: #
1673: # Evaluate the value
1674: #
1675: if ($value=~/\D/) {
1676: $Apache::functionplotresponse::fpr_xmin=$xmin;
1677: $Apache::functionplotresponse::fpr_xmax=$xmax;
1678: $value=&Apache::run::run($value,$safeeval);
1679: &addlog("Value evaluated to $value");
1680: }
1681:
1682: #
1683: # Minimum and maximum lengths of the interval
1684: #
1.49 www 1685: if ((defined($minimumlength)) || (defined($maximumlength))) {
1686: &addlog("Minimumlength $minimumlength Maximumlength $maximumlength");
1687: }
1.30 www 1688: my $li=0;
1689: my $lh=400;
1690:
1.31 www 1691: # Special case: the upper boundary was not defined
1692: # and needs to be set to the value where
1693: # the condition is not true anymore => set flag
1694:
1695: my $findupper=0;
1696: if (($xfinal eq '')
1697: && (!defined($Apache::functionplotresponse::functionplotrulelabels{$xfinallabel}))
1698: && ($xfinallabel)) {
1699: $findupper=1;
1700: }
1701:
1.25 www 1702: # if a hard value is set for the boundaries, it overrides the label
1.30 www 1703: if (($xinitial ne '') && ($xinitiallabel ne '') && ($xinitiallabel ne 'start')) {
1704: $li=&array_index($xmin,$xmax,$xinitial);
1705: $Apache::functionplotresponse::functionplotrulelabels{$xinitiallabel}=$li;
1706: }
1707: if (($xfinal ne '') && ($xfinallabel ne '') && ($xfinallabel ne 'end')) {
1708: $lh=&array_index($xmin,$xmax,$xfinal);
1709: $Apache::functionplotresponse::functionplotrulelabels{$xfinallabel}=$lh;
1.25 www 1710: }
1.31 www 1711: # if the label is defined, use it
1.25 www 1712: if (defined($Apache::functionplotresponse::functionplotrulelabels{$xinitiallabel})) {
1.35 www 1713: &addlog("Using lower label $xinitiallabel");
1.30 www 1714: $li=$Apache::functionplotresponse::functionplotrulelabels{$xinitiallabel};
1715: } else {
1716: $li=&array_index($xmin,$xmax,$xinitial);
1.25 www 1717: }
1.31 www 1718: unless ($findupper) {
1719: if (defined($Apache::functionplotresponse::functionplotrulelabels{$xfinallabel})) {
1.35 www 1720: &addlog("Using upper label $xfinallabel");
1.33 www 1721: $lh=$Apache::functionplotresponse::functionplotrulelabels{$xfinallabel}-1;
1.31 www 1722: } else {
1723: $lh=&array_index($xmin,$xmax,$xfinal);
1724: }
1.25 www 1725: }
1726: # Basic sanity checks
1.30 www 1727: if ($li<0) { $li=0; }
1728: if ($lh>400) { $lh=400; }
1.38 www 1729: if (($li>$lh) || (!defined($lh))) {
1.30 www 1730: $lh=$li;
1.25 www 1731: }
1.31 www 1732:
1.35 www 1733: &addlog("Boundaries: x=".&actualval($li,$xmin,$xmax)." (".$Apache::functionplotresponse::actualxval[$li]."; index $li)) to x=".
1734: &actualval($lh,$xmin,$xmax)." (".$Apache::functionplotresponse::actualxval[$lh]."; index $lh))");
1735: if ($findupper) {
1736: &addlog("Looking for label $xfinallabel");
1737: }
1.33 www 1738: my $tol=$percent*($ymax-$ymin)/100;
1739: if ($xmax>$xmin) {
1740: if ($derivative==2) {
1741: $tol=4.*$tol/($xmax-$xmin);
1742: } elsif ($derivative==1) {
1743: $tol=2.*$tol/($xmax-$xmin);
1.42 www 1744: } elsif ($derivative==-1) {
1745: $tol=$tol*($xmax-$xmin)/2.;
1.33 www 1746: }
1.27 www 1747: }
1.42 www 1748: my $integral=0;
1749: my $binwidth=($xmax-$xmin)/400.;
1750: if (($derivative<0) && (!$findupper)) {
1751: # definite integral, calculate over whole length
1752: &addlog("Calculating definite integral");
1753: for (my $i=$li; $i<=$lh; $i++) {
1754: $integral+=$Apache::functionplotresponse::func[$i]*$binwidth;
1755: }
1756: unless (&compare_rel($relationship,$value,$integral,$tol)) {
1757: &addlog("Actual integral ".(defined($integral)?$integral:'undef').", expected $value, tolerance $tol");
1758: &addlog("Rule $label failed.");
1.79 www 1759: &setfailed($label);
1.42 www 1760: return 0;
1761: }
1762: } else {
1763: for (my $i=$li; $i<=$lh; $i++) {
1.25 www 1764: my $val;
1765: if ($derivative==2) {
1.27 www 1766: $val=$Apache::functionplotresponse::d2funcdx2[$i];
1.25 www 1767: } elsif ($derivative==1) {
1.27 www 1768: $val=$Apache::functionplotresponse::dfuncdx[$i];
1.42 www 1769: } elsif ($derivative==-1) {
1770: $integral+=$Apache::functionplotresponse::func[$i]*$binwidth;
1771: $val=$integral;
1.25 www 1772: } else {
1.27 www 1773: $val=$Apache::functionplotresponse::func[$i];
1.25 www 1774: }
1.31 www 1775: unless (&compare_rel($relationship,$value,$val,$tol)) {
1.37 www 1776: &addlog("Actual value ".(defined($val)?$val:'undef').", expected $value, tolerance $tol");
1.35 www 1777: &addlog("Condition not fulfilled at x=".&actualval($i,$xmin,$xmax)." (".$Apache::functionplotresponse::actualxval[$i]."; index $i)");
1.31 www 1778: if (($findupper) && ($i>$li)) {
1.49 www 1779: # Check lengths
1780: unless (&checklength($i,$li,$minimumlength,$maximumlength,$xmin,$xmax,$label)) { return 0; }
1781: # Successfully found a new label, set it
1.31 www 1782: $Apache::functionplotresponse::functionplotrulelabels{$xfinallabel}=$i;
1.35 www 1783: &addlog("Rule $label passed, setting label $xfinallabel");
1.31 www 1784: return 1;
1785: } else {
1.35 www 1786: &addlog("Rule $label failed.");
1.49 www 1787: &setfailed($label);
1.31 www 1788: return 0;
1789: }
1790: }
1.42 www 1791: }
1.25 www 1792: }
1.49 www 1793: # Corner case where this makes sense: using start or stop as defined labels
1794: unless (&checklength($lh,$li,$minimumlength,$maximumlength,$xmin,$xmax,$label)) { return 0; }
1.35 www 1795: &addlog("Rule $label passed.");
1.27 www 1796: return 1;
1.24 www 1797: }
1798:
1.49 www 1799: #
1800: # check for minimum and maximum lengths
1801: #
1802:
1803: sub checklength {
1804: my ($i,$li,$minimumlength,$maximumlength,$xmin,$xmax,$label)=@_;
1805: unless (($minimumlength) || ($maximumlength)) { return 1; }
1806: my $length=&actualval($i,$xmin,$xmax)-&actualval($li,$xmin,$xmax);
1807: if ($minimumlength) {
1808: if ($length<$minimumlength) {
1809: &addlog("Rule $label failed, actual length $length, minimum length $minimumlength");
1810: &setfailed($label);
1811: return 0;
1812: }
1813: }
1814: if ($maximumlength) {
1815: if ($length>$maximumlength) {
1816: &addlog("Rule $label failed, actual length $length, maximum length $maximumlength");
1817: &setfailed($label);
1818: return 0;
1819: }
1820: }
1821: return 1;
1822: }
1823:
1824: sub setfailed {
1.79 www 1825: my ($hintlabel)=@_;
1.49 www 1826: $hintlabel=~s/^R//;
1827: push(@Apache::functionplotresponse::failedrules,$hintlabel);
1828: &addlog("Set hint condition $hintlabel");
1829: }
1830:
1.37 www 1831: sub start_functionplotruleset {
1832: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1833: if ($target eq 'edit') {
1834: return &Apache::edit::start_table($token).
1835: '<tr><td><span class="LC_nobreak">'.&mt('Function Plot Rule Set').'</span></td>'
1836: .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
1837: .&Apache::edit::deletelist($target,$token).' '.
1.50 www 1838: &Apache::edit::insertlist($target,$token).' '
1839: .&Apache::loncommon::help_open_topic('Function_Plot_Response_Rule_Set','Function Plot Rules')
1.37 www 1840: .'</span></td>'
1841: ."<td> "
1842: .&Apache::edit::end_row()
1843: .&Apache::edit::start_spanning_row()
1844: ."\n";
1845: }
1846: }
1.3 www 1847:
1.37 www 1848: sub end_functionplotruleset {
1849: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1850: my $id=$Apache::inputtags::response[-1];
1851: my $partid=$Apache::inputtags::part;
1852: my $internalid = $partid.'_'.$id;
1853:
1854: if ($target eq 'edit' ) {
1855: return &Apache::edit::end_table();
1856: } elsif ($target eq 'grade'
1.10 www 1857: && &Apache::response::submitted()
1858: && $Apache::lonhomework::type ne 'exam') {
1.12 www 1859: #
1860: # Actually grade
1861: #
1.41 www 1862: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-2);
1.27 www 1863:
1.24 www 1864: my $ad='';
1.30 www 1865: undef %Apache::functionplotresponse::functionplotrulelabels;
1.35 www 1866: $Apache::functionplotresponse::ruleslog='';
1.30 www 1867: $Apache::functionplotresponse::functionplotrulelabels{'start'}=400;
1868: $Apache::functionplotresponse::functionplotrulelabels{'end'}=0;
1.64 www 1869: if (&populate_arrays($internalid,$xmin,$xmax,$ymin,$ymax) eq 'no_func') {
1.21 www 1870: $ad='NOT_FUNCTION';
1.18 www 1871: } else {
1.35 www 1872: &addlog("Start of function ".&actualval($Apache::functionplotresponse::functionplotrulelabels{'start'},$xmin,$xmax)." (index ".
1873: $Apache::functionplotresponse::functionplotrulelabels{'start'}.")");
1874: &addlog("End of function ".&actualval($Apache::functionplotresponse::functionplotrulelabels{'end'},$xmin,$xmax)." (index ".
1875: $Apache::functionplotresponse::functionplotrulelabels{'end'}.")");
1876:
1.24 www 1877: # We have a function that we can actually grade, go through the spline rules.
1.25 www 1878: foreach my $rule (@Apache::functionplotresponse::functionplotrules) {
1.65 www 1879: unless (&functionplotrulecheck($rule,$xmin,$xmax,$ymin,$ymax,$safeeval)) {
1.24 www 1880: $ad='INCORRECT';
1881: last;
1882: }
1883: }
1.77 www 1884: # And now go through the vector rules
1885: foreach my $rule (@Apache::functionplotresponse::functionplotvectorrules) {
1886: unless (&functionplotvectorrulecheck($rule,$xmin,$xmax,$ymin,$ymax,$safeeval)) {
1887: $ad='INCORRECT';
1888: last;
1889: }
1890: }
1.24 www 1891: # If it's not wrong, it's correct
1892: unless ($ad) { $ad='EXACT_ANS' };
1.18 www 1893: }
1.37 www 1894: &addlog("Set hint conditions: ".join(",",@Apache::functionplotresponse::failedrules));
1895: &addlog("Assigned award detail: $ad");
1896: # Store for later to be assigned at end_functionplotresponse
1897: $Apache::functionplotresponse::awarddetail=$ad;
1898: }
1899: }
1900:
1901:
1902: sub end_functionplotresponse {
1903: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1904: &Apache::response::end_response;
1905:
1906: my $result;
1907: my $id=$Apache::inputtags::response[-1];
1908: my $partid=$Apache::inputtags::part;
1909: my $internalid = $partid.'_'.$id;
1.20 www 1910:
1.37 www 1911: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
1912: if ($target eq 'grade'
1913: && &Apache::response::submitted()
1914: && $Apache::lonhomework::type eq 'exam') {
1915:
1916: &Apache::response::scored_response($partid,$id);
1917:
1918: } elsif ($target eq 'grade'
1919: && &Apache::response::submitted()
1920: && $Apache::lonhomework::type ne 'exam') {
1921: my ($response,%coords)=&get_answer_from_form_fields($internalid);
1922: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
1923: my %previous=&Apache::response::check_for_previous($response,$partid,$id);
1924: #
1925: # Assign grade
1926: #
1927: my $ad=$Apache::functionplotresponse::awarddetail;
1.12 www 1928: #
1929: # Store grading info
1930: #
1931: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
1932: &Apache::response::handle_previous(\%previous,$ad);
1.10 www 1933: } elsif ($target eq 'web') {
1.37 www 1934: undef @Apache::functionplotresponse::failedrules;
1935: }
1936: return $result;
1937: }
1938:
1939: sub end_functionplotelements {
1940: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1941: my $result='';
1942: my $id=$Apache::inputtags::response[-1];
1943: my $partid=$Apache::inputtags::part;
1944: my $internalid = $partid.'_'.$id;
1945: if ($target eq 'edit' ) {
1946: $result=&Apache::edit::end_table();
1947: } elsif ($target eq 'web') {
1.47 www 1948: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-2);
1949:
1950: # Are we in show answer mode?
1951: my $showanswer=&Apache::response::show_answer();
1952: if ($showanswer) {
1953: # Render answerdisplay
1954: my $answerdisplay=&Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval,-2);
1955: if ($answerdisplay=~/\S/s) {
1956: foreach my $plot (split(/\s*\,\s*/,$answerdisplay)) {
1.58 www 1957: my @components=split(/\s*\:\s*/,$plot);
1958: if ($#components<3) {
1959: # Just a simple plot
1960: my ($func,$xl,$xh)=@components;
1961: if ((!defined($xl)) || ($xl eq '')) { $xl=$xmin; }
1962: if ((!defined($xh)) || ($xh eq '')) { $xh=$xmax; }
1963: $result.=&plot_script($internalid,$func,1,'','00aa00',$xl,$xh,6);
1964: } else {
1965: # This is a spline
1.59 www 1966: $result.=&answer_spline_script($internalid,@components);
1.58 www 1967: }
1.47 www 1968: }
1969: }
1970: }
1.48 www 1971: my $fixed=0;
1972: if (($showanswer) || (&Apache::response::check_status()>=2)) { $fixed=1; }
1.6 www 1973: # Now is the time to render all of the stored splines
1974: foreach my $label (keys(%Apache::functionplotresponse::splineorder)) {
1.48 www 1975: $result.=&generate_spline($internalid,$label,$xmin,$xmax,$ymin,$ymax,$fixed);
1.6 www 1976: }
1.5 www 1977: # close the init script
1.3 www 1978: $result.=&end_init_script();
1.17 www 1979: # register all splines in this response
1980: $result.='<input type="hidden" name="HWVAL_AllSplines_'.$internalid.'" value="'.
1981: join(',',keys(%Apache::functionplotresponse::splineorder)).'" />'."\n";
1982: foreach my $label (keys(%Apache::functionplotresponse::splineorder)) {
1983: $result.='<input type="hidden" name="HWVAL_SplineOrder_'.$internalid.'_'.$label.'" value="'.
1984: $Apache::functionplotresponse::splineorder{$label}.'" />'."\n";
1985: }
1.9 www 1986: # generate the input fields
1987: $result.=$Apache::functionplotresponse::inputfields;
1.5 www 1988: # actually start the <applet>-tag
1.3 www 1989: $result.=&geogebra_startcode($internalid);
1.5 www 1990: # load the spline bytecode
1.3 www 1991: $result.=&geogebra_spline_program();
1.5 www 1992: # set default parameters
1.3 www 1993: $result.=&geogebra_default_parameters($internalid);
1.5 www 1994: # close the <applet>-tag
1.1 www 1995: $result.=&geogebra_endcode();
1996: }
1997: return $result;
1998: }
1999:
1.41 www 2000: sub boundaries {
2001: my ($parstack,$safeeval,$level)=@_;
2002: my $xmin=&Apache::lonxml::get_param('xmin',$parstack,$safeeval,$level);
2003: $xmin=(defined($xmin)?$xmin:-10);
2004: my $xmax=&Apache::lonxml::get_param('xmax',$parstack,$safeeval,$level);
2005: $xmax=(defined($xmax)?$xmax:10);
2006: my $ymin=&Apache::lonxml::get_param('ymin',$parstack,$safeeval,$level);
2007: $ymin=(defined($ymin)?$ymin:-10);
2008: my $ymax=&Apache::lonxml::get_param('ymax',$parstack,$safeeval,$level);
2009: $ymax=(defined($ymax)?$ymax:10);
2010: if ($xmax<=$xmin) {
2011: $xmax=$xmin+20;
2012: }
2013: if ($ymax<=$ymin) {
2014: $ymax=$ymin+20;
2015: }
2016: return ($xmin,$xmax,$ymin,$ymax);
2017: }
2018:
1.37 www 2019: sub start_functionplotelements {
2020: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
2021: my $result='';
2022: my $id=$Apache::inputtags::response[-1];
2023: my $partid=$Apache::inputtags::part;
2024: my $internalid = $partid.'_'.$id;
2025:
2026: if ($target eq 'edit') {
2027: return &Apache::edit::start_table($token).
2028: '<tr><td><span class="LC_nobreak">'.&mt('Function Plot Elements').'</span></td>'
2029: .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
2030: .&Apache::edit::deletelist($target,$token).' '.
1.50 www 2031: &Apache::edit::insertlist($target,$token).' '
2032: .&Apache::loncommon::help_open_topic('Function_Plot_Response_Elements','Function Plot Elements')
1.37 www 2033: .'</span></td>'
2034: ."<td> "
2035: .&Apache::edit::end_row()
2036: .&Apache::edit::start_spanning_row()
2037: ."\n";
2038: } elsif ($target eq 'web') {
1.41 www 2039: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-2);
1.37 www 2040: my $xaxisvisible=(&Apache::lonxml::get_param('xaxisvisible',$parstack,$safeeval,-2)=~/on|true|yes|1/i?'true':'false');
2041: my $yaxisvisible=(&Apache::lonxml::get_param('yaxisvisible',$parstack,$safeeval,-2)=~/on|true|yes|1/i?'true':'false');
2042: my $gridvisible=(&Apache::lonxml::get_param('gridvisible',$parstack,$safeeval,-2)=~/on|true|yes|1/i?'true':'false');
2043: my $xlabel=&Apache::lonxml::get_param('xlabel',$parstack,$safeeval,-2);
2044: my $ylabel=&Apache::lonxml::get_param('ylabel',$parstack,$safeeval,-2);
2045:
2046:
2047: # paste in the update routine to receive stuff back from the applet
2048: $result.=&update_script($internalid);
2049: # start the initscript for this applet
2050: $result.=&start_init_script($internalid);
2051: # put the axis commands inside
2052: $result.=&axes_script($internalid,$xmin,$xmax,$ymin,$ymax,$xaxisvisible,$yaxisvisible,$gridvisible);
1.45 www 2053: $result.=&axes_label($internalid,$xmin,$xmax,$ymin,$ymax,$xlabel,$ylabel);
1.37 www 2054: # init script is left open
2055: }
2056: return $result;
2057: }
2058:
1.1 www 2059: 1;
2060:
2061: __END__
2062:
2063: =head1 NAME
2064:
2065: Apache::functionplotresponse.pm;
2066:
2067: =head1 SYNOPSIS
2068:
2069: Handles tags associated with accepting function plots.
2070:
2071: This is part of the LearningOnline Network with CAPA project
2072: described at http://www.lon-capa.org.
2073:
2074: =head1 HANDLER SUBROUTINE
2075:
2076: start_functionplotresponse()
2077:
2078: =head1 OTHER SUBROUTINES
2079:
2080: =over
2081:
2082: =item end_functionplotresponse()
2083:
2084: =back
2085:
2086: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>