Annotation of loncom/homework/functionplotresponse.pm, revision 1.9

1.1       www         1: # LearningOnline Network with CAPA
                      2: # option list style responses
                      3: #
1.9     ! www         4: # $Id: functionplotresponse.pm,v 1.8 2010/10/08 23:26:04 www Exp $
1.1       www         5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: 
                     29: package Apache::functionplotresponse;
                     30: use strict;
                     31: use Apache::response();
                     32: use Apache::lonlocal;
                     33: use Apache::lonnet;
                     34: 
                     35: BEGIN {
1.6       www        36:   &Apache::lonxml::register('Apache::functionplotresponse',('functionplotresponse','backgroundplot','spline'));
1.1       www        37: }
                     38: 
1.5       www        39: #
                     40: # There can be a number of applets on a page, each called ggbApplet_$id, 
                     41: # where $id is the "_"-concatenated part and responseid
                     42: #
                     43: 
1.1       www        44: sub geogebra_startcode {
1.3       www        45:     my ($id)=@_;
1.1       www        46:     return (<<ENDSTARTCODE);
1.3       www        47: <applet name="ggbApplet_$id" code="geogebra.GeoGebraApplet" archive="geogebra.jar"
1.1       www        48:          codebase="/adm/geogebra/"  width="714" height="447" MAYSCRIPT>
                     49:        <param name="java_arguments" value="-Xmx512m -Djnlp.packEnabled=true"/>
                     50: ENDSTARTCODE
                     51: }
                     52: 
                     53: sub geogebra_endcode {
1.2       www        54:     return &Apache::lonhtmlcommon::java_not_enabled()."</applet>\n";
                     55: }
                     56: 
1.5       www        57: #
                     58: # This is the internal GeoGebra bytecode which defines the spline functions
                     59: #
