Diff for /loncom/homework/functionplotresponse.pm between versions 1.59 and 1.71

version 1.59, 2011/04/04 21:06:46 version 1.71, 2011/11/18 17:44:07
Line 1 Line 1
 # LearningOnline Network with CAPA  # LearningOnline Network with CAPA
 # option list style responses  # Functionplot responses
 #  #
 # $Id$  # $Id$
 #  #
Line 31  use strict; Line 31  use strict;
 use Apache::response();  use Apache::response();
 use Apache::lonlocal;  use Apache::lonlocal;
 use Apache::lonnet;  use Apache::lonnet;
   use Apache::run;
     
 BEGIN {  BEGIN {
   &Apache::lonxml::register('Apache::functionplotresponse',('functionplotresponse','backgroundplot','spline',    &Apache::lonxml::register('Apache::functionplotresponse',('functionplotresponse','backgroundplot','spline',
                                                               'plotobject','plotvector',
                                                             'functionplotrule','functionplotruleset',                                                              'functionplotrule','functionplotruleset',
                                                             'functionplotelements'));                                                              'functionplotelements'));
 }  }
Line 74  sub geogebra_default_parameters { Line 76  sub geogebra_default_parameters {
         <param name="image" value="/adm/lonIcons/lonanim.gif"  />          <param name="image" value="/adm/lonIcons/lonanim.gif"  />
         <param name="boxborder" value="false"  />          <param name="boxborder" value="false"  />
         <param name="centerimage" value="true"  />          <param name="centerimage" value="true"  />
  <param name="cache_archive" value="geogebra.jar, geogebra_main.jar, geogebra_gui.jar, geogebra_cas.jar, geogebra_algos.jar, geogebra_export.jar, geogebra_javascript.jar, jlatexmath.jar, jlm_greek.jar, jlm_cyrillic.jar, geogebra_properties.jar" />   <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" />
  <param name="cache_version" value="3.9.214.0, 3.9.214.0, 3.9.214.0, 3.9.214.0, 3.9.214.0, 3.9.214.0, 3.9.214.0, 3.9.214.0, 3.9.214.0, 3.9.214.0, 3.9.214.0" />   <param name="cache_version" value="4.0.1.0,4.0.1.0,4.0.1.0,4.0.1.0,4.0.1.0,4.0.1.0,4.0.1.0,4.0.1.0,4.0.1.0,4.0.1.0,4.0.1.0" />
         <param name="framePossible" value="false" />          <param name="framePossible" value="false" />
   
         <param name="showResetIcon" value="false" />          <param name="showResetIcon" value="false" />
Line 163  sub update_register { Line 165  sub update_register {
 sub set_point_coordinate {  sub set_point_coordinate {
    my ($id,$variable,$x,$y,$fixed)=@_;     my ($id,$variable,$x,$y,$fixed)=@_;
    my $mult=($fixed?'a*':'');     my $mult=($fixed?'a*':'');
   # Get rid of wild exponents, make sure it's a number
      $x=1.*$x;
      $y=1.*$y;
   # GeoGebra does not understand "E"
      $x=~s/[e|E]/\*10\^/;
      $x=~s/\+//;
      $y=~s/[e|E]/\*10\^/;
      $y=~s/\+//;
    return (<<ENDSETVARIABLE);     return (<<ENDSETVARIABLE);
 document.ggbApplet_$id.evalCommand("a=1");  document.ggbApplet_$id.evalCommand("a=1");
 document.ggbApplet_$id.evalCommand("$variable=$mult($x,$y)");  document.ggbApplet_$id.evalCommand("$variable=$mult($x,$y)");
Line 370  sub plot_script { Line 380  sub plot_script {
 }  }
   
 #  #
   # Subroutine to produce objects
   #
   
   sub plotobject_script {
      my ($id,$label,$x,$y)=@_;
      unless ($label) {
         $Apache::functionplotresponse::counter++;
         $label='O'.$Apache::functionplotresponse::counter;
      }
      return "document.ggbApplet_$id.evalCommand('a=1');\n".
             "document.ggbApplet_$id.setVisible('a', false);\n".
             "document.ggbApplet_$id.setLabelVisible('a', false);\n".
             "document.ggbApplet_$id.evalCommand('$label=a*($x,$y)');\n".
             "document.ggbApplet_$id.setVisible('$label', true);\n".
             "document.ggbApplet_$id.setLabelVisible('$label', true);\n";
   }
   
   #
   # Subroutine to produce vectors
   #
   
   sub plotvector_script {
      my ($id,$label,$xs,$ys,$xe,$ye)=@_;
      unless ($label) {
         $Apache::functionplotresponse::counter++;
         $label='V'.$Apache::functionplotresponse::counter;
      }
      my $startlabel=$label.'Start';
      my $endlabel=$label.'End';
      my $pointlabel=$label.'Point';
      my $anglelabel=$label.'Angle';
      return(<<ENDVECTOR);
   document.ggbApplet_$id.evalCommand("$startlabel=($xs,$ys)");
   document.ggbApplet_$id.setVisible("$startlabel",false);
   document.ggbApplet_$id.setLabelVisible("$startlabel",false);
   document.ggbApplet_$id.evalCommand("$endlabel=($xe,$ye)");
   document.ggbApplet_$id.setLabelVisible("$endlabel",false);
   document.ggbApplet_$id.evalCommand("$label=Vector[$startlabel,$endlabel]");
   document.ggbApplet_$id.setLabelVisible("$label",true);
   document.ggbApplet_$id.setLineThickness("$label",8);
   // Displays the Angle
   //document.ggbApplet1.evalCommand("Gravitypoint=(110,y(Gravitystart))"); //The x-value for this should be 2*(xmax-xmin)+xmax;
   //document.ggbApplet1.evalCommand("GravityAngle=Angle[Gravitypoint,Gravitystart,Gravityend]");
   //document.ggbApplet1.setLabelVisible("GravityAngle",true);
   //document.ggbApplet1.setLabelStyle("GravityAngle",VALUE=2);
   // Keeps track of points we care about (This should use the same listener function we use in graph problems)
   //document.ggbApplet1.registerObjectUpdateListener('Gravitystart','updatePointCoordinates');
   //document.ggbApplet1.registerObjectUpdateListener('Gravityend','updatePointCoordinates');
   //document.ggbApplet1.registerObjectUpdateListener('GravityAngle','updatePointCoordinates');
   ENDVECTOR
   }
   
   #
 # Answer spline display  # Answer spline display
 #   # 
 # points: x,y,slope_x,slope_y  # points: x,y,slope_x,slope_y
Line 380  sub answer_spline_script { Line 443  sub answer_spline_script {
    if ($order<2) { $order=2; }     if ($order<2) { $order=2; }
    if ($order>8) { $order=8; }     if ($order>8) { $order=8; }
    $Apache::functionplotresponse::counter++;     $Apache::functionplotresponse::counter++;
    my $label='C'.$Apache::functionplotresponse::counter;     my $label='CSpline'.$Apache::functionplotresponse::counter;
    my $output='document.ggbApplet_'.$id.'.evalCommand("'.$label.'=Spline'.$order.'[';     my $output='document.ggbApplet_'.$id.'.evalCommand("'.$label.'=Spline'.$order.'[';
    for (my $i=0;$i<=$#points;$i+=4) {     for (my $i=0;$i<=$#points;$i+=4) {
       $output.="($points[$i],$points[$i+1]),($points[$i+2],$points[$i+3]),";        $output.="($points[$i],$points[$i+1]),($points[$i+2],$points[$i+3]),";
Line 388  sub answer_spline_script { Line 451  sub answer_spline_script {
    $output=~s/\,$//;     $output=~s/\,$//;
    $output.=']");'."\n";     $output.=']");'."\n";
    for (my $i=2; $i<2*$order; $i+=2) {     for (my $i=2; $i<2*$order; $i+=2) {
        $output.='document.ggbApplet_'.$id.'.setColor("'.$label.'_'.$i.'",0,170,0);'."\n";         $output.='document.ggbApplet_'.$id.'.setColor("'.$label.'_'.($i>=10?'{':'').$i.($i>=10?'}':'').'",0,170,0);'."\n";
    }     }
      for (my $i=1; $i<2*$order; $i+=2) {
          $output.='document.ggbApplet_'.$id.'.setVisible("'.$label.'_'.($i>=10?'{':'').$i.($i>=10?'}':'').'",false);'."\n";
      }
   
    return $output;     return $output;
 }  }
   
Line 418  sub generate_spline { Line 485  sub generate_spline {
    $result.='document.ggbApplet_'.$id.'.evalCommand("Spline'.$order.'['.join(',',@coords).']");'."\n";     $result.='document.ggbApplet_'.$id.'.evalCommand("Spline'.$order.'['.join(',',@coords).']");'."\n";
    return $result;     return $result;
 }  }
   
   #
   # Object
   #
   
   sub start_plotobject {
      my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
      my $result='';
      my $internalid = $Apache::inputtags::part.'_'.$Apache::inputtags::response[-1];
      my $x=&Apache::lonxml::get_param('x',$parstack,$safeeval);
      my $y=&Apache::lonxml::get_param('y',$parstack,$safeeval);
      my $label=&Apache::lonxml::get_param('label',$parstack,$safeeval);
      $label=~s/\W//gs;
      $label=ucfirst($label);
      unless ($label) { $label="NewObject"; }
      if ($target eq 'web') {
         my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-3);
         unless (defined($x)) { $x=$xmin; }
         unless (defined($y)) { $y=$ymin; }
         $result.=&plotobject_script($internalid,$label,$x,$y);
      } elsif ($target eq 'edit') {
           $result=&Apache::edit::tag_start($target,$token,'Plot Object').
                &Apache::edit::text_arg('Label on Plot:','label',
                                        $token,'16').
                &Apache::edit::text_arg('x:','x',
                                        $token,'8').
                &Apache::edit::text_arg('y:','y',
                                        $token,'8').
                &Apache::edit::end_row();
     } elsif ($target eq 'modified') {
       my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'label','x','y');
       if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
     }
     return $result;
   }
   
   sub end_plotobject {
      my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
      my $result='';
      if ($target eq 'edit') {
          $result=&Apache::edit::end_table();
      }
      return $result;
   }
   
   #
   # Vector
   #
   
   sub start_plotvector {
      my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
      my $result='';
      my $internalid = $Apache::inputtags::part.'_'.$Apache::inputtags::response[-1];
      my $tailx=&Apache::lonxml::get_param('tailx',$parstack,$safeeval);
      my $taily=&Apache::lonxml::get_param('taily',$parstack,$safeeval);
      my $tipx=&Apache::lonxml::get_param('tipx',$parstack,$safeeval);
      my $tipy=&Apache::lonxml::get_param('tipy',$parstack,$safeeval);
   
      my $label=&Apache::lonxml::get_param('label',$parstack,$safeeval);
      $label=~s/\W//gs;
      $label=ucfirst($label);
      unless ($label) { $label="NewVector"; }
      if ($target eq 'web') {
         my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-3);
         unless (defined($tailx)) { $tailx=$xmin; }
         unless (defined($taily)) { $taily=$ymin; }
         unless (defined($tipx)) { $tipx=$xmin; }
         unless (defined($tipy)) { $tipy=$ymin; }
         $result.=&plotvector_script($internalid,$label,$tailx,$taily,$tipx,$tipy);
      } elsif ($target eq 'edit') {
           $result=&Apache::edit::tag_start($target,$token,'Plot Vector').
                &Apache::edit::text_arg('Label on Plot:','label',
                                        $token,'16').
                &Apache::edit::text_arg('Tail x:','tailx',
                                        $token,'8').
                &Apache::edit::text_arg('Tail y:','taily',
                                        $token,'8').
                &Apache::edit::text_arg('Tip x:','tipx',
                                        $token,'8').
                &Apache::edit::text_arg('Tip y:','tipy',
                                        $token,'8').
   
                &Apache::edit::end_row();
     } elsif ($target eq 'modified') {
       my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'label','tailx','taily','tipx','tipy');
       if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
     }
     return $result;
   }
   
   sub end_plotvector {
      my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
      my $result='';
      if ($target eq 'edit') {
          $result=&Apache::edit::end_table();
      }
      return $result;
   }
   
   
   
 #  #
 # <backgroundplot function="..." fixed="yes/no" />  # <backgroundplot function="..." fixed="yes/no" />
 #  #
Line 559  sub start_functionplotrule { Line 727  sub start_functionplotrule {
                                    ['lt','less than'],                                     ['lt','less than'],
                                    ['le','less than or equal']],$token).                                     ['le','less than or equal']],$token).
              $result.= &Apache::edit::select_or_text_arg('Value:','value',               $result.= &Apache::edit::select_or_text_arg('Value:','value',
                                                [['undef','not defined']],$token,'8').                                                 [['undef','not defined']],$token,'30').
              &Apache::edit::text_arg('Percent error:','percenterror',               &Apache::edit::text_arg('Percent error:','percenterror',
                                      $token,'8').                                       $token,'8').
              &Apache::edit::end_row();               &Apache::edit::end_row();
Line 747  sub array_index { Line 915  sub array_index {
 #  #
   
 sub populate_arrays {  sub populate_arrays {
     my ($id,$xmin,$xmax)=@_;      my ($id,$xmin,$xmax,$ymin,$ymax)=@_;
     for (my $i=0; $i<=400; $i++) {      for (my $i=0; $i<=400; $i++) {
        $Apache::functionplotresponse::actualxval[$i]=undef;         $Apache::functionplotresponse::actualxval[$i]=undef;
        $Apache::functionplotresponse::func[$i]=undef;         $Apache::functionplotresponse::func[$i]=undef;
Line 775  sub populate_arrays { Line 943  sub populate_arrays {
                 my $xi=&array_index($xmin,$xmax,$xreal);                  my $xi=&array_index($xmin,$xmax,$xreal);
                 if ($xi<$xiold) { return 'no_func'; }                  if ($xi<$xiold) { return 'no_func'; }
                 if (($xi>$xiold) && ($xi>=0) && ($xi<=400)) {                  if (($xi>$xiold) && ($xi>=0) && ($xi<=400)) {
                    if (defined($Apache::functionplotresponse::func[$xi])) { return 'no_func'; }  
                    $xiold=$xi;                     $xiold=$xi;
                    $Apache::functionplotresponse::actualxval[$xi]=$xreal;                     $Apache::functionplotresponse::actualxval[$xi]=$xreal;
 # Function value  # Function value
                    my $funcval=&cubic_hermite($t,@yparms);                     my $funcval=&cubic_hermite($t,@yparms);
   
   # Do we already have a value for this point, and is it different from the new one?
                      if ((defined($Apache::functionplotresponse::func[$xi])) &&
                          (abs($Apache::functionplotresponse::func[$xi]-$funcval)>($ymax-$ymin)/100.)) { 
                          return 'no_func'; 
                      }
   # Okay, remember the new point
                    $Apache::functionplotresponse::func[$xi]=$funcval;                     $Apache::functionplotresponse::func[$xi]=$funcval;
   
                    if (defined($funcval)) {                     if (defined($funcval)) {
                       if ($xi<$Apache::functionplotresponse::functionplotrulelabels{'start'}) {                        if ($xi<$Apache::functionplotresponse::functionplotrulelabels{'start'}) {
                          $Apache::functionplotresponse::functionplotrulelabels{'start'}=$xi;                           $Apache::functionplotresponse::functionplotrulelabels{'start'}=$xi;
Line 876  sub start_functionplotresponse { Line 1051  sub start_functionplotresponse {
              &Apache::edit::select_arg('Grid visible:','gridvisible',               &Apache::edit::select_arg('Grid visible:','gridvisible',
                                   ['yes','no'],$token).'<br />'.                                    ['yes','no'],$token).'<br />'.
              &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,...):',               &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,...):',
                                          'answerdisplay',$token,'50');                                           'answerdisplay',$token,'50').
   
              &Apache::edit::end_row().&Apache::edit::start_spanning_row();               &Apache::edit::end_row().&Apache::edit::start_spanning_row();
   } elsif ($target eq 'modified') {    } elsif ($target eq 'modified') {
     my $constructtag=&Apache::edit::get_new_args($token,$parstack,      my $constructtag=&Apache::edit::get_new_args($token,$parstack,
Line 962  sub compare_rel { Line 1136  sub compare_rel {
   
 sub addlog {  sub addlog {
    my ($text)=@_;     my ($text)=@_;
      $text=~s/\'/\\\'/g;
    $Apache::functionplotresponse::ruleslog.=$text.'<br />';     $Apache::functionplotresponse::ruleslog.=$text.'<br />';
 }  }
   
Line 969  sub actualval { Line 1144  sub actualval {
    my ($i,$xmin,$xmax)=@_;     my ($i,$xmin,$xmax)=@_;
    return $xmin+$i/400.*($xmax-$xmin);     return $xmin+$i/400.*($xmax-$xmin);
 }  }
   
   sub fpr_val {
      my ($arg)=@_;
      return &actualval($Apache::functionplotresponse::functionplotrulelabels{$arg},
                        $Apache::functionplotresponse::fpr_xmin,
                        $Apache::functionplotresponse::fpr_xmax);
   }
   
   sub fpr_f {
      my ($arg)=@_;
      return $Apache::functionplotresponse::func[&array_index($Apache::functionplotresponse::fpr_xmin,
                                                              $Apache::functionplotresponse::fpr_xmax,
                                                              $arg)];
   }
   
   sub fpr_dfdx {
      my ($arg)=@_;
      return $Apache::functionplotresponse::dfuncdx[&array_index($Apache::functionplotresponse::fpr_xmin,
                                                                 $Apache::functionplotresponse::fpr_xmax,
                                                                 $arg)];
   }
   
   sub fpr_d2fdx2 {
      my ($arg)=@_;
      return $Apache::functionplotresponse::d2funcdx2[&array_index($Apache::functionplotresponse::fpr_xmin,
                                                                   $Apache::functionplotresponse::fpr_xmax,
                                                                   $arg)];
   }
     
 sub functionplotrulecheck {  sub functionplotrulecheck {
    my ($rule,$xmin,$xmax,$ymin,$ymax)=@_;     my ($rule,$xmin,$xmax,$ymin,$ymax,$safeeval)=@_;
   
    my ($label,$derivative,$xinitial,$xinitiallabel,$xfinal,$xfinallabel,$minimumlength,$maximumlength,$relationship,$value,$percent)     my ($label,$derivative,$xinitial,$xinitiallabel,$xfinal,$xfinallabel,$minimumlength,$maximumlength,$relationship,$value,$percent)
       =split(/\:/,$rule);        =split(/\:/,$rule);
    $percent=($percent>0?$percent:5);     $percent=($percent>0?$percent:5);
    &addlog("=================");     &addlog("=================");
    &addlog("Rule $label for ".($derivative<0?'integral':('function itself','first derivative','second derivative')[$derivative])." $relationship $value");     &addlog("Rule $label for ".($derivative<0?'integral':('function itself','first derivative','second derivative')[$derivative])." $relationship $value");
   #
   # Evaluate the value
   #
      if ($value=~/\D/) {
         $Apache::functionplotresponse::fpr_xmin=$xmin;
         $Apache::functionplotresponse::fpr_xmax=$xmax;
         $value=&Apache::run::run($value,$safeeval);
         &addlog("Value evaluated to $value");
      }
   
   #
   # Minimum and maximum lengths of the interval
   #
    if ((defined($minimumlength)) || (defined($maximumlength))) {     if ((defined($minimumlength)) || (defined($maximumlength))) {
       &addlog("Minimumlength $minimumlength Maximumlength $maximumlength");        &addlog("Minimumlength $minimumlength Maximumlength $maximumlength");
    }     }
Line 1166  sub end_functionplotruleset { Line 1382  sub end_functionplotruleset {
         $Apache::functionplotresponse::ruleslog='';          $Apache::functionplotresponse::ruleslog='';
         $Apache::functionplotresponse::functionplotrulelabels{'start'}=400;          $Apache::functionplotresponse::functionplotrulelabels{'start'}=400;
         $Apache::functionplotresponse::functionplotrulelabels{'end'}=0;          $Apache::functionplotresponse::functionplotrulelabels{'end'}=0;
         if (&populate_arrays($internalid,$xmin,$xmax) eq 'no_func') {          if (&populate_arrays($internalid,$xmin,$xmax,$ymin,$ymax) eq 'no_func') {
            $ad='NOT_FUNCTION';             $ad='NOT_FUNCTION';
         } else {          } else {
            &addlog("Start of function ".&actualval($Apache::functionplotresponse::functionplotrulelabels{'start'},$xmin,$xmax)." (index ".             &addlog("Start of function ".&actualval($Apache::functionplotresponse::functionplotrulelabels{'start'},$xmin,$xmax)." (index ".
Line 1176  sub end_functionplotruleset { Line 1392  sub end_functionplotruleset {
   
 # We have a function that we can actually grade, go through the spline rules.  # We have a function that we can actually grade, go through the spline rules.
            foreach my $rule (@Apache::functionplotresponse::functionplotrules) {             foreach my $rule (@Apache::functionplotresponse::functionplotrules) {
               unless (&functionplotrulecheck($rule,$xmin,$xmax,$ymin,$ymax)) {                unless (&functionplotrulecheck($rule,$xmin,$xmax,$ymin,$ymax,$safeeval)) {
                  $ad='INCORRECT';                   $ad='INCORRECT';
                  last;                   last;
               }                }

Removed from v.1.59  
changed lines
  Added in v.1.71


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