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