1.2       www        60: sub geogebra_spline_program {
                     61:     return (<<ENDSPLINEPROGRAM);
                     62: <param name="ggbBase64" value="UEsDBBQACAAIADhQET0AAAAAAAAAAAAAAAASAAAAZ2VvZ2VicmFfbWFjcm8ueG1s7Z1bb+PGGYavk19BMDfexkvP+QCsEqzOuWi7gIveBG1By7RXrSwJsqyY+fWdGZLS2pQtzdqWSPoDApOSqCH5vSTnmdH7bj79en8zCVbJ4nY8m7ZCHKEwSKaj2eV4et0K75ZXH1X46y8/frpOZtfJxSIOrmaLm3jZCmlEQvP+TTxazILRzeXf4pukFZ7PJ+NpQsJgOZtNtrw1TCbz9Vu/f8Gn5/j0Czk9J/8Kg/FoNu2PJ+YrYXD7dfbHb9N/mG+040UrXC7ukvXefpvO75ZBjFrh5zCIsVnEZklaYdssqFnE4Vmx7d/vluuNV9nW7VW29ee228zs9NY0P1qa8w+W42W2//hu+XW2sGuX8dK+Y7ZMJslNMl0Gy3Ru3pnPxtNlGEzii2Rij+SXH3/4ZI86mF38Nxkt82MuPr+KJ7eJ3d8Pn8znndlktghM86bY1+7vhfsbT+ZfY7NmRHCbTuI0WQSreGI/zd8xzf11dpk8eDeejo0m9gRul8ncNoBNCedJcpkrmh2yWZmbJp2y3xzQaDZbXN4G962QRBwhKpEginGFJA2DtBWyiDPNJOVYEiwFEmHwZ9au+7orxPn4z/yQqKvqWV6sHWVrN6JsLMIIESqJ4EIiJiXP6yYoUoQIoYggFPHXq5u54l9cOML58UtHIkW4FJJQIqhQQrrK8UhizqRCmHPCNCeveMU1pXIsYlIrIYS57jBhiOWV4+ZdLZCWGEtMsGfl7ueL5NZ2BUU54ov/3Jse4X6erZ7cfwhawQmJUPCX4P7fJzRCH4KPAS1eE/v65wDb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OH/vrO7YT2EXqrQdw/Gb8/ObL2eO5keQI2gjV5jSazCiBtlSVaOuc9nb3OaWP64esBlVDoTnZKd1wm9N4wqjDe7F++JSZvToRLr3Lq4BTTndcGqDEdBp4ZfYA9gPO+7OQguvLz+IPlNbcDIHkue16vMXqHrGyqTbCjNupv7CTwn4TR1GBJlKVOV0kKB2/XkFbUXKZ0/ksICyn7M6IUOyse6XFm2qtc7+R7xAhxIg3wIwwh2VTH2gGfSlFJXjrgvE60ImQk6jsPqfLhCEliHqPWpNoFthKpXS1vRgLDTn/Bq3w7iWbLJaLmMX4wz88dgkWlqSTZJGweTRny9UqWWIKUS8KYYtJzFJkZctZki5SluChf9v81XDcgrVYSkP4GcyYz1qL6ppwt3k2X5W8QZ0SrX3ine21b4N4gLb24m7DvGkX45e2ljBgEVZpD+WhUOdtyFscqF8unT/i9RT1k5JKE+0CRf31sBZh9T5O6NVr5n1m3mPgcKRXe7Ri3sOvRVi9lxRtkDYEHo1Rj1XhZ2GIA5Dcde+YG8kLwGagpG+F3YwbbJrDLVLn/6NvCpya921zpYz+E2U2veu37AC6BRm6qsXC9qo35Oi6N5TO66igFA1ug2FICHfF+oUCAlpBrWHUHUYupMtbP3TuHZxNRxFOg0GtpcW3A+OxLpZt51Kfugm3OF05/c4bMGTD8Y2xF0oqbtHLPR3T98f9iA1vzPovUEsHCEyILqxEAgAAlQQAAFBLAQIUABQACAAIADhQET1gj4YNdwsAAFgqAQASAAAAAAAAAAAAAAAAAAAAAABnZW9nZWJyYV9tYWNyby54bWxQSwECFAAUAAgACAA4UBE9TIgurEQCAACVBAAADAAAAAAAAAAAAAAAAAC3CwAAZ2VvZ2VicmEueG1sUEsFBgAAAAACAAIAegAAADUOAAAAAA=="/>
                     63: ENDSPLINEPROGRAM
                     64: }
                     65: 
1.5       www        66: #
                     67: # The standard set of parameters inside <applet>
                     68: #
1.2       www        69: sub geogebra_default_parameters {
1.3       www        70:    my ($id)=@_;
1.2       www        71:    return(<<ENDDEFAULTPARAMETERS);
                     72:         <param name="image" value="/adm/lonIcons/lonanim.gif"  />
                     73:         <param name="boxborder" value="false"  />
                     74:         <param name="centerimage" value="true"  />
                     75:         <param name="java_arguments" value="-Xmx512m" />
                     76:         <param name="cache_archive" value="geogebra.jar, geogebra_main.jar, geogebra_gui.jar, geogebra_cas.jar, geogebra_export.jar, geogebra_properties.jar" />
                     77:         <param name="cache_version" value="3.2.44.0, 3.2.44.0, 3.2.44.0, 3.2.44.0, 3.2.44.0, 3.2.44.0" />
                     78:         <param name="framePossible" value="false" />
                     79: 
                     80:         <param name="showResetIcon" value="false" />
                     81:         <param name="showAnimationButton" value="false" />
                     82:         <param name="enableRightClick" value="false" />
                     83:         <param name="errorDialogsActive" value="true" />
                     84:         <param name="enableLabelDrags" value="false" />
                     85:         <param name="showMenuBar" value="false" />
                     86:         <param name="showToolBar" value="false" />
                     87:         <param name="showToolBarHelp" value="false" />
                     88:         <param name="showAlgebraInput" value="false" />
                     89:         <param name="enableShiftDragZoom" value="false" />
                     90:         <param name="allowRescaling" value="false" />
                     91:         <param name="enableLabelDrags" value="false" />
1.3       www        92:         <param name="ggbOnInitParam" value="applet_$id" />
1.2       www        93: ENDDEFAULTPARAMETERS
1.1       www        94: }
                     95: 
