Annotation of loncom/homework/functionplotresponse.pm, revision 1.83
1.1 www 1: # LearningOnline Network with CAPA
1.70 www 2: # Functionplot responses
1.1 www 3: #
1.83 ! www 4: # $Id: functionplotresponse.pm,v 1.82 2011/11/21 20:20:48 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.76 www 38: 'plotobject','plotvector','functionplotvectorrule','functionplotvectorsumrule',
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="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"/>
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: }
1.77 www 392: &generate_input_field($id,$label,$x,$y);
1.70 www 393: return "document.ggbApplet_$id.evalCommand('a=1');\n".
394: "document.ggbApplet_$id.setVisible('a', false);\n".
395: "document.ggbApplet_$id.setLabelVisible('a', false);\n".
396: "document.ggbApplet_$id.evalCommand('$label=a*($x,$y)');\n".
397: "document.ggbApplet_$id.setVisible('$label', true);\n".
398: "document.ggbApplet_$id.setLabelVisible('$label', true);\n";
399: }
400:
401: #
402: # Subroutine to produce vectors
403: #
404:
405: sub plotvector_script {
1.72 www 406: my ($id,$label,$xs,$ys,$xe,$ye,$xmin,$xmax)=@_;
1.70 www 407: unless ($label) {
408: $Apache::functionplotresponse::counter++;
409: $label='V'.$Apache::functionplotresponse::counter;
410: }
1.71 www 411: my $startlabel=$label.'Start';
412: my $endlabel=$label.'End';
413: my $pointlabel=$label.'Point';
1.72 www 414: my $pointx=2.*($xmax-$xmin)+$xmax;
1.71 www 415: my $anglelabel=$label.'Angle';
1.74 www 416: return
417: &new_point_coordinate($id,$startlabel,$xs,$ys,0).
418: &new_point_coordinate($id,$endlabel,$xe,$ye,0).
419: (<<ENDVECTOR);
1.71 www 420: document.ggbApplet_$id.evalCommand("$label=Vector[$startlabel,$endlabel]");
421: document.ggbApplet_$id.setLabelVisible("$label",true);
422: document.ggbApplet_$id.setLineThickness("$label",8);
1.72 www 423: document.ggbApplet_$id.evalCommand("$pointlabel=($pointx,y($startlabel))");
424: document.ggbApplet_$id.evalCommand("$anglelabel=Angle[$pointlabel,$startlabel,$endlabel]");
425: document.ggbApplet_$id.setLabelVisible("$anglelabel",true);
426: document.ggbApplet_$id.setLabelStyle("$anglelabel",VALUE=2);
1.70 www 427: ENDVECTOR
428: }
429:
430: #
1.58 www 431: # Answer spline display
432: #
433: # points: x,y,slope_x,slope_y
434:
435: sub answer_spline_script {
436: my ($id,@points)=@_;
437: my $order=int(($#points+1)/4);
438: if ($order<2) { $order=2; }
439: if ($order>8) { $order=8; }
440: $Apache::functionplotresponse::counter++;
1.60 www 441: my $label='CSpline'.$Apache::functionplotresponse::counter;
1.59 www 442: my $output='document.ggbApplet_'.$id.'.evalCommand("'.$label.'=Spline'.$order.'[';
443: for (my $i=0;$i<=$#points;$i+=4) {
444: $output.="($points[$i],$points[$i+1]),($points[$i+2],$points[$i+3]),";
445: }
446: $output=~s/\,$//;
447: $output.=']");'."\n";
448: for (my $i=2; $i<2*$order; $i+=2) {
1.61 www 449: $output.='document.ggbApplet_'.$id.'.setColor("'.$label.'_'.($i>=10?'{':'').$i.($i>=10?'}':'').'",0,170,0);'."\n";
1.59 www 450: }
1.60 www 451: for (my $i=1; $i<2*$order; $i+=2) {
1.61 www 452: $output.='document.ggbApplet_'.$id.'.setVisible("'.$label.'_'.($i>=10?'{':'').$i.($i>=10?'}':'').'",false);'."\n";
1.60 www 453: }
454:
1.59 www 455: return $output;
1.58 www 456: }
457:
458: #
1.7 www 459: # Subroutine that generates code for spline $label based on stored information
1.6 www 460: #
461:
462: sub generate_spline {
1.48 www 463: my ($id,$label,$xmin,$xmax,$ymin,$ymax,$fixed)=@_;
1.6 www 464: my $result='';
1.7 www 465: my $order=$Apache::functionplotresponse::splineorder{$label};
466: my $x=$Apache::functionplotresponse::splineinitx{$label};
467: my $y=$Apache::functionplotresponse::splineinity{$label};
468: my $sx=$Apache::functionplotresponse::splinescalex{$label};
469: my $sy=$Apache::functionplotresponse::splinescaley{$label};
470: my @coords=();
471: foreach my $i (1..$order) {
1.48 www 472: $result.=&new_point_coordinate($id,$label.'P'.$i,$x,$y,$fixed);
1.40 www 473: my $xp=$x;
1.8 www 474: $x+=$sx/(2.*($order-1));
1.7 www 475: push(@coords,$label.'P'.$i);
1.48 www 476: $result.=&new_slope_coordinate($id,$label.'S'.$i,$x,$y+$sy,$label.'P'.$i,$xp,$y,$xmin,$xmax,$ymin,$ymax,$fixed);
1.8 www 477: $x+=$sx/(2.*($order-1));
1.7 www 478: push(@coords,$label.'S'.$i);
479: }
480: $result.='document.ggbApplet_'.$id.'.evalCommand("Spline'.$order.'['.join(',',@coords).']");'."\n";
1.6 www 481: return $result;
482: }
1.70 www 483:
484: #
485: # Object
486: #
487:
488: sub start_plotobject {
489: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
490: my $result='';
491: my $internalid = $Apache::inputtags::part.'_'.$Apache::inputtags::response[-1];
492: my $x=&Apache::lonxml::get_param('x',$parstack,$safeeval);
493: my $y=&Apache::lonxml::get_param('y',$parstack,$safeeval);
494: my $label=&Apache::lonxml::get_param('label',$parstack,$safeeval);
495: $label=~s/\W//gs;
496: $label=ucfirst($label);
497: unless ($label) { $label="NewObject"; }
498: if ($target eq 'web') {
499: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-3);
500: unless (defined($x)) { $x=$xmin; }
501: unless (defined($y)) { $y=$ymin; }
502: $result.=&plotobject_script($internalid,$label,$x,$y);
503: } elsif ($target eq 'edit') {
504: $result=&Apache::edit::tag_start($target,$token,'Plot Object').
505: &Apache::edit::text_arg('Label on Plot:','label',
506: $token,'16').
507: &Apache::edit::text_arg('x:','x',
508: $token,'8').
509: &Apache::edit::text_arg('y:','y',
510: $token,'8').
511: &Apache::edit::end_row();
512: } elsif ($target eq 'modified') {
1.76 www 513: $env{'form.'.&Apache::edit::html_element_name('label')}=ucfirst($env{'form.'.&Apache::edit::html_element_name('label')});
1.70 www 514: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'label','x','y');
515: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
516: }
517: return $result;
518: }
519:
520: sub end_plotobject {
521: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
522: my $result='';
523: if ($target eq 'edit') {
524: $result=&Apache::edit::end_table();
525: }
526: return $result;
527: }
528:
1.71 www 529: #
530: # Vector
531: #
532:
533: sub start_plotvector {
534: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
535: my $result='';
536: my $internalid = $Apache::inputtags::part.'_'.$Apache::inputtags::response[-1];
537: my $tailx=&Apache::lonxml::get_param('tailx',$parstack,$safeeval);
538: my $taily=&Apache::lonxml::get_param('taily',$parstack,$safeeval);
539: my $tipx=&Apache::lonxml::get_param('tipx',$parstack,$safeeval);
540: my $tipy=&Apache::lonxml::get_param('tipy',$parstack,$safeeval);
541:
542: my $label=&Apache::lonxml::get_param('label',$parstack,$safeeval);
543: $label=~s/\W//gs;
544: $label=ucfirst($label);
545: unless ($label) { $label="NewVector"; }
546: if ($target eq 'web') {
547: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-3);
548: unless (defined($tailx)) { $tailx=$xmin; }
549: unless (defined($taily)) { $taily=$ymin; }
550: unless (defined($tipx)) { $tipx=$xmin; }
551: unless (defined($tipy)) { $tipy=$ymin; }
1.72 www 552: $result.=&plotvector_script($internalid,$label,$tailx,$taily,$tipx,$tipy,$xmin,$xmax);
1.71 www 553: } elsif ($target eq 'edit') {
554: $result=&Apache::edit::tag_start($target,$token,'Plot Vector').
555: &Apache::edit::text_arg('Label on Plot:','label',
556: $token,'16').
557: &Apache::edit::text_arg('Tail x:','tailx',
558: $token,'8').
559: &Apache::edit::text_arg('Tail y:','taily',
560: $token,'8').
561: &Apache::edit::text_arg('Tip x:','tipx',
562: $token,'8').
563: &Apache::edit::text_arg('Tip y:','tipy',
564: $token,'8').
565:
566: &Apache::edit::end_row();
567: } elsif ($target eq 'modified') {
1.76 www 568: $env{'form.'.&Apache::edit::html_element_name('label')}=ucfirst($env{'form.'.&Apache::edit::html_element_name('label')});
1.71 www 569: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'label','tailx','taily','tipx','tipy');
570: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
571: }
572: return $result;
573: }
574:
575: sub end_plotvector {
576: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
577: my $result='';
578: if ($target eq 'edit') {
579: $result=&Apache::edit::end_table();
580: }
581: return $result;
582: }
583:
584:
1.70 www 585:
1.6 www 586: #
587: # <backgroundplot function="..." fixed="yes/no" />
1.5 www 588: #
1.4 www 589: sub start_backgroundplot {
590: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
591: my $result='';
592: my $internalid = $Apache::inputtags::part.'_'.$Apache::inputtags::response[-1];
593: my $function=&Apache::lonxml::get_param('function',$parstack,$safeeval);
1.47 www 594: my $xinitial=&Apache::lonxml::get_param('xinitial',$parstack,$safeeval);
595: my $xfinal=&Apache::lonxml::get_param('xfinal',$parstack,$safeeval);
1.41 www 596: my $label=&Apache::lonxml::get_param('label',$parstack,$safeeval);
1.46 www 597: my $color=&Apache::lonxml::get_param('color',$parstack,$safeeval);
598: $color=~s/[^a-fA-F0-9]//gs;
599: unless (length($color)==6) { $color=''; }
1.6 www 600: my $fixed=(&Apache::lonxml::get_param('fixed',$parstack,$safeeval)=~/on|true|yes|1/i?1:0);
601:
1.4 www 602: unless ($function) { $function="0"; }
603: if ($target eq 'web') {
1.41 www 604: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-3);
1.47 www 605: unless (defined($xinitial)) { $xinitial=$xmin; }
606: unless (defined($xfinal)) { $xfinal=$xmax; }
607: $result.=&plot_script($internalid,$function,$fixed,$label,$color,$xinitial,$xfinal);
1.13 www 608: } elsif ($target eq 'edit') {
609: $result=&Apache::edit::tag_start($target,$token,'Background Function Plot').
610: &Apache::edit::text_arg('Function:','function',
611: $token,'16').
1.47 www 612: &Apache::edit::text_arg('Initial x-value (optional):','xinitial',
613: $token,'8').
614: &Apache::edit::text_arg('Final x-value (optional):','xfinal',
615: $token,'8').
1.41 www 616: &Apache::edit::text_arg('Label on Plot:','label',
617: $token,'8').
1.46 www 618: &Apache::edit::text_arg('Color (hex code):','color',
619: $token,'8').
1.13 www 620: &Apache::edit::select_arg('Fixed location:','fixed',
621: ['yes','no'],$token).
622: &Apache::edit::end_row();
623: } elsif ($target eq 'modified') {
624: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.47 www 625: $safeeval,'function','label','xinitial','xfinal','color','fixed');
1.13 www 626: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
627: }
628: return $result;
1.4 www 629: }
630:
631: sub end_backgroundplot {
1.13 www 632: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
633: my $result='';
634: if ($target eq 'edit') {
635: $result=&Apache::edit::end_table();
636: }
637: return $result;
1.6 www 638: }
639:
640: #
1.25 www 641: # <functionplotrule ... />
1.12 www 642: #
1.25 www 643: sub start_functionplotrule {
1.12 www 644: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
645: my $result='';
1.24 www 646: my $label=&Apache::lonxml::get_param('index',$parstack,$safeeval);
647: $Apache::functionplotresponse::counter++;
648: if ($label=~/\W/) {
1.25 www 649: &Apache::lonxml::warning(&mt('Rule indices should only contain alphanumeric characters.'));
1.24 www 650: }
651: $label=~s/\W//gs;
652: unless ($label) {
653: $label='R'.$Apache::functionplotresponse::counter;
654: } else {
655: $label='R'.$label;
656: }
657:
658: if ($target eq 'grade') {
659: # Simply remember - in order - for later
1.25 www 660: my $beginninglabel=&Apache::lonxml::get_param('xinitiallabel',$parstack,$safeeval);
661: my $endinglabel=&Apache::lonxml::get_param('xfinallabel',$parstack,$safeeval);
1.24 www 662: if (($beginninglabel=~/\W/) || ($endinglabel=~/W/)) {
1.25 www 663: &Apache::lonxml::warning(&mt('Rule labels must be alphanumeric.'));
1.24 www 664: }
665: $beginninglabel=~s/\W//gs;
666: $endinglabel=~s/\W//gs;
667: my $relationship=&Apache::lonxml::get_param('relationship',$parstack,$safeeval);
668: $relationship=~s/\W//gs;
669: $relationship=lc($relationship);
670: unless ($relationship=~/^(eq|ge|gt|le|lt|ne)$/) {
1.25 www 671: &Apache::lonxml::warning(&mt('Rule relationship not defined.'));
1.24 www 672: $relationship='eq';
673: }
1.35 www 674: my $derivative=&Apache::lonxml::get_param('derivativeorder',$parstack,$safeeval);
1.42 www 675: unless (($derivative==-1) || ($derivative==0) || ($derivative==1) || ($derivative==2)) {
1.25 www 676: &Apache::lonxml::warning(&mt('Rule derivative not defined.'));
1.24 www 677: $derivative=0;
678: }
1.25 www 679: push(@Apache::functionplotresponse::functionplotrules,join(':',(
1.24 www 680: $label,
681: $derivative,
1.25 www 682: &Apache::lonxml::get_param('xinitial',$parstack,$safeeval),
1.24 www 683: $beginninglabel,
1.25 www 684: &Apache::lonxml::get_param('xfinal',$parstack,$safeeval),
1.24 www 685: $endinglabel,
1.35 www 686: &Apache::lonxml::get_param('minimumlength',$parstack,$safeeval),
687: &Apache::lonxml::get_param('maximumlength',$parstack,$safeeval),
1.24 www 688: $relationship,
1.33 www 689: &Apache::lonxml::get_param('value',$parstack,$safeeval),
690: &Apache::lonxml::get_param('percenterror',$parstack,$safeeval)
1.24 www 691: )));
692: } elsif ($target eq 'edit') {
1.75 www 693: $result=&Apache::edit::tag_start($target,$token,'Function Plot Graph Rule').
1.36 www 694: &Apache::edit::text_arg('Index/Name:','index',
695: $token,'10').' '.
1.35 www 696: &Apache::edit::select_arg(&mt('Function:'),'derivativeorder',
1.29 www 697: [['0','Function itself'],
698: ['1','First derivative'],
1.42 www 699: ['2','Second derivative'],
700: ['-1','Integral']],$token).'<br />'.
1.44 www 701: &Apache::edit::text_arg('Initial x-value:','xinitial',
1.35 www 702: $token,'8').
1.44 www 703: &Apache::edit::select_or_text_arg('Initial x-value label:','xinitiallabel',
704: [['start','Start of Plot'],
705: ['end','End of Plot']],$token,'8').'<br />'.
1.30 www 706:
1.44 www 707: &Apache::edit::text_arg('Final x-value (optional):','xfinal',
1.35 www 708: $token,'8').
1.44 www 709: &Apache::edit::select_or_text_arg('Final x-value label (optional):','xfinallabel',
1.32 www 710: [['end','End of Plot']],$token,'8').'<br />'.
1.44 www 711: &Apache::edit::text_arg('Minimum length for range (optional):','minimumlength',
1.35 www 712: $token,'8').
1.44 www 713: &Apache::edit::text_arg('Maximum length for range (optional):','maximumlength',
1.35 www 714: $token,'8').'<br />'.
1.38 www 715: &Apache::edit::select_or_text_arg(&mt('Relationship:'),'relationship',
1.29 www 716: [['eq','equal'],
717: ['ne','not equal'],
718: ['ge','greater than or equal'],
719: ['gt','greater than'],
720: ['lt','less than'],
721: ['le','less than or equal']],$token).
722: $result.= &Apache::edit::select_or_text_arg('Value:','value',
1.65 www 723: [['undef','not defined']],$token,'30').
1.29 www 724: &Apache::edit::text_arg('Percent error:','percenterror',
1.35 www 725: $token,'8').
1.29 www 726: &Apache::edit::end_row();
727: } elsif ($target eq 'modified') {
1.54 www 728: if (($env{'form.'.&Apache::edit::html_element_name('xinitial')} ne '') && ($env{'form.'.&Apache::edit::html_element_name('xinitiallabel')} eq 'start')) {
1.53 www 729: $env{'form.'.&Apache::edit::html_element_name('xinitiallabel')}='';
730: }
1.54 www 731: if (($env{'form.'.&Apache::edit::html_element_name('xfinal')} ne '') && ($env{'form.'.&Apache::edit::html_element_name('xfinallabel')} eq 'end')) {
1.53 www 732: $env{'form.'.&Apache::edit::html_element_name('xfinallabel')}='';
733: }
1.29 www 734: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.35 www 735: $safeeval,'index','derivativeorder',
736: 'xinitial','xinitiallabel','xfinal','xfinallabel',
737: 'minimumlength','maximumlength',
738: 'relationship','value','percenterror');
1.29 www 739: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
1.13 www 740: }
1.12 www 741: return $result;
742: }
743:
1.25 www 744: sub end_functionplotrule {
1.13 www 745: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
746: my $result='';
747: if ($target eq 'edit') {
748: $result=&Apache::edit::end_table();
749: }
750: return $result;
1.12 www 751: }
752:
753:
754: #
1.75 www 755: # <functionplotvectorrule ... />
756: #
757: sub start_functionplotvectorrule {
758: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
759: my $result='';
760: my $label=&Apache::lonxml::get_param('index',$parstack,$safeeval);
761: $Apache::functionplotresponse::counter++;
762: if ($label=~/\W/) {
763: &Apache::lonxml::warning(&mt('Rule indices should only contain alphanumeric characters.'));
764: }
765: $label=~s/\W//gs;
766: unless ($label) {
1.79 www 767: $label='R'.$Apache::functionplotresponse::counter;
1.75 www 768: } else {
1.79 www 769: $label='R'.$label;
1.75 www 770: }
771:
772: if ($target eq 'grade') {
773: # Simply remember - in order - for later
1.77 www 774:
775: my $id=$Apache::inputtags::response[-1];
776: my $partid=$Apache::inputtags::part;
777: my $internalid = $partid.'_'.$id;
778:
1.75 www 779: my $vector=&Apache::lonxml::get_param('vector',$parstack,$safeeval);
780: $vector=~s/\W//gs;
781: $vector=ucfirst($vector);
1.76 www 782:
783: my $attachpoint=&Apache::lonxml::get_param('attachpoint',$parstack,$safeeval);
784: $attachpoint=~s/\W//gs;
785: $attachpoint=ucfirst($attachpoint);
786:
787: my $notattachpoint=&Apache::lonxml::get_param('notattachpoint',$parstack,$safeeval);
788: $notattachpoint=~s/\W//gs;
789: $notattachpoint=ucfirst($notattachpoint);
790:
1.75 www 791: my $tailpoint=&Apache::lonxml::get_param('tailpoint',$parstack,$safeeval);
792: $tailpoint=~s/\W//gs;
793: $tailpoint=ucfirst($tailpoint);
794: my $tippoint=&Apache::lonxml::get_param('tippoint',$parstack,$safeeval);
795: $tippoint=~s/\W//gs;
796: $tippoint=ucfirst($tippoint);
797:
798: my $nottailpoint=&Apache::lonxml::get_param('nottailpoint',$parstack,$safeeval);
799: $nottailpoint=~s/\W//gs;
800: $nottailpoint=ucfirst($nottailpoint);
801: my $nottippoint=&Apache::lonxml::get_param('nottippoint',$parstack,$safeeval);
802: $nottippoint=~s/\W//gs;
803: $nottippoint=ucfirst($nottippoint);
804:
805: push(@Apache::functionplotresponse::functionplotvectorrules,join(':',(
806: $label,
1.76 www 807: 'vector',
1.77 www 808: $internalid,
1.75 www 809: $vector,
1.76 www 810: $attachpoint,
811: $notattachpoint,
1.75 www 812: $tailpoint,
813: $tippoint,
814: $nottailpoint,
815: $nottippoint,
816: &Apache::lonxml::get_param('length',$parstack,$safeeval),
817: &Apache::lonxml::get_param('angle',$parstack,$safeeval),
1.79 www 818: &Apache::lonxml::get_param('lengtherror',$parstack,$safeeval),
819: &Apache::lonxml::get_param('angleerror',$parstack,$safeeval),
1.75 www 820: )));
821: } elsif ($target eq 'edit') {
822: $result=&Apache::edit::tag_start($target,$token,'Function Plot Vector Rule').
823: &Apache::edit::text_arg('Index/Name:','index',
824: $token,'10').' '.
825: &Apache::edit::text_arg('Vector:','vector',
826: $token,'16').'<br />'.
1.76 www 827: &Apache::edit::text_arg('Attached to object:','attachpoint',
828: $token,'16').
829: &Apache::edit::text_arg('Not attached to object:','notattachpoint',
830: $token,'16').'<br />'.
1.75 www 831: &Apache::edit::text_arg('Tail attached to object:','tailpoint',
832: $token,'16').
833: &Apache::edit::text_arg('Tip attached to object:','tippoint',
834: $token,'16').
835: &Apache::edit::text_arg('Tail not attached to object:','nottailpoint',
836: $token,'16').
837: &Apache::edit::text_arg('Tip not attached to object:','nottippoint',
838: $token,'16').'<br />'.
839: &Apache::edit::text_arg('Length:','length',
840: $token,'16').
841: &Apache::edit::text_arg('Angle:','angle',
842: $token,'16').
1.79 www 843: &Apache::edit::text_arg('Absolute error length:','lengtherror',
1.75 www 844: $token,'8').
1.79 www 845: &Apache::edit::text_arg('Absolute error angle:','angleerror',
1.75 www 846: $token,'8').
847: &Apache::edit::end_row();
848: } elsif ($target eq 'modified') {
1.76 www 849: $env{'form.'.&Apache::edit::html_element_name('vector')}=ucfirst($env{'form.'.&Apache::edit::html_element_name('vector')});
850: $env{'form.'.&Apache::edit::html_element_name('attachpoint')}=ucfirst($env{'form.'.&Apache::edit::html_element_name('attachpoint')});
851: $env{'form.'.&Apache::edit::html_element_name('notattachpoint')}=ucfirst($env{'form.'.&Apache::edit::html_element_name('notattachpoint')});
852: $env{'form.'.&Apache::edit::html_element_name('tailpoint')}=ucfirst($env{'form.'.&Apache::edit::html_element_name('tailpoint')});
853: $env{'form.'.&Apache::edit::html_element_name('tippoint')}=ucfirst($env{'form.'.&Apache::edit::html_element_name('tippoint')});
854: $env{'form.'.&Apache::edit::html_element_name('nottailpoint')}=ucfirst($env{'form.'.&Apache::edit::html_element_name('nottailpoint')});
855: $env{'form.'.&Apache::edit::html_element_name('nottippoint')}=ucfirst($env{'form.'.&Apache::edit::html_element_name('nottippoint')});
1.75 www 856: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.76 www 857: $safeeval,'index','vector','attachpoint','notattachpoint',
1.75 www 858: 'tailpoint','tippoint','nottailpoint','nottipoint',
859: 'length','angle',
1.79 www 860: 'lengtherror','angleerror');
1.75 www 861: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
862: }
863: return $result;
864: }
865:
866: sub end_functionplotvectorrule {
867: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
868: my $result='';
869: if ($target eq 'edit') {
870: $result=&Apache::edit::end_table();
871: }
872: return $result;
873: }
874:
1.76 www 875: #
876: # <functionplotvectorsumrule ... />
877: #
878: sub start_functionplotvectorsumrule {
879: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
880: my $result='';
881: my $label=&Apache::lonxml::get_param('index',$parstack,$safeeval);
882: $Apache::functionplotresponse::counter++;
883: if ($label=~/\W/) {
884: &Apache::lonxml::warning(&mt('Rule indices should only contain alphanumeric characters.'));
885: }
886: $label=~s/\W//gs;
887: unless ($label) {
1.79 www 888: $label='R'.$Apache::functionplotresponse::counter;
1.76 www 889: } else {
1.79 www 890: $label='R'.$label;
1.76 www 891: }
892: if ($target eq 'grade') {
893: # Simply remember - in order - for later
1.77 www 894: my $id=$Apache::inputtags::response[-1];
895: my $partid=$Apache::inputtags::part;
896: my $internalid = $partid.'_'.$id;
1.82 www 897: my $vectors=&Apache::lonxml::get_param('vectors',$parstack,$safeeval);
1.76 www 898: push(@Apache::functionplotresponse::functionplotvectorrules,join(':',(
899: $label,
900: 'sum',
1.77 www 901: $internalid,
1.82 www 902: $vectors,
1.76 www 903: &Apache::lonxml::get_param('length',$parstack,$safeeval),
904: &Apache::lonxml::get_param('angle',$parstack,$safeeval),
1.80 www 905: &Apache::lonxml::get_param('lengtherror',$parstack,$safeeval),
1.79 www 906: &Apache::lonxml::get_param('angleerror',$parstack,$safeeval),
1.76 www 907: )));
908: } elsif ($target eq 'edit') {
909: $result=&Apache::edit::tag_start($target,$token,'Function Plot Vector Sum Rule').
910: &Apache::edit::text_arg('Index/Name:','index',
911: $token,'10').' '.
1.82 www 912: &Apache::edit::text_arg('Comma-separated list of vectors:','vectors',
913: $token,'30').'<br />'.
1.76 www 914: &Apache::edit::text_arg('Sum vector length:','length',
915: $token,'16').
916: &Apache::edit::text_arg('Sum vector angle:','angle',
917: $token,'16').
1.80 www 918: &Apache::edit::text_arg('Error length:','lengtherror',
1.76 www 919: $token,'8').
1.79 www 920: &Apache::edit::text_arg('Error angle:','angleerror',
1.76 www 921: $token,'8').
922: &Apache::edit::end_row();
923: } elsif ($target eq 'modified') {
924: $env{'form.'.&Apache::edit::html_element_name('object')}=ucfirst($env{'form.'.&Apache::edit::html_element_name('object')});
925: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.82 www 926: $safeeval,'index','vectors',
1.76 www 927: 'length','angle',
1.80 www 928: ,'lengtherror','angleerror');
1.76 www 929: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
930: }
931: return $result;
932: }
1.75 www 933:
1.76 www 934: sub end_functionplotvectorsumrule {
935: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
936: my $result='';
937: if ($target eq 'edit') {
938: $result=&Apache::edit::end_table();
939: }
940: return $result;
941: }
1.75 www 942:
943: #
1.23 www 944: # <spline index="..." order="1,2,3,4" initx="..." inity="..." scalex="..." scaley="..." />
1.6 www 945: #
946: # Unfortunately, GeoGebra seems to want all splines after everything else, so we need to store them
947: #
948: sub start_spline {
949: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.13 www 950: my $result='';
951: if ($target eq 'web') {
1.23 www 952: my $label=&Apache::lonxml::get_param('index',$parstack,$safeeval);
1.13 www 953: $Apache::functionplotresponse::counter++;
954: if ($label=~/\W/) {
1.23 www 955: &Apache::lonxml::warning(&mt('Spline indices should only contain alphanumeric characters.'));
1.13 www 956: }
957: $label=~s/\W//gs;
1.23 www 958: unless ($label) {
959: $label='S'.$Apache::functionplotresponse::counter;
960: } else {
961: $label='S'.$label;
962: }
1.13 www 963: if ($Apache::functionplotresponse::splineorder{$label}) {
1.23 www 964: &Apache::lonxml::error(&mt('Spline indices must be unique.'));
1.13 www 965: }
966:
967: my $order=&Apache::lonxml::get_param('order',$parstack,$safeeval);
968: if ($order<2) { $order=2; }
969: if ($order>8) { $order=8; }
970: $Apache::functionplotresponse::splineorder{$label}=$order;
971:
972: my $x=&Apache::lonxml::get_param('initx',$parstack,$safeeval);
973: unless ($x) { $x=0; }
974: $Apache::functionplotresponse::splineinitx{$label}=$x;
975:
976: my $y=&Apache::lonxml::get_param('inity',$parstack,$safeeval);
977: unless ($y) { $y=0; }
978: $Apache::functionplotresponse::splineinity{$label}=$y;
979:
980: my $sx=&Apache::lonxml::get_param('scalex',$parstack,$safeeval);
981: unless ($sx) { $sx=$order; }
982: $Apache::functionplotresponse::splinescalex{$label}=$sx;
983:
984: my $sy=&Apache::lonxml::get_param('scaley',$parstack,$safeeval);
985: unless ($sy) { $sy=2; }
986: $Apache::functionplotresponse::splinescaley{$label}=$sy;
987: } elsif ($target eq 'edit') {
988: $result=&Apache::edit::tag_start($target,$token,'Spline').
1.23 www 989: &Apache::edit::text_arg('Index:','index',
1.14 www 990: $token,'4').' '.
1.13 www 991: &Apache::edit::select_arg('Order:','order',
1.44 www 992: ['2','3','4','5','6','7','8'],$token).' '.
1.13 www 993: &Apache::edit::text_arg('Initial x-value:','initx',
1.14 www 994: $token,'4').' '.
1.13 www 995: &Apache::edit::text_arg('Initial y-value:','inity',
1.14 www 996: $token,'4').' '.
1.13 www 997: &Apache::edit::text_arg('Scale x:','scalex',
1.14 www 998: $token,'4').' '.
1.13 www 999: &Apache::edit::text_arg('Scale y:','scaley',
1000: $token,'4').
1001: &Apache::edit::end_row();
1002: } elsif ($target eq 'modified') {
1003: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.23 www 1004: $safeeval,'index','order','initx','inity',
1.13 www 1005: 'scalex','scaley');
1006: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
1007: }
1008: return $result;
1.6 www 1009: }
1010:
1011: sub end_spline {
1.13 www 1012: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1013: my $result='';
1014: if ($target eq 'edit') {
1015: $result=&Apache::edit::end_table();
1016: }
1017: return $result;
1.4 www 1018: }
1019:
1.3 www 1020: sub end_init_script {
1021: return (<<ENDENDINIT);
1022: }
1023: // ]]>
1024: </script>
1025: ENDENDINIT
1026: }
1027:
1.10 www 1028: #
1029: # Storing and restoring spline coordinates from part answers
1030: #
1031: sub decode_previous_answer {
1032: my ($answer)=@_;
1033: foreach my $coordinate (split(/\,/,$answer)) {
1034: my ($key,$value)=split(/\=/,$coordinate);
1035: $Apache::functionplotresponse::previous{$key}=$value;
1036: }
1037: }
1038:
1039: sub get_answer_from_form_fields {
1040: my ($id)=@_;
1041: my $answer='';
1042: my %coords=();
1043: foreach my $field (keys(%env)) {
1044: if ($field=~/^form\.HWVAL\_$id/) {
1045: $field=~/^form\.(.*)$/;
1046: $coords{$1}=$env{$field};
1047: }
1048: }
1049: $answer=join(',',map { $_.'='.$coords{$_} } (sort(keys(%coords))));
1050: return ($answer,%coords);
1051: }
1052:
1.15 www 1053: #
1054: # The following functions calculate the cubic-hermite splines server-side
1055: #
1056:
1057: sub cubic_hermite {
1058: my ($t,$p1,$s1,$p2,$s2)=@_;
1.42 www 1059: return (2.*$t*$t*$t-3.*$t*$t+1.)*$p1 + 3.*($t*$t*$t-2.*$t*$t+$t)*($s1-$p1)+
1060: (-2.*$t*$t*$t+3.*$t*$t) *$p2 + 3.*($t*$t*$t-$t*$t) *($s2-$p2);
1.15 www 1061: }
1062:
1063: #
1064: # d/dt(...)
1065: #
1066:
1067: sub ddt_cubic_hermite {
1068: my ($t,$p1,$s1,$p2,$s2)=@_;
1.42 www 1069: return (6.*$t*$t-6.*$t) *$p1 + 3.*(3.*$t*$t-4.*$t+1.)*($s1-$p1)+
1070: (-6.*$t*$t+6.*$t)*$p2 + 3.*(3.*$t*$t-2.*$t) *($s2-$p2);
1.15 www 1071: }
1072:
1073: #
1074: # d^2/dt^2(...)
1075: #
1076:
1077: sub d2dt2_cubic_hermite {
1078: my ($t,$p1,$s1,$p2,$s2)=@_;
1.42 www 1079: return (12.*$t-6.) *$p1 + 3.*(6.*$t-4.)*($s1-$p1)+
1080: (-12.*$t+6.)*$p2 + 3.*(6.*$t-2.)*($s2-$p2);
1.15 www 1081: }
1082:
1083: #
1.18 www 1084: # Array index calculation
1085: #
1086: sub array_index {
1087: my ($xmin,$xmax,$x)=@_;
1.38 www 1088: if ($x ne '') {
1089: return int(($x-$xmin)/($xmax-$xmin)*400.+0.5);
1090: } else {
1091: return undef;
1092: }
1.20 www 1093: }
1094:
1095: #
1.18 www 1096: # Populate the arrays
1097: #
1098:
1099: sub populate_arrays {
1.64 www 1100: my ($id,$xmin,$xmax,$ymin,$ymax)=@_;
1.26 www 1101: for (my $i=0; $i<=400; $i++) {
1.35 www 1102: $Apache::functionplotresponse::actualxval[$i]=undef;
1.19 www 1103: $Apache::functionplotresponse::func[$i]=undef;
1.22 www 1104: $Apache::functionplotresponse::dfuncdx[$i]=undef;
1.19 www 1105: $Apache::functionplotresponse::d2funcd2x[$i]=undef;
1.18 www 1106: }
1.19 www 1107: unless ($xmax>$xmin) { return 'no_func'; }
1.18 www 1108: # Run over all splines in response
1109: foreach my $label (split(/\,/,$env{"form.HWVAL_AllSplines_$id"})) {
1.19 www 1110: my $xiold=-1;
1.18 www 1111: # Run over all points in spline
1.19 www 1112: for (my $i=1; $i<$env{"form.HWVAL_SplineOrder_".$id."_".$label}; $i++) {
1.18 www 1113: my $ni=$i+1;
1114: my @xparms=($env{'form.HWVAL_'.$id.'_'.$label.'P'.$i.'_x'},
1115: $env{'form.HWVAL_'.$id.'_'.$label.'S'.$i.'_x'},
1116: $env{'form.HWVAL_'.$id.'_'.$label.'P'.$ni.'_x'},
1117: $env{'form.HWVAL_'.$id.'_'.$label.'S'.$ni.'_x'});
1118: my @yparms=($env{'form.HWVAL_'.$id.'_'.$label.'P'.$i.'_y'},
1119: $env{'form.HWVAL_'.$id.'_'.$label.'S'.$i.'_y'},
1120: $env{'form.HWVAL_'.$id.'_'.$label.'P'.$ni.'_y'},
1121: $env{'form.HWVAL_'.$id.'_'.$label.'S'.$ni.'_y'});
1122: # Run in small steps over spline parameter
1.26 www 1123: for (my $t=0; $t<=1; $t+=0.0001) {
1.35 www 1124: my $xreal=&cubic_hermite($t,@xparms);
1125: my $xi=&array_index($xmin,$xmax,$xreal);
1.19 www 1126: if ($xi<$xiold) { return 'no_func'; }
1.26 www 1127: if (($xi>$xiold) && ($xi>=0) && ($xi<=400)) {
1.18 www 1128: $xiold=$xi;
1.35 www 1129: $Apache::functionplotresponse::actualxval[$xi]=$xreal;
1.22 www 1130: # Function value
1.30 www 1131: my $funcval=&cubic_hermite($t,@yparms);
1.64 www 1132:
1133: # Do we already have a value for this point, and is it different from the new one?
1134: if ((defined($Apache::functionplotresponse::func[$xi])) &&
1135: (abs($Apache::functionplotresponse::func[$xi]-$funcval)>($ymax-$ymin)/100.)) {
1136: return 'no_func';
1137: }
1138: # Okay, remember the new point
1.30 www 1139: $Apache::functionplotresponse::func[$xi]=$funcval;
1.64 www 1140:
1.30 www 1141: if (defined($funcval)) {
1142: if ($xi<$Apache::functionplotresponse::functionplotrulelabels{'start'}) {
1143: $Apache::functionplotresponse::functionplotrulelabels{'start'}=$xi;
1144: }
1145: if ($xi>$Apache::functionplotresponse::functionplotrulelabels{'end'}) {
1146: $Apache::functionplotresponse::functionplotrulelabels{'end'}=$xi;
1147: }
1148: }
1.28 www 1149: # Chain rule
1.22 www 1150: # dy/dx=dy/dt/(dx/dt)
1151: my $dxdt=&ddt_cubic_hermite($t,@xparms);
1152: if ($dxdt) {
1153: $Apache::functionplotresponse::dfuncdx[$xi]=&ddt_cubic_hermite($t,@yparms)/$dxdt;
1.34 www 1154: # Second derivative
1.28 www 1155: $Apache::functionplotresponse::d2funcdx2[$xi]=
1.34 www 1156: ($dxdt*&d2dt2_cubic_hermite($t,@yparms)-&ddt_cubic_hermite($t,@yparms)*&d2dt2_cubic_hermite($t,@xparms))/
1157: ($dxdt*$dxdt*$dxdt);
1.22 www 1158: }
1.18 www 1159: }
1160: }
1161: }
1162: }
1163: }
1164:
1165: #
1.37 www 1166: # Implementation of <functionplotresponse>
1.15 www 1167: #
1168:
1.1 www 1169: sub start_functionplotresponse {
1170: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1171: my $result='';
1.6 www 1172: # To remember the splines - somehow, they need to come last
1173: undef %Apache::functionplotresponse::splineorder;
1174: undef %Apache::functionplotresponse::splineinitx;
1175: undef %Apache::functionplotresponse::splineinity;
1176: undef %Apache::functionplotresponse::splinescalex;
1177: undef %Apache::functionplotresponse::splinescaley;
1.10 www 1178: # Remember input fields, etc
1179: undef %Apache::functionplotresponse::previous;
1.9 www 1180: $Apache::functionplotresponse::inputfields='';
1.6 www 1181: $Apache::functionplotresponse::counter=0;
1.24 www 1182: # Remember rules
1.25 www 1183: undef @Apache::functionplotresponse::functionplotrules;
1.75 www 1184: undef @Apache::functionplotresponse::functionplotvectorrules;
1.37 www 1185: # Remember failed rules
1186: if ($target eq 'grade') {
1187: undef @Apache::functionplotresponse::failedrules;
1188: }
1189: # Delete previous awards
1190: undef $Apache::functionplotresponse::awarddetail;
1.10 www 1191: # Part and ID
1192: my $partid=$Apache::inputtags::part;
1193: my $id=&Apache::response::start_response($parstack,$safeeval);
1.6 www 1194: # Internal ID to mark the applet and its coordinates
1.10 www 1195: my $internalid = $partid.'_'.$id;
1196: # Previous answer
1197: &decode_previous_answer($Apache::lonhomework::history{"resource.$partid.$id.submission"});
1198:
1.6 www 1199: # Parameters of <functionplotresponse>
1.41 www 1200: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval);
1.6 www 1201: my $xaxisvisible=(&Apache::lonxml::get_param('xaxisvisible',$parstack,$safeeval)=~/on|true|yes|1/i?'true':'false');
1202: my $yaxisvisible=(&Apache::lonxml::get_param('yaxisvisible',$parstack,$safeeval)=~/on|true|yes|1/i?'true':'false');
1203: my $gridvisible=(&Apache::lonxml::get_param('gridvisible',$parstack,$safeeval)=~/on|true|yes|1/i?'true':'false');
1.14 www 1204: my $xlabel=&Apache::lonxml::get_param('xlabel',$parstack,$safeeval);
1205: my $ylabel=&Apache::lonxml::get_param('ylabel',$parstack,$safeeval);
1.12 www 1206: if ($target eq 'edit') {
1207: $result.=&Apache::edit::start_table($token)
1208: .'<tr><td><span class="LC_nobreak">'.&mt('Function Plot Question').'</span></td>'
1209: .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
1.37 www 1210: .&Apache::edit::deletelist($target,$token).' '
1.50 www 1211: .&Apache::edit::insertlist($target,$token).' '
1212: .&Apache::loncommon::help_open_topic('Function_Plot_Response_Question','Function Plot Responses')
1.12 www 1213: .'</span></td>'
1214: ."<td> "
1215: .&Apache::edit::end_row()
1216: .&Apache::edit::start_spanning_row()
1217: ."\n";
1.14 www 1218: $result.=&Apache::edit::text_arg('Label x-axis:','xlabel',
1219: $token,'6').' '.
1220: &Apache::edit::text_arg('Minimum x-value:','xmin',
1221: $token,'4').' '.
1.12 www 1222: &Apache::edit::text_arg('Maximum x-value:','xmax',
1.14 www 1223: $token,'4').' '.
1.12 www 1224: &Apache::edit::select_arg('x-axis visible:','xaxisvisible',
1.32 www 1225: ['yes','no'],$token).'<br />'.
1.14 www 1226: &Apache::edit::text_arg('Label y-axis:','ylabel',
1227: $token,'6').' '.
1.12 www 1228: &Apache::edit::text_arg('Minimum y-value:','ymin',
1.14 www 1229: $token,'4').' '.
1.12 www 1230: &Apache::edit::text_arg('Maximum y-value:','ymax',
1.14 www 1231: $token,'4').' '.
1.12 www 1232: &Apache::edit::select_arg('y-axis visible:','yaxisvisible',
1.32 www 1233: ['yes','no'],$token).'<br />'.
1.12 www 1234: &Apache::edit::select_arg('Grid visible:','gridvisible',
1.47 www 1235: ['yes','no'],$token).'<br />'.
1.58 www 1236: &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 1237: 'answerdisplay',$token,'50').
1.12 www 1238: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
1239: } elsif ($target eq 'modified') {
1240: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.14 www 1241: $safeeval,'xlabel','xmin','xmax','ylabel','ymin','ymax',
1.47 www 1242: 'xaxisvisible','yaxisvisible','gridvisible','answerdisplay');
1.12 www 1243: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.3 www 1244:
1.12 www 1245: } elsif ($target eq 'meta') {
1.10 www 1246: $result=&Apache::response::meta_package_write('functionplotresponse');
1.35 www 1247: } elsif (($target eq 'answer') &&
1248: ($env{'form.answer_output_mode'} ne 'tex') &&
1249: ($Apache::lonhomework::viewgrades == 'F')) {
1250: my (undef,undef,$udom,$uname)=&Apache::lonnet::whichuser();
1251: my $windowopen=&Apache::lonhtmlcommon::javascript_docopen();
1252: my $start_page = &Apache::loncommon::start_page('Rules Log', undef,
1253: {'only_body' => 1,
1254: 'bgcolor' => '#FFFFFF',
1255: 'js_ready' => 1,});
1256: my $end_page = &Apache::loncommon::end_page({'js_ready' => 1,});
1257: $uname =~s/\W//g;
1258: $udom =~s/\W//g;
1259: my $function_name =
1260: join('_','LONCAPA_scriptvars',$uname,$udom,
1261: $env{'form.counter'},$Apache::lonxml::curdepth);
1262: my $rules_var ="<script type=\"text/javascript\">
1263: // <![CDATA[
1264: function $function_name() {newWindow=open('','new_W','width=500,height=500,scrollbars=1,resizable=yes');newWindow.$windowopen;newWindow.document.writeln('$start_page<pre>".
1265: $Apache::functionplotresponse::ruleslog.
1266: "<\\/pre>$end_page');newWindow.document.close();newWindow.focus()}
1267: // ]]>
1268: </script><a href=\"javascript:$function_name();void(0);\">".&mt('Rules Log')."</a><br />";
1269: &Apache::lonxml::add_script_result($rules_var);
1270: }
1271:
1.1 www 1272: return $result;
1273: }
1274:
1.25 www 1275: sub compare_rel {
1.26 www 1276: my ($relationship,$value,$realval,$tol)=@_;
1.36 www 1277: # is the real value undefined?
1.26 www 1278: unless (defined($realval)) {
1.36 www 1279: # the real value is not defined
1.26 www 1280: if ($relationship eq 'eq') {
1281: if ($value eq 'undef') {
1282: return 1;
1283: } else {
1284: return 0;
1285: }
1286: } elsif ($relationship eq 'ne') {
1287: if ($value eq 'undef') {
1288: return 0;
1289: } else {
1290: return 1;
1291: }
1292: } else {
1293: return 0;
1294: }
1295: }
1296:
1.36 www 1297: # is the expected value undefined?
1298: if ($value eq 'undef') {
1299: # but by now we know that the real value is defined
1300: return 0;
1301: }
1302:
1303: # both are defined.
1.26 www 1304: if ($relationship eq 'gt') {
1305: return ($realval>$value);
1306: } elsif ($relationship eq 'ge') {
1307: return ($realval>$value-$tol);
1308: } elsif ($relationship eq 'lt') {
1309: return ($realval<$value);
1310: } elsif ($relationship eq 'le') {
1311: return ($realval<$value+$tol);
1312: } elsif ($relationship eq 'ne') {
1313: return (abs($value-$realval)>$tol);
1314: } else {
1315: return (abs($value-$realval)<$tol);
1316: }
1.25 www 1317: return 0;
1318: }
1319:
1.35 www 1320: sub addlog {
1321: my ($text)=@_;
1.67 www 1322: $text=~s/\'/\\\'/g;
1.35 www 1323: $Apache::functionplotresponse::ruleslog.=$text.'<br />';
1324: }
1325:
1326: sub actualval {
1327: my ($i,$xmin,$xmax)=@_;
1328: return $xmin+$i/400.*($xmax-$xmin);
1329: }
1.65 www 1330:
1.66 www 1331: sub fpr_val {
1332: my ($arg)=@_;
1333: return &actualval($Apache::functionplotresponse::functionplotrulelabels{$arg},
1334: $Apache::functionplotresponse::fpr_xmin,
1335: $Apache::functionplotresponse::fpr_xmax);
1336: }
1337:
1.65 www 1338: sub fpr_f {
1339: my ($arg)=@_;
1340: return $Apache::functionplotresponse::func[&array_index($Apache::functionplotresponse::fpr_xmin,
1341: $Apache::functionplotresponse::fpr_xmax,
1342: $arg)];
1343: }
1344:
1345: sub fpr_dfdx {
1346: my ($arg)=@_;
1347: return $Apache::functionplotresponse::dfuncdx[&array_index($Apache::functionplotresponse::fpr_xmin,
1348: $Apache::functionplotresponse::fpr_xmax,
1349: $arg)];
1350: }
1351:
1352: sub fpr_d2fdx2 {
1353: my ($arg)=@_;
1354: return $Apache::functionplotresponse::d2funcdx2[&array_index($Apache::functionplotresponse::fpr_xmin,
1355: $Apache::functionplotresponse::fpr_xmax,
1356: $arg)];
1357: }
1.77 www 1358:
1359:
1360: sub vectorcoords {
1361: my ($id,$label)=@_;
1362: return ($env{'form.HWVAL_'.$id.'_'.$label.'Start_x'},
1363: $env{'form.HWVAL_'.$id.'_'.$label.'End_x'},
1364: $env{'form.HWVAL_'.$id.'_'.$label.'Start_y'},
1365: $env{'form.HWVAL_'.$id.'_'.$label.'End_y'});
1366: }
1367:
1368: sub objectcoords {
1369: my ($id,$label)=@_;
1370: return ($env{'form.HWVAL_'.$id.'_'.$label.'_x'},
1371: $env{'form.HWVAL_'.$id.'_'.$label.'_y'});
1372: }
1.81 www 1373:
1374: sub attached {
1375: my ($id,$vector,$object,$xmin,$xmax,$ymin,$ymax)=@_;
1376: my ($xs,$xe,$ys,$ye)=&vectorcoords($id,$vector);
1377: my ($xo,$yo)=&objectcoords($id,$object);
1378: my $tolx=($xmax-$xmin)/100.;
1379: my $toly=($ymax-$ymin)/100.;
1380: my $tail=0;
1381: my $tip=0;
1382: &addlog("Proximity $vector ($xs,$xe,$ys,$ye) to $object ($xo,$yo)");
1383: if ((abs($xs-$xo)<$tolx) && (abs($ys-$yo)<$toly)) { $tail=1; }
1384: if ((abs($xe-$xo)<$tolx) && (abs($ye-$yo)<$toly)) { $tip=1; }
1385: &addlog("Result tail:$tail tip:$tip");
1386: return($tail,$tip);
1387: }
1388:
1.77 www 1389:
1390: sub vectorangle {
1391: my ($x,$y)=@_;
1.78 www 1392: my $angle=57.2957795*atan2($y,$x);
1393: if ($angle<0) { $angle=360+$angle; }
1394: return $angle;
1.77 www 1395: }
1396:
1397: sub vectorlength {
1398: my ($x,$y)=@_;
1399: return sqrt($x*$x+$y*$y);
1400: }
1401:
1.78 www 1402: sub relvector {
1403: my ($xs,$xe,$ys,$ye)=@_;
1404: return ($xe-$xs,$ye-$ys);
1405: }
1406:
1407: sub plotvectorlength {
1408: return &vectorlength(&relvector(&vectorcoords(@_)));
1409: }
1410:
1411: sub plotvectorangle {
1412: return &vectorangle(&relvector(&vectorcoords(@_)));
1413: }
1414:
1.77 www 1415:
1416: #
1417: # Evaluate a functionplotvectorrule
1418: #
1419:
1420: sub functionplotvectorrulecheck {
1421: my ($rule,$xmin,$xmax,$ymin,$ymax,$safeeval)=@_;
1.78 www 1422: &addlog("=================");
1.77 www 1423: my ($label,$type)=split(/\:/,$rule);
1424: if ($type eq 'vector') {
1425: return &vectorcheck($rule,$xmin,$xmax,$ymin,$ymax,$safeeval);
1426: } elsif ($type eq 'sum') {
1427: return &sumcheck($rule,$xmin,$xmax,$ymin,$ymax,$safeeval);
1428: }
1429: }
1430:
1431: sub vectorcheck {
1432: my ($rule,$xmin,$xmax,$ymin,$ymax,$safeeval)=@_;
1433: my ($label,$type,$id,$vector,
1434: $attachpoint,$notattachpoint,
1435: $tailpoint,$tippoint,$nottailpoint,$nottippoint,
1.79 www 1436: $length,$angle,$lengtherror,$angleerror)=split(/\:/,$rule);
1.78 www 1437: &addlog("Vector Rule $label for vector ".$vector);
1.79 www 1438: if ($length ne '') {
1439: &addlog("Checking for length $length with error $lengtherror");
1440: $length=&Apache::run::run($length,$safeeval);
1441: &addlog("Length evaluated to $length");
1442: my $thislength=&plotvectorlength($id,$vector);
1443: &addlog("Found length $thislength");
1444: if (abs($thislength-$length)>$lengtherror) {
1445: &setfailed($label);
1446: return 0;
1447: }
1448: }
1.80 www 1449: if ($angle ne '') {
1450: &addlog("Checking for angle $angle with error $angleerror");
1451: $angle=&Apache::run::run($angle,$safeeval);
1452: &addlog("Angle evaluated to $angle");
1453: my $thisangle=&plotvectorangle($id,$vector);
1454: &addlog("Found angle $thisangle");
1455: my $anglediff=abs($thisangle-$angle);
1456: &addlog("Angle difference: $anglediff");
1457: if ($anglediff>360.-$anglediff) {
1458: $anglediff=360.-$anglediff;
1459: }
1460: &addlog("Smallest angle difference: $anglediff");
1461: if ($anglediff>$angleerror) {
1462: &setfailed($label);
1463: return 0;
1464: }
1465: }
1.81 www 1466: if ($attachpoint ne '') {
1467: &addlog("Checking attached: ".$attachpoint);
1468: my ($tail,$tip)=&attached($id,$vector,$attachpoint,$xmin,$xmax,$ymin,$ymax);
1469: unless ($tail || $tip) {
1470: &setfailed($label);
1471: return 0;
1472: }
1473: }
1474: if ($notattachpoint ne '') {
1475: &addlog("Checking not attached: ".$notattachpoint);
1476: my ($tail,$tip)=&attached($id,$vector,$notattachpoint,$xmin,$xmax,$ymin,$ymax);
1477: if ($tail || $tip) {
1478: &setfailed($label);
1479: return 0;
1480: }
1481: }
1482: if ($tailpoint ne '') {
1483: &addlog("Checking tail: ".$tailpoint);
1484: my ($tail,$tip)=&attached($id,$vector,$tailpoint,$xmin,$xmax,$ymin,$ymax);
1485: unless ($tail) {
1486: &setfailed($label);
1487: return 0;
1488: }
1489: }
1490: if ($nottailpoint ne '') {
1491: &addlog("Checking not tail: ".$nottailpoint);
1492: my ($tail,$tip)=&attached($id,$vector,$nottailpoint,$xmin,$xmax,$ymin,$ymax);
1493: if ($tail) {
1494: &setfailed($label);
1495: return 0;
1496: }
1497: }
1498: if ($tippoint ne '') {
1499: &addlog("Checking tip: ".$tippoint);
1500: my ($tail,$tip)=&attached($id,$vector,$tippoint,$xmin,$xmax,$ymin,$ymax);
1501: unless ($tip) {
1502: &setfailed($label);
1503: return 0;
1504: }
1505: }
1506: if ($nottippoint ne '') {
1507: &addlog("Checking not tip: ".$nottippoint);
1508: my ($tail,$tip)=&attached($id,$vector,$nottippoint,$xmin,$xmax,$ymin,$ymax);
1509: if ($tip) {
1510: &setfailed($label);
1511: return 0;
1512: }
1513: }
1514:
1.79 www 1515: &addlog("Rule $label passed.");
1516: return 1;
1.77 www 1517: }
1518:
1519: sub sumcheck {
1520: my ($rule,$xmin,$xmax,$ymin,$ymax,$safeeval)=@_;
1.82 www 1521: my ($label,$type,$id,$vectors,$length,$angle,$lengtherror,$angleerror)=split(/\:/,$rule);
1522: &addlog("Vector Sum Rule $label for vectors ".$vectors);
1523: my $sumx=0;
1524: my $sumy=0;
1525: foreach my $sv (split(/\s*\,\s*/,$vectors)) {
1526: my ($rx,$ry)=&relvector(&vectorcoords($id,$sv));
1527: $sumx+=$rx;
1528: $sumy+=$ry;
1529: }
1530: &addlog("Sum vector ($sumx,$sumy)");
1.83 ! www 1531: if ($length ne '') {
! 1532: &addlog("Checking length $length with error $lengtherror");
! 1533: $length=&Apache::run::run($length,$safeeval);
! 1534: &addlog("Evaluated to $length");
! 1535: my $thislength=&vectorlength($sumx,$sumy);
! 1536: &addlog("Actual length $thislength");
! 1537: if (abs($length-$thislength)>$lengtherror) {
! 1538: &setfailed($label);
! 1539: return 0;
! 1540: }
! 1541: }
! 1542: if ($angle ne '') {
! 1543: &addlog("Checking angle $angle with error $angleerror");
! 1544: $angle=&Apache::run::run($angle,$safeeval);
! 1545: &addlog("Evaluated to $angle");
! 1546: my $thisangle=&vectorangle($sumx,$sumy);
! 1547: &addlog("Actual angle $thisangle");
! 1548: my $anglediff=abs($thisangle-$angle);
! 1549: &addlog("Angle difference: $anglediff");
! 1550: if ($anglediff>360.-$anglediff) {
! 1551: $anglediff=360.-$anglediff;
! 1552: }
! 1553: &addlog("Smallest angle difference: $anglediff");
! 1554: if ($anglediff>$angleerror) {
! 1555: &setfailed($label);
! 1556: return 0;
! 1557: }
! 1558: }
1.79 www 1559: &addlog("Rule $label passed.");
1560: return 1;
1.77 www 1561: }
1562:
1563: #
1564: # Evaluate a functionplotrule
1565: #
1.35 www 1566:
1.25 www 1567: sub functionplotrulecheck {
1.65 www 1568: my ($rule,$xmin,$xmax,$ymin,$ymax,$safeeval)=@_;
1.33 www 1569:
1.35 www 1570: my ($label,$derivative,$xinitial,$xinitiallabel,$xfinal,$xfinallabel,$minimumlength,$maximumlength,$relationship,$value,$percent)
1.25 www 1571: =split(/\:/,$rule);
1.33 www 1572: $percent=($percent>0?$percent:5);
1.35 www 1573: &addlog("=================");
1.42 www 1574: &addlog("Rule $label for ".($derivative<0?'integral':('function itself','first derivative','second derivative')[$derivative])." $relationship $value");
1.65 www 1575: #
1576: # Evaluate the value
1577: #
1578: if ($value=~/\D/) {
1579: $Apache::functionplotresponse::fpr_xmin=$xmin;
1580: $Apache::functionplotresponse::fpr_xmax=$xmax;
1581: $value=&Apache::run::run($value,$safeeval);
1582: &addlog("Value evaluated to $value");
1583: }
1584:
1585: #
1586: # Minimum and maximum lengths of the interval
1587: #
1.49 www 1588: if ((defined($minimumlength)) || (defined($maximumlength))) {
1589: &addlog("Minimumlength $minimumlength Maximumlength $maximumlength");
1590: }
1.30 www 1591: my $li=0;
1592: my $lh=400;
1593:
1.31 www 1594: # Special case: the upper boundary was not defined
1595: # and needs to be set to the value where
1596: # the condition is not true anymore => set flag
1597:
1598: my $findupper=0;
1599: if (($xfinal eq '')
1600: && (!defined($Apache::functionplotresponse::functionplotrulelabels{$xfinallabel}))
1601: && ($xfinallabel)) {
1602: $findupper=1;
1603: }
1604:
1.25 www 1605: # if a hard value is set for the boundaries, it overrides the label
1.30 www 1606: if (($xinitial ne '') && ($xinitiallabel ne '') && ($xinitiallabel ne 'start')) {
1607: $li=&array_index($xmin,$xmax,$xinitial);
1608: $Apache::functionplotresponse::functionplotrulelabels{$xinitiallabel}=$li;
1609: }
1610: if (($xfinal ne '') && ($xfinallabel ne '') && ($xfinallabel ne 'end')) {
1611: $lh=&array_index($xmin,$xmax,$xfinal);
1612: $Apache::functionplotresponse::functionplotrulelabels{$xfinallabel}=$lh;
1.25 www 1613: }
1.31 www 1614: # if the label is defined, use it
1.25 www 1615: if (defined($Apache::functionplotresponse::functionplotrulelabels{$xinitiallabel})) {
1.35 www 1616: &addlog("Using lower label $xinitiallabel");
1.30 www 1617: $li=$Apache::functionplotresponse::functionplotrulelabels{$xinitiallabel};
1618: } else {
1619: $li=&array_index($xmin,$xmax,$xinitial);
1.25 www 1620: }
1.31 www 1621: unless ($findupper) {
1622: if (defined($Apache::functionplotresponse::functionplotrulelabels{$xfinallabel})) {
1.35 www 1623: &addlog("Using upper label $xfinallabel");
1.33 www 1624: $lh=$Apache::functionplotresponse::functionplotrulelabels{$xfinallabel}-1;
1.31 www 1625: } else {
1626: $lh=&array_index($xmin,$xmax,$xfinal);
1627: }
1.25 www 1628: }
1629: # Basic sanity checks
1.30 www 1630: if ($li<0) { $li=0; }
1631: if ($lh>400) { $lh=400; }
1.38 www 1632: if (($li>$lh) || (!defined($lh))) {
1.30 www 1633: $lh=$li;
1.25 www 1634: }
1.31 www 1635:
1.35 www 1636: &addlog("Boundaries: x=".&actualval($li,$xmin,$xmax)." (".$Apache::functionplotresponse::actualxval[$li]."; index $li)) to x=".
1637: &actualval($lh,$xmin,$xmax)." (".$Apache::functionplotresponse::actualxval[$lh]."; index $lh))");
1638: if ($findupper) {
1639: &addlog("Looking for label $xfinallabel");
1640: }
1.33 www 1641: my $tol=$percent*($ymax-$ymin)/100;
1642: if ($xmax>$xmin) {
1643: if ($derivative==2) {
1644: $tol=4.*$tol/($xmax-$xmin);
1645: } elsif ($derivative==1) {
1646: $tol=2.*$tol/($xmax-$xmin);
1.42 www 1647: } elsif ($derivative==-1) {
1648: $tol=$tol*($xmax-$xmin)/2.;
1.33 www 1649: }
1.27 www 1650: }
1.42 www 1651: my $integral=0;
1652: my $binwidth=($xmax-$xmin)/400.;
1653: if (($derivative<0) && (!$findupper)) {
1654: # definite integral, calculate over whole length
1655: &addlog("Calculating definite integral");
1656: for (my $i=$li; $i<=$lh; $i++) {
1657: $integral+=$Apache::functionplotresponse::func[$i]*$binwidth;
1658: }
1659: unless (&compare_rel($relationship,$value,$integral,$tol)) {
1660: &addlog("Actual integral ".(defined($integral)?$integral:'undef').", expected $value, tolerance $tol");
1661: &addlog("Rule $label failed.");
1.79 www 1662: &setfailed($label);
1.42 www 1663: return 0;
1664: }
1665: } else {
1666: for (my $i=$li; $i<=$lh; $i++) {
1.25 www 1667: my $val;
1668: if ($derivative==2) {
1.27 www 1669: $val=$Apache::functionplotresponse::d2funcdx2[$i];
1.25 www 1670: } elsif ($derivative==1) {
1.27 www 1671: $val=$Apache::functionplotresponse::dfuncdx[$i];
1.42 www 1672: } elsif ($derivative==-1) {
1673: $integral+=$Apache::functionplotresponse::func[$i]*$binwidth;
1674: $val=$integral;
1.25 www 1675: } else {
1.27 www 1676: $val=$Apache::functionplotresponse::func[$i];
1.25 www 1677: }
1.31 www 1678: unless (&compare_rel($relationship,$value,$val,$tol)) {
1.37 www 1679: &addlog("Actual value ".(defined($val)?$val:'undef').", expected $value, tolerance $tol");
1.35 www 1680: &addlog("Condition not fulfilled at x=".&actualval($i,$xmin,$xmax)." (".$Apache::functionplotresponse::actualxval[$i]."; index $i)");
1.31 www 1681: if (($findupper) && ($i>$li)) {
1.49 www 1682: # Check lengths
1683: unless (&checklength($i,$li,$minimumlength,$maximumlength,$xmin,$xmax,$label)) { return 0; }
1684: # Successfully found a new label, set it
1.31 www 1685: $Apache::functionplotresponse::functionplotrulelabels{$xfinallabel}=$i;
1.35 www 1686: &addlog("Rule $label passed, setting label $xfinallabel");
1.31 www 1687: return 1;
1688: } else {
1.35 www 1689: &addlog("Rule $label failed.");
1.49 www 1690: &setfailed($label);
1.31 www 1691: return 0;
1692: }
1693: }
1.42 www 1694: }
1.25 www 1695: }
1.49 www 1696: # Corner case where this makes sense: using start or stop as defined labels
1697: unless (&checklength($lh,$li,$minimumlength,$maximumlength,$xmin,$xmax,$label)) { return 0; }
1.35 www 1698: &addlog("Rule $label passed.");
1.27 www 1699: return 1;
1.24 www 1700: }
1701:
1.49 www 1702: #
1703: # check for minimum and maximum lengths
1704: #
1705:
1706: sub checklength {
1707: my ($i,$li,$minimumlength,$maximumlength,$xmin,$xmax,$label)=@_;
1708: unless (($minimumlength) || ($maximumlength)) { return 1; }
1709: my $length=&actualval($i,$xmin,$xmax)-&actualval($li,$xmin,$xmax);
1710: if ($minimumlength) {
1711: if ($length<$minimumlength) {
1712: &addlog("Rule $label failed, actual length $length, minimum length $minimumlength");
1713: &setfailed($label);
1714: return 0;
1715: }
1716: }
1717: if ($maximumlength) {
1718: if ($length>$maximumlength) {
1719: &addlog("Rule $label failed, actual length $length, maximum length $maximumlength");
1720: &setfailed($label);
1721: return 0;
1722: }
1723: }
1724: return 1;
1725: }
1726:
1727: sub setfailed {
1.79 www 1728: my ($hintlabel)=@_;
1.49 www 1729: $hintlabel=~s/^R//;
1730: push(@Apache::functionplotresponse::failedrules,$hintlabel);
1731: &addlog("Set hint condition $hintlabel");
1732: }
1733:
1.37 www 1734: sub start_functionplotruleset {
1735: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1736: if ($target eq 'edit') {
1737: return &Apache::edit::start_table($token).
1738: '<tr><td><span class="LC_nobreak">'.&mt('Function Plot Rule Set').'</span></td>'
1739: .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
1740: .&Apache::edit::deletelist($target,$token).' '.
1.50 www 1741: &Apache::edit::insertlist($target,$token).' '
1742: .&Apache::loncommon::help_open_topic('Function_Plot_Response_Rule_Set','Function Plot Rules')
1.37 www 1743: .'</span></td>'
1744: ."<td> "
1745: .&Apache::edit::end_row()
1746: .&Apache::edit::start_spanning_row()
1747: ."\n";
1748: }
1749: }
1.3 www 1750:
1.37 www 1751: sub end_functionplotruleset {
1752: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1753: my $id=$Apache::inputtags::response[-1];
1754: my $partid=$Apache::inputtags::part;
1755: my $internalid = $partid.'_'.$id;
1756:
1757: if ($target eq 'edit' ) {
1758: return &Apache::edit::end_table();
1759: } elsif ($target eq 'grade'
1.10 www 1760: && &Apache::response::submitted()
1761: && $Apache::lonhomework::type ne 'exam') {
1.12 www 1762: #
1763: # Actually grade
1764: #
1.41 www 1765: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-2);
1.27 www 1766:
1.24 www 1767: my $ad='';
1.30 www 1768: undef %Apache::functionplotresponse::functionplotrulelabels;
1.35 www 1769: $Apache::functionplotresponse::ruleslog='';
1.30 www 1770: $Apache::functionplotresponse::functionplotrulelabels{'start'}=400;
1771: $Apache::functionplotresponse::functionplotrulelabels{'end'}=0;
1.64 www 1772: if (&populate_arrays($internalid,$xmin,$xmax,$ymin,$ymax) eq 'no_func') {
1.21 www 1773: $ad='NOT_FUNCTION';
1.18 www 1774: } else {
1.35 www 1775: &addlog("Start of function ".&actualval($Apache::functionplotresponse::functionplotrulelabels{'start'},$xmin,$xmax)." (index ".
1776: $Apache::functionplotresponse::functionplotrulelabels{'start'}.")");
1777: &addlog("End of function ".&actualval($Apache::functionplotresponse::functionplotrulelabels{'end'},$xmin,$xmax)." (index ".
1778: $Apache::functionplotresponse::functionplotrulelabels{'end'}.")");
1779:
1.24 www 1780: # We have a function that we can actually grade, go through the spline rules.
1.25 www 1781: foreach my $rule (@Apache::functionplotresponse::functionplotrules) {
1.65 www 1782: unless (&functionplotrulecheck($rule,$xmin,$xmax,$ymin,$ymax,$safeeval)) {
1.24 www 1783: $ad='INCORRECT';
1784: last;
1785: }
1786: }
1.77 www 1787: # And now go through the vector rules
1788: foreach my $rule (@Apache::functionplotresponse::functionplotvectorrules) {
1789: unless (&functionplotvectorrulecheck($rule,$xmin,$xmax,$ymin,$ymax,$safeeval)) {
1790: $ad='INCORRECT';
1791: last;
1792: }
1793: }
1.24 www 1794: # If it's not wrong, it's correct
1795: unless ($ad) { $ad='EXACT_ANS' };
1.18 www 1796: }
1.37 www 1797: &addlog("Set hint conditions: ".join(",",@Apache::functionplotresponse::failedrules));
1798: &addlog("Assigned award detail: $ad");
1799: # Store for later to be assigned at end_functionplotresponse
1800: $Apache::functionplotresponse::awarddetail=$ad;
1801: }
1802: }
1803:
1804:
1805: sub end_functionplotresponse {
1806: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1807: &Apache::response::end_response;
1808:
1809: my $result;
1810: my $id=$Apache::inputtags::response[-1];
1811: my $partid=$Apache::inputtags::part;
1812: my $internalid = $partid.'_'.$id;
1.20 www 1813:
1.37 www 1814: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
1815: if ($target eq 'grade'
1816: && &Apache::response::submitted()
1817: && $Apache::lonhomework::type eq 'exam') {
1818:
1819: &Apache::response::scored_response($partid,$id);
1820:
1821: } elsif ($target eq 'grade'
1822: && &Apache::response::submitted()
1823: && $Apache::lonhomework::type ne 'exam') {
1824: my ($response,%coords)=&get_answer_from_form_fields($internalid);
1825: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
1826: my %previous=&Apache::response::check_for_previous($response,$partid,$id);
1827: #
1828: # Assign grade
1829: #
1830: my $ad=$Apache::functionplotresponse::awarddetail;
1.12 www 1831: #
1832: # Store grading info
1833: #
1834: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
1835: &Apache::response::handle_previous(\%previous,$ad);
1.10 www 1836: } elsif ($target eq 'web') {
1.37 www 1837: undef @Apache::functionplotresponse::failedrules;
1838: }
1839: return $result;
1840: }
1841:
1842: sub end_functionplotelements {
1843: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1844: my $result='';
1845: my $id=$Apache::inputtags::response[-1];
1846: my $partid=$Apache::inputtags::part;
1847: my $internalid = $partid.'_'.$id;
1848: if ($target eq 'edit' ) {
1849: $result=&Apache::edit::end_table();
1850: } elsif ($target eq 'web') {
1.47 www 1851: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-2);
1852:
1853: # Are we in show answer mode?
1854: my $showanswer=&Apache::response::show_answer();
1855: if ($showanswer) {
1856: # Render answerdisplay
1857: my $answerdisplay=&Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval,-2);
1858: if ($answerdisplay=~/\S/s) {
1859: foreach my $plot (split(/\s*\,\s*/,$answerdisplay)) {
1.58 www 1860: my @components=split(/\s*\:\s*/,$plot);
1861: if ($#components<3) {
1862: # Just a simple plot
1863: my ($func,$xl,$xh)=@components;
1864: if ((!defined($xl)) || ($xl eq '')) { $xl=$xmin; }
1865: if ((!defined($xh)) || ($xh eq '')) { $xh=$xmax; }
1866: $result.=&plot_script($internalid,$func,1,'','00aa00',$xl,$xh,6);
1867: } else {
1868: # This is a spline
1.59 www 1869: $result.=&answer_spline_script($internalid,@components);
1.58 www 1870: }
1.47 www 1871: }
1872: }
1873: }
1.48 www 1874: my $fixed=0;
1875: if (($showanswer) || (&Apache::response::check_status()>=2)) { $fixed=1; }
1.6 www 1876: # Now is the time to render all of the stored splines
1877: foreach my $label (keys(%Apache::functionplotresponse::splineorder)) {
1.48 www 1878: $result.=&generate_spline($internalid,$label,$xmin,$xmax,$ymin,$ymax,$fixed);
1.6 www 1879: }
1.5 www 1880: # close the init script
1.3 www 1881: $result.=&end_init_script();
1.17 www 1882: # register all splines in this response
1883: $result.='<input type="hidden" name="HWVAL_AllSplines_'.$internalid.'" value="'.
1884: join(',',keys(%Apache::functionplotresponse::splineorder)).'" />'."\n";
1885: foreach my $label (keys(%Apache::functionplotresponse::splineorder)) {
1886: $result.='<input type="hidden" name="HWVAL_SplineOrder_'.$internalid.'_'.$label.'" value="'.
1887: $Apache::functionplotresponse::splineorder{$label}.'" />'."\n";
1888: }
1.9 www 1889: # generate the input fields
1890: $result.=$Apache::functionplotresponse::inputfields;
1.5 www 1891: # actually start the <applet>-tag
1.3 www 1892: $result.=&geogebra_startcode($internalid);
1.5 www 1893: # load the spline bytecode
1.3 www 1894: $result.=&geogebra_spline_program();
1.5 www 1895: # set default parameters
1.3 www 1896: $result.=&geogebra_default_parameters($internalid);
1.5 www 1897: # close the <applet>-tag
1.1 www 1898: $result.=&geogebra_endcode();
1899: }
1900: return $result;
1901: }
1902:
1.41 www 1903: sub boundaries {
1904: my ($parstack,$safeeval,$level)=@_;
1905: my $xmin=&Apache::lonxml::get_param('xmin',$parstack,$safeeval,$level);
1906: $xmin=(defined($xmin)?$xmin:-10);
1907: my $xmax=&Apache::lonxml::get_param('xmax',$parstack,$safeeval,$level);
1908: $xmax=(defined($xmax)?$xmax:10);
1909: my $ymin=&Apache::lonxml::get_param('ymin',$parstack,$safeeval,$level);
1910: $ymin=(defined($ymin)?$ymin:-10);
1911: my $ymax=&Apache::lonxml::get_param('ymax',$parstack,$safeeval,$level);
1912: $ymax=(defined($ymax)?$ymax:10);
1913: if ($xmax<=$xmin) {
1914: $xmax=$xmin+20;
1915: }
1916: if ($ymax<=$ymin) {
1917: $ymax=$ymin+20;
1918: }
1919: return ($xmin,$xmax,$ymin,$ymax);
1920: }
1921:
1.37 www 1922: sub start_functionplotelements {
1923: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1924: my $result='';
1925: my $id=$Apache::inputtags::response[-1];
1926: my $partid=$Apache::inputtags::part;
1927: my $internalid = $partid.'_'.$id;
1928:
1929: if ($target eq 'edit') {
1930: return &Apache::edit::start_table($token).
1931: '<tr><td><span class="LC_nobreak">'.&mt('Function Plot Elements').'</span></td>'
1932: .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
1933: .&Apache::edit::deletelist($target,$token).' '.
1.50 www 1934: &Apache::edit::insertlist($target,$token).' '
1935: .&Apache::loncommon::help_open_topic('Function_Plot_Response_Elements','Function Plot Elements')
1.37 www 1936: .'</span></td>'
1937: ."<td> "
1938: .&Apache::edit::end_row()
1939: .&Apache::edit::start_spanning_row()
1940: ."\n";
1941: } elsif ($target eq 'web') {
1.41 www 1942: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-2);
1.37 www 1943: my $xaxisvisible=(&Apache::lonxml::get_param('xaxisvisible',$parstack,$safeeval,-2)=~/on|true|yes|1/i?'true':'false');
1944: my $yaxisvisible=(&Apache::lonxml::get_param('yaxisvisible',$parstack,$safeeval,-2)=~/on|true|yes|1/i?'true':'false');
1945: my $gridvisible=(&Apache::lonxml::get_param('gridvisible',$parstack,$safeeval,-2)=~/on|true|yes|1/i?'true':'false');
1946: my $xlabel=&Apache::lonxml::get_param('xlabel',$parstack,$safeeval,-2);
1947: my $ylabel=&Apache::lonxml::get_param('ylabel',$parstack,$safeeval,-2);
1948:
1949:
1950: # paste in the update routine to receive stuff back from the applet
1951: $result.=&update_script($internalid);
1952: # start the initscript for this applet
1953: $result.=&start_init_script($internalid);
1954: # put the axis commands inside
1955: $result.=&axes_script($internalid,$xmin,$xmax,$ymin,$ymax,$xaxisvisible,$yaxisvisible,$gridvisible);
1.45 www 1956: $result.=&axes_label($internalid,$xmin,$xmax,$ymin,$ymax,$xlabel,$ylabel);
1.37 www 1957: # init script is left open
1958: }
1959: return $result;
1960: }
1961:
1.1 www 1962: 1;
1963:
1964: __END__
1965:
1966: =head1 NAME
1967:
1968: Apache::functionplotresponse.pm;
1969:
1970: =head1 SYNOPSIS
1971:
1972: Handles tags associated with accepting function plots.
1973:
1974: This is part of the LearningOnline Network with CAPA project
1975: described at http://www.lon-capa.org.
1976:
1977: =head1 HANDLER SUBROUTINE
1978:
1979: start_functionplotresponse()
1980:
1981: =head1 OTHER SUBROUTINES
1982:
1983: =over
1984:
1985: =item end_functionplotresponse()
1986:
1987: =back
1988:
1989: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>