Annotation of loncom/homework/functionplotresponse.pm, revision 1.41
1.1 www 1: # LearningOnline Network with CAPA
2: # option list style responses
3: #
1.41 ! www 4: # $Id: functionplotresponse.pm,v 1.40 2010/11/19 17:20:44 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.36 www 34:
1.1 www 35: BEGIN {
1.37 www 36: &Apache::lonxml::register('Apache::functionplotresponse',('functionplotresponse','backgroundplot','spline',
37: 'functionplotrule','functionplotruleset',
38: 'functionplotelements'));
1.1 www 39: }
40:
1.5 www 41: #
42: # There can be a number of applets on a page, each called ggbApplet_$id,
43: # where $id is the "_"-concatenated part and responseid
44: #
45:
1.1 www 46: sub geogebra_startcode {
1.3 www 47: my ($id)=@_;
1.1 www 48: return (<<ENDSTARTCODE);
1.3 www 49: <applet name="ggbApplet_$id" code="geogebra.GeoGebraApplet" archive="geogebra.jar"
1.40 www 50: codebase="/adm/geogebra/" width="722" height="447" MAYSCRIPT>
1.1 www 51: <param name="java_arguments" value="-Xmx512m -Djnlp.packEnabled=true"/>
52: ENDSTARTCODE
53: }
54:
55: sub geogebra_endcode {
1.2 www 56: return &Apache::lonhtmlcommon::java_not_enabled()."</applet>\n";
57: }
58:
1.5 www 59: #
60: # This is the internal GeoGebra bytecode which defines the spline functions
61: #
1.2 www 62: sub geogebra_spline_program {
63: return (<<ENDSPLINEPROGRAM);
64: <param name="ggbBase64" value="UEsDBBQACAAIADhQET0AAAAAAAAAAAAAAAASAAAAZ2VvZ2VicmFfbWFjcm8ueG1s7Z1bb+PGGYavk19BMDfexkvP+QCsEqzOuWi7gIveBG1By7RXrSwJsqyY+fWdGZLS2pQtzdqWSPoDApOSqCH5vSTnmdH7bj79en8zCVbJ4nY8m7ZCHKEwSKaj2eV4et0K75ZXH1X46y8/frpOZtfJxSIOrmaLm3jZCmlEQvP+TTxazILRzeXf4pukFZ7PJ+NpQsJgOZtNtrw1TCbz9Vu/f8Gn5/j0Czk9J/8Kg/FoNu2PJ+YrYXD7dfbHb9N/mG+040UrXC7ukvXefpvO75ZBjFrh5zCIsVnEZklaYdssqFnE4Vmx7d/vluuNV9nW7VW29ee228zs9NY0P1qa8w+W42W2//hu+XW2sGuX8dK+Y7ZMJslNMl0Gy3Ru3pnPxtNlGEzii2Rij+SXH3/4ZI86mF38Nxkt82MuPr+KJ7eJ3d8Pn8znndlktghM86bY1+7vhfsbT+ZfY7NmRHCbTuI0WQSreGI/zd8xzf11dpk8eDeejo0m9gRul8ncNoBNCedJcpkrmh2yWZmbJp2y3xzQaDZbXN4G962QRBwhKpEginGFJA2DtBWyiDPNJOVYEiwFEmHwZ9au+7orxPn4z/yQqKvqWV6sHWVrN6JsLMIIESqJ4EIiJiXP6yYoUoQIoYggFPHXq5u54l9cOML58UtHIkW4FJJQIqhQQrrK8UhizqRCmHPCNCeveMU1pXIsYlIrIYS57jBhiOWV4+ZdLZCWGEtMsGfl7ueL5NZ2BUU54ov/3Jse4X6erZ7cfwhawQmJUPCX4P7fJzRCH4KPAS1eE/v65wDbhXnj5LN9xd2nJ5vNyaPN7/ONY/OZ+8bJx4d7+PnhHrLN21vbfriJbbH9IQzOSp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BlVDoTnZKd1wm9N4wqjDe7F++JSZvToRLr3Lq4BTTndcGqDEdBp4ZfYA9gPO+7OQguvLz+IPlNbcDIHkue16vMXqHrGyqTbCjNupv7CTwn4TR1GBJlKVOV0kKB2/XkFbUXKZ0/ksICyn7M6IUOyse6XFm2qtc7+R7xAhxIg3wIwwh2VTH2gGfSlFJXjrgvE60ImQk6jsPqfLhCEliHqPWpNoFthKpXS1vRgLDTn/Bq3w7iWbLJaLmMX4wz88dgkWlqSTZJGweTRny9UqWWIKUS8KYYtJzFJkZctZki5SluChf9v81XDcgrVYSkP4GcyYz1qL6ppwt3k2X5W8QZ0SrX3ine21b4N4gLb24m7DvGkX45e2ljBgEVZpD+WhUOdtyFscqF8unT/i9RT1k5JKE+0CRf31sBZh9T5O6NVr5n1m3mPgcKRXe7Ri3sOvRVi9lxRtkDYEHo1Rj1XhZ2GIA5Dcde+YG8kLwGagpG+F3YwbbJrDLVLn/6NvCpya921zpYz+E2U2veu37AC6BRm6qsXC9qo35Oi6N5TO66igFA1ug2FICHfF+oUCAlpBrWHUHUYupMtbP3TuHZxNRxFOg0GtpcW3A+OxLpZt51Kfugm3OF05/c4bMGTD8Y2xF0oqbtHLPR3T98f9iA1vzPovUEsHCEyILqxEAgAAlQQAAFBLAQIUABQACAAIADhQET1gj4YNdwsAAFgqAQASAAAAAAAAAAAAAAAAAAAAAABnZW9nZWJyYV9tYWNyby54bWxQSwECFAAUAAgACAA4UBE9TIgurEQCAACVBAAADAAAAAAAAAAAAAAAAAC3CwAAZ2VvZ2VicmEueG1sUEsFBgAAAAACAAIAegAAADUOAAAAAA=="/>
65: ENDSPLINEPROGRAM
66: }
67:
1.5 www 68: #
69: # The standard set of parameters inside <applet>
70: #
1.2 www 71: sub geogebra_default_parameters {
1.3 www 72: my ($id)=@_;
1.2 www 73: return(<<ENDDEFAULTPARAMETERS);
74: <param name="image" value="/adm/lonIcons/lonanim.gif" />
75: <param name="boxborder" value="false" />
76: <param name="centerimage" value="true" />
1.39 www 77: <param name="cache_archive" value="geogebra.jar, geogebra_main.jar, geogebra_gui.jar, geogebra_cas.jar, geogebra_algos.jar, geogebra_export.jar, geogebra_javascript.jar, jlatexmath.jar, jlm_greek.jar, jlm_cyrillic.jar, geogebra_properties.jar" />
78: <param name="cache_version" value="3.9.115.0, 3.9.115.0, 3.9.115.0, 3.9.115.0, 3.9.115.0, 3.9.115.0, 3.9.115.0, 3.9.115.0, 3.9.115.0, 3.9.115.0, 3.9.115.0" />
1.2 www 79: <param name="framePossible" value="false" />
80:
81: <param name="showResetIcon" value="false" />
82: <param name="showAnimationButton" value="false" />
83: <param name="enableRightClick" value="false" />
84: <param name="errorDialogsActive" value="true" />
85: <param name="enableLabelDrags" value="false" />
86: <param name="showMenuBar" value="false" />
87: <param name="showToolBar" value="false" />
88: <param name="showToolBarHelp" value="false" />
89: <param name="showAlgebraInput" value="false" />
90: <param name="enableShiftDragZoom" value="false" />
91: <param name="allowRescaling" value="false" />
92: <param name="enableLabelDrags" value="false" />
1.3 www 93: <param name="ggbOnInitParam" value="applet_$id" />
1.2 www 94: ENDDEFAULTPARAMETERS
1.1 www 95: }
96:
1.5 www 97: #
98: # This subroutine is called by LON-CAPA at </problem>
99: # Each applet on the page will call function ggbOnInit when it is done loading
100: # This function in turn will call the respective function registered by start_init_script
101: # Which one of the registered functions is called is determined by ggbOnInitParam, which GeoGebra passes to ggbOnInit
102: #
103:
1.3 www 104: sub init_script {
1.16 www 105: if ($#Apache::functionplotresponse::callscripts>=0) {
106: my $script='';
107: foreach my $id (@Apache::functionplotresponse::callscripts) {
108: $script.="if (param=='applet_$id') { loaded_$id=true; }\n";
109: }
110: $script.="if (".join(' && ',map { "loaded_$_" } (@Apache::functionplotresponse::callscripts)).
1.17 www 111: ") { setTimeout('ggbInitAll()',20) }";
1.16 www 112: my $calls=join("\n",map { "ggbInit_$_();" } (@Apache::functionplotresponse::callscripts));
1.3 www 113: return (<<ENDGGBINIT);
114: <script type="text/javascript">
115: // <![CDATA[
1.16 www 116: // Function that each applet will call when loaded
117: // It will pass "its" parameter
118: // Set flags for when an applet is loaded, wait till all are loaded, and then some
1.3 www 119: function ggbOnInit(param) {
1.16 www 120: $script
121: }
122: function ggbInitAll() {
1.17 www 123: $calls
1.3 www 124: }
125: // ]]>
126: </script>
127: ENDGGBINIT
128: }
129: }
130:
1.5 www 131: #
132: # Each Geogebra applet is supposed to call this when parameters change
1.10 www 133: # Changes the hidden fields on the web page
1.5 www 134: #
135: sub update_script {
136: my ($id)=@_;
137: return (<<ENDUPDATESCRIPT);
138: <script type="text/javascript">
139: // <![CDATA[
140: function updatePointCoordinates_$id(coordinateName) {
141: var x = document.ggbApplet_$id.getXcoord(coordinateName);
142: var y = document.ggbApplet_$id.getYcoord(coordinateName);
1.9 www 143: document.lonhomework.elements["HWVAL_$id\_" + coordinateName + "_x"].value = x;
144: document.lonhomework.elements["HWVAL_$id\_" + coordinateName + "_y"].value = y;
1.5 www 145: }
146: // ]]>
147: </script>
148: ENDUPDATESCRIPT
149: }
150:
151: #
152: # Register the above update-handler for a variable
153: #
154:
155: sub update_register {
156: my ($id,$variable)=@_;
1.6 www 157: return "document.ggbApplet_$id.registerObjectUpdateListener('$variable','updatePointCoordinates_$id');\n";
158: }
159:
160: #
1.40 www 161: # Set a point coordinate variable
1.6 www 162: #
1.40 www 163: sub set_point_coordinate {
1.6 www 164: my ($id,$variable,$x,$y)=@_;
165: return (<<ENDSETVARIABLE);
1.9 www 166: document.ggbApplet_$id.evalCommand("$variable=($x,$y)");
1.40 www 167: document.ggbApplet_$id.setLabelVisible("$variable",false);
1.6 www 168: ENDSETVARIABLE
169: }
170:
171: #
1.40 www 172: # Set a slope coordinate variable
173: #
174: sub set_slope_coordinate {
175: my ($id,$variable,$xrel,$yrel,$xmin,$xmax,$ymin,$ymax,$pointname)=@_;
176: my $xvariable=$variable.'x';
177: my $yvariable=$variable.'y';
178: my $domain=$xmax-$xmin;
179: my $range=$ymax-$ymin;
180: my $xinterval=$domain/100.;
181: my $yinterval=$range/200.;
182: return (<<ENDSETSVARIABLE);
183: document.ggbApplet_$id.evalCommand("$xvariable=Slider[0,$domain,$xinterval]");
184: document.ggbApplet_$id.setVisible("$xvariable", false);
185: document.ggbApplet_$id.evalCommand("$xvariable=$xrel");
186: document.ggbApplet_$id.evalCommand("$yvariable=Slider[-$range,$range,$yinterval]");
187: document.ggbApplet_$id.setVisible("$yvariable", false);
188: document.ggbApplet_$id.evalCommand("$yvariable=$yrel");
189: document.ggbApplet_$id.evalCommand("$variable=($xvariable+x($pointname),$yvariable+y($pointname))");
190: document.ggbApplet_$id.setLabelVisible("$variable", false);
191: ENDSETSVARIABLE
192: }
193:
194: #
1.9 www 195: # Input field name for a coordinate variable
196: #
197:
198: sub field_name {
199: my ($id,$variable,$name)=@_;
200: return "HWVAL_$id\_$variable\_$name";
201: }
202:
203: #
204: # Generate an input field for a coordinate variable
205: #
206:
207: sub generate_input_field {
208: my ($id,$variable,$x,$y)=@_;
209: $Apache::functionplotresponse::inputfields.=
210: "<input type='hidden' name='".&field_name($id,$variable,'x')."' value='$x' />\n".
211: "<input type='hidden' name='".&field_name($id,$variable,'y')."' value='$y' />\n";
212: }
213:
214: #
1.40 www 215: # Initialize a new point coordinate variable at set a listener on it
1.6 www 216: #
1.40 www 217: sub new_point_coordinate {
1.6 www 218: my ($id,$variable,$x,$y)=@_;
1.11 www 219: if (defined($Apache::functionplotresponse::previous{&field_name($id,$variable,'x')})) {
1.10 www 220: $x=$Apache::functionplotresponse::previous{&field_name($id,$variable,'x')};
221: }
1.11 www 222: if (defined($Apache::functionplotresponse::previous{&field_name($id,$variable,'y')})) {
1.10 www 223: $y=$Apache::functionplotresponse::previous{&field_name($id,$variable,'y')};
224: }
1.9 www 225: &generate_input_field($id,$variable,$x,$y);
1.40 www 226: return &set_point_coordinate($id,$variable,$x,$y).&update_register($id,$variable);
227: }
228:
229: #
230: # Initialize a new slope coordinate variable at set a listener on it
231: #
232: sub new_slope_coordinate {
233: my ($id,$variable,$x,$y,$pointname,$xp,$yp,$xmin,$xmax,$ymin,$ymax)=@_;
234: #
235: # $variable: name of the slope point
236: # $x, $y: coordinates of the slope point
237: # $pointname: name of the associated point point
238: # $xp $yp: coordinates of the point point
239: #
240: if (defined($Apache::functionplotresponse::previous{&field_name($id,$variable,'x')})) {
241: $x=$Apache::functionplotresponse::previous{&field_name($id,$variable,'x')};
242: }
243: if (defined($Apache::functionplotresponse::previous{&field_name($id,$variable,'y')})) {
244: $y=$Apache::functionplotresponse::previous{&field_name($id,$variable,'y')};
245: }
246: if (defined($Apache::functionplotresponse::previous{&field_name($id,$pointname,'x')})) {
247: $xp=$Apache::functionplotresponse::previous{&field_name($id,$pointname,'x')};
248: }
249: if (defined($Apache::functionplotresponse::previous{&field_name($id,$pointname,'y')})) {
250: $yp=$Apache::functionplotresponse::previous{&field_name($id,$pointname,'y')};
251: }
252:
253: &generate_input_field($id,$variable,$x,$y);
254: my $xrel=$x-$xp;
255: my $yrel=$y-$yp;
256: return &set_slope_coordinate($id,$variable,$xrel,$yrel,$xmin,$xmax,$ymin,$ymax,$pointname).&update_register($id,$variable);
1.5 www 257: }
258:
259: #
260: # This registers the init-function call for ggbOnInit, which LON-CAPA will place at </problem>
261: # It then starts the right headers
262: #
1.3 www 263: sub start_init_script {
264: my ($id)=@_;
1.16 www 265: # Add id to the list of ggbInit_$id functions that need to be called
266: push(@Apache::functionplotresponse::callscripts,$id);
1.5 www 267: # ... and open this function
1.3 www 268: return (<<ENDSTARTINIT);
269: <script type="text/javascript">
270: // <![CDATA[
1.5 www 271: // variable that will eventually be passed back to the server
272: var coordinateMap_$id = [];
1.16 www 273: // flag for not loaded yet
274: var loaded_$id=false;
1.5 www 275: // Init-function for applet
1.3 www 276: function ggbInit_$id() {
277: ENDSTARTINIT
278: }
279:
1.5 www 280: #
281: # This sets the axes inside ggbInit_$id
282: #
283:
1.4 www 284: sub axes_script {
285: my ($id,$xmin,$xmax,$ymin,$ymax,$xvisible,$yvisible,$gvisible)=@_;
286: return (<<ENDAXESSCRIPT);
287: // changes (xmin, xmax, ymin, ymax)
288: document.ggbApplet_$id.setCoordSystem($xmin,$xmax,$ymin,$ymax);
289:
290: // makes the (x,y) axis (in)visible
291: document.ggbApplet_$id.setAxesVisible($xvisible,$yvisible);
292: // makes the grid (in)visible
293: document.ggbApplet_$id.setGridVisible($gvisible);
294: ENDAXESSCRIPT
295: }
296:
1.14 www 297: sub axes_label {
298: my ($id,$xlabel,$ylabel)=@_;
299: unless ($xlabel || $ylabel) { return ''; }
300: my $return='document.ggbApplet_'.$id.'.evalCommand("topRight=Corner[3]");';
301: if ($xlabel) {
302: $return.=(<<ENDXAXISLABELSCRIPT);
303: document.ggbApplet_$id.evalCommand("Xlabel=(x(topRight)-AxisStepX[],AxisStepY[]/6)");
304: document.ggbApplet_$id.setVisible("Xlabel",false);
305: document.ggbApplet_$id.evalCommand("Text[\\"$xlabel\\", Xlabel]");
306: ENDXAXISLABELSCRIPT
307: }
308: if ($ylabel) {
309: $return.=(<<ENDYAXISLABELSCRIPT);
310: document.ggbApplet_$id.evalCommand("Ylabel=(AxisStepX[]/6,y(topRight)-AxisStepY[]/3)");
311: document.ggbApplet_$id.setVisible("Ylabel",false);
312: document.ggbApplet_$id.evalCommand("Text[\\"$ylabel\\", Ylabel]");
313: ENDYAXISLABELSCRIPT
314: }
315: return $return;
316: }
317:
1.4 www 318: sub plot_script {
1.41 ! www 319: my ($id,$function,$fixed,$label,$xmin,$xmax)=@_;
! 320: $label=~s/\W//g;
! 321: if (($label) && ($label!~/^[A-Za-z]/)) {
! 322: $label='C'.$label;
! 323: }
! 324: my $visible=0;
! 325: if ($label) {
! 326: $visible="1";
! 327: } else {
! 328: $Apache::functionplotresponse::counter++;
! 329: $label='C'.$Apache::functionplotresponse::counter;
! 330: }
1.6 www 331: if ($fixed) {
1.41 ! www 332: return "document.ggbApplet_$id.evalCommand('$label=Function[$function,$xmin,$xmax]')".
! 333: ($visible?'':"document.ggbApplet.setLabelVisible('$label', false);");
1.6 www 334: } else {
335: return "document.ggbApplet_$id.evalCommand('y=$function')";
336: }
1.4 www 337: }
338:
1.5 www 339: #
1.7 www 340: # Subroutine that generates code for spline $label based on stored information
1.6 www 341: #
342:
343: sub generate_spline {
1.40 www 344: my ($id,$label,$xmin,$xmax,$ymin,$ymax)=@_;
1.6 www 345: my $result='';
1.7 www 346: my $order=$Apache::functionplotresponse::splineorder{$label};
347: my $x=$Apache::functionplotresponse::splineinitx{$label};
348: my $y=$Apache::functionplotresponse::splineinity{$label};
349: my $sx=$Apache::functionplotresponse::splinescalex{$label};
350: my $sy=$Apache::functionplotresponse::splinescaley{$label};
351: my @coords=();
352: foreach my $i (1..$order) {
1.40 www 353: $result.=&new_point_coordinate($id,$label.'P'.$i,$x,$y);
354: my $xp=$x;
1.8 www 355: $x+=$sx/(2.*($order-1));
1.7 www 356: push(@coords,$label.'P'.$i);
1.40 www 357: $result.=&new_slope_coordinate($id,$label.'S'.$i,$x,$y+$sy,$label.'P'.$i,$xp,$y,$xmin,$xmax,$ymin,$ymax);
1.8 www 358: $x+=$sx/(2.*($order-1));
1.7 www 359: push(@coords,$label.'S'.$i);
360: }
361: $result.='document.ggbApplet_'.$id.'.evalCommand("Spline'.$order.'['.join(',',@coords).']");'."\n";
1.6 www 362: return $result;
363: }
364: #
365: # <backgroundplot function="..." fixed="yes/no" />
1.5 www 366: #
1.4 www 367: sub start_backgroundplot {
368: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
369: my $result='';
370: my $internalid = $Apache::inputtags::part.'_'.$Apache::inputtags::response[-1];
371: my $function=&Apache::lonxml::get_param('function',$parstack,$safeeval);
1.41 ! www 372: my $label=&Apache::lonxml::get_param('label',$parstack,$safeeval);
1.6 www 373: my $fixed=(&Apache::lonxml::get_param('fixed',$parstack,$safeeval)=~/on|true|yes|1/i?1:0);
374:
1.4 www 375: unless ($function) { $function="0"; }
376: if ($target eq 'web') {
1.41 ! www 377: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-3);
! 378: $result.=&plot_script($internalid,$function,$fixed,$label,$xmin,$xmax);
1.13 www 379: } elsif ($target eq 'edit') {
380: $result=&Apache::edit::tag_start($target,$token,'Background Function Plot').
381: &Apache::edit::text_arg('Function:','function',
382: $token,'16').
1.41 ! www 383: &Apache::edit::text_arg('Label on Plot:','label',
! 384: $token,'8').
1.13 www 385: &Apache::edit::select_arg('Fixed location:','fixed',
386: ['yes','no'],$token).
387: &Apache::edit::end_row();
388: } elsif ($target eq 'modified') {
389: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.41 ! www 390: $safeeval,'function','label','fixed');
1.13 www 391: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
392: }
393: return $result;
1.4 www 394: }
395:
396: sub end_backgroundplot {
1.13 www 397: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
398: my $result='';
399: if ($target eq 'edit') {
400: $result=&Apache::edit::end_table();
401: }
402: return $result;
1.6 www 403: }
404:
405: #
1.25 www 406: # <functionplotrule ... />
1.12 www 407: #
1.25 www 408: sub start_functionplotrule {
1.12 www 409: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
410: my $result='';
1.24 www 411: my $label=&Apache::lonxml::get_param('index',$parstack,$safeeval);
412: $Apache::functionplotresponse::counter++;
413: if ($label=~/\W/) {
1.25 www 414: &Apache::lonxml::warning(&mt('Rule indices should only contain alphanumeric characters.'));
1.24 www 415: }
416: $label=~s/\W//gs;
417: unless ($label) {
418: $label='R'.$Apache::functionplotresponse::counter;
419: } else {
420: $label='R'.$label;
421: }
422: if ($Apache::functionplotresponse::splineorder{$label}) {
1.25 www 423: &Apache::lonxml::error(&mt('Rule indices must be unique.'));
1.24 www 424: }
425:
426:
427: if ($target eq 'grade') {
428: # Simply remember - in order - for later
1.25 www 429: my $beginninglabel=&Apache::lonxml::get_param('xinitiallabel',$parstack,$safeeval);
430: my $endinglabel=&Apache::lonxml::get_param('xfinallabel',$parstack,$safeeval);
1.24 www 431: if (($beginninglabel=~/\W/) || ($endinglabel=~/W/)) {
1.25 www 432: &Apache::lonxml::warning(&mt('Rule labels must be alphanumeric.'));
1.24 www 433: }
434: $beginninglabel=~s/\W//gs;
435: $endinglabel=~s/\W//gs;
436: my $relationship=&Apache::lonxml::get_param('relationship',$parstack,$safeeval);
437: $relationship=~s/\W//gs;
438: $relationship=lc($relationship);
439: unless ($relationship=~/^(eq|ge|gt|le|lt|ne)$/) {
1.25 www 440: &Apache::lonxml::warning(&mt('Rule relationship not defined.'));
1.24 www 441: $relationship='eq';
442: }
1.35 www 443: my $derivative=&Apache::lonxml::get_param('derivativeorder',$parstack,$safeeval);
1.24 www 444: unless (($derivative==0) || ($derivative==1) || ($derivative==2)) {
1.25 www 445: &Apache::lonxml::warning(&mt('Rule derivative not defined.'));
1.24 www 446: $derivative=0;
447: }
1.25 www 448: push(@Apache::functionplotresponse::functionplotrules,join(':',(
1.24 www 449: $label,
450: $derivative,
1.25 www 451: &Apache::lonxml::get_param('xinitial',$parstack,$safeeval),
1.24 www 452: $beginninglabel,
1.25 www 453: &Apache::lonxml::get_param('xfinal',$parstack,$safeeval),
1.24 www 454: $endinglabel,
1.35 www 455: &Apache::lonxml::get_param('minimumlength',$parstack,$safeeval),
456: &Apache::lonxml::get_param('maximumlength',$parstack,$safeeval),
1.24 www 457: $relationship,
1.33 www 458: &Apache::lonxml::get_param('value',$parstack,$safeeval),
459: &Apache::lonxml::get_param('percenterror',$parstack,$safeeval)
1.24 www 460: )));
461: } elsif ($target eq 'edit') {
1.29 www 462: $result=&Apache::edit::tag_start($target,$token,'Function Plot Evaluation Rule').
1.36 www 463: &Apache::edit::text_arg('Index/Name:','index',
464: $token,'10').' '.
1.35 www 465: &Apache::edit::select_arg(&mt('Function:'),'derivativeorder',
1.29 www 466: [['0','Function itself'],
467: ['1','First derivative'],
1.32 www 468: ['2','Second derivative']],$token).'<br />'.
1.29 www 469: &Apache::edit::text_arg('(Initial) x-value:','xinitial',
1.35 www 470: $token,'8').
1.30 www 471: &Apache::edit::select_or_text_arg('(Initial) x-value label:','xinitiallabel',
1.32 www 472: [['start','Start of Plot']],$token,'8').'<br />'.
1.30 www 473:
474: &Apache::edit::text_arg('Optional final x-value for ranges:','xfinal',
1.35 www 475: $token,'8').
1.30 www 476: &Apache::edit::select_or_text_arg('Optional final x-value label:','xfinallabel',
1.32 www 477: [['end','End of Plot']],$token,'8').'<br />'.
1.35 www 478: &Apache::edit::text_arg('Optional minimum length for range:','minimumlength',
479: $token,'8').
480: &Apache::edit::text_arg('Optional maximum length for range:','maximumlength',
481: $token,'8').'<br />'.
1.38 www 482: &Apache::edit::select_or_text_arg(&mt('Relationship:'),'relationship',
1.29 www 483: [['eq','equal'],
484: ['ne','not equal'],
485: ['ge','greater than or equal'],
486: ['gt','greater than'],
487: ['lt','less than'],
488: ['le','less than or equal']],$token).
489: $result.= &Apache::edit::select_or_text_arg('Value:','value',
1.35 www 490: [['undef','not defined']],$token,'8').
1.29 www 491: &Apache::edit::text_arg('Percent error:','percenterror',
1.35 www 492: $token,'8').
1.29 www 493: &Apache::edit::end_row();
494: } elsif ($target eq 'modified') {
495: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.35 www 496: $safeeval,'index','derivativeorder',
497: 'xinitial','xinitiallabel','xfinal','xfinallabel',
498: 'minimumlength','maximumlength',
499: 'relationship','value','percenterror');
1.29 www 500: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
1.13 www 501: }
1.12 www 502: return $result;
503: }
504:
1.25 www 505: sub end_functionplotrule {
1.13 www 506: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
507: my $result='';
508: if ($target eq 'edit') {
509: $result=&Apache::edit::end_table();
510: }
511: return $result;
1.12 www 512: }
513:
514:
515: #
1.23 www 516: # <spline index="..." order="1,2,3,4" initx="..." inity="..." scalex="..." scaley="..." />
1.6 www 517: #
518: # Unfortunately, GeoGebra seems to want all splines after everything else, so we need to store them
519: #
520: sub start_spline {
521: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.13 www 522: my $result='';
523: if ($target eq 'web') {
1.23 www 524: my $label=&Apache::lonxml::get_param('index',$parstack,$safeeval);
1.13 www 525: $Apache::functionplotresponse::counter++;
526: if ($label=~/\W/) {
1.23 www 527: &Apache::lonxml::warning(&mt('Spline indices should only contain alphanumeric characters.'));
1.13 www 528: }
529: $label=~s/\W//gs;
1.23 www 530: unless ($label) {
531: $label='S'.$Apache::functionplotresponse::counter;
532: } else {
533: $label='S'.$label;
534: }
1.13 www 535: if ($Apache::functionplotresponse::splineorder{$label}) {
1.23 www 536: &Apache::lonxml::error(&mt('Spline indices must be unique.'));
1.13 www 537: }
538:
539: my $order=&Apache::lonxml::get_param('order',$parstack,$safeeval);
540: if ($order<2) { $order=2; }
541: if ($order>8) { $order=8; }
542: $Apache::functionplotresponse::splineorder{$label}=$order;
543:
544: my $x=&Apache::lonxml::get_param('initx',$parstack,$safeeval);
545: unless ($x) { $x=0; }
546: $Apache::functionplotresponse::splineinitx{$label}=$x;
547:
548: my $y=&Apache::lonxml::get_param('inity',$parstack,$safeeval);
549: unless ($y) { $y=0; }
550: $Apache::functionplotresponse::splineinity{$label}=$y;
551:
552: my $sx=&Apache::lonxml::get_param('scalex',$parstack,$safeeval);
553: unless ($sx) { $sx=$order; }
554: $Apache::functionplotresponse::splinescalex{$label}=$sx;
555:
556: my $sy=&Apache::lonxml::get_param('scaley',$parstack,$safeeval);
557: unless ($sy) { $sy=2; }
558: $Apache::functionplotresponse::splinescaley{$label}=$sy;
559: } elsif ($target eq 'edit') {
560: $result=&Apache::edit::tag_start($target,$token,'Spline').
1.23 www 561: &Apache::edit::text_arg('Index:','index',
1.14 www 562: $token,'4').' '.
1.13 www 563: &Apache::edit::select_arg('Order:','order',
1.14 www 564: ['2','3','4','5','6','7','8','9'],$token).' '.
1.13 www 565: &Apache::edit::text_arg('Initial x-value:','initx',
1.14 www 566: $token,'4').' '.
1.13 www 567: &Apache::edit::text_arg('Initial y-value:','inity',
1.14 www 568: $token,'4').' '.
1.13 www 569: &Apache::edit::text_arg('Scale x:','scalex',
1.14 www 570: $token,'4').' '.
1.13 www 571: &Apache::edit::text_arg('Scale y:','scaley',
572: $token,'4').
573: &Apache::edit::end_row();
574: } elsif ($target eq 'modified') {
575: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.23 www 576: $safeeval,'index','order','initx','inity',
1.13 www 577: 'scalex','scaley');
578: if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
579: }
580: return $result;
1.6 www 581: }
582:
583: sub end_spline {
1.13 www 584: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
585: my $result='';
586: if ($target eq 'edit') {
587: $result=&Apache::edit::end_table();
588: }
589: return $result;
1.4 www 590: }
591:
1.3 www 592: sub end_init_script {
593: return (<<ENDENDINIT);
594: }
595: // ]]>
596: </script>
597: ENDENDINIT
598: }
599:
1.10 www 600: #
601: # Storing and restoring spline coordinates from part answers
602: #
603: sub decode_previous_answer {
604: my ($answer)=@_;
605: foreach my $coordinate (split(/\,/,$answer)) {
606: my ($key,$value)=split(/\=/,$coordinate);
607: $Apache::functionplotresponse::previous{$key}=$value;
608: }
609: }
610:
611: sub get_answer_from_form_fields {
612: my ($id)=@_;
613: my $answer='';
614: my %coords=();
615: foreach my $field (keys(%env)) {
616: if ($field=~/^form\.HWVAL\_$id/) {
617: $field=~/^form\.(.*)$/;
618: $coords{$1}=$env{$field};
619: }
620: }
621: $answer=join(',',map { $_.'='.$coords{$_} } (sort(keys(%coords))));
622: return ($answer,%coords);
623: }
624:
1.15 www 625: #
626: # The following functions calculate the cubic-hermite splines server-side
627: #
628:
629: sub cubic_hermite {
630: my ($t,$p1,$s1,$p2,$s2)=@_;
631: return (2.*$t*$t*$t-3.*$t*$t+1.)*$p1 + 5.*($t*$t*$t-2.*$t*$t+$t)*($s1-$p1)+
632: (-2.*$t*$t*$t+3.*$t*$t) *$p2 + 5.*($t*$t*$t-$t*$t) *($s2-$p2);
633: }
634:
635: #
636: # d/dt(...)
637: #
638:
639: sub ddt_cubic_hermite {
640: my ($t,$p1,$s1,$p2,$s2)=@_;
641: return (6.*$t*$t-6.*$t) *$p1 + 5.*(3.*$t*$t-4.*$t+1.)*($s1-$p1)+
642: (-6.*$t*$t+6.*$t)*$p2 + 5.*(3.*$t*$t-2.*$t) *($s2-$p2);
643: }
644:
645: #
646: # d^2/dt^2(...)
647: #
648:
649: sub d2dt2_cubic_hermite {
650: my ($t,$p1,$s1,$p2,$s2)=@_;
651: return (12.*$t-6.) *$p1 + 5.*(6.*$t-4.)*($s1-$p1)+
652: (-12.*$t+6.)*$p2 + 5.*(6.*$t-2.)*($s2-$p2);
653: }
654:
655: #
1.18 www 656: # Array index calculation
657: #
658: sub array_index {
659: my ($xmin,$xmax,$x)=@_;
1.38 www 660: if ($x ne '') {
661: return int(($x-$xmin)/($xmax-$xmin)*400.+0.5);
662: } else {
663: return undef;
664: }
1.20 www 665: }
666:
667: #
1.18 www 668: # Populate the arrays
669: #
670:
671: sub populate_arrays {
672: my ($id,$xmin,$xmax)=@_;
1.26 www 673: for (my $i=0; $i<=400; $i++) {
1.35 www 674: $Apache::functionplotresponse::actualxval[$i]=undef;
1.19 www 675: $Apache::functionplotresponse::func[$i]=undef;
1.22 www 676: $Apache::functionplotresponse::dfuncdx[$i]=undef;
1.19 www 677: $Apache::functionplotresponse::d2funcd2x[$i]=undef;
1.18 www 678: }
1.19 www 679: unless ($xmax>$xmin) { return 'no_func'; }
1.18 www 680: # Run over all splines in response
681: foreach my $label (split(/\,/,$env{"form.HWVAL_AllSplines_$id"})) {
1.19 www 682: my $xiold=-1;
1.18 www 683: # Run over all points in spline
1.19 www 684: for (my $i=1; $i<$env{"form.HWVAL_SplineOrder_".$id."_".$label}; $i++) {
1.18 www 685: my $ni=$i+1;
686: my @xparms=($env{'form.HWVAL_'.$id.'_'.$label.'P'.$i.'_x'},
687: $env{'form.HWVAL_'.$id.'_'.$label.'S'.$i.'_x'},
688: $env{'form.HWVAL_'.$id.'_'.$label.'P'.$ni.'_x'},
689: $env{'form.HWVAL_'.$id.'_'.$label.'S'.$ni.'_x'});
690: my @yparms=($env{'form.HWVAL_'.$id.'_'.$label.'P'.$i.'_y'},
691: $env{'form.HWVAL_'.$id.'_'.$label.'S'.$i.'_y'},
692: $env{'form.HWVAL_'.$id.'_'.$label.'P'.$ni.'_y'},
693: $env{'form.HWVAL_'.$id.'_'.$label.'S'.$ni.'_y'});
694: # Run in small steps over spline parameter
1.26 www 695: for (my $t=0; $t<=1; $t+=0.0001) {
1.35 www 696: my $xreal=&cubic_hermite($t,@xparms);
697: my $xi=&array_index($xmin,$xmax,$xreal);
1.19 www 698: if ($xi<$xiold) { return 'no_func'; }
1.26 www 699: if (($xi>$xiold) && ($xi>=0) && ($xi<=400)) {
1.19 www 700: if (defined($Apache::functionplotresponse::func[$xi])) { return 'no_func'; }
1.18 www 701: $xiold=$xi;
1.35 www 702: $Apache::functionplotresponse::actualxval[$xi]=$xreal;
1.22 www 703: # Function value
1.30 www 704: my $funcval=&cubic_hermite($t,@yparms);
705: $Apache::functionplotresponse::func[$xi]=$funcval;
706: if (defined($funcval)) {
707: if ($xi<$Apache::functionplotresponse::functionplotrulelabels{'start'}) {
708: $Apache::functionplotresponse::functionplotrulelabels{'start'}=$xi;
709: }
710: if ($xi>$Apache::functionplotresponse::functionplotrulelabels{'end'}) {
711: $Apache::functionplotresponse::functionplotrulelabels{'end'}=$xi;
712: }
713: }
1.28 www 714: # Chain rule
1.22 www 715: # dy/dx=dy/dt/(dx/dt)
716: my $dxdt=&ddt_cubic_hermite($t,@xparms);
717: if ($dxdt) {
718: $Apache::functionplotresponse::dfuncdx[$xi]=&ddt_cubic_hermite($t,@yparms)/$dxdt;
1.34 www 719: # Second derivative
1.28 www 720: $Apache::functionplotresponse::d2funcdx2[$xi]=
1.34 www 721: ($dxdt*&d2dt2_cubic_hermite($t,@yparms)-&ddt_cubic_hermite($t,@yparms)*&d2dt2_cubic_hermite($t,@xparms))/
722: ($dxdt*$dxdt*$dxdt);
1.22 www 723: }
1.18 www 724: }
725: }
726: }
727: }
728: }
729:
730: #
1.37 www 731: # Implementation of <functionplotresponse>
1.15 www 732: #
733:
1.1 www 734: sub start_functionplotresponse {
735: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
736: my $result='';
1.6 www 737: # To remember the splines - somehow, they need to come last
738: undef %Apache::functionplotresponse::splineorder;
739: undef %Apache::functionplotresponse::splineinitx;
740: undef %Apache::functionplotresponse::splineinity;
741: undef %Apache::functionplotresponse::splinescalex;
742: undef %Apache::functionplotresponse::splinescaley;
1.10 www 743: # Remember input fields, etc
744: undef %Apache::functionplotresponse::previous;
1.9 www 745: $Apache::functionplotresponse::inputfields='';
1.6 www 746: $Apache::functionplotresponse::counter=0;
1.24 www 747: # Remember rules
1.25 www 748: undef @Apache::functionplotresponse::functionplotrules;
1.37 www 749: # Remember failed rules
750: if ($target eq 'grade') {
751: undef @Apache::functionplotresponse::failedrules;
752: }
753: # Delete previous awards
754: undef $Apache::functionplotresponse::awarddetail;
1.10 www 755: # Part and ID
756: my $partid=$Apache::inputtags::part;
757: my $id=&Apache::response::start_response($parstack,$safeeval);
1.6 www 758: # Internal ID to mark the applet and its coordinates
1.10 www 759: my $internalid = $partid.'_'.$id;
760: # Previous answer
761: &decode_previous_answer($Apache::lonhomework::history{"resource.$partid.$id.submission"});
762:
1.6 www 763: # Parameters of <functionplotresponse>
1.41 ! www 764: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval);
1.6 www 765: my $xaxisvisible=(&Apache::lonxml::get_param('xaxisvisible',$parstack,$safeeval)=~/on|true|yes|1/i?'true':'false');
766: my $yaxisvisible=(&Apache::lonxml::get_param('yaxisvisible',$parstack,$safeeval)=~/on|true|yes|1/i?'true':'false');
767: my $gridvisible=(&Apache::lonxml::get_param('gridvisible',$parstack,$safeeval)=~/on|true|yes|1/i?'true':'false');
1.14 www 768: my $xlabel=&Apache::lonxml::get_param('xlabel',$parstack,$safeeval);
769: my $ylabel=&Apache::lonxml::get_param('ylabel',$parstack,$safeeval);
1.12 www 770: if ($target eq 'edit') {
771: $result.=&Apache::edit::start_table($token)
772: .'<tr><td><span class="LC_nobreak">'.&mt('Function Plot Question').'</span></td>'
773: .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
1.37 www 774: .&Apache::edit::deletelist($target,$token).' '
775: .&Apache::edit::insertlist($target,$token)
1.12 www 776: .'</span></td>'
777: ."<td> "
778: .&Apache::edit::end_row()
779: .&Apache::edit::start_spanning_row()
780: ."\n";
1.14 www 781: $result.=&Apache::edit::text_arg('Label x-axis:','xlabel',
782: $token,'6').' '.
783: &Apache::edit::text_arg('Minimum x-value:','xmin',
784: $token,'4').' '.
1.12 www 785: &Apache::edit::text_arg('Maximum x-value:','xmax',
1.14 www 786: $token,'4').' '.
1.12 www 787: &Apache::edit::select_arg('x-axis visible:','xaxisvisible',
1.32 www 788: ['yes','no'],$token).'<br />'.
1.14 www 789: &Apache::edit::text_arg('Label y-axis:','ylabel',
790: $token,'6').' '.
1.12 www 791: &Apache::edit::text_arg('Minimum y-value:','ymin',
1.14 www 792: $token,'4').' '.
1.12 www 793: &Apache::edit::text_arg('Maximum y-value:','ymax',
1.14 www 794: $token,'4').' '.
1.12 www 795: &Apache::edit::select_arg('y-axis visible:','yaxisvisible',
1.32 www 796: ['yes','no'],$token).'<br />'.
1.12 www 797: &Apache::edit::select_arg('Grid visible:','gridvisible',
798: ['yes','no'],$token).
799: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
800: } elsif ($target eq 'modified') {
801: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.14 www 802: $safeeval,'xlabel','xmin','xmax','ylabel','ymin','ymax',
1.12 www 803: 'xaxisvisible','yaxisvisible','gridvisible');
804: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.3 www 805:
1.12 www 806: } elsif ($target eq 'meta') {
1.10 www 807: $result=&Apache::response::meta_package_write('functionplotresponse');
1.35 www 808: } elsif (($target eq 'answer') &&
809: ($env{'form.answer_output_mode'} ne 'tex') &&
810: ($Apache::lonhomework::viewgrades == 'F')) {
811: my (undef,undef,$udom,$uname)=&Apache::lonnet::whichuser();
812: my $windowopen=&Apache::lonhtmlcommon::javascript_docopen();
813: my $start_page = &Apache::loncommon::start_page('Rules Log', undef,
814: {'only_body' => 1,
815: 'bgcolor' => '#FFFFFF',
816: 'js_ready' => 1,});
817: my $end_page = &Apache::loncommon::end_page({'js_ready' => 1,});
818: $uname =~s/\W//g;
819: $udom =~s/\W//g;
820: my $function_name =
821: join('_','LONCAPA_scriptvars',$uname,$udom,
822: $env{'form.counter'},$Apache::lonxml::curdepth);
823: my $rules_var ="<script type=\"text/javascript\">
824: // <![CDATA[
825: function $function_name() {newWindow=open('','new_W','width=500,height=500,scrollbars=1,resizable=yes');newWindow.$windowopen;newWindow.document.writeln('$start_page<pre>".
826: $Apache::functionplotresponse::ruleslog.
827: "<\\/pre>$end_page');newWindow.document.close();newWindow.focus()}
828: // ]]>
829: </script><a href=\"javascript:$function_name();void(0);\">".&mt('Rules Log')."</a><br />";
830: &Apache::lonxml::add_script_result($rules_var);
831: }
832:
1.1 www 833: return $result;
834: }
835:
1.25 www 836: sub compare_rel {
1.26 www 837: my ($relationship,$value,$realval,$tol)=@_;
1.36 www 838: # is the real value undefined?
1.26 www 839: unless (defined($realval)) {
1.36 www 840: # the real value is not defined
1.26 www 841: if ($relationship eq 'eq') {
842: if ($value eq 'undef') {
843: return 1;
844: } else {
845: return 0;
846: }
847: } elsif ($relationship eq 'ne') {
848: if ($value eq 'undef') {
849: return 0;
850: } else {
851: return 1;
852: }
853: } else {
854: return 0;
855: }
856: }
857:
1.36 www 858: # is the expected value undefined?
859: if ($value eq 'undef') {
860: # but by now we know that the real value is defined
861: return 0;
862: }
863:
864: # both are defined.
1.26 www 865: if ($relationship eq 'gt') {
866: return ($realval>$value);
867: } elsif ($relationship eq 'ge') {
868: return ($realval>$value-$tol);
869: } elsif ($relationship eq 'lt') {
870: return ($realval<$value);
871: } elsif ($relationship eq 'le') {
872: return ($realval<$value+$tol);
873: } elsif ($relationship eq 'ne') {
874: return (abs($value-$realval)>$tol);
875: } else {
876: return (abs($value-$realval)<$tol);
877: }
1.25 www 878: return 0;
879: }
880:
1.35 www 881: sub addlog {
882: my ($text)=@_;
883: $Apache::functionplotresponse::ruleslog.=$text.'<br />';
884: }
885:
886: sub actualval {
887: my ($i,$xmin,$xmax)=@_;
888: return $xmin+$i/400.*($xmax-$xmin);
889: }
890:
1.25 www 891: sub functionplotrulecheck {
1.33 www 892: my ($rule,$xmin,$xmax,$ymin,$ymax)=@_;
893:
1.35 www 894: my ($label,$derivative,$xinitial,$xinitiallabel,$xfinal,$xfinallabel,$minimumlength,$maximumlength,$relationship,$value,$percent)
1.25 www 895: =split(/\:/,$rule);
1.33 www 896: $percent=($percent>0?$percent:5);
1.35 www 897: &addlog("=================");
898: &addlog("Rule $label for ".('function itself','first derivative','second derivative')[$derivative]." $relationship $value");
1.30 www 899: my $li=0;
900: my $lh=400;
901:
1.31 www 902: # Special case: the upper boundary was not defined
903: # and needs to be set to the value where
904: # the condition is not true anymore => set flag
905:
906: my $findupper=0;
907: if (($xfinal eq '')
908: && (!defined($Apache::functionplotresponse::functionplotrulelabels{$xfinallabel}))
909: && ($xfinallabel)) {
910: $findupper=1;
911: }
912:
1.25 www 913: # if a hard value is set for the boundaries, it overrides the label
1.30 www 914: if (($xinitial ne '') && ($xinitiallabel ne '') && ($xinitiallabel ne 'start')) {
915: $li=&array_index($xmin,$xmax,$xinitial);
916: $Apache::functionplotresponse::functionplotrulelabels{$xinitiallabel}=$li;
917: }
918: if (($xfinal ne '') && ($xfinallabel ne '') && ($xfinallabel ne 'end')) {
919: $lh=&array_index($xmin,$xmax,$xfinal);
920: $Apache::functionplotresponse::functionplotrulelabels{$xfinallabel}=$lh;
1.25 www 921: }
1.31 www 922: # if the label is defined, use it
1.25 www 923: if (defined($Apache::functionplotresponse::functionplotrulelabels{$xinitiallabel})) {
1.35 www 924: &addlog("Using lower label $xinitiallabel");
1.30 www 925: $li=$Apache::functionplotresponse::functionplotrulelabels{$xinitiallabel};
926: } else {
927: $li=&array_index($xmin,$xmax,$xinitial);
1.25 www 928: }
1.31 www 929: unless ($findupper) {
930: if (defined($Apache::functionplotresponse::functionplotrulelabels{$xfinallabel})) {
1.35 www 931: &addlog("Using upper label $xfinallabel");
1.33 www 932: $lh=$Apache::functionplotresponse::functionplotrulelabels{$xfinallabel}-1;
1.31 www 933: } else {
934: $lh=&array_index($xmin,$xmax,$xfinal);
935: }
1.25 www 936: }
937: # Basic sanity checks
1.30 www 938: if ($li<0) { $li=0; }
939: if ($lh>400) { $lh=400; }
1.38 www 940: if (($li>$lh) || (!defined($lh))) {
1.30 www 941: $lh=$li;
1.25 www 942: }
1.31 www 943:
1.35 www 944: &addlog("Boundaries: x=".&actualval($li,$xmin,$xmax)." (".$Apache::functionplotresponse::actualxval[$li]."; index $li)) to x=".
945: &actualval($lh,$xmin,$xmax)." (".$Apache::functionplotresponse::actualxval[$lh]."; index $lh))");
946: if ($findupper) {
947: &addlog("Looking for label $xfinallabel");
948: }
1.33 www 949: my $tol=$percent*($ymax-$ymin)/100;
950: if ($xmax>$xmin) {
951: if ($derivative==2) {
952: $tol=4.*$tol/($xmax-$xmin);
953: } elsif ($derivative==1) {
954: $tol=2.*$tol/($xmax-$xmin);
955: }
1.27 www 956: }
957: for (my $i=$li; $i<=$lh; $i++) {
1.25 www 958: my $val;
959: if ($derivative==2) {
1.27 www 960: $val=$Apache::functionplotresponse::d2funcdx2[$i];
1.25 www 961: } elsif ($derivative==1) {
1.27 www 962: $val=$Apache::functionplotresponse::dfuncdx[$i];
1.25 www 963: } else {
1.27 www 964: $val=$Apache::functionplotresponse::func[$i];
1.25 www 965: }
1.31 www 966: unless (&compare_rel($relationship,$value,$val,$tol)) {
1.37 www 967: &addlog("Actual value ".(defined($val)?$val:'undef').", expected $value, tolerance $tol");
1.35 www 968: &addlog("Condition not fulfilled at x=".&actualval($i,$xmin,$xmax)." (".$Apache::functionplotresponse::actualxval[$i]."; index $i)");
1.31 www 969: if (($findupper) && ($i>$li)) {
1.35 www 970: # check for minimum and maximum lengths
971: my $length=&actualval($i,$xmin,$xmax)-&actualval($li,$xmin,$xmax);
972: if ($minimumlength) {
973: if ($length<$minimumlength) {
974: &addlog("Rule $label failed, actual length $length, minimum length $minimumlength");
975: return 0;
976: }
977: }
978: if ($maximumlength) {
979: if ($length>$maximumlength) {
980: &addlog("Rule $label failed, actual length $length, maximum length $maximumlength");
981: return 0;
982: }
983: }
1.31 www 984: $Apache::functionplotresponse::functionplotrulelabels{$xfinallabel}=$i;
1.35 www 985: &addlog("Rule $label passed, setting label $xfinallabel");
1.31 www 986: return 1;
987: } else {
1.35 www 988: &addlog("Rule $label failed.");
1.37 www 989: my $hintlabel=$label;
990: $hintlabel=~s/^R//;
991: push(@Apache::functionplotresponse::failedrules,$hintlabel);
992: &addlog("Set hint condition $hintlabel");
1.31 www 993: return 0;
994: }
995: }
1.25 www 996: }
1.35 www 997: &addlog("Rule $label passed.");
1.27 www 998: return 1;
1.24 www 999: }
1000:
1.37 www 1001: sub start_functionplotruleset {
1002: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1003: if ($target eq 'edit') {
1004: return &Apache::edit::start_table($token).
1005: '<tr><td><span class="LC_nobreak">'.&mt('Function Plot Rule Set').'</span></td>'
1006: .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
1007: .&Apache::edit::deletelist($target,$token).' '.
1008: &Apache::edit::insertlist($target,$token)
1009: .'</span></td>'
1010: ."<td> "
1011: .&Apache::edit::end_row()
1012: .&Apache::edit::start_spanning_row()
1013: ."\n";
1014: }
1015: }
1.3 www 1016:
1.37 www 1017: sub end_functionplotruleset {
1018: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1019: my $id=$Apache::inputtags::response[-1];
1020: my $partid=$Apache::inputtags::part;
1021: my $internalid = $partid.'_'.$id;
1022:
1023: if ($target eq 'edit' ) {
1024: return &Apache::edit::end_table();
1025: } elsif ($target eq 'grade'
1.10 www 1026: && &Apache::response::submitted()
1027: && $Apache::lonhomework::type ne 'exam') {
1.12 www 1028: #
1029: # Actually grade
1030: #
1.41 ! www 1031: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-2);
1.27 www 1032:
1.24 www 1033: my $ad='';
1.30 www 1034: undef %Apache::functionplotresponse::functionplotrulelabels;
1.35 www 1035: $Apache::functionplotresponse::ruleslog='';
1.30 www 1036: $Apache::functionplotresponse::functionplotrulelabels{'start'}=400;
1037: $Apache::functionplotresponse::functionplotrulelabels{'end'}=0;
1.18 www 1038: if (&populate_arrays($internalid,$xmin,$xmax) eq 'no_func') {
1.21 www 1039: $ad='NOT_FUNCTION';
1.18 www 1040: } else {
1.35 www 1041: &addlog("Start of function ".&actualval($Apache::functionplotresponse::functionplotrulelabels{'start'},$xmin,$xmax)." (index ".
1042: $Apache::functionplotresponse::functionplotrulelabels{'start'}.")");
1043: &addlog("End of function ".&actualval($Apache::functionplotresponse::functionplotrulelabels{'end'},$xmin,$xmax)." (index ".
1044: $Apache::functionplotresponse::functionplotrulelabels{'end'}.")");
1045:
1.24 www 1046: # We have a function that we can actually grade, go through the spline rules.
1.25 www 1047: foreach my $rule (@Apache::functionplotresponse::functionplotrules) {
1.33 www 1048: unless (&functionplotrulecheck($rule,$xmin,$xmax,$ymin,$ymax)) {
1.24 www 1049: $ad='INCORRECT';
1050: last;
1051: }
1052: }
1053: # If it's not wrong, it's correct
1054: unless ($ad) { $ad='EXACT_ANS' };
1.18 www 1055: }
1.37 www 1056: &addlog("Set hint conditions: ".join(",",@Apache::functionplotresponse::failedrules));
1057: &addlog("Assigned award detail: $ad");
1058: # Store for later to be assigned at end_functionplotresponse
1059: $Apache::functionplotresponse::awarddetail=$ad;
1060: }
1061: }
1062:
1063:
1064: sub end_functionplotresponse {
1065: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1066: &Apache::response::end_response;
1067:
1068: my $result;
1069: my $id=$Apache::inputtags::response[-1];
1070: my $partid=$Apache::inputtags::part;
1071: my $internalid = $partid.'_'.$id;
1.20 www 1072:
1.37 www 1073: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
1074: if ($target eq 'grade'
1075: && &Apache::response::submitted()
1076: && $Apache::lonhomework::type eq 'exam') {
1077:
1078: &Apache::response::scored_response($partid,$id);
1079:
1080: } elsif ($target eq 'grade'
1081: && &Apache::response::submitted()
1082: && $Apache::lonhomework::type ne 'exam') {
1083: my ($response,%coords)=&get_answer_from_form_fields($internalid);
1084: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
1085: my %previous=&Apache::response::check_for_previous($response,$partid,$id);
1086: #
1087: # Assign grade
1088: #
1089: my $ad=$Apache::functionplotresponse::awarddetail;
1.12 www 1090: #
1091: # Store grading info
1092: #
1093: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
1094: &Apache::response::handle_previous(\%previous,$ad);
1.10 www 1095: } elsif ($target eq 'web') {
1.37 www 1096: undef @Apache::functionplotresponse::failedrules;
1097: }
1098: return $result;
1099: }
1100:
1101: sub end_functionplotelements {
1102: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1103: my $result='';
1104: my $id=$Apache::inputtags::response[-1];
1105: my $partid=$Apache::inputtags::part;
1106: my $internalid = $partid.'_'.$id;
1107: if ($target eq 'edit' ) {
1108: $result=&Apache::edit::end_table();
1109: } elsif ($target eq 'web') {
1.6 www 1110: # Now is the time to render all of the stored splines
1.41 ! www 1111: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-2);
1.6 www 1112: foreach my $label (keys(%Apache::functionplotresponse::splineorder)) {
1.40 www 1113: $result.=&generate_spline($internalid,$label,$xmin,$xmax,$ymin,$ymax);
1.6 www 1114: }
1.5 www 1115: # close the init script
1.3 www 1116: $result.=&end_init_script();
1.17 www 1117: # register all splines in this response
1118: $result.='<input type="hidden" name="HWVAL_AllSplines_'.$internalid.'" value="'.
1119: join(',',keys(%Apache::functionplotresponse::splineorder)).'" />'."\n";
1120: foreach my $label (keys(%Apache::functionplotresponse::splineorder)) {
1121: $result.='<input type="hidden" name="HWVAL_SplineOrder_'.$internalid.'_'.$label.'" value="'.
1122: $Apache::functionplotresponse::splineorder{$label}.'" />'."\n";
1123: }
1.9 www 1124: # generate the input fields
1125: $result.=$Apache::functionplotresponse::inputfields;
1.5 www 1126: # actually start the <applet>-tag
1.3 www 1127: $result.=&geogebra_startcode($internalid);
1.5 www 1128: # load the spline bytecode
1.3 www 1129: $result.=&geogebra_spline_program();
1.5 www 1130: # set default parameters
1.3 www 1131: $result.=&geogebra_default_parameters($internalid);
1.5 www 1132: # close the <applet>-tag
1.1 www 1133: $result.=&geogebra_endcode();
1134: }
1135: return $result;
1136: }
1137:
1.41 ! www 1138: sub boundaries {
! 1139: my ($parstack,$safeeval,$level)=@_;
! 1140: my $xmin=&Apache::lonxml::get_param('xmin',$parstack,$safeeval,$level);
! 1141: $xmin=(defined($xmin)?$xmin:-10);
! 1142: my $xmax=&Apache::lonxml::get_param('xmax',$parstack,$safeeval,$level);
! 1143: $xmax=(defined($xmax)?$xmax:10);
! 1144: my $ymin=&Apache::lonxml::get_param('ymin',$parstack,$safeeval,$level);
! 1145: $ymin=(defined($ymin)?$ymin:-10);
! 1146: my $ymax=&Apache::lonxml::get_param('ymax',$parstack,$safeeval,$level);
! 1147: $ymax=(defined($ymax)?$ymax:10);
! 1148: if ($xmax<=$xmin) {
! 1149: $xmax=$xmin+20;
! 1150: }
! 1151: if ($ymax<=$ymin) {
! 1152: $ymax=$ymin+20;
! 1153: }
! 1154: return ($xmin,$xmax,$ymin,$ymax);
! 1155: }
! 1156:
1.37 www 1157: sub start_functionplotelements {
1158: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1159: my $result='';
1160: my $id=$Apache::inputtags::response[-1];
1161: my $partid=$Apache::inputtags::part;
1162: my $internalid = $partid.'_'.$id;
1163:
1164: if ($target eq 'edit') {
1165: return &Apache::edit::start_table($token).
1166: '<tr><td><span class="LC_nobreak">'.&mt('Function Plot Elements').'</span></td>'
1167: .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
1168: .&Apache::edit::deletelist($target,$token).' '.
1169: &Apache::edit::insertlist($target,$token)
1170: .'</span></td>'
1171: ."<td> "
1172: .&Apache::edit::end_row()
1173: .&Apache::edit::start_spanning_row()
1174: ."\n";
1175: } elsif ($target eq 'web') {
1.41 ! www 1176: my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-2);
1.37 www 1177: my $xaxisvisible=(&Apache::lonxml::get_param('xaxisvisible',$parstack,$safeeval,-2)=~/on|true|yes|1/i?'true':'false');
1178: my $yaxisvisible=(&Apache::lonxml::get_param('yaxisvisible',$parstack,$safeeval,-2)=~/on|true|yes|1/i?'true':'false');
1179: my $gridvisible=(&Apache::lonxml::get_param('gridvisible',$parstack,$safeeval,-2)=~/on|true|yes|1/i?'true':'false');
1180: my $xlabel=&Apache::lonxml::get_param('xlabel',$parstack,$safeeval,-2);
1181: my $ylabel=&Apache::lonxml::get_param('ylabel',$parstack,$safeeval,-2);
1182:
1183:
1184: # paste in the update routine to receive stuff back from the applet
1185: $result.=&update_script($internalid);
1186: # start the initscript for this applet
1187: $result.=&start_init_script($internalid);
1188: # put the axis commands inside
1189: $result.=&axes_script($internalid,$xmin,$xmax,$ymin,$ymax,$xaxisvisible,$yaxisvisible,$gridvisible);
1190: $result.=&axes_label($internalid,$xlabel,$ylabel);
1191: # init script is left open
1192: }
1193: return $result;
1194: }
1195:
1.1 www 1196: 1;
1197:
1198: __END__
1199:
1200: =head1 NAME
1201:
1202: Apache::functionplotresponse.pm;
1203:
1204: =head1 SYNOPSIS
1205:
1206: Handles tags associated with accepting function plots.
1207:
1208: This is part of the LearningOnline Network with CAPA project
1209: described at http://www.lon-capa.org.
1210:
1211: =head1 HANDLER SUBROUTINE
1212:
1213: start_functionplotresponse()
1214:
1215: =head1 OTHER SUBROUTINES
1216:
1217: =over
1218:
1219: =item end_functionplotresponse()
1220:
1221: =back
1222:
1223: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>