--- loncom/homework/response.pm 2006/10/17 17:09:44 1.148 +++ loncom/homework/response.pm 2006/12/15 22:11:43 1.156 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # various response type definitons response definition # -# $Id: response.pm,v 1.148 2006/10/17 17:09:44 albertel Exp $ +# $Id: response.pm,v 1.156 2006/12/15 22:11:43 www Exp $ # # Copyright Michigan State University Board of Trustees # @@ -30,6 +30,7 @@ package Apache::response; use strict; use Apache::lonlocal; use Apache::lonnet; +use Apache::lonmaxima(); BEGIN { &Apache::lonxml::register('Apache::response',('responseparam','parameter','dataresponse','customresponse','mathresponse')); @@ -306,7 +307,7 @@ sub handle_previous { } sub view_or_modify { - my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser(); + my ($symb,$courseid,$domain,$name) = &Apache::lonnet::whichuser(); my $myself=0; if ( ($name eq $env{'user.name'}) && ($domain eq $env{'user.domain'}) ) { $myself=1; @@ -340,7 +341,7 @@ sub end_dataresponse { if ( $target eq 'web' ) { } elsif ($target eq 'grade' ) { if ( defined $env{'form.submitted'}) { - my ($symb,$courseid,$domain,$name)=&Apache::lonxml::whichuser(); + my ($symb,$courseid,$domain,$name)=&Apache::lonnet::whichuser(); my $allowed=&Apache::lonnet::allowed('mgr',$courseid); if ($allowed) { &Apache::response::setup_params('dataresponse',$safeeval); @@ -373,7 +374,7 @@ sub start_customresponse { if ( &Apache::response::show_answer() ) { my $answer = &Apache::lonxml::get_param('answerdisplay',$parstack, $safeeval); - $Apache::inputtags::answertxt{$id}=$answer; + $Apache::inputtags::answertxt{$id}=[$answer]; } } elsif ($target eq 'edit') { $result.=&Apache::edit::tag_start($target,$token); @@ -464,17 +465,23 @@ sub start_mathresponse { if ( &Apache::response::show_answer() ) { my $answer = &Apache::lonxml::get_param('answerdisplay',$parstack, $safeeval); - $Apache::inputtags::answertxt{$id}=$answer; + $Apache::inputtags::answertxt{$id}=[$answer]; } } elsif ($target eq 'edit') { $result.=&Apache::edit::tag_start($target,$token); $result.=&Apache::edit::text_arg('String to display for answer:', 'answerdisplay',$token); + $result.=&Apache::edit::select_arg('Algebra System:', + 'cas', + ['maxima'], + $token); + $result.=&Apache::edit::text_arg('Argument Array:', + 'args',$token); $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row(); } elsif ($target eq 'modified') { my $constructtag; $constructtag=&Apache::edit::get_new_args($token,$parstack, - $safeeval,'answerdisplay'); + $safeeval,'answerdisplay','cas','args'); if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); $result.=&Apache::edit::handle_insert(); @@ -494,8 +501,7 @@ sub end_mathresponse { my $id=$Apache::inputtags::response[-1]; if ( $target eq 'grade' && &Apache::response::submitted() ) { my $response = &Apache::response::getresponse(); - if ( $response =~ /[^\s]/ && - $Apache::response::custom_answer_type eq 'loncapa/perl') { + if ( $response =~ /[^\s]/ ) { if (!$Apache::lonxml::default_homework_loaded) { &Apache::lonxml::default_homework_load($safeeval); } @@ -504,10 +510,12 @@ sub end_mathresponse { $Apache::lonhomework::results{"resource.$part.$id.submission"}= $response; my $error; - ${$safeeval->varglob('LONCAPA::mathresponse_submission')}= - $response; - - my $award = &Apache::run::run('{ my $submission=$LONCAPA::mathresponse_submission;'.$Apache::response::custom_answer.'}',$safeeval); + my $award; + my $cas = &Apache::lonxml::get_param('cas',$parstack,$safeeval); + if ($cas eq 'maxima') { + my $args = [&Apache::lonxml::get_param_var('args',$parstack,$safeeval)]; + $award=&Apache::lonmaxima::maxima_run($Apache::response::custom_answer,$response,$args); + } if (!&Apache::inputtags::valid_award($award)) { $error = $award; $award = 'ERROR'; @@ -527,6 +535,28 @@ sub end_mathresponse { return $result; } +sub implicit_multiplication { + my ($expression)=@_; +# Escape scientific notation, so 3e8 does not become 3*e*8 +# 3e8 -> 3&8; 3e-8 -> 3&-8; 3E+8 -> e&+8 + $expression=~s/(\d+)e([\+\-]*\d+)/$1\&\($2\)/gsi; +# 3x10^8 -> 3&8; 3*10^-8 -> 3&-8 + $expression=~s/(\d+)(?:x|\*)10(?:\^|\*\*)([\+\-]*\d+)/$1\&\($2\)/gsi; +# Fill in multiplication signs +# a b -> a*b;3 b -> 3*b;3 4 -> 3*4 + $expression=~s/(\w)\s+(\w)/$1\*$2/gs; +# )( -> )*(; ) ( -> )*( + $expression=~s/\)\s*\(/\)\*\(/gs; +# 3a -> 3*a; 3( -> 3*(; 3 ( -> 3*(; 3A -> 3*A + $expression=~s/(\d)\s*([a-zA-Z\(])/$1\*$2/gs; +# a ( -> a*( + $expression=~s/(\w)\s+\(/$1\*\(/gs; +# )a -> )*a; )3 -> )*3; ) 3 -> )*3 + $expression=~s/\)\s*(\w)/\)\*$1/gs; +# 3&8 -> 3e8; 3&-4 -> 3e-4 + $expression=~s/(\d+)\&\(([\+\-]*\d+)\)/$1e$2/gs; + return $expression; +} sub start_answer { my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_; @@ -776,7 +806,7 @@ sub answer_footer { sub showallfoils { if (defined($env{'form.showallfoils'})) { - my ($symb)=&Apache::lonxml::whichuser(); + my ($symb)=&Apache::lonnet::whichuser(); if (($env{'request.state'} eq 'construct') || ($env{'user.adv'} && $symb eq '') || ($Apache::lonhomework::viewgrades) ) { @@ -998,7 +1028,7 @@ sub submitted { } sub add_to_gradingqueue { - my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser(); + my ($symb,$courseid,$domain,$name) = &Apache::lonnet::whichuser(); if ( $courseid eq '' || $symb eq '' || $env{'request.state'} eq 'construct'