File:  [LON-CAPA] / loncom / homework / default_homework.lcpm
Revision 1.15: download - view: text, annotated - select for diffs
Tue Jan 16 20:15:40 2001 UTC (23 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- screwed up multiple response checking
- screwed up the unit parsing

    1: # file name (temp): default_homework 
    2: # used by lonxml::xmlparse() as input variable $safeinit to Apache::run::run()
    3: # 
    4: #
    5: $RANDOMINIT=0;
    6: 
    7: sub caparesponse_check {
    8:   my $answer='';  #done
    9:   my $type='';    #done
   10:   my $tol_type=''; # gets it's value from whether tol has a % or not done
   11:   my $tol='';     #done
   12:   my $sig='';
   13:   my $sig_lbound='';
   14:   my $sig_ubound='';
   15:   my $ans_fmt='';
   16:   my $units='';
   17:   my $calc='';
   18:   my ($response,$expr)=@_;
   19:   
   20:   ($answer,$type,$tol,$sig,$ans_fmt,
   21:    $units,$calc) = eval $expr.';return $answer,$type,$tol,$sig,$ans_fmt,$units,$calc';
   22:   #type's definitons come from capaParser.h
   23:   if ($type eq '' ) {
   24:     if ( $answer eq ($answer *1.0)) { $type = 2;
   25:     } else { $type = 3; }
   26:   } else {
   27:          if ($type eq 'cs')    { $type = 4;
   28:     } elsif ($type eq 'ci')    { $type = 3;
   29:     } elsif ($type eq 'mc')    { $type = 5;
   30:     } elsif ($type eq 'fml')   { $type = 8;
   31:     } elsif ($type eq 'subj')  { $type = 7;
   32:     } else { return "ERROR: Unknown type of answer: $type" }
   33:   }
   34: 
   35:   if ($tol eq '') {
   36:     $tol=0.0;
   37:     $tol_type=1; #TOL_ABSOLUTE
   38:   } else {
   39:     if ($tol =~ /%$/) {
   40:       chop $tol;
   41:       $tol_type=2; #TOL_PERCENTAGE
   42:     } else {
   43:       $tol_type=1; #TOL_ABSOLUTE
   44:     }
   45:   }
   46: 
   47:   if ($sig eq '') {
   48:     $sig_lbound = 0; #SIG_LB_DEFAULT
   49:     $sig_ubound =15; #SIG_UB_DEFAULT
   50:   } else {
   51:     ($sig_lbound,$sig_ubound) = split /,/,$sig;
   52:   }
   53:   my $result = &caparesponse_capa_check_answer($response,$answer,$type,
   54: 					       $tol_type,$tol,
   55: 					       $sig_lbound,$sig_ubound,
   56: 					       $ans_fmt,$units,$calc);
   57: 
   58:   if    ($result == '1') { $result='EXACT_ANS'; } 
   59:   elsif ($result == '2') { $result='APPROX_ANS'; }
   60:   elsif ($result == '3') { $result='SIG_FAIL'; }
   61:   elsif ($result == '4') { $result='UNIT_FAIL'; }
   62:   elsif ($result == '5') { $result='NO_UNIT'; }
   63:   elsif ($result == '6') { $result='UNIT_OK'; }
   64:   elsif ($result == '7') { $result='INCORRECT'; }
   65:   elsif ($result == '8') { $result='UNIT_NOTNEEDED'; }
   66:   elsif ($result == '9') { $result='ANS_CNT_NOT_MATCH'; }
   67:   elsif ($result =='10') { $result='SUB_RECORDED'; }
   68:   elsif ($result =='11') { $result='BAD_FORMULA'; }
   69:   elsif ($result =='12') { $result='WANTED_NUMERIC'; }
   70:   else  {$result = "ERROR: Unknown Result:$result:$@:";}
   71: 
   72:   return "$result:<br>Error $error:<br>Answer $answer:<br>Response $response:<br>$type|$tol|$tol_type|$sig:$sig_lbound:$sig_ubound|$units<br>";
   73: }
   74: 
   75: sub caparesponse_check_list {
   76:   my ($response,$expr)=@_;
   77:   my $result='';
   78:   my $aresult='';
   79:   my $current_answer;
   80:   my $answer = eval $expr.';return $answer';
   81:   my (@list) = eval $answer;
   82:   my (@responselist)=split /,/,$response;
   83:   my $unit='';
   84:   $result.="Final response :$responselist['-1']:<br>";
   85:   if ( $responselist['-1']=~ /(.*[^\s])\s+([^\s]+)/ ) {
   86:     $responselist['-1']=$1;
   87:     $unit=$2;
   88:   }
   89:   $result.="Final response :$responselist['-1']:<br>";
   90:   $unit=~s/\s//;
   91:   my $i=0;
   92:   my $awards='';
   93:   for ($i=0; $i<@list;$i++) {
   94:     $aresult=&caparesponse_check($responselist[$i]." $unit",
   95: 				 $expr.';my $answer='.$list[$i].';');
   96:     my ($temp)=split /:/, $aresult;
   97:     $awards.="$temp,";
   98:     $result.=$aresult;
   99:   }
  100:   chop $awards;
  101:   return "$awards:<br>$result";
  102: }
  103: 
  104: sub tex {
  105:   if ( $external::target eq "tex" ) {
  106:     return @_[0];
  107:   } else {
  108:     return @_[1];
  109:   }
  110: }
  111: 
  112: sub web {
  113:   if ( $external::target eq "tex" ) {
  114:     return @_[1];
  115:   } else {
  116:     if ( $external::target eq "web") {
  117:       return @_[0];
  118:     } else {
  119:       return @_[2];
  120:     }
  121:   }
  122: }
  123: 
  124: sub problem {
  125:   return '1';
  126: }
  127: 
  128: sub hinton {
  129:   return 0;
  130: }
  131: 
  132: sub random {
  133:   my ($start,$end,$step)=@_;
  134:   if ( ! $RANDOMINIT ) { srand($external::randomseed); $RANDOMINIT=1; }
  135:   my $num=1+int(($end-$start)/$step);
  136:   my $result=$start + int(rand() * $num)*$step;
  137:   return $result;
  138: }
  139: 
  140: sub tan  { sin($_[0]) / cos($_[0]) }
  141: sub acos { atan2(sqrt(1 - $_[0] * $_[0]), $_[0] ); }
  142: sub asin { atan2($_[0], sqrt(1- $_[0] * $_[0]) );  }
  143: 
  144: sub html {
  145:   if ( $external::target eq "web" ) {
  146:     return @_[0];
  147:   }
  148: }
  149: 
  150: 
  151: sub pow   {return $_[0] ** $_[1]; }
  152: sub ceil  {return (($_[0]-int($_[0]))== 0.0) ? $_[0] : (int($_[0])+ 1); }
  153: sub floor {return int($_[0]);  }
  154: 
  155: sub format {
  156:   my ($value,$fmt)=@_;
  157:   return sprintf('%.'.$fmt,$value);
  158: }
  159: 
  160: sub map {
  161:   my $num = $#_/2;
  162:   my $i;
  163:   my @used;
  164:   my @permutation;
  165:   for ($i=0; $i < $num;) {
  166:     $rand = &random(1,$num,1);
  167:     if ( $used[$rand] == '0' ) {
  168:       $i++;
  169:       $used[$rand]='1';
  170:       $permutation[$i]=$rand;
  171:     }
  172:   }
  173:   for ( $i=1 ; $i<$num+1 ; $i++ ) {
  174:     ${$_[$permutation[$i]]}=$_[$i+$num]
  175:   }
  176: }
  177: 
  178: sub choose {
  179:   my $num = $_[0];
  180:   return $_[$num];
  181: }
  182: #$external::randomseed=time;
  183: #$a=2;
  184: #$b=2;
  185: #$c=2;
  186: #&map(1,\$a,\$b,\$c,3,4,5);
  187: #print $a."\n".$b."\n";

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