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