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