1.5       www        96: #
                     97: # This subroutine is called by LON-CAPA at </problem>
                     98: # Each applet on the page will call function ggbOnInit when it is done loading
                     99: # This function in turn will call the respective function registered by start_init_script
                    100: # Which one of the registered functions is called is determined by ggbOnInitParam, which GeoGebra passes to ggbOnInit
                    101: #
                    102: 
1.3       www       103: sub init_script {
                    104:    if ($Apache::functionplotresponse::callscripts) {
                    105:       return (<<ENDGGBINIT);
                    106: <script type="text/javascript">
                    107: // <![CDATA[
                    108: function ggbOnInit(param) {
                    109: $Apache::functionplotresponse::callscripts
                    110: }
                    111: // ]]>
                    112: </script>
                    113: ENDGGBINIT
                    114:    }
                    115: }
                    116: 
1.5       www       117: #
                    118: # Each Geogebra applet is supposed to call this when parameters change
                    119: #
                    120: sub update_script {
                    121:     my ($id)=@_;
                    122:     return (<<ENDUPDATESCRIPT);
                    123: <script type="text/javascript">
                    124: // <![CDATA[
                    125: function updatePointCoordinates_$id(coordinateName) {
                    126:             var x = document.ggbApplet_$id.getXcoord(coordinateName);
                    127:             var y = document.ggbApplet_$id.getYcoord(coordinateName);
1.9     ! www       128:             document.lonhomework.elements["HWVAL_$id\_" + coordinateName + "_x"].value = x;
        !           129:             document.lonhomework.elements["HWVAL_$id\_" + coordinateName + "_y"].value = y;
1.5       www       130:         }
                    131: // ]]>
                    132: </script>
                    133: ENDUPDATESCRIPT
                    134: }
                    135: 
                    136: #
                    137: # Register the above update-handler for a variable
                    138: #
                    139: 
                    140: sub update_register {
                    141:    my ($id,$variable)=@_;
1.6       www       142:    return "document.ggbApplet_$id.registerObjectUpdateListener('$variable','updatePointCoordinates_$id');\n";
                    143: }
                    144: 
                    145: #
                    146: # Set a coordinate variable
                    147: #
                    148: sub set_coordinate {
                    149:    my ($id,$variable,$x,$y)=@_;
                    150:    return (<<ENDSETVARIABLE);
1.9     ! www       151: document.ggbApplet_$id.evalCommand("$variable=($x,$y)");
1.6       www       152: ENDSETVARIABLE
                    153: }
                    154: 
                    155: #
1.9     ! www       156: # Input field name for a coordinate variable
        !           157: #
        !           158: 
        !           159: sub field_name {
        !           160:     my ($id,$variable,$name)=@_;
        !           161:     return "HWVAL_$id\_$variable\_$name";
        !           162: }
        !           163: 
        !           164: #
        !           165: # Generate an input field for a coordinate variable
        !           166: #
        !           167: 
        !           168: sub generate_input_field {
        !           169:     my ($id,$variable,$x,$y)=@_;
        !           170:     $Apache::functionplotresponse::inputfields.=
        !           171:        "<input type='hidden' name='".&field_name($id,$variable,'x')."' value='$x' />\n".
        !           172:        "<input type='hidden' name='".&field_name($id,$variable,'y')."' value='$y' />\n";
        !           173: }
        !           174: 
        !           175: #
1.6       www       176: # Initialize a new coordinate variable at set a listener on it
                    177: #
                    178: sub new_coordinate {
                    179:     my ($id,$variable,$x,$y)=@_;
1.9     ! www       180:     &generate_input_field($id,$variable,$x,$y);
1.6       www       181:     return &set_coordinate($id,$variable,$x,$y).&update_register($id,$variable);
1.5       www       182: }
                    183: 
                    184: #
                    185: # This registers the init-function call for ggbOnInit, which LON-CAPA will place at </problem>
                    186: # It then starts the right headers
                    187: #
