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