Annotation of loncom/homework/default_homework.lcpm, revision 1.42

1.42    ! albertel    1: # The LearningOnline Network with CAPA 
1.1       harris41    2: # used by lonxml::xmlparse() as input variable $safeinit to Apache::run::run()
1.42    ! albertel    3: #
        !             4: # $Id: gplheader.pl,v 1.1 2001/11/29 18:19:27 www Exp $
        !             5: #
        !             6: # Copyright Michigan State University Board of Trustees
        !             7: #
        !             8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
        !             9: #
        !            10: # LON-CAPA is free software; you can redistribute it and/or modify
        !            11: # it under the terms of the GNU General Public License as published by
        !            12: # the Free Software Foundation; either version 2 of the License, or
        !            13: # (at your option) any later version.
        !            14: #
        !            15: # LON-CAPA is distributed in the hope that it will be useful,
        !            16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
        !            17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        !            18: # GNU General Public License for more details.
        !            19: #
        !            20: # You should have received a copy of the GNU General Public License
        !            21: # along with LON-CAPA; if not, write to the Free Software
        !            22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
        !            23: #
        !            24: # /home/httpd/html/adm/gpl.txt
        !            25: #
        !            26: # http://www.lon-capa.org/
        !            27: #
1.20      harris41   28: #
                     29: # Guy Albertelli
                     30: #
                     31: # 05/25/2001 H. K. Ng
1.24      ng         32: # 05/31/2001 H. K. Ng
1.20      harris41   33: #
1.25      albertel   34: #init some globals
1.38      albertel   35: $hidden::RANDOMINIT=0;
1.22      ng         36: $pi=atan2(1,1)*4;
                     37: $rad2deg=180.0/$pi;
                     38: $deg2rad=$pi/180.0;
