--- loncom/homework/default_homework.lcpm 2010/12/16 16:01:01 1.151 +++ loncom/homework/default_homework.lcpm 2011/06/08 01:39:28 1.157 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # used by lonxml::xmlparse() as input variable $safeinit to Apache::run::run() # -# $Id: default_homework.lcpm,v 1.151 2010/12/16 16:01:01 raeburn Exp $ +# $Id: default_homework.lcpm,v 1.157 2011/06/08 01:39:28 www Exp $ # # Copyright Michigan State University Board of Trustees # @@ -270,7 +270,10 @@ sub caparesponse_check_list { my $type = $LONCAPA::CAPAresponse_args{'type'}; my $answerunit=$LONCAPA::CAPAresponse_args{'unit'}; &LONCAPA_INTERNAL_DEBUG("Got type :$type: answer unit :$answerunit:\n"); - + + my $preprocess=$LONCAPA::CAPAresponse_args{'preprocess'}; + $preprocess=~s/^\&//; + my $num_input_lines = scalar(@{$LONCAPA::CAPAresponse_answer->{'answers'}}); @@ -309,12 +312,19 @@ sub caparesponse_check_list { &LONCAPA_INTERNAL_DEBUG("Initial final response :$responses->[0][-1]:"); my $unit; + my ($allowalgebra)=(¶meter_setting('allowalgebra',¤tpart())=~/^(yes|1|on)$/i); if ($type eq 'float' || $type eq '') { #for numerical problems split off the unit -# if ( $responses->[0][-1]=~ /(.*[^\s])\s+([^\s]+)/ ) { - if ( $responses->[0][-1]=~ /^([\d\.\,\s\$]*(?:(?:[xX\*]10[\^\*]*|[eE]*)[\+\-]*\d*)*(?:^|\S)\d+)([\$\s\w\^\*\/\(\)\+\-]*[^\d\.\s\,][\$\s\w\^\*\/\(\)\+\-]*)$/ ) { - $responses->[0][-1]=$1; - $unit=&capa_formula_fix($2); + my $part1; + my $part2; + if ($allowalgebra) { + ($part1,$part2)=($responses->[0][-1]=~ /^(.*[^\s])\s+([^\s]+)$/); + } else { + ($part1,$part2)=($responses->[0][-1]=~ /^([\d\.\,\s\$]*(?:(?:[xX\*]10[\^\*]*|[eE]*)[\+\-]*\d*)*(?:^|\S)\d+)([\$\s\w\^\*\/\(\)\+\-]*[^\d\.\s\,][\$\s\w\^\*\/\(\)\+\-]*)$/); + } + if (defined($part1) && defined($part2)) { + $responses->[0][-1]=$part1; + $unit=&capa_formula_fix($part2); &LONCAPA_INTERNAL_DEBUG("Found unit :$unit:"); } } @@ -322,7 +332,15 @@ sub caparesponse_check_list { $unit=~s/\s//; my $error; foreach my $response (@$responses) { - foreach my $element (@$response) { + foreach my $element (@$response) { + # See if we have preprocessor + if ($preprocess=~/\S/) { + if (defined(&$preprocess)) { + no strict 'refs'; + $element=&$preprocess($element); + use strict 'refs'; + } + } if (($type eq 'float') || (($type eq '') && ($unit ne ''))) { $element =~ s/\s//g; } @@ -344,6 +362,10 @@ sub caparesponse_check_list { if (($element==0) && ($unit!~/\w/) && ($answerunit=~/\w/)) { $appendunit=$answerunit; } +# Do the math for the student if allowed + if ($allowalgebra) { + $element=&cas('maxima',$element); + } if ($appendunit ne '') { $element .= " $appendunit"; } @@ -364,6 +386,7 @@ sub caparesponse_check_list { if (ref($LONCAPA::CAPAresponse_answer->{'answers'}) eq 'ARRAY') { foreach my $strans (@{$LONCAPA::CAPAresponse_answer->{'answers'}}) { if (ref($strans) eq 'ARRAY') { + $ansstring = join("\0",@{$strans}); foreach my $item (@{$strans}) { if ($item =~ /[\000-\037]/) { $allow_control_char = 1; @@ -373,15 +396,6 @@ sub caparesponse_check_list { } } } - if (($type eq 'cs') || ($type eq 'ci')) { - if (ref($LONCAPA::CAPAresponse_answer->{'answers'}) eq 'ARRAY') { - foreach my $answer (@{ $LONCAPA::CAPAresponse_answer->{'answers'} }) { - if (ref($answer) eq 'ARRAY') { - $ansstring = join("\0",@{$answer}); - } - } - } - } # &LONCAPA_INTERNAL_DEBUG(&LONCAPA_INTERNAL_Dumper($responses)); my %memoized; @@ -1133,11 +1147,24 @@ sub class { return $course; } +sub classid { + my $courseid = &EXT('request.course.id'); + $courseid = '' if $courseid eq ""; + return $courseid; +} + sub firstname { my $firstname = &EXT('environment.firstname'); $firstname = '' if $firstname eq ""; return $firstname; } + +sub middlename { + my $middlename = &EXT('environment.middlename'); + $middlename = '' if $middlename eq ""; + return $middlename; +} + sub lastname { my $lastname = &EXT('environment.lastname'); @@ -1204,6 +1231,40 @@ sub answer_date_epoch { return &EXT('resource.'.$partid.'.answerdate'); } +sub parameter_setting { + my ($which,$partid)=@_; + unless ($partid) { $partid=0; } + return &EXT('resource.'.$partid.'.'.$which); +} + +sub stored_data { + my ($which,$partid)=@_; + unless ($partid) { $partid=0; } + return &EXT('user.resource.resource.'.$partid.'.'.$which); +} + +sub wrong_bubbles { + my ($correct,$lower,$upper,$step,@given)=@_; + my @array=(); + my %hash=(); + foreach my $new (@given) { + $hash{$new}=1; + } + my $num=int(¶meter_setting('numbubbles',¤tpart())); + unless ($num) { $num=8; } + if ($num>1) { + for (my $i=0;$i<=500;$i++) { + my $new=&random($lower,$upper,$step); + if ($hash{$new}) { next; } + if (abs($new-$correct)<$step) { next; } + $hash{$new}=1; + @array=keys(%hash); + if ($#array+2>=$num) { last; } + } + } + return @array; +} + sub array_moments { my @input=@_; my (@output,$N);