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