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