[system] / trunk / webwork / system / courseScripts / PG_CAPAmacros.pl Repository:
ViewVC logotype

View of /trunk/webwork/system/courseScripts/PG_CAPAmacros.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 194 - (download) (as text) (annotate)
Sat Sep 8 04:51:25 2001 UTC (18 years, 3 months ago) by gage
File size: 5278 byte(s)
Fixed a bug in CAPA_ans which called the old style numerical compare
and string compare functions (including some useless parameters)

This will fix several errors that have arisen in CAPA problems, but
perhaps not all of them.

    1 #!/usr/local/bin/webwork-perl
    2 
    3 BEGIN {
    4   be_strict();
    5 }
    6 
    7 
    8 sub CAPA_ans {
    9   my $ans = shift;
   10   my %options = @_;
   11   my $answer_evaluator = 0;
   12 
   13     #TEXT("answerCounter =". ++$problemCounter,"$BR"); # checking whether values are reinitialized
   14   if (defined($options{'reltol'}) or defined($options{'tol'}) or defined($options{'unit'})  ) {
   15 
   16     if (defined( $options{'unit'} ) ) {
   17       #$ans = "$ans  $options{'unit'}";
   18       $answer_evaluator = num_cmp($ans, 'format'  =>  $options{format},
   19                                          reltol =>  ( defined($options{reltol}) ) ? $options{reltol} :undef,
   20                                          tol    =>  ( defined($options{tol})    ) ? $options{tol} : undef ,
   21                                          unit   =>  $options{unit},
   22       );
   23     } else { # numerical compare:
   24       if (defined($options{'reltol'})  ) {  #relative tolerance is given with a percent sign
   25               my $reltol =  $options{ 'reltol' };
   26               my $format = $options{'format'} if defined($options{'format'});
   27           $answer_evaluator = num_cmp($ans,reltol=>$reltol,'format' => $format);
   28       } elsif (defined($options{'tol'})  ) {
   29             my $format = $options{'format'} if defined($options{'format'});
   30           $answer_evaluator = num_cmp($ans,tol => $options{'tol'}, 'format' => $format);
   31         } else {
   32              my $tol = $ans*$main::numRelPercentTolDefault;
   33              my $format = $options{'format'} if defined($options{'format'});
   34              $answer_evaluator = num_cmp($ans,reltol=> $tol,'format'=> $format);
   35         }
   36     }
   37   } else {
   38      # string comparisons
   39      if ( defined($options{'str'})  and   $options{'str'} =~/CS/i )  {
   40             $answer_evaluator =str_cmp($ans,filters=>['compress_whitespace']);
   41      } elsif  ( defined($options{'str'}) and $options{'str'} =~/MC/i )  {
   42               $answer_evaluator = str_cmp($ans,filters=>[qw( compress_whitespace ignore_case ignore_order )]);
   43      } else {
   44               $answer_evaluator = str_cmp($ans,filters=>[qw( compress_whitespace ignore_case )]);
   45      }
   46   }
   47 
   48    $answer_evaluator;
   49 }
   50 
   51 sub CAPA_import {
   52   my $filePath = shift;
   53   my %save_envir = %main::envir;
   54   my $r_string =  read_whole_problem_file($filePath);
   55 
   56   $main::envir{'probFileName'} = $filePath;
   57   $main::envir{'fileName'} = $filePath;
   58   includePGtext($r_string); # the 0 prevents  a new Safe compartment from being used
   59   %main::envir = %save_envir;
   60 }
   61 
   62 
   63 
   64 sub CAPA_map {
   65   my $seed = shift;
   66     my $array_var_ref = shift;
   67     my $PGrand = new PGrandom($seed);
   68     local $main::array_values_ref = shift;   # this must be local since it must be passed to PG_restricted_eval
   69     my $size = @$main::array_values_ref;  # get number of values
   70   my @array = 0..($size-1);
   71   my @slice = ();
   72   while ( @slice < $size) {
   73     push(@slice, splice(@array , $PGrand->random(0,$#array) , 1) );
   74   }
   75     my $string = "";
   76     my $var;
   77     my $i = 0;
   78      foreach $var (@$array_var_ref) {
   79     $string .= "\$$var = \$\$main::array_values_ref[ $slice[$i++]]; ";
   80    }
   81 
   82      # it is important that PG-restriced eval can accesss the $array_values_ref
   83      my($val, $PG_eval_errors,$PG_full_error_report) =PG_restricted_eval($string);
   84      my $out = '';
   85      $string =~ s/\$/\\\$/g;  # protect variables for error message
   86      $out = "Error in MAP subroutine: $PG_eval_errors <BR>\n" . $string ." <BR>\n" if $PG_eval_errors;
   87      $out;
   88 }
   89 
   90 sub compare_units {
   91 
   92 
   93 }
   94 
   95 sub CAPA_hint {
   96     my $hint = shift;
   97   TEXT(hint(qq{ HINT:   $hint $main::BR}));
   98 }
   99 
  100 sub CAPA_explanation {
  101   TEXT(solution( qq{ $main::BR$main::BR EXPLANATION:   @_ $main::BR$main::BR} )) if solution(@_);
  102 }
  103 
  104 sub pow {
  105     my($base,$exponent)=@_;
  106     $base**$exponent;
  107 }
  108 sub CAPA_tex {
  109     my $tex = shift;
  110    # $tex =~ s|/*|\$|g;
  111     #$tex =~ s/\\/\\\\/g;   #protect backslashes???
  112     my $nontex = shift;
  113     &M3($tex, $nontex,$nontex);
  114 }
  115 sub CAPA_web {
  116   my $text = shift;
  117   my $tex  = shift;
  118   my $html = shift;
  119   &M3($tex,"\\begin{rawhtml}$html\\end{rawhtml}",$html);
  120 }
  121 sub CAPA_html {
  122   my $html = shift;
  123   &M3("","\\begin{rawhtml}$html\\end{rawhtml}",$html);
  124 }
  125 sub var_in_tex {
  126     my($tex)=$_[0];
  127     &M3( "$tex","$tex","");
  128 }
  129 
  130 
  131 
  132 sub isNumberQ {  # determine whether the input is a number
  133   my $in = shift;
  134   $in =~ /^[\d\.\+\-Ee]+$/;
  135 }
  136 
  137 sub choose {
  138    # my($in)=join(" ",@_);
  139     my($var)=$_[0];
  140     $_[$var];
  141 }
  142 
  143 sub problem {
  144   $main::probNum;
  145 }
  146 
  147 sub pin {
  148     $main::psvnNumber;
  149 }
  150 sub section {
  151     $main::sectionNumber;
  152 }
  153 sub name {
  154     $main::studentName;
  155 }
  156 sub set {
  157     $main::setNumber;
  158 }
  159 sub question {
  160     $main::probNum;
  161 }
  162 
  163 sub due_date {
  164   $main::formattedDueDate;
  165  }
  166 
  167 sub answer_date {
  168    $main::formattedAnswerDate;
  169 }
  170 sub open_date {
  171    $main::formattedOpenDate;
  172 }
  173 sub to_string {
  174     $_[0];
  175 }
  176 
  177 
  178 sub CAPA_EV {
  179 
  180    my $out = &EV3(@_);
  181    $out  =~ s/\n\n/\n/g;  # hack to prevent introduction of paragraphs in TeX??
  182    # HACK TO DO THE RIGHT THING WITH DOLLAR SIGNS
  183    $out = ev_substring($out,"/*/*","/*/*",\&display_math_ev3);
  184    $out = ev_substring($out,"/*","/*",\&math_ev3);
  185    # TEXT($main::BR, $main::BR, $out,$main::BR,$main::BR);
  186    $out;
  187 }
  188 
  189 # these are very commonly needed files
  190 CAPA_import("${main::CAPA_Tools}StdMacros");
  191 CAPA_import("${main::CAPA_Tools}StdUnits");
  192 CAPA_import("${main::CAPA_Tools}StdConst");
  193 #####################
  194 
  195 $main::prob_val="";     # gets rid of spurious errors.
  196 $main::prob_try="";
  197 
  198 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9