1.3       albertel   39: 
1.7       albertel   40: sub caparesponse_check {
1.36      albertel   41:   #not properly used yet: calc
                     42:   #not to be used: $ans_fmt
1.39      albertel   43:   my ($answer,$type,$tol,$sig,$ans_fmt,$unit,$calc,$samples) =
                     44:     eval $_[1].
                     45:       ';return ($answer,$type,$tol,$sig,$ans_fmt,$unit,$calc,$samples);';
1.36      albertel   46: 
1.10      albertel   47:   my $tol_type=''; # gets it's value from whether tol has a % or not done
1.19      albertel   48:   my $sig_lbound=''; #done
                     49:   my $sig_ubound=''; #done
1.7       albertel   50:   my ($response,$expr)=@_;
1.31      albertel   51: 
1.32      albertel   52: 
1.8       albertel   53:   #type's definitons come from capaParser.h
1.31      albertel   54:   my $message='';
1.32      albertel   55:   #remove leading and trailing whitespace
                     56:   if ($response=~ /^\s|\s$/) {
                     57:     $response=~ s:^\s+|\s+$::g;
                     58:     $message .="Removed ws now :$response:<br />";
                     59:   } else {
                     60:     $message .="no ws in :$response:<br />";
                     61:   }
                     62: 
1.8       albertel   63:   if ($type eq '' ) {
1.31      albertel   64:     $message .= "Didn't find a type :$type:$expr: defaulting<br />";
1.8       albertel   65:     if ( $answer eq ($answer *1.0)) { $type = 2;
                     66:     } else { $type = 3; }
                     67:   } else {
                     68:          if ($type eq 'cs')    { $type = 4;
                     69:     } elsif ($type eq 'ci')    { $type = 3;
                     70:     } elsif ($type eq 'mc')    { $type = 5;
                     71:     } elsif ($type eq 'fml')   { $type = 8;
1.9       albertel   72:     } elsif ($type eq 'subj')  { $type = 7;
1.41      albertel   73:     } elsif ($type eq 'float') { $type = 2;
                     74:     } elsif ($type eq 'int')   { $type = 1;
1.9       albertel   75:     } else { return "ERROR: Unknown type of answer: $type" }
1.8       albertel   76:   }
                     77: 
1.39      albertel   78:   my $points;
                     79:   my $id_list;
                     80:   #formula type setup the sample points
                     81:   if ($type eq '8') {
                     82:     ($id_list,$points)=split(/@/,$samples);
                     83:     $message.="Found :$points: points<br />";
                     84:   }
1.10      albertel   85:   if ($tol eq '') {
                     86:     $tol=0.0;
                     87:     $tol_type=1; #TOL_ABSOLUTE
                     88:   } else {
                     89:     if ($tol =~ /%$/) {
1.12      albertel   90:       chop $tol;
1.10      albertel   91:       $tol_type=2; #TOL_PERCENTAGE
                     92:     } else {
                     93:       $tol_type=1; #TOL_ABSOLUTE
                     94:     }
                     95:   }
1.12      albertel   96: 
                     97:   if ($sig eq '') {
                     98:     $sig_lbound = 0; #SIG_LB_DEFAULT
                     99:     $sig_ubound =15; #SIG_UB_DEFAULT
                    100:   } else {
                    101:     ($sig_lbound,$sig_ubound) = split /,/,$sig;
                    102:   }
1.7       albertel  103:   my $result = &caparesponse_capa_check_answer($response,$answer,$type,
1.10      albertel  104: 					       $tol_type,$tol,
1.7       albertel  105: 					       $sig_lbound,$sig_ubound,
1.39      albertel  106: 					       $ans_fmt,$unit,$calc,$id_list,
                    107: 					       $points,$external::randomseed);
1.9       albertel  108: 
                    109:   if    ($result == '1') { $result='EXACT_ANS'; } 
                    110:   elsif ($result == '2') { $result='APPROX_ANS'; }
                    111:   elsif ($result == '3') { $result='SIG_FAIL'; }
                    112:   elsif ($result == '4') { $result='UNIT_FAIL'; }
                    113:   elsif ($result == '5') { $result='NO_UNIT'; }
                    114:   elsif ($result == '6') { $result='UNIT_OK'; }
                    115:   elsif ($result == '7') { $result='INCORRECT'; }
                    116:   elsif ($result == '8') { $result='UNIT_NOTNEEDED'; }
                    117:   elsif ($result == '9') { $result='ANS_CNT_NOT_MATCH'; }
                    118:   elsif ($result =='10') { $result='SUB_RECORDED'; }
                    119:   elsif ($result =='11') { $result='BAD_FORMULA'; }
                    120:   elsif ($result =='12') { $result='WANTED_NUMERIC'; }
1.13      albertel  121:   else  {$result = "ERROR: Unknown Result:$result:$@:";}
1.9       albertel  122: 
1.36      albertel  123:   return "$result:<br />Error $error:<br />Answer $answer:<br />Response $response:<br /> type-$type|$tol|$tol_type|$sig:$sig_lbound:$sig_ubound|$unit|<br />$message$expr";
1.14      albertel  124: }
                    125: 
