File:  [LON-CAPA] / loncom / homework / default_homework.lcpm
Revision 1.23: download - view: text, annotated - select for diffs
Wed May 30 21:49:13 2001 UTC (22 years, 11 months ago) by ng
Branches: MAIN
CVS tags: HEAD
name, section, class, id functions work.
perl intrinsic functions (sin, cos, atan2, log, exp, sqrt) can be called using &functionname
only normal_xxx functions left to do

    1: # file name (temp): default_homework 
    2: # used by lonxml::xmlparse() as input variable $safeinit to Apache::run::run()
    3: # 
    4: #
    5: #init some globals
    6: #
    7: # Guy Albertelli
    8: #
    9: # 05/25/2001 H. K. Ng
   10: #
   11: $RANDOMINIT=0;
   12: $pi=atan2(1,1)*4;
   13: $rad2deg=180.0/$pi;
   14: $deg2rad=$pi/180.0;
   15: 
   16: sub caparesponse_check {
   17:   my $answer='';  #done
   18:   my $type='';    #done
   19:   my $tol_type=''; # gets it's value from whether tol has a % or not done
   20:   my $tol='';     #done
   21:   my $sig='';     #done lowerbnd,upperbnd
   22:   my $sig_lbound=''; #done
   23:   my $sig_ubound=''; #done
   24:   my $ans_fmt='';
   25:   my $units='';     #done
   26:   my $calc='';
   27:   my ($response,$expr)=@_;
   28:   
   29:   ($answer,$type,$tol,$sig,$ans_fmt,
   30:    $units,$calc) = eval $expr.';return $answer,$type,$tol,$sig,$ans_fmt,$units,$calc';
   31:   #type's definitons come from capaParser.h
   32:   if ($type eq '' ) {
   33:     if ( $answer eq ($answer *1.0)) { $type = 2;
   34:     } else { $type = 3; }
   35:   } else {
   36:          if ($type eq 'cs')    { $type = 4;
   37:     } elsif ($type eq 'ci')    { $type = 3;
   38:     } elsif ($type eq 'mc')    { $type = 5;
   39:     } elsif ($type eq 'fml')   { $type = 8;
   40:     } elsif ($type eq 'subj')  { $type = 7;
   41:     } else { return "ERROR: Unknown type of answer: $type" }
   42:   }
   43: 
   44:   if ($tol eq '') {
   45:     $tol=0.0;
   46:     $tol_type=1; #TOL_ABSOLUTE
   47:   } else {
   48:     if ($tol =~ /%$/) {
   49:       chop $tol;
   50:       $tol_type=2; #TOL_PERCENTAGE
   51:     } else {
   52:       $tol_type=1; #TOL_ABSOLUTE
   53:     }
   54:   }
   55: 
   56:   if ($sig eq '') {
   57:     $sig_lbound = 0; #SIG_LB_DEFAULT
   58:     $sig_ubound =15; #SIG_UB_DEFAULT
   59:   } else {
   60:     ($sig_lbound,$sig_ubound) = split /,/,$sig;
   61:   }
   62:   my $result = &caparesponse_capa_check_answer($response,$answer,$type,
   63: 					       $tol_type,$tol,
   64: 					       $sig_lbound,$sig_ubound,
   65: 					       $ans_fmt,$units,$calc);
   66: 
   67:   if    ($result == '1') { $result='EXACT_ANS'; } 
   68:   elsif ($result == '2') { $result='APPROX_ANS'; }
   69:   elsif ($result == '3') { $result='SIG_FAIL'; }
   70:   elsif ($result == '4') { $result='UNIT_FAIL'; }
   71:   elsif ($result == '5') { $result='NO_UNIT'; }
   72:   elsif ($result == '6') { $result='UNIT_OK'; }
   73:   elsif ($result == '7') { $result='INCORRECT'; }
   74:   elsif ($result == '8') { $result='UNIT_NOTNEEDED'; }
   75:   elsif ($result == '9') { $result='ANS_CNT_NOT_MATCH'; }
   76:   elsif ($result =='10') { $result='SUB_RECORDED'; }
   77:   elsif ($result =='11') { $result='BAD_FORMULA'; }
   78:   elsif ($result =='12') { $result='WANTED_NUMERIC'; }
   79:   else  {$result = "ERROR: Unknown Result:$result:$@:";}
   80: 
   81:   return "$result:<br />Error $error:<br />Answer $answer:<br />Response $response:<br />$type|$tol|$tol_type|$sig:$sig_lbound:$sig_ubound|$units<br />";
   82: }
   83: 
   84: sub caparesponse_check_list {
   85:   my ($response,$expr)=@_;
   86:   my $result='';
   87:   my $aresult='';
   88:   my $current_answer;
   89:   my $answer = eval $expr.';return $answer';
   90:   my (@list) = eval $answer;
   91:   my (@responselist)=split /,/,$response;
   92:   my $unit='';
   93:   $result.="Final response :$responselist['-1']:<br />";
   94:   if ( $responselist['-1']=~ /(.*[^\s])\s+([^\s]+)/ ) {
   95:     $responselist['-1']=$1;
   96:     $unit=$2;
   97:   }
   98:   $result.="Final response :$responselist['-1']:<br />";
   99:   $unit=~s/\s//;
  100:   my $i=0;
  101:   my $awards='';
  102:   for ($i=0; $i<@list;$i++) {
  103:     if ($unit eq '') {
  104:       $aresult=&caparesponse_check($responselist[$i],
  105: 			     $expr.';my $answer='.$list[$i].';');
  106:     } else {
  107:       $aresult=&caparesponse_check($responselist[$i]." $unit",
  108: 				   $expr.';my $answer='.$list[$i].';');
  109:     }
  110:     my ($temp)=split /:/, $aresult;
  111:     $awards.="$temp,";
  112:     $result.=$aresult;
  113:   }
  114:   chop $awards;
  115:   return "$awards:<br />$result";
  116: }
  117: 
  118: sub tex {
  119:   if ( $external::target eq "tex" ) {
  120:     return @_[0];
  121:   } else {
  122:     return @_[1];
  123:   }
  124: }
  125: 
  126: sub web {
  127:   if ( $external::target eq "tex" ) {
  128:     return @_[1];
  129:   } else {
  130:     if ( $external::target eq "web") {
  131:       return @_[0];
  132:     } else {
  133:       return @_[2];
  134:     }
  135:   }
  136: }
  137: 
  138: sub problem {
  139:   return '1';
  140: }
  141: 
  142: sub hinton {
  143:   return 0;
  144: }
  145: 
  146: sub random {
  147:   my ($start,$end,$step)=@_;
  148:   if ( ! $RANDOMINIT ) { srand($external::randomseed); $RANDOMINIT=1; }
  149:   my $num=1+int(($end-$start)/$step);
  150:   my $result=$start + int(rand() * $num)*$step;
  151:   return $result;
  152: }
  153: 
  154: sub abs { abs(shift) }
  155: sub sin { sin(shift) }
  156: sub cos { cos(shift) }
  157: sub exp { exp(shift) }
  158: sub int { int(shift) }
  159: sub log { log(shift) }
  160: sub atan2 { atan2($_[0],$_[1]) }
  161: sub sqrt { sqrt(shift) }
  162: 
  163: sub tan  { sin($_[0]) / cos($_[0]) }
  164: #sub atan { atan2($_[0], 1); }
  165: #sub acos { atan2(sqrt(1 - $_[0] * $_[0]), $_[0] ); }
  166: #sub asin { atan2($_[0], sqrt(1- $_[0] * $_[0]) );  }
  167: 
  168: sub log10 { log($_[0])/log(10); }
  169: 
  170: sub factorial {
  171:     my $input = int(shift);
  172:     return "Error - unable to take factorial of an negative number ($input)" if $input < 0;
  173:     return "Error - factorial result is greater than system limit ($input)" if $input > 170;
  174:     return 1 if $input == 0;
  175:     my $result = 1; 
  176:     for (my $i=2; $i<=$input; $i++) { $result *= $i }
  177:     return $result;
  178: }
  179: 
  180: sub sgn {
  181:     return -1 if $_[0] < 0;
  182:     return 0 if $_[0] == 0;
  183:     return 1 if $_[0] > 0;
  184: }
  185: 
  186: sub min {
  187:     my @sorted = sort { $a <=> $b || $a cmp $b } @_;
  188:     return shift @sorted;
  189: }
  190: 
  191: sub max {
  192:     my @sorted = sort { $a <=> $b || $a cmp $b } @_;
  193:     return pop @sorted;
  194: }
  195: 
  196: sub html {
  197:   if ( $external::target eq "web" ) {
  198:     return @_[0];
  199:   }
  200: }
  201: 
  202: sub roundto {
  203:     my ($input,$n) = @_;
  204:     return sprintf('%.'.$n.'f',$input);
  205: }
  206: 
  207: sub to_string {
  208:     my ($input,$n) = @_;
  209:     return sprintf('%'.$n,$input) if $n ne "";
  210:     return sprintf($input) if $n eq "";
  211: }
  212: 
  213: sub sub_string {
  214:     my ($str,$start,$len) = @_;
  215:     return substr($str,$start-1,$len);
  216: }
  217: 
  218: sub pow   {return $_[0] ** $_[1]; }
  219: sub ceil  {return (($_[0]-int($_[0]))== 0.0) ? $_[0] : (int($_[0])+ 1); }
  220: sub floor {return int($_[0]);  }
  221: 
  222: sub format {
  223:   my ($value,$fmt)=@_;
  224:   return sprintf('%.'.$fmt,$value);
  225: }
  226: 
  227: sub map {
  228:   my $num = $#_/2;
  229:   my $i;
  230:   my @used;
  231:   my @permutation;
  232:   for ($i=0; $i < $num;) {
  233:     $rand = &random(1,$num,1);
  234:     if ( $used[$rand] == '0' ) {
  235:       $i++;
  236:       $used[$rand]='1';
  237:       $permutation[$i]=$rand;
  238:     }
  239:   }
  240:   for ( $i=1 ; $i<$num+1 ; $i++ ) {
  241:     ${$_[$permutation[$i]]}=$_[$i+$num]
  242:   }
  243: }
  244: 
  245: sub capa_id { return }
  246: 
  247: sub problem { return }
  248: 
  249: sub name{
  250:   my $fullname = &EXT('environment.lastname').', '.&EXT('environment.firstname').' '.&EXT('environment.middlename');
  251:   $fullname = "" if $fullname eq ",  ";
  252:   $fullname =~ s/\%2d/-/;
  253:   return $fullname;
  254: }
  255: 
  256: sub student_number { 
  257:   my $id = &EXT('environment.id');
  258:   $id = '' if $id eq "";
  259:   return $id;
  260: }
  261: 
  262: sub class {
  263:   my $course = &EXT('course.description');
  264:   $course = '' if $course eq "";
  265:   return $course;
  266: }
  267: 
  268: sub sec { 
  269:   my $sec = &EXT('request.course.sec');
  270:   $sec = '' if $sec eq "";
  271:   return $sec;
  272: }
  273: 
  274: sub open_date { 
  275:   my @dc = split(/\s+/,localtime(&EXT('resource.0.opendate')));
  276:   return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
  277: }
  278: 
  279: sub due_date { 
  280:   my @dc = split(/\s+/,localtime(&EXT('resource.0.duedate')));
  281:   return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
  282: }
  283: 
  284: sub answer_date { 
  285:   my @dc = split(/\s+/,localtime(&EXT('resource.0.answerdate')));
  286:   return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
  287:  }
  288: 
  289: sub choose {
  290:   my $num = $_[0];
  291:   return $_[$num];
  292: }
  293: 
  294: #$external::randomseed=time;
  295: #$a=2;
  296: #$b=2;
  297: #$c=2;
  298: #&map(1,\$a,\$b,\$c,3,4,5);
  299: #print $a."\n".$b."\n";
  300: 
  301: 
  302: 
  303: 
  304: 
  305: 

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