1.3       www       188: sub start_init_script {
                    189:     my ($id)=@_;
1.5       www       190: # Add a line to ggbOnInit-function which calls the right function based on parameter passed from GeoGebra
1.3       www       191:     $Apache::functionplotresponse::callscripts.="if (param=='applet_$id') { ggbInit_$id(); }\n";
1.5       www       192: # ... and open this function
1.3       www       193:     return (<<ENDSTARTINIT);
                    194: <script type="text/javascript">
                    195: // <![CDATA[
1.5       www       196: // variable that will eventually be passed back to the server
                    197: var coordinateMap_$id = [];
                    198: // Init-function for applet
1.3       www       199: function ggbInit_$id() {
                    200: ENDSTARTINIT
                    201: }
                    202: 
1.5       www       203: #
                    204: # This sets the axes inside ggbInit_$id
                    205: #
                    206: 
1.4       www       207: sub axes_script {
                    208:     my ($id,$xmin,$xmax,$ymin,$ymax,$xvisible,$yvisible,$gvisible)=@_;
                    209:     return (<<ENDAXESSCRIPT);
                    210:             // changes (xmin, xmax, ymin, ymax)
                    211:             document.ggbApplet_$id.setCoordSystem($xmin,$xmax,$ymin,$ymax);
                    212: 
                    213:             // makes the (x,y) axis (in)visible
                    214:             document.ggbApplet_$id.setAxesVisible($xvisible,$yvisible);
                    215:             // makes the grid (in)visible
                    216:             document.ggbApplet_$id.setGridVisible($gvisible);
                    217: ENDAXESSCRIPT
                    218: }
                    219: 
                    220: sub plot_script {
1.6       www       221:    my ($id,$function,$fixed)=@_;
                    222:    if ($fixed) {
                    223: # Use stupid trick to nail this to a location
                    224:       return (<<ENDPLOTSCRIPT);
                    225:             document.ggbApplet_$id.evalCommand("a=1");
                    226:             document.ggbApplet_$id.setVisible("a",false);
                    227:             // This MUST be a DEPENDENT OBJECT in order to be locked down:
                    228:             document.ggbApplet_$id.evalCommand("y=a*($function)");
1.4       www       229: ENDPLOTSCRIPT
1.6       www       230:    } else {
                    231:        return "document.ggbApplet_$id.evalCommand('y=$function')";
                    232:    }
1.4       www       233: }
                    234: 
1.5       www       235: #
1.7       www       236: # Subroutine that generates code for spline $label based on stored information
1.6       www       237: #
                    238: 
                    239: sub generate_spline {
                    240:    my ($id,$label)=@_;
                    241:    my $result='';
1.7       www       242:    my $order=$Apache::functionplotresponse::splineorder{$label};
                    243:    my $x=$Apache::functionplotresponse::splineinitx{$label};
                    244:    my $y=$Apache::functionplotresponse::splineinity{$label};
                    245:    my $sx=$Apache::functionplotresponse::splinescalex{$label};
                    246:    my $sy=$Apache::functionplotresponse::splinescaley{$label};
                    247:    my @coords=();
                    248:    foreach my $i (1..$order) {
                    249:        $result.=&new_coordinate($id,$label.'P'.$i,$x,$y);
1.8       www       250:        $x+=$sx/(2.*($order-1));
1.7       www       251:        push(@coords,$label.'P'.$i);
                    252:        $result.=&new_coordinate($id,$label.'S'.$i,$x,$y+$sy);
1.8       www       253:        $x+=$sx/(2.*($order-1));
1.7       www       254:        push(@coords,$label.'S'.$i);
                    255:    }
                    256:    $result.='document.ggbApplet_'.$id.'.evalCommand("Spline'.$order.'['.join(',',@coords).']");'."\n";
1.6       www       257:    return $result;
                    258: }
                    259: #
                    260: # <backgroundplot function="..." fixed="yes/no" />
1.5       www       261: #
1.4       www       262: sub start_backgroundplot {
                    263:    my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    264:    my $result='';
                    265:    my $internalid = $Apache::inputtags::part.'_'.$Apache::inputtags::response[-1];
                    266:    my $function=&Apache::lonxml::get_param('function',$parstack,$safeeval);
1.6       www       267:    my $fixed=(&Apache::lonxml::get_param('fixed',$parstack,$safeeval)=~/on|true|yes|1/i?1:0);
                    268:  
1.4       www       269:    unless ($function) { $function="0"; }
                    270:    if ($target eq 'web') {
1.6       www       271:       $result.=&plot_script($internalid,$function,$fixed);
1.4       www       272:    }
                    273:    return $result;
                    274: }
                    275: 
                    276: sub end_backgroundplot {
1.6       www       277:    return '';
                    278: }
                    279: 
                    280: #
                    281: # <spline label="..." order="1,2,3,4" initx="..." inity="..." scalex="..." scaley="..." />
                    282: #
                    283: # Unfortunately, GeoGebra seems to want all splines after everything else, so we need to store them
                    284: #
                    285: sub start_spline {
                    286:    my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    287:    my $label=&Apache::lonxml::get_param('label',$parstack,$safeeval);
                    288:    $Apache::functionplotresponse::counter++;
                    289:    unless ($label) { $label='S'.$Apache::functionplotresponse::counter; }
                    290: 
                    291:    my $order=&Apache::lonxml::get_param('order',$parstack,$safeeval);
1.8       www       292:    if ($order<2) { $order=2; }
                    293:    if ($order>8) { $order=8; }
1.6       www       294:    $Apache::functionplotresponse::splineorder{$label}=$order;
                    295: 
                    296:    my $x=&Apache::lonxml::get_param('initx',$parstack,$safeeval);
                    297:    unless ($x) { $x=0; }
                    298:    $Apache::functionplotresponse::splineinitx{$label}=$x;
                    299: 
                    300:    my $y=&Apache::lonxml::get_param('inity',$parstack,$safeeval);
                    301:    unless ($y) { $y=0; }
                    302:    $Apache::functionplotresponse::splineinity{$label}=$y;
                    303: 
                    304:    my $sx=&Apache::lonxml::get_param('scalex',$parstack,$safeeval);
                    305:    unless ($sx) { $sx=$order; }
                    306:    $Apache::functionplotresponse::splinescalex{$label}=$sx;
                    307: 
                    308:    my $sy=&Apache::lonxml::get_param('scaley',$parstack,$safeeval);
                    309:    unless ($sy) { $sy=2; }
                    310:    $Apache::functionplotresponse::splinescaley{$label}=$sy;
1.7       www       311:    return '';
1.6       www       312: }
                    313: 
                    314: sub end_spline {
                    315:    return '';
1.4       www       316: }
                    317:  
1.3       www       318: sub end_init_script {
                    319:     return (<<ENDENDINIT);
                    320: }
                    321: // ]]>
                    322: </script>
                    323: ENDENDINIT
                    324: }
                    325: 