1.37      albertel  126: sub get_array_args {
                    127:   my ($expr,$arg)=@_;
1.30      albertel  128:   # do these first, because who knows what varname the instructor might have used
                    129:   # but it probably isn't $CAPARESPONSE_CHECK_LIST_answer
1.37      albertel  130:   my $CAPARESPONSE_CHECK_LIST_answer = eval $expr.';return $'.$arg; #'
1.40      albertel  131:   my $GET_ARRAY_ARGS_result;
                    132:   my @GET_ARRAY_ARGS_list;
1.39      albertel  133:   if ($CAPARESPONSE_CHECK_LIST_answer =~ /^\s*[\$\@]/) {
1.40      albertel  134:     (@GET_ARRAY_ARGS_list) = eval $CAPARESPONSE_CHECK_LIST_answer;
1.39      albertel  135:   }
1.40      albertel  136:   $GET_ARRAY_ARGS_result.="error:$@:<br />";
1.31      albertel  137:   # if the eval fails just use what is in the answer exactly
1.40      albertel  138:   if (!defined(@GET_ARRAY_ARGS_list) || !defined($GET_ARRAY_ARGS_list[0])) {
                    139:     $GET_ARRAY_ARGS_result.="list zero is undefined<br />";
                    140:     $GET_ARRAY_ARGS_list[0]=$CAPARESPONSE_CHECK_LIST_answer;
1.31      albertel  141:   }
1.40      albertel  142:   return $GET_ARRAY_ARGS_result,@GET_ARRAY_ARGS_list;
1.37      albertel  143: }
                    144: 
                    145: sub caparesponse_check_list {
                    146:   my ($response,$expr)=@_;
1.40      albertel  147:   my $result;
                    148:   my ($result,@list) = &get_array_args($expr,'answer');
1.15      albertel  149:   my $aresult='';
1.14      albertel  150:   my $current_answer;
1.39      albertel  151:   my $answers=join(':',@list);
                    152:   $result.="Got response :$answers:<br />";
1.31      albertel  153:   my @responselist;
1.32      albertel  154:   my $type =eval $expr.';return $answer;';
1.31      albertel  155:   if ($type ne '' && $#list > 0) {
                    156:     (@responselist)=split /,/,$response;
                    157:   } else {
                    158:     (@responselist)=($response);
                    159:   }
1.15      albertel  160:   my $unit='';
1.31      albertel  161:   $result.="Initial final response :$responselist['-1']:<br />";
                    162:   if ($type eq '') {
                    163:     #for numerical problems split off the unit
                    164:     if ( $responselist['-1']=~ /(.*[^\s])\s+([^\s]+)/ ) {
                    165:       $responselist['-1']=$1;
                    166:       $unit=$2;
                    167:     }
1.15      albertel  168:   }
1.31      albertel  169:   $result.="Final final response :$responselist['-1']:<br />";
                    170:   $result.=":$#list: answers<br />";
1.14      albertel  171:   $unit=~s/\s//;
                    172:   my $i=0;
                    173:   my $awards='';
                    174:   for ($i=0; $i<@list;$i++) {
1.31      albertel  175:     $result.="trying answer :$list[$i]:<br />";
1.19      albertel  176:     if ($unit eq '') {
                    177:       $aresult=&caparesponse_check($responselist[$i],
1.31      albertel  178: 			     $expr.';my $answer=\''.$list[$i].'\';');
1.19      albertel  179:     } else {
                    180:       $aresult=&caparesponse_check($responselist[$i]." $unit",
1.31      albertel  181: 				   $expr.';my $answer=\''.$list[$i].'\';');
1.19      albertel  182:     }
1.15      albertel  183:     my ($temp)=split /:/, $aresult;
1.14      albertel  184:     $awards.="$temp,";
1.15      albertel  185:     $result.=$aresult;
1.14      albertel  186:   }
                    187:   chop $awards;
1.17      albertel  188:   return "$awards:<br />$result";
1.7       albertel  189: }
                    190: 
1.4       albertel  191: sub tex {
                    192:   if ( $external::target eq "tex" ) {
                    193:     return @_[0];
                    194:   } else {
                    195:     return @_[1];
                    196:   }
                    197: }
                    198: 
1.24      ng        199: sub var_in_tex {
                    200:   if ( $external::target eq "tex" ) {
                    201:     return @_[0];
                    202:   } else {
                    203:     return "";
                    204:   }
                    205: }
                    206: 
1.4       albertel  207: sub web {
                    208:   if ( $external::target eq "tex" ) {
                    209:     return @_[1];
                    210:   } else {
                    211:     if ( $external::target eq "web") {
1.26      ng        212:       return @_[2];
                    213:     } else {
1.4       albertel  214:       return @_[0];
                    215:     }
                    216:   }
                    217: }
                    218: 
1.24      ng        219: sub html {
                    220:   if ( $external::target eq "web" ) {
1.26      ng        221:     return shift;
1.24      ng        222:   }
                    223: }
                    224: 
1.4       albertel  225: sub problem {
1.11      albertel  226:   return '1';
1.4       albertel  227: }
                    228: 
1.1       harris41  229: sub hinton {
                    230:   return 0;
                    231: }
                    232: 
                    233: sub random {
                    234:   my ($start,$end,$step)=@_;
1.38      albertel  235:   if ( ! $hidden::RANDOMINIT ) {
                    236:     srand($external::randomseed);
                    237:     $hidden::RANDOMINIT=1;
                    238:   }
1.1       harris41  239:   my $num=1+int(($end-$start)/$step);
                    240:   my $result=$start + int(rand() * $num)*$step;
                    241:   return $result;
                    242: }
                    243: 
