File:  [LON-CAPA] / loncom / homework / default_homework.lcpm
Revision 1.150: download - view: text, annotated - select for diffs
Mon Oct 18 19:47:31 2010 UTC (13 years, 6 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 5531.
  - remove control characters from student submission for stringresponse,
    unless "computer's answer" also includes control characters.
    (correctness evaluated with control characters removed).
  - log to lonnet.log if control characters were removed (if <CR>, <FF> or
    newline, escape these control characters, when included in log report).

    1: # The LearningOnline Network with CAPA 
    2: # used by lonxml::xmlparse() as input variable $safeinit to Apache::run::run()
    3: #
    4: # $Id: default_homework.lcpm,v 1.150 2010/10/18 19:47:31 raeburn 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: #
   28: #
   29: 
   30: #init some globals
   31: $hidden::RANDOMINIT=0;
   32: $pi=atan2(1,1)*4;
   33: $rad2deg=180.0/$pi;
   34: $deg2rad=$pi/180.0;
   35: $"=' ';
   36: use strict;
   37: {
   38:     my $n = 0;
   39:     my $total = 0;
   40:     my $num_left = 0;
   41:     my @order;
   42:     my $type;
   43: 
   44:     sub init_permutation {
   45: 	my ($size,$requested_type) = @_;
   46: 	@order = (0..$size-1);
   47: 	$n = $size;
   48: 	$type = $requested_type;
   49: 	if ($type eq 'ordered') {
   50: 	    $total = $num_left = 1;
   51: 	} elsif ($type eq 'unordered') {
   52: 	    $total = $num_left = &factorial($size);
   53: 	} else {
   54: 	    die("Unkown type: $type");
   55: 	}
   56:     }
   57: 
   58:     sub get_next_permutation {
   59: 	if ($num_left == $total) {
   60: 	    $num_left--;
   61: 	    return \@order;
   62: 	}
   63: 
   64: 	# Find largest index j with a[j] < a[j+1]
   65: 
   66: 	my $j = scalar(@order) - 2;
   67: 	while ($order[$j] > $order[$j+1]) {
   68: 	    $j--;
   69: 	}
   70: 
   71: 	# Find index k such that a[k] is smallest integer
   72: 	# greater than a[j] to the right of a[j]
   73: 
   74: 	my $k = scalar(@order) - 1;
   75: 	while ($order[$j] > $order[$k]) {
   76: 	    $k--;
   77: 	}
   78: 
   79: 	# Interchange a[j] and a[k]
   80: 
   81: 	@order[($k,$j)] = @order[($j,$k)];
   82: 
   83: 	# Put tail end of permutation after jth position in increasing order
   84: 
   85: 	my $r = scalar(@order) - 1;
   86: 	my $s = $j + 1;
   87: 
   88: 	while ($r > $s) {
   89: 	    @order[($s,$r)]=@order[($r,$s)];
   90: 	    $r--;
   91: 	    $s++;
   92: 	}
   93: 
   94: 	$num_left--;
   95: 	return(\@order);
   96:     }
   97:     
   98:     sub get_permutations_left {
   99: 	return $num_left;
  100:     }
  101: }
  102: 
  103: sub check_commas {
  104:     my ($response)=@_;
  105:     #print("$response ");
  106:     my @numbers=split(',',$response);
  107:     #print(" numbers ".join('-',@numbers)." ");
  108:     if (scalar(@numbers) > 1) {
  109:         #print(" numbers[0] ".$numbers[0]." "); 
  110: 	if (length($numbers[0]) > 3 || length($numbers[0]) == 0) { return -1; }
  111: 	shift(@numbers);
  112: 	#print(" numbers ".scalar(@numbers)." ");
  113: 	while (scalar(@numbers) > 1) {
  114: 	    #print(" numbers ".join('-',@numbers)." ");
  115: 	    if (length($numbers[0]) != 3) { return -2; }
  116: 	    shift(@numbers);
  117: 	}
  118: 	my ($number)=split('\.',$numbers[0]);
  119: 	#print(" number ".$number." ");
  120: 	#print(" numbers[0] ".$numbers[0]." ");
  121: 	if (length($number) != 3) { return -3; }
  122:     } else {
  123: 	my ($number)=split('\.',$numbers[0]);
  124: 	if (length($number) > 3) { return -4; }
  125:     }
  126:     return 1;
  127: }
  128: 
  129: 
  130: sub caparesponse_check {
  131:     my ($answer,$response)=@_;
  132:     #not properly used yet: calc
  133:     #not to be used: $ans_fmt
  134:     my $type=$LONCAPA::CAPAresponse_args{'type'};
  135:     my $tol=$LONCAPA::CAPAresponse_args{'tol'};
  136:     my $sig=$LONCAPA::CAPAresponse_args{'sig'};
  137:     my $ans_fmt=$LONCAPA::CAPAresponse_args{'format'};
  138:     my $unit=$LONCAPA::CAPAresponse_args{'unit'};
  139:     my $calc=$LONCAPA::CAPAresponse_args{'calc'};
  140:     my $samples=$LONCAPA::CAPAresponse_args{'samples'};
  141:     
  142:     my $tol_type=''; # gets it's value from whether tol has a % or not done
  143:     my $sig_lbound=''; #done
  144:     my $sig_ubound=''; #done
  145: 
  146: 
  147:     #type's definitons come from capaParser.h
  148: 
  149:     #remove leading and trailing whitespace
  150:     if (!defined($response)) {
  151: 	$response='';
  152:     }
  153:     if ($response=~ /^\s|\s$/) {
  154: 	$response=~ s:^\s+|\s+$::g;
  155: 	&LONCAPA_INTERNAL_DEBUG("Removed ws now :$response:");
  156:     }
  157: 
  158:     #&LONCAPA_INTERNAL_DEBUG(" type is $type ");
  159:     if ($type eq 'cs' || $type eq 'ci') {
  160: 	#for string answers make sure all places spaces occur, there is 
  161:         #really only 1 space, in both the answer and the response
  162: 	$answer=~s/ +/ /g;
  163: 	$response=~s/ +/ /g;
  164:     } elsif ($type eq 'mc') {
  165: 	$answer=~s/[\s,]//g;
  166: 	$response=~s/[\s,]//g;
  167:     }
  168:     if ($type eq 'float' && $unit=~/\$/) {
  169: 	if ($response!~/^\$|\$$/)  { return ('NO_UNIT', undef); }
  170: 	$response=~s/\$//g;
  171:     }
  172:     if ($type eq 'float' && $unit=~/\,/ && (&check_commas($response)<0)) {
  173: 	return "COMMA_FAIL:";
  174:     }
  175:     $ans_fmt=~s/\W//g;
  176:     $unit=~s/[\$,]//g;
  177:     if ($type eq 'float') { $response=~s/,//g; }
  178: 
  179:     if (length($response) > 500) { return ('TOO_LONG',undef); }
  180: 
  181:     if ($type eq '' ) {
  182: 	&LONCAPA_INTERNAL_DEBUG("Didn't find a type :$type: defaulting");
  183: 	if ( $answer eq ($answer *1.0)) { $type = 2;
  184: 				      } else { $type = 3; }
  185:     } else {
  186: 	if    ($type eq 'cs')    { $type = 4; }
  187: 	elsif ($type eq 'ci')    { $type = 3 }
  188: 	elsif ($type eq 'mc')    { $type = 5; }
  189: 	elsif ($type eq 'fml')   { $type = 8; }
  190:         elsif ($type eq 'math')  { $type = 9; }
  191: 	elsif ($type eq 'subj')  { $type = 7; }
  192: 	elsif ($type eq 'float') { $type = 2; }
  193: 	elsif ($type eq 'int')   { $type = 1; }
  194: 	else { return ('ERROR', "Unknown type of answer: $type") }
  195:     }
  196: 
  197:     my $points;
  198:     my $id_list;
  199:     #formula type setup the sample points
  200:     if ($type eq '8') {
  201: 	($id_list,$points)=split(/@/,$samples);
  202: 	&LONCAPA_INTERNAL_DEBUG("Found :$id_list:$points: points in $samples");
  203:     }
  204:     if ($tol eq '') {
  205: 	$tol=0.0;
  206: 	$tol_type=1; #TOL_ABSOLUTE
  207:     } else {
  208: 	if ($tol =~ /%$/) {
  209: 	    chop $tol;
  210: 	    $tol_type=2; #TOL_PERCENTAGE
  211: 	} else {
  212: 	    $tol_type=1; #TOL_ABSOLUTE
  213: 	}
  214:     }
  215: 
  216:     ($sig_ubound,$sig_lbound)=&LONCAPA_INTERNAL_get_sigrange($sig);
  217: 
  218:     my $reterror="";
  219:     my $result;
  220:     if (($type eq '9') || ($type eq '8')) {
  221:         if ($response=~/\=/) {
  222:             return ('BAD_FORMULA','Please submit just an expression, not an equation.');
  223:         } elsif ($response =~ /\,/ and $response !~ /^\s*\{.*\}\s*$/) {
  224:             return ('BAD_FORMULA');
  225:         }
  226:     }
  227:     if ($type eq '9') {
  228:       $result = &maxima_check(&maxima_cas_formula_fix($response),&maxima_cas_formula_fix($answer),\$reterror);
  229:     } else {
  230: 	if ($type eq '8') { # fml type
  231: 	    $response = &capa_formula_fix($response);
  232: 	    $answer   = &capa_formula_fix($answer);
  233: 	}
  234:        $result = &caparesponse_capa_check_answer($response,$answer,$type,
  235: 						 $tol_type,$tol,
  236: 						 $sig_lbound,$sig_ubound,
  237: 						 $ans_fmt,$unit,$calc,$id_list,
  238: 						 $points,$external::randomseed,
  239: 						 \$reterror);
  240:     }
  241:     if    ($result == '1') { $result='EXACT_ANS'; } 
  242:     elsif ($result == '2') { $result='APPROX_ANS'; }
  243:     elsif ($result == '3') { $result='SIG_FAIL'; }
  244:     elsif ($result == '4') { $result='UNIT_FAIL'; }
  245:     elsif ($result == '5') { $result='NO_UNIT'; }
  246:     elsif ($result == '6') { $result='UNIT_OK'; }
  247:     elsif ($result == '7') { $result='INCORRECT'; }
  248:     elsif ($result == '8') { $result='UNIT_NOTNEEDED'; }
  249:     elsif ($result == '9') { $result='ANS_CNT_NOT_MATCH'; }
  250:     elsif ($result =='10') { $result='SUB_RECORDED'; }
  251:     elsif ($result =='11') { $result='BAD_FORMULA'; }
  252:     elsif ($result =='12' && !$response) { $result='MISSING_ANSWER'; }
  253:     elsif ($result =='12') { $result='WANTED_NUMERIC'; }
  254:     elsif ($result =='13') { $result='UNIT_INVALID_INSTRUCTOR'; }
  255:     elsif ($result =='141') { $result='UNIT_INVALID_STUDENT'; }
  256:     elsif ($result =='142') { $result='UNIT_INVALID_STUDENT'; }
  257:     elsif ($result =='143') { $result='UNIT_INVALID_STUDENT'; }
  258:     elsif ($result =='15') { $result='UNIT_IRRECONCIBLE'; }
  259:     else  {$result = "ERROR: Unknown Result:$result:$@:";}
  260: 
  261:     &LONCAPA_INTERNAL_DEBUG("RetError $reterror: Answer $answer: Response $response:  type-$type|$tol|$tol_type|$sig:$sig_lbound:$sig_ubound|$unit|");
  262:     &LONCAPA_INTERNAL_DEBUG(" $answer $response $result ");
  263:     return ($result,$reterror);
  264: }
  265: 
  266: 
  267: sub caparesponse_check_list {
  268:     my $responses=$LONCAPA::CAPAresponse_args{'response'};
  269:     &LONCAPA_INTERNAL_DEBUG("args ".join(':',%LONCAPA::CAPAresponse_args));
  270:     my $type = $LONCAPA::CAPAresponse_args{'type'};
  271:     my $answerunit=$LONCAPA::CAPAresponse_args{'unit'};
  272:     &LONCAPA_INTERNAL_DEBUG("Got type :$type: answer unit :$answerunit:\n");
  273:     
  274:     my $num_input_lines =
  275: 	scalar(@{$LONCAPA::CAPAresponse_answer->{'answers'}});
  276:     
  277:     if ($type ne '' ) {
  278: 	if (scalar(@$responses) < $num_input_lines) {
  279: 	    return 'MISSING_ANSWER';
  280: 	}
  281: 	if (scalar(@$responses) > $num_input_lines) {
  282: 	    return 'EXTRA_ANSWER';
  283: 	}
  284: 
  285:     }
  286: 
  287:     foreach my $which (0..($num_input_lines-1)) {
  288: 	my $answer_size = 
  289: 	    scalar(@{$LONCAPA::CAPAresponse_answer->{'answers'}[$which]});
  290: 	if ($type ne '' 
  291: 	    && $answer_size > 1) {
  292: 	    $responses->[$which]=[split(/,/,$responses->[$which])];
  293: 	} else {
  294: 	    $responses->[$which]=[$responses->[$which]];
  295: 	}
  296:     }
  297:     foreach my $which (0..($num_input_lines-1)) {
  298: 	my $answer_size = 
  299: 	    scalar(@{$LONCAPA::CAPAresponse_answer->{'answers'}[$which]});
  300: 	my $response_size = 
  301: 	    scalar(@{$responses->[$which]});
  302: 	if ($answer_size > $response_size) {
  303: 	    return 'MISSING_ANSWER';
  304: 	}
  305: 	if ($answer_size < $response_size) {
  306: 	    return 'EXTRA_ANSWER';
  307: 	}
  308:     }
  309: 
  310:     &LONCAPA_INTERNAL_DEBUG("Initial final response :$responses->[0][-1]:");
  311:     my $unit;
  312:     if ($type eq 'float' || $type eq '') {
  313: 	#for numerical problems split off the unit
  314: #	if ( $responses->[0][-1]=~ /(.*[^\s])\s+([^\s]+)/ ) {
  315:         if ( $responses->[0][-1]=~ /^([\d\.\,\s\$]*(?:(?:[xX\*]10[\^\*]*|[eE]*)[\+\-]*\d*)*(?:^|\S)\d+)([\$\s\w\^\*\/\(\)\+\-]*[^\d\.\s\,][\$\s\w\^\*\/\(\)\+\-]*)$/ ) {
  316: 	    $responses->[0][-1]=$1;
  317: 	    $unit=&capa_formula_fix($2);
  318:             &LONCAPA_INTERNAL_DEBUG("Found unit :$unit:");
  319: 	}
  320:     }
  321:     &LONCAPA_INTERNAL_DEBUG("Final final response :$responses->[0][-1]:$unit:");
  322:     $unit=~s/\s//;
  323:     my $error;
  324:     foreach my $response (@$responses) {
  325:        foreach my $element (@$response) {	
  326:           if (($type eq 'float') || (($type eq '') && ($unit ne ''))) {
  327:               $element =~ s/\s//g;
  328:           }
  329:           my $appendunit=$unit;
  330: # Deal with percentages
  331: # unit is unit entered by student, answerunit is unit by author
  332: # Deprecated: divide answer by 100 if student entered percent,
  333: # but author did not. Too much confusion
  334: #          if (($unit=~/\%/) && ($answerunit ne '%'))  {
  335: #             $element=$element/100;
  336: #             $appendunit=~s/\%//;
  337: #          }    
  338: # Author entered percent, student did not
  339:           if (($unit!~/\%/) && ($answerunit=~/\%/)) {
  340:              $element=$element*100;
  341:              $appendunit='%'.$appendunit;
  342:           }
  343: # Zero does not need a dimension
  344:           if (($element==0) && ($unit!~/\w/) && ($answerunit=~/\w/)) {
  345:              $appendunit=$answerunit;
  346:           }
  347:           if ($appendunit ne '') {
  348:               $element .= " $appendunit";
  349:           }  
  350:           &LONCAPA_INTERNAL_DEBUG("Made response element :$element:");
  351:        }
  352:     }
  353:     
  354:     foreach my $thisanswer (@{ $LONCAPA::CAPAresponse_answer->{'answers'} }) {
  355: 	if (!defined($thisanswer)) {
  356: 	    return ('ERROR','answer was undefined');
  357: 	}
  358:     }
  359: 
  360:     my $allow_control_char = 0;
  361:     my $control_chars_removed = 0;
  362:     if ($type eq 'cs' || $type eq 'ci') {
  363:         if (ref($LONCAPA::CAPAresponse_answer->{'answers'}) eq 'ARRAY') {
  364:             foreach my $strans (@{$LONCAPA::CAPAresponse_answer->{'answers'}}) {
  365:                 if ($strans =~ /[\000-\037]/) {
  366:                     $allow_control_char = 1;
  367:                 }
  368:             }
  369:         }
  370:     }
  371: 
  372: #    &LONCAPA_INTERNAL_DEBUG(&LONCAPA_INTERNAL_Dumper($responses));
  373:     my %memoized;
  374:     if ($LONCAPA::CAPAresponse_answer->{'type'}  eq 'ordered') {
  375: 	for (my $i=0; $i<scalar(@$responses);$i++) {
  376: 	    my $answer   = $LONCAPA::CAPAresponse_answer->{'answers'}[$i];
  377: 	    my $response = $responses->[$i];
  378: 	    my $key = "$answer\0$response";
  379: 	    my (@awards,@msgs);
  380: 	    for (my $j=0; $j<scalar(@$response); $j++) {
  381:                 if ($type eq 'cs' || $type eq 'ci') {
  382:                     unless ($allow_control_char) {
  383:                         if ($response->[$j] =~ /[\000-\037]/) { 
  384:                             $response->[$j] =~ s/[\000-\037]//g;
  385:                             $control_chars_removed = 1;
  386:                         }  
  387:                     }
  388:                 }
  389: 		my ($award,$msg) = &caparesponse_check($answer->[$j],
  390: 						       $response->[$j]);
  391:                 if ($type eq 'cs' || $type eq 'ci') {
  392:                     $error = &verify_stringresponse($type,$award,$response->[$j],
  393:                                                     $answer->[$j]);
  394:                 }
  395: 		push(@awards,$award);
  396: 		push(@msgs,  $msg);
  397: 	    }
  398: 	    my ($award,$msg) = 
  399: 		&LONCAPA_INTERNAL_FINALIZEAWARDS(\@awards,\@msgs);
  400: 	    $memoized{$key} = [$award,$msg];
  401: 	}
  402:     } else {
  403: 	#FIXME broken with unorder responses where one is a <value>
  404:         #      and the other is a <vector> (need to delay parse til
  405:         #      inside the loop?)
  406: 	foreach my $response (@$responses) {
  407: 	    my $response_size = scalar(@{$response});
  408: 	    foreach my $answer (@{ $LONCAPA::CAPAresponse_answer->{'answers'} }) {
  409: 		my $key = "$answer\0$response";
  410: 		my $answer_size =  scalar(@{$answer});
  411: 		my ($award,$msg);
  412: 		if ($answer_size > $response_size) {
  413: 		    $award = 'MISSING_ANSWER';
  414: 		} elsif ($answer_size < $response_size) {
  415: 		    $award = 'EXTRA_ANSWER';
  416: 		} else {
  417: 		    my (@awards,@msgs);
  418: 		    for (my $j=0; $j<scalar(@$response); $j++) {
  419:                         if ($type eq 'cs' || $type eq 'ci') {
  420:                             unless ($allow_control_char) {
  421:                                 if ($response->[$j] =~ /[\000-\037]/) {
  422:                                     $response->[$j] =~ s/[\000-\037]//g;
  423:                                     $control_chars_removed = 1;
  424:                                 }
  425:                             }
  426:                         }
  427: 			my ($award,$msg) = &caparesponse_check($answer->[$j],
  428: 							       $response->[$j]);
  429:                         if ($type eq 'cs' || $type eq 'ci') {
  430:                             $error = &verify_stringresponse($type,$award,$response->[$j],
  431:                                                             $answer->[$j]);
  432:                         }
  433: 			push(@awards,$award);
  434: 			push(@msgs,  $msg);
  435: 		    }
  436: 		    ($award,$msg) = 
  437: 			&LONCAPA_INTERNAL_FINALIZEAWARDS(\@awards,\@msgs);
  438: 		}
  439: 		$memoized{$key} = [$award,$msg];
  440: 	    }
  441: 	}
  442:     }
  443: 
  444:     my ($final_award,$final_msg);
  445:     &init_permutation(scalar(@$responses),
  446: 		      $LONCAPA::CAPAresponse_answer->{'type'});
  447: 
  448:     # possible FIXMEs
  449:     # - significant time is spent calling non-safe space routine
  450:     #   from safe space
  451:     # - early outs could be possible with classifying awards is to stratas
  452:     #   and stopping as so as hitting the top strata 
  453:     # - some early outs also might be possible with check ing the 
  454:     #   memoized hash of results (is correct even possible? etc.)
  455: 
  456:     my (@final_awards,@final_msg);
  457:     while( &get_permutations_left() ) {
  458: 	my $order = &get_next_permutation();
  459: 	my (@awards, @msgs, $i);
  460: 	foreach my $thisanswer (@{ $LONCAPA::CAPAresponse_answer->{'answers'} }) {
  461: 	    my $key = "$thisanswer\0".$responses->[$order->[$i]];
  462: 	    push(@awards,$memoized{$key}[0]);
  463: 	    push(@msgs,$memoized{$key}[1]);
  464: 	    $i++;
  465: 
  466: 	}
  467: 	&LONCAPA_INTERNAL_DEBUG(" all awards ".join(':',@awards));
  468: 
  469: 	my ($possible_award,$possible_msg) = 
  470: 	    &LONCAPA_INTERNAL_FINALIZEAWARDS(\@awards,\@msgs);
  471: 	&LONCAPA_INTERNAL_DEBUG(" pos awards ".$possible_award);
  472: 	push(@final_awards,$possible_award);
  473: 	push(@final_msg,$possible_msg);
  474:     }
  475: 
  476:     &LONCAPA_INTERNAL_DEBUG(" all final_awards ".join(':',@final_awards));
  477:     my ($final_award,$final_msg) = 
  478: 	&LONCAPA_INTERNAL_FINALIZEAWARDS(\@final_awards,\@final_msg,undef,1);
  479:     return ($final_award,$final_msg,$error,$control_chars_removed);
  480: }
  481: 
  482: sub verify_stringresponse {
  483:     my ($type,$award,$resp,$ans) = @_;
  484:     return if ($award eq 'EXACT_ANS');
  485:     my $error;
  486:     if ($resp =~ /^\s|\s$/) {
  487:         $resp =~ s{^\s+|\s+$}{}g;
  488:     }
  489:     if ($ans =~ /^\s|\s$/) {
  490:         $ans =~ s{^\s+|\s+$}{}g;
  491:     }
  492:     if ($type eq 'ci') {
  493:         $resp = lc($resp);
  494:         $ans = lc($ans);
  495:     }
  496:     if ($resp eq $ans) {
  497:         if ($award eq 'INCORRECT') {
  498:             $error = 'MISGRADED';
  499:         }
  500:     }
  501:     return $error;
  502: }
  503: 
  504: sub cas {
  505:     my ($system,$input,$library)=@_;
  506:     my $output;
  507:     my $dump;
  508:     if ($system eq 'maxima') {
  509:        $output=&maxima_eval($input,$library);
  510:     } elsif ($system eq 'R') {
  511:        ($output,$dump)=&r_eval($input,$library,0);
  512:     } else {
  513:        $output='Error: unrecognized CAS';
  514:     }
  515:     return $output;
  516: }
  517: 
  518: sub cas_hashref {
  519:     my ($system,$input,$library)=@_;
  520:     if ($system eq 'maxima') {
  521:        return 'Error: unsupported CAS';
  522:     } elsif ($system eq 'R') {
  523:        return &r_eval($input,$library,1);
  524:     } else {
  525:        return 'Error: unrecognized CAS';
  526:     }
  527: }
  528: 
  529: #
  530: # cas_hashref_entry takes a list of indices and gets the entry in a hash generated by Rreturn.
  531: # Call: cas_hashref_entry(Rvalue, index1, index2, ...) where Rvalue is a hash returned by Rreturn.
  532: # Rentry will return the first scalar value it encounters (ignoring excess indices).
  533: # If an invalid key is given, it returns undef.
  534: #
  535: sub cas_hashref_entry {
  536:     return &Rentry(@_);
  537: }
  538: 
  539: #
  540: # cas_hashref_array takes a list of indices and gets a column array from a hash generated by Rreturn.
  541: # Call: cas_hashref_array(Rvalue, index1, index2, ...) where Rvalue is a hash returned by Rreturn.
  542: # If an invalid key is given, it returns undef.
  543: #
  544: sub cas_hashref_array {
  545:     return &Rarray(@_);
  546: }
  547: 
  548: sub tex {
  549:     if ( $external::target eq "tex" ) {
  550: 	return $_[0];
  551:     } else {
  552: 	return $_[1];
  553:     }
  554: }
  555: 
  556: sub var_in_tex {
  557:     if ( $external::target eq "tex" ) {
  558: 	return $_[0];
  559:     } else {
  560: 	return "";
  561:     }
  562: }
  563: 
  564: sub web {
  565:     if ( $external::target eq "tex" ) {
  566: 	return $_[1];
  567:     } else {
  568: 	if ( $external::target eq "web" || $external::target eq "answer") {
  569: 	    return $_[2];
  570: 	} else {
  571: 	    return $_[0];
  572: 	}
  573:     }
  574: }
  575: 
  576: sub html {
  577:     if ( $external::target eq "web" ) {
  578: 	return shift;
  579:     }
  580: }
  581: 
  582: sub hinton {
  583:     return 0;
  584: }
  585: 
  586: sub random {
  587:     my ($start,$end,$step)=@_;
  588:     if ( ! $hidden::RANDOMINIT ) {
  589: 	if ($external::randomseed == 0) { $external::randomseed=1; }
  590: 	if ($external::randomseed =~/,/) {
  591: 	    my ($num1,$num2)=split(/,/,$external::randomseed);
  592: 	    &random_set_seed(1,abs($num1));
  593: 	} elsif ($external::randomseed =~/:/) {
  594: 	    my ($num1,$num2)=split(/:/,$external::randomseed);
  595: 	    &random_set_seed(abs($num1),abs($num2));
  596: 	} else {
  597: 	    &random_set_seed(1,int(abs($external::randomseed)));
  598: 	}
  599: 	&math_random_uniform();
  600: 	$hidden::RANDOMINIT=1;
  601:     }
  602:     if (!defined($step)) { $step=1; }
  603:     my $num=1+int(($end-$start)/$step);
  604:     my $result=$start + int(&math_random_uniform() * $num)*$step;
  605:     return $result;
  606: }
  607: 
  608: sub random_normal {
  609:     my ($item_cnt,$seed,$av,$std_dev) = @_;
  610:     my @oldseed=&random_get_seed();
  611:     my @retArray;
  612:     &random_set_seed_from_phrase($seed);
  613:     @retArray=&math_random_normal($item_cnt,$av,$std_dev);
  614:     &random_set_seed(@oldseed);
  615:     return @retArray;
  616: }
  617: 
  618: sub random_beta {
  619:     my ($item_cnt,$seed,$aa,$bb) = @_;
  620:     my @oldseed=&random_get_seed();
  621:     my @retArray;
  622:     &random_set_seed_from_phrase($seed);
  623:     @retArray=&math_random_beta($item_cnt,$aa,$bb);
  624:     &random_set_seed(@oldseed);
  625:     return @retArray;
  626: }
  627: 
  628: sub random_gamma {
  629:     my ($item_cnt,$seed,$a,$r) = @_;
  630:     my @oldseed=&random_get_seed();
  631:     my @retArray;
  632:     &random_set_seed_from_phrase($seed);
  633:     @retArray=&math_random_gamma($item_cnt,$a,$r);
  634:     &random_set_seed(@oldseed);
  635:     return @retArray;
  636: }
  637: 
  638: sub random_exponential {
  639:     my ($item_cnt,$seed,$av) = @_;
  640:     my @oldseed=&random_get_seed();
  641:     my @retArray;
  642:     &random_set_seed_from_phrase($seed);
  643:     @retArray=&math_random_exponential($item_cnt,$av);
  644:     &random_set_seed(@oldseed);
  645:     return @retArray;
  646: }
  647: 
  648: sub random_poisson {
  649:     my ($item_cnt,$seed,$mu) = @_;
  650:     my @oldseed=&random_get_seed();
  651:     my @retArray;
  652:     &random_set_seed_from_phrase($seed);
  653:     @retArray=&math_random_poisson($item_cnt,$mu);
  654:     &random_set_seed(@oldseed);
  655:     return @retArray;
  656: }
  657: 
  658: sub random_chi {
  659:     my ($item_cnt,$seed,$df) = @_;
  660:     my @oldseed=&random_get_seed();
  661:     my @retArray;
  662:     &random_set_seed_from_phrase($seed);
  663:     @retArray=&math_random_chi_square($item_cnt,$df);
  664:     &random_set_seed(@oldseed);
  665:     return @retArray;
  666: }
  667: 
  668: sub random_noncentral_chi {
  669:     my ($item_cnt,$seed,$df,$nonc) = @_;
  670:     my @oldseed=&random_get_seed();
  671:     my @retArray;
  672:     &random_set_seed_from_phrase($seed);
  673:     @retArray=&math_random_noncentral_chi_square($item_cnt,$df,$nonc);
  674:     &random_set_seed(@oldseed);
  675:     return @retArray;
  676: }
  677: 
  678: sub random_f {
  679:     my ($item_cnt,$seed,$dfn,$dfd) = @_;
  680:     my @oldseed=&random_get_seed();
  681:     my @retArray;
  682:     &random_set_seed_from_phrase($seed);
  683:     @retArray=&math_random_f($item_cnt,$dfn,$dfd);
  684:     &random_set_seed(@oldseed);
  685:     return @retArray;
  686: }
  687: 
  688: sub random_noncentral_f {
  689:     my ($item_cnt,$seed,$dfn,$dfd,$nonc) = @_;
  690:     my @oldseed=&random_get_seed();
  691:     my @retArray;
  692:     &random_set_seed_from_phrase($seed);
  693:     @retArray=&math_random_noncentral_f($item_cnt,$dfn,$dfd,$nonc);
  694:     &random_set_seed(@oldseed);
  695:     return @retArray;
  696: }
  697: 
  698: sub random_multivariate_normal {
  699:     my ($item_cnt,$seed,$mean,$covar) = @_;
  700:     my @oldseed=&random_get_seed();
  701:     &random_set_seed_from_phrase($seed);
  702:     my @retArray=&math_random_multivariate_normal($item_cnt,@$mean,@$covar);
  703:     &random_set_seed(@oldseed);
  704:     return @retArray;
  705: }
  706: 
  707: sub random_multinomial {
  708:     my ($item_cnt,$seed,@p) = @_;
  709:     my @oldseed=&random_get_seed();
  710:     my @retArray;
  711:     &random_set_seed_from_phrase($seed);
  712:     my @retArray=&math_random_multinomial($item_cnt,@p);
  713:     &random_set_seed(@oldseed);
  714:     return @retArray;
  715: }
  716: 
  717: sub random_permutation {
  718:     my ($seed,@inArray) = @_;
  719:     my @oldseed=&random_get_seed();
  720:     my @retArray;
  721:     &random_set_seed_from_phrase($seed);
  722:     @retArray=&math_random_permutation(@inArray);
  723:     &random_set_seed(@oldseed);
  724:     return @retArray;
  725: }
  726: 
  727: sub random_uniform {
  728:     my ($item_cnt,$seed,$low,$high) = @_;
  729:     my @oldseed=&random_get_seed();
  730:     my @retArray;
  731:     &random_set_seed_from_phrase($seed);
  732:     @retArray=&math_random_uniform($item_cnt,$low,$high);
  733:     &random_set_seed(@oldseed);
  734:     return @retArray;
  735: }
  736: 
  737: sub random_uniform_integer {
  738:     my ($item_cnt,$seed,$low,$high) = @_;
  739:     my @oldseed=&random_get_seed();
  740:     my @retArray;
  741:     &random_set_seed_from_phrase($seed);
  742:     @retArray=&math_random_uniform_integer($item_cnt,$low,$high);
  743:     &random_set_seed(@oldseed);
  744:     return @retArray;
  745: }
  746: 
  747: sub random_binomial {
  748:     my ($item_cnt,$seed,$nt,$p) = @_;
  749:     my @oldseed=&random_get_seed();
  750:     my @retArray;
  751:     &random_set_seed_from_phrase($seed);
  752:     @retArray=&math_random_binomial($item_cnt,$nt,$p);
  753:     &random_set_seed(@oldseed);
  754:     return @retArray;
  755: }
  756: 
  757: sub random_negative_binomial {
  758:     my ($item_cnt,$seed,$ne,$p) = @_;
  759:     my @oldseed=&random_get_seed();
  760:     my @retArray;
  761:     &random_set_seed_from_phrase($seed);
  762:     @retArray=&math_random_negative_binomial($item_cnt,$ne,$p);
  763:     &random_set_seed(@oldseed);
  764:     return @retArray;
  765: }
  766: 
  767: sub abs { CORE::abs(shift) }
  768: sub sin { CORE::sin(shift) }
  769: sub cos { CORE::cos(shift) }
  770: sub exp { CORE::exp(shift) }
  771: sub int { CORE::int(shift) }
  772: sub log { CORE::log(shift) }
  773: sub atan2 { CORE::atan2($_[0],$_[1]) }
  774: sub sqrt { CORE::sqrt(shift) }
  775: 
  776: sub tan  { CORE::sin($_[0]) / CORE::cos($_[0]) }
  777: #sub atan { atan2($_[0], 1); }
  778: #sub acos { atan2(sqrt(1 - $_[0] * $_[0]), $_[0] ); }
  779: #sub asin { atan2($_[0], sqrt(1- $_[0] * $_[0]) );  }
  780: 
  781: sub log10 { CORE::log($_[0])/CORE::log(10); }
  782: 
  783: sub factorial {
  784:     my $input = CORE::int(shift);
  785:     return "Error - unable to take factorial of an negative number ($input)" if $input < 0;
  786:     return "Error - factorial result is greater than system limit ($input)" if $input > 170;
  787:     return 1 if $input == 0;
  788:     my $result = 1; 
  789:     for (my $i=2; $i<=$input; $i++) { $result *= $i }
  790:     return $result;
  791: }
  792: 
  793: sub sgn {
  794:     return -1 if $_[0] < 0;
  795:     return 0 if $_[0] == 0;
  796:     return 1 if $_[0] > 0;
  797: }
  798: 
  799: sub min {
  800:     my @sorted = sort { $a <=> $b || $a cmp $b } @_;
  801:     return shift @sorted;
  802: }
  803: 
  804: sub max {
  805:     my @sorted = sort { $a <=> $b || $a cmp $b } @_;
  806:     return pop @sorted;
  807: }
  808: 
  809: sub roundto {
  810:     my ($input,$n) = @_;
  811:     return sprintf('%.'.$n.'f',$input);
  812: }
  813: 
  814: sub to_string {
  815:     my ($input,$n) = @_;
  816:     return sprintf($input) if $n eq "";
  817:     $n = '.'.$n if $n !~ /^\./;
  818:     return sprintf('%'.$n,$input) if $n ne "";
  819: }
  820: 
  821: sub sub_string {
  822:     my ($str,$start,$len) = @_;
  823:     return substr($str,$start-1,$len);
  824: }
  825: 
  826: sub pow   {return $_[0] ** $_[1]; }
  827: sub ceil  {return (($_[0]-CORE::int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? (CORE::int($_[0])+ 1) : CORE::int($_[0])); }
  828: sub floor  {return (($_[0]-CORE::int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? CORE::int($_[0]) : (CORE::int($_[0])-1)); }
  829: #sub floor {return int($_[0]); }
  830: 
  831: sub format {
  832:     my ($value,$fmt)=@_;
  833:     my ($dollarmode,$commamode,$alwaysperiod,$options);
  834:     if ($fmt =~ /^([^\d]*)(.*)/) { $options=$1; $fmt=$2; } 
  835:     #if ($options =~ /\$/) { $dollamode=1; }
  836:     #if ($options =~ /,/)  { $commamode=1; }
  837:     if ($options =~ /\./) { $alwaysperiod=1; }
  838:     my $result;
  839:     if ($fmt=~/s$/i) {
  840: 	$result=&format_significant_figures($value,$fmt);
  841:     } else {
  842: 	$fmt=~s/e/E/g;
  843: 	$result=sprintf('%.'.$fmt,$value);
  844: 	if ($alwaysperiod && $fmt eq '0f') { $result .='.'; }
  845: 	$result=~s/(E[+-]*)0/$1/;
  846:     }
  847:     #if ($dollarmode) {$result=&dollarformat($result);}
  848:     #if ($commamode) {$result=&commaformat($result);}
  849:     return $result;
  850: }
  851: 
  852: sub chemparse {
  853:     my ($reaction) = @_;
  854:     my @tokens = split(/(\s\+|\->|<=>|<\-|\.)/,$reaction);
  855:     my $formula = '';
  856:     foreach my $token (@tokens) {
  857: 	if ($token eq '->' ) {
  858: 	    $formula .= '<m>\ensuremath{\rightarrow}</m> ';
  859: 	    next;
  860: 	}
  861: 	if ($token eq '<-' ) {
  862: 	    $formula .= '<m>\ensuremath{\leftarrow}</m> ';
  863: 	    next;
  864: 	}  
  865: 	if ($token eq '<=>') {
  866: 	    if ($external::target eq 'web' &&
  867: 		&EXT('request.browser.unicode')) {
  868: 		$formula .= '&#8652; ';
  869: 	    } else {
  870: 		$formula .= &web('<=> ','<m>\ensuremath{\rightleftharpoons}</m> ',
  871: 				 '&lt;=&gt; ');
  872: 	    }
  873: 	    next;
  874: 	}
  875: 	if ($token eq '.') {
  876: 	  $formula =~ s/(\&nbsp\;| )$//;
  877: 	  $formula .= '&middot;';
  878: 	  next;
  879: 	}
  880: 	$token =~ /^\s*([\d|\/]*(?:&frac\d\d)?)(.*)/;
  881:         $formula .= $1 if ($1 ne '1');  # stoichiometric coefficient
  882: 	
  883: 	my $molecule = $2;
  884: 	# subscripts
  885: 	$molecule =~ s|(?<=[a-zA-Z\)\]\s])(\d+)|<sub>$1</sub>|g;
  886: 	# superscripts
  887: 	$molecule =~ s|\^(\d*[+\-]*)|<sup>$1</sup>|g;
  888: 	# strip whitespace
  889: 	$molecule =~ s/\s*//g;
  890: 	# forced space
  891: 	$molecule =~ s/_/ /g;
  892: 	$molecule =~ s/-/&minus;/g;
  893: 	$formula .= $molecule.'&nbsp;';
  894:     }
  895:     # get rid of trailing space
  896:     $formula =~ s/(\&nbsp\;| )$//;
  897:     return &xmlparse($formula);
  898: }
  899: 
  900: sub prettyprint {
  901:     my ($value,$fmt,$target)=@_;
  902:     my $result;
  903:     if (!$target) { $target = $external::target; }
  904:     if ($fmt =~ /chem/i) { return(&chemparse($value)); }
  905:     my ($dollarmode,$commamode,$alwaysperiod,$options);
  906:     if ($fmt =~ /^([^\d]*)(.*)/) { $options=$1; $fmt=$2; } 
  907:     if ($options =~ /\$/) { $dollarmode=1; }
  908:     if ($options =~ /,/)  { $commamode=1; }
  909:     if ($options =~ /\./) { $alwaysperiod=1; }
  910:     if ($fmt=~/s$/i) {
  911: 	$value=&format_significant_figures($value,$fmt);
  912:     } elsif ($fmt) {
  913: 	$value=sprintf('%.'.$fmt,$value);
  914:     }
  915:     if ($alwaysperiod && $fmt eq '0f') {
  916: 	if ($target eq 'tex') {
  917: 	    $value .='\\ensuremath{.}';
  918: 	} else {
  919: 	    $value .='.';
  920: 	}
  921:     }
  922:     if ($value =~ /([0-9\.\-\+]+)E([0-9\-\+]+)/i ) {
  923: 	my $frac=$1;
  924: 	if ($dollarmode) { $frac=&dollarformat($frac); }
  925: 	if ($commamode) { $frac=&commaformat($frac); }
  926: 	my $exponent=$2;
  927: 	$exponent=~s/^\+0*//;
  928: 	$exponent=~s/^-0*/-/;
  929: 	$exponent=~s/^-0*/-/;
  930: 	if ($exponent eq '-') { undef($exponent); }
  931: 	if ($exponent) {
  932: 	    if ($target eq 'web') {
  933: 		$result=$frac.'&#215;10<sup>'.$exponent.'</sup>';
  934: 	    } elsif ($target eq 'tex') {
  935: 		$result='\ensuremath{'.$frac.'\times 10^{'.$exponent.'}}';
  936: 	    } else {
  937: 		$result=$value;
  938: 	    }
  939: 	} else {
  940: 	    $result=$frac;
  941: 	}
  942:     } else {
  943: 	$result=$value;
  944: 	if    ($dollarmode) { $result=&dollarformat($result,$target); }
  945: 	elsif ($commamode)  { $result=&commaformat($result,$target); }
  946:     }
  947:     return $result;
  948: }
  949: 
  950: sub commaformat {
  951:     my ($number,$target) = @_;
  952:     if ($number =~ /\./) {
  953: 	while ($number =~ /([^0-9]*)([0-9]+)([^\.,][^\.,][^\.,])([,0-9]*\.[0-9]*)$/) {
  954: 	    $number = $1.$2.','.$3.$4;
  955: 	}
  956:     } else {
  957: 	while ($number =~ /^([^0-9]*)([0-9]+)([^,][^,][^,])([,0-9]*)$/) {
  958: 	    $number = $1.$2.','.$3.$4;
  959: 	}
  960:     }
  961:     return $number;
  962: }
  963: 
  964: sub dollarformat {
  965:     my ($number,$target) = @_;
  966:     if (!$target) { $target = $external::target; }
  967:     $number=&commaformat($number,$target);
  968:     if ($target eq 'tex') {
  969: 	$number='\$'.$number; #' stupid emacs
  970:     } else {
  971: 	$number='$'.$number; #' stupid emacs
  972:     }
  973:     return $number; 
  974: }
  975: 
  976: # format of form ns or nS where n is an integer
  977: sub format_significant_figures {
  978:     my ($number,$format) = @_; 
  979:     return '0' if ($number == 0);
  980:     # extract number of significant figures needed
  981:     my ($sig) = ($format =~ /(\d+)s/i);
  982:     # arbitrary choice - suggestions ?? or throw error message?
  983:     $sig = 3 if ($sig eq '');
  984:     # save the minus sign
  985:     my $sign = ($number < 0) ? '-' : '';
  986:     $number = abs($number);
  987:     # needed to correct for a number greater than 1 (or
  988:     my $power = ($number < 1) ? 0 : 1;
  989:     # could round up. Take the integer part of log10.
  990:     my $x10 = int(log($number)/log(10));
  991:     # find number with values left of decimal pt = # of sign figs.
  992:     my $xsig = $number*10**($sig-$x10-$power);
  993:     # get just digits left of decimal pt - also rounds off correctly
  994:     my $xint  = sprintf('%.0f',$xsig);
  995:     # save any trailing zero's
  996:     my ($zeros) = ($xint =~ /(0+)$/);
  997:     # return number to original magnitude
  998:     my $numSig = $xint*10**($x10-$sig+$power);
  999:     # insert trailing zero's if have decimal point
 1000:     $numSig =~ s/^(\d+)\.(\d+)(\e?(.*)?)$/$1\.$2$zeros$3/;
 1001:     # put a decimal pt for number ending with 0 and length = # of sig fig
 1002:     $numSig.='.' if (length($numSig) == $sig && $numSig =~ /0$/);
 1003:     if (length($numSig) < $sig) {
 1004: 	$numSig.='.'.substr($zeros,0,($sig-length($numSig)));
 1005:     }
 1006:     # return number with sign
 1007:     return $sign.$numSig;
 1008: 
 1009: }
 1010: 
 1011: sub map {
 1012:     my ($phrase,$dest,$source)=@_;
 1013:     my @oldseed=&random_get_seed();
 1014:     my @seed = &random_seed_from_phrase($phrase);
 1015:     &random_set_seed(@seed);
 1016:     my $destct = scalar(@$dest);
 1017:     if (!$source) {
 1018: 	my @output;
 1019: 	my @idx = &math_random_permuted_index($destct);
 1020: 	my $ctr = 0;
 1021: 	while ($ctr < $destct) {
 1022: 	    $output[$ctr] = $$dest[$idx[$ctr]];
 1023: 	    $ctr++;
 1024: 	}
 1025:         &random_set_seed(@oldseed);
 1026: 	return @output;
 1027:     } else {
 1028: 	my $num = scalar(@$source);
 1029: 	my @idx = &math_random_permuted_index($num);
 1030: 	my $ctr = 0;
 1031: 	my $tot = $num;
 1032: 	$tot = $destct if $destct < $num;
 1033: 	if (ref($$dest[0])) {
 1034: 	    while ($ctr < $tot) {
 1035: 		${$$dest[$ctr]} = $$source[$idx[$ctr]];
 1036: 	        $ctr++;
 1037:             }
 1038:         } else {
 1039: 	    while ($ctr < $tot) {
 1040: 		$$dest[$ctr] = $$source[$idx[$ctr]];
 1041: 		$ctr++;
 1042: 	    }
 1043: 	}
 1044:     }
 1045:     &random_set_seed(@oldseed);
 1046:     return '';
 1047: }
 1048: 
 1049: sub rmap {
 1050:     my ($phrase,$dest,$source)=@_;
 1051:     my @oldseed=&random_get_seed();
 1052:     my @seed = &random_seed_from_phrase($phrase);
 1053:     &random_set_seed(@seed);
 1054:     my $destct = scalar(@$dest);
 1055:     if (!$source) {
 1056: 	my @idx = &math_random_permuted_index($destct);
 1057: 	my $ctr = 0;
 1058: 	my @r_idx;
 1059: 	while ($ctr < $destct) {
 1060: 	    $r_idx[$idx[$ctr]] = $ctr;
 1061: 	    $ctr++;
 1062: 	}
 1063: 	my @output;
 1064: 	$ctr = 0;
 1065: 	while ($ctr < $destct) {
 1066: 	    $output[$ctr] = $$dest[$r_idx[$ctr]];
 1067: 	    $ctr++;
 1068: 	}
 1069:         &random_set_seed(@oldseed);
 1070: 	return @output;
 1071:     } else {
 1072: 	my $num = scalar(@$source);
 1073: 	my @idx = &math_random_permuted_index($num);
 1074: 	my $ctr = 0;
 1075: 	my $tot = $num;
 1076: 	$tot = $destct if $destct < $num;
 1077: 	my @r_idx;
 1078: 	while ($ctr < $tot) {
 1079: 	    $r_idx[$idx[$ctr]] = $ctr;
 1080: 	    $ctr++;
 1081: 	}
 1082: 	$ctr = 0;
 1083: 	if (ref($$dest[0])) {
 1084: 	    while ($ctr < $tot) {
 1085: 		${$$dest[$ctr]} = $$source[$r_idx[$ctr]];
 1086: 	        $ctr++;
 1087:             }
 1088:         } else {
 1089: 	    while ($ctr < $tot) {
 1090: 		$$dest[$ctr] = $$source[$r_idx[$ctr]];
 1091: 		$ctr++;
 1092: 	    }
 1093: 	}
 1094:     }
 1095:     &random_set_seed(@oldseed);
 1096:     return '';
 1097: }
 1098: 
 1099: sub capa_id { return }
 1100: 
 1101: sub problem { return }
 1102: 
 1103: sub name{
 1104:     my $fullname = &EXT('environment.lastname').', '.&EXT('environment.firstname').' '.&EXT('environment.middlename');
 1105:     $fullname = "" if $fullname eq ",  ";
 1106:     $fullname =~ s/\%2d/-/g;
 1107:     return $fullname;
 1108: }
 1109: 
 1110: sub student_number { 
 1111:     my $id = &EXT('environment.id');
 1112:     $id = '' if $id eq "";
 1113:     return $id;
 1114: }
 1115: 
 1116: sub class {
 1117:     my $course = &EXT('course.description');
 1118:     $course = '' if $course eq "";
 1119:     return $course;
 1120: }
 1121: 
 1122: sub firstname {
 1123:     my $firstname = &EXT('environment.firstname');
 1124:     $firstname = '' if $firstname eq "";
 1125:     return $firstname;
 1126: }
 1127:                                                                                 
 1128: sub lastname {
 1129:     my $lastname = &EXT('environment.lastname');
 1130:     $lastname = '' if $lastname eq "";
 1131:     return $lastname;
 1132: }
 1133: 
 1134: sub sec { 
 1135:     my $sec = &EXT('request.course.sec');
 1136:     $sec = '' if $sec eq "";
 1137:     return $sec;
 1138: }
 1139: 
 1140: sub submission {
 1141:    my ($partid,$responseid,$subnumber)=@_;
 1142:    my $sub='';
 1143:    if ($subnumber) { $sub=$subnumber.':'; }
 1144:    return &EXT('user.resource.'.$sub.'resource.'.$partid.'.'.$responseid.'.submission');
 1145: }
 1146: 
 1147: sub currentpart {
 1148:    return $external::part;
 1149: }
 1150: 
 1151: sub eval_time {
 1152:    my ($timestamp)=@_;
 1153:    unless ($timestamp) { return ''; }
 1154:    return &locallocaltime($timestamp);
 1155: }
 1156: 
 1157: sub open_date { 
 1158:     my ($partid)=@_;
 1159:     unless ($partid) { $partid=0; }
 1160:     return &eval_time(&EXT('resource.'.$partid.'.opendate'));
 1161: }
 1162: 
 1163: sub due_date {
 1164:     my ($partid)=@_;
 1165:     unless ($partid) { $partid=0; } 
 1166:     return &eval_time(&EXT('resource.'.$partid.'.duedate'));
 1167: }
 1168: 
 1169: sub answer_date { 
 1170:     my ($partid)=@_;
 1171:     unless ($partid) { $partid=0; }
 1172:     return &eval_time(&EXT('resource.'.$partid.'.answerdate'));
 1173: }
 1174: 
 1175: sub open_date_epoch {
 1176:     my ($partid)=@_;
 1177:     unless ($partid) { $partid=0; }
 1178:     return &EXT('resource.'.$partid.'.opendate');
 1179: }
 1180: 
 1181: sub due_date_epoch {
 1182:     my ($partid)=@_;
 1183:     unless ($partid) { $partid=0; }
 1184:     return &EXT('resource.'.$partid.'.duedate');
 1185: }
 1186: 
 1187: sub answer_date_epoch {
 1188:     my ($partid)=@_;
 1189:     unless ($partid) { $partid=0; }
 1190:     return &EXT('resource.'.$partid.'.answerdate');
 1191: }
 1192: 
 1193: sub array_moments {
 1194:     my @input=@_;
 1195:     my (@output,$N);
 1196:     $N=scalar (@input);
 1197:     $output[0]=$N;
 1198:     if ($N <= 1) {
 1199: 	$output[1]=$input[0];
 1200: 	$output[1]="Input array not defined" if ($N == 0);
 1201: 	$output[2]="variance undefined for N<=1";
 1202: 	$output[3]="skewness undefined for N<=1";
 1203: 	$output[4]="kurtosis undefined for N<=1";
 1204: 	return @output;
 1205:     }
 1206:     my $sum=0;
 1207:     foreach my $line (@input) {
 1208: 	$sum+=$line;
 1209:     }
 1210:     $output[1] = $sum/$N;
 1211:     my ($x,$sdev,$var,$skew,$kurt) = 0;
 1212:     foreach my $line (@input) {
 1213: 	$x=$line-$output[1];
 1214: 	$var+=$x**2;
 1215: 	$skew+=$x**3;
 1216: 	$kurt+=$x**4;
 1217:     }
 1218:     $output[2]=$var/($N-1);
 1219:     $sdev=CORE::sqrt($output[2]);
 1220:     if ($sdev == 0) {
 1221: 	$output[3]="inf-variance=0";
 1222: 	$output[4]="inf-variance=0";
 1223: 	return @output;
 1224:     }
 1225:     $output[3]=$skew/($sdev**3*$N);
 1226:     $output[4]=$kurt/($sdev**4*$N)-3;
 1227:     return @output;
 1228: }
 1229: 
 1230: sub choose {
 1231:     my $num = $_[0];
 1232:     return $_[$num];
 1233: }
 1234: 
 1235: #&sum1(1,$x,sub { &sum1($_[0],2*$_[0], sub { fact($_[0])**2 })});
 1236: #sub sum1 {
 1237: #    my ($start,$end,$sub)=@_;
 1238: #    my $sum=0;
 1239: #    for (my $i=$start;$i<=$end;$i++) {
 1240: #        $sum+=&$sub($i);
 1241: #    }
 1242: #    return $sum
 1243: #}
 1244: 
 1245: #&sum2('a',1,$x,'&sum2(\'b\',$a,2*$a, \'&factorial($b)**2\')');
 1246: #sub sum2 {
 1247: #    my ($varname,$start,$end,$line)=@_;
 1248: #    my $sum=0;
 1249: #    for (my $i=$start;$i<=$end;$i++) {
 1250: #	my $func=sub {
 1251: #	    eval("\$".$varname."=$i");
 1252: #	    eval($line);
 1253: #	};
 1254: #        $sum+=&$func($i);
 1255: #    }
 1256: #    return $sum
 1257: #}
 1258: 
 1259: # expiremental idea
 1260: sub proper_path {
 1261:     my ($path)=@_;
 1262:     if ( $external::target eq "tex" ) {
 1263: 	return '/home/httpd/html'.$path;
 1264:     } else {
 1265: 	return $path;
 1266:     }
 1267: }
 1268: 

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