1.1       www       326: sub start_functionplotresponse {
                    327:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    328:   my $result='';
1.6       www       329: # To remember the splines - somehow, they need to come last
                    330:   undef %Apache::functionplotresponse::splineorder;
                    331:   undef %Apache::functionplotresponse::splineinitx;
                    332:   undef %Apache::functionplotresponse::splineinity;
                    333:   undef %Apache::functionplotresponse::splinescalex;
                    334:   undef %Apache::functionplotresponse::splinescaley;
1.9     ! www       335:   $Apache::functionplotresponse::inputfields='';
1.6       www       336:   $Apache::functionplotresponse::counter=0;
                    337: # Internal ID to mark the applet and its coordinates
1.3       www       338:   my $internalid = $Apache::inputtags::part.'_'.&Apache::response::start_response($parstack,$safeeval);
1.6       www       339: # Parameters of <functionplotresponse>
1.3       www       340:   my $xmin=&Apache::lonxml::get_param('xmin',$parstack,$safeeval);
1.4       www       341:   $xmin=($xmin?$xmin:-10);
1.3       www       342:   my $xmax=&Apache::lonxml::get_param('xmax',$parstack,$safeeval);
1.4       www       343:   $xmax=($xmax?$xmax:10);
1.3       www       344:   my $ymin=&Apache::lonxml::get_param('ymin',$parstack,$safeeval);
1.4       www       345:   $ymin=($ymin?$ymin:-10);
1.3       www       346:   my $ymax=&Apache::lonxml::get_param('ymax',$parstack,$safeeval);
1.4       www       347:   $ymax=($ymax?$ymax:10);
1.6       www       348:   my $xaxisvisible=(&Apache::lonxml::get_param('xaxisvisible',$parstack,$safeeval)=~/on|true|yes|1/i?'true':'false');
                    349:   my $yaxisvisible=(&Apache::lonxml::get_param('yaxisvisible',$parstack,$safeeval)=~/on|true|yes|1/i?'true':'false');
                    350:   my $gridvisible=(&Apache::lonxml::get_param('gridvisible',$parstack,$safeeval)=~/on|true|yes|1/i?'true':'false');
1.3       www       351: 
1.1       www       352:   if ($target eq 'web') {
1.5       www       353: # paste in the update routine to receive stuff back from the applet
                    354:      $result.=&update_script($internalid);
                    355: # start the initscript for this applet
1.3       www       356:      $result.=&start_init_script($internalid);
1.5       www       357: # put the axis commands inside
1.4       www       358:      $result.=&axes_script($internalid,$xmin,$xmax,$ymin,$ymax,$xaxisvisible,$yaxisvisible,$gridvisible);
1.5       www       359: # init script is left open
1.1       www       360:   }
                    361:   return $result;
                    362: }
                    363: 
                    364: sub end_functionplotresponse {
                    365:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    366:   &Apache::response::end_response;
1.3       www       367: 
1.1       www       368:   my $result;
1.3       www       369:   my $internalid = $Apache::inputtags::part.'_'.$Apache::inputtags::response[-1];
                    370: 
1.1       www       371: #  if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
                    372:   if ($target eq 'web') {
1.6       www       373: # Now is the time to render all of the stored splines
                    374:      foreach my $label (keys(%Apache::functionplotresponse::splineorder)) {
                    375:         $result.=&generate_spline($internalid,$label);
                    376:      }
1.5       www       377: # close the init script
1.3       www       378:      $result.=&end_init_script();
1.9     ! www       379: # generate the input fields
        !           380:      $result.=$Apache::functionplotresponse::inputfields;
1.5       www       381: # actually start the <applet>-tag
1.3       www       382:      $result.=&geogebra_startcode($internalid);
1.5       www       383: # load the spline bytecode
1.3       www       384:      $result.=&geogebra_spline_program();
1.5       www       385: # set default parameters
1.3       www       386:      $result.=&geogebra_default_parameters($internalid);
1.5       www       387: # close the <applet>-tag
1.1       www       388:      $result.=&geogebra_endcode();
                    389:   }
                    390:   return $result;
                    391: }
                    392: 
                    393: 1;
                    394: 
                    395: __END__
                    396:  
                    397: =head1 NAME
                    398: 
                    399: Apache::functionplotresponse.pm;
                    400: 
                    401: =head1 SYNOPSIS
                    402: 
                    403: Handles tags associated with accepting function plots.
                    404: 
                    405: This is part of the LearningOnline Network with CAPA project
                    406: described at http://www.lon-capa.org.
                    407: 
                    408: =head1 HANDLER SUBROUTINE
                    409: 
                    410: start_functionplotresponse()
                    411: 
                    412: =head1 OTHER SUBROUTINES
                    413: 
                    414: =over
                    415: 
                    416: =item end_functionplotresponse()
                    417: 
                    418: =back
                    419: 
                    420: =cut

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>