1.26      ng        244: sub random_normal {
                    245:   my ($item_cnt,$seed,$av,$std_dev) = @_;
                    246:   my @retArray;
                    247:   &random_set_seed_from_phrase($seed);
                    248:   @retArray=&math_random_normal($item_cnt,$av,$std_dev);
                    249:   return @retArray;
                    250: }
                    251: 
                    252: sub random_beta {
                    253:   my ($item_cnt,$seed,$aa,$bb) = @_;
                    254:   my @retArray;
                    255:   &random_set_seed_from_phrase($seed);
                    256:   @retArray=&math_random_beta($item_cnt,$aa,$bb);
                    257:   return @retArray;
                    258: }
                    259: 
                    260: sub random_gamma {
                    261:   my ($item_cnt,$seed,$a,$r) = @_;
                    262:   my @retArray;
                    263:   &random_set_seed_from_phrase($seed);
                    264:   @retArray=&math_random_gamma($item_cnt,$a,$r);
                    265:   return @retArray;
                    266: }
                    267: 
                    268: sub random_exponential {
                    269:   my ($item_cnt,$seed,$av) = @_;
                    270:   my @retArray;
                    271:   &random_set_seed_from_phrase($seed);
                    272:   @retArray=&math_random_exponential($item_cnt,$av);
                    273:   return @retArray;
                    274: }
                    275: 
                    276: sub random_poisson {
                    277:   my ($item_cnt,$seed,$mu) = @_;
                    278:   my @retArray;
                    279:   &random_set_seed_from_phrase($seed);
                    280:   @retArray=&math_random_poisson($item_cnt,$mu);
                    281:   return @retArray;
                    282: }
                    283: 
                    284: sub random_chi {
                    285:   my ($item_cnt,$seed,$df) = @_;
                    286:   my @retArray;
                    287:   &random_set_seed_from_phrase($seed);
                    288:   @retArray=&math_random_chi_square($item_cnt,$df);
                    289:   return @retArray;
                    290: }
                    291: 
                    292: sub random_noncentral_chi {
                    293:   my ($item_cnt,$seed,$df,$nonc) = @_;
                    294:   my @retArray;
                    295:   &random_set_seed_from_phrase($seed);
                    296:   @retArray=&math_random_noncentral_chi_square($item_cnt,$df,$nonc);
                    297:   return @retArray;
                    298: }
                    299: 
                    300: sub random_f {
                    301:   my ($item_cnt,$seed,$dfn,$dfd) = @_;
                    302:   my @retArray;
                    303:   &random_set_seed_from_phrase($seed);
                    304:   @retArray=&math_random_f($item_cnt,$dfn,$dfd);
                    305:   return @retArray;
                    306: }
                    307: 
                    308: sub random_noncentral_f {
                    309:   my ($item_cnt,$seed,$dfn,$dfd,$nonc) = @_;
                    310:   my @retArray;
                    311:   &random_set_seed_from_phrase($seed);
                    312:   @retArray=&math_random_noncentral_f($item_cnt,$dfn,$dfd,$nonc);
                    313:   return @retArray;
                    314: }
                    315: 
                    316: sub random_multivariate_normal {
1.33      ng        317:   my ($item_cnt,$seed,$mean,$covar) = @_;
1.26      ng        318:   &random_set_seed_from_phrase($seed);
1.33      ng        319:   @retArray=&math_random_multivariate_normal($item_cnt,@$mean,@$covar);
1.26      ng        320:   return @retArray;
                    321: }
                    322: 
                    323: sub random_multinomial {
                    324:   my ($item_cnt,$seed,@p) = @_;
                    325:   my @retArray;
                    326:   &random_set_seed_from_phrase($seed);
                    327:   @retArray=&math_random_multinomial($item_cnt,@p);
                    328:   return @retArray;
                    329: }
                    330: 
                    331: sub random_permutation {
                    332:   my ($seed,@inArray) = @_;
                    333:   my @retArray;
                    334:   &random_set_seed_from_phrase($seed);
                    335:   @retArray=&math_random_permutation(@inArray);
                    336:   return @retArray;
                    337: }
                    338: 
                    339: sub random_uniform {
                    340:   my ($item_cnt,$seed,$low,$high) = @_;
                    341:   my @retArray;
                    342:   &random_set_seed_from_phrase($seed);
                    343:   @retArray=&math_random_uniform($item_cnt,$low,$high);
                    344:   return @retArray;
                    345: }
                    346: 
                    347: sub random_uniform_integer {
                    348:   my ($item_cnt,$seed,$low,$high) = @_;
                    349:   my @retArray;
                    350:   &random_set_seed_from_phrase($seed);
                    351:   @retArray=&math_random_uniform_integer($item_cnt,$low,$high);
                    352:   return @retArray;
                    353: }
                    354: 
                    355: sub random_binomial {
                    356:   my ($item_cnt,$seed,$nt,$p) = @_;
                    357:   my @retArray;
                    358:   &random_set_seed_from_phrase($seed);
                    359:   @retArray=&math_random_binomial($item_cnt,$nt,$p);
                    360:   return @retArray;
                    361: }
                    362: 
                    363: sub random_negative_binomial {
                    364:   my ($item_cnt,$seed,$ne,$p) = @_;
                    365:   my @retArray;
                    366:   &random_set_seed_from_phrase($seed);
                    367:   @retArray=&math_random_negative_binomial($item_cnt,$ne,$p);
                    368:   return @retArray;
                    369: }
                    370: 
