[system] / trunk / pg / macros / PG_CAPAmacros.pl Repository:
ViewVC logotype

View of /trunk/pg/macros/PG_CAPAmacros.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6451 - (download) (as text) (annotate)
Tue Oct 12 00:04:19 2010 UTC (9 years, 3 months ago) by gage
File size: 5642 byte(s)
protect @ signs in user_id when naming graphs.
add Quiz prefix in MultiAnswer questions
replace psvnNumber by psvn


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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9