1.23      ng        371: sub abs { abs(shift) }
                    372: sub sin { sin(shift) }
                    373: sub cos { cos(shift) }
                    374: sub exp { exp(shift) }
                    375: sub int { int(shift) }
                    376: sub log { log(shift) }
                    377: sub atan2 { atan2($_[0],$_[1]) }
                    378: sub sqrt { sqrt(shift) }
                    379: 
1.1       harris41  380: sub tan  { sin($_[0]) / cos($_[0]) }
1.21      harris41  381: #sub atan { atan2($_[0], 1); }
                    382: #sub acos { atan2(sqrt(1 - $_[0] * $_[0]), $_[0] ); }
                    383: #sub asin { atan2($_[0], sqrt(1- $_[0] * $_[0]) );  }
1.22      ng        384: 
1.18      albertel  385: sub log10 { log($_[0])/log(10); }
1.22      ng        386: 
1.20      harris41  387: sub factorial {
                    388:     my $input = int(shift);
                    389:     return "Error - unable to take factorial of an negative number ($input)" if $input < 0;
                    390:     return "Error - factorial result is greater than system limit ($input)" if $input > 170;
                    391:     return 1 if $input == 0;
                    392:     my $result = 1; 
                    393:     for (my $i=2; $i<=$input; $i++) { $result *= $i }
                    394:     return $result;
                    395: }
                    396: 
                    397: sub sgn {
                    398:     return -1 if $_[0] < 0;
                    399:     return 0 if $_[0] == 0;
                    400:     return 1 if $_[0] > 0;
                    401: }
                    402: 
                    403: sub min {
                    404:     my @sorted = sort { $a <=> $b || $a cmp $b } @_;
                    405:     return shift @sorted;
                    406: }
                    407: 
                    408: sub max {
                    409:     my @sorted = sort { $a <=> $b || $a cmp $b } @_;
                    410:     return pop @sorted;
                    411: }
1.1       harris41  412: 
1.20      harris41  413: sub roundto {
                    414:     my ($input,$n) = @_;
                    415:     return sprintf('%.'.$n.'f',$input);
                    416: }
                    417: 
                    418: sub to_string {
                    419:     my ($input,$n) = @_;
1.26      ng        420:     return sprintf($input) if $n eq "";
                    421:     $n = '.'.$n if $n !~ /^\./;
1.20      harris41  422:     return sprintf('%'.$n,$input) if $n ne "";
                    423: }
                    424: 
                    425: sub sub_string {
                    426:     my ($str,$start,$len) = @_;
                    427:     return substr($str,$start-1,$len);
                    428: }
1.1       harris41  429: 
                    430: sub pow   {return $_[0] ** $_[1]; }
1.27      ng        431: sub ceil  {return (($_[0]-int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? (int($_[0])+ 1) : int($_[0])); }
                    432: sub floor  {return (($_[0]-int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? int($_[0]) : (int($_[0])-1)); }
                    433: #sub floor {return int($_[0]); }
1.1       harris41  434: 
1.2       albertel  435: sub format {
                    436:   my ($value,$fmt)=@_;
1.11      albertel  437:   return sprintf('%.'.$fmt,$value);
1.2       albertel  438: }
1.5       albertel  439: 
                    440: sub map {
1.27      ng        441:     my ($phrase,$dest,$source)=@_;
                    442:     my @seed = &random_seed_from_phrase($phrase);
                    443:     &random_set_seed(@seed);
                    444:     my $destct = scalar(@$dest);
1.28      ng        445:     if (!$source) {
                    446: 	my @output;
                    447: 	my @idx = &math_random_permuted_index($destct);
                    448: 	my $ctr = 0;
                    449: 	while ($ctr < $destct) {
                    450: 	    $output[$ctr] = $$dest[$idx[$ctr]];
1.27      ng        451: 	    $ctr++;
1.28      ng        452: 	}
                    453: 	return @output;
1.27      ng        454:     } else {
1.28      ng        455: 	my $num = scalar(@$source);
                    456: 	my @idx = &math_random_permuted_index($num);
                    457: 	my $ctr = 0;
                    458: 	my $tot = $num;
                    459: 	$tot = $destct if $destct < $num;
                    460: 	if (ref($$dest[0])) {
                    461: 	    while ($ctr < $tot) {
                    462: 		${$$dest[$ctr]} = $$source[$idx[$ctr]];
                    463: 	        $ctr++;
                    464:             }
                    465:         } else {
                    466: 	    while ($ctr < $tot) {
                    467: 		$$dest[$ctr] = $$source[$idx[$ctr]];
                    468: 		$ctr++;
                    469: 	    }
                    470: 	}
1.27      ng        471:     }
                    472: }
                    473: 
                    474: sub rmap {
                    475:     my ($phrase,$dest,$source)=@_;
                    476:     my @seed = &random_seed_from_phrase($phrase);
                    477:     &random_set_seed(@seed);
                    478:     my $destct = scalar(@$dest);
1.28      ng        479:     if (!$source) {
                    480: 	my @idx = &math_random_permuted_index($destct);
                    481: 	my $ctr = 0;
                    482: 	my @r_idx;
                    483: 	while ($ctr < $destct) {
                    484: 	    $r_idx[$idx[$ctr]] = $ctr;
                    485: 	    $ctr++;
                    486: 	}
                    487: 	my @output;
                    488: 	$ctr = 0;
                    489: 	while ($ctr < $destct) {
                    490: 	    $output[$ctr] = $$dest[$r_idx[$ctr]];
1.27      ng        491: 	    $ctr++;
1.28      ng        492: 	}
                    493: 	return @output;
1.27      ng        494:     } else {
1.28      ng        495: 	my $num = scalar(@$source);
                    496: 	my @idx = &math_random_permuted_index($num);
                    497: 	my $ctr = 0;
                    498: 	my $tot = $num;
                    499: 	$tot = $destct if $destct < $num;
                    500: 	my @r_idx;
1.27      ng        501: 	while ($ctr < $tot) {
1.28      ng        502: 	    $r_idx[$idx[$ctr]] = $ctr;
1.27      ng        503: 	    $ctr++;
1.28      ng        504: 	}
                    505: 	$ctr = 0;
                    506: 	if (ref($$dest[0])) {
                    507: 	    while ($ctr < $tot) {
                    508: 		${$$dest[$ctr]} = $$source[$r_idx[$ctr]];
                    509: 	        $ctr++;
                    510:             }
                    511:         } else {
                    512: 	    while ($ctr < $tot) {
                    513: 		$$dest[$ctr] = $$source[$r_idx[$ctr]];
                    514: 		$ctr++;
                    515: 	    }
                    516: 	}
1.6       albertel  517:     }
1.5       albertel  518: }
1.22      ng        519: 
1.23      ng        520: sub capa_id { return }
                    521: 
                    522: sub problem { return }
                    523: 
1.22      ng        524: sub name{
                    525:   my $fullname = &EXT('environment.lastname').', '.&EXT('environment.firstname').' '.&EXT('environment.middlename');
                    526:   $fullname = "" if $fullname eq ",  ";
1.26      ng        527:   $fullname =~ s/\%2d/-/g;
1.22      ng        528:   return $fullname;
                    529: }
                    530: 
                    531: sub student_number { 
                    532:   my $id = &EXT('environment.id');
                    533:   $id = '' if $id eq "";
                    534:   return $id;
                    535: }
                    536: 
                    537: sub class {
                    538:   my $course = &EXT('course.description');
                    539:   $course = '' if $course eq "";
                    540:   return $course;
                    541: }
                    542: 
                    543: sub sec { 
                    544:   my $sec = &EXT('request.course.sec');
1.23      ng        545:   $sec = '' if $sec eq "";
1.22      ng        546:   return $sec;
                    547: }
                    548: 
1.23      ng        549: sub open_date { 
                    550:   my @dc = split(/\s+/,localtime(&EXT('resource.0.opendate')));
1.24      ng        551:   return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
                    552:   my @hm = split(/:/,$dc[3]);
                    553:   my $ampm = " am";
                    554:   if ($hm[0] > 12) {
                    555:     $hm[0]-=12;
                    556:     $ampm = " pm";
                    557:   }
                    558:   return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
1.23      ng        559: }
                    560: 
                    561: sub due_date { 
                    562:   my @dc = split(/\s+/,localtime(&EXT('resource.0.duedate')));
1.24      ng        563:   return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
                    564:   my @hm = split(/:/,$dc[3]);
                    565:   my $ampm = " am";
                    566:   if ($hm[0] > 12) {
                    567:     $hm[0]-=12;
                    568:     $ampm = " pm";
                    569:   }
                    570:   return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
                    571: #  return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
1.23      ng        572: }
                    573: 
                    574: sub answer_date { 
                    575:   my @dc = split(/\s+/,localtime(&EXT('resource.0.answerdate')));
1.24      ng        576:   return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
                    577:   my @hm = split(/:/,$dc[3]);
                    578:   my $ampm = " am";
                    579:   if ($hm[0] > 12) {
                    580:     $hm[0]-=12;
                    581:     $ampm = " pm";
                    582:   }
                    583:   return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
                    584: #  return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
                    585: }
                    586: 
                    587: sub array_moments {
                    588:   my @input=@_;
                    589:   my (@output,$N);
                    590:   $N=scalar (@input);
                    591:   $output[0]=$N;
                    592:   if ($N <= 1) {
                    593:     $output[1]=$input[0];
1.28      ng        594:     $output[1]="Input array not defined" if ($N == 0);
1.24      ng        595:     $output[2]="variance undefined for N<=1";
                    596:     $output[3]="skewness undefined for N<=1";
                    597:     $output[4]="kurtosis undefined for N<=1";
                    598:     return @output;
                    599:   }
                    600:   my $sum=0;
                    601:   foreach my $line (@input) {
                    602:     $sum+=$line;
                    603:   }
                    604:   $output[1] = $sum/$N;
                    605:   my ($x,$sdev,$var,$skew,$kurt) = 0;
                    606:   foreach my $line (@input) {
                    607:     $x=$line-$output[1];
                    608:     $var+=$x**2;
                    609:     $skew+=$x**3;
                    610:     $kurt+=$x**4;
                    611:   }
                    612:   $output[2]=$var/($N-1);
                    613:   $sdev=sqrt($output[2]);
                    614:   if ($sdev == 0) {
                    615:      $output[3]="inf-variance=0";
                    616:      $output[4]="inf-variance=0";
                    617:      return @output;
                    618:   }
                    619:   $output[3]=$skew/($sdev**3*$N);
                    620:   $output[4]=$kurt/($sdev**4*$N)-3;
                    621:   return @output;
                    622: }
1.5       albertel  623: 
                    624: sub choose {
                    625:   my $num = $_[0];
                    626:   return $_[$num];
                    627: }
1.23      ng        628: 
